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