1 // SPDX-License-Identifier: GPL-2.0
3 * Opticon USB barcode to serial driver
8 * Copyright (C) 2008 - 2009 Novell Inc.
11 #include <linux/kernel.h>
12 #include <linux/tty.h>
13 #include <linux/tty_driver.h>
14 #include <linux/slab.h>
15 #include <linux/tty_flip.h>
16 #include <linux/serial.h>
17 #include <linux/module.h>
18 #include <linux/usb.h>
19 #include <linux/usb/serial.h>
20 #include <linux/uaccess.h>
22 #define CONTROL_RTS 0x02
23 #define RESEND_CTS_STATE 0x03
25 /* max number of write urbs in flight */
26 #define URB_UPPER_LIMIT 8
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)"
32 static const struct usb_device_id id_table[] = {
33 { USB_DEVICE(0x065a, 0x0009) },
36 MODULE_DEVICE_TABLE(usb, id_table);
38 /* This structure holds all of the individual device information */
39 struct opticon_private {
40 spinlock_t lock; /* protects the following flags */
47 static void opticon_process_data_packet(struct usb_serial_port *port,
48 const unsigned char *buf, size_t len)
50 tty_insert_flip_string(&port->port, buf, len);
51 tty_flip_buffer_push(&port->port);
54 static void opticon_process_status_packet(struct usb_serial_port *port,
55 const unsigned char *buf, size_t len)
57 struct opticon_private *priv = usb_get_serial_port_data(port);
60 spin_lock_irqsave(&priv->lock, flags);
65 spin_unlock_irqrestore(&priv->lock, flags);
68 static void opticon_process_read_urb(struct urb *urb)
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;
75 if (urb->actual_length <= 2) {
76 dev_dbg(&port->dev, "malformed packet received: %d bytes\n",
81 * Data from the device comes with a 2 byte header:
84 * This is real data to be sent to the tty layer
86 * This is a CTS level change, the third byte is the CTS
87 * value (0 for low, 1 for high).
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);
94 dev_dbg(&port->dev, "unknown packet received: %02x %02x\n",
99 static int send_control_msg(struct usb_serial_port *port, u8 requesttype,
102 struct usb_serial *serial = port->serial;
106 buffer = kzalloc(1, GFP_KERNEL);
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),
115 USB_DIR_OUT|USB_TYPE_VENDOR|USB_RECIP_INTERFACE,
125 static int opticon_open(struct tty_struct *tty, struct usb_serial_port *port)
127 struct opticon_private *priv = usb_get_serial_port_data(port);
131 spin_lock_irqsave(&priv->lock, flags);
133 spin_unlock_irqrestore(&priv->lock, flags);
136 send_control_msg(port, CONTROL_RTS, 0);
138 /* clear the halt status of the endpoint */
139 usb_clear_halt(port->serial->dev, port->read_urb->pipe);
141 res = usb_serial_generic_open(tty, port);
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);
152 static void opticon_write_control_callback(struct urb *urb)
154 struct usb_serial_port *port = urb->context;
155 struct opticon_private *priv = usb_get_serial_port_data(port);
156 int status = urb->status;
159 /* free up the transfer buffer, as usb_free_urb() does not do this */
160 kfree(urb->transfer_buffer);
162 /* setup packet may be set if we're using it for writing */
163 kfree(urb->setup_packet);
167 "%s - non-zero urb status received: %d\n",
170 spin_lock_irqsave(&priv->lock, flags);
171 --priv->outstanding_urbs;
172 spin_unlock_irqrestore(&priv->lock, flags);
174 usb_serial_port_softint(port);
177 static int opticon_write(struct tty_struct *tty, struct usb_serial_port *port,
178 const unsigned char *buf, int count)
180 struct opticon_private *priv = usb_get_serial_port_data(port);
181 struct usb_serial *serial = port->serial;
183 unsigned char *buffer;
186 struct usb_ctrlrequest *dr;
188 spin_lock_irqsave(&priv->lock, flags);
189 if (priv->outstanding_urbs > URB_UPPER_LIMIT) {
190 spin_unlock_irqrestore(&priv->lock, flags);
191 dev_dbg(&port->dev, "%s - write limit hit\n", __func__);
194 priv->outstanding_urbs++;
195 spin_unlock_irqrestore(&priv->lock, flags);
197 buffer = kmalloc(count, GFP_ATOMIC);
200 goto error_no_buffer;
203 urb = usb_alloc_urb(0, GFP_ATOMIC);
209 memcpy(buffer, buf, count);
211 usb_serial_debug_data(&port->dev, __func__, count, buffer);
213 /* The connected devices do not have a bulk write endpoint,
214 * to transmit data to de barcode device the control endpoint is used */
215 dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_ATOMIC);
221 dr->bRequestType = USB_TYPE_VENDOR | USB_RECIP_INTERFACE | USB_DIR_OUT;
225 dr->wLength = cpu_to_le16(count);
227 usb_fill_control_urb(urb, serial->dev,
228 usb_sndctrlpipe(serial->dev, 0),
229 (unsigned char *)dr, buffer, count,
230 opticon_write_control_callback, port);
232 /* send it down the pipe */
233 status = usb_submit_urb(urb, GFP_ATOMIC);
236 "%s - usb_submit_urb(write endpoint) failed status = %d\n",
242 /* we are done with this urb, so let the host driver
243 * really free it when it is finished with it */
254 spin_lock_irqsave(&priv->lock, flags);
255 --priv->outstanding_urbs;
256 spin_unlock_irqrestore(&priv->lock, flags);
260 static int opticon_write_room(struct tty_struct *tty)
262 struct usb_serial_port *port = tty->driver_data;
263 struct opticon_private *priv = usb_get_serial_port_data(port);
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.
271 spin_lock_irqsave(&priv->lock, flags);
272 if (priv->outstanding_urbs > URB_UPPER_LIMIT * 2 / 3) {
273 spin_unlock_irqrestore(&priv->lock, flags);
274 dev_dbg(&port->dev, "%s - write limit hit\n", __func__);
277 spin_unlock_irqrestore(&priv->lock, flags);
282 static int opticon_tiocmget(struct tty_struct *tty)
284 struct usb_serial_port *port = tty->driver_data;
285 struct opticon_private *priv = usb_get_serial_port_data(port);
289 spin_lock_irqsave(&priv->lock, flags);
294 spin_unlock_irqrestore(&priv->lock, flags);
296 dev_dbg(&port->dev, "%s - %x\n", __func__, result);
300 static int opticon_tiocmset(struct tty_struct *tty,
301 unsigned int set, unsigned int clear)
303 struct usb_serial_port *port = tty->driver_data;
304 struct opticon_private *priv = usb_get_serial_port_data(port);
307 bool changed = false;
310 /* We only support RTS so we only handle that */
311 spin_lock_irqsave(&priv->lock, flags);
316 if (clear & TIOCM_RTS)
318 changed = rts ^ priv->rts;
319 spin_unlock_irqrestore(&priv->lock, flags);
324 ret = send_control_msg(port, CONTROL_RTS, !rts);
326 return usb_translate_errors(ret);
331 static int get_serial_info(struct usb_serial_port *port,
332 struct serial_struct __user *serial)
334 struct serial_struct tmp;
336 memset(&tmp, 0x00, sizeof(tmp));
338 /* fake emulate a 16550 uart to make userspace code happy */
339 tmp.type = PORT_16550A;
340 tmp.line = port->minor;
343 tmp.xmit_fifo_size = 1024;
344 tmp.baud_base = 9600;
345 tmp.close_delay = 5*HZ;
346 tmp.closing_wait = 30*HZ;
348 if (copy_to_user(serial, &tmp, sizeof(*serial)))
353 static int opticon_ioctl(struct tty_struct *tty,
354 unsigned int cmd, unsigned long arg)
356 struct usb_serial_port *port = tty->driver_data;
360 return get_serial_info(port,
361 (struct serial_struct __user *)arg);
367 static int opticon_port_probe(struct usb_serial_port *port)
369 struct opticon_private *priv;
371 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
375 spin_lock_init(&priv->lock);
377 usb_set_serial_port_data(port, priv);
382 static int opticon_port_remove(struct usb_serial_port *port)
384 struct opticon_private *priv = usb_get_serial_port_data(port);
391 static struct usb_serial_driver opticon_device = {
393 .owner = THIS_MODULE,
396 .id_table = id_table,
400 .port_probe = opticon_port_probe,
401 .port_remove = opticon_port_remove,
402 .open = opticon_open,
403 .write = opticon_write,
404 .write_room = opticon_write_room,
405 .throttle = usb_serial_generic_throttle,
406 .unthrottle = usb_serial_generic_unthrottle,
407 .ioctl = opticon_ioctl,
408 .tiocmget = opticon_tiocmget,
409 .tiocmset = opticon_tiocmset,
410 .process_read_urb = opticon_process_read_urb,
413 static struct usb_serial_driver * const serial_drivers[] = {
414 &opticon_device, NULL
417 module_usb_serial_driver(serial_drivers, id_table);
419 MODULE_DESCRIPTION(DRIVER_DESC);
420 MODULE_LICENSE("GPL v2");