Adding support for MOXA ART SoC. Testing port of linux-2.6.32.60-moxart.
[linux-3.6.7-moxart.git] / drivers / usb / serial / opticon.c
blob623358a426d9dd0b6dfe2e00080fe361f87c8fe9
1 /*
2 * Opticon USB barcode to serial driver
4 * Copyright (C) 2011 Martin Jansen <martin.jansen@opticon.com>
5 * Copyright (C) 2008 - 2009 Greg Kroah-Hartman <gregkh@suse.de>
6 * Copyright (C) 2008 - 2009 Novell Inc.
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License version
10 * 2 as published by the Free Software Foundation.
13 #include <linux/kernel.h>
14 #include <linux/init.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 bool debug;
37 static const struct usb_device_id id_table[] = {
38 { USB_DEVICE(0x065a, 0x0009) },
39 { },
41 MODULE_DEVICE_TABLE(usb, id_table);
43 /* This structure holds all of the individual device information */
44 struct opticon_private {
45 struct usb_device *udev;
46 struct usb_serial *serial;
47 struct usb_serial_port *port;
48 unsigned char *bulk_in_buffer;
49 struct urb *bulk_read_urb;
50 int buffer_size;
51 u8 bulk_address;
52 spinlock_t lock; /* protects the following flags */
53 bool throttled;
54 bool actually_throttled;
55 bool rts;
56 bool cts;
57 int outstanding_urbs;
62 static void opticon_read_bulk_callback(struct urb *urb)
64 struct opticon_private *priv = urb->context;
65 unsigned char *data = urb->transfer_buffer;
66 struct usb_serial_port *port = priv->port;
67 int status = urb->status;
68 struct tty_struct *tty;
69 int result;
70 int data_length;
71 unsigned long flags;
73 switch (status) {
74 case 0:
75 /* success */
76 break;
77 case -ECONNRESET:
78 case -ENOENT:
79 case -ESHUTDOWN:
80 /* this urb is terminated, clean up */
81 dbg("%s - urb shutting down with status: %d",
82 __func__, status);
83 return;
84 default:
85 dbg("%s - nonzero urb status received: %d",
86 __func__, status);
87 goto exit;
90 usb_serial_debug_data(debug, &port->dev, __func__, urb->actual_length,
91 data);
93 if (urb->actual_length > 2) {
94 data_length = urb->actual_length - 2;
97 * Data from the device comes with a 2 byte header:
99 * <0x00><0x00>data...
100 * This is real data to be sent to the tty layer
101 * <0x00><0x01)level
102 * This is a CTS level change, the third byte is the CTS
103 * value (0 for low, 1 for high).
105 if ((data[0] == 0x00) && (data[1] == 0x00)) {
106 /* real data, send it to the tty layer */
107 tty = tty_port_tty_get(&port->port);
108 if (tty) {
109 tty_insert_flip_string(tty, data + 2,
110 data_length);
111 tty_flip_buffer_push(tty);
112 tty_kref_put(tty);
114 } else {
115 if ((data[0] == 0x00) && (data[1] == 0x01)) {
116 spin_lock_irqsave(&priv->lock, flags);
117 /* CTS status information package */
118 if (data[2] == 0x00)
119 priv->cts = false;
120 else
121 priv->cts = true;
122 spin_unlock_irqrestore(&priv->lock, flags);
123 } else {
124 dev_dbg(&priv->udev->dev,
125 "Unknown data packet received from the device:"
126 " %2x %2x\n",
127 data[0], data[1]);
130 } else {
131 dev_dbg(&priv->udev->dev,
132 "Improper amount of data received from the device, "
133 "%d bytes", urb->actual_length);
136 exit:
137 spin_lock(&priv->lock);
139 /* Continue trying to always read if we should */
140 if (!priv->throttled) {
141 usb_fill_bulk_urb(priv->bulk_read_urb, priv->udev,
142 usb_rcvbulkpipe(priv->udev,
143 priv->bulk_address),
144 priv->bulk_in_buffer, priv->buffer_size,
145 opticon_read_bulk_callback, priv);
146 result = usb_submit_urb(priv->bulk_read_urb, GFP_ATOMIC);
147 if (result)
148 dev_err(&port->dev,
149 "%s - failed resubmitting read urb, error %d\n",
150 __func__, result);
151 } else
152 priv->actually_throttled = true;
153 spin_unlock(&priv->lock);
156 static int send_control_msg(struct usb_serial_port *port, u8 requesttype,
157 u8 val)
159 struct usb_serial *serial = port->serial;
160 int retval;
161 u8 *buffer;
163 buffer = kzalloc(1, GFP_KERNEL);
164 if (!buffer)
165 return -ENOMEM;
167 buffer[0] = val;
168 /* Send the message to the vendor control endpoint
169 * of the connected device */
170 retval = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
171 requesttype,
172 USB_DIR_OUT|USB_TYPE_VENDOR|USB_RECIP_INTERFACE,
173 0, 0, buffer, 1, 0);
174 kfree(buffer);
176 return retval;
179 static int opticon_open(struct tty_struct *tty, struct usb_serial_port *port)
181 struct opticon_private *priv = usb_get_serial_data(port->serial);
182 unsigned long flags;
183 int result = 0;
185 spin_lock_irqsave(&priv->lock, flags);
186 priv->throttled = false;
187 priv->actually_throttled = false;
188 priv->port = port;
189 priv->rts = false;
190 spin_unlock_irqrestore(&priv->lock, flags);
192 /* Clear RTS line */
193 send_control_msg(port, CONTROL_RTS, 0);
195 /* Setup the read URB and start reading from the device */
196 usb_fill_bulk_urb(priv->bulk_read_urb, priv->udev,
197 usb_rcvbulkpipe(priv->udev,
198 priv->bulk_address),
199 priv->bulk_in_buffer, priv->buffer_size,
200 opticon_read_bulk_callback, priv);
202 /* clear the halt status of the enpoint */
203 usb_clear_halt(priv->udev, priv->bulk_read_urb->pipe);
205 result = usb_submit_urb(priv->bulk_read_urb, GFP_KERNEL);
206 if (result)
207 dev_err(&port->dev,
208 "%s - failed resubmitting read urb, error %d\n",
209 __func__, result);
210 /* Request CTS line state, sometimes during opening the current
211 * CTS state can be missed. */
212 send_control_msg(port, RESEND_CTS_STATE, 1);
213 return result;
216 static void opticon_close(struct usb_serial_port *port)
218 struct opticon_private *priv = usb_get_serial_data(port->serial);
220 /* shutdown our urbs */
221 usb_kill_urb(priv->bulk_read_urb);
224 static void opticon_write_control_callback(struct urb *urb)
226 struct opticon_private *priv = urb->context;
227 int status = urb->status;
228 unsigned long flags;
230 /* free up the transfer buffer, as usb_free_urb() does not do this */
231 kfree(urb->transfer_buffer);
233 /* setup packet may be set if we're using it for writing */
234 kfree(urb->setup_packet);
236 if (status)
237 dbg("%s - nonzero write bulk status received: %d",
238 __func__, status);
240 spin_lock_irqsave(&priv->lock, flags);
241 --priv->outstanding_urbs;
242 spin_unlock_irqrestore(&priv->lock, flags);
244 usb_serial_port_softint(priv->port);
247 static int opticon_write(struct tty_struct *tty, struct usb_serial_port *port,
248 const unsigned char *buf, int count)
250 struct opticon_private *priv = usb_get_serial_data(port->serial);
251 struct usb_serial *serial = port->serial;
252 struct urb *urb;
253 unsigned char *buffer;
254 unsigned long flags;
255 int status;
256 struct usb_ctrlrequest *dr;
258 spin_lock_irqsave(&priv->lock, flags);
259 if (priv->outstanding_urbs > URB_UPPER_LIMIT) {
260 spin_unlock_irqrestore(&priv->lock, flags);
261 dbg("%s - write limit hit", __func__);
262 return 0;
264 priv->outstanding_urbs++;
265 spin_unlock_irqrestore(&priv->lock, flags);
267 buffer = kmalloc(count, GFP_ATOMIC);
268 if (!buffer) {
269 dev_err(&port->dev, "out of memory\n");
270 count = -ENOMEM;
272 goto error_no_buffer;
275 urb = usb_alloc_urb(0, GFP_ATOMIC);
276 if (!urb) {
277 dev_err(&port->dev, "no more free urbs\n");
278 count = -ENOMEM;
279 goto error_no_urb;
282 memcpy(buffer, buf, count);
284 usb_serial_debug_data(debug, &port->dev, __func__, count, buffer);
286 /* The conncected devices do not have a bulk write endpoint,
287 * to transmit data to de barcode device the control endpoint is used */
288 dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_NOIO);
289 if (!dr) {
290 dev_err(&port->dev, "out of memory\n");
291 count = -ENOMEM;
292 goto error_no_dr;
295 dr->bRequestType = USB_TYPE_VENDOR | USB_RECIP_INTERFACE | USB_DIR_OUT;
296 dr->bRequest = 0x01;
297 dr->wValue = 0;
298 dr->wIndex = 0;
299 dr->wLength = cpu_to_le16(count);
301 usb_fill_control_urb(urb, serial->dev,
302 usb_sndctrlpipe(serial->dev, 0),
303 (unsigned char *)dr, buffer, count,
304 opticon_write_control_callback, priv);
306 /* send it down the pipe */
307 status = usb_submit_urb(urb, GFP_ATOMIC);
308 if (status) {
309 dev_err(&port->dev,
310 "%s - usb_submit_urb(write endpoint) failed status = %d\n",
311 __func__, status);
312 count = status;
313 goto error;
316 /* we are done with this urb, so let the host driver
317 * really free it when it is finished with it */
318 usb_free_urb(urb);
320 return count;
321 error:
322 kfree(dr);
323 error_no_dr:
324 usb_free_urb(urb);
325 error_no_urb:
326 kfree(buffer);
327 error_no_buffer:
328 spin_lock_irqsave(&priv->lock, flags);
329 --priv->outstanding_urbs;
330 spin_unlock_irqrestore(&priv->lock, flags);
331 return count;
334 static int opticon_write_room(struct tty_struct *tty)
336 struct usb_serial_port *port = tty->driver_data;
337 struct opticon_private *priv = usb_get_serial_data(port->serial);
338 unsigned long flags;
341 * We really can take almost anything the user throws at us
342 * but let's pick a nice big number to tell the tty
343 * layer that we have lots of free space, unless we don't.
345 spin_lock_irqsave(&priv->lock, flags);
346 if (priv->outstanding_urbs > URB_UPPER_LIMIT * 2 / 3) {
347 spin_unlock_irqrestore(&priv->lock, flags);
348 dbg("%s - write limit hit", __func__);
349 return 0;
351 spin_unlock_irqrestore(&priv->lock, flags);
353 return 2048;
356 static void opticon_throttle(struct tty_struct *tty)
358 struct usb_serial_port *port = tty->driver_data;
359 struct opticon_private *priv = usb_get_serial_data(port->serial);
360 unsigned long flags;
362 spin_lock_irqsave(&priv->lock, flags);
363 priv->throttled = true;
364 spin_unlock_irqrestore(&priv->lock, flags);
368 static void opticon_unthrottle(struct tty_struct *tty)
370 struct usb_serial_port *port = tty->driver_data;
371 struct opticon_private *priv = usb_get_serial_data(port->serial);
372 unsigned long flags;
373 int result, was_throttled;
375 spin_lock_irqsave(&priv->lock, flags);
376 priv->throttled = false;
377 was_throttled = priv->actually_throttled;
378 priv->actually_throttled = false;
379 spin_unlock_irqrestore(&priv->lock, flags);
381 if (was_throttled) {
382 result = usb_submit_urb(priv->bulk_read_urb, GFP_ATOMIC);
383 if (result)
384 dev_err(&port->dev,
385 "%s - failed submitting read urb, error %d\n",
386 __func__, result);
390 static int opticon_tiocmget(struct tty_struct *tty)
392 struct usb_serial_port *port = tty->driver_data;
393 struct opticon_private *priv = usb_get_serial_data(port->serial);
394 unsigned long flags;
395 int result = 0;
397 spin_lock_irqsave(&priv->lock, flags);
398 if (priv->rts)
399 result |= TIOCM_RTS;
400 if (priv->cts)
401 result |= TIOCM_CTS;
402 spin_unlock_irqrestore(&priv->lock, flags);
404 dbg("%s - %x", __func__, result);
405 return result;
408 static int opticon_tiocmset(struct tty_struct *tty,
409 unsigned int set, unsigned int clear)
411 struct usb_serial_port *port = tty->driver_data;
412 struct usb_serial *serial = port->serial;
413 struct opticon_private *priv = usb_get_serial_data(port->serial);
414 unsigned long flags;
415 bool rts;
416 bool changed = false;
417 int ret;
419 /* We only support RTS so we only handle that */
420 spin_lock_irqsave(&priv->lock, flags);
422 rts = priv->rts;
423 if (set & TIOCM_RTS)
424 priv->rts = true;
425 if (clear & TIOCM_RTS)
426 priv->rts = false;
427 changed = rts ^ priv->rts;
428 spin_unlock_irqrestore(&priv->lock, flags);
430 if (!changed)
431 return 0;
433 /* Send the new RTS state to the connected device */
434 mutex_lock(&serial->disc_mutex);
435 if (!serial->disconnected)
436 ret = send_control_msg(port, CONTROL_RTS, !rts);
437 else
438 ret = -ENODEV;
439 mutex_unlock(&serial->disc_mutex);
441 return ret;
444 static int get_serial_info(struct opticon_private *priv,
445 struct serial_struct __user *serial)
447 struct serial_struct tmp;
449 if (!serial)
450 return -EFAULT;
452 memset(&tmp, 0x00, sizeof(tmp));
454 /* fake emulate a 16550 uart to make userspace code happy */
455 tmp.type = PORT_16550A;
456 tmp.line = priv->serial->minor;
457 tmp.port = 0;
458 tmp.irq = 0;
459 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
460 tmp.xmit_fifo_size = 1024;
461 tmp.baud_base = 9600;
462 tmp.close_delay = 5*HZ;
463 tmp.closing_wait = 30*HZ;
465 if (copy_to_user(serial, &tmp, sizeof(*serial)))
466 return -EFAULT;
467 return 0;
470 static int opticon_ioctl(struct tty_struct *tty,
471 unsigned int cmd, unsigned long arg)
473 struct usb_serial_port *port = tty->driver_data;
474 struct opticon_private *priv = usb_get_serial_data(port->serial);
476 dbg("%s - port %d, cmd = 0x%x", __func__, port->number, cmd);
478 switch (cmd) {
479 case TIOCGSERIAL:
480 return get_serial_info(priv,
481 (struct serial_struct __user *)arg);
484 return -ENOIOCTLCMD;
487 static int opticon_startup(struct usb_serial *serial)
489 struct opticon_private *priv;
490 struct usb_host_interface *intf;
491 int i;
492 int retval = -ENOMEM;
493 bool bulk_in_found = false;
495 /* create our private serial structure */
496 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
497 if (priv == NULL) {
498 dev_err(&serial->dev->dev, "%s - Out of memory\n", __func__);
499 return -ENOMEM;
501 spin_lock_init(&priv->lock);
502 priv->serial = serial;
503 priv->port = serial->port[0];
504 priv->udev = serial->dev;
505 priv->outstanding_urbs = 0; /* Init the outstanding urbs */
507 /* find our bulk endpoint */
508 intf = serial->interface->altsetting;
509 for (i = 0; i < intf->desc.bNumEndpoints; ++i) {
510 struct usb_endpoint_descriptor *endpoint;
512 endpoint = &intf->endpoint[i].desc;
513 if (!usb_endpoint_is_bulk_in(endpoint))
514 continue;
516 priv->bulk_read_urb = usb_alloc_urb(0, GFP_KERNEL);
517 if (!priv->bulk_read_urb) {
518 dev_err(&priv->udev->dev, "out of memory\n");
519 goto error;
522 priv->buffer_size = usb_endpoint_maxp(endpoint) * 2;
523 priv->bulk_in_buffer = kmalloc(priv->buffer_size, GFP_KERNEL);
524 if (!priv->bulk_in_buffer) {
525 dev_err(&priv->udev->dev, "out of memory\n");
526 goto error;
529 priv->bulk_address = endpoint->bEndpointAddress;
531 bulk_in_found = true;
532 break;
535 if (!bulk_in_found) {
536 dev_err(&priv->udev->dev,
537 "Error - the proper endpoints were not found!\n");
538 goto error;
541 usb_set_serial_data(serial, priv);
542 return 0;
544 error:
545 usb_free_urb(priv->bulk_read_urb);
546 kfree(priv->bulk_in_buffer);
547 kfree(priv);
548 return retval;
551 static void opticon_disconnect(struct usb_serial *serial)
553 struct opticon_private *priv = usb_get_serial_data(serial);
555 usb_kill_urb(priv->bulk_read_urb);
556 usb_free_urb(priv->bulk_read_urb);
559 static void opticon_release(struct usb_serial *serial)
561 struct opticon_private *priv = usb_get_serial_data(serial);
563 kfree(priv->bulk_in_buffer);
564 kfree(priv);
567 static int opticon_suspend(struct usb_serial *serial, pm_message_t message)
569 struct opticon_private *priv = usb_get_serial_data(serial);
571 usb_kill_urb(priv->bulk_read_urb);
572 return 0;
575 static int opticon_resume(struct usb_serial *serial)
577 struct opticon_private *priv = usb_get_serial_data(serial);
578 struct usb_serial_port *port = serial->port[0];
579 int result;
581 mutex_lock(&port->port.mutex);
582 /* This is protected by the port mutex against close/open */
583 if (test_bit(ASYNCB_INITIALIZED, &port->port.flags))
584 result = usb_submit_urb(priv->bulk_read_urb, GFP_NOIO);
585 else
586 result = 0;
587 mutex_unlock(&port->port.mutex);
588 return result;
591 static struct usb_serial_driver opticon_device = {
592 .driver = {
593 .owner = THIS_MODULE,
594 .name = "opticon",
596 .id_table = id_table,
597 .num_ports = 1,
598 .attach = opticon_startup,
599 .open = opticon_open,
600 .close = opticon_close,
601 .write = opticon_write,
602 .write_room = opticon_write_room,
603 .disconnect = opticon_disconnect,
604 .release = opticon_release,
605 .throttle = opticon_throttle,
606 .unthrottle = opticon_unthrottle,
607 .ioctl = opticon_ioctl,
608 .tiocmget = opticon_tiocmget,
609 .tiocmset = opticon_tiocmset,
610 .suspend = opticon_suspend,
611 .resume = opticon_resume,
614 static struct usb_serial_driver * const serial_drivers[] = {
615 &opticon_device, NULL
618 module_usb_serial_driver(serial_drivers, id_table);
620 MODULE_DESCRIPTION(DRIVER_DESC);
621 MODULE_LICENSE("GPL");
623 module_param(debug, bool, S_IRUGO | S_IWUSR);
624 MODULE_PARM_DESC(debug, "Debug enabled or not");