USB: serial: reimplement generic fifo-based writes
[linux-2.6.git] / drivers / usb / serial / generic.c
blob3ae17840175cfa61969eabfba78f435e10910723
1 /*
2 * USB Serial Converter Generic functions
4 * Copyright (C) 2010 Johan Hovold (jhovold@gmail.com)
5 * Copyright (C) 1999 - 2002 Greg Kroah-Hartman (greg@kroah.com)
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.
13 #include <linux/kernel.h>
14 #include <linux/errno.h>
15 #include <linux/slab.h>
16 #include <linux/sysrq.h>
17 #include <linux/tty.h>
18 #include <linux/tty_flip.h>
19 #include <linux/module.h>
20 #include <linux/moduleparam.h>
21 #include <linux/usb.h>
22 #include <linux/usb/serial.h>
23 #include <linux/uaccess.h>
24 #include <linux/kfifo.h>
25 #include <linux/serial.h>
27 static int debug;
29 #define MAX_TX_URBS 40
31 #ifdef CONFIG_USB_SERIAL_GENERIC
33 static int generic_probe(struct usb_interface *interface,
34 const struct usb_device_id *id);
36 static __u16 vendor = 0x05f9;
37 static __u16 product = 0xffff;
39 module_param(vendor, ushort, 0);
40 MODULE_PARM_DESC(vendor, "User specified USB idVendor");
42 module_param(product, ushort, 0);
43 MODULE_PARM_DESC(product, "User specified USB idProduct");
45 static struct usb_device_id generic_device_ids[2]; /* Initially all zeroes. */
47 /* we want to look at all devices, as the vendor/product id can change
48 * depending on the command line argument */
49 static const struct usb_device_id generic_serial_ids[] = {
50 {.driver_info = 42},
54 static struct usb_driver generic_driver = {
55 .name = "usbserial_generic",
56 .probe = generic_probe,
57 .disconnect = usb_serial_disconnect,
58 .id_table = generic_serial_ids,
59 .no_dynamic_id = 1,
62 /* All of the device info needed for the Generic Serial Converter */
63 struct usb_serial_driver usb_serial_generic_device = {
64 .driver = {
65 .owner = THIS_MODULE,
66 .name = "generic",
68 .id_table = generic_device_ids,
69 .usb_driver = &generic_driver,
70 .num_ports = 1,
71 .disconnect = usb_serial_generic_disconnect,
72 .release = usb_serial_generic_release,
73 .throttle = usb_serial_generic_throttle,
74 .unthrottle = usb_serial_generic_unthrottle,
75 .resume = usb_serial_generic_resume,
78 static int generic_probe(struct usb_interface *interface,
79 const struct usb_device_id *id)
81 const struct usb_device_id *id_pattern;
83 id_pattern = usb_match_id(interface, generic_device_ids);
84 if (id_pattern != NULL)
85 return usb_serial_probe(interface, id);
86 return -ENODEV;
88 #endif
90 int usb_serial_generic_register(int _debug)
92 int retval = 0;
94 debug = _debug;
95 #ifdef CONFIG_USB_SERIAL_GENERIC
96 generic_device_ids[0].idVendor = vendor;
97 generic_device_ids[0].idProduct = product;
98 generic_device_ids[0].match_flags =
99 USB_DEVICE_ID_MATCH_VENDOR | USB_DEVICE_ID_MATCH_PRODUCT;
101 /* register our generic driver with ourselves */
102 retval = usb_serial_register(&usb_serial_generic_device);
103 if (retval)
104 goto exit;
105 retval = usb_register(&generic_driver);
106 if (retval)
107 usb_serial_deregister(&usb_serial_generic_device);
108 exit:
109 #endif
110 return retval;
113 void usb_serial_generic_deregister(void)
115 #ifdef CONFIG_USB_SERIAL_GENERIC
116 /* remove our generic driver */
117 usb_deregister(&generic_driver);
118 usb_serial_deregister(&usb_serial_generic_device);
119 #endif
122 int usb_serial_generic_open(struct tty_struct *tty, struct usb_serial_port *port)
124 int result = 0;
125 unsigned long flags;
127 dbg("%s - port %d", __func__, port->number);
129 /* clear the throttle flags */
130 spin_lock_irqsave(&port->lock, flags);
131 port->throttled = 0;
132 port->throttle_req = 0;
133 spin_unlock_irqrestore(&port->lock, flags);
135 /* if we have a bulk endpoint, start reading from it */
136 if (port->bulk_in_size)
137 result = usb_serial_generic_submit_read_urb(port, GFP_KERNEL);
139 return result;
141 EXPORT_SYMBOL_GPL(usb_serial_generic_open);
143 static void generic_cleanup(struct usb_serial_port *port)
145 struct usb_serial *serial = port->serial;
146 unsigned long flags;
147 int i;
149 dbg("%s - port %d", __func__, port->number);
151 if (serial->dev) {
152 /* shutdown any bulk transfers that might be going on */
153 if (port->bulk_out_size) {
154 usb_kill_urb(port->write_urb);
155 for (i = 0; i < ARRAY_SIZE(port->write_urbs); ++i)
156 usb_kill_urb(port->write_urbs[i]);
158 spin_lock_irqsave(&port->lock, flags);
159 kfifo_reset_out(&port->write_fifo);
160 spin_unlock_irqrestore(&port->lock, flags);
162 if (port->bulk_in_size)
163 usb_kill_urb(port->read_urb);
167 void usb_serial_generic_close(struct usb_serial_port *port)
169 dbg("%s - port %d", __func__, port->number);
170 generic_cleanup(port);
172 EXPORT_SYMBOL_GPL(usb_serial_generic_close);
174 int usb_serial_generic_prepare_write_buffer(struct usb_serial_port *port,
175 void **dest, size_t size, const void *src, size_t count)
177 if (!*dest) {
178 size = count;
179 *dest = kmalloc(count, GFP_ATOMIC);
180 if (!*dest) {
181 dev_err(&port->dev, "%s - could not allocate buffer\n",
182 __func__);
183 return -ENOMEM;
186 if (src) {
187 count = size;
188 memcpy(*dest, src, size);
189 } else {
190 count = kfifo_out_locked(&port->write_fifo, *dest, size,
191 &port->lock);
193 return count;
195 EXPORT_SYMBOL_GPL(usb_serial_generic_prepare_write_buffer);
197 static int usb_serial_multi_urb_write(struct tty_struct *tty,
198 struct usb_serial_port *port, const unsigned char *buf, int count)
200 unsigned long flags;
201 struct urb *urb;
202 void *buffer;
203 int status;
205 spin_lock_irqsave(&port->lock, flags);
206 if (port->tx_urbs == MAX_TX_URBS) {
207 spin_unlock_irqrestore(&port->lock, flags);
208 dbg("%s - write limit hit", __func__);
209 return 0;
211 port->tx_urbs++;
212 spin_unlock_irqrestore(&port->lock, flags);
214 urb = usb_alloc_urb(0, GFP_ATOMIC);
215 if (!urb) {
216 dev_err(&port->dev, "%s - no free urbs available\n", __func__);
217 status = -ENOMEM;
218 goto err_urb;
221 buffer = NULL;
222 count = min_t(int, count, PAGE_SIZE);
223 count = port->serial->type->prepare_write_buffer(port, &buffer, 0,
224 buf, count);
225 if (count < 0) {
226 status = count;
227 goto err_buf;
229 usb_serial_debug_data(debug, &port->dev, __func__, count, buffer);
230 usb_fill_bulk_urb(urb, port->serial->dev,
231 usb_sndbulkpipe(port->serial->dev,
232 port->bulk_out_endpointAddress),
233 buffer, count,
234 port->serial->type->write_bulk_callback, port);
236 status = usb_submit_urb(urb, GFP_ATOMIC);
237 if (status) {
238 dev_err(&port->dev, "%s - error submitting urb: %d\n",
239 __func__, status);
240 goto err;
242 spin_lock_irqsave(&port->lock, flags);
243 port->tx_bytes += urb->transfer_buffer_length;
244 spin_unlock_irqrestore(&port->lock, flags);
246 usb_free_urb(urb);
248 return count;
249 err:
250 kfree(buffer);
251 err_buf:
252 usb_free_urb(urb);
253 err_urb:
254 spin_lock_irqsave(&port->lock, flags);
255 port->tx_urbs--;
256 spin_unlock_irqrestore(&port->lock, flags);
258 return status;
262 * usb_serial_generic_write_start - kick off an URB write
263 * @port: Pointer to the &struct usb_serial_port data
265 * Returns zero on success, or a negative errno value
267 static int usb_serial_generic_write_start(struct usb_serial_port *port)
269 struct urb *urb;
270 int count, result;
271 unsigned long flags;
272 int i;
274 if (test_and_set_bit_lock(USB_SERIAL_WRITE_BUSY, &port->flags))
275 return 0;
276 retry:
277 spin_lock_irqsave(&port->lock, flags);
278 if (!port->write_urbs_free || !kfifo_len(&port->write_fifo)) {
279 clear_bit_unlock(USB_SERIAL_WRITE_BUSY, &port->flags);
280 spin_unlock_irqrestore(&port->lock, flags);
281 return 0;
283 i = (int)find_first_bit(&port->write_urbs_free,
284 ARRAY_SIZE(port->write_urbs));
285 spin_unlock_irqrestore(&port->lock, flags);
287 urb = port->write_urbs[i];
288 count = port->serial->type->prepare_write_buffer(port,
289 &urb->transfer_buffer,
290 port->bulk_out_size, NULL, 0);
291 urb->transfer_buffer_length = count;
292 usb_serial_debug_data(debug, &port->dev, __func__, count,
293 urb->transfer_buffer);
294 result = usb_submit_urb(urb, GFP_ATOMIC);
295 if (result) {
296 dev_err(&port->dev, "%s - error submitting urb: %d\n",
297 __func__, result);
298 clear_bit_unlock(USB_SERIAL_WRITE_BUSY, &port->flags);
299 return result;
301 clear_bit(i, &port->write_urbs_free);
303 spin_lock_irqsave(&port->lock, flags);
304 port->tx_bytes += count;
305 spin_unlock_irqrestore(&port->lock, flags);
307 /* Try sending off another urb, unless in irq context (in which case
308 * there will be no free urb). */
309 if (!in_irq())
310 goto retry;
312 clear_bit_unlock(USB_SERIAL_WRITE_BUSY, &port->flags);
314 return 0;
318 * usb_serial_generic_write - generic write function for serial USB devices
319 * @tty: Pointer to &struct tty_struct for the device
320 * @port: Pointer to the &usb_serial_port structure for the device
321 * @buf: Pointer to the data to write
322 * @count: Number of bytes to write
324 * Returns the number of characters actually written, which may be anything
325 * from zero to @count. If an error occurs, it returns the negative errno
326 * value.
328 int usb_serial_generic_write(struct tty_struct *tty,
329 struct usb_serial_port *port, const unsigned char *buf, int count)
331 struct usb_serial *serial = port->serial;
332 int result;
334 dbg("%s - port %d", __func__, port->number);
336 /* only do something if we have a bulk out endpoint */
337 if (!port->bulk_out_size)
338 return -ENODEV;
340 if (!count)
341 return 0;
343 if (serial->type->multi_urb_write)
344 return usb_serial_multi_urb_write(tty, port, buf, count);
346 count = kfifo_in_locked(&port->write_fifo, buf, count, &port->lock);
347 result = usb_serial_generic_write_start(port);
349 if (result >= 0)
350 result = count;
352 return result;
354 EXPORT_SYMBOL_GPL(usb_serial_generic_write);
356 int usb_serial_generic_write_room(struct tty_struct *tty)
358 struct usb_serial_port *port = tty->driver_data;
359 struct usb_serial *serial = port->serial;
360 unsigned long flags;
361 int room;
363 dbg("%s - port %d", __func__, port->number);
365 if (!port->bulk_out_size)
366 return 0;
368 spin_lock_irqsave(&port->lock, flags);
369 if (serial->type->multi_urb_write)
370 room = (MAX_TX_URBS - port->tx_urbs) * PAGE_SIZE;
371 else
372 room = kfifo_avail(&port->write_fifo);
373 spin_unlock_irqrestore(&port->lock, flags);
375 dbg("%s - returns %d", __func__, room);
376 return room;
379 int usb_serial_generic_chars_in_buffer(struct tty_struct *tty)
381 struct usb_serial_port *port = tty->driver_data;
382 struct usb_serial *serial = port->serial;
383 unsigned long flags;
384 int chars;
386 dbg("%s - port %d", __func__, port->number);
388 if (!port->bulk_out_size)
389 return 0;
391 spin_lock_irqsave(&port->lock, flags);
392 if (serial->type->multi_urb_write)
393 chars = port->tx_bytes;
394 else
395 chars = kfifo_len(&port->write_fifo) + port->tx_bytes;
396 spin_unlock_irqrestore(&port->lock, flags);
398 dbg("%s - returns %d", __func__, chars);
399 return chars;
402 int usb_serial_generic_submit_read_urb(struct usb_serial_port *port,
403 gfp_t mem_flags)
405 int result;
407 result = usb_submit_urb(port->read_urb, mem_flags);
408 if (result && result != -EPERM) {
409 dev_err(&port->dev, "%s - error submitting urb: %d\n",
410 __func__, result);
412 return result;
414 EXPORT_SYMBOL_GPL(usb_serial_generic_submit_read_urb);
416 void usb_serial_generic_process_read_urb(struct urb *urb)
418 struct usb_serial_port *port = urb->context;
419 struct tty_struct *tty;
420 char *ch = (char *)urb->transfer_buffer;
421 int i;
423 tty = tty_port_tty_get(&port->port);
424 if (!tty)
425 return;
427 /* The per character mucking around with sysrq path it too slow for
428 stuff like 3G modems, so shortcircuit it in the 99.9999999% of cases
429 where the USB serial is not a console anyway */
430 if (!port->port.console || !port->sysrq)
431 tty_insert_flip_string(tty, ch, urb->actual_length);
432 else {
433 for (i = 0; i < urb->actual_length; i++, ch++) {
434 if (!usb_serial_handle_sysrq_char(tty, port, *ch))
435 tty_insert_flip_char(tty, *ch, TTY_NORMAL);
438 tty_flip_buffer_push(tty);
439 tty_kref_put(tty);
441 EXPORT_SYMBOL_GPL(usb_serial_generic_process_read_urb);
443 void usb_serial_generic_read_bulk_callback(struct urb *urb)
445 struct usb_serial_port *port = urb->context;
446 unsigned char *data = urb->transfer_buffer;
447 int status = urb->status;
448 unsigned long flags;
450 dbg("%s - port %d", __func__, port->number);
452 if (unlikely(status != 0)) {
453 dbg("%s - nonzero read bulk status received: %d",
454 __func__, status);
455 return;
458 usb_serial_debug_data(debug, &port->dev, __func__,
459 urb->actual_length, data);
460 port->serial->type->process_read_urb(urb);
462 /* Throttle the device if requested by tty */
463 spin_lock_irqsave(&port->lock, flags);
464 port->throttled = port->throttle_req;
465 if (!port->throttled) {
466 spin_unlock_irqrestore(&port->lock, flags);
467 usb_serial_generic_submit_read_urb(port, GFP_ATOMIC);
468 } else
469 spin_unlock_irqrestore(&port->lock, flags);
471 EXPORT_SYMBOL_GPL(usb_serial_generic_read_bulk_callback);
473 void usb_serial_generic_write_bulk_callback(struct urb *urb)
475 unsigned long flags;
476 struct usb_serial_port *port = urb->context;
477 int status = urb->status;
478 int i;
480 dbg("%s - port %d", __func__, port->number);
482 if (port->serial->type->multi_urb_write) {
483 kfree(urb->transfer_buffer);
485 spin_lock_irqsave(&port->lock, flags);
486 port->tx_bytes -= urb->transfer_buffer_length;
487 port->tx_urbs--;
488 spin_unlock_irqrestore(&port->lock, flags);
489 } else {
490 for (i = 0; i < ARRAY_SIZE(port->write_urbs); ++i)
491 if (port->write_urbs[i] == urb)
492 break;
494 spin_lock_irqsave(&port->lock, flags);
495 port->tx_bytes -= urb->transfer_buffer_length;
496 set_bit(i, &port->write_urbs_free);
497 spin_unlock_irqrestore(&port->lock, flags);
499 if (status) {
500 spin_lock_irqsave(&port->lock, flags);
501 kfifo_reset_out(&port->write_fifo);
502 spin_unlock_irqrestore(&port->lock, flags);
503 } else {
504 usb_serial_generic_write_start(port);
508 if (status)
509 dbg("%s - non-zero urb status: %d", __func__, status);
511 usb_serial_port_softint(port);
513 EXPORT_SYMBOL_GPL(usb_serial_generic_write_bulk_callback);
515 void usb_serial_generic_throttle(struct tty_struct *tty)
517 struct usb_serial_port *port = tty->driver_data;
518 unsigned long flags;
520 dbg("%s - port %d", __func__, port->number);
522 /* Set the throttle request flag. It will be picked up
523 * by usb_serial_generic_read_bulk_callback(). */
524 spin_lock_irqsave(&port->lock, flags);
525 port->throttle_req = 1;
526 spin_unlock_irqrestore(&port->lock, flags);
528 EXPORT_SYMBOL_GPL(usb_serial_generic_throttle);
530 void usb_serial_generic_unthrottle(struct tty_struct *tty)
532 struct usb_serial_port *port = tty->driver_data;
533 int was_throttled;
535 dbg("%s - port %d", __func__, port->number);
537 /* Clear the throttle flags */
538 spin_lock_irq(&port->lock);
539 was_throttled = port->throttled;
540 port->throttled = port->throttle_req = 0;
541 spin_unlock_irq(&port->lock);
543 if (was_throttled)
544 usb_serial_generic_submit_read_urb(port, GFP_KERNEL);
546 EXPORT_SYMBOL_GPL(usb_serial_generic_unthrottle);
548 #ifdef CONFIG_MAGIC_SYSRQ
549 int usb_serial_handle_sysrq_char(struct tty_struct *tty,
550 struct usb_serial_port *port, unsigned int ch)
552 if (port->sysrq && port->port.console) {
553 if (ch && time_before(jiffies, port->sysrq)) {
554 handle_sysrq(ch, tty);
555 port->sysrq = 0;
556 return 1;
558 port->sysrq = 0;
560 return 0;
562 #else
563 int usb_serial_handle_sysrq_char(struct tty_struct *tty,
564 struct usb_serial_port *port, unsigned int ch)
566 return 0;
568 #endif
569 EXPORT_SYMBOL_GPL(usb_serial_handle_sysrq_char);
571 int usb_serial_handle_break(struct usb_serial_port *port)
573 if (!port->sysrq) {
574 port->sysrq = jiffies + HZ*5;
575 return 1;
577 port->sysrq = 0;
578 return 0;
580 EXPORT_SYMBOL_GPL(usb_serial_handle_break);
582 int usb_serial_generic_resume(struct usb_serial *serial)
584 struct usb_serial_port *port;
585 int i, c = 0, r;
587 for (i = 0; i < serial->num_ports; i++) {
588 port = serial->port[i];
589 if (!test_bit(ASYNCB_INITIALIZED, &port->port.flags))
590 continue;
592 if (port->read_urb) {
593 r = usb_submit_urb(port->read_urb, GFP_NOIO);
594 if (r < 0)
595 c++;
598 if (port->bulk_out_size) {
599 r = usb_serial_generic_write_start(port);
600 if (r < 0)
601 c++;
605 return c ? -EIO : 0;
607 EXPORT_SYMBOL_GPL(usb_serial_generic_resume);
609 void usb_serial_generic_disconnect(struct usb_serial *serial)
611 int i;
613 dbg("%s", __func__);
615 /* stop reads and writes on all ports */
616 for (i = 0; i < serial->num_ports; ++i)
617 generic_cleanup(serial->port[i]);
620 void usb_serial_generic_release(struct usb_serial *serial)
622 dbg("%s", __func__);