2 * Opticon USB barcode to serial driver
7 * Copyright (C) 2008 - 2009 Novell Inc.
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.
14 #include <linux/kernel.h>
15 #include <linux/tty.h>
16 #include <linux/tty_driver.h>
17 #include <linux/slab.h>
18 #include <linux/tty_flip.h>
19 #include <linux/serial.h>
20 #include <linux/module.h>
21 #include <linux/usb.h>
22 #include <linux/usb/serial.h>
23 #include <linux/uaccess.h>
25 #define CONTROL_RTS 0x02
26 #define RESEND_CTS_STATE 0x03
28 /* max number of write urbs in flight */
29 #define URB_UPPER_LIMIT 8
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)"
35 static const struct usb_device_id id_table[] = {
36 { USB_DEVICE(0x065a, 0x0009) },
39 MODULE_DEVICE_TABLE(usb, id_table);
41 /* This structure holds all of the individual device information */
42 struct opticon_private {
43 spinlock_t lock; /* protects the following flags */
50 static void opticon_process_data_packet(struct usb_serial_port *port,
51 const unsigned char *buf, size_t len)
53 tty_insert_flip_string(&port->port, buf, len);
54 tty_flip_buffer_push(&port->port);
57 static void opticon_process_status_packet(struct usb_serial_port *port,
58 const unsigned char *buf, size_t len)
60 struct opticon_private *priv = usb_get_serial_port_data(port);
63 spin_lock_irqsave(&priv->lock, flags);
68 spin_unlock_irqrestore(&priv->lock, flags);
71 static void opticon_process_read_urb(struct urb *urb)
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;
78 if (urb->actual_length <= 2) {
79 dev_dbg(&port->dev, "malformed packet received: %d bytes\n",
84 * Data from the device comes with a 2 byte header:
87 * This is real data to be sent to the tty layer
89 * This is a CTS level change, the third byte is the CTS
90 * value (0 for low, 1 for high).
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);
97 dev_dbg(&port->dev, "unknown packet received: %02x %02x\n",
102 static int send_control_msg(struct usb_serial_port *port, u8 requesttype,
105 struct usb_serial *serial = port->serial;
109 buffer = kzalloc(1, GFP_KERNEL);
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),
118 USB_DIR_OUT|USB_TYPE_VENDOR|USB_RECIP_INTERFACE,
128 static int opticon_open(struct tty_struct *tty, struct usb_serial_port *port)
130 struct opticon_private *priv = usb_get_serial_port_data(port);
134 spin_lock_irqsave(&priv->lock, flags);
136 spin_unlock_irqrestore(&priv->lock, flags);
139 send_control_msg(port, CONTROL_RTS, 0);
141 /* clear the halt status of the endpoint */
142 usb_clear_halt(port->serial->dev, port->read_urb->pipe);
144 res = usb_serial_generic_open(tty, port);
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);
155 static void opticon_write_control_callback(struct urb *urb)
157 struct usb_serial_port *port = urb->context;
158 struct opticon_private *priv = usb_get_serial_port_data(port);
159 int status = urb->status;
162 /* free up the transfer buffer, as usb_free_urb() does not do this */
163 kfree(urb->transfer_buffer);
165 /* setup packet may be set if we're using it for writing */
166 kfree(urb->setup_packet);
170 "%s - non-zero urb status received: %d\n",
173 spin_lock_irqsave(&priv->lock, flags);
174 --priv->outstanding_urbs;
175 spin_unlock_irqrestore(&priv->lock, flags);
177 usb_serial_port_softint(port);
180 static int opticon_write(struct tty_struct *tty, struct usb_serial_port *port,
181 const unsigned char *buf, int count)
183 struct opticon_private *priv = usb_get_serial_port_data(port);
184 struct usb_serial *serial = port->serial;
186 unsigned char *buffer;
189 struct usb_ctrlrequest *dr;
191 spin_lock_irqsave(&priv->lock, flags);
192 if (priv->outstanding_urbs > URB_UPPER_LIMIT) {
193 spin_unlock_irqrestore(&priv->lock, flags);
194 dev_dbg(&port->dev, "%s - write limit hit\n", __func__);
197 priv->outstanding_urbs++;
198 spin_unlock_irqrestore(&priv->lock, flags);
200 buffer = kmalloc(count, GFP_ATOMIC);
203 goto error_no_buffer;
206 urb = usb_alloc_urb(0, GFP_ATOMIC);
212 memcpy(buffer, buf, count);
214 usb_serial_debug_data(&port->dev, __func__, count, buffer);
216 /* The connected devices do not have a bulk write endpoint,
217 * to transmit data to de barcode device the control endpoint is used */
218 dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_ATOMIC);
224 dr->bRequestType = USB_TYPE_VENDOR | USB_RECIP_INTERFACE | USB_DIR_OUT;
228 dr->wLength = cpu_to_le16(count);
230 usb_fill_control_urb(urb, serial->dev,
231 usb_sndctrlpipe(serial->dev, 0),
232 (unsigned char *)dr, buffer, count,
233 opticon_write_control_callback, port);
235 /* send it down the pipe */
236 status = usb_submit_urb(urb, GFP_ATOMIC);
239 "%s - usb_submit_urb(write endpoint) failed status = %d\n",
245 /* we are done with this urb, so let the host driver
246 * really free it when it is finished with it */
257 spin_lock_irqsave(&priv->lock, flags);
258 --priv->outstanding_urbs;
259 spin_unlock_irqrestore(&priv->lock, flags);
263 static int opticon_write_room(struct tty_struct *tty)
265 struct usb_serial_port *port = tty->driver_data;
266 struct opticon_private *priv = usb_get_serial_port_data(port);
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.
274 spin_lock_irqsave(&priv->lock, flags);
275 if (priv->outstanding_urbs > URB_UPPER_LIMIT * 2 / 3) {
276 spin_unlock_irqrestore(&priv->lock, flags);
277 dev_dbg(&port->dev, "%s - write limit hit\n", __func__);
280 spin_unlock_irqrestore(&priv->lock, flags);
285 static int opticon_tiocmget(struct tty_struct *tty)
287 struct usb_serial_port *port = tty->driver_data;
288 struct opticon_private *priv = usb_get_serial_port_data(port);
292 spin_lock_irqsave(&priv->lock, flags);
297 spin_unlock_irqrestore(&priv->lock, flags);
299 dev_dbg(&port->dev, "%s - %x\n", __func__, result);
303 static int opticon_tiocmset(struct tty_struct *tty,
304 unsigned int set, unsigned int clear)
306 struct usb_serial_port *port = tty->driver_data;
307 struct opticon_private *priv = usb_get_serial_port_data(port);
310 bool changed = false;
313 /* We only support RTS so we only handle that */
314 spin_lock_irqsave(&priv->lock, flags);
319 if (clear & TIOCM_RTS)
321 changed = rts ^ priv->rts;
322 spin_unlock_irqrestore(&priv->lock, flags);
327 ret = send_control_msg(port, CONTROL_RTS, !rts);
329 return usb_translate_errors(ret);
334 static int get_serial_info(struct usb_serial_port *port,
335 struct serial_struct __user *serial)
337 struct serial_struct tmp;
339 memset(&tmp, 0x00, sizeof(tmp));
341 /* fake emulate a 16550 uart to make userspace code happy */
342 tmp.type = PORT_16550A;
343 tmp.line = port->minor;
346 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
347 tmp.xmit_fifo_size = 1024;
348 tmp.baud_base = 9600;
349 tmp.close_delay = 5*HZ;
350 tmp.closing_wait = 30*HZ;
352 if (copy_to_user(serial, &tmp, sizeof(*serial)))
357 static int opticon_ioctl(struct tty_struct *tty,
358 unsigned int cmd, unsigned long arg)
360 struct usb_serial_port *port = tty->driver_data;
364 return get_serial_info(port,
365 (struct serial_struct __user *)arg);
371 static int opticon_startup(struct usb_serial *serial)
373 if (!serial->num_bulk_in) {
374 dev_err(&serial->dev->dev, "no bulk in endpoint\n");
381 static int opticon_port_probe(struct usb_serial_port *port)
383 struct opticon_private *priv;
385 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
389 spin_lock_init(&priv->lock);
391 usb_set_serial_port_data(port, priv);
396 static int opticon_port_remove(struct usb_serial_port *port)
398 struct opticon_private *priv = usb_get_serial_port_data(port);
405 static struct usb_serial_driver opticon_device = {
407 .owner = THIS_MODULE,
410 .id_table = id_table,
413 .attach = opticon_startup,
414 .port_probe = opticon_port_probe,
415 .port_remove = opticon_port_remove,
416 .open = opticon_open,
417 .write = opticon_write,
418 .write_room = opticon_write_room,
419 .throttle = usb_serial_generic_throttle,
420 .unthrottle = usb_serial_generic_unthrottle,
421 .ioctl = opticon_ioctl,
422 .tiocmget = opticon_tiocmget,
423 .tiocmset = opticon_tiocmset,
424 .process_read_urb = opticon_process_read_urb,
427 static struct usb_serial_driver * const serial_drivers[] = {
428 &opticon_device, NULL
431 module_usb_serial_driver(serial_drivers, id_table);
433 MODULE_DESCRIPTION(DRIVER_DESC);
434 MODULE_LICENSE("GPL");