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