diff options
Diffstat (limited to 'drivers/usb/serial/opticon.c')
-rw-r--r-- | drivers/usb/serial/opticon.c | 351 |
1 files changed, 93 insertions, 258 deletions
diff --git a/drivers/usb/serial/opticon.c b/drivers/usb/serial/opticon.c index 6aba731d4864..c6bfb83efb1e 100644 --- a/drivers/usb/serial/opticon.c +++ b/drivers/usb/serial/opticon.c @@ -1,6 +1,7 @@ /* * Opticon USB barcode to serial driver * + * Copyright (C) 2011 - 2012 Johan Hovold <jhovold@gmail.com> * Copyright (C) 2011 Martin Jansen <martin.jansen@opticon.com> * Copyright (C) 2008 - 2009 Greg Kroah-Hartman <gregkh@suse.de> * Copyright (C) 2008 - 2009 Novell Inc. @@ -40,114 +41,70 @@ MODULE_DEVICE_TABLE(usb, id_table); /* This structure holds all of the individual device information */ struct opticon_private { - struct usb_device *udev; - struct usb_serial *serial; - struct usb_serial_port *port; - unsigned char *bulk_in_buffer; - struct urb *bulk_read_urb; - int buffer_size; - u8 bulk_address; spinlock_t lock; /* protects the following flags */ - bool throttled; - bool actually_throttled; bool rts; bool cts; int outstanding_urbs; }; - -static void opticon_read_bulk_callback(struct urb *urb) +static void opticon_process_data_packet(struct usb_serial_port *port, + const unsigned char *buf, size_t len) { - struct opticon_private *priv = urb->context; - unsigned char *data = urb->transfer_buffer; - struct usb_serial_port *port = priv->port; - int status = urb->status; struct tty_struct *tty; - int result; - int data_length; + + tty = tty_port_tty_get(&port->port); + if (!tty) + return; + + tty_insert_flip_string(tty, buf, len); + tty_flip_buffer_push(tty); + tty_kref_put(tty); +} + +static void opticon_process_status_packet(struct usb_serial_port *port, + const unsigned char *buf, size_t len) +{ + struct opticon_private *priv = usb_get_serial_port_data(port); unsigned long flags; - switch (status) { - case 0: - /* success */ - break; - case -ECONNRESET: - case -ENOENT: - case -ESHUTDOWN: - /* this urb is terminated, clean up */ - dev_dbg(&priv->udev->dev, "%s - urb shutting down with status: %d\n", - __func__, status); + spin_lock_irqsave(&priv->lock, flags); + if (buf[0] == 0x00) + priv->cts = false; + else + priv->cts = true; + spin_unlock_irqrestore(&priv->lock, flags); +} + +static void opticon_process_read_urb(struct urb *urb) +{ + struct usb_serial_port *port = urb->context; + const unsigned char *hdr = urb->transfer_buffer; + const unsigned char *data = hdr + 2; + size_t data_len = urb->actual_length - 2; + + if (urb->actual_length <= 2) { + dev_dbg(&port->dev, "malformed packet received: %d bytes\n", + urb->actual_length); return; - default: - dev_dbg(&priv->udev->dev, "%s - nonzero urb status received: %d\n", - __func__, status); - goto exit; } - - usb_serial_debug_data(&port->dev, __func__, urb->actual_length, data); - - if (urb->actual_length > 2) { - data_length = urb->actual_length - 2; - - /* - * Data from the device comes with a 2 byte header: - * - * <0x00><0x00>data... - * This is real data to be sent to the tty layer - * <0x00><0x01)level - * This is a CTS level change, the third byte is the CTS - * value (0 for low, 1 for high). - */ - if ((data[0] == 0x00) && (data[1] == 0x00)) { - /* real data, send it to the tty layer */ - tty = tty_port_tty_get(&port->port); - if (tty) { - tty_insert_flip_string(tty, data + 2, - data_length); - tty_flip_buffer_push(tty); - tty_kref_put(tty); - } - } else { - if ((data[0] == 0x00) && (data[1] == 0x01)) { - spin_lock_irqsave(&priv->lock, flags); - /* CTS status information package */ - if (data[2] == 0x00) - priv->cts = false; - else - priv->cts = true; - spin_unlock_irqrestore(&priv->lock, flags); - } else { - dev_dbg(&priv->udev->dev, - "Unknown data packet received from the device:" - " %2x %2x\n", - data[0], data[1]); - } - } + /* + * Data from the device comes with a 2 byte header: + * + * <0x00><0x00>data... + * This is real data to be sent to the tty layer + * <0x00><0x01>level + * This is a CTS level change, the third byte is the CTS + * value (0 for low, 1 for high). + */ + if ((hdr[0] == 0x00) && (hdr[1] == 0x00)) { + opticon_process_data_packet(port, data, data_len); + } else if ((hdr[0] == 0x00) && (hdr[1] == 0x01)) { + opticon_process_status_packet(port, data, data_len); } else { - dev_dbg(&priv->udev->dev, - "Improper amount of data received from the device, " - "%d bytes", urb->actual_length); + dev_dbg(&port->dev, "unknown packet received: %02x %02x\n", + hdr[0], hdr[1]); } - -exit: - spin_lock(&priv->lock); - - /* Continue trying to always read if we should */ - if (!priv->throttled) { - usb_fill_bulk_urb(priv->bulk_read_urb, priv->udev, - usb_rcvbulkpipe(priv->udev, - priv->bulk_address), - priv->bulk_in_buffer, priv->buffer_size, - opticon_read_bulk_callback, priv); - result = usb_submit_urb(priv->bulk_read_urb, GFP_ATOMIC); - if (result) - dev_err(&port->dev, - "%s - failed resubmitting read urb, error %d\n", - __func__, result); - } else - priv->actually_throttled = true; - spin_unlock(&priv->lock); } static int send_control_msg(struct usb_serial_port *port, u8 requesttype, @@ -175,52 +132,35 @@ static int send_control_msg(struct usb_serial_port *port, u8 requesttype, static int opticon_open(struct tty_struct *tty, struct usb_serial_port *port) { - struct opticon_private *priv = usb_get_serial_data(port->serial); + struct opticon_private *priv = usb_get_serial_port_data(port); unsigned long flags; - int result = 0; + int res; spin_lock_irqsave(&priv->lock, flags); - priv->throttled = false; - priv->actually_throttled = false; - priv->port = port; priv->rts = false; spin_unlock_irqrestore(&priv->lock, flags); /* Clear RTS line */ send_control_msg(port, CONTROL_RTS, 0); - /* Setup the read URB and start reading from the device */ - usb_fill_bulk_urb(priv->bulk_read_urb, priv->udev, - usb_rcvbulkpipe(priv->udev, - priv->bulk_address), - priv->bulk_in_buffer, priv->buffer_size, - opticon_read_bulk_callback, priv); - /* clear the halt status of the enpoint */ - usb_clear_halt(priv->udev, priv->bulk_read_urb->pipe); + usb_clear_halt(port->serial->dev, port->read_urb->pipe); + + res = usb_serial_generic_open(tty, port); + if (!res) + return res; - result = usb_submit_urb(priv->bulk_read_urb, GFP_KERNEL); - if (result) - dev_err(&port->dev, - "%s - failed resubmitting read urb, error %d\n", - __func__, result); /* Request CTS line state, sometimes during opening the current * CTS state can be missed. */ send_control_msg(port, RESEND_CTS_STATE, 1); - return result; -} - -static void opticon_close(struct usb_serial_port *port) -{ - struct opticon_private *priv = usb_get_serial_data(port->serial); - /* shutdown our urbs */ - usb_kill_urb(priv->bulk_read_urb); + return res; } static void opticon_write_control_callback(struct urb *urb) { - struct opticon_private *priv = urb->context; + struct usb_serial_port *port = urb->context; + struct opticon_private *priv = usb_get_serial_port_data(port); int status = urb->status; unsigned long flags; @@ -231,20 +171,21 @@ static void opticon_write_control_callback(struct urb *urb) kfree(urb->setup_packet); if (status) - dev_dbg(&priv->udev->dev, "%s - nonzero write bulk status received: %d\n", + dev_dbg(&port->dev, + "%s - non-zero urb status received: %d\n", __func__, status); spin_lock_irqsave(&priv->lock, flags); --priv->outstanding_urbs; spin_unlock_irqrestore(&priv->lock, flags); - usb_serial_port_softint(priv->port); + usb_serial_port_softint(port); } static int opticon_write(struct tty_struct *tty, struct usb_serial_port *port, const unsigned char *buf, int count) { - struct opticon_private *priv = usb_get_serial_data(port->serial); + struct opticon_private *priv = usb_get_serial_port_data(port); struct usb_serial *serial = port->serial; struct urb *urb; unsigned char *buffer; @@ -298,7 +239,7 @@ static int opticon_write(struct tty_struct *tty, struct usb_serial_port *port, usb_fill_control_urb(urb, serial->dev, usb_sndctrlpipe(serial->dev, 0), (unsigned char *)dr, buffer, count, - opticon_write_control_callback, priv); + opticon_write_control_callback, port); /* send it down the pipe */ status = usb_submit_urb(urb, GFP_ATOMIC); @@ -331,7 +272,7 @@ error_no_buffer: static int opticon_write_room(struct tty_struct *tty) { struct usb_serial_port *port = tty->driver_data; - struct opticon_private *priv = usb_get_serial_data(port->serial); + struct opticon_private *priv = usb_get_serial_port_data(port); unsigned long flags; /* @@ -350,44 +291,10 @@ static int opticon_write_room(struct tty_struct *tty) return 2048; } -static void opticon_throttle(struct tty_struct *tty) -{ - struct usb_serial_port *port = tty->driver_data; - struct opticon_private *priv = usb_get_serial_data(port->serial); - unsigned long flags; - - spin_lock_irqsave(&priv->lock, flags); - priv->throttled = true; - spin_unlock_irqrestore(&priv->lock, flags); -} - - -static void opticon_unthrottle(struct tty_struct *tty) -{ - struct usb_serial_port *port = tty->driver_data; - struct opticon_private *priv = usb_get_serial_data(port->serial); - unsigned long flags; - int result, was_throttled; - - spin_lock_irqsave(&priv->lock, flags); - priv->throttled = false; - was_throttled = priv->actually_throttled; - priv->actually_throttled = false; - spin_unlock_irqrestore(&priv->lock, flags); - - if (was_throttled) { - result = usb_submit_urb(priv->bulk_read_urb, GFP_ATOMIC); - if (result) - dev_err(&port->dev, - "%s - failed submitting read urb, error %d\n", - __func__, result); - } -} - static int opticon_tiocmget(struct tty_struct *tty) { struct usb_serial_port *port = tty->driver_data; - struct opticon_private *priv = usb_get_serial_data(port->serial); + struct opticon_private *priv = usb_get_serial_port_data(port); unsigned long flags; int result = 0; @@ -407,7 +314,7 @@ static int opticon_tiocmset(struct tty_struct *tty, { struct usb_serial_port *port = tty->driver_data; struct usb_serial *serial = port->serial; - struct opticon_private *priv = usb_get_serial_data(port->serial); + struct opticon_private *priv = usb_get_serial_port_data(port); unsigned long flags; bool rts; bool changed = false; @@ -438,7 +345,7 @@ static int opticon_tiocmset(struct tty_struct *tty, return ret; } -static int get_serial_info(struct opticon_private *priv, +static int get_serial_info(struct usb_serial_port *port, struct serial_struct __user *serial) { struct serial_struct tmp; @@ -450,7 +357,7 @@ static int get_serial_info(struct opticon_private *priv, /* fake emulate a 16550 uart to make userspace code happy */ tmp.type = PORT_16550A; - tmp.line = priv->serial->minor; + tmp.line = port->serial->minor; tmp.port = 0; tmp.irq = 0; tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ; @@ -468,13 +375,12 @@ static int opticon_ioctl(struct tty_struct *tty, unsigned int cmd, unsigned long arg) { struct usb_serial_port *port = tty->driver_data; - struct opticon_private *priv = usb_get_serial_data(port->serial); dev_dbg(&port->dev, "%s - port %d, cmd = 0x%x\n", __func__, port->number, cmd); switch (cmd) { case TIOCGSERIAL: - return get_serial_info(priv, + return get_serial_info(port, (struct serial_struct __user *)arg); } @@ -483,106 +389,36 @@ static int opticon_ioctl(struct tty_struct *tty, static int opticon_startup(struct usb_serial *serial) { - struct opticon_private *priv; - struct usb_host_interface *intf; - int i; - int retval = -ENOMEM; - bool bulk_in_found = false; - - /* create our private serial structure */ - priv = kzalloc(sizeof(*priv), GFP_KERNEL); - if (priv == NULL) { - dev_err(&serial->dev->dev, "%s - Out of memory\n", __func__); - return -ENOMEM; - } - spin_lock_init(&priv->lock); - priv->serial = serial; - priv->port = serial->port[0]; - priv->udev = serial->dev; - priv->outstanding_urbs = 0; /* Init the outstanding urbs */ - - /* find our bulk endpoint */ - intf = serial->interface->altsetting; - for (i = 0; i < intf->desc.bNumEndpoints; ++i) { - struct usb_endpoint_descriptor *endpoint; - - endpoint = &intf->endpoint[i].desc; - if (!usb_endpoint_is_bulk_in(endpoint)) - continue; - - priv->bulk_read_urb = usb_alloc_urb(0, GFP_KERNEL); - if (!priv->bulk_read_urb) { - dev_err(&priv->udev->dev, "out of memory\n"); - goto error; - } - - priv->buffer_size = usb_endpoint_maxp(endpoint) * 2; - priv->bulk_in_buffer = kmalloc(priv->buffer_size, GFP_KERNEL); - if (!priv->bulk_in_buffer) { - dev_err(&priv->udev->dev, "out of memory\n"); - goto error; - } - - priv->bulk_address = endpoint->bEndpointAddress; - - bulk_in_found = true; - break; - } - - if (!bulk_in_found) { - dev_err(&priv->udev->dev, - "Error - the proper endpoints were not found!\n"); - goto error; + if (!serial->num_bulk_in) { + dev_err(&serial->dev->dev, "no bulk in endpoint\n"); + return -ENODEV; } - usb_set_serial_data(serial, priv); return 0; - -error: - usb_free_urb(priv->bulk_read_urb); - kfree(priv->bulk_in_buffer); - kfree(priv); - return retval; } -static void opticon_disconnect(struct usb_serial *serial) +static int opticon_port_probe(struct usb_serial_port *port) { - struct opticon_private *priv = usb_get_serial_data(serial); - - usb_kill_urb(priv->bulk_read_urb); - usb_free_urb(priv->bulk_read_urb); -} + struct opticon_private *priv; -static void opticon_release(struct usb_serial *serial) -{ - struct opticon_private *priv = usb_get_serial_data(serial); + priv = kzalloc(sizeof(*priv), GFP_KERNEL); + if (!priv) + return -ENOMEM; - kfree(priv->bulk_in_buffer); - kfree(priv); -} + spin_lock_init(&priv->lock); -static int opticon_suspend(struct usb_serial *serial, pm_message_t message) -{ - struct opticon_private *priv = usb_get_serial_data(serial); + usb_set_serial_port_data(port, priv); - usb_kill_urb(priv->bulk_read_urb); return 0; } -static int opticon_resume(struct usb_serial *serial) +static int opticon_port_remove(struct usb_serial_port *port) { - struct opticon_private *priv = usb_get_serial_data(serial); - struct usb_serial_port *port = serial->port[0]; - int result; - - mutex_lock(&port->port.mutex); - /* This is protected by the port mutex against close/open */ - if (test_bit(ASYNCB_INITIALIZED, &port->port.flags)) - result = usb_submit_urb(priv->bulk_read_urb, GFP_NOIO); - else - result = 0; - mutex_unlock(&port->port.mutex); - return result; + struct opticon_private *priv = usb_get_serial_port_data(port); + + kfree(priv); + + return 0; } static struct usb_serial_driver opticon_device = { @@ -592,20 +428,19 @@ static struct usb_serial_driver opticon_device = { }, .id_table = id_table, .num_ports = 1, + .bulk_in_size = 256, .attach = opticon_startup, + .port_probe = opticon_port_probe, + .port_remove = opticon_port_remove, .open = opticon_open, - .close = opticon_close, .write = opticon_write, .write_room = opticon_write_room, - .disconnect = opticon_disconnect, - .release = opticon_release, - .throttle = opticon_throttle, - .unthrottle = opticon_unthrottle, + .throttle = usb_serial_generic_throttle, + .unthrottle = usb_serial_generic_unthrottle, .ioctl = opticon_ioctl, .tiocmget = opticon_tiocmget, .tiocmset = opticon_tiocmset, - .suspend = opticon_suspend, - .resume = opticon_resume, + .process_read_urb = opticon_process_read_urb, }; static struct usb_serial_driver * const serial_drivers[] = { |