blob: 701452ae91979a23129d24d656a5e4a2df7b8946 [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>
16#include <linux/tty_flip.h>
Greg Kroah-Hartmanfaac64a2009-02-06 18:31:46 -080017#include <linux/serial.h>
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -080018#include <linux/module.h>
19#include <linux/usb.h>
20#include <linux/usb/serial.h>
21#include <linux/uaccess.h>
22
23static int debug;
24
Németh Márton7d40d7e2010-01-10 15:34:24 +010025static const struct usb_device_id id_table[] = {
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -080026 { USB_DEVICE(0x065a, 0x0009) },
27 { },
28};
29MODULE_DEVICE_TABLE(usb, id_table);
30
31/* This structure holds all of the individual device information */
32struct opticon_private {
33 struct usb_device *udev;
34 struct usb_serial *serial;
35 struct usb_serial_port *port;
36 unsigned char *bulk_in_buffer;
37 struct urb *bulk_read_urb;
38 int buffer_size;
39 u8 bulk_address;
40 spinlock_t lock; /* protects the following flags */
41 bool throttled;
42 bool actually_throttled;
43 bool rts;
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -080044 int outstanding_urbs;
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -080045};
46
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -080047/* max number of write urbs in flight */
48#define URB_UPPER_LIMIT 4
49
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -080050static void opticon_bulk_callback(struct urb *urb)
51{
52 struct opticon_private *priv = urb->context;
53 unsigned char *data = urb->transfer_buffer;
54 struct usb_serial_port *port = priv->port;
55 int status = urb->status;
56 struct tty_struct *tty;
57 int result;
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -080058 int data_length;
59
60 dbg("%s - port %d", __func__, port->number);
61
62 switch (status) {
63 case 0:
64 /* success */
65 break;
66 case -ECONNRESET:
67 case -ENOENT:
68 case -ESHUTDOWN:
69 /* this urb is terminated, clean up */
70 dbg("%s - urb shutting down with status: %d",
71 __func__, status);
72 return;
73 default:
74 dbg("%s - nonzero urb status received: %d",
75 __func__, status);
76 goto exit;
77 }
78
79 usb_serial_debug_data(debug, &port->dev, __func__, urb->actual_length,
80 data);
81
82 if (urb->actual_length > 2) {
83 data_length = urb->actual_length - 2;
84
85 /*
86 * Data from the device comes with a 2 byte header:
87 *
88 * <0x00><0x00>data...
89 * This is real data to be sent to the tty layer
90 * <0x00><0x01)level
91 * This is a RTS level change, the third byte is the RTS
92 * value (0 for low, 1 for high).
93 */
94 if ((data[0] == 0x00) && (data[1] == 0x00)) {
95 /* real data, send it to the tty layer */
96 tty = tty_port_tty_get(&port->port);
97 if (tty) {
Alan Coxa108bfc2010-02-18 16:44:01 +000098 tty_insert_flip_string(tty, data,
99 data_length);
100 tty_flip_buffer_push(tty);
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800101 tty_kref_put(tty);
102 }
103 } else {
104 if ((data[0] == 0x00) && (data[1] == 0x01)) {
105 if (data[2] == 0x00)
106 priv->rts = false;
107 else
108 priv->rts = true;
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800109 } else {
110 dev_dbg(&priv->udev->dev,
111 "Unknown data packet received from the device:"
112 " %2x %2x\n",
113 data[0], data[1]);
114 }
115 }
116 } else {
117 dev_dbg(&priv->udev->dev,
Uwe Kleine-König9ddc5b62010-01-20 17:02:24 +0100118 "Improper amount of data received from the device, "
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800119 "%d bytes", urb->actual_length);
120 }
121
122exit:
123 spin_lock(&priv->lock);
124
125 /* Continue trying to always read if we should */
126 if (!priv->throttled) {
127 usb_fill_bulk_urb(priv->bulk_read_urb, priv->udev,
128 usb_rcvbulkpipe(priv->udev,
129 priv->bulk_address),
130 priv->bulk_in_buffer, priv->buffer_size,
131 opticon_bulk_callback, priv);
132 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
133 if (result)
134 dev_err(&port->dev,
135 "%s - failed resubmitting read urb, error %d\n",
136 __func__, result);
137 } else
138 priv->actually_throttled = true;
139 spin_unlock(&priv->lock);
140}
141
Alan Coxa509a7e2009-09-19 13:13:26 -0700142static int opticon_open(struct tty_struct *tty, struct usb_serial_port *port)
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800143{
144 struct opticon_private *priv = usb_get_serial_data(port->serial);
145 unsigned long flags;
146 int result = 0;
147
148 dbg("%s - port %d", __func__, port->number);
149
150 spin_lock_irqsave(&priv->lock, flags);
151 priv->throttled = false;
152 priv->actually_throttled = false;
153 priv->port = port;
154 spin_unlock_irqrestore(&priv->lock, flags);
155
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800156 /* Start reading from the device */
157 usb_fill_bulk_urb(priv->bulk_read_urb, priv->udev,
158 usb_rcvbulkpipe(priv->udev,
159 priv->bulk_address),
160 priv->bulk_in_buffer, priv->buffer_size,
161 opticon_bulk_callback, priv);
162 result = usb_submit_urb(priv->bulk_read_urb, GFP_KERNEL);
163 if (result)
164 dev_err(&port->dev,
165 "%s - failed resubmitting read urb, error %d\n",
166 __func__, result);
167 return result;
168}
169
Alan Cox335f8512009-06-11 12:26:29 +0100170static void opticon_close(struct usb_serial_port *port)
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800171{
172 struct opticon_private *priv = usb_get_serial_data(port->serial);
173
174 dbg("%s - port %d", __func__, port->number);
175
176 /* shutdown our urbs */
177 usb_kill_urb(priv->bulk_read_urb);
178}
179
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -0800180static void opticon_write_bulk_callback(struct urb *urb)
181{
182 struct opticon_private *priv = urb->context;
183 int status = urb->status;
184 unsigned long flags;
185
186 /* free up the transfer buffer, as usb_free_urb() does not do this */
187 kfree(urb->transfer_buffer);
188
189 if (status)
190 dbg("%s - nonzero write bulk status received: %d",
191 __func__, status);
192
193 spin_lock_irqsave(&priv->lock, flags);
194 --priv->outstanding_urbs;
195 spin_unlock_irqrestore(&priv->lock, flags);
196
197 usb_serial_port_softint(priv->port);
198}
199
200static int opticon_write(struct tty_struct *tty, struct usb_serial_port *port,
201 const unsigned char *buf, int count)
202{
203 struct opticon_private *priv = usb_get_serial_data(port->serial);
204 struct usb_serial *serial = port->serial;
205 struct urb *urb;
206 unsigned char *buffer;
207 unsigned long flags;
208 int status;
209
210 dbg("%s - port %d", __func__, port->number);
211
212 spin_lock_irqsave(&priv->lock, flags);
213 if (priv->outstanding_urbs > URB_UPPER_LIMIT) {
214 spin_unlock_irqrestore(&priv->lock, flags);
Joe Perches759f3632010-02-05 16:50:08 -0800215 dbg("%s - write limit hit", __func__);
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -0800216 return 0;
217 }
218 priv->outstanding_urbs++;
219 spin_unlock_irqrestore(&priv->lock, flags);
220
221 buffer = kmalloc(count, GFP_ATOMIC);
222 if (!buffer) {
223 dev_err(&port->dev, "out of memory\n");
224 count = -ENOMEM;
225 goto error_no_buffer;
226 }
227
228 urb = usb_alloc_urb(0, GFP_ATOMIC);
229 if (!urb) {
230 dev_err(&port->dev, "no more free urbs\n");
231 count = -ENOMEM;
232 goto error_no_urb;
233 }
234
235 memcpy(buffer, buf, count);
236
237 usb_serial_debug_data(debug, &port->dev, __func__, count, buffer);
238
239 usb_fill_bulk_urb(urb, serial->dev,
240 usb_sndbulkpipe(serial->dev,
241 port->bulk_out_endpointAddress),
242 buffer, count, opticon_write_bulk_callback, priv);
243
244 /* send it down the pipe */
245 status = usb_submit_urb(urb, GFP_ATOMIC);
246 if (status) {
247 dev_err(&port->dev,
248 "%s - usb_submit_urb(write bulk) failed with status = %d\n",
249 __func__, status);
250 count = status;
251 goto error;
252 }
253
254 /* we are done with this urb, so let the host driver
255 * really free it when it is finished with it */
256 usb_free_urb(urb);
257
258 return count;
259error:
260 usb_free_urb(urb);
261error_no_urb:
262 kfree(buffer);
263error_no_buffer:
264 spin_lock_irqsave(&priv->lock, flags);
265 --priv->outstanding_urbs;
266 spin_unlock_irqrestore(&priv->lock, flags);
267 return count;
268}
269
270static int opticon_write_room(struct tty_struct *tty)
271{
272 struct usb_serial_port *port = tty->driver_data;
273 struct opticon_private *priv = usb_get_serial_data(port->serial);
274 unsigned long flags;
275
276 dbg("%s - port %d", __func__, port->number);
277
278 /*
279 * We really can take almost anything the user throws at us
280 * but let's pick a nice big number to tell the tty
281 * layer that we have lots of free space, unless we don't.
282 */
283 spin_lock_irqsave(&priv->lock, flags);
284 if (priv->outstanding_urbs > URB_UPPER_LIMIT * 2 / 3) {
285 spin_unlock_irqrestore(&priv->lock, flags);
Joe Perches759f3632010-02-05 16:50:08 -0800286 dbg("%s - write limit hit", __func__);
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -0800287 return 0;
288 }
289 spin_unlock_irqrestore(&priv->lock, flags);
290
291 return 2048;
292}
293
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800294static void opticon_throttle(struct tty_struct *tty)
295{
296 struct usb_serial_port *port = tty->driver_data;
297 struct opticon_private *priv = usb_get_serial_data(port->serial);
298 unsigned long flags;
299
300 dbg("%s - port %d", __func__, port->number);
301 spin_lock_irqsave(&priv->lock, flags);
302 priv->throttled = true;
303 spin_unlock_irqrestore(&priv->lock, flags);
304}
305
306
307static void opticon_unthrottle(struct tty_struct *tty)
308{
309 struct usb_serial_port *port = tty->driver_data;
310 struct opticon_private *priv = usb_get_serial_data(port->serial);
311 unsigned long flags;
Oliver Neukum88fa6592009-10-07 09:25:10 +0200312 int result, was_throttled;
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800313
314 dbg("%s - port %d", __func__, port->number);
315
316 spin_lock_irqsave(&priv->lock, flags);
317 priv->throttled = false;
Oliver Neukum88fa6592009-10-07 09:25:10 +0200318 was_throttled = priv->actually_throttled;
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800319 priv->actually_throttled = false;
320 spin_unlock_irqrestore(&priv->lock, flags);
321
322 priv->bulk_read_urb->dev = port->serial->dev;
Oliver Neukum88fa6592009-10-07 09:25:10 +0200323 if (was_throttled) {
324 result = usb_submit_urb(priv->bulk_read_urb, GFP_ATOMIC);
325 if (result)
326 dev_err(&port->dev,
327 "%s - failed submitting read urb, error %d\n",
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800328 __func__, result);
Oliver Neukum88fa6592009-10-07 09:25:10 +0200329 }
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800330}
331
Greg Kroah-Hartmanfaac64a2009-02-06 18:31:46 -0800332static int opticon_tiocmget(struct tty_struct *tty, struct file *file)
333{
334 struct usb_serial_port *port = tty->driver_data;
335 struct opticon_private *priv = usb_get_serial_data(port->serial);
336 unsigned long flags;
337 int result = 0;
338
339 dbg("%s - port %d", __func__, port->number);
340
341 spin_lock_irqsave(&priv->lock, flags);
342 if (priv->rts)
343 result = TIOCM_RTS;
344 spin_unlock_irqrestore(&priv->lock, flags);
345
346 dbg("%s - %x", __func__, result);
347 return result;
348}
349
350static int get_serial_info(struct opticon_private *priv,
351 struct serial_struct __user *serial)
352{
353 struct serial_struct tmp;
354
355 if (!serial)
356 return -EFAULT;
357
358 memset(&tmp, 0x00, sizeof(tmp));
359
360 /* fake emulate a 16550 uart to make userspace code happy */
361 tmp.type = PORT_16550A;
362 tmp.line = priv->serial->minor;
363 tmp.port = 0;
364 tmp.irq = 0;
365 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
366 tmp.xmit_fifo_size = 1024;
367 tmp.baud_base = 9600;
368 tmp.close_delay = 5*HZ;
369 tmp.closing_wait = 30*HZ;
370
371 if (copy_to_user(serial, &tmp, sizeof(*serial)))
372 return -EFAULT;
373 return 0;
374}
375
376static int opticon_ioctl(struct tty_struct *tty, struct file *file,
377 unsigned int cmd, unsigned long arg)
378{
379 struct usb_serial_port *port = tty->driver_data;
380 struct opticon_private *priv = usb_get_serial_data(port->serial);
381
382 dbg("%s - port %d, cmd = 0x%x", __func__, port->number, cmd);
383
384 switch (cmd) {
385 case TIOCGSERIAL:
386 return get_serial_info(priv,
387 (struct serial_struct __user *)arg);
388 }
389
390 return -ENOIOCTLCMD;
391}
392
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800393static int opticon_startup(struct usb_serial *serial)
394{
395 struct opticon_private *priv;
396 struct usb_host_interface *intf;
397 int i;
398 int retval = -ENOMEM;
399 bool bulk_in_found = false;
400
401 /* create our private serial structure */
402 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
403 if (priv == NULL) {
404 dev_err(&serial->dev->dev, "%s - Out of memory\n", __func__);
405 return -ENOMEM;
406 }
407 spin_lock_init(&priv->lock);
408 priv->serial = serial;
409 priv->port = serial->port[0];
410 priv->udev = serial->dev;
411
412 /* find our bulk endpoint */
413 intf = serial->interface->altsetting;
414 for (i = 0; i < intf->desc.bNumEndpoints; ++i) {
415 struct usb_endpoint_descriptor *endpoint;
416
417 endpoint = &intf->endpoint[i].desc;
418 if (!usb_endpoint_is_bulk_in(endpoint))
419 continue;
420
421 priv->bulk_read_urb = usb_alloc_urb(0, GFP_KERNEL);
422 if (!priv->bulk_read_urb) {
423 dev_err(&priv->udev->dev, "out of memory\n");
424 goto error;
425 }
426
427 priv->buffer_size = le16_to_cpu(endpoint->wMaxPacketSize) * 2;
428 priv->bulk_in_buffer = kmalloc(priv->buffer_size, GFP_KERNEL);
429 if (!priv->bulk_in_buffer) {
430 dev_err(&priv->udev->dev, "out of memory\n");
431 goto error;
432 }
433
434 priv->bulk_address = endpoint->bEndpointAddress;
435
436 /* set up our bulk urb */
437 usb_fill_bulk_urb(priv->bulk_read_urb, priv->udev,
438 usb_rcvbulkpipe(priv->udev,
439 endpoint->bEndpointAddress),
440 priv->bulk_in_buffer, priv->buffer_size,
441 opticon_bulk_callback, priv);
442
443 bulk_in_found = true;
444 break;
445 }
446
447 if (!bulk_in_found) {
448 dev_err(&priv->udev->dev,
449 "Error - the proper endpoints were not found!\n");
450 goto error;
451 }
452
453 usb_set_serial_data(serial, priv);
454 return 0;
455
456error:
457 usb_free_urb(priv->bulk_read_urb);
458 kfree(priv->bulk_in_buffer);
459 kfree(priv);
460 return retval;
461}
462
Alan Sternf9c99bb2009-06-02 11:53:55 -0400463static void opticon_disconnect(struct usb_serial *serial)
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800464{
465 struct opticon_private *priv = usb_get_serial_data(serial);
466
467 dbg("%s", __func__);
468
469 usb_kill_urb(priv->bulk_read_urb);
470 usb_free_urb(priv->bulk_read_urb);
Alan Sternf9c99bb2009-06-02 11:53:55 -0400471}
472
473static void opticon_release(struct usb_serial *serial)
474{
475 struct opticon_private *priv = usb_get_serial_data(serial);
476
477 dbg("%s", __func__);
478
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800479 kfree(priv->bulk_in_buffer);
480 kfree(priv);
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800481}
482
Oliver Neukumd1c07132009-01-14 18:34:06 +0100483static int opticon_suspend(struct usb_interface *intf, pm_message_t message)
484{
485 struct usb_serial *serial = usb_get_intfdata(intf);
486 struct opticon_private *priv = usb_get_serial_data(serial);
487
488 usb_kill_urb(priv->bulk_read_urb);
489 return 0;
490}
491
492static int opticon_resume(struct usb_interface *intf)
493{
494 struct usb_serial *serial = usb_get_intfdata(intf);
495 struct opticon_private *priv = usb_get_serial_data(serial);
496 struct usb_serial_port *port = serial->port[0];
497 int result;
498
Alan Cox82fc5942009-10-06 16:06:46 +0100499 mutex_lock(&port->port.mutex);
Alan Cox2a0785e2009-10-06 16:06:57 +0100500 /* This is protected by the port mutex against close/open */
501 if (test_bit(ASYNCB_INITIALIZED, &port->port.flags))
Oliver Neukumd1c07132009-01-14 18:34:06 +0100502 result = usb_submit_urb(priv->bulk_read_urb, GFP_NOIO);
503 else
504 result = 0;
Alan Cox82fc5942009-10-06 16:06:46 +0100505 mutex_unlock(&port->port.mutex);
Oliver Neukumd1c07132009-01-14 18:34:06 +0100506 return result;
507}
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800508
509static struct usb_driver opticon_driver = {
510 .name = "opticon",
511 .probe = usb_serial_probe,
512 .disconnect = usb_serial_disconnect,
Oliver Neukumd1c07132009-01-14 18:34:06 +0100513 .suspend = opticon_suspend,
514 .resume = opticon_resume,
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800515 .id_table = id_table,
516 .no_dynamic_id = 1,
517};
518
519static struct usb_serial_driver opticon_device = {
520 .driver = {
521 .owner = THIS_MODULE,
522 .name = "opticon",
523 },
524 .id_table = id_table,
525 .usb_driver = &opticon_driver,
526 .num_ports = 1,
527 .attach = opticon_startup,
528 .open = opticon_open,
529 .close = opticon_close,
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -0800530 .write = opticon_write,
531 .write_room = opticon_write_room,
Alan Sternf9c99bb2009-06-02 11:53:55 -0400532 .disconnect = opticon_disconnect,
533 .release = opticon_release,
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800534 .throttle = opticon_throttle,
535 .unthrottle = opticon_unthrottle,
Greg Kroah-Hartmanfaac64a2009-02-06 18:31:46 -0800536 .ioctl = opticon_ioctl,
537 .tiocmget = opticon_tiocmget,
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800538};
539
540static int __init opticon_init(void)
541{
542 int retval;
543
544 retval = usb_serial_register(&opticon_device);
545 if (retval)
546 return retval;
547 retval = usb_register(&opticon_driver);
548 if (retval)
549 usb_serial_deregister(&opticon_device);
550 return retval;
551}
552
553static void __exit opticon_exit(void)
554{
555 usb_deregister(&opticon_driver);
556 usb_serial_deregister(&opticon_device);
557}
558
559module_init(opticon_init);
560module_exit(opticon_exit);
561MODULE_LICENSE("GPL");
562
563module_param(debug, bool, S_IRUGO | S_IWUSR);
564MODULE_PARM_DESC(debug, "Debug enabled or not");