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