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