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