blob: 4fe7c3d9ef984b53e34623f8a1587412218a33f6 [file] [log] [blame]
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -08001/*
2 * Opticon USB barcode to serial driver
3 *
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -08004 * Copyright (C) 2008 - 2009 Greg Kroah-Hartman <gregkh@suse.de>
5 * Copyright (C) 2008 - 2009 Novell Inc.
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -08006 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License version
9 * 2 as published by the Free Software Foundation.
10 */
11
12#include <linux/kernel.h>
13#include <linux/init.h>
14#include <linux/tty.h>
15#include <linux/tty_driver.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090016#include <linux/slab.h>
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -080017#include <linux/tty_flip.h>
Greg Kroah-Hartmanfaac64a2009-02-06 18:31:46 -080018#include <linux/serial.h>
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -080019#include <linux/module.h>
20#include <linux/usb.h>
21#include <linux/usb/serial.h>
22#include <linux/uaccess.h>
23
24static int debug;
25
Németh Márton7d40d7e2010-01-10 15:34:24 +010026static const struct usb_device_id id_table[] = {
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -080027 { USB_DEVICE(0x065a, 0x0009) },
28 { },
29};
30MODULE_DEVICE_TABLE(usb, id_table);
31
32/* This structure holds all of the individual device information */
33struct opticon_private {
34 struct usb_device *udev;
35 struct usb_serial *serial;
36 struct usb_serial_port *port;
37 unsigned char *bulk_in_buffer;
38 struct urb *bulk_read_urb;
39 int buffer_size;
40 u8 bulk_address;
41 spinlock_t lock; /* protects the following flags */
42 bool throttled;
43 bool actually_throttled;
44 bool rts;
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -080045 int outstanding_urbs;
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -080046};
47
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -080048/* max number of write urbs in flight */
49#define URB_UPPER_LIMIT 4
50
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -080051static void opticon_bulk_callback(struct urb *urb)
52{
53 struct opticon_private *priv = urb->context;
54 unsigned char *data = urb->transfer_buffer;
55 struct usb_serial_port *port = priv->port;
56 int status = urb->status;
57 struct tty_struct *tty;
58 int result;
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -080059 int data_length;
60
61 dbg("%s - port %d", __func__, port->number);
62
63 switch (status) {
64 case 0:
65 /* success */
66 break;
67 case -ECONNRESET:
68 case -ENOENT:
69 case -ESHUTDOWN:
70 /* this urb is terminated, clean up */
71 dbg("%s - urb shutting down with status: %d",
72 __func__, status);
73 return;
74 default:
75 dbg("%s - nonzero urb status received: %d",
76 __func__, status);
77 goto exit;
78 }
79
80 usb_serial_debug_data(debug, &port->dev, __func__, urb->actual_length,
81 data);
82
83 if (urb->actual_length > 2) {
84 data_length = urb->actual_length - 2;
85
86 /*
87 * Data from the device comes with a 2 byte header:
88 *
89 * <0x00><0x00>data...
90 * This is real data to be sent to the tty layer
91 * <0x00><0x01)level
92 * This is a RTS level change, the third byte is the RTS
93 * value (0 for low, 1 for high).
94 */
95 if ((data[0] == 0x00) && (data[1] == 0x00)) {
96 /* real data, send it to the tty layer */
97 tty = tty_port_tty_get(&port->port);
98 if (tty) {
Alon Ziv97cd8dc2010-10-10 08:32:18 +020099 tty_insert_flip_string(tty, data + 2,
100 data_length);
Alan Coxa108bfc2010-02-18 16:44:01 +0000101 tty_flip_buffer_push(tty);
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800102 tty_kref_put(tty);
103 }
104 } else {
105 if ((data[0] == 0x00) && (data[1] == 0x01)) {
106 if (data[2] == 0x00)
107 priv->rts = false;
108 else
109 priv->rts = true;
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800110 } else {
111 dev_dbg(&priv->udev->dev,
112 "Unknown data packet received from the device:"
113 " %2x %2x\n",
114 data[0], data[1]);
115 }
116 }
117 } else {
118 dev_dbg(&priv->udev->dev,
Uwe Kleine-König9ddc5b62010-01-20 17:02:24 +0100119 "Improper amount of data received from the device, "
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800120 "%d bytes", urb->actual_length);
121 }
122
123exit:
124 spin_lock(&priv->lock);
125
126 /* Continue trying to always read if we should */
127 if (!priv->throttled) {
128 usb_fill_bulk_urb(priv->bulk_read_urb, priv->udev,
129 usb_rcvbulkpipe(priv->udev,
130 priv->bulk_address),
131 priv->bulk_in_buffer, priv->buffer_size,
132 opticon_bulk_callback, priv);
Alon Ziv97cd8dc2010-10-10 08:32:18 +0200133 result = usb_submit_urb(priv->bulk_read_urb, GFP_ATOMIC);
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800134 if (result)
135 dev_err(&port->dev,
136 "%s - failed resubmitting read urb, error %d\n",
137 __func__, result);
138 } else
139 priv->actually_throttled = true;
140 spin_unlock(&priv->lock);
141}
142
Alan Coxa509a7e2009-09-19 13:13:26 -0700143static int opticon_open(struct tty_struct *tty, struct usb_serial_port *port)
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800144{
145 struct opticon_private *priv = usb_get_serial_data(port->serial);
146 unsigned long flags;
147 int result = 0;
148
149 dbg("%s - port %d", __func__, port->number);
150
151 spin_lock_irqsave(&priv->lock, flags);
152 priv->throttled = false;
153 priv->actually_throttled = false;
154 priv->port = port;
155 spin_unlock_irqrestore(&priv->lock, flags);
156
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800157 /* Start reading from the device */
158 usb_fill_bulk_urb(priv->bulk_read_urb, priv->udev,
159 usb_rcvbulkpipe(priv->udev,
160 priv->bulk_address),
161 priv->bulk_in_buffer, priv->buffer_size,
162 opticon_bulk_callback, priv);
163 result = usb_submit_urb(priv->bulk_read_urb, GFP_KERNEL);
164 if (result)
165 dev_err(&port->dev,
166 "%s - failed resubmitting read urb, error %d\n",
167 __func__, result);
168 return result;
169}
170
Alan Cox335f8512009-06-11 12:26:29 +0100171static void opticon_close(struct usb_serial_port *port)
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800172{
173 struct opticon_private *priv = usb_get_serial_data(port->serial);
174
175 dbg("%s - port %d", __func__, port->number);
176
177 /* shutdown our urbs */
178 usb_kill_urb(priv->bulk_read_urb);
179}
180
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -0800181static void opticon_write_bulk_callback(struct urb *urb)
182{
183 struct opticon_private *priv = urb->context;
184 int status = urb->status;
185 unsigned long flags;
186
187 /* free up the transfer buffer, as usb_free_urb() does not do this */
188 kfree(urb->transfer_buffer);
189
Alon Ziv0d930e52010-10-10 08:32:19 +0200190 /* setup packet may be set if we're using it for writing */
191 kfree(urb->setup_packet);
192
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -0800193 if (status)
194 dbg("%s - nonzero write bulk status received: %d",
195 __func__, status);
196
197 spin_lock_irqsave(&priv->lock, flags);
198 --priv->outstanding_urbs;
199 spin_unlock_irqrestore(&priv->lock, flags);
200
201 usb_serial_port_softint(priv->port);
202}
203
204static int opticon_write(struct tty_struct *tty, struct usb_serial_port *port,
205 const unsigned char *buf, int count)
206{
207 struct opticon_private *priv = usb_get_serial_data(port->serial);
208 struct usb_serial *serial = port->serial;
209 struct urb *urb;
210 unsigned char *buffer;
211 unsigned long flags;
212 int status;
213
214 dbg("%s - port %d", __func__, port->number);
215
216 spin_lock_irqsave(&priv->lock, flags);
217 if (priv->outstanding_urbs > URB_UPPER_LIMIT) {
218 spin_unlock_irqrestore(&priv->lock, flags);
Joe Perches759f3632010-02-05 16:50:08 -0800219 dbg("%s - write limit hit", __func__);
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -0800220 return 0;
221 }
222 priv->outstanding_urbs++;
223 spin_unlock_irqrestore(&priv->lock, flags);
224
225 buffer = kmalloc(count, GFP_ATOMIC);
226 if (!buffer) {
227 dev_err(&port->dev, "out of memory\n");
228 count = -ENOMEM;
229 goto error_no_buffer;
230 }
231
232 urb = usb_alloc_urb(0, GFP_ATOMIC);
233 if (!urb) {
234 dev_err(&port->dev, "no more free urbs\n");
235 count = -ENOMEM;
236 goto error_no_urb;
237 }
238
239 memcpy(buffer, buf, count);
240
241 usb_serial_debug_data(debug, &port->dev, __func__, count, buffer);
242
Alon Ziv0d930e52010-10-10 08:32:19 +0200243 if (port->bulk_out_endpointAddress) {
244 usb_fill_bulk_urb(urb, serial->dev,
245 usb_sndbulkpipe(serial->dev,
246 port->bulk_out_endpointAddress),
247 buffer, count, opticon_write_bulk_callback, priv);
248 } else {
249 struct usb_ctrlrequest *dr;
250
251 dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_NOIO);
252 if (!dr)
253 return -ENOMEM;
254
255 dr->bRequestType = USB_TYPE_VENDOR | USB_RECIP_INTERFACE | USB_DIR_OUT;
256 dr->bRequest = 0x01;
257 dr->wValue = 0;
258 dr->wIndex = 0;
259 dr->wLength = cpu_to_le16(count);
260
261 usb_fill_control_urb(urb, serial->dev,
262 usb_sndctrlpipe(serial->dev, 0),
263 (unsigned char *)dr, buffer, count,
264 opticon_write_bulk_callback, priv);
265 }
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -0800266
267 /* send it down the pipe */
268 status = usb_submit_urb(urb, GFP_ATOMIC);
269 if (status) {
270 dev_err(&port->dev,
271 "%s - usb_submit_urb(write bulk) failed with status = %d\n",
272 __func__, status);
273 count = status;
274 goto error;
275 }
276
277 /* we are done with this urb, so let the host driver
278 * really free it when it is finished with it */
279 usb_free_urb(urb);
280
281 return count;
282error:
283 usb_free_urb(urb);
284error_no_urb:
285 kfree(buffer);
286error_no_buffer:
287 spin_lock_irqsave(&priv->lock, flags);
288 --priv->outstanding_urbs;
289 spin_unlock_irqrestore(&priv->lock, flags);
290 return count;
291}
292
293static int opticon_write_room(struct tty_struct *tty)
294{
295 struct usb_serial_port *port = tty->driver_data;
296 struct opticon_private *priv = usb_get_serial_data(port->serial);
297 unsigned long flags;
298
299 dbg("%s - port %d", __func__, port->number);
300
301 /*
302 * We really can take almost anything the user throws at us
303 * but let's pick a nice big number to tell the tty
304 * layer that we have lots of free space, unless we don't.
305 */
306 spin_lock_irqsave(&priv->lock, flags);
307 if (priv->outstanding_urbs > URB_UPPER_LIMIT * 2 / 3) {
308 spin_unlock_irqrestore(&priv->lock, flags);
Joe Perches759f3632010-02-05 16:50:08 -0800309 dbg("%s - write limit hit", __func__);
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -0800310 return 0;
311 }
312 spin_unlock_irqrestore(&priv->lock, flags);
313
314 return 2048;
315}
316
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800317static void opticon_throttle(struct tty_struct *tty)
318{
319 struct usb_serial_port *port = tty->driver_data;
320 struct opticon_private *priv = usb_get_serial_data(port->serial);
321 unsigned long flags;
322
323 dbg("%s - port %d", __func__, port->number);
324 spin_lock_irqsave(&priv->lock, flags);
325 priv->throttled = true;
326 spin_unlock_irqrestore(&priv->lock, flags);
327}
328
329
330static void opticon_unthrottle(struct tty_struct *tty)
331{
332 struct usb_serial_port *port = tty->driver_data;
333 struct opticon_private *priv = usb_get_serial_data(port->serial);
334 unsigned long flags;
Oliver Neukum88fa6592009-10-07 09:25:10 +0200335 int result, was_throttled;
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800336
337 dbg("%s - port %d", __func__, port->number);
338
339 spin_lock_irqsave(&priv->lock, flags);
340 priv->throttled = false;
Oliver Neukum88fa6592009-10-07 09:25:10 +0200341 was_throttled = priv->actually_throttled;
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800342 priv->actually_throttled = false;
343 spin_unlock_irqrestore(&priv->lock, flags);
344
345 priv->bulk_read_urb->dev = port->serial->dev;
Oliver Neukum88fa6592009-10-07 09:25:10 +0200346 if (was_throttled) {
347 result = usb_submit_urb(priv->bulk_read_urb, GFP_ATOMIC);
348 if (result)
349 dev_err(&port->dev,
350 "%s - failed submitting read urb, error %d\n",
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800351 __func__, result);
Oliver Neukum88fa6592009-10-07 09:25:10 +0200352 }
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800353}
354
Greg Kroah-Hartmanfaac64a2009-02-06 18:31:46 -0800355static int opticon_tiocmget(struct tty_struct *tty, struct file *file)
356{
357 struct usb_serial_port *port = tty->driver_data;
358 struct opticon_private *priv = usb_get_serial_data(port->serial);
359 unsigned long flags;
360 int result = 0;
361
362 dbg("%s - port %d", __func__, port->number);
363
364 spin_lock_irqsave(&priv->lock, flags);
365 if (priv->rts)
366 result = TIOCM_RTS;
367 spin_unlock_irqrestore(&priv->lock, flags);
368
369 dbg("%s - %x", __func__, result);
370 return result;
371}
372
373static int get_serial_info(struct opticon_private *priv,
374 struct serial_struct __user *serial)
375{
376 struct serial_struct tmp;
377
378 if (!serial)
379 return -EFAULT;
380
381 memset(&tmp, 0x00, sizeof(tmp));
382
383 /* fake emulate a 16550 uart to make userspace code happy */
384 tmp.type = PORT_16550A;
385 tmp.line = priv->serial->minor;
386 tmp.port = 0;
387 tmp.irq = 0;
388 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
389 tmp.xmit_fifo_size = 1024;
390 tmp.baud_base = 9600;
391 tmp.close_delay = 5*HZ;
392 tmp.closing_wait = 30*HZ;
393
394 if (copy_to_user(serial, &tmp, sizeof(*serial)))
395 return -EFAULT;
396 return 0;
397}
398
399static int opticon_ioctl(struct tty_struct *tty, struct file *file,
400 unsigned int cmd, unsigned long arg)
401{
402 struct usb_serial_port *port = tty->driver_data;
403 struct opticon_private *priv = usb_get_serial_data(port->serial);
404
405 dbg("%s - port %d, cmd = 0x%x", __func__, port->number, cmd);
406
407 switch (cmd) {
408 case TIOCGSERIAL:
409 return get_serial_info(priv,
410 (struct serial_struct __user *)arg);
411 }
412
413 return -ENOIOCTLCMD;
414}
415
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800416static int opticon_startup(struct usb_serial *serial)
417{
418 struct opticon_private *priv;
419 struct usb_host_interface *intf;
420 int i;
421 int retval = -ENOMEM;
422 bool bulk_in_found = false;
423
424 /* create our private serial structure */
425 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
426 if (priv == NULL) {
427 dev_err(&serial->dev->dev, "%s - Out of memory\n", __func__);
428 return -ENOMEM;
429 }
430 spin_lock_init(&priv->lock);
431 priv->serial = serial;
432 priv->port = serial->port[0];
433 priv->udev = serial->dev;
434
435 /* find our bulk endpoint */
436 intf = serial->interface->altsetting;
437 for (i = 0; i < intf->desc.bNumEndpoints; ++i) {
438 struct usb_endpoint_descriptor *endpoint;
439
440 endpoint = &intf->endpoint[i].desc;
441 if (!usb_endpoint_is_bulk_in(endpoint))
442 continue;
443
444 priv->bulk_read_urb = usb_alloc_urb(0, GFP_KERNEL);
445 if (!priv->bulk_read_urb) {
446 dev_err(&priv->udev->dev, "out of memory\n");
447 goto error;
448 }
449
450 priv->buffer_size = le16_to_cpu(endpoint->wMaxPacketSize) * 2;
451 priv->bulk_in_buffer = kmalloc(priv->buffer_size, GFP_KERNEL);
452 if (!priv->bulk_in_buffer) {
453 dev_err(&priv->udev->dev, "out of memory\n");
454 goto error;
455 }
456
457 priv->bulk_address = endpoint->bEndpointAddress;
458
459 /* set up our bulk urb */
460 usb_fill_bulk_urb(priv->bulk_read_urb, priv->udev,
461 usb_rcvbulkpipe(priv->udev,
462 endpoint->bEndpointAddress),
463 priv->bulk_in_buffer, priv->buffer_size,
464 opticon_bulk_callback, priv);
465
466 bulk_in_found = true;
467 break;
468 }
469
470 if (!bulk_in_found) {
471 dev_err(&priv->udev->dev,
472 "Error - the proper endpoints were not found!\n");
473 goto error;
474 }
475
476 usb_set_serial_data(serial, priv);
477 return 0;
478
479error:
480 usb_free_urb(priv->bulk_read_urb);
481 kfree(priv->bulk_in_buffer);
482 kfree(priv);
483 return retval;
484}
485
Alan Sternf9c99bb2009-06-02 11:53:55 -0400486static void opticon_disconnect(struct usb_serial *serial)
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800487{
488 struct opticon_private *priv = usb_get_serial_data(serial);
489
490 dbg("%s", __func__);
491
492 usb_kill_urb(priv->bulk_read_urb);
493 usb_free_urb(priv->bulk_read_urb);
Alan Sternf9c99bb2009-06-02 11:53:55 -0400494}
495
496static void opticon_release(struct usb_serial *serial)
497{
498 struct opticon_private *priv = usb_get_serial_data(serial);
499
500 dbg("%s", __func__);
501
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800502 kfree(priv->bulk_in_buffer);
503 kfree(priv);
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800504}
505
Oliver Neukumd1c07132009-01-14 18:34:06 +0100506static int opticon_suspend(struct usb_interface *intf, pm_message_t message)
507{
508 struct usb_serial *serial = usb_get_intfdata(intf);
509 struct opticon_private *priv = usb_get_serial_data(serial);
510
511 usb_kill_urb(priv->bulk_read_urb);
512 return 0;
513}
514
515static int opticon_resume(struct usb_interface *intf)
516{
517 struct usb_serial *serial = usb_get_intfdata(intf);
518 struct opticon_private *priv = usb_get_serial_data(serial);
519 struct usb_serial_port *port = serial->port[0];
520 int result;
521
Alan Cox82fc5942009-10-06 16:06:46 +0100522 mutex_lock(&port->port.mutex);
Alan Cox2a0785e2009-10-06 16:06:57 +0100523 /* This is protected by the port mutex against close/open */
524 if (test_bit(ASYNCB_INITIALIZED, &port->port.flags))
Oliver Neukumd1c07132009-01-14 18:34:06 +0100525 result = usb_submit_urb(priv->bulk_read_urb, GFP_NOIO);
526 else
527 result = 0;
Alan Cox82fc5942009-10-06 16:06:46 +0100528 mutex_unlock(&port->port.mutex);
Oliver Neukumd1c07132009-01-14 18:34:06 +0100529 return result;
530}
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800531
532static struct usb_driver opticon_driver = {
533 .name = "opticon",
534 .probe = usb_serial_probe,
535 .disconnect = usb_serial_disconnect,
Oliver Neukumd1c07132009-01-14 18:34:06 +0100536 .suspend = opticon_suspend,
537 .resume = opticon_resume,
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800538 .id_table = id_table,
539 .no_dynamic_id = 1,
540};
541
542static struct usb_serial_driver opticon_device = {
543 .driver = {
544 .owner = THIS_MODULE,
545 .name = "opticon",
546 },
547 .id_table = id_table,
548 .usb_driver = &opticon_driver,
549 .num_ports = 1,
550 .attach = opticon_startup,
551 .open = opticon_open,
552 .close = opticon_close,
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -0800553 .write = opticon_write,
554 .write_room = opticon_write_room,
Alan Sternf9c99bb2009-06-02 11:53:55 -0400555 .disconnect = opticon_disconnect,
556 .release = opticon_release,
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800557 .throttle = opticon_throttle,
558 .unthrottle = opticon_unthrottle,
Greg Kroah-Hartmanfaac64a2009-02-06 18:31:46 -0800559 .ioctl = opticon_ioctl,
560 .tiocmget = opticon_tiocmget,
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800561};
562
563static int __init opticon_init(void)
564{
565 int retval;
566
567 retval = usb_serial_register(&opticon_device);
568 if (retval)
569 return retval;
570 retval = usb_register(&opticon_driver);
571 if (retval)
572 usb_serial_deregister(&opticon_device);
573 return retval;
574}
575
576static void __exit opticon_exit(void)
577{
578 usb_deregister(&opticon_driver);
579 usb_serial_deregister(&opticon_device);
580}
581
582module_init(opticon_init);
583module_exit(opticon_exit);
584MODULE_LICENSE("GPL");
585
586module_param(debug, bool, S_IRUGO | S_IWUSR);
587MODULE_PARM_DESC(debug, "Debug enabled or not");