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