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