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