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