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) { |
Alon Ziv | 97cd8dc | 2010-10-10 08:32:18 +0200 | [diff] [blame] | 138 | result = usb_submit_urb(priv->bulk_read_urb, GFP_ATOMIC); |
Greg Kroah-Hartman | 57262b8 | 2008-11-03 13:27:03 -0800 | [diff] [blame] | 139 | if (result) |
| 140 | dev_err(&port->dev, |
| 141 | "%s - failed resubmitting read urb, error %d\n", |
| 142 | __func__, result); |
| 143 | } else |
| 144 | priv->actually_throttled = true; |
| 145 | spin_unlock(&priv->lock); |
| 146 | } |
| 147 | |
Martin Jansen | 309a057 | 2011-02-24 14:50:16 +0100 | [diff] [blame] | 148 | static int send_control_msg(struct usb_serial_port *port, u8 requesttype, |
| 149 | u8 val) |
| 150 | { |
| 151 | struct usb_serial *serial = port->serial; |
| 152 | int retval; |
Johan Hovold | ea0dbeb | 2012-10-25 10:29:11 +0200 | [diff] [blame] | 153 | u8 *buffer; |
| 154 | |
| 155 | buffer = kzalloc(1, GFP_KERNEL); |
| 156 | if (!buffer) |
| 157 | return -ENOMEM; |
Martin Jansen | 309a057 | 2011-02-24 14:50:16 +0100 | [diff] [blame] | 158 | |
| 159 | buffer[0] = val; |
| 160 | /* Send the message to the vendor control endpoint |
| 161 | * of the connected device */ |
| 162 | retval = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), |
| 163 | requesttype, |
| 164 | USB_DIR_OUT|USB_TYPE_VENDOR|USB_RECIP_INTERFACE, |
| 165 | 0, 0, buffer, 1, 0); |
Johan Hovold | ea0dbeb | 2012-10-25 10:29:11 +0200 | [diff] [blame] | 166 | kfree(buffer); |
Martin Jansen | 309a057 | 2011-02-24 14:50:16 +0100 | [diff] [blame] | 167 | |
| 168 | return retval; |
| 169 | } |
| 170 | |
Alan Cox | a509a7e | 2009-09-19 13:13:26 -0700 | [diff] [blame] | 171 | 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] | 172 | { |
| 173 | struct opticon_private *priv = usb_get_serial_data(port->serial); |
| 174 | unsigned long flags; |
| 175 | int result = 0; |
| 176 | |
Greg Kroah-Hartman | 57262b8 | 2008-11-03 13:27:03 -0800 | [diff] [blame] | 177 | spin_lock_irqsave(&priv->lock, flags); |
| 178 | priv->throttled = false; |
| 179 | priv->actually_throttled = false; |
| 180 | priv->port = port; |
Martin Jansen | 309a057 | 2011-02-24 14:50:16 +0100 | [diff] [blame] | 181 | priv->rts = false; |
Greg Kroah-Hartman | 57262b8 | 2008-11-03 13:27:03 -0800 | [diff] [blame] | 182 | spin_unlock_irqrestore(&priv->lock, flags); |
| 183 | |
Martin Jansen | 309a057 | 2011-02-24 14:50:16 +0100 | [diff] [blame] | 184 | /* Clear RTS line */ |
| 185 | send_control_msg(port, CONTROL_RTS, 0); |
| 186 | |
Martin Jansen | 309a057 | 2011-02-24 14:50:16 +0100 | [diff] [blame] | 187 | /* clear the halt status of the enpoint */ |
| 188 | usb_clear_halt(priv->udev, priv->bulk_read_urb->pipe); |
| 189 | |
Greg Kroah-Hartman | 57262b8 | 2008-11-03 13:27:03 -0800 | [diff] [blame] | 190 | result = usb_submit_urb(priv->bulk_read_urb, GFP_KERNEL); |
| 191 | if (result) |
| 192 | dev_err(&port->dev, |
| 193 | "%s - failed resubmitting read urb, error %d\n", |
| 194 | __func__, result); |
Martin Jansen | 309a057 | 2011-02-24 14:50:16 +0100 | [diff] [blame] | 195 | /* Request CTS line state, sometimes during opening the current |
| 196 | * CTS state can be missed. */ |
| 197 | send_control_msg(port, RESEND_CTS_STATE, 1); |
Greg Kroah-Hartman | 57262b8 | 2008-11-03 13:27:03 -0800 | [diff] [blame] | 198 | return result; |
| 199 | } |
| 200 | |
Alan Cox | 335f851 | 2009-06-11 12:26:29 +0100 | [diff] [blame] | 201 | static void opticon_close(struct usb_serial_port *port) |
Greg Kroah-Hartman | 57262b8 | 2008-11-03 13:27:03 -0800 | [diff] [blame] | 202 | { |
| 203 | struct opticon_private *priv = usb_get_serial_data(port->serial); |
| 204 | |
Greg Kroah-Hartman | 57262b8 | 2008-11-03 13:27:03 -0800 | [diff] [blame] | 205 | /* shutdown our urbs */ |
| 206 | usb_kill_urb(priv->bulk_read_urb); |
| 207 | } |
| 208 | |
Martin Jansen | 309a057 | 2011-02-24 14:50:16 +0100 | [diff] [blame] | 209 | static void opticon_write_control_callback(struct urb *urb) |
Greg Kroah-Hartman | 648d4e1 | 2009-02-06 18:30:56 -0800 | [diff] [blame] | 210 | { |
| 211 | struct opticon_private *priv = urb->context; |
| 212 | int status = urb->status; |
| 213 | unsigned long flags; |
| 214 | |
| 215 | /* free up the transfer buffer, as usb_free_urb() does not do this */ |
| 216 | kfree(urb->transfer_buffer); |
| 217 | |
Alon Ziv | 0d930e5 | 2010-10-10 08:32:19 +0200 | [diff] [blame] | 218 | /* setup packet may be set if we're using it for writing */ |
| 219 | kfree(urb->setup_packet); |
| 220 | |
Greg Kroah-Hartman | 648d4e1 | 2009-02-06 18:30:56 -0800 | [diff] [blame] | 221 | if (status) |
Greg Kroah-Hartman | d44d9ab | 2012-09-13 17:18:19 -0700 | [diff] [blame] | 222 | dev_dbg(&priv->udev->dev, "%s - nonzero write bulk status received: %d\n", |
| 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]; |
| 489 | priv->udev = serial->dev; |
Martin Jansen | 309a057 | 2011-02-24 14:50:16 +0100 | [diff] [blame] | 490 | priv->outstanding_urbs = 0; /* Init the outstanding urbs */ |
Greg Kroah-Hartman | 57262b8 | 2008-11-03 13:27:03 -0800 | [diff] [blame] | 491 | |
| 492 | /* find our bulk endpoint */ |
| 493 | intf = serial->interface->altsetting; |
| 494 | for (i = 0; i < intf->desc.bNumEndpoints; ++i) { |
| 495 | struct usb_endpoint_descriptor *endpoint; |
| 496 | |
| 497 | endpoint = &intf->endpoint[i].desc; |
| 498 | if (!usb_endpoint_is_bulk_in(endpoint)) |
| 499 | continue; |
| 500 | |
| 501 | priv->bulk_read_urb = usb_alloc_urb(0, GFP_KERNEL); |
| 502 | if (!priv->bulk_read_urb) { |
| 503 | dev_err(&priv->udev->dev, "out of memory\n"); |
| 504 | goto error; |
| 505 | } |
| 506 | |
Kuninori Morimoto | 29cc889 | 2011-08-23 03:12:03 -0700 | [diff] [blame] | 507 | priv->buffer_size = usb_endpoint_maxp(endpoint) * 2; |
Greg Kroah-Hartman | 57262b8 | 2008-11-03 13:27:03 -0800 | [diff] [blame] | 508 | priv->bulk_in_buffer = kmalloc(priv->buffer_size, GFP_KERNEL); |
| 509 | if (!priv->bulk_in_buffer) { |
| 510 | dev_err(&priv->udev->dev, "out of memory\n"); |
| 511 | goto error; |
| 512 | } |
| 513 | |
| 514 | priv->bulk_address = endpoint->bEndpointAddress; |
| 515 | |
Greg Kroah-Hartman | 57262b8 | 2008-11-03 13:27:03 -0800 | [diff] [blame] | 516 | bulk_in_found = true; |
| 517 | break; |
| 518 | } |
| 519 | |
| 520 | if (!bulk_in_found) { |
| 521 | dev_err(&priv->udev->dev, |
| 522 | "Error - the proper endpoints were not found!\n"); |
| 523 | goto error; |
| 524 | } |
| 525 | |
Johan Hovold | 0b8718a | 2012-11-18 13:23:22 +0100 | [diff] [blame^] | 526 | usb_fill_bulk_urb(priv->bulk_read_urb, serial->dev, |
| 527 | usb_rcvbulkpipe(serial->dev, |
| 528 | priv->bulk_address), |
| 529 | priv->bulk_in_buffer, priv->buffer_size, |
| 530 | opticon_read_bulk_callback, priv); |
| 531 | |
Greg Kroah-Hartman | 57262b8 | 2008-11-03 13:27:03 -0800 | [diff] [blame] | 532 | usb_set_serial_data(serial, priv); |
| 533 | return 0; |
| 534 | |
| 535 | error: |
| 536 | usb_free_urb(priv->bulk_read_urb); |
| 537 | kfree(priv->bulk_in_buffer); |
| 538 | kfree(priv); |
| 539 | return retval; |
| 540 | } |
| 541 | |
Alan Stern | f9c99bb | 2009-06-02 11:53:55 -0400 | [diff] [blame] | 542 | static void opticon_disconnect(struct usb_serial *serial) |
Greg Kroah-Hartman | 57262b8 | 2008-11-03 13:27:03 -0800 | [diff] [blame] | 543 | { |
| 544 | struct opticon_private *priv = usb_get_serial_data(serial); |
| 545 | |
Greg Kroah-Hartman | 57262b8 | 2008-11-03 13:27:03 -0800 | [diff] [blame] | 546 | usb_kill_urb(priv->bulk_read_urb); |
| 547 | usb_free_urb(priv->bulk_read_urb); |
Alan Stern | f9c99bb | 2009-06-02 11:53:55 -0400 | [diff] [blame] | 548 | } |
| 549 | |
| 550 | static void opticon_release(struct usb_serial *serial) |
| 551 | { |
| 552 | struct opticon_private *priv = usb_get_serial_data(serial); |
| 553 | |
Greg Kroah-Hartman | 57262b8 | 2008-11-03 13:27:03 -0800 | [diff] [blame] | 554 | kfree(priv->bulk_in_buffer); |
| 555 | kfree(priv); |
Greg Kroah-Hartman | 57262b8 | 2008-11-03 13:27:03 -0800 | [diff] [blame] | 556 | } |
| 557 | |
Greg Kroah-Hartman | d530296 | 2012-05-10 14:35:21 -0700 | [diff] [blame] | 558 | static int opticon_suspend(struct usb_serial *serial, pm_message_t message) |
Oliver Neukum | d1c0713 | 2009-01-14 18:34:06 +0100 | [diff] [blame] | 559 | { |
Oliver Neukum | d1c0713 | 2009-01-14 18:34:06 +0100 | [diff] [blame] | 560 | struct opticon_private *priv = usb_get_serial_data(serial); |
| 561 | |
| 562 | usb_kill_urb(priv->bulk_read_urb); |
| 563 | return 0; |
| 564 | } |
| 565 | |
Greg Kroah-Hartman | d530296 | 2012-05-10 14:35:21 -0700 | [diff] [blame] | 566 | static int opticon_resume(struct usb_serial *serial) |
Oliver Neukum | d1c0713 | 2009-01-14 18:34:06 +0100 | [diff] [blame] | 567 | { |
Oliver Neukum | d1c0713 | 2009-01-14 18:34:06 +0100 | [diff] [blame] | 568 | struct opticon_private *priv = usb_get_serial_data(serial); |
| 569 | struct usb_serial_port *port = serial->port[0]; |
| 570 | int result; |
| 571 | |
Alan Cox | 82fc594 | 2009-10-06 16:06:46 +0100 | [diff] [blame] | 572 | mutex_lock(&port->port.mutex); |
Alan Cox | 2a0785e | 2009-10-06 16:06:57 +0100 | [diff] [blame] | 573 | /* This is protected by the port mutex against close/open */ |
| 574 | if (test_bit(ASYNCB_INITIALIZED, &port->port.flags)) |
Oliver Neukum | d1c0713 | 2009-01-14 18:34:06 +0100 | [diff] [blame] | 575 | result = usb_submit_urb(priv->bulk_read_urb, GFP_NOIO); |
| 576 | else |
| 577 | result = 0; |
Alan Cox | 82fc594 | 2009-10-06 16:06:46 +0100 | [diff] [blame] | 578 | mutex_unlock(&port->port.mutex); |
Oliver Neukum | d1c0713 | 2009-01-14 18:34:06 +0100 | [diff] [blame] | 579 | return result; |
| 580 | } |
Greg Kroah-Hartman | 57262b8 | 2008-11-03 13:27:03 -0800 | [diff] [blame] | 581 | |
Greg Kroah-Hartman | 57262b8 | 2008-11-03 13:27:03 -0800 | [diff] [blame] | 582 | static struct usb_serial_driver opticon_device = { |
| 583 | .driver = { |
| 584 | .owner = THIS_MODULE, |
| 585 | .name = "opticon", |
| 586 | }, |
| 587 | .id_table = id_table, |
Greg Kroah-Hartman | 57262b8 | 2008-11-03 13:27:03 -0800 | [diff] [blame] | 588 | .num_ports = 1, |
| 589 | .attach = opticon_startup, |
| 590 | .open = opticon_open, |
| 591 | .close = opticon_close, |
Greg Kroah-Hartman | 648d4e1 | 2009-02-06 18:30:56 -0800 | [diff] [blame] | 592 | .write = opticon_write, |
| 593 | .write_room = opticon_write_room, |
Alan Stern | f9c99bb | 2009-06-02 11:53:55 -0400 | [diff] [blame] | 594 | .disconnect = opticon_disconnect, |
| 595 | .release = opticon_release, |
Greg Kroah-Hartman | 57262b8 | 2008-11-03 13:27:03 -0800 | [diff] [blame] | 596 | .throttle = opticon_throttle, |
| 597 | .unthrottle = opticon_unthrottle, |
Greg Kroah-Hartman | faac64a | 2009-02-06 18:31:46 -0800 | [diff] [blame] | 598 | .ioctl = opticon_ioctl, |
| 599 | .tiocmget = opticon_tiocmget, |
Martin Jansen | 309a057 | 2011-02-24 14:50:16 +0100 | [diff] [blame] | 600 | .tiocmset = opticon_tiocmset, |
Greg Kroah-Hartman | d530296 | 2012-05-10 14:35:21 -0700 | [diff] [blame] | 601 | .suspend = opticon_suspend, |
| 602 | .resume = opticon_resume, |
Greg Kroah-Hartman | 57262b8 | 2008-11-03 13:27:03 -0800 | [diff] [blame] | 603 | }; |
| 604 | |
Alan Stern | f667dda | 2012-02-23 14:57:18 -0500 | [diff] [blame] | 605 | static struct usb_serial_driver * const serial_drivers[] = { |
| 606 | &opticon_device, NULL |
| 607 | }; |
| 608 | |
Greg Kroah-Hartman | 68e2411 | 2012-05-08 15:46:14 -0700 | [diff] [blame] | 609 | module_usb_serial_driver(serial_drivers, id_table); |
Greg Kroah-Hartman | 57262b8 | 2008-11-03 13:27:03 -0800 | [diff] [blame] | 610 | |
Martin Jansen | 309a057 | 2011-02-24 14:50:16 +0100 | [diff] [blame] | 611 | MODULE_DESCRIPTION(DRIVER_DESC); |
Greg Kroah-Hartman | 57262b8 | 2008-11-03 13:27:03 -0800 | [diff] [blame] | 612 | MODULE_LICENSE("GPL"); |