Greg Kroah-Hartman | 57262b8 | 2008-11-03 13:27:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Opticon USB barcode to serial driver |
| 3 | * |
Johan Hovold | 7a6ee2b | 2012-11-18 13:23:36 +0100 | [diff] [blame] | 4 | * Copyright (C) 2011 - 2012 Johan Hovold <jhovold@gmail.com> |
Martin Jansen | 309a057 | 2011-02-24 14:50:16 +0100 | [diff] [blame] | 5 | * Copyright (C) 2011 Martin Jansen <martin.jansen@opticon.com> |
Greg Kroah-Hartman | 648d4e1 | 2009-02-06 18:30:56 -0800 | [diff] [blame] | 6 | * Copyright (C) 2008 - 2009 Greg Kroah-Hartman <gregkh@suse.de> |
| 7 | * Copyright (C) 2008 - 2009 Novell Inc. |
Greg Kroah-Hartman | 57262b8 | 2008-11-03 13:27:03 -0800 | [diff] [blame] | 8 | * |
| 9 | * This program is free software; you can redistribute it and/or |
| 10 | * modify it under the terms of the GNU General Public License version |
| 11 | * 2 as published by the Free Software Foundation. |
| 12 | */ |
| 13 | |
| 14 | #include <linux/kernel.h> |
| 15 | #include <linux/init.h> |
| 16 | #include <linux/tty.h> |
| 17 | #include <linux/tty_driver.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 18 | #include <linux/slab.h> |
Greg Kroah-Hartman | 57262b8 | 2008-11-03 13:27:03 -0800 | [diff] [blame] | 19 | #include <linux/tty_flip.h> |
Greg Kroah-Hartman | faac64a | 2009-02-06 18:31:46 -0800 | [diff] [blame] | 20 | #include <linux/serial.h> |
Greg Kroah-Hartman | 57262b8 | 2008-11-03 13:27:03 -0800 | [diff] [blame] | 21 | #include <linux/module.h> |
| 22 | #include <linux/usb.h> |
| 23 | #include <linux/usb/serial.h> |
| 24 | #include <linux/uaccess.h> |
| 25 | |
Martin Jansen | 309a057 | 2011-02-24 14:50:16 +0100 | [diff] [blame] | 26 | #define CONTROL_RTS 0x02 |
| 27 | #define RESEND_CTS_STATE 0x03 |
| 28 | |
| 29 | /* max number of write urbs in flight */ |
| 30 | #define URB_UPPER_LIMIT 8 |
| 31 | |
| 32 | /* This driver works for the Opticon 1D barcode reader |
| 33 | * an examples of 1D barcode types are EAN, UPC, Code39, IATA etc.. */ |
| 34 | #define DRIVER_DESC "Opticon USB barcode to serial driver (1D)" |
| 35 | |
Németh Márton | 7d40d7e | 2010-01-10 15:34:24 +0100 | [diff] [blame] | 36 | static const struct usb_device_id id_table[] = { |
Greg Kroah-Hartman | 57262b8 | 2008-11-03 13:27:03 -0800 | [diff] [blame] | 37 | { USB_DEVICE(0x065a, 0x0009) }, |
| 38 | { }, |
| 39 | }; |
| 40 | MODULE_DEVICE_TABLE(usb, id_table); |
| 41 | |
| 42 | /* This structure holds all of the individual device information */ |
| 43 | struct opticon_private { |
Greg Kroah-Hartman | 57262b8 | 2008-11-03 13:27:03 -0800 | [diff] [blame] | 44 | spinlock_t lock; /* protects the following flags */ |
Greg Kroah-Hartman | 57262b8 | 2008-11-03 13:27:03 -0800 | [diff] [blame] | 45 | bool rts; |
Martin Jansen | 309a057 | 2011-02-24 14:50:16 +0100 | [diff] [blame] | 46 | bool cts; |
Greg Kroah-Hartman | 648d4e1 | 2009-02-06 18:30:56 -0800 | [diff] [blame] | 47 | int outstanding_urbs; |
Greg Kroah-Hartman | 57262b8 | 2008-11-03 13:27:03 -0800 | [diff] [blame] | 48 | }; |
| 49 | |
Greg Kroah-Hartman | 648d4e1 | 2009-02-06 18:30:56 -0800 | [diff] [blame] | 50 | |
Johan Hovold | 3280207 | 2012-11-18 13:23:35 +0100 | [diff] [blame] | 51 | static void opticon_process_data_packet(struct usb_serial_port *port, |
| 52 | const unsigned char *buf, size_t len) |
| 53 | { |
Jiri Slaby | 05c7cd3 | 2013-01-03 15:53:04 +0100 | [diff] [blame] | 54 | tty_insert_flip_string(&port->port, buf, len); |
Jiri Slaby | 2e124b4 | 2013-01-03 15:53:06 +0100 | [diff] [blame] | 55 | tty_flip_buffer_push(&port->port); |
Johan Hovold | 3280207 | 2012-11-18 13:23:35 +0100 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | static void opticon_process_status_packet(struct usb_serial_port *port, |
| 59 | const unsigned char *buf, size_t len) |
| 60 | { |
| 61 | struct opticon_private *priv = usb_get_serial_port_data(port); |
| 62 | unsigned long flags; |
| 63 | |
| 64 | spin_lock_irqsave(&priv->lock, flags); |
| 65 | if (buf[0] == 0x00) |
| 66 | priv->cts = false; |
| 67 | else |
| 68 | priv->cts = true; |
| 69 | spin_unlock_irqrestore(&priv->lock, flags); |
| 70 | } |
| 71 | |
| 72 | static void opticon_process_read_urb(struct urb *urb) |
| 73 | { |
| 74 | struct usb_serial_port *port = urb->context; |
| 75 | const unsigned char *hdr = urb->transfer_buffer; |
| 76 | const unsigned char *data = hdr + 2; |
| 77 | size_t data_len = urb->actual_length - 2; |
| 78 | |
| 79 | if (urb->actual_length <= 2) { |
| 80 | dev_dbg(&port->dev, "malformed packet received: %d bytes\n", |
| 81 | urb->actual_length); |
| 82 | return; |
| 83 | } |
| 84 | /* |
| 85 | * Data from the device comes with a 2 byte header: |
| 86 | * |
| 87 | * <0x00><0x00>data... |
| 88 | * This is real data to be sent to the tty layer |
| 89 | * <0x00><0x01>level |
| 90 | * This is a CTS level change, the third byte is the CTS |
| 91 | * value (0 for low, 1 for high). |
| 92 | */ |
| 93 | if ((hdr[0] == 0x00) && (hdr[1] == 0x00)) { |
| 94 | opticon_process_data_packet(port, data, data_len); |
| 95 | } else if ((hdr[0] == 0x00) && (hdr[1] == 0x01)) { |
| 96 | opticon_process_status_packet(port, data, data_len); |
| 97 | } else { |
| 98 | dev_dbg(&port->dev, "unknown packet received: %02x %02x\n", |
| 99 | hdr[0], hdr[1]); |
| 100 | } |
| 101 | } |
Martin Jansen | 309a057 | 2011-02-24 14:50:16 +0100 | [diff] [blame] | 102 | |
Martin Jansen | 309a057 | 2011-02-24 14:50:16 +0100 | [diff] [blame] | 103 | static int send_control_msg(struct usb_serial_port *port, u8 requesttype, |
| 104 | u8 val) |
| 105 | { |
| 106 | struct usb_serial *serial = port->serial; |
| 107 | int retval; |
Johan Hovold | ea0dbeb | 2012-10-25 10:29:11 +0200 | [diff] [blame] | 108 | u8 *buffer; |
| 109 | |
| 110 | buffer = kzalloc(1, GFP_KERNEL); |
| 111 | if (!buffer) |
| 112 | return -ENOMEM; |
Martin Jansen | 309a057 | 2011-02-24 14:50:16 +0100 | [diff] [blame] | 113 | |
| 114 | buffer[0] = val; |
| 115 | /* Send the message to the vendor control endpoint |
| 116 | * of the connected device */ |
| 117 | retval = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), |
| 118 | requesttype, |
| 119 | USB_DIR_OUT|USB_TYPE_VENDOR|USB_RECIP_INTERFACE, |
| 120 | 0, 0, buffer, 1, 0); |
Johan Hovold | ea0dbeb | 2012-10-25 10:29:11 +0200 | [diff] [blame] | 121 | kfree(buffer); |
Martin Jansen | 309a057 | 2011-02-24 14:50:16 +0100 | [diff] [blame] | 122 | |
Johan Hovold | 94c51dc | 2013-03-21 12:37:42 +0100 | [diff] [blame^] | 123 | if (retval < 0) |
| 124 | return retval; |
| 125 | |
| 126 | return 0; |
Martin Jansen | 309a057 | 2011-02-24 14:50:16 +0100 | [diff] [blame] | 127 | } |
| 128 | |
Alan Cox | a509a7e | 2009-09-19 13:13:26 -0700 | [diff] [blame] | 129 | 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] | 130 | { |
Johan Hovold | 70d25ee | 2012-11-18 13:23:30 +0100 | [diff] [blame] | 131 | struct opticon_private *priv = usb_get_serial_port_data(port); |
Greg Kroah-Hartman | 57262b8 | 2008-11-03 13:27:03 -0800 | [diff] [blame] | 132 | unsigned long flags; |
Johan Hovold | 7a6ee2b | 2012-11-18 13:23:36 +0100 | [diff] [blame] | 133 | int res; |
Greg Kroah-Hartman | 57262b8 | 2008-11-03 13:27:03 -0800 | [diff] [blame] | 134 | |
Greg Kroah-Hartman | 57262b8 | 2008-11-03 13:27:03 -0800 | [diff] [blame] | 135 | spin_lock_irqsave(&priv->lock, flags); |
Martin Jansen | 309a057 | 2011-02-24 14:50:16 +0100 | [diff] [blame] | 136 | priv->rts = false; |
Greg Kroah-Hartman | 57262b8 | 2008-11-03 13:27:03 -0800 | [diff] [blame] | 137 | spin_unlock_irqrestore(&priv->lock, flags); |
| 138 | |
Martin Jansen | 309a057 | 2011-02-24 14:50:16 +0100 | [diff] [blame] | 139 | /* Clear RTS line */ |
| 140 | send_control_msg(port, CONTROL_RTS, 0); |
| 141 | |
Martin Jansen | 309a057 | 2011-02-24 14:50:16 +0100 | [diff] [blame] | 142 | /* clear the halt status of the enpoint */ |
Johan Hovold | 7a6ee2b | 2012-11-18 13:23:36 +0100 | [diff] [blame] | 143 | usb_clear_halt(port->serial->dev, port->read_urb->pipe); |
Martin Jansen | 309a057 | 2011-02-24 14:50:16 +0100 | [diff] [blame] | 144 | |
Johan Hovold | 7a6ee2b | 2012-11-18 13:23:36 +0100 | [diff] [blame] | 145 | res = usb_serial_generic_open(tty, port); |
| 146 | if (!res) |
| 147 | return res; |
| 148 | |
Martin Jansen | 309a057 | 2011-02-24 14:50:16 +0100 | [diff] [blame] | 149 | /* Request CTS line state, sometimes during opening the current |
| 150 | * CTS state can be missed. */ |
| 151 | send_control_msg(port, RESEND_CTS_STATE, 1); |
Greg Kroah-Hartman | 57262b8 | 2008-11-03 13:27:03 -0800 | [diff] [blame] | 152 | |
Johan Hovold | 7a6ee2b | 2012-11-18 13:23:36 +0100 | [diff] [blame] | 153 | return res; |
Greg Kroah-Hartman | 57262b8 | 2008-11-03 13:27:03 -0800 | [diff] [blame] | 154 | } |
| 155 | |
Martin Jansen | 309a057 | 2011-02-24 14:50:16 +0100 | [diff] [blame] | 156 | static void opticon_write_control_callback(struct urb *urb) |
Greg Kroah-Hartman | 648d4e1 | 2009-02-06 18:30:56 -0800 | [diff] [blame] | 157 | { |
Johan Hovold | e32d82b | 2012-11-18 13:23:32 +0100 | [diff] [blame] | 158 | struct usb_serial_port *port = urb->context; |
| 159 | struct opticon_private *priv = usb_get_serial_port_data(port); |
Greg Kroah-Hartman | 648d4e1 | 2009-02-06 18:30:56 -0800 | [diff] [blame] | 160 | int status = urb->status; |
| 161 | unsigned long flags; |
| 162 | |
| 163 | /* free up the transfer buffer, as usb_free_urb() does not do this */ |
| 164 | kfree(urb->transfer_buffer); |
| 165 | |
Alon Ziv | 0d930e5 | 2010-10-10 08:32:19 +0200 | [diff] [blame] | 166 | /* setup packet may be set if we're using it for writing */ |
| 167 | kfree(urb->setup_packet); |
| 168 | |
Greg Kroah-Hartman | 648d4e1 | 2009-02-06 18:30:56 -0800 | [diff] [blame] | 169 | if (status) |
Johan Hovold | e32d82b | 2012-11-18 13:23:32 +0100 | [diff] [blame] | 170 | dev_dbg(&port->dev, |
Johan Hovold | e29a773 | 2012-11-18 13:23:23 +0100 | [diff] [blame] | 171 | "%s - non-zero urb status received: %d\n", |
Greg Kroah-Hartman | d44d9ab | 2012-09-13 17:18:19 -0700 | [diff] [blame] | 172 | __func__, status); |
Greg Kroah-Hartman | 648d4e1 | 2009-02-06 18:30:56 -0800 | [diff] [blame] | 173 | |
| 174 | spin_lock_irqsave(&priv->lock, flags); |
| 175 | --priv->outstanding_urbs; |
| 176 | spin_unlock_irqrestore(&priv->lock, flags); |
| 177 | |
Johan Hovold | e32d82b | 2012-11-18 13:23:32 +0100 | [diff] [blame] | 178 | usb_serial_port_softint(port); |
Greg Kroah-Hartman | 648d4e1 | 2009-02-06 18:30:56 -0800 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | static int opticon_write(struct tty_struct *tty, struct usb_serial_port *port, |
| 182 | const unsigned char *buf, int count) |
| 183 | { |
Johan Hovold | 70d25ee | 2012-11-18 13:23:30 +0100 | [diff] [blame] | 184 | struct opticon_private *priv = usb_get_serial_port_data(port); |
Greg Kroah-Hartman | 648d4e1 | 2009-02-06 18:30:56 -0800 | [diff] [blame] | 185 | struct usb_serial *serial = port->serial; |
| 186 | struct urb *urb; |
| 187 | unsigned char *buffer; |
| 188 | unsigned long flags; |
| 189 | int status; |
Martin Jansen | 309a057 | 2011-02-24 14:50:16 +0100 | [diff] [blame] | 190 | struct usb_ctrlrequest *dr; |
Greg Kroah-Hartman | 648d4e1 | 2009-02-06 18:30:56 -0800 | [diff] [blame] | 191 | |
Greg Kroah-Hartman | 648d4e1 | 2009-02-06 18:30:56 -0800 | [diff] [blame] | 192 | spin_lock_irqsave(&priv->lock, flags); |
| 193 | if (priv->outstanding_urbs > URB_UPPER_LIMIT) { |
| 194 | spin_unlock_irqrestore(&priv->lock, flags); |
Greg Kroah-Hartman | d44d9ab | 2012-09-13 17:18:19 -0700 | [diff] [blame] | 195 | dev_dbg(&port->dev, "%s - write limit hit\n", __func__); |
Greg Kroah-Hartman | 648d4e1 | 2009-02-06 18:30:56 -0800 | [diff] [blame] | 196 | return 0; |
| 197 | } |
| 198 | priv->outstanding_urbs++; |
| 199 | spin_unlock_irqrestore(&priv->lock, flags); |
| 200 | |
| 201 | buffer = kmalloc(count, GFP_ATOMIC); |
| 202 | if (!buffer) { |
| 203 | dev_err(&port->dev, "out of memory\n"); |
| 204 | count = -ENOMEM; |
Martin Jansen | 309a057 | 2011-02-24 14:50:16 +0100 | [diff] [blame] | 205 | |
Greg Kroah-Hartman | 648d4e1 | 2009-02-06 18:30:56 -0800 | [diff] [blame] | 206 | goto error_no_buffer; |
| 207 | } |
| 208 | |
| 209 | urb = usb_alloc_urb(0, GFP_ATOMIC); |
| 210 | if (!urb) { |
| 211 | dev_err(&port->dev, "no more free urbs\n"); |
| 212 | count = -ENOMEM; |
| 213 | goto error_no_urb; |
| 214 | } |
| 215 | |
| 216 | memcpy(buffer, buf, count); |
| 217 | |
Greg Kroah-Hartman | 59d33f2 | 2012-09-18 09:58:57 +0100 | [diff] [blame] | 218 | usb_serial_debug_data(&port->dev, __func__, count, buffer); |
Greg Kroah-Hartman | 648d4e1 | 2009-02-06 18:30:56 -0800 | [diff] [blame] | 219 | |
Martin Jansen | 309a057 | 2011-02-24 14:50:16 +0100 | [diff] [blame] | 220 | /* The conncected devices do not have a bulk write endpoint, |
| 221 | * to transmit data to de barcode device the control endpoint is used */ |
| 222 | dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_NOIO); |
Julia Lawall | b0795bb | 2011-05-13 17:30:46 +0200 | [diff] [blame] | 223 | if (!dr) { |
| 224 | dev_err(&port->dev, "out of memory\n"); |
| 225 | count = -ENOMEM; |
Johan Hovold | acbf0e5 | 2012-10-25 10:29:12 +0200 | [diff] [blame] | 226 | goto error_no_dr; |
Julia Lawall | b0795bb | 2011-05-13 17:30:46 +0200 | [diff] [blame] | 227 | } |
Alon Ziv | 0d930e5 | 2010-10-10 08:32:19 +0200 | [diff] [blame] | 228 | |
Martin Jansen | 309a057 | 2011-02-24 14:50:16 +0100 | [diff] [blame] | 229 | dr->bRequestType = USB_TYPE_VENDOR | USB_RECIP_INTERFACE | USB_DIR_OUT; |
| 230 | dr->bRequest = 0x01; |
| 231 | dr->wValue = 0; |
| 232 | dr->wIndex = 0; |
| 233 | dr->wLength = cpu_to_le16(count); |
Alon Ziv | 0d930e5 | 2010-10-10 08:32:19 +0200 | [diff] [blame] | 234 | |
Martin Jansen | 309a057 | 2011-02-24 14:50:16 +0100 | [diff] [blame] | 235 | usb_fill_control_urb(urb, serial->dev, |
| 236 | usb_sndctrlpipe(serial->dev, 0), |
| 237 | (unsigned char *)dr, buffer, count, |
Johan Hovold | e32d82b | 2012-11-18 13:23:32 +0100 | [diff] [blame] | 238 | opticon_write_control_callback, port); |
Greg Kroah-Hartman | 648d4e1 | 2009-02-06 18:30:56 -0800 | [diff] [blame] | 239 | |
| 240 | /* send it down the pipe */ |
| 241 | status = usb_submit_urb(urb, GFP_ATOMIC); |
| 242 | if (status) { |
| 243 | dev_err(&port->dev, |
Martin Jansen | 309a057 | 2011-02-24 14:50:16 +0100 | [diff] [blame] | 244 | "%s - usb_submit_urb(write endpoint) failed status = %d\n", |
Greg Kroah-Hartman | 648d4e1 | 2009-02-06 18:30:56 -0800 | [diff] [blame] | 245 | __func__, status); |
| 246 | count = status; |
| 247 | goto error; |
| 248 | } |
| 249 | |
| 250 | /* we are done with this urb, so let the host driver |
| 251 | * really free it when it is finished with it */ |
| 252 | usb_free_urb(urb); |
| 253 | |
| 254 | return count; |
| 255 | error: |
Johan Hovold | acbf0e5 | 2012-10-25 10:29:12 +0200 | [diff] [blame] | 256 | kfree(dr); |
| 257 | error_no_dr: |
Greg Kroah-Hartman | 648d4e1 | 2009-02-06 18:30:56 -0800 | [diff] [blame] | 258 | usb_free_urb(urb); |
| 259 | error_no_urb: |
| 260 | kfree(buffer); |
| 261 | error_no_buffer: |
| 262 | spin_lock_irqsave(&priv->lock, flags); |
| 263 | --priv->outstanding_urbs; |
| 264 | spin_unlock_irqrestore(&priv->lock, flags); |
| 265 | return count; |
| 266 | } |
| 267 | |
| 268 | static int opticon_write_room(struct tty_struct *tty) |
| 269 | { |
| 270 | struct usb_serial_port *port = tty->driver_data; |
Johan Hovold | 70d25ee | 2012-11-18 13:23:30 +0100 | [diff] [blame] | 271 | struct opticon_private *priv = usb_get_serial_port_data(port); |
Greg Kroah-Hartman | 648d4e1 | 2009-02-06 18:30:56 -0800 | [diff] [blame] | 272 | unsigned long flags; |
| 273 | |
Greg Kroah-Hartman | 648d4e1 | 2009-02-06 18:30:56 -0800 | [diff] [blame] | 274 | /* |
| 275 | * We really can take almost anything the user throws at us |
| 276 | * but let's pick a nice big number to tell the tty |
| 277 | * layer that we have lots of free space, unless we don't. |
| 278 | */ |
| 279 | spin_lock_irqsave(&priv->lock, flags); |
| 280 | if (priv->outstanding_urbs > URB_UPPER_LIMIT * 2 / 3) { |
| 281 | spin_unlock_irqrestore(&priv->lock, flags); |
Greg Kroah-Hartman | d44d9ab | 2012-09-13 17:18:19 -0700 | [diff] [blame] | 282 | dev_dbg(&port->dev, "%s - write limit hit\n", __func__); |
Greg Kroah-Hartman | 648d4e1 | 2009-02-06 18:30:56 -0800 | [diff] [blame] | 283 | return 0; |
| 284 | } |
| 285 | spin_unlock_irqrestore(&priv->lock, flags); |
| 286 | |
| 287 | return 2048; |
| 288 | } |
| 289 | |
Alan Cox | 60b33c1 | 2011-02-14 16:26:14 +0000 | [diff] [blame] | 290 | static int opticon_tiocmget(struct tty_struct *tty) |
Greg Kroah-Hartman | faac64a | 2009-02-06 18:31:46 -0800 | [diff] [blame] | 291 | { |
| 292 | struct usb_serial_port *port = tty->driver_data; |
Johan Hovold | 70d25ee | 2012-11-18 13:23:30 +0100 | [diff] [blame] | 293 | struct opticon_private *priv = usb_get_serial_port_data(port); |
Greg Kroah-Hartman | faac64a | 2009-02-06 18:31:46 -0800 | [diff] [blame] | 294 | unsigned long flags; |
| 295 | int result = 0; |
| 296 | |
Greg Kroah-Hartman | faac64a | 2009-02-06 18:31:46 -0800 | [diff] [blame] | 297 | spin_lock_irqsave(&priv->lock, flags); |
| 298 | if (priv->rts) |
Martin Jansen | 309a057 | 2011-02-24 14:50:16 +0100 | [diff] [blame] | 299 | result |= TIOCM_RTS; |
| 300 | if (priv->cts) |
| 301 | result |= TIOCM_CTS; |
Greg Kroah-Hartman | faac64a | 2009-02-06 18:31:46 -0800 | [diff] [blame] | 302 | spin_unlock_irqrestore(&priv->lock, flags); |
| 303 | |
Greg Kroah-Hartman | d44d9ab | 2012-09-13 17:18:19 -0700 | [diff] [blame] | 304 | dev_dbg(&port->dev, "%s - %x\n", __func__, result); |
Greg Kroah-Hartman | faac64a | 2009-02-06 18:31:46 -0800 | [diff] [blame] | 305 | return result; |
| 306 | } |
| 307 | |
Randy Dunlap | 4acfaf8 | 2011-04-03 11:42:00 -0700 | [diff] [blame] | 308 | static int opticon_tiocmset(struct tty_struct *tty, |
Martin Jansen | 309a057 | 2011-02-24 14:50:16 +0100 | [diff] [blame] | 309 | unsigned int set, unsigned int clear) |
| 310 | { |
| 311 | struct usb_serial_port *port = tty->driver_data; |
Johan Hovold | 81d5a67 | 2012-04-25 15:56:29 +0200 | [diff] [blame] | 312 | struct usb_serial *serial = port->serial; |
Johan Hovold | 70d25ee | 2012-11-18 13:23:30 +0100 | [diff] [blame] | 313 | struct opticon_private *priv = usb_get_serial_port_data(port); |
Martin Jansen | 309a057 | 2011-02-24 14:50:16 +0100 | [diff] [blame] | 314 | unsigned long flags; |
| 315 | bool rts; |
| 316 | bool changed = false; |
Johan Hovold | 81d5a67 | 2012-04-25 15:56:29 +0200 | [diff] [blame] | 317 | int ret; |
Martin Jansen | 309a057 | 2011-02-24 14:50:16 +0100 | [diff] [blame] | 318 | |
Martin Jansen | 309a057 | 2011-02-24 14:50:16 +0100 | [diff] [blame] | 319 | /* We only support RTS so we only handle that */ |
| 320 | spin_lock_irqsave(&priv->lock, flags); |
| 321 | |
| 322 | rts = priv->rts; |
| 323 | if (set & TIOCM_RTS) |
| 324 | priv->rts = true; |
| 325 | if (clear & TIOCM_RTS) |
| 326 | priv->rts = false; |
| 327 | changed = rts ^ priv->rts; |
| 328 | spin_unlock_irqrestore(&priv->lock, flags); |
| 329 | |
| 330 | if (!changed) |
| 331 | return 0; |
| 332 | |
| 333 | /* Send the new RTS state to the connected device */ |
Johan Hovold | 81d5a67 | 2012-04-25 15:56:29 +0200 | [diff] [blame] | 334 | mutex_lock(&serial->disc_mutex); |
Johan Hovold | 94c51dc | 2013-03-21 12:37:42 +0100 | [diff] [blame^] | 335 | if (!serial->disconnected) { |
Johan Hovold | 81d5a67 | 2012-04-25 15:56:29 +0200 | [diff] [blame] | 336 | ret = send_control_msg(port, CONTROL_RTS, !rts); |
Johan Hovold | 94c51dc | 2013-03-21 12:37:42 +0100 | [diff] [blame^] | 337 | if (ret) |
| 338 | ret = usb_translate_errors(ret); |
| 339 | } else { |
Johan Hovold | 81d5a67 | 2012-04-25 15:56:29 +0200 | [diff] [blame] | 340 | ret = -ENODEV; |
Johan Hovold | 94c51dc | 2013-03-21 12:37:42 +0100 | [diff] [blame^] | 341 | } |
Johan Hovold | 81d5a67 | 2012-04-25 15:56:29 +0200 | [diff] [blame] | 342 | mutex_unlock(&serial->disc_mutex); |
| 343 | |
| 344 | return ret; |
Martin Jansen | 309a057 | 2011-02-24 14:50:16 +0100 | [diff] [blame] | 345 | } |
| 346 | |
Johan Hovold | 56be1a1 | 2012-11-18 13:23:31 +0100 | [diff] [blame] | 347 | static int get_serial_info(struct usb_serial_port *port, |
Greg Kroah-Hartman | faac64a | 2009-02-06 18:31:46 -0800 | [diff] [blame] | 348 | struct serial_struct __user *serial) |
| 349 | { |
| 350 | struct serial_struct tmp; |
| 351 | |
| 352 | if (!serial) |
| 353 | return -EFAULT; |
| 354 | |
| 355 | memset(&tmp, 0x00, sizeof(tmp)); |
| 356 | |
| 357 | /* fake emulate a 16550 uart to make userspace code happy */ |
| 358 | tmp.type = PORT_16550A; |
Johan Hovold | 56be1a1 | 2012-11-18 13:23:31 +0100 | [diff] [blame] | 359 | tmp.line = port->serial->minor; |
Greg Kroah-Hartman | faac64a | 2009-02-06 18:31:46 -0800 | [diff] [blame] | 360 | tmp.port = 0; |
| 361 | tmp.irq = 0; |
| 362 | tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ; |
| 363 | tmp.xmit_fifo_size = 1024; |
| 364 | tmp.baud_base = 9600; |
| 365 | tmp.close_delay = 5*HZ; |
| 366 | tmp.closing_wait = 30*HZ; |
| 367 | |
| 368 | if (copy_to_user(serial, &tmp, sizeof(*serial))) |
| 369 | return -EFAULT; |
| 370 | return 0; |
| 371 | } |
| 372 | |
Alan Cox | 00a0d0d | 2011-02-14 16:27:06 +0000 | [diff] [blame] | 373 | static int opticon_ioctl(struct tty_struct *tty, |
Greg Kroah-Hartman | faac64a | 2009-02-06 18:31:46 -0800 | [diff] [blame] | 374 | unsigned int cmd, unsigned long arg) |
| 375 | { |
| 376 | struct usb_serial_port *port = tty->driver_data; |
Greg Kroah-Hartman | faac64a | 2009-02-06 18:31:46 -0800 | [diff] [blame] | 377 | |
Greg Kroah-Hartman | d44d9ab | 2012-09-13 17:18:19 -0700 | [diff] [blame] | 378 | 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] | 379 | |
| 380 | switch (cmd) { |
| 381 | case TIOCGSERIAL: |
Johan Hovold | 56be1a1 | 2012-11-18 13:23:31 +0100 | [diff] [blame] | 382 | return get_serial_info(port, |
Greg Kroah-Hartman | faac64a | 2009-02-06 18:31:46 -0800 | [diff] [blame] | 383 | (struct serial_struct __user *)arg); |
| 384 | } |
| 385 | |
| 386 | return -ENOIOCTLCMD; |
| 387 | } |
| 388 | |
Greg Kroah-Hartman | 57262b8 | 2008-11-03 13:27:03 -0800 | [diff] [blame] | 389 | static int opticon_startup(struct usb_serial *serial) |
| 390 | { |
Johan Hovold | a0a5fd9 | 2012-11-18 13:23:27 +0100 | [diff] [blame] | 391 | if (!serial->num_bulk_in) { |
| 392 | dev_err(&serial->dev->dev, "no bulk in endpoint\n"); |
| 393 | return -ENODEV; |
| 394 | } |
Greg Kroah-Hartman | 57262b8 | 2008-11-03 13:27:03 -0800 | [diff] [blame] | 395 | |
Johan Hovold | 70d25ee | 2012-11-18 13:23:30 +0100 | [diff] [blame] | 396 | return 0; |
| 397 | } |
| 398 | |
| 399 | static int opticon_port_probe(struct usb_serial_port *port) |
| 400 | { |
Johan Hovold | 70d25ee | 2012-11-18 13:23:30 +0100 | [diff] [blame] | 401 | struct opticon_private *priv; |
Johan Hovold | 70d25ee | 2012-11-18 13:23:30 +0100 | [diff] [blame] | 402 | |
Greg Kroah-Hartman | 57262b8 | 2008-11-03 13:27:03 -0800 | [diff] [blame] | 403 | priv = kzalloc(sizeof(*priv), GFP_KERNEL); |
Johan Hovold | 70d25ee | 2012-11-18 13:23:30 +0100 | [diff] [blame] | 404 | if (!priv) |
Greg Kroah-Hartman | 57262b8 | 2008-11-03 13:27:03 -0800 | [diff] [blame] | 405 | return -ENOMEM; |
Johan Hovold | 70d25ee | 2012-11-18 13:23:30 +0100 | [diff] [blame] | 406 | |
Greg Kroah-Hartman | 57262b8 | 2008-11-03 13:27:03 -0800 | [diff] [blame] | 407 | spin_lock_init(&priv->lock); |
Johan Hovold | 0b8718a | 2012-11-18 13:23:22 +0100 | [diff] [blame] | 408 | |
Johan Hovold | 70d25ee | 2012-11-18 13:23:30 +0100 | [diff] [blame] | 409 | usb_set_serial_port_data(port, priv); |
Greg Kroah-Hartman | 57262b8 | 2008-11-03 13:27:03 -0800 | [diff] [blame] | 410 | |
Johan Hovold | 70d25ee | 2012-11-18 13:23:30 +0100 | [diff] [blame] | 411 | return 0; |
Greg Kroah-Hartman | 57262b8 | 2008-11-03 13:27:03 -0800 | [diff] [blame] | 412 | } |
| 413 | |
Johan Hovold | 70d25ee | 2012-11-18 13:23:30 +0100 | [diff] [blame] | 414 | static int opticon_port_remove(struct usb_serial_port *port) |
Alan Stern | f9c99bb | 2009-06-02 11:53:55 -0400 | [diff] [blame] | 415 | { |
Johan Hovold | 70d25ee | 2012-11-18 13:23:30 +0100 | [diff] [blame] | 416 | struct opticon_private *priv = usb_get_serial_port_data(port); |
Alan Stern | f9c99bb | 2009-06-02 11:53:55 -0400 | [diff] [blame] | 417 | |
Greg Kroah-Hartman | 57262b8 | 2008-11-03 13:27:03 -0800 | [diff] [blame] | 418 | kfree(priv); |
Johan Hovold | 70d25ee | 2012-11-18 13:23:30 +0100 | [diff] [blame] | 419 | |
| 420 | return 0; |
Greg Kroah-Hartman | 57262b8 | 2008-11-03 13:27:03 -0800 | [diff] [blame] | 421 | } |
| 422 | |
Greg Kroah-Hartman | 57262b8 | 2008-11-03 13:27:03 -0800 | [diff] [blame] | 423 | static struct usb_serial_driver opticon_device = { |
| 424 | .driver = { |
| 425 | .owner = THIS_MODULE, |
| 426 | .name = "opticon", |
| 427 | }, |
| 428 | .id_table = id_table, |
Greg Kroah-Hartman | 57262b8 | 2008-11-03 13:27:03 -0800 | [diff] [blame] | 429 | .num_ports = 1, |
Johan Hovold | 333396f | 2012-11-18 13:23:33 +0100 | [diff] [blame] | 430 | .bulk_in_size = 256, |
Greg Kroah-Hartman | 57262b8 | 2008-11-03 13:27:03 -0800 | [diff] [blame] | 431 | .attach = opticon_startup, |
Johan Hovold | 70d25ee | 2012-11-18 13:23:30 +0100 | [diff] [blame] | 432 | .port_probe = opticon_port_probe, |
| 433 | .port_remove = opticon_port_remove, |
Greg Kroah-Hartman | 57262b8 | 2008-11-03 13:27:03 -0800 | [diff] [blame] | 434 | .open = opticon_open, |
Greg Kroah-Hartman | 648d4e1 | 2009-02-06 18:30:56 -0800 | [diff] [blame] | 435 | .write = opticon_write, |
| 436 | .write_room = opticon_write_room, |
Johan Hovold | 7a6ee2b | 2012-11-18 13:23:36 +0100 | [diff] [blame] | 437 | .throttle = usb_serial_generic_throttle, |
| 438 | .unthrottle = usb_serial_generic_unthrottle, |
Greg Kroah-Hartman | faac64a | 2009-02-06 18:31:46 -0800 | [diff] [blame] | 439 | .ioctl = opticon_ioctl, |
| 440 | .tiocmget = opticon_tiocmget, |
Martin Jansen | 309a057 | 2011-02-24 14:50:16 +0100 | [diff] [blame] | 441 | .tiocmset = opticon_tiocmset, |
Johan Hovold | 7a6ee2b | 2012-11-18 13:23:36 +0100 | [diff] [blame] | 442 | .process_read_urb = opticon_process_read_urb, |
Greg Kroah-Hartman | 57262b8 | 2008-11-03 13:27:03 -0800 | [diff] [blame] | 443 | }; |
| 444 | |
Alan Stern | f667dda | 2012-02-23 14:57:18 -0500 | [diff] [blame] | 445 | static struct usb_serial_driver * const serial_drivers[] = { |
| 446 | &opticon_device, NULL |
| 447 | }; |
| 448 | |
Greg Kroah-Hartman | 68e2411 | 2012-05-08 15:46:14 -0700 | [diff] [blame] | 449 | module_usb_serial_driver(serial_drivers, id_table); |
Greg Kroah-Hartman | 57262b8 | 2008-11-03 13:27:03 -0800 | [diff] [blame] | 450 | |
Martin Jansen | 309a057 | 2011-02-24 14:50:16 +0100 | [diff] [blame] | 451 | MODULE_DESCRIPTION(DRIVER_DESC); |
Greg Kroah-Hartman | 57262b8 | 2008-11-03 13:27:03 -0800 | [diff] [blame] | 452 | MODULE_LICENSE("GPL"); |