Greg Kroah-Hartman | 57262b8 | 2008-11-03 13:27:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Opticon USB barcode to serial driver |
| 3 | * |
Martin Jansen | 309a057 | 2011-02-24 14:50:16 +0100 | [diff] [blame] | 4 | * Copyright (C) 2011 Martin Jansen <martin.jansen@opticon.com> |
Greg Kroah-Hartman | 648d4e1 | 2009-02-06 18:30:56 -0800 | [diff] [blame] | 5 | * Copyright (C) 2008 - 2009 Greg Kroah-Hartman <gregkh@suse.de> |
| 6 | * Copyright (C) 2008 - 2009 Novell Inc. |
Greg Kroah-Hartman | 57262b8 | 2008-11-03 13:27:03 -0800 | [diff] [blame] | 7 | * |
| 8 | * This program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public License version |
| 10 | * 2 as published by the Free Software Foundation. |
| 11 | */ |
| 12 | |
| 13 | #include <linux/kernel.h> |
| 14 | #include <linux/init.h> |
| 15 | #include <linux/tty.h> |
| 16 | #include <linux/tty_driver.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 17 | #include <linux/slab.h> |
Greg Kroah-Hartman | 57262b8 | 2008-11-03 13:27:03 -0800 | [diff] [blame] | 18 | #include <linux/tty_flip.h> |
Greg Kroah-Hartman | faac64a | 2009-02-06 18:31:46 -0800 | [diff] [blame] | 19 | #include <linux/serial.h> |
Greg Kroah-Hartman | 57262b8 | 2008-11-03 13:27:03 -0800 | [diff] [blame] | 20 | #include <linux/module.h> |
| 21 | #include <linux/usb.h> |
| 22 | #include <linux/usb/serial.h> |
| 23 | #include <linux/uaccess.h> |
| 24 | |
Martin Jansen | 309a057 | 2011-02-24 14:50:16 +0100 | [diff] [blame] | 25 | #define CONTROL_RTS 0x02 |
| 26 | #define RESEND_CTS_STATE 0x03 |
| 27 | |
| 28 | /* max number of write urbs in flight */ |
| 29 | #define URB_UPPER_LIMIT 8 |
| 30 | |
| 31 | /* This driver works for the Opticon 1D barcode reader |
| 32 | * an examples of 1D barcode types are EAN, UPC, Code39, IATA etc.. */ |
| 33 | #define DRIVER_DESC "Opticon USB barcode to serial driver (1D)" |
| 34 | |
Németh Márton | 7d40d7e | 2010-01-10 15:34:24 +0100 | [diff] [blame] | 35 | static const struct usb_device_id id_table[] = { |
Greg Kroah-Hartman | 57262b8 | 2008-11-03 13:27:03 -0800 | [diff] [blame] | 36 | { USB_DEVICE(0x065a, 0x0009) }, |
| 37 | { }, |
| 38 | }; |
| 39 | MODULE_DEVICE_TABLE(usb, id_table); |
| 40 | |
| 41 | /* This structure holds all of the individual device information */ |
| 42 | struct opticon_private { |
Greg Kroah-Hartman | 57262b8 | 2008-11-03 13:27:03 -0800 | [diff] [blame] | 43 | struct urb *bulk_read_urb; |
Greg Kroah-Hartman | 57262b8 | 2008-11-03 13:27:03 -0800 | [diff] [blame] | 44 | spinlock_t lock; /* protects the following flags */ |
| 45 | bool throttled; |
| 46 | bool actually_throttled; |
| 47 | bool rts; |
Martin Jansen | 309a057 | 2011-02-24 14:50:16 +0100 | [diff] [blame] | 48 | bool cts; |
Greg Kroah-Hartman | 648d4e1 | 2009-02-06 18:30:56 -0800 | [diff] [blame] | 49 | int outstanding_urbs; |
Greg Kroah-Hartman | 57262b8 | 2008-11-03 13:27:03 -0800 | [diff] [blame] | 50 | }; |
| 51 | |
Greg Kroah-Hartman | 648d4e1 | 2009-02-06 18:30:56 -0800 | [diff] [blame] | 52 | |
Johan Hovold | 3280207 | 2012-11-18 13:23:35 +0100 | [diff] [blame^] | 53 | static void opticon_process_data_packet(struct usb_serial_port *port, |
| 54 | const unsigned char *buf, size_t len) |
| 55 | { |
| 56 | struct tty_struct *tty; |
| 57 | |
| 58 | tty = tty_port_tty_get(&port->port); |
| 59 | if (!tty) |
| 60 | return; |
| 61 | |
| 62 | tty_insert_flip_string(tty, buf, len); |
| 63 | tty_flip_buffer_push(tty); |
| 64 | tty_kref_put(tty); |
| 65 | } |
| 66 | |
| 67 | static void opticon_process_status_packet(struct usb_serial_port *port, |
| 68 | const unsigned char *buf, size_t len) |
| 69 | { |
| 70 | struct opticon_private *priv = usb_get_serial_port_data(port); |
| 71 | unsigned long flags; |
| 72 | |
| 73 | spin_lock_irqsave(&priv->lock, flags); |
| 74 | if (buf[0] == 0x00) |
| 75 | priv->cts = false; |
| 76 | else |
| 77 | priv->cts = true; |
| 78 | spin_unlock_irqrestore(&priv->lock, flags); |
| 79 | } |
| 80 | |
| 81 | static void opticon_process_read_urb(struct urb *urb) |
| 82 | { |
| 83 | struct usb_serial_port *port = urb->context; |
| 84 | const unsigned char *hdr = urb->transfer_buffer; |
| 85 | const unsigned char *data = hdr + 2; |
| 86 | size_t data_len = urb->actual_length - 2; |
| 87 | |
| 88 | if (urb->actual_length <= 2) { |
| 89 | dev_dbg(&port->dev, "malformed packet received: %d bytes\n", |
| 90 | urb->actual_length); |
| 91 | return; |
| 92 | } |
| 93 | /* |
| 94 | * Data from the device comes with a 2 byte header: |
| 95 | * |
| 96 | * <0x00><0x00>data... |
| 97 | * This is real data to be sent to the tty layer |
| 98 | * <0x00><0x01>level |
| 99 | * This is a CTS level change, the third byte is the CTS |
| 100 | * value (0 for low, 1 for high). |
| 101 | */ |
| 102 | if ((hdr[0] == 0x00) && (hdr[1] == 0x00)) { |
| 103 | opticon_process_data_packet(port, data, data_len); |
| 104 | } else if ((hdr[0] == 0x00) && (hdr[1] == 0x01)) { |
| 105 | opticon_process_status_packet(port, data, data_len); |
| 106 | } else { |
| 107 | dev_dbg(&port->dev, "unknown packet received: %02x %02x\n", |
| 108 | hdr[0], hdr[1]); |
| 109 | } |
| 110 | } |
Martin Jansen | 309a057 | 2011-02-24 14:50:16 +0100 | [diff] [blame] | 111 | |
| 112 | static void opticon_read_bulk_callback(struct urb *urb) |
Greg Kroah-Hartman | 57262b8 | 2008-11-03 13:27:03 -0800 | [diff] [blame] | 113 | { |
Johan Hovold | e32d82b | 2012-11-18 13:23:32 +0100 | [diff] [blame] | 114 | struct usb_serial_port *port = urb->context; |
| 115 | struct opticon_private *priv = usb_get_serial_port_data(port); |
Greg Kroah-Hartman | 57262b8 | 2008-11-03 13:27:03 -0800 | [diff] [blame] | 116 | unsigned char *data = urb->transfer_buffer; |
Greg Kroah-Hartman | 57262b8 | 2008-11-03 13:27:03 -0800 | [diff] [blame] | 117 | int status = urb->status; |
Greg Kroah-Hartman | 57262b8 | 2008-11-03 13:27:03 -0800 | [diff] [blame] | 118 | int result; |
Greg Kroah-Hartman | 57262b8 | 2008-11-03 13:27:03 -0800 | [diff] [blame] | 119 | |
Greg Kroah-Hartman | 57262b8 | 2008-11-03 13:27:03 -0800 | [diff] [blame] | 120 | switch (status) { |
| 121 | case 0: |
| 122 | /* success */ |
| 123 | break; |
| 124 | case -ECONNRESET: |
| 125 | case -ENOENT: |
| 126 | case -ESHUTDOWN: |
| 127 | /* this urb is terminated, clean up */ |
Johan Hovold | e29a773 | 2012-11-18 13:23:23 +0100 | [diff] [blame] | 128 | dev_dbg(&port->dev, "%s - urb shutting down with status: %d\n", |
Greg Kroah-Hartman | d44d9ab | 2012-09-13 17:18:19 -0700 | [diff] [blame] | 129 | __func__, status); |
Greg Kroah-Hartman | 57262b8 | 2008-11-03 13:27:03 -0800 | [diff] [blame] | 130 | return; |
| 131 | default: |
Johan Hovold | e29a773 | 2012-11-18 13:23:23 +0100 | [diff] [blame] | 132 | dev_dbg(&port->dev, "%s - nonzero urb status received: %d\n", |
Greg Kroah-Hartman | d44d9ab | 2012-09-13 17:18:19 -0700 | [diff] [blame] | 133 | __func__, status); |
Greg Kroah-Hartman | 57262b8 | 2008-11-03 13:27:03 -0800 | [diff] [blame] | 134 | goto exit; |
| 135 | } |
| 136 | |
Greg Kroah-Hartman | 59d33f2 | 2012-09-18 09:58:57 +0100 | [diff] [blame] | 137 | usb_serial_debug_data(&port->dev, __func__, urb->actual_length, data); |
Greg Kroah-Hartman | 57262b8 | 2008-11-03 13:27:03 -0800 | [diff] [blame] | 138 | |
Johan Hovold | 3280207 | 2012-11-18 13:23:35 +0100 | [diff] [blame^] | 139 | opticon_process_read_urb(urb); |
Greg Kroah-Hartman | 57262b8 | 2008-11-03 13:27:03 -0800 | [diff] [blame] | 140 | exit: |
| 141 | spin_lock(&priv->lock); |
| 142 | |
| 143 | /* Continue trying to always read if we should */ |
| 144 | if (!priv->throttled) { |
Alon Ziv | 97cd8dc | 2010-10-10 08:32:18 +0200 | [diff] [blame] | 145 | result = usb_submit_urb(priv->bulk_read_urb, GFP_ATOMIC); |
Greg Kroah-Hartman | 57262b8 | 2008-11-03 13:27:03 -0800 | [diff] [blame] | 146 | if (result) |
| 147 | dev_err(&port->dev, |
| 148 | "%s - failed resubmitting read urb, error %d\n", |
| 149 | __func__, result); |
| 150 | } else |
| 151 | priv->actually_throttled = true; |
| 152 | spin_unlock(&priv->lock); |
| 153 | } |
| 154 | |
Martin Jansen | 309a057 | 2011-02-24 14:50:16 +0100 | [diff] [blame] | 155 | static int send_control_msg(struct usb_serial_port *port, u8 requesttype, |
| 156 | u8 val) |
| 157 | { |
| 158 | struct usb_serial *serial = port->serial; |
| 159 | int retval; |
Johan Hovold | ea0dbeb | 2012-10-25 10:29:11 +0200 | [diff] [blame] | 160 | u8 *buffer; |
| 161 | |
| 162 | buffer = kzalloc(1, GFP_KERNEL); |
| 163 | if (!buffer) |
| 164 | return -ENOMEM; |
Martin Jansen | 309a057 | 2011-02-24 14:50:16 +0100 | [diff] [blame] | 165 | |
| 166 | buffer[0] = val; |
| 167 | /* Send the message to the vendor control endpoint |
| 168 | * of the connected device */ |
| 169 | retval = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), |
| 170 | requesttype, |
| 171 | USB_DIR_OUT|USB_TYPE_VENDOR|USB_RECIP_INTERFACE, |
| 172 | 0, 0, buffer, 1, 0); |
Johan Hovold | ea0dbeb | 2012-10-25 10:29:11 +0200 | [diff] [blame] | 173 | kfree(buffer); |
Martin Jansen | 309a057 | 2011-02-24 14:50:16 +0100 | [diff] [blame] | 174 | |
| 175 | return retval; |
| 176 | } |
| 177 | |
Alan Cox | a509a7e | 2009-09-19 13:13:26 -0700 | [diff] [blame] | 178 | static int opticon_open(struct tty_struct *tty, struct usb_serial_port *port) |
Greg Kroah-Hartman | 57262b8 | 2008-11-03 13:27:03 -0800 | [diff] [blame] | 179 | { |
Johan Hovold | 70d25ee | 2012-11-18 13:23:30 +0100 | [diff] [blame] | 180 | struct opticon_private *priv = usb_get_serial_port_data(port); |
Greg Kroah-Hartman | 57262b8 | 2008-11-03 13:27:03 -0800 | [diff] [blame] | 181 | unsigned long flags; |
| 182 | int result = 0; |
| 183 | |
Greg Kroah-Hartman | 57262b8 | 2008-11-03 13:27:03 -0800 | [diff] [blame] | 184 | spin_lock_irqsave(&priv->lock, flags); |
| 185 | priv->throttled = false; |
| 186 | priv->actually_throttled = false; |
Martin Jansen | 309a057 | 2011-02-24 14:50:16 +0100 | [diff] [blame] | 187 | priv->rts = false; |
Greg Kroah-Hartman | 57262b8 | 2008-11-03 13:27:03 -0800 | [diff] [blame] | 188 | spin_unlock_irqrestore(&priv->lock, flags); |
| 189 | |
Martin Jansen | 309a057 | 2011-02-24 14:50:16 +0100 | [diff] [blame] | 190 | /* Clear RTS line */ |
| 191 | send_control_msg(port, CONTROL_RTS, 0); |
| 192 | |
Martin Jansen | 309a057 | 2011-02-24 14:50:16 +0100 | [diff] [blame] | 193 | /* clear the halt status of the enpoint */ |
Johan Hovold | 3157fad | 2012-11-18 13:23:24 +0100 | [diff] [blame] | 194 | usb_clear_halt(port->serial->dev, priv->bulk_read_urb->pipe); |
Martin Jansen | 309a057 | 2011-02-24 14:50:16 +0100 | [diff] [blame] | 195 | |
Greg Kroah-Hartman | 57262b8 | 2008-11-03 13:27:03 -0800 | [diff] [blame] | 196 | result = usb_submit_urb(priv->bulk_read_urb, GFP_KERNEL); |
| 197 | if (result) |
| 198 | dev_err(&port->dev, |
| 199 | "%s - failed resubmitting read urb, error %d\n", |
| 200 | __func__, result); |
Martin Jansen | 309a057 | 2011-02-24 14:50:16 +0100 | [diff] [blame] | 201 | /* Request CTS line state, sometimes during opening the current |
| 202 | * CTS state can be missed. */ |
| 203 | send_control_msg(port, RESEND_CTS_STATE, 1); |
Greg Kroah-Hartman | 57262b8 | 2008-11-03 13:27:03 -0800 | [diff] [blame] | 204 | return result; |
| 205 | } |
| 206 | |
Alan Cox | 335f851 | 2009-06-11 12:26:29 +0100 | [diff] [blame] | 207 | static void opticon_close(struct usb_serial_port *port) |
Greg Kroah-Hartman | 57262b8 | 2008-11-03 13:27:03 -0800 | [diff] [blame] | 208 | { |
Johan Hovold | 70d25ee | 2012-11-18 13:23:30 +0100 | [diff] [blame] | 209 | struct opticon_private *priv = usb_get_serial_port_data(port); |
Greg Kroah-Hartman | 57262b8 | 2008-11-03 13:27:03 -0800 | [diff] [blame] | 210 | |
Greg Kroah-Hartman | 57262b8 | 2008-11-03 13:27:03 -0800 | [diff] [blame] | 211 | /* shutdown our urbs */ |
| 212 | usb_kill_urb(priv->bulk_read_urb); |
| 213 | } |
| 214 | |
Martin Jansen | 309a057 | 2011-02-24 14:50:16 +0100 | [diff] [blame] | 215 | static void opticon_write_control_callback(struct urb *urb) |
Greg Kroah-Hartman | 648d4e1 | 2009-02-06 18:30:56 -0800 | [diff] [blame] | 216 | { |
Johan Hovold | e32d82b | 2012-11-18 13:23:32 +0100 | [diff] [blame] | 217 | struct usb_serial_port *port = urb->context; |
| 218 | struct opticon_private *priv = usb_get_serial_port_data(port); |
Greg Kroah-Hartman | 648d4e1 | 2009-02-06 18:30:56 -0800 | [diff] [blame] | 219 | int status = urb->status; |
| 220 | unsigned long flags; |
| 221 | |
| 222 | /* free up the transfer buffer, as usb_free_urb() does not do this */ |
| 223 | kfree(urb->transfer_buffer); |
| 224 | |
Alon Ziv | 0d930e5 | 2010-10-10 08:32:19 +0200 | [diff] [blame] | 225 | /* setup packet may be set if we're using it for writing */ |
| 226 | kfree(urb->setup_packet); |
| 227 | |
Greg Kroah-Hartman | 648d4e1 | 2009-02-06 18:30:56 -0800 | [diff] [blame] | 228 | if (status) |
Johan Hovold | e32d82b | 2012-11-18 13:23:32 +0100 | [diff] [blame] | 229 | dev_dbg(&port->dev, |
Johan Hovold | e29a773 | 2012-11-18 13:23:23 +0100 | [diff] [blame] | 230 | "%s - non-zero urb status received: %d\n", |
Greg Kroah-Hartman | d44d9ab | 2012-09-13 17:18:19 -0700 | [diff] [blame] | 231 | __func__, status); |
Greg Kroah-Hartman | 648d4e1 | 2009-02-06 18:30:56 -0800 | [diff] [blame] | 232 | |
| 233 | spin_lock_irqsave(&priv->lock, flags); |
| 234 | --priv->outstanding_urbs; |
| 235 | spin_unlock_irqrestore(&priv->lock, flags); |
| 236 | |
Johan Hovold | e32d82b | 2012-11-18 13:23:32 +0100 | [diff] [blame] | 237 | usb_serial_port_softint(port); |
Greg Kroah-Hartman | 648d4e1 | 2009-02-06 18:30:56 -0800 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | static int opticon_write(struct tty_struct *tty, struct usb_serial_port *port, |
| 241 | const unsigned char *buf, int count) |
| 242 | { |
Johan Hovold | 70d25ee | 2012-11-18 13:23:30 +0100 | [diff] [blame] | 243 | struct opticon_private *priv = usb_get_serial_port_data(port); |
Greg Kroah-Hartman | 648d4e1 | 2009-02-06 18:30:56 -0800 | [diff] [blame] | 244 | struct usb_serial *serial = port->serial; |
| 245 | struct urb *urb; |
| 246 | unsigned char *buffer; |
| 247 | unsigned long flags; |
| 248 | int status; |
Martin Jansen | 309a057 | 2011-02-24 14:50:16 +0100 | [diff] [blame] | 249 | struct usb_ctrlrequest *dr; |
Greg Kroah-Hartman | 648d4e1 | 2009-02-06 18:30:56 -0800 | [diff] [blame] | 250 | |
Greg Kroah-Hartman | 648d4e1 | 2009-02-06 18:30:56 -0800 | [diff] [blame] | 251 | spin_lock_irqsave(&priv->lock, flags); |
| 252 | if (priv->outstanding_urbs > URB_UPPER_LIMIT) { |
| 253 | spin_unlock_irqrestore(&priv->lock, flags); |
Greg Kroah-Hartman | d44d9ab | 2012-09-13 17:18:19 -0700 | [diff] [blame] | 254 | dev_dbg(&port->dev, "%s - write limit hit\n", __func__); |
Greg Kroah-Hartman | 648d4e1 | 2009-02-06 18:30:56 -0800 | [diff] [blame] | 255 | return 0; |
| 256 | } |
| 257 | priv->outstanding_urbs++; |
| 258 | spin_unlock_irqrestore(&priv->lock, flags); |
| 259 | |
| 260 | buffer = kmalloc(count, GFP_ATOMIC); |
| 261 | if (!buffer) { |
| 262 | dev_err(&port->dev, "out of memory\n"); |
| 263 | count = -ENOMEM; |
Martin Jansen | 309a057 | 2011-02-24 14:50:16 +0100 | [diff] [blame] | 264 | |
Greg Kroah-Hartman | 648d4e1 | 2009-02-06 18:30:56 -0800 | [diff] [blame] | 265 | goto error_no_buffer; |
| 266 | } |
| 267 | |
| 268 | urb = usb_alloc_urb(0, GFP_ATOMIC); |
| 269 | if (!urb) { |
| 270 | dev_err(&port->dev, "no more free urbs\n"); |
| 271 | count = -ENOMEM; |
| 272 | goto error_no_urb; |
| 273 | } |
| 274 | |
| 275 | memcpy(buffer, buf, count); |
| 276 | |
Greg Kroah-Hartman | 59d33f2 | 2012-09-18 09:58:57 +0100 | [diff] [blame] | 277 | usb_serial_debug_data(&port->dev, __func__, count, buffer); |
Greg Kroah-Hartman | 648d4e1 | 2009-02-06 18:30:56 -0800 | [diff] [blame] | 278 | |
Martin Jansen | 309a057 | 2011-02-24 14:50:16 +0100 | [diff] [blame] | 279 | /* The conncected devices do not have a bulk write endpoint, |
| 280 | * to transmit data to de barcode device the control endpoint is used */ |
| 281 | dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_NOIO); |
Julia Lawall | b0795bb | 2011-05-13 17:30:46 +0200 | [diff] [blame] | 282 | if (!dr) { |
| 283 | dev_err(&port->dev, "out of memory\n"); |
| 284 | count = -ENOMEM; |
Johan Hovold | acbf0e5 | 2012-10-25 10:29:12 +0200 | [diff] [blame] | 285 | goto error_no_dr; |
Julia Lawall | b0795bb | 2011-05-13 17:30:46 +0200 | [diff] [blame] | 286 | } |
Alon Ziv | 0d930e5 | 2010-10-10 08:32:19 +0200 | [diff] [blame] | 287 | |
Martin Jansen | 309a057 | 2011-02-24 14:50:16 +0100 | [diff] [blame] | 288 | dr->bRequestType = USB_TYPE_VENDOR | USB_RECIP_INTERFACE | USB_DIR_OUT; |
| 289 | dr->bRequest = 0x01; |
| 290 | dr->wValue = 0; |
| 291 | dr->wIndex = 0; |
| 292 | dr->wLength = cpu_to_le16(count); |
Alon Ziv | 0d930e5 | 2010-10-10 08:32:19 +0200 | [diff] [blame] | 293 | |
Martin Jansen | 309a057 | 2011-02-24 14:50:16 +0100 | [diff] [blame] | 294 | usb_fill_control_urb(urb, serial->dev, |
| 295 | usb_sndctrlpipe(serial->dev, 0), |
| 296 | (unsigned char *)dr, buffer, count, |
Johan Hovold | e32d82b | 2012-11-18 13:23:32 +0100 | [diff] [blame] | 297 | opticon_write_control_callback, port); |
Greg Kroah-Hartman | 648d4e1 | 2009-02-06 18:30:56 -0800 | [diff] [blame] | 298 | |
| 299 | /* send it down the pipe */ |
| 300 | status = usb_submit_urb(urb, GFP_ATOMIC); |
| 301 | if (status) { |
| 302 | dev_err(&port->dev, |
Martin Jansen | 309a057 | 2011-02-24 14:50:16 +0100 | [diff] [blame] | 303 | "%s - usb_submit_urb(write endpoint) failed status = %d\n", |
Greg Kroah-Hartman | 648d4e1 | 2009-02-06 18:30:56 -0800 | [diff] [blame] | 304 | __func__, status); |
| 305 | count = status; |
| 306 | goto error; |
| 307 | } |
| 308 | |
| 309 | /* we are done with this urb, so let the host driver |
| 310 | * really free it when it is finished with it */ |
| 311 | usb_free_urb(urb); |
| 312 | |
| 313 | return count; |
| 314 | error: |
Johan Hovold | acbf0e5 | 2012-10-25 10:29:12 +0200 | [diff] [blame] | 315 | kfree(dr); |
| 316 | error_no_dr: |
Greg Kroah-Hartman | 648d4e1 | 2009-02-06 18:30:56 -0800 | [diff] [blame] | 317 | usb_free_urb(urb); |
| 318 | error_no_urb: |
| 319 | kfree(buffer); |
| 320 | error_no_buffer: |
| 321 | spin_lock_irqsave(&priv->lock, flags); |
| 322 | --priv->outstanding_urbs; |
| 323 | spin_unlock_irqrestore(&priv->lock, flags); |
| 324 | return count; |
| 325 | } |
| 326 | |
| 327 | static int opticon_write_room(struct tty_struct *tty) |
| 328 | { |
| 329 | struct usb_serial_port *port = tty->driver_data; |
Johan Hovold | 70d25ee | 2012-11-18 13:23:30 +0100 | [diff] [blame] | 330 | struct opticon_private *priv = usb_get_serial_port_data(port); |
Greg Kroah-Hartman | 648d4e1 | 2009-02-06 18:30:56 -0800 | [diff] [blame] | 331 | unsigned long flags; |
| 332 | |
Greg Kroah-Hartman | 648d4e1 | 2009-02-06 18:30:56 -0800 | [diff] [blame] | 333 | /* |
| 334 | * We really can take almost anything the user throws at us |
| 335 | * but let's pick a nice big number to tell the tty |
| 336 | * layer that we have lots of free space, unless we don't. |
| 337 | */ |
| 338 | spin_lock_irqsave(&priv->lock, flags); |
| 339 | if (priv->outstanding_urbs > URB_UPPER_LIMIT * 2 / 3) { |
| 340 | spin_unlock_irqrestore(&priv->lock, flags); |
Greg Kroah-Hartman | d44d9ab | 2012-09-13 17:18:19 -0700 | [diff] [blame] | 341 | dev_dbg(&port->dev, "%s - write limit hit\n", __func__); |
Greg Kroah-Hartman | 648d4e1 | 2009-02-06 18:30:56 -0800 | [diff] [blame] | 342 | return 0; |
| 343 | } |
| 344 | spin_unlock_irqrestore(&priv->lock, flags); |
| 345 | |
| 346 | return 2048; |
| 347 | } |
| 348 | |
Greg Kroah-Hartman | 57262b8 | 2008-11-03 13:27:03 -0800 | [diff] [blame] | 349 | static void opticon_throttle(struct tty_struct *tty) |
| 350 | { |
| 351 | struct usb_serial_port *port = tty->driver_data; |
Johan Hovold | 70d25ee | 2012-11-18 13:23:30 +0100 | [diff] [blame] | 352 | struct opticon_private *priv = usb_get_serial_port_data(port); |
Greg Kroah-Hartman | 57262b8 | 2008-11-03 13:27:03 -0800 | [diff] [blame] | 353 | unsigned long flags; |
| 354 | |
Greg Kroah-Hartman | 57262b8 | 2008-11-03 13:27:03 -0800 | [diff] [blame] | 355 | spin_lock_irqsave(&priv->lock, flags); |
| 356 | priv->throttled = true; |
| 357 | spin_unlock_irqrestore(&priv->lock, flags); |
| 358 | } |
| 359 | |
| 360 | |
| 361 | static void opticon_unthrottle(struct tty_struct *tty) |
| 362 | { |
| 363 | struct usb_serial_port *port = tty->driver_data; |
Johan Hovold | 70d25ee | 2012-11-18 13:23:30 +0100 | [diff] [blame] | 364 | struct opticon_private *priv = usb_get_serial_port_data(port); |
Greg Kroah-Hartman | 57262b8 | 2008-11-03 13:27:03 -0800 | [diff] [blame] | 365 | unsigned long flags; |
Oliver Neukum | 88fa659 | 2009-10-07 09:25:10 +0200 | [diff] [blame] | 366 | int result, was_throttled; |
Greg Kroah-Hartman | 57262b8 | 2008-11-03 13:27:03 -0800 | [diff] [blame] | 367 | |
Greg Kroah-Hartman | 57262b8 | 2008-11-03 13:27:03 -0800 | [diff] [blame] | 368 | spin_lock_irqsave(&priv->lock, flags); |
| 369 | priv->throttled = false; |
Oliver Neukum | 88fa659 | 2009-10-07 09:25:10 +0200 | [diff] [blame] | 370 | was_throttled = priv->actually_throttled; |
Greg Kroah-Hartman | 57262b8 | 2008-11-03 13:27:03 -0800 | [diff] [blame] | 371 | priv->actually_throttled = false; |
| 372 | spin_unlock_irqrestore(&priv->lock, flags); |
| 373 | |
Oliver Neukum | 88fa659 | 2009-10-07 09:25:10 +0200 | [diff] [blame] | 374 | if (was_throttled) { |
| 375 | result = usb_submit_urb(priv->bulk_read_urb, GFP_ATOMIC); |
| 376 | if (result) |
| 377 | dev_err(&port->dev, |
| 378 | "%s - failed submitting read urb, error %d\n", |
Greg Kroah-Hartman | 57262b8 | 2008-11-03 13:27:03 -0800 | [diff] [blame] | 379 | __func__, result); |
Oliver Neukum | 88fa659 | 2009-10-07 09:25:10 +0200 | [diff] [blame] | 380 | } |
Greg Kroah-Hartman | 57262b8 | 2008-11-03 13:27:03 -0800 | [diff] [blame] | 381 | } |
| 382 | |
Alan Cox | 60b33c1 | 2011-02-14 16:26:14 +0000 | [diff] [blame] | 383 | static int opticon_tiocmget(struct tty_struct *tty) |
Greg Kroah-Hartman | faac64a | 2009-02-06 18:31:46 -0800 | [diff] [blame] | 384 | { |
| 385 | struct usb_serial_port *port = tty->driver_data; |
Johan Hovold | 70d25ee | 2012-11-18 13:23:30 +0100 | [diff] [blame] | 386 | struct opticon_private *priv = usb_get_serial_port_data(port); |
Greg Kroah-Hartman | faac64a | 2009-02-06 18:31:46 -0800 | [diff] [blame] | 387 | unsigned long flags; |
| 388 | int result = 0; |
| 389 | |
Greg Kroah-Hartman | faac64a | 2009-02-06 18:31:46 -0800 | [diff] [blame] | 390 | spin_lock_irqsave(&priv->lock, flags); |
| 391 | if (priv->rts) |
Martin Jansen | 309a057 | 2011-02-24 14:50:16 +0100 | [diff] [blame] | 392 | result |= TIOCM_RTS; |
| 393 | if (priv->cts) |
| 394 | result |= TIOCM_CTS; |
Greg Kroah-Hartman | faac64a | 2009-02-06 18:31:46 -0800 | [diff] [blame] | 395 | spin_unlock_irqrestore(&priv->lock, flags); |
| 396 | |
Greg Kroah-Hartman | d44d9ab | 2012-09-13 17:18:19 -0700 | [diff] [blame] | 397 | dev_dbg(&port->dev, "%s - %x\n", __func__, result); |
Greg Kroah-Hartman | faac64a | 2009-02-06 18:31:46 -0800 | [diff] [blame] | 398 | return result; |
| 399 | } |
| 400 | |
Randy Dunlap | 4acfaf8 | 2011-04-03 11:42:00 -0700 | [diff] [blame] | 401 | static int opticon_tiocmset(struct tty_struct *tty, |
Martin Jansen | 309a057 | 2011-02-24 14:50:16 +0100 | [diff] [blame] | 402 | unsigned int set, unsigned int clear) |
| 403 | { |
| 404 | struct usb_serial_port *port = tty->driver_data; |
Johan Hovold | 81d5a67 | 2012-04-25 15:56:29 +0200 | [diff] [blame] | 405 | struct usb_serial *serial = port->serial; |
Johan Hovold | 70d25ee | 2012-11-18 13:23:30 +0100 | [diff] [blame] | 406 | struct opticon_private *priv = usb_get_serial_port_data(port); |
Martin Jansen | 309a057 | 2011-02-24 14:50:16 +0100 | [diff] [blame] | 407 | unsigned long flags; |
| 408 | bool rts; |
| 409 | bool changed = false; |
Johan Hovold | 81d5a67 | 2012-04-25 15:56:29 +0200 | [diff] [blame] | 410 | int ret; |
Martin Jansen | 309a057 | 2011-02-24 14:50:16 +0100 | [diff] [blame] | 411 | |
Martin Jansen | 309a057 | 2011-02-24 14:50:16 +0100 | [diff] [blame] | 412 | /* We only support RTS so we only handle that */ |
| 413 | spin_lock_irqsave(&priv->lock, flags); |
| 414 | |
| 415 | rts = priv->rts; |
| 416 | if (set & TIOCM_RTS) |
| 417 | priv->rts = true; |
| 418 | if (clear & TIOCM_RTS) |
| 419 | priv->rts = false; |
| 420 | changed = rts ^ priv->rts; |
| 421 | spin_unlock_irqrestore(&priv->lock, flags); |
| 422 | |
| 423 | if (!changed) |
| 424 | return 0; |
| 425 | |
| 426 | /* Send the new RTS state to the connected device */ |
Johan Hovold | 81d5a67 | 2012-04-25 15:56:29 +0200 | [diff] [blame] | 427 | mutex_lock(&serial->disc_mutex); |
| 428 | if (!serial->disconnected) |
| 429 | ret = send_control_msg(port, CONTROL_RTS, !rts); |
| 430 | else |
| 431 | ret = -ENODEV; |
| 432 | mutex_unlock(&serial->disc_mutex); |
| 433 | |
| 434 | return ret; |
Martin Jansen | 309a057 | 2011-02-24 14:50:16 +0100 | [diff] [blame] | 435 | } |
| 436 | |
Johan Hovold | 56be1a1 | 2012-11-18 13:23:31 +0100 | [diff] [blame] | 437 | static int get_serial_info(struct usb_serial_port *port, |
Greg Kroah-Hartman | faac64a | 2009-02-06 18:31:46 -0800 | [diff] [blame] | 438 | struct serial_struct __user *serial) |
| 439 | { |
| 440 | struct serial_struct tmp; |
| 441 | |
| 442 | if (!serial) |
| 443 | return -EFAULT; |
| 444 | |
| 445 | memset(&tmp, 0x00, sizeof(tmp)); |
| 446 | |
| 447 | /* fake emulate a 16550 uart to make userspace code happy */ |
| 448 | tmp.type = PORT_16550A; |
Johan Hovold | 56be1a1 | 2012-11-18 13:23:31 +0100 | [diff] [blame] | 449 | tmp.line = port->serial->minor; |
Greg Kroah-Hartman | faac64a | 2009-02-06 18:31:46 -0800 | [diff] [blame] | 450 | tmp.port = 0; |
| 451 | tmp.irq = 0; |
| 452 | tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ; |
| 453 | tmp.xmit_fifo_size = 1024; |
| 454 | tmp.baud_base = 9600; |
| 455 | tmp.close_delay = 5*HZ; |
| 456 | tmp.closing_wait = 30*HZ; |
| 457 | |
| 458 | if (copy_to_user(serial, &tmp, sizeof(*serial))) |
| 459 | return -EFAULT; |
| 460 | return 0; |
| 461 | } |
| 462 | |
Alan Cox | 00a0d0d | 2011-02-14 16:27:06 +0000 | [diff] [blame] | 463 | static int opticon_ioctl(struct tty_struct *tty, |
Greg Kroah-Hartman | faac64a | 2009-02-06 18:31:46 -0800 | [diff] [blame] | 464 | unsigned int cmd, unsigned long arg) |
| 465 | { |
| 466 | struct usb_serial_port *port = tty->driver_data; |
Greg Kroah-Hartman | faac64a | 2009-02-06 18:31:46 -0800 | [diff] [blame] | 467 | |
Greg Kroah-Hartman | d44d9ab | 2012-09-13 17:18:19 -0700 | [diff] [blame] | 468 | dev_dbg(&port->dev, "%s - port %d, cmd = 0x%x\n", __func__, port->number, cmd); |
Greg Kroah-Hartman | faac64a | 2009-02-06 18:31:46 -0800 | [diff] [blame] | 469 | |
| 470 | switch (cmd) { |
| 471 | case TIOCGSERIAL: |
Johan Hovold | 56be1a1 | 2012-11-18 13:23:31 +0100 | [diff] [blame] | 472 | return get_serial_info(port, |
Greg Kroah-Hartman | faac64a | 2009-02-06 18:31:46 -0800 | [diff] [blame] | 473 | (struct serial_struct __user *)arg); |
| 474 | } |
| 475 | |
| 476 | return -ENOIOCTLCMD; |
| 477 | } |
| 478 | |
Greg Kroah-Hartman | 57262b8 | 2008-11-03 13:27:03 -0800 | [diff] [blame] | 479 | static int opticon_startup(struct usb_serial *serial) |
| 480 | { |
Johan Hovold | a0a5fd9 | 2012-11-18 13:23:27 +0100 | [diff] [blame] | 481 | if (!serial->num_bulk_in) { |
| 482 | dev_err(&serial->dev->dev, "no bulk in endpoint\n"); |
| 483 | return -ENODEV; |
| 484 | } |
Greg Kroah-Hartman | 57262b8 | 2008-11-03 13:27:03 -0800 | [diff] [blame] | 485 | |
Johan Hovold | 70d25ee | 2012-11-18 13:23:30 +0100 | [diff] [blame] | 486 | return 0; |
| 487 | } |
| 488 | |
| 489 | static int opticon_port_probe(struct usb_serial_port *port) |
| 490 | { |
Johan Hovold | 70d25ee | 2012-11-18 13:23:30 +0100 | [diff] [blame] | 491 | struct opticon_private *priv; |
Johan Hovold | 70d25ee | 2012-11-18 13:23:30 +0100 | [diff] [blame] | 492 | |
Greg Kroah-Hartman | 57262b8 | 2008-11-03 13:27:03 -0800 | [diff] [blame] | 493 | priv = kzalloc(sizeof(*priv), GFP_KERNEL); |
Johan Hovold | 70d25ee | 2012-11-18 13:23:30 +0100 | [diff] [blame] | 494 | if (!priv) |
Greg Kroah-Hartman | 57262b8 | 2008-11-03 13:27:03 -0800 | [diff] [blame] | 495 | return -ENOMEM; |
Johan Hovold | 70d25ee | 2012-11-18 13:23:30 +0100 | [diff] [blame] | 496 | |
Greg Kroah-Hartman | 57262b8 | 2008-11-03 13:27:03 -0800 | [diff] [blame] | 497 | spin_lock_init(&priv->lock); |
Johan Hovold | 5ad4734 | 2012-11-18 13:23:34 +0100 | [diff] [blame] | 498 | priv->bulk_read_urb = port->read_urbs[0]; |
Johan Hovold | 0b8718a | 2012-11-18 13:23:22 +0100 | [diff] [blame] | 499 | |
Johan Hovold | 70d25ee | 2012-11-18 13:23:30 +0100 | [diff] [blame] | 500 | usb_set_serial_port_data(port, priv); |
Greg Kroah-Hartman | 57262b8 | 2008-11-03 13:27:03 -0800 | [diff] [blame] | 501 | |
Johan Hovold | 70d25ee | 2012-11-18 13:23:30 +0100 | [diff] [blame] | 502 | return 0; |
Greg Kroah-Hartman | 57262b8 | 2008-11-03 13:27:03 -0800 | [diff] [blame] | 503 | } |
| 504 | |
Johan Hovold | 70d25ee | 2012-11-18 13:23:30 +0100 | [diff] [blame] | 505 | static int opticon_port_remove(struct usb_serial_port *port) |
Alan Stern | f9c99bb | 2009-06-02 11:53:55 -0400 | [diff] [blame] | 506 | { |
Johan Hovold | 70d25ee | 2012-11-18 13:23:30 +0100 | [diff] [blame] | 507 | struct opticon_private *priv = usb_get_serial_port_data(port); |
Alan Stern | f9c99bb | 2009-06-02 11:53:55 -0400 | [diff] [blame] | 508 | |
Greg Kroah-Hartman | 57262b8 | 2008-11-03 13:27:03 -0800 | [diff] [blame] | 509 | kfree(priv); |
Johan Hovold | 70d25ee | 2012-11-18 13:23:30 +0100 | [diff] [blame] | 510 | |
| 511 | return 0; |
Greg Kroah-Hartman | 57262b8 | 2008-11-03 13:27:03 -0800 | [diff] [blame] | 512 | } |
| 513 | |
Greg Kroah-Hartman | d530296 | 2012-05-10 14:35:21 -0700 | [diff] [blame] | 514 | static int opticon_suspend(struct usb_serial *serial, pm_message_t message) |
Oliver Neukum | d1c0713 | 2009-01-14 18:34:06 +0100 | [diff] [blame] | 515 | { |
Johan Hovold | 70d25ee | 2012-11-18 13:23:30 +0100 | [diff] [blame] | 516 | struct opticon_private *priv; |
| 517 | |
| 518 | priv = usb_get_serial_port_data(serial->port[0]); |
Oliver Neukum | d1c0713 | 2009-01-14 18:34:06 +0100 | [diff] [blame] | 519 | |
| 520 | usb_kill_urb(priv->bulk_read_urb); |
| 521 | return 0; |
| 522 | } |
| 523 | |
Greg Kroah-Hartman | d530296 | 2012-05-10 14:35:21 -0700 | [diff] [blame] | 524 | static int opticon_resume(struct usb_serial *serial) |
Oliver Neukum | d1c0713 | 2009-01-14 18:34:06 +0100 | [diff] [blame] | 525 | { |
Oliver Neukum | d1c0713 | 2009-01-14 18:34:06 +0100 | [diff] [blame] | 526 | struct usb_serial_port *port = serial->port[0]; |
Johan Hovold | 70d25ee | 2012-11-18 13:23:30 +0100 | [diff] [blame] | 527 | struct opticon_private *priv = usb_get_serial_port_data(port); |
Oliver Neukum | d1c0713 | 2009-01-14 18:34:06 +0100 | [diff] [blame] | 528 | int result; |
| 529 | |
Alan Cox | 82fc594 | 2009-10-06 16:06:46 +0100 | [diff] [blame] | 530 | mutex_lock(&port->port.mutex); |
Alan Cox | 2a0785e | 2009-10-06 16:06:57 +0100 | [diff] [blame] | 531 | /* This is protected by the port mutex against close/open */ |
| 532 | if (test_bit(ASYNCB_INITIALIZED, &port->port.flags)) |
Oliver Neukum | d1c0713 | 2009-01-14 18:34:06 +0100 | [diff] [blame] | 533 | result = usb_submit_urb(priv->bulk_read_urb, GFP_NOIO); |
| 534 | else |
| 535 | result = 0; |
Alan Cox | 82fc594 | 2009-10-06 16:06:46 +0100 | [diff] [blame] | 536 | mutex_unlock(&port->port.mutex); |
Oliver Neukum | d1c0713 | 2009-01-14 18:34:06 +0100 | [diff] [blame] | 537 | return result; |
| 538 | } |
Greg Kroah-Hartman | 57262b8 | 2008-11-03 13:27:03 -0800 | [diff] [blame] | 539 | |
Greg Kroah-Hartman | 57262b8 | 2008-11-03 13:27:03 -0800 | [diff] [blame] | 540 | static struct usb_serial_driver opticon_device = { |
| 541 | .driver = { |
| 542 | .owner = THIS_MODULE, |
| 543 | .name = "opticon", |
| 544 | }, |
| 545 | .id_table = id_table, |
Greg Kroah-Hartman | 57262b8 | 2008-11-03 13:27:03 -0800 | [diff] [blame] | 546 | .num_ports = 1, |
Johan Hovold | 333396f | 2012-11-18 13:23:33 +0100 | [diff] [blame] | 547 | .bulk_in_size = 256, |
Greg Kroah-Hartman | 57262b8 | 2008-11-03 13:27:03 -0800 | [diff] [blame] | 548 | .attach = opticon_startup, |
Johan Hovold | 70d25ee | 2012-11-18 13:23:30 +0100 | [diff] [blame] | 549 | .port_probe = opticon_port_probe, |
| 550 | .port_remove = opticon_port_remove, |
Greg Kroah-Hartman | 57262b8 | 2008-11-03 13:27:03 -0800 | [diff] [blame] | 551 | .open = opticon_open, |
| 552 | .close = opticon_close, |
Greg Kroah-Hartman | 648d4e1 | 2009-02-06 18:30:56 -0800 | [diff] [blame] | 553 | .write = opticon_write, |
| 554 | .write_room = opticon_write_room, |
Greg Kroah-Hartman | 57262b8 | 2008-11-03 13:27:03 -0800 | [diff] [blame] | 555 | .throttle = opticon_throttle, |
| 556 | .unthrottle = opticon_unthrottle, |
Greg Kroah-Hartman | faac64a | 2009-02-06 18:31:46 -0800 | [diff] [blame] | 557 | .ioctl = opticon_ioctl, |
| 558 | .tiocmget = opticon_tiocmget, |
Martin Jansen | 309a057 | 2011-02-24 14:50:16 +0100 | [diff] [blame] | 559 | .tiocmset = opticon_tiocmset, |
Greg Kroah-Hartman | d530296 | 2012-05-10 14:35:21 -0700 | [diff] [blame] | 560 | .suspend = opticon_suspend, |
| 561 | .resume = opticon_resume, |
Johan Hovold | 5ad4734 | 2012-11-18 13:23:34 +0100 | [diff] [blame] | 562 | .read_bulk_callback = opticon_read_bulk_callback, |
Greg Kroah-Hartman | 57262b8 | 2008-11-03 13:27:03 -0800 | [diff] [blame] | 563 | }; |
| 564 | |
Alan Stern | f667dda | 2012-02-23 14:57:18 -0500 | [diff] [blame] | 565 | static struct usb_serial_driver * const serial_drivers[] = { |
| 566 | &opticon_device, NULL |
| 567 | }; |
| 568 | |
Greg Kroah-Hartman | 68e2411 | 2012-05-08 15:46:14 -0700 | [diff] [blame] | 569 | module_usb_serial_driver(serial_drivers, id_table); |
Greg Kroah-Hartman | 57262b8 | 2008-11-03 13:27:03 -0800 | [diff] [blame] | 570 | |
Martin Jansen | 309a057 | 2011-02-24 14:50:16 +0100 | [diff] [blame] | 571 | MODULE_DESCRIPTION(DRIVER_DESC); |
Greg Kroah-Hartman | 57262b8 | 2008-11-03 13:27:03 -0800 | [diff] [blame] | 572 | MODULE_LICENSE("GPL"); |