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