Greg Kroah-Hartman | 68b44ea | 2009-02-13 17:25:46 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Symbol USB barcode to serial driver |
| 3 | * |
| 4 | * Copyright (C) 2009 Greg Kroah-Hartman <gregkh@suse.de> |
| 5 | * Copyright (C) 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> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 15 | #include <linux/slab.h> |
Greg Kroah-Hartman | 68b44ea | 2009-02-13 17:25:46 -0800 | [diff] [blame] | 16 | #include <linux/tty_driver.h> |
| 17 | #include <linux/tty_flip.h> |
| 18 | #include <linux/module.h> |
| 19 | #include <linux/usb.h> |
| 20 | #include <linux/usb/serial.h> |
| 21 | #include <linux/uaccess.h> |
| 22 | |
Németh Márton | 7d40d7e | 2010-01-10 15:34:24 +0100 | [diff] [blame] | 23 | static const struct usb_device_id id_table[] = { |
Greg Kroah-Hartman | 68b44ea | 2009-02-13 17:25:46 -0800 | [diff] [blame] | 24 | { USB_DEVICE(0x05e0, 0x0600) }, |
| 25 | { }, |
| 26 | }; |
| 27 | MODULE_DEVICE_TABLE(usb, id_table); |
| 28 | |
| 29 | /* This structure holds all of the individual device information */ |
| 30 | struct symbol_private { |
| 31 | struct usb_device *udev; |
| 32 | struct usb_serial *serial; |
| 33 | struct usb_serial_port *port; |
| 34 | unsigned char *int_buffer; |
| 35 | struct urb *int_urb; |
| 36 | int buffer_size; |
| 37 | u8 bInterval; |
| 38 | u8 int_address; |
| 39 | spinlock_t lock; /* protects the following flags */ |
| 40 | bool throttled; |
| 41 | bool actually_throttled; |
| 42 | bool rts; |
| 43 | }; |
| 44 | |
| 45 | static void symbol_int_callback(struct urb *urb) |
| 46 | { |
| 47 | struct symbol_private *priv = urb->context; |
| 48 | unsigned char *data = urb->transfer_buffer; |
| 49 | struct usb_serial_port *port = priv->port; |
| 50 | int status = urb->status; |
Greg Kroah-Hartman | 68b44ea | 2009-02-13 17:25:46 -0800 | [diff] [blame] | 51 | int result; |
Greg Kroah-Hartman | 68b44ea | 2009-02-13 17:25:46 -0800 | [diff] [blame] | 52 | int data_length; |
| 53 | |
Greg Kroah-Hartman | 68b44ea | 2009-02-13 17:25:46 -0800 | [diff] [blame] | 54 | switch (status) { |
| 55 | case 0: |
| 56 | /* success */ |
| 57 | break; |
| 58 | case -ECONNRESET: |
| 59 | case -ENOENT: |
| 60 | case -ESHUTDOWN: |
| 61 | /* this urb is terminated, clean up */ |
Greg Kroah-Hartman | e4083ea | 2012-05-15 16:27:32 -0700 | [diff] [blame] | 62 | dev_dbg(&port->dev, "%s - urb shutting down with status: %d\n", |
| 63 | __func__, status); |
Greg Kroah-Hartman | 68b44ea | 2009-02-13 17:25:46 -0800 | [diff] [blame] | 64 | return; |
| 65 | default: |
Greg Kroah-Hartman | e4083ea | 2012-05-15 16:27:32 -0700 | [diff] [blame] | 66 | dev_dbg(&port->dev, "%s - nonzero urb status received: %d\n", |
| 67 | __func__, status); |
Greg Kroah-Hartman | 68b44ea | 2009-02-13 17:25:46 -0800 | [diff] [blame] | 68 | goto exit; |
| 69 | } |
| 70 | |
Greg Kroah-Hartman | 59d33f2 | 2012-09-18 09:58:57 +0100 | [diff] [blame] | 71 | usb_serial_debug_data(&port->dev, __func__, urb->actual_length, data); |
Greg Kroah-Hartman | 68b44ea | 2009-02-13 17:25:46 -0800 | [diff] [blame] | 72 | |
| 73 | if (urb->actual_length > 1) { |
| 74 | data_length = urb->actual_length - 1; |
| 75 | |
| 76 | /* |
| 77 | * Data from the device comes with a 1 byte header: |
| 78 | * |
| 79 | * <size of data>data... |
| 80 | * This is real data to be sent to the tty layer |
| 81 | * we pretty much just ignore the size and send everything |
| 82 | * else to the tty layer. |
| 83 | */ |
Jiri Slaby | 2e124b4 | 2013-01-03 15:53:06 +0100 | [diff] [blame] | 84 | tty_insert_flip_string(&port->port, &data[1], data_length); |
| 85 | tty_flip_buffer_push(&port->port); |
Greg Kroah-Hartman | 68b44ea | 2009-02-13 17:25:46 -0800 | [diff] [blame] | 86 | } else { |
| 87 | dev_dbg(&priv->udev->dev, |
Uwe Kleine-König | 9ddc5b6 | 2010-01-20 17:02:24 +0100 | [diff] [blame] | 88 | "Improper amount of data received from the device, " |
Greg Kroah-Hartman | 68b44ea | 2009-02-13 17:25:46 -0800 | [diff] [blame] | 89 | "%d bytes", urb->actual_length); |
| 90 | } |
| 91 | |
| 92 | exit: |
| 93 | spin_lock(&priv->lock); |
| 94 | |
| 95 | /* Continue trying to always read if we should */ |
| 96 | if (!priv->throttled) { |
| 97 | usb_fill_int_urb(priv->int_urb, priv->udev, |
| 98 | usb_rcvintpipe(priv->udev, |
| 99 | priv->int_address), |
| 100 | priv->int_buffer, priv->buffer_size, |
| 101 | symbol_int_callback, priv, priv->bInterval); |
| 102 | result = usb_submit_urb(priv->int_urb, GFP_ATOMIC); |
| 103 | if (result) |
| 104 | dev_err(&port->dev, |
| 105 | "%s - failed resubmitting read urb, error %d\n", |
| 106 | __func__, result); |
| 107 | } else |
| 108 | priv->actually_throttled = true; |
| 109 | spin_unlock(&priv->lock); |
| 110 | } |
| 111 | |
Alan Cox | a509a7e | 2009-09-19 13:13:26 -0700 | [diff] [blame] | 112 | static int symbol_open(struct tty_struct *tty, struct usb_serial_port *port) |
Greg Kroah-Hartman | 68b44ea | 2009-02-13 17:25:46 -0800 | [diff] [blame] | 113 | { |
| 114 | struct symbol_private *priv = usb_get_serial_data(port->serial); |
| 115 | unsigned long flags; |
| 116 | int result = 0; |
| 117 | |
Greg Kroah-Hartman | 68b44ea | 2009-02-13 17:25:46 -0800 | [diff] [blame] | 118 | spin_lock_irqsave(&priv->lock, flags); |
| 119 | priv->throttled = false; |
| 120 | priv->actually_throttled = false; |
| 121 | priv->port = port; |
| 122 | spin_unlock_irqrestore(&priv->lock, flags); |
| 123 | |
Greg Kroah-Hartman | 68b44ea | 2009-02-13 17:25:46 -0800 | [diff] [blame] | 124 | /* Start reading from the device */ |
| 125 | usb_fill_int_urb(priv->int_urb, priv->udev, |
| 126 | usb_rcvintpipe(priv->udev, priv->int_address), |
| 127 | priv->int_buffer, priv->buffer_size, |
| 128 | symbol_int_callback, priv, priv->bInterval); |
| 129 | result = usb_submit_urb(priv->int_urb, GFP_KERNEL); |
| 130 | if (result) |
| 131 | dev_err(&port->dev, |
| 132 | "%s - failed resubmitting read urb, error %d\n", |
| 133 | __func__, result); |
| 134 | return result; |
| 135 | } |
| 136 | |
Alan Cox | 335f851 | 2009-06-11 12:26:29 +0100 | [diff] [blame] | 137 | static void symbol_close(struct usb_serial_port *port) |
Greg Kroah-Hartman | 68b44ea | 2009-02-13 17:25:46 -0800 | [diff] [blame] | 138 | { |
| 139 | struct symbol_private *priv = usb_get_serial_data(port->serial); |
| 140 | |
Greg Kroah-Hartman | 68b44ea | 2009-02-13 17:25:46 -0800 | [diff] [blame] | 141 | /* shutdown our urbs */ |
| 142 | usb_kill_urb(priv->int_urb); |
| 143 | } |
| 144 | |
| 145 | static void symbol_throttle(struct tty_struct *tty) |
| 146 | { |
| 147 | struct usb_serial_port *port = tty->driver_data; |
| 148 | struct symbol_private *priv = usb_get_serial_data(port->serial); |
Greg Kroah-Hartman | 68b44ea | 2009-02-13 17:25:46 -0800 | [diff] [blame] | 149 | |
Oliver Neukum | 6383251 | 2009-10-07 10:50:23 +0200 | [diff] [blame] | 150 | spin_lock_irq(&priv->lock); |
Greg Kroah-Hartman | 68b44ea | 2009-02-13 17:25:46 -0800 | [diff] [blame] | 151 | priv->throttled = true; |
Oliver Neukum | 6383251 | 2009-10-07 10:50:23 +0200 | [diff] [blame] | 152 | spin_unlock_irq(&priv->lock); |
Greg Kroah-Hartman | 68b44ea | 2009-02-13 17:25:46 -0800 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | static void symbol_unthrottle(struct tty_struct *tty) |
| 156 | { |
| 157 | struct usb_serial_port *port = tty->driver_data; |
| 158 | struct symbol_private *priv = usb_get_serial_data(port->serial); |
Greg Kroah-Hartman | 68b44ea | 2009-02-13 17:25:46 -0800 | [diff] [blame] | 159 | int result; |
Oliver Neukum | b2a5cf1 | 2009-10-07 09:30:49 +0200 | [diff] [blame] | 160 | bool was_throttled; |
Greg Kroah-Hartman | 68b44ea | 2009-02-13 17:25:46 -0800 | [diff] [blame] | 161 | |
Oliver Neukum | 6383251 | 2009-10-07 10:50:23 +0200 | [diff] [blame] | 162 | spin_lock_irq(&priv->lock); |
Greg Kroah-Hartman | 68b44ea | 2009-02-13 17:25:46 -0800 | [diff] [blame] | 163 | priv->throttled = false; |
Oliver Neukum | b2a5cf1 | 2009-10-07 09:30:49 +0200 | [diff] [blame] | 164 | was_throttled = priv->actually_throttled; |
Greg Kroah-Hartman | 68b44ea | 2009-02-13 17:25:46 -0800 | [diff] [blame] | 165 | priv->actually_throttled = false; |
Oliver Neukum | 6383251 | 2009-10-07 10:50:23 +0200 | [diff] [blame] | 166 | spin_unlock_irq(&priv->lock); |
Greg Kroah-Hartman | 68b44ea | 2009-02-13 17:25:46 -0800 | [diff] [blame] | 167 | |
Oliver Neukum | b2a5cf1 | 2009-10-07 09:30:49 +0200 | [diff] [blame] | 168 | if (was_throttled) { |
Oliver Neukum | 6383251 | 2009-10-07 10:50:23 +0200 | [diff] [blame] | 169 | result = usb_submit_urb(priv->int_urb, GFP_KERNEL); |
Oliver Neukum | b2a5cf1 | 2009-10-07 09:30:49 +0200 | [diff] [blame] | 170 | if (result) |
| 171 | dev_err(&port->dev, |
| 172 | "%s - failed submitting read urb, error %d\n", |
Greg Kroah-Hartman | 68b44ea | 2009-02-13 17:25:46 -0800 | [diff] [blame] | 173 | __func__, result); |
Oliver Neukum | b2a5cf1 | 2009-10-07 09:30:49 +0200 | [diff] [blame] | 174 | } |
Greg Kroah-Hartman | 68b44ea | 2009-02-13 17:25:46 -0800 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | static int symbol_startup(struct usb_serial *serial) |
| 178 | { |
| 179 | struct symbol_private *priv; |
| 180 | struct usb_host_interface *intf; |
| 181 | int i; |
| 182 | int retval = -ENOMEM; |
| 183 | bool int_in_found = false; |
| 184 | |
| 185 | /* create our private serial structure */ |
| 186 | priv = kzalloc(sizeof(*priv), GFP_KERNEL); |
| 187 | if (priv == NULL) { |
| 188 | dev_err(&serial->dev->dev, "%s - Out of memory\n", __func__); |
| 189 | return -ENOMEM; |
| 190 | } |
| 191 | spin_lock_init(&priv->lock); |
| 192 | priv->serial = serial; |
| 193 | priv->port = serial->port[0]; |
| 194 | priv->udev = serial->dev; |
| 195 | |
| 196 | /* find our interrupt endpoint */ |
| 197 | intf = serial->interface->altsetting; |
| 198 | for (i = 0; i < intf->desc.bNumEndpoints; ++i) { |
| 199 | struct usb_endpoint_descriptor *endpoint; |
| 200 | |
| 201 | endpoint = &intf->endpoint[i].desc; |
| 202 | if (!usb_endpoint_is_int_in(endpoint)) |
| 203 | continue; |
| 204 | |
| 205 | priv->int_urb = usb_alloc_urb(0, GFP_KERNEL); |
| 206 | if (!priv->int_urb) { |
| 207 | dev_err(&priv->udev->dev, "out of memory\n"); |
| 208 | goto error; |
| 209 | } |
| 210 | |
Kuninori Morimoto | 29cc889 | 2011-08-23 03:12:03 -0700 | [diff] [blame] | 211 | priv->buffer_size = usb_endpoint_maxp(endpoint) * 2; |
Greg Kroah-Hartman | 68b44ea | 2009-02-13 17:25:46 -0800 | [diff] [blame] | 212 | priv->int_buffer = kmalloc(priv->buffer_size, GFP_KERNEL); |
| 213 | if (!priv->int_buffer) { |
| 214 | dev_err(&priv->udev->dev, "out of memory\n"); |
| 215 | goto error; |
| 216 | } |
| 217 | |
| 218 | priv->int_address = endpoint->bEndpointAddress; |
| 219 | priv->bInterval = endpoint->bInterval; |
| 220 | |
| 221 | /* set up our int urb */ |
| 222 | usb_fill_int_urb(priv->int_urb, priv->udev, |
| 223 | usb_rcvintpipe(priv->udev, |
| 224 | endpoint->bEndpointAddress), |
| 225 | priv->int_buffer, priv->buffer_size, |
| 226 | symbol_int_callback, priv, priv->bInterval); |
| 227 | |
| 228 | int_in_found = true; |
| 229 | break; |
| 230 | } |
| 231 | |
| 232 | if (!int_in_found) { |
| 233 | dev_err(&priv->udev->dev, |
| 234 | "Error - the proper endpoints were not found!\n"); |
| 235 | goto error; |
| 236 | } |
| 237 | |
| 238 | usb_set_serial_data(serial, priv); |
| 239 | return 0; |
| 240 | |
| 241 | error: |
| 242 | usb_free_urb(priv->int_urb); |
| 243 | kfree(priv->int_buffer); |
| 244 | kfree(priv); |
| 245 | return retval; |
| 246 | } |
| 247 | |
Alan Stern | f9c99bb | 2009-06-02 11:53:55 -0400 | [diff] [blame] | 248 | static void symbol_disconnect(struct usb_serial *serial) |
Greg Kroah-Hartman | 68b44ea | 2009-02-13 17:25:46 -0800 | [diff] [blame] | 249 | { |
| 250 | struct symbol_private *priv = usb_get_serial_data(serial); |
| 251 | |
Greg Kroah-Hartman | 68b44ea | 2009-02-13 17:25:46 -0800 | [diff] [blame] | 252 | usb_kill_urb(priv->int_urb); |
| 253 | usb_free_urb(priv->int_urb); |
Alan Stern | f9c99bb | 2009-06-02 11:53:55 -0400 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | static void symbol_release(struct usb_serial *serial) |
| 257 | { |
| 258 | struct symbol_private *priv = usb_get_serial_data(serial); |
| 259 | |
Greg Kroah-Hartman | 68b44ea | 2009-02-13 17:25:46 -0800 | [diff] [blame] | 260 | kfree(priv->int_buffer); |
| 261 | kfree(priv); |
Greg Kroah-Hartman | 68b44ea | 2009-02-13 17:25:46 -0800 | [diff] [blame] | 262 | } |
| 263 | |
Greg Kroah-Hartman | 68b44ea | 2009-02-13 17:25:46 -0800 | [diff] [blame] | 264 | static struct usb_serial_driver symbol_device = { |
| 265 | .driver = { |
| 266 | .owner = THIS_MODULE, |
| 267 | .name = "symbol", |
| 268 | }, |
| 269 | .id_table = id_table, |
Greg Kroah-Hartman | 68b44ea | 2009-02-13 17:25:46 -0800 | [diff] [blame] | 270 | .num_ports = 1, |
| 271 | .attach = symbol_startup, |
| 272 | .open = symbol_open, |
| 273 | .close = symbol_close, |
Alan Stern | f9c99bb | 2009-06-02 11:53:55 -0400 | [diff] [blame] | 274 | .disconnect = symbol_disconnect, |
| 275 | .release = symbol_release, |
Greg Kroah-Hartman | 68b44ea | 2009-02-13 17:25:46 -0800 | [diff] [blame] | 276 | .throttle = symbol_throttle, |
| 277 | .unthrottle = symbol_unthrottle, |
Greg Kroah-Hartman | 68b44ea | 2009-02-13 17:25:46 -0800 | [diff] [blame] | 278 | }; |
| 279 | |
Alan Stern | d860322 | 2012-02-23 14:57:25 -0500 | [diff] [blame] | 280 | static struct usb_serial_driver * const serial_drivers[] = { |
| 281 | &symbol_device, NULL |
| 282 | }; |
| 283 | |
Greg Kroah-Hartman | 68e2411 | 2012-05-08 15:46:14 -0700 | [diff] [blame] | 284 | module_usb_serial_driver(serial_drivers, id_table); |
Greg Kroah-Hartman | 68b44ea | 2009-02-13 17:25:46 -0800 | [diff] [blame] | 285 | |
Greg Kroah-Hartman | 68b44ea | 2009-02-13 17:25:46 -0800 | [diff] [blame] | 286 | MODULE_LICENSE("GPL"); |