Commit | Line | Data |
---|---|---|
57262b82 GKH |
1 | /* |
2 | * Opticon USB barcode to serial driver | |
3 | * | |
648d4e16 GKH |
4 | * Copyright (C) 2008 - 2009 Greg Kroah-Hartman <gregkh@suse.de> |
5 | * Copyright (C) 2008 - 2009 Novell Inc. | |
57262b82 GKH |
6 | * |
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> | |
faac64ad | 17 | #include <linux/serial.h> |
57262b82 GKH |
18 | #include <linux/module.h> |
19 | #include <linux/usb.h> | |
20 | #include <linux/usb/serial.h> | |
21 | #include <linux/uaccess.h> | |
22 | ||
23 | static int debug; | |
24 | ||
25 | static struct usb_device_id id_table[] = { | |
26 | { USB_DEVICE(0x065a, 0x0009) }, | |
27 | { }, | |
28 | }; | |
29 | MODULE_DEVICE_TABLE(usb, id_table); | |
30 | ||
31 | /* This structure holds all of the individual device information */ | |
32 | struct 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; | |
648d4e16 | 44 | int outstanding_urbs; |
57262b82 GKH |
45 | }; |
46 | ||
648d4e16 GKH |
47 | /* max number of write urbs in flight */ |
48 | #define URB_UPPER_LIMIT 4 | |
49 | ||
57262b82 GKH |
50 | static 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; | |
57262b82 GKH |
114 | } 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 | ||
127 | exit: | |
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 | ||
147 | static 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 | ||
57262b82 GKH |
162 | /* 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 | ||
335f8514 | 176 | static void opticon_close(struct usb_serial_port *port) |
57262b82 GKH |
177 | { |
178 | struct opticon_private *priv = usb_get_serial_data(port->serial); | |
179 | ||
180 | dbg("%s - port %d", __func__, port->number); | |
181 | ||
182 | /* shutdown our urbs */ | |
183 | usb_kill_urb(priv->bulk_read_urb); | |
184 | } | |
185 | ||
648d4e16 GKH |
186 | static void opticon_write_bulk_callback(struct urb *urb) |
187 | { | |
188 | struct opticon_private *priv = urb->context; | |
189 | int status = urb->status; | |
190 | unsigned long flags; | |
191 | ||
192 | /* free up the transfer buffer, as usb_free_urb() does not do this */ | |
193 | kfree(urb->transfer_buffer); | |
194 | ||
195 | if (status) | |
196 | dbg("%s - nonzero write bulk status received: %d", | |
197 | __func__, status); | |
198 | ||
199 | spin_lock_irqsave(&priv->lock, flags); | |
200 | --priv->outstanding_urbs; | |
201 | spin_unlock_irqrestore(&priv->lock, flags); | |
202 | ||
203 | usb_serial_port_softint(priv->port); | |
204 | } | |
205 | ||
206 | static int opticon_write(struct tty_struct *tty, struct usb_serial_port *port, | |
207 | const unsigned char *buf, int count) | |
208 | { | |
209 | struct opticon_private *priv = usb_get_serial_data(port->serial); | |
210 | struct usb_serial *serial = port->serial; | |
211 | struct urb *urb; | |
212 | unsigned char *buffer; | |
213 | unsigned long flags; | |
214 | int status; | |
215 | ||
216 | dbg("%s - port %d", __func__, port->number); | |
217 | ||
218 | spin_lock_irqsave(&priv->lock, flags); | |
219 | if (priv->outstanding_urbs > URB_UPPER_LIMIT) { | |
220 | spin_unlock_irqrestore(&priv->lock, flags); | |
221 | dbg("%s - write limit hit\n", __func__); | |
222 | return 0; | |
223 | } | |
224 | priv->outstanding_urbs++; | |
225 | spin_unlock_irqrestore(&priv->lock, flags); | |
226 | ||
227 | buffer = kmalloc(count, GFP_ATOMIC); | |
228 | if (!buffer) { | |
229 | dev_err(&port->dev, "out of memory\n"); | |
230 | count = -ENOMEM; | |
231 | goto error_no_buffer; | |
232 | } | |
233 | ||
234 | urb = usb_alloc_urb(0, GFP_ATOMIC); | |
235 | if (!urb) { | |
236 | dev_err(&port->dev, "no more free urbs\n"); | |
237 | count = -ENOMEM; | |
238 | goto error_no_urb; | |
239 | } | |
240 | ||
241 | memcpy(buffer, buf, count); | |
242 | ||
243 | usb_serial_debug_data(debug, &port->dev, __func__, count, buffer); | |
244 | ||
245 | usb_fill_bulk_urb(urb, serial->dev, | |
246 | usb_sndbulkpipe(serial->dev, | |
247 | port->bulk_out_endpointAddress), | |
248 | buffer, count, opticon_write_bulk_callback, priv); | |
249 | ||
250 | /* send it down the pipe */ | |
251 | status = usb_submit_urb(urb, GFP_ATOMIC); | |
252 | if (status) { | |
253 | dev_err(&port->dev, | |
254 | "%s - usb_submit_urb(write bulk) failed with status = %d\n", | |
255 | __func__, status); | |
256 | count = status; | |
257 | goto error; | |
258 | } | |
259 | ||
260 | /* we are done with this urb, so let the host driver | |
261 | * really free it when it is finished with it */ | |
262 | usb_free_urb(urb); | |
263 | ||
264 | return count; | |
265 | error: | |
266 | usb_free_urb(urb); | |
267 | error_no_urb: | |
268 | kfree(buffer); | |
269 | error_no_buffer: | |
270 | spin_lock_irqsave(&priv->lock, flags); | |
271 | --priv->outstanding_urbs; | |
272 | spin_unlock_irqrestore(&priv->lock, flags); | |
273 | return count; | |
274 | } | |
275 | ||
276 | static int opticon_write_room(struct tty_struct *tty) | |
277 | { | |
278 | struct usb_serial_port *port = tty->driver_data; | |
279 | struct opticon_private *priv = usb_get_serial_data(port->serial); | |
280 | unsigned long flags; | |
281 | ||
282 | dbg("%s - port %d", __func__, port->number); | |
283 | ||
284 | /* | |
285 | * We really can take almost anything the user throws at us | |
286 | * but let's pick a nice big number to tell the tty | |
287 | * layer that we have lots of free space, unless we don't. | |
288 | */ | |
289 | spin_lock_irqsave(&priv->lock, flags); | |
290 | if (priv->outstanding_urbs > URB_UPPER_LIMIT * 2 / 3) { | |
291 | spin_unlock_irqrestore(&priv->lock, flags); | |
292 | dbg("%s - write limit hit\n", __func__); | |
293 | return 0; | |
294 | } | |
295 | spin_unlock_irqrestore(&priv->lock, flags); | |
296 | ||
297 | return 2048; | |
298 | } | |
299 | ||
57262b82 GKH |
300 | static void opticon_throttle(struct tty_struct *tty) |
301 | { | |
302 | struct usb_serial_port *port = tty->driver_data; | |
303 | struct opticon_private *priv = usb_get_serial_data(port->serial); | |
304 | unsigned long flags; | |
305 | ||
306 | dbg("%s - port %d", __func__, port->number); | |
307 | spin_lock_irqsave(&priv->lock, flags); | |
308 | priv->throttled = true; | |
309 | spin_unlock_irqrestore(&priv->lock, flags); | |
310 | } | |
311 | ||
312 | ||
313 | static void opticon_unthrottle(struct tty_struct *tty) | |
314 | { | |
315 | struct usb_serial_port *port = tty->driver_data; | |
316 | struct opticon_private *priv = usb_get_serial_data(port->serial); | |
317 | unsigned long flags; | |
318 | int result; | |
319 | ||
320 | dbg("%s - port %d", __func__, port->number); | |
321 | ||
322 | spin_lock_irqsave(&priv->lock, flags); | |
323 | priv->throttled = false; | |
324 | priv->actually_throttled = false; | |
325 | spin_unlock_irqrestore(&priv->lock, flags); | |
326 | ||
327 | priv->bulk_read_urb->dev = port->serial->dev; | |
328 | result = usb_submit_urb(priv->bulk_read_urb, GFP_ATOMIC); | |
329 | if (result) | |
330 | dev_err(&port->dev, | |
331 | "%s - failed submitting read urb, error %d\n", | |
332 | __func__, result); | |
333 | } | |
334 | ||
faac64ad GKH |
335 | static int opticon_tiocmget(struct tty_struct *tty, struct file *file) |
336 | { | |
337 | struct usb_serial_port *port = tty->driver_data; | |
338 | struct opticon_private *priv = usb_get_serial_data(port->serial); | |
339 | unsigned long flags; | |
340 | int result = 0; | |
341 | ||
342 | dbg("%s - port %d", __func__, port->number); | |
343 | ||
344 | spin_lock_irqsave(&priv->lock, flags); | |
345 | if (priv->rts) | |
346 | result = TIOCM_RTS; | |
347 | spin_unlock_irqrestore(&priv->lock, flags); | |
348 | ||
349 | dbg("%s - %x", __func__, result); | |
350 | return result; | |
351 | } | |
352 | ||
353 | static int get_serial_info(struct opticon_private *priv, | |
354 | struct serial_struct __user *serial) | |
355 | { | |
356 | struct serial_struct tmp; | |
357 | ||
358 | if (!serial) | |
359 | return -EFAULT; | |
360 | ||
361 | memset(&tmp, 0x00, sizeof(tmp)); | |
362 | ||
363 | /* fake emulate a 16550 uart to make userspace code happy */ | |
364 | tmp.type = PORT_16550A; | |
365 | tmp.line = priv->serial->minor; | |
366 | tmp.port = 0; | |
367 | tmp.irq = 0; | |
368 | tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ; | |
369 | tmp.xmit_fifo_size = 1024; | |
370 | tmp.baud_base = 9600; | |
371 | tmp.close_delay = 5*HZ; | |
372 | tmp.closing_wait = 30*HZ; | |
373 | ||
374 | if (copy_to_user(serial, &tmp, sizeof(*serial))) | |
375 | return -EFAULT; | |
376 | return 0; | |
377 | } | |
378 | ||
379 | static int opticon_ioctl(struct tty_struct *tty, struct file *file, | |
380 | unsigned int cmd, unsigned long arg) | |
381 | { | |
382 | struct usb_serial_port *port = tty->driver_data; | |
383 | struct opticon_private *priv = usb_get_serial_data(port->serial); | |
384 | ||
385 | dbg("%s - port %d, cmd = 0x%x", __func__, port->number, cmd); | |
386 | ||
387 | switch (cmd) { | |
388 | case TIOCGSERIAL: | |
389 | return get_serial_info(priv, | |
390 | (struct serial_struct __user *)arg); | |
391 | } | |
392 | ||
393 | return -ENOIOCTLCMD; | |
394 | } | |
395 | ||
57262b82 GKH |
396 | static int opticon_startup(struct usb_serial *serial) |
397 | { | |
398 | struct opticon_private *priv; | |
399 | struct usb_host_interface *intf; | |
400 | int i; | |
401 | int retval = -ENOMEM; | |
402 | bool bulk_in_found = false; | |
403 | ||
404 | /* create our private serial structure */ | |
405 | priv = kzalloc(sizeof(*priv), GFP_KERNEL); | |
406 | if (priv == NULL) { | |
407 | dev_err(&serial->dev->dev, "%s - Out of memory\n", __func__); | |
408 | return -ENOMEM; | |
409 | } | |
410 | spin_lock_init(&priv->lock); | |
411 | priv->serial = serial; | |
412 | priv->port = serial->port[0]; | |
413 | priv->udev = serial->dev; | |
414 | ||
415 | /* find our bulk endpoint */ | |
416 | intf = serial->interface->altsetting; | |
417 | for (i = 0; i < intf->desc.bNumEndpoints; ++i) { | |
418 | struct usb_endpoint_descriptor *endpoint; | |
419 | ||
420 | endpoint = &intf->endpoint[i].desc; | |
421 | if (!usb_endpoint_is_bulk_in(endpoint)) | |
422 | continue; | |
423 | ||
424 | priv->bulk_read_urb = usb_alloc_urb(0, GFP_KERNEL); | |
425 | if (!priv->bulk_read_urb) { | |
426 | dev_err(&priv->udev->dev, "out of memory\n"); | |
427 | goto error; | |
428 | } | |
429 | ||
430 | priv->buffer_size = le16_to_cpu(endpoint->wMaxPacketSize) * 2; | |
431 | priv->bulk_in_buffer = kmalloc(priv->buffer_size, GFP_KERNEL); | |
432 | if (!priv->bulk_in_buffer) { | |
433 | dev_err(&priv->udev->dev, "out of memory\n"); | |
434 | goto error; | |
435 | } | |
436 | ||
437 | priv->bulk_address = endpoint->bEndpointAddress; | |
438 | ||
439 | /* set up our bulk urb */ | |
440 | usb_fill_bulk_urb(priv->bulk_read_urb, priv->udev, | |
441 | usb_rcvbulkpipe(priv->udev, | |
442 | endpoint->bEndpointAddress), | |
443 | priv->bulk_in_buffer, priv->buffer_size, | |
444 | opticon_bulk_callback, priv); | |
445 | ||
446 | bulk_in_found = true; | |
447 | break; | |
448 | } | |
449 | ||
450 | if (!bulk_in_found) { | |
451 | dev_err(&priv->udev->dev, | |
452 | "Error - the proper endpoints were not found!\n"); | |
453 | goto error; | |
454 | } | |
455 | ||
456 | usb_set_serial_data(serial, priv); | |
457 | return 0; | |
458 | ||
459 | error: | |
460 | usb_free_urb(priv->bulk_read_urb); | |
461 | kfree(priv->bulk_in_buffer); | |
462 | kfree(priv); | |
463 | return retval; | |
464 | } | |
465 | ||
f9c99bb8 | 466 | static void opticon_disconnect(struct usb_serial *serial) |
57262b82 GKH |
467 | { |
468 | struct opticon_private *priv = usb_get_serial_data(serial); | |
469 | ||
470 | dbg("%s", __func__); | |
471 | ||
472 | usb_kill_urb(priv->bulk_read_urb); | |
473 | usb_free_urb(priv->bulk_read_urb); | |
f9c99bb8 AS |
474 | } |
475 | ||
476 | static void opticon_release(struct usb_serial *serial) | |
477 | { | |
478 | struct opticon_private *priv = usb_get_serial_data(serial); | |
479 | ||
480 | dbg("%s", __func__); | |
481 | ||
57262b82 GKH |
482 | kfree(priv->bulk_in_buffer); |
483 | kfree(priv); | |
57262b82 GKH |
484 | } |
485 | ||
d1c0713d ON |
486 | static int opticon_suspend(struct usb_interface *intf, pm_message_t message) |
487 | { | |
488 | struct usb_serial *serial = usb_get_intfdata(intf); | |
489 | struct opticon_private *priv = usb_get_serial_data(serial); | |
490 | ||
491 | usb_kill_urb(priv->bulk_read_urb); | |
492 | return 0; | |
493 | } | |
494 | ||
495 | static int opticon_resume(struct usb_interface *intf) | |
496 | { | |
497 | struct usb_serial *serial = usb_get_intfdata(intf); | |
498 | struct opticon_private *priv = usb_get_serial_data(serial); | |
499 | struct usb_serial_port *port = serial->port[0]; | |
500 | int result; | |
501 | ||
502 | mutex_lock(&port->mutex); | |
503 | if (port->port.count) | |
504 | result = usb_submit_urb(priv->bulk_read_urb, GFP_NOIO); | |
505 | else | |
506 | result = 0; | |
507 | mutex_unlock(&port->mutex); | |
508 | return result; | |
509 | } | |
57262b82 GKH |
510 | |
511 | static struct usb_driver opticon_driver = { | |
512 | .name = "opticon", | |
513 | .probe = usb_serial_probe, | |
514 | .disconnect = usb_serial_disconnect, | |
d1c0713d ON |
515 | .suspend = opticon_suspend, |
516 | .resume = opticon_resume, | |
57262b82 GKH |
517 | .id_table = id_table, |
518 | .no_dynamic_id = 1, | |
519 | }; | |
520 | ||
521 | static struct usb_serial_driver opticon_device = { | |
522 | .driver = { | |
523 | .owner = THIS_MODULE, | |
524 | .name = "opticon", | |
525 | }, | |
526 | .id_table = id_table, | |
527 | .usb_driver = &opticon_driver, | |
528 | .num_ports = 1, | |
529 | .attach = opticon_startup, | |
530 | .open = opticon_open, | |
531 | .close = opticon_close, | |
648d4e16 GKH |
532 | .write = opticon_write, |
533 | .write_room = opticon_write_room, | |
f9c99bb8 AS |
534 | .disconnect = opticon_disconnect, |
535 | .release = opticon_release, | |
57262b82 GKH |
536 | .throttle = opticon_throttle, |
537 | .unthrottle = opticon_unthrottle, | |
faac64ad GKH |
538 | .ioctl = opticon_ioctl, |
539 | .tiocmget = opticon_tiocmget, | |
57262b82 GKH |
540 | }; |
541 | ||
542 | static int __init opticon_init(void) | |
543 | { | |
544 | int retval; | |
545 | ||
546 | retval = usb_serial_register(&opticon_device); | |
547 | if (retval) | |
548 | return retval; | |
549 | retval = usb_register(&opticon_driver); | |
550 | if (retval) | |
551 | usb_serial_deregister(&opticon_device); | |
552 | return retval; | |
553 | } | |
554 | ||
555 | static void __exit opticon_exit(void) | |
556 | { | |
557 | usb_deregister(&opticon_driver); | |
558 | usb_serial_deregister(&opticon_device); | |
559 | } | |
560 | ||
561 | module_init(opticon_init); | |
562 | module_exit(opticon_exit); | |
563 | MODULE_LICENSE("GPL"); | |
564 | ||
565 | module_param(debug, bool, S_IRUGO | S_IWUSR); | |
566 | MODULE_PARM_DESC(debug, "Debug enabled or not"); |