linux/drivers/usb/serial/opticon.c
<<
>>
Prefs
   1/*
   2 * Opticon USB barcode to serial driver
   3 *
   4 * Copyright (C) 2008 - 2009 Greg Kroah-Hartman <gregkh@suse.de>
   5 * Copyright (C) 2008 - 2009 Novell Inc.
   6 *
   7 *      This program is free software; you can redistribute it and/or
   8 *      modify it under the terms of the GNU General Public License version
   9 *      2 as published by the Free Software Foundation.
  10 */
  11
  12#include <linux/kernel.h>
  13#include <linux/init.h>
  14#include <linux/tty.h>
  15#include <linux/tty_driver.h>
  16#include <linux/tty_flip.h>
  17#include <linux/serial.h>
  18#include <linux/module.h>
  19#include <linux/usb.h>
  20#include <linux/usb/serial.h>
  21#include <linux/uaccess.h>
  22
  23static int debug;
  24
  25static struct usb_device_id id_table[] = {
  26        { USB_DEVICE(0x065a, 0x0009) },
  27        { },
  28};
  29MODULE_DEVICE_TABLE(usb, id_table);
  30
  31/* This structure holds all of the individual device information */
  32struct opticon_private {
  33        struct usb_device *udev;
  34        struct usb_serial *serial;
  35        struct usb_serial_port *port;
  36        unsigned char *bulk_in_buffer;
  37        struct urb *bulk_read_urb;
  38        int buffer_size;
  39        u8 bulk_address;
  40        spinlock_t lock;        /* protects the following flags */
  41        bool throttled;
  42        bool actually_throttled;
  43        bool rts;
  44        int outstanding_urbs;
  45};
  46
  47/* max number of write urbs in flight */
  48#define URB_UPPER_LIMIT 4
  49
  50static void opticon_bulk_callback(struct urb *urb)
  51{
  52        struct opticon_private *priv = urb->context;
  53        unsigned char *data = urb->transfer_buffer;
  54        struct usb_serial_port *port = priv->port;
  55        int status = urb->status;
  56        struct tty_struct *tty;
  57        int result;
  58        int available_room = 0;
  59        int data_length;
  60
  61        dbg("%s - port %d", __func__, port->number);
  62
  63        switch (status) {
  64        case 0:
  65                /* success */
  66                break;
  67        case -ECONNRESET:
  68        case -ENOENT:
  69        case -ESHUTDOWN:
  70                /* this urb is terminated, clean up */
  71                dbg("%s - urb shutting down with status: %d",
  72                    __func__, status);
  73                return;
  74        default:
  75                dbg("%s - nonzero urb status received: %d",
  76                    __func__, status);
  77                goto exit;
  78        }
  79
  80        usb_serial_debug_data(debug, &port->dev, __func__, urb->actual_length,
  81                              data);
  82
  83        if (urb->actual_length > 2) {
  84                data_length = urb->actual_length - 2;
  85
  86                /*
  87                 * Data from the device comes with a 2 byte header:
  88                 *
  89                 * <0x00><0x00>data...
  90                 *      This is real data to be sent to the tty layer
  91                 * <0x00><0x01)level
  92                 *      This is a RTS level change, the third byte is the RTS
  93                 *      value (0 for low, 1 for high).
  94                 */
  95                if ((data[0] == 0x00) && (data[1] == 0x00)) {
  96                        /* real data, send it to the tty layer */
  97                        tty = tty_port_tty_get(&port->port);
  98                        if (tty) {
  99                                available_room = tty_buffer_request_room(tty,
 100                                                                data_length);
 101                                if (available_room) {
 102                                        tty_insert_flip_string(tty, data,
 103                                                               available_room);
 104                                        tty_flip_buffer_push(tty);
 105                                }
 106                                tty_kref_put(tty);
 107                        }
 108                } else {
 109                        if ((data[0] == 0x00) && (data[1] == 0x01)) {
 110                                if (data[2] == 0x00)
 111                                        priv->rts = false;
 112                                else
 113                                        priv->rts = true;
 114                        } else {
 115                        dev_dbg(&priv->udev->dev,
 116                                "Unknown data packet received from the device:"
 117                                " %2x %2x\n",
 118                                data[0], data[1]);
 119                        }
 120                }
 121        } else {
 122                dev_dbg(&priv->udev->dev,
 123                        "Improper ammount of data received from the device, "
 124                        "%d bytes", urb->actual_length);
 125        }
 126
 127exit:
 128        spin_lock(&priv->lock);
 129
 130        /* Continue trying to always read if we should */
 131        if (!priv->throttled) {
 132                usb_fill_bulk_urb(priv->bulk_read_urb, priv->udev,
 133                                  usb_rcvbulkpipe(priv->udev,
 134                                                  priv->bulk_address),
 135                                  priv->bulk_in_buffer, priv->buffer_size,
 136                                  opticon_bulk_callback, priv);
 137                result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
 138                if (result)
 139                        dev_err(&port->dev,
 140                            "%s - failed resubmitting read urb, error %d\n",
 141                                                        __func__, result);
 142        } else
 143                priv->actually_throttled = true;
 144        spin_unlock(&priv->lock);
 145}
 146
 147static int opticon_open(struct tty_struct *tty, struct usb_serial_port *port)
 148{
 149        struct opticon_private *priv = usb_get_serial_data(port->serial);
 150        unsigned long flags;
 151        int result = 0;
 152
 153        dbg("%s - port %d", __func__, port->number);
 154
 155        spin_lock_irqsave(&priv->lock, flags);
 156        priv->throttled = false;
 157        priv->actually_throttled = false;
 158        priv->port = port;
 159        spin_unlock_irqrestore(&priv->lock, flags);
 160
 161        /* Start reading from the device */
 162        usb_fill_bulk_urb(priv->bulk_read_urb, priv->udev,
 163                          usb_rcvbulkpipe(priv->udev,
 164                                          priv->bulk_address),
 165                          priv->bulk_in_buffer, priv->buffer_size,
 166                          opticon_bulk_callback, priv);
 167        result = usb_submit_urb(priv->bulk_read_urb, GFP_KERNEL);
 168        if (result)
 169                dev_err(&port->dev,
 170                        "%s - failed resubmitting read urb, error %d\n",
 171                        __func__, result);
 172        return result;
 173}
 174
 175static void opticon_close(struct usb_serial_port *port)
 176{
 177        struct opticon_private *priv = usb_get_serial_data(port->serial);
 178
 179        dbg("%s - port %d", __func__, port->number);
 180
 181        /* shutdown our urbs */
 182        usb_kill_urb(priv->bulk_read_urb);
 183}
 184
 185static void opticon_write_bulk_callback(struct urb *urb)
 186{
 187        struct opticon_private *priv = urb->context;
 188        int status = urb->status;
 189        unsigned long flags;
 190
 191        /* free up the transfer buffer, as usb_free_urb() does not do this */
 192        kfree(urb->transfer_buffer);
 193
 194        if (status)
 195                dbg("%s - nonzero write bulk status received: %d",
 196                    __func__, status);
 197
 198        spin_lock_irqsave(&priv->lock, flags);
 199        --priv->outstanding_urbs;
 200        spin_unlock_irqrestore(&priv->lock, flags);
 201
 202        usb_serial_port_softint(priv->port);
 203}
 204
 205static int opticon_write(struct tty_struct *tty, struct usb_serial_port *port,
 206                         const unsigned char *buf, int count)
 207{
 208        struct opticon_private *priv = usb_get_serial_data(port->serial);
 209        struct usb_serial *serial = port->serial;
 210        struct urb *urb;
 211        unsigned char *buffer;
 212        unsigned long flags;
 213        int status;
 214
 215        dbg("%s - port %d", __func__, port->number);
 216
 217        spin_lock_irqsave(&priv->lock, flags);
 218        if (priv->outstanding_urbs > URB_UPPER_LIMIT) {
 219                spin_unlock_irqrestore(&priv->lock, flags);
 220                dbg("%s - write limit hit\n", __func__);
 221                return 0;
 222        }
 223        priv->outstanding_urbs++;
 224        spin_unlock_irqrestore(&priv->lock, flags);
 225
 226        buffer = kmalloc(count, GFP_ATOMIC);
 227        if (!buffer) {
 228                dev_err(&port->dev, "out of memory\n");
 229                count = -ENOMEM;
 230                goto error_no_buffer;
 231        }
 232
 233        urb = usb_alloc_urb(0, GFP_ATOMIC);
 234        if (!urb) {
 235                dev_err(&port->dev, "no more free urbs\n");
 236                count = -ENOMEM;
 237                goto error_no_urb;
 238        }
 239
 240        memcpy(buffer, buf, count);
 241
 242        usb_serial_debug_data(debug, &port->dev, __func__, count, buffer);
 243
 244        usb_fill_bulk_urb(urb, serial->dev,
 245                          usb_sndbulkpipe(serial->dev,
 246                                          port->bulk_out_endpointAddress),
 247                          buffer, count, opticon_write_bulk_callback, priv);
 248
 249        /* send it down the pipe */
 250        status = usb_submit_urb(urb, GFP_ATOMIC);
 251        if (status) {
 252                dev_err(&port->dev,
 253                   "%s - usb_submit_urb(write bulk) failed with status = %d\n",
 254                                                        __func__, status);
 255                count = status;
 256                goto error;
 257        }
 258
 259        /* we are done with this urb, so let the host driver
 260         * really free it when it is finished with it */
 261        usb_free_urb(urb);
 262
 263        return count;
 264error:
 265        usb_free_urb(urb);
 266error_no_urb:
 267        kfree(buffer);
 268error_no_buffer:
 269        spin_lock_irqsave(&priv->lock, flags);
 270        --priv->outstanding_urbs;
 271        spin_unlock_irqrestore(&priv->lock, flags);
 272        return count;
 273}
 274
 275static int opticon_write_room(struct tty_struct *tty)
 276{
 277        struct usb_serial_port *port = tty->driver_data;
 278        struct opticon_private *priv = usb_get_serial_data(port->serial);
 279        unsigned long flags;
 280
 281        dbg("%s - port %d", __func__, port->number);
 282
 283        /*
 284         * We really can take almost anything the user throws at us
 285         * but let's pick a nice big number to tell the tty
 286         * layer that we have lots of free space, unless we don't.
 287         */
 288        spin_lock_irqsave(&priv->lock, flags);
 289        if (priv->outstanding_urbs > URB_UPPER_LIMIT * 2 / 3) {
 290                spin_unlock_irqrestore(&priv->lock, flags);
 291                dbg("%s - write limit hit\n", __func__);
 292                return 0;
 293        }
 294        spin_unlock_irqrestore(&priv->lock, flags);
 295
 296        return 2048;
 297}
 298
 299static void opticon_throttle(struct tty_struct *tty)
 300{
 301        struct usb_serial_port *port = tty->driver_data;
 302        struct opticon_private *priv = usb_get_serial_data(port->serial);
 303        unsigned long flags;
 304
 305        dbg("%s - port %d", __func__, port->number);
 306        spin_lock_irqsave(&priv->lock, flags);
 307        priv->throttled = true;
 308        spin_unlock_irqrestore(&priv->lock, flags);
 309}
 310
 311
 312static void opticon_unthrottle(struct tty_struct *tty)
 313{
 314        struct usb_serial_port *port = tty->driver_data;
 315        struct opticon_private *priv = usb_get_serial_data(port->serial);
 316        unsigned long flags;
 317        int result, was_throttled;
 318
 319        dbg("%s - port %d", __func__, port->number);
 320
 321        spin_lock_irqsave(&priv->lock, flags);
 322        priv->throttled = false;
 323        was_throttled = priv->actually_throttled;
 324        priv->actually_throttled = false;
 325        spin_unlock_irqrestore(&priv->lock, flags);
 326
 327        priv->bulk_read_urb->dev = port->serial->dev;
 328        if (was_throttled) {
 329                result = usb_submit_urb(priv->bulk_read_urb, GFP_ATOMIC);
 330                if (result)
 331                        dev_err(&port->dev,
 332                                "%s - failed submitting read urb, error %d\n",
 333                                                        __func__, result);
 334        }
 335}
 336
 337static int opticon_tiocmget(struct tty_struct *tty, struct file *file)
 338{
 339        struct usb_serial_port *port = tty->driver_data;
 340        struct opticon_private *priv = usb_get_serial_data(port->serial);
 341        unsigned long flags;
 342        int result = 0;
 343
 344        dbg("%s - port %d", __func__, port->number);
 345
 346        spin_lock_irqsave(&priv->lock, flags);
 347        if (priv->rts)
 348                result = TIOCM_RTS;
 349        spin_unlock_irqrestore(&priv->lock, flags);
 350
 351        dbg("%s - %x", __func__, result);
 352        return result;
 353}
 354
 355static int get_serial_info(struct opticon_private *priv,
 356                           struct serial_struct __user *serial)
 357{
 358        struct serial_struct tmp;
 359
 360        if (!serial)
 361                return -EFAULT;
 362
 363        memset(&tmp, 0x00, sizeof(tmp));
 364
 365        /* fake emulate a 16550 uart to make userspace code happy */
 366        tmp.type                = PORT_16550A;
 367        tmp.line                = priv->serial->minor;
 368        tmp.port                = 0;
 369        tmp.irq                 = 0;
 370        tmp.flags               = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
 371        tmp.xmit_fifo_size      = 1024;
 372        tmp.baud_base           = 9600;
 373        tmp.close_delay         = 5*HZ;
 374        tmp.closing_wait        = 30*HZ;
 375
 376        if (copy_to_user(serial, &tmp, sizeof(*serial)))
 377                return -EFAULT;
 378        return 0;
 379}
 380
 381static int opticon_ioctl(struct tty_struct *tty, struct file *file,
 382                         unsigned int cmd, unsigned long arg)
 383{
 384        struct usb_serial_port *port = tty->driver_data;
 385        struct opticon_private *priv = usb_get_serial_data(port->serial);
 386
 387        dbg("%s - port %d, cmd = 0x%x", __func__, port->number, cmd);
 388
 389        switch (cmd) {
 390        case TIOCGSERIAL:
 391                return get_serial_info(priv,
 392                                       (struct serial_struct __user *)arg);
 393        }
 394
 395        return -ENOIOCTLCMD;
 396}
 397
 398static int opticon_startup(struct usb_serial *serial)
 399{
 400        struct opticon_private *priv;
 401        struct usb_host_interface *intf;
 402        int i;
 403        int retval = -ENOMEM;
 404        bool bulk_in_found = false;
 405
 406        /* create our private serial structure */
 407        priv = kzalloc(sizeof(*priv), GFP_KERNEL);
 408        if (priv == NULL) {
 409                dev_err(&serial->dev->dev, "%s - Out of memory\n", __func__);
 410                return -ENOMEM;
 411        }
 412        spin_lock_init(&priv->lock);
 413        priv->serial = serial;
 414        priv->port = serial->port[0];
 415        priv->udev = serial->dev;
 416
 417        /* find our bulk endpoint */
 418        intf = serial->interface->altsetting;
 419        for (i = 0; i < intf->desc.bNumEndpoints; ++i) {
 420                struct usb_endpoint_descriptor *endpoint;
 421
 422                endpoint = &intf->endpoint[i].desc;
 423                if (!usb_endpoint_is_bulk_in(endpoint))
 424                        continue;
 425
 426                priv->bulk_read_urb = usb_alloc_urb(0, GFP_KERNEL);
 427                if (!priv->bulk_read_urb) {
 428                        dev_err(&priv->udev->dev, "out of memory\n");
 429                        goto error;
 430                }
 431
 432                priv->buffer_size = le16_to_cpu(endpoint->wMaxPacketSize) * 2;
 433                priv->bulk_in_buffer = kmalloc(priv->buffer_size, GFP_KERNEL);
 434                if (!priv->bulk_in_buffer) {
 435                        dev_err(&priv->udev->dev, "out of memory\n");
 436                        goto error;
 437                }
 438
 439                priv->bulk_address = endpoint->bEndpointAddress;
 440
 441                /* set up our bulk urb */
 442                usb_fill_bulk_urb(priv->bulk_read_urb, priv->udev,
 443                                  usb_rcvbulkpipe(priv->udev,
 444                                                  endpoint->bEndpointAddress),
 445                                  priv->bulk_in_buffer, priv->buffer_size,
 446                                  opticon_bulk_callback, priv);
 447
 448                bulk_in_found = true;
 449                break;
 450                }
 451
 452        if (!bulk_in_found) {
 453                dev_err(&priv->udev->dev,
 454                        "Error - the proper endpoints were not found!\n");
 455                goto error;
 456        }
 457
 458        usb_set_serial_data(serial, priv);
 459        return 0;
 460
 461error:
 462        usb_free_urb(priv->bulk_read_urb);
 463        kfree(priv->bulk_in_buffer);
 464        kfree(priv);
 465        return retval;
 466}
 467
 468static void opticon_disconnect(struct usb_serial *serial)
 469{
 470        struct opticon_private *priv = usb_get_serial_data(serial);
 471
 472        dbg("%s", __func__);
 473
 474        usb_kill_urb(priv->bulk_read_urb);
 475        usb_free_urb(priv->bulk_read_urb);
 476}
 477
 478static void opticon_release(struct usb_serial *serial)
 479{
 480        struct opticon_private *priv = usb_get_serial_data(serial);
 481
 482        dbg("%s", __func__);
 483
 484        kfree(priv->bulk_in_buffer);
 485        kfree(priv);
 486}
 487
 488static int opticon_suspend(struct usb_interface *intf, pm_message_t message)
 489{
 490        struct usb_serial *serial = usb_get_intfdata(intf);
 491        struct opticon_private *priv = usb_get_serial_data(serial);
 492
 493        usb_kill_urb(priv->bulk_read_urb);
 494        return 0;
 495}
 496
 497static int opticon_resume(struct usb_interface *intf)
 498{
 499        struct usb_serial *serial = usb_get_intfdata(intf);
 500        struct opticon_private *priv = usb_get_serial_data(serial);
 501        struct usb_serial_port *port = serial->port[0];
 502        int result;
 503
 504        mutex_lock(&port->mutex);
 505        if (port->port.count)
 506                result = usb_submit_urb(priv->bulk_read_urb, GFP_NOIO);
 507        else
 508                result = 0;
 509        mutex_unlock(&port->mutex);
 510        return result;
 511}
 512
 513static struct usb_driver opticon_driver = {
 514        .name =         "opticon",
 515        .probe =        usb_serial_probe,
 516        .disconnect =   usb_serial_disconnect,
 517        .suspend =      opticon_suspend,
 518        .resume =       opticon_resume,
 519        .id_table =     id_table,
 520        .no_dynamic_id =        1,
 521};
 522
 523static struct usb_serial_driver opticon_device = {
 524        .driver = {
 525                .owner =        THIS_MODULE,
 526                .name =         "opticon",
 527        },
 528        .id_table =             id_table,
 529        .usb_driver =           &opticon_driver,
 530        .num_ports =            1,
 531        .attach =               opticon_startup,
 532        .open =                 opticon_open,
 533        .close =                opticon_close,
 534        .write =                opticon_write,
 535        .write_room =           opticon_write_room,
 536        .disconnect =           opticon_disconnect,
 537        .release =              opticon_release,
 538        .throttle =             opticon_throttle,
 539        .unthrottle =           opticon_unthrottle,
 540        .ioctl =                opticon_ioctl,
 541        .tiocmget =             opticon_tiocmget,
 542};
 543
 544static int __init opticon_init(void)
 545{
 546        int retval;
 547
 548        retval = usb_serial_register(&opticon_device);
 549        if (retval)
 550                return retval;
 551        retval = usb_register(&opticon_driver);
 552        if (retval)
 553                usb_serial_deregister(&opticon_device);
 554        return retval;
 555}
 556
 557static void __exit opticon_exit(void)
 558{
 559        usb_deregister(&opticon_driver);
 560        usb_serial_deregister(&opticon_device);
 561}
 562
 563module_init(opticon_init);
 564module_exit(opticon_exit);
 565MODULE_LICENSE("GPL");
 566
 567module_param(debug, bool, S_IRUGO | S_IWUSR);
 568MODULE_PARM_DESC(debug, "Debug enabled or not");
 569