blob: b500ad10b7589185087bbcb64cc0e4ba97fbfd3b [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
25static struct usb_device_id id_table[] = {
26 { 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;
58 int available_room = 0;
59 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) {
99 available_room = tty_buffer_request_room(tty,
100 data_length);
101 if (available_room) {
102 tty_insert_flip_string(tty, data,
103 available_room);
104 tty_flip_buffer_push(tty);
105 }
106 tty_kref_put(tty);
107 }
108 } else {
109 if ((data[0] == 0x00) && (data[1] == 0x01)) {
110 if (data[2] == 0x00)
111 priv->rts = false;
112 else
113 priv->rts = true;
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800114 } else {
115 dev_dbg(&priv->udev->dev,
116 "Unknown data packet received from the device:"
117 " %2x %2x\n",
118 data[0], data[1]);
119 }
120 }
121 } else {
122 dev_dbg(&priv->udev->dev,
123 "Improper ammount of data received from the device, "
124 "%d bytes", urb->actual_length);
125 }
126
127exit:
128 spin_lock(&priv->lock);
129
130 /* Continue trying to always read if we should */
131 if (!priv->throttled) {
132 usb_fill_bulk_urb(priv->bulk_read_urb, priv->udev,
133 usb_rcvbulkpipe(priv->udev,
134 priv->bulk_address),
135 priv->bulk_in_buffer, priv->buffer_size,
136 opticon_bulk_callback, priv);
137 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
138 if (result)
139 dev_err(&port->dev,
140 "%s - failed resubmitting read urb, error %d\n",
141 __func__, result);
142 } else
143 priv->actually_throttled = true;
144 spin_unlock(&priv->lock);
145}
146
147static int opticon_open(struct tty_struct *tty, struct usb_serial_port *port,
148 struct file *filp)
149{
150 struct opticon_private *priv = usb_get_serial_data(port->serial);
151 unsigned long flags;
152 int result = 0;
153
154 dbg("%s - port %d", __func__, port->number);
155
156 spin_lock_irqsave(&priv->lock, flags);
157 priv->throttled = false;
158 priv->actually_throttled = false;
159 priv->port = port;
160 spin_unlock_irqrestore(&priv->lock, flags);
161
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800162 /* Start reading from the device */
163 usb_fill_bulk_urb(priv->bulk_read_urb, priv->udev,
164 usb_rcvbulkpipe(priv->udev,
165 priv->bulk_address),
166 priv->bulk_in_buffer, priv->buffer_size,
167 opticon_bulk_callback, priv);
168 result = usb_submit_urb(priv->bulk_read_urb, GFP_KERNEL);
169 if (result)
170 dev_err(&port->dev,
171 "%s - failed resubmitting read urb, error %d\n",
172 __func__, result);
173 return result;
174}
175
176static void opticon_close(struct tty_struct *tty, struct usb_serial_port *port,
177 struct file *filp)
178{
179 struct opticon_private *priv = usb_get_serial_data(port->serial);
180
181 dbg("%s - port %d", __func__, port->number);
182
183 /* shutdown our urbs */
184 usb_kill_urb(priv->bulk_read_urb);
185}
186
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -0800187static void opticon_write_bulk_callback(struct urb *urb)
188{
189 struct opticon_private *priv = urb->context;
190 int status = urb->status;
191 unsigned long flags;
192
193 /* free up the transfer buffer, as usb_free_urb() does not do this */
194 kfree(urb->transfer_buffer);
195
196 if (status)
197 dbg("%s - nonzero write bulk status received: %d",
198 __func__, status);
199
200 spin_lock_irqsave(&priv->lock, flags);
201 --priv->outstanding_urbs;
202 spin_unlock_irqrestore(&priv->lock, flags);
203
204 usb_serial_port_softint(priv->port);
205}
206
207static int opticon_write(struct tty_struct *tty, struct usb_serial_port *port,
208 const unsigned char *buf, int count)
209{
210 struct opticon_private *priv = usb_get_serial_data(port->serial);
211 struct usb_serial *serial = port->serial;
212 struct urb *urb;
213 unsigned char *buffer;
214 unsigned long flags;
215 int status;
216
217 dbg("%s - port %d", __func__, port->number);
218
219 spin_lock_irqsave(&priv->lock, flags);
220 if (priv->outstanding_urbs > URB_UPPER_LIMIT) {
221 spin_unlock_irqrestore(&priv->lock, flags);
222 dbg("%s - write limit hit\n", __func__);
223 return 0;
224 }
225 priv->outstanding_urbs++;
226 spin_unlock_irqrestore(&priv->lock, flags);
227
228 buffer = kmalloc(count, GFP_ATOMIC);
229 if (!buffer) {
230 dev_err(&port->dev, "out of memory\n");
231 count = -ENOMEM;
232 goto error_no_buffer;
233 }
234
235 urb = usb_alloc_urb(0, GFP_ATOMIC);
236 if (!urb) {
237 dev_err(&port->dev, "no more free urbs\n");
238 count = -ENOMEM;
239 goto error_no_urb;
240 }
241
242 memcpy(buffer, buf, count);
243
244 usb_serial_debug_data(debug, &port->dev, __func__, count, buffer);
245
246 usb_fill_bulk_urb(urb, serial->dev,
247 usb_sndbulkpipe(serial->dev,
248 port->bulk_out_endpointAddress),
249 buffer, count, opticon_write_bulk_callback, priv);
250
251 /* send it down the pipe */
252 status = usb_submit_urb(urb, GFP_ATOMIC);
253 if (status) {
254 dev_err(&port->dev,
255 "%s - usb_submit_urb(write bulk) failed with status = %d\n",
256 __func__, status);
257 count = status;
258 goto error;
259 }
260
261 /* we are done with this urb, so let the host driver
262 * really free it when it is finished with it */
263 usb_free_urb(urb);
264
265 return count;
266error:
267 usb_free_urb(urb);
268error_no_urb:
269 kfree(buffer);
270error_no_buffer:
271 spin_lock_irqsave(&priv->lock, flags);
272 --priv->outstanding_urbs;
273 spin_unlock_irqrestore(&priv->lock, flags);
274 return count;
275}
276
277static int opticon_write_room(struct tty_struct *tty)
278{
279 struct usb_serial_port *port = tty->driver_data;
280 struct opticon_private *priv = usb_get_serial_data(port->serial);
281 unsigned long flags;
282
283 dbg("%s - port %d", __func__, port->number);
284
285 /*
286 * We really can take almost anything the user throws at us
287 * but let's pick a nice big number to tell the tty
288 * layer that we have lots of free space, unless we don't.
289 */
290 spin_lock_irqsave(&priv->lock, flags);
291 if (priv->outstanding_urbs > URB_UPPER_LIMIT * 2 / 3) {
292 spin_unlock_irqrestore(&priv->lock, flags);
293 dbg("%s - write limit hit\n", __func__);
294 return 0;
295 }
296 spin_unlock_irqrestore(&priv->lock, flags);
297
298 return 2048;
299}
300
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800301static void opticon_throttle(struct tty_struct *tty)
302{
303 struct usb_serial_port *port = tty->driver_data;
304 struct opticon_private *priv = usb_get_serial_data(port->serial);
305 unsigned long flags;
306
307 dbg("%s - port %d", __func__, port->number);
308 spin_lock_irqsave(&priv->lock, flags);
309 priv->throttled = true;
310 spin_unlock_irqrestore(&priv->lock, flags);
311}
312
313
314static void opticon_unthrottle(struct tty_struct *tty)
315{
316 struct usb_serial_port *port = tty->driver_data;
317 struct opticon_private *priv = usb_get_serial_data(port->serial);
318 unsigned long flags;
319 int result;
320
321 dbg("%s - port %d", __func__, port->number);
322
323 spin_lock_irqsave(&priv->lock, flags);
324 priv->throttled = false;
325 priv->actually_throttled = false;
326 spin_unlock_irqrestore(&priv->lock, flags);
327
328 priv->bulk_read_urb->dev = port->serial->dev;
329 result = usb_submit_urb(priv->bulk_read_urb, GFP_ATOMIC);
330 if (result)
331 dev_err(&port->dev,
332 "%s - failed submitting read urb, error %d\n",
333 __func__, result);
334}
335
Greg Kroah-Hartmanfaac64a2009-02-06 18:31:46 -0800336static int opticon_tiocmget(struct tty_struct *tty, struct file *file)
337{
338 struct usb_serial_port *port = tty->driver_data;
339 struct opticon_private *priv = usb_get_serial_data(port->serial);
340 unsigned long flags;
341 int result = 0;
342
343 dbg("%s - port %d", __func__, port->number);
344
345 spin_lock_irqsave(&priv->lock, flags);
346 if (priv->rts)
347 result = TIOCM_RTS;
348 spin_unlock_irqrestore(&priv->lock, flags);
349
350 dbg("%s - %x", __func__, result);
351 return result;
352}
353
354static int get_serial_info(struct opticon_private *priv,
355 struct serial_struct __user *serial)
356{
357 struct serial_struct tmp;
358
359 if (!serial)
360 return -EFAULT;
361
362 memset(&tmp, 0x00, sizeof(tmp));
363
364 /* fake emulate a 16550 uart to make userspace code happy */
365 tmp.type = PORT_16550A;
366 tmp.line = priv->serial->minor;
367 tmp.port = 0;
368 tmp.irq = 0;
369 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
370 tmp.xmit_fifo_size = 1024;
371 tmp.baud_base = 9600;
372 tmp.close_delay = 5*HZ;
373 tmp.closing_wait = 30*HZ;
374
375 if (copy_to_user(serial, &tmp, sizeof(*serial)))
376 return -EFAULT;
377 return 0;
378}
379
380static int opticon_ioctl(struct tty_struct *tty, struct file *file,
381 unsigned int cmd, unsigned long arg)
382{
383 struct usb_serial_port *port = tty->driver_data;
384 struct opticon_private *priv = usb_get_serial_data(port->serial);
385
386 dbg("%s - port %d, cmd = 0x%x", __func__, port->number, cmd);
387
388 switch (cmd) {
389 case TIOCGSERIAL:
390 return get_serial_info(priv,
391 (struct serial_struct __user *)arg);
392 }
393
394 return -ENOIOCTLCMD;
395}
396
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800397static int opticon_startup(struct usb_serial *serial)
398{
399 struct opticon_private *priv;
400 struct usb_host_interface *intf;
401 int i;
402 int retval = -ENOMEM;
403 bool bulk_in_found = false;
404
405 /* create our private serial structure */
406 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
407 if (priv == NULL) {
408 dev_err(&serial->dev->dev, "%s - Out of memory\n", __func__);
409 return -ENOMEM;
410 }
411 spin_lock_init(&priv->lock);
412 priv->serial = serial;
413 priv->port = serial->port[0];
414 priv->udev = serial->dev;
415
416 /* find our bulk endpoint */
417 intf = serial->interface->altsetting;
418 for (i = 0; i < intf->desc.bNumEndpoints; ++i) {
419 struct usb_endpoint_descriptor *endpoint;
420
421 endpoint = &intf->endpoint[i].desc;
422 if (!usb_endpoint_is_bulk_in(endpoint))
423 continue;
424
425 priv->bulk_read_urb = usb_alloc_urb(0, GFP_KERNEL);
426 if (!priv->bulk_read_urb) {
427 dev_err(&priv->udev->dev, "out of memory\n");
428 goto error;
429 }
430
431 priv->buffer_size = le16_to_cpu(endpoint->wMaxPacketSize) * 2;
432 priv->bulk_in_buffer = kmalloc(priv->buffer_size, GFP_KERNEL);
433 if (!priv->bulk_in_buffer) {
434 dev_err(&priv->udev->dev, "out of memory\n");
435 goto error;
436 }
437
438 priv->bulk_address = endpoint->bEndpointAddress;
439
440 /* set up our bulk urb */
441 usb_fill_bulk_urb(priv->bulk_read_urb, priv->udev,
442 usb_rcvbulkpipe(priv->udev,
443 endpoint->bEndpointAddress),
444 priv->bulk_in_buffer, priv->buffer_size,
445 opticon_bulk_callback, priv);
446
447 bulk_in_found = true;
448 break;
449 }
450
451 if (!bulk_in_found) {
452 dev_err(&priv->udev->dev,
453 "Error - the proper endpoints were not found!\n");
454 goto error;
455 }
456
457 usb_set_serial_data(serial, priv);
458 return 0;
459
460error:
461 usb_free_urb(priv->bulk_read_urb);
462 kfree(priv->bulk_in_buffer);
463 kfree(priv);
464 return retval;
465}
466
467static void opticon_shutdown(struct usb_serial *serial)
468{
469 struct opticon_private *priv = usb_get_serial_data(serial);
470
471 dbg("%s", __func__);
472
473 usb_kill_urb(priv->bulk_read_urb);
474 usb_free_urb(priv->bulk_read_urb);
475 kfree(priv->bulk_in_buffer);
476 kfree(priv);
477 usb_set_serial_data(serial, NULL);
478}
479
Oliver Neukumd1c07132009-01-14 18:34:06 +0100480static int opticon_suspend(struct usb_interface *intf, pm_message_t message)
481{
482 struct usb_serial *serial = usb_get_intfdata(intf);
483 struct opticon_private *priv = usb_get_serial_data(serial);
484
485 usb_kill_urb(priv->bulk_read_urb);
486 return 0;
487}
488
489static int opticon_resume(struct usb_interface *intf)
490{
491 struct usb_serial *serial = usb_get_intfdata(intf);
492 struct opticon_private *priv = usb_get_serial_data(serial);
493 struct usb_serial_port *port = serial->port[0];
494 int result;
495
496 mutex_lock(&port->mutex);
497 if (port->port.count)
498 result = usb_submit_urb(priv->bulk_read_urb, GFP_NOIO);
499 else
500 result = 0;
501 mutex_unlock(&port->mutex);
502 return result;
503}
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800504
505static struct usb_driver opticon_driver = {
506 .name = "opticon",
507 .probe = usb_serial_probe,
508 .disconnect = usb_serial_disconnect,
Oliver Neukumd1c07132009-01-14 18:34:06 +0100509 .suspend = opticon_suspend,
510 .resume = opticon_resume,
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800511 .id_table = id_table,
512 .no_dynamic_id = 1,
513};
514
515static struct usb_serial_driver opticon_device = {
516 .driver = {
517 .owner = THIS_MODULE,
518 .name = "opticon",
519 },
520 .id_table = id_table,
521 .usb_driver = &opticon_driver,
522 .num_ports = 1,
523 .attach = opticon_startup,
524 .open = opticon_open,
525 .close = opticon_close,
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -0800526 .write = opticon_write,
527 .write_room = opticon_write_room,
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800528 .shutdown = opticon_shutdown,
529 .throttle = opticon_throttle,
530 .unthrottle = opticon_unthrottle,
Greg Kroah-Hartmanfaac64a2009-02-06 18:31:46 -0800531 .ioctl = opticon_ioctl,
532 .tiocmget = opticon_tiocmget,
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800533};
534
535static int __init opticon_init(void)
536{
537 int retval;
538
539 retval = usb_serial_register(&opticon_device);
540 if (retval)
541 return retval;
542 retval = usb_register(&opticon_driver);
543 if (retval)
544 usb_serial_deregister(&opticon_device);
545 return retval;
546}
547
548static void __exit opticon_exit(void)
549{
550 usb_deregister(&opticon_driver);
551 usb_serial_deregister(&opticon_device);
552}
553
554module_init(opticon_init);
555module_exit(opticon_exit);
556MODULE_LICENSE("GPL");
557
558module_param(debug, bool, S_IRUGO | S_IWUSR);
559MODULE_PARM_DESC(debug, "Debug enabled or not");