USB: opticon: use port as urb context
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / usb / serial / opticon.c
CommitLineData
57262b82
GKH
1/*
2 * Opticon USB barcode to serial driver
3 *
309a0579 4 * Copyright (C) 2011 Martin Jansen <martin.jansen@opticon.com>
648d4e16
GKH
5 * Copyright (C) 2008 - 2009 Greg Kroah-Hartman <gregkh@suse.de>
6 * Copyright (C) 2008 - 2009 Novell Inc.
57262b82
GKH
7 *
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.
11 */
12
13#include <linux/kernel.h>
14#include <linux/init.h>
15#include <linux/tty.h>
16#include <linux/tty_driver.h>
5a0e3ad6 17#include <linux/slab.h>
57262b82 18#include <linux/tty_flip.h>
faac64ad 19#include <linux/serial.h>
57262b82
GKH
20#include <linux/module.h>
21#include <linux/usb.h>
22#include <linux/usb/serial.h>
23#include <linux/uaccess.h>
24
309a0579
MJ
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
7d40d7e8 35static const struct usb_device_id id_table[] = {
57262b82
GKH
36 { USB_DEVICE(0x065a, 0x0009) },
37 { },
38};
39MODULE_DEVICE_TABLE(usb, id_table);
40
41/* This structure holds all of the individual device information */
42struct opticon_private {
57262b82
GKH
43 unsigned char *bulk_in_buffer;
44 struct urb *bulk_read_urb;
45 int buffer_size;
46 u8 bulk_address;
47 spinlock_t lock; /* protects the following flags */
48 bool throttled;
49 bool actually_throttled;
50 bool rts;
309a0579 51 bool cts;
648d4e16 52 int outstanding_urbs;
57262b82
GKH
53};
54
648d4e16 55
309a0579
MJ
56
57static void opticon_read_bulk_callback(struct urb *urb)
57262b82 58{
e32d82bc
JH
59 struct usb_serial_port *port = urb->context;
60 struct opticon_private *priv = usb_get_serial_port_data(port);
57262b82 61 unsigned char *data = urb->transfer_buffer;
57262b82
GKH
62 int status = urb->status;
63 struct tty_struct *tty;
64 int result;
57262b82 65 int data_length;
309a0579 66 unsigned long flags;
57262b82 67
57262b82
GKH
68 switch (status) {
69 case 0:
70 /* success */
71 break;
72 case -ECONNRESET:
73 case -ENOENT:
74 case -ESHUTDOWN:
75 /* this urb is terminated, clean up */
e29a7738 76 dev_dbg(&port->dev, "%s - urb shutting down with status: %d\n",
d44d9ab7 77 __func__, status);
57262b82
GKH
78 return;
79 default:
e29a7738 80 dev_dbg(&port->dev, "%s - nonzero urb status received: %d\n",
d44d9ab7 81 __func__, status);
57262b82
GKH
82 goto exit;
83 }
84
59d33f2f 85 usb_serial_debug_data(&port->dev, __func__, urb->actual_length, data);
57262b82
GKH
86
87 if (urb->actual_length > 2) {
88 data_length = urb->actual_length - 2;
89
90 /*
91 * Data from the device comes with a 2 byte header:
92 *
93 * <0x00><0x00>data...
309a0579 94 * This is real data to be sent to the tty layer
57262b82 95 * <0x00><0x01)level
309a0579
MJ
96 * This is a CTS level change, the third byte is the CTS
97 * value (0 for low, 1 for high).
57262b82
GKH
98 */
99 if ((data[0] == 0x00) && (data[1] == 0x00)) {
100 /* real data, send it to the tty layer */
101 tty = tty_port_tty_get(&port->port);
102 if (tty) {
97cd8dc4
AZ
103 tty_insert_flip_string(tty, data + 2,
104 data_length);
a108bfcb 105 tty_flip_buffer_push(tty);
57262b82
GKH
106 tty_kref_put(tty);
107 }
108 } else {
109 if ((data[0] == 0x00) && (data[1] == 0x01)) {
309a0579 110 spin_lock_irqsave(&priv->lock, flags);
25985edc 111 /* CTS status information package */
57262b82 112 if (data[2] == 0x00)
309a0579 113 priv->cts = false;
57262b82 114 else
309a0579
MJ
115 priv->cts = true;
116 spin_unlock_irqrestore(&priv->lock, flags);
57262b82 117 } else {
e29a7738 118 dev_dbg(&port->dev,
c6f694af
AZ
119 "Unknown data packet received from the device:"
120 " %2x %2x\n",
121 data[0], data[1]);
57262b82
GKH
122 }
123 }
124 } else {
e29a7738 125 dev_dbg(&port->dev,
9ddc5b6f 126 "Improper amount of data received from the device, "
57262b82
GKH
127 "%d bytes", urb->actual_length);
128 }
129
130exit:
131 spin_lock(&priv->lock);
132
133 /* Continue trying to always read if we should */
134 if (!priv->throttled) {
97cd8dc4 135 result = usb_submit_urb(priv->bulk_read_urb, GFP_ATOMIC);
57262b82
GKH
136 if (result)
137 dev_err(&port->dev,
138 "%s - failed resubmitting read urb, error %d\n",
139 __func__, result);
140 } else
141 priv->actually_throttled = true;
142 spin_unlock(&priv->lock);
143}
144
309a0579
MJ
145static int send_control_msg(struct usb_serial_port *port, u8 requesttype,
146 u8 val)
147{
148 struct usb_serial *serial = port->serial;
149 int retval;
ea0dbebf
JH
150 u8 *buffer;
151
152 buffer = kzalloc(1, GFP_KERNEL);
153 if (!buffer)
154 return -ENOMEM;
309a0579
MJ
155
156 buffer[0] = val;
157 /* Send the message to the vendor control endpoint
158 * of the connected device */
159 retval = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
160 requesttype,
161 USB_DIR_OUT|USB_TYPE_VENDOR|USB_RECIP_INTERFACE,
162 0, 0, buffer, 1, 0);
ea0dbebf 163 kfree(buffer);
309a0579
MJ
164
165 return retval;
166}
167
a509a7e4 168static int opticon_open(struct tty_struct *tty, struct usb_serial_port *port)
57262b82 169{
70d25eee 170 struct opticon_private *priv = usb_get_serial_port_data(port);
57262b82
GKH
171 unsigned long flags;
172 int result = 0;
173
57262b82
GKH
174 spin_lock_irqsave(&priv->lock, flags);
175 priv->throttled = false;
176 priv->actually_throttled = false;
309a0579 177 priv->rts = false;
57262b82
GKH
178 spin_unlock_irqrestore(&priv->lock, flags);
179
309a0579
MJ
180 /* Clear RTS line */
181 send_control_msg(port, CONTROL_RTS, 0);
182
309a0579 183 /* clear the halt status of the enpoint */
3157fad9 184 usb_clear_halt(port->serial->dev, priv->bulk_read_urb->pipe);
309a0579 185
57262b82
GKH
186 result = usb_submit_urb(priv->bulk_read_urb, GFP_KERNEL);
187 if (result)
188 dev_err(&port->dev,
189 "%s - failed resubmitting read urb, error %d\n",
190 __func__, result);
309a0579
MJ
191 /* Request CTS line state, sometimes during opening the current
192 * CTS state can be missed. */
193 send_control_msg(port, RESEND_CTS_STATE, 1);
57262b82
GKH
194 return result;
195}
196
335f8514 197static void opticon_close(struct usb_serial_port *port)
57262b82 198{
70d25eee 199 struct opticon_private *priv = usb_get_serial_port_data(port);
57262b82 200
57262b82
GKH
201 /* shutdown our urbs */
202 usb_kill_urb(priv->bulk_read_urb);
203}
204
309a0579 205static void opticon_write_control_callback(struct urb *urb)
648d4e16 206{
e32d82bc
JH
207 struct usb_serial_port *port = urb->context;
208 struct opticon_private *priv = usb_get_serial_port_data(port);
648d4e16
GKH
209 int status = urb->status;
210 unsigned long flags;
211
212 /* free up the transfer buffer, as usb_free_urb() does not do this */
213 kfree(urb->transfer_buffer);
214
0d930e51
AZ
215 /* setup packet may be set if we're using it for writing */
216 kfree(urb->setup_packet);
217
648d4e16 218 if (status)
e32d82bc 219 dev_dbg(&port->dev,
e29a7738 220 "%s - non-zero urb status received: %d\n",
d44d9ab7 221 __func__, status);
648d4e16
GKH
222
223 spin_lock_irqsave(&priv->lock, flags);
224 --priv->outstanding_urbs;
225 spin_unlock_irqrestore(&priv->lock, flags);
226
e32d82bc 227 usb_serial_port_softint(port);
648d4e16
GKH
228}
229
230static int opticon_write(struct tty_struct *tty, struct usb_serial_port *port,
231 const unsigned char *buf, int count)
232{
70d25eee 233 struct opticon_private *priv = usb_get_serial_port_data(port);
648d4e16
GKH
234 struct usb_serial *serial = port->serial;
235 struct urb *urb;
236 unsigned char *buffer;
237 unsigned long flags;
238 int status;
309a0579 239 struct usb_ctrlrequest *dr;
648d4e16 240
648d4e16
GKH
241 spin_lock_irqsave(&priv->lock, flags);
242 if (priv->outstanding_urbs > URB_UPPER_LIMIT) {
243 spin_unlock_irqrestore(&priv->lock, flags);
d44d9ab7 244 dev_dbg(&port->dev, "%s - write limit hit\n", __func__);
648d4e16
GKH
245 return 0;
246 }
247 priv->outstanding_urbs++;
248 spin_unlock_irqrestore(&priv->lock, flags);
249
250 buffer = kmalloc(count, GFP_ATOMIC);
251 if (!buffer) {
252 dev_err(&port->dev, "out of memory\n");
253 count = -ENOMEM;
309a0579 254
648d4e16
GKH
255 goto error_no_buffer;
256 }
257
258 urb = usb_alloc_urb(0, GFP_ATOMIC);
259 if (!urb) {
260 dev_err(&port->dev, "no more free urbs\n");
261 count = -ENOMEM;
262 goto error_no_urb;
263 }
264
265 memcpy(buffer, buf, count);
266
59d33f2f 267 usb_serial_debug_data(&port->dev, __func__, count, buffer);
648d4e16 268
309a0579
MJ
269 /* The conncected devices do not have a bulk write endpoint,
270 * to transmit data to de barcode device the control endpoint is used */
271 dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_NOIO);
b0795bbf
JL
272 if (!dr) {
273 dev_err(&port->dev, "out of memory\n");
274 count = -ENOMEM;
acbf0e52 275 goto error_no_dr;
b0795bbf 276 }
309a0579
MJ
277
278 dr->bRequestType = USB_TYPE_VENDOR | USB_RECIP_INTERFACE | USB_DIR_OUT;
279 dr->bRequest = 0x01;
280 dr->wValue = 0;
281 dr->wIndex = 0;
282 dr->wLength = cpu_to_le16(count);
283
284 usb_fill_control_urb(urb, serial->dev,
285 usb_sndctrlpipe(serial->dev, 0),
286 (unsigned char *)dr, buffer, count,
e32d82bc 287 opticon_write_control_callback, port);
648d4e16
GKH
288
289 /* send it down the pipe */
290 status = usb_submit_urb(urb, GFP_ATOMIC);
291 if (status) {
292 dev_err(&port->dev,
309a0579 293 "%s - usb_submit_urb(write endpoint) failed status = %d\n",
648d4e16
GKH
294 __func__, status);
295 count = status;
296 goto error;
297 }
298
299 /* we are done with this urb, so let the host driver
300 * really free it when it is finished with it */
301 usb_free_urb(urb);
302
303 return count;
304error:
acbf0e52
JH
305 kfree(dr);
306error_no_dr:
648d4e16
GKH
307 usb_free_urb(urb);
308error_no_urb:
309 kfree(buffer);
310error_no_buffer:
311 spin_lock_irqsave(&priv->lock, flags);
312 --priv->outstanding_urbs;
313 spin_unlock_irqrestore(&priv->lock, flags);
314 return count;
315}
316
317static int opticon_write_room(struct tty_struct *tty)
318{
319 struct usb_serial_port *port = tty->driver_data;
70d25eee 320 struct opticon_private *priv = usb_get_serial_port_data(port);
648d4e16
GKH
321 unsigned long flags;
322
648d4e16
GKH
323 /*
324 * We really can take almost anything the user throws at us
325 * but let's pick a nice big number to tell the tty
326 * layer that we have lots of free space, unless we don't.
327 */
328 spin_lock_irqsave(&priv->lock, flags);
329 if (priv->outstanding_urbs > URB_UPPER_LIMIT * 2 / 3) {
330 spin_unlock_irqrestore(&priv->lock, flags);
d44d9ab7 331 dev_dbg(&port->dev, "%s - write limit hit\n", __func__);
648d4e16
GKH
332 return 0;
333 }
334 spin_unlock_irqrestore(&priv->lock, flags);
335
336 return 2048;
337}
338
57262b82
GKH
339static void opticon_throttle(struct tty_struct *tty)
340{
341 struct usb_serial_port *port = tty->driver_data;
70d25eee 342 struct opticon_private *priv = usb_get_serial_port_data(port);
57262b82
GKH
343 unsigned long flags;
344
57262b82
GKH
345 spin_lock_irqsave(&priv->lock, flags);
346 priv->throttled = true;
347 spin_unlock_irqrestore(&priv->lock, flags);
348}
349
350
351static void opticon_unthrottle(struct tty_struct *tty)
352{
353 struct usb_serial_port *port = tty->driver_data;
70d25eee 354 struct opticon_private *priv = usb_get_serial_port_data(port);
57262b82 355 unsigned long flags;
88fa6590 356 int result, was_throttled;
57262b82 357
57262b82
GKH
358 spin_lock_irqsave(&priv->lock, flags);
359 priv->throttled = false;
88fa6590 360 was_throttled = priv->actually_throttled;
57262b82
GKH
361 priv->actually_throttled = false;
362 spin_unlock_irqrestore(&priv->lock, flags);
363
88fa6590
ON
364 if (was_throttled) {
365 result = usb_submit_urb(priv->bulk_read_urb, GFP_ATOMIC);
366 if (result)
367 dev_err(&port->dev,
368 "%s - failed submitting read urb, error %d\n",
57262b82 369 __func__, result);
88fa6590 370 }
57262b82
GKH
371}
372
60b33c13 373static int opticon_tiocmget(struct tty_struct *tty)
faac64ad
GKH
374{
375 struct usb_serial_port *port = tty->driver_data;
70d25eee 376 struct opticon_private *priv = usb_get_serial_port_data(port);
faac64ad
GKH
377 unsigned long flags;
378 int result = 0;
379
faac64ad
GKH
380 spin_lock_irqsave(&priv->lock, flags);
381 if (priv->rts)
309a0579
MJ
382 result |= TIOCM_RTS;
383 if (priv->cts)
384 result |= TIOCM_CTS;
faac64ad
GKH
385 spin_unlock_irqrestore(&priv->lock, flags);
386
d44d9ab7 387 dev_dbg(&port->dev, "%s - %x\n", __func__, result);
faac64ad
GKH
388 return result;
389}
390
4acfaf82 391static int opticon_tiocmset(struct tty_struct *tty,
309a0579
MJ
392 unsigned int set, unsigned int clear)
393{
394 struct usb_serial_port *port = tty->driver_data;
81d5a672 395 struct usb_serial *serial = port->serial;
70d25eee 396 struct opticon_private *priv = usb_get_serial_port_data(port);
309a0579
MJ
397 unsigned long flags;
398 bool rts;
399 bool changed = false;
81d5a672 400 int ret;
309a0579 401
309a0579
MJ
402 /* We only support RTS so we only handle that */
403 spin_lock_irqsave(&priv->lock, flags);
404
405 rts = priv->rts;
406 if (set & TIOCM_RTS)
407 priv->rts = true;
408 if (clear & TIOCM_RTS)
409 priv->rts = false;
410 changed = rts ^ priv->rts;
411 spin_unlock_irqrestore(&priv->lock, flags);
412
413 if (!changed)
414 return 0;
415
416 /* Send the new RTS state to the connected device */
81d5a672
JH
417 mutex_lock(&serial->disc_mutex);
418 if (!serial->disconnected)
419 ret = send_control_msg(port, CONTROL_RTS, !rts);
420 else
421 ret = -ENODEV;
422 mutex_unlock(&serial->disc_mutex);
423
424 return ret;
309a0579
MJ
425}
426
56be1a17 427static int get_serial_info(struct usb_serial_port *port,
faac64ad
GKH
428 struct serial_struct __user *serial)
429{
430 struct serial_struct tmp;
431
432 if (!serial)
433 return -EFAULT;
434
435 memset(&tmp, 0x00, sizeof(tmp));
436
437 /* fake emulate a 16550 uart to make userspace code happy */
438 tmp.type = PORT_16550A;
56be1a17 439 tmp.line = port->serial->minor;
faac64ad
GKH
440 tmp.port = 0;
441 tmp.irq = 0;
442 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
443 tmp.xmit_fifo_size = 1024;
444 tmp.baud_base = 9600;
445 tmp.close_delay = 5*HZ;
446 tmp.closing_wait = 30*HZ;
447
448 if (copy_to_user(serial, &tmp, sizeof(*serial)))
449 return -EFAULT;
450 return 0;
451}
452
00a0d0d6 453static int opticon_ioctl(struct tty_struct *tty,
faac64ad
GKH
454 unsigned int cmd, unsigned long arg)
455{
456 struct usb_serial_port *port = tty->driver_data;
faac64ad 457
d44d9ab7 458 dev_dbg(&port->dev, "%s - port %d, cmd = 0x%x\n", __func__, port->number, cmd);
faac64ad
GKH
459
460 switch (cmd) {
461 case TIOCGSERIAL:
56be1a17 462 return get_serial_info(port,
faac64ad
GKH
463 (struct serial_struct __user *)arg);
464 }
465
466 return -ENOIOCTLCMD;
467}
468
57262b82
GKH
469static int opticon_startup(struct usb_serial *serial)
470{
a0a5fd92
JH
471 if (!serial->num_bulk_in) {
472 dev_err(&serial->dev->dev, "no bulk in endpoint\n");
473 return -ENODEV;
474 }
57262b82 475
70d25eee
JH
476 return 0;
477}
478
479static int opticon_port_probe(struct usb_serial_port *port)
480{
481 struct usb_serial *serial = port->serial;
482 struct opticon_private *priv;
483 int retval = -ENOMEM;
484
57262b82 485 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
70d25eee 486 if (!priv)
57262b82 487 return -ENOMEM;
70d25eee 488
57262b82 489 spin_lock_init(&priv->lock);
57262b82 490
a0a5fd92 491 priv->bulk_read_urb = usb_alloc_urb(0, GFP_KERNEL);
70d25eee 492 if (!priv->bulk_read_urb)
a0a5fd92 493 goto error;
57262b82 494
70d25eee 495 priv->buffer_size = 2 * port->bulk_in_size;
a0a5fd92 496 priv->bulk_in_buffer = kmalloc(priv->buffer_size, GFP_KERNEL);
70d25eee 497 if (!priv->bulk_in_buffer)
57262b82 498 goto error;
57262b82 499
70d25eee 500 priv->bulk_address = port->bulk_in_endpointAddress;
a0a5fd92 501
0b8718a2
JH
502 usb_fill_bulk_urb(priv->bulk_read_urb, serial->dev,
503 usb_rcvbulkpipe(serial->dev,
504 priv->bulk_address),
505 priv->bulk_in_buffer, priv->buffer_size,
e32d82bc 506 opticon_read_bulk_callback, port);
0b8718a2 507
70d25eee 508 usb_set_serial_port_data(port, priv);
57262b82 509
70d25eee 510 return 0;
57262b82
GKH
511error:
512 usb_free_urb(priv->bulk_read_urb);
513 kfree(priv->bulk_in_buffer);
514 kfree(priv);
515 return retval;
516}
517
70d25eee 518static int opticon_port_remove(struct usb_serial_port *port)
f9c99bb8 519{
70d25eee 520 struct opticon_private *priv = usb_get_serial_port_data(port);
f9c99bb8 521
70f9bf65 522 usb_free_urb(priv->bulk_read_urb);
57262b82
GKH
523 kfree(priv->bulk_in_buffer);
524 kfree(priv);
70d25eee
JH
525
526 return 0;
57262b82
GKH
527}
528
d530296f 529static int opticon_suspend(struct usb_serial *serial, pm_message_t message)
d1c0713d 530{
70d25eee
JH
531 struct opticon_private *priv;
532
533 priv = usb_get_serial_port_data(serial->port[0]);
d1c0713d
ON
534
535 usb_kill_urb(priv->bulk_read_urb);
536 return 0;
537}
538
d530296f 539static int opticon_resume(struct usb_serial *serial)
d1c0713d 540{
d1c0713d 541 struct usb_serial_port *port = serial->port[0];
70d25eee 542 struct opticon_private *priv = usb_get_serial_port_data(port);
d1c0713d
ON
543 int result;
544
82fc5943 545 mutex_lock(&port->port.mutex);
2a0785ea
AC
546 /* This is protected by the port mutex against close/open */
547 if (test_bit(ASYNCB_INITIALIZED, &port->port.flags))
d1c0713d
ON
548 result = usb_submit_urb(priv->bulk_read_urb, GFP_NOIO);
549 else
550 result = 0;
82fc5943 551 mutex_unlock(&port->port.mutex);
d1c0713d
ON
552 return result;
553}
57262b82 554
57262b82
GKH
555static struct usb_serial_driver opticon_device = {
556 .driver = {
557 .owner = THIS_MODULE,
558 .name = "opticon",
559 },
560 .id_table = id_table,
57262b82
GKH
561 .num_ports = 1,
562 .attach = opticon_startup,
70d25eee
JH
563 .port_probe = opticon_port_probe,
564 .port_remove = opticon_port_remove,
57262b82
GKH
565 .open = opticon_open,
566 .close = opticon_close,
648d4e16
GKH
567 .write = opticon_write,
568 .write_room = opticon_write_room,
57262b82
GKH
569 .throttle = opticon_throttle,
570 .unthrottle = opticon_unthrottle,
faac64ad
GKH
571 .ioctl = opticon_ioctl,
572 .tiocmget = opticon_tiocmget,
309a0579 573 .tiocmset = opticon_tiocmset,
d530296f
GKH
574 .suspend = opticon_suspend,
575 .resume = opticon_resume,
57262b82
GKH
576};
577
f667ddad
AS
578static struct usb_serial_driver * const serial_drivers[] = {
579 &opticon_device, NULL
580};
581
68e24113 582module_usb_serial_driver(serial_drivers, id_table);
57262b82 583
309a0579 584MODULE_DESCRIPTION(DRIVER_DESC);
57262b82 585MODULE_LICENSE("GPL");