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>
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[] = {
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,
62 /* All of the device info needed for the Generic Serial Converter */
63 struct usb_serial_driver usb_serial_generic_device = {
68 .id_table = generic_device_ids,
69 .usb_driver = &generic_driver,
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);
90 int usb_serial_generic_register(int _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);
105 retval = usb_register(&generic_driver);
107 usb_serial_deregister(&usb_serial_generic_device);
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);
122 int usb_serial_generic_open(struct tty_struct *tty, struct usb_serial_port *port)
127 dbg("%s - port %d", __func__, port->number);
129 /* clear the throttle flags */
130 spin_lock_irqsave(&port->lock, flags);
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);
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;
149 dbg("%s - port %d", __func__, port->number);
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)
179 *dest = kmalloc(count, GFP_ATOMIC);
181 dev_err(&port->dev, "%s - could not allocate buffer\n",
188 memcpy(*dest, src, size);
190 count = kfifo_out_locked(&port->write_fifo, *dest, size,
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)
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__);
212 spin_unlock_irqrestore(&port->lock, flags);
214 urb = usb_alloc_urb(0, GFP_ATOMIC);
216 dev_err(&port->dev, "%s - no free urbs available\n", __func__);
222 count = min_t(int, count, PAGE_SIZE);
223 count = port->serial->type->prepare_write_buffer(port, &buffer, 0,
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),
234 port->serial->type->write_bulk_callback, port);
236 status = usb_submit_urb(urb, GFP_ATOMIC);
238 dev_err(&port->dev, "%s - error submitting urb: %d\n",
242 spin_lock_irqsave(&port->lock, flags);
243 port->tx_bytes += urb->transfer_buffer_length;
244 spin_unlock_irqrestore(&port->lock, flags);
254 spin_lock_irqsave(&port->lock, flags);
256 spin_unlock_irqrestore(&port->lock, flags);
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)
274 if (test_and_set_bit_lock(USB_SERIAL_WRITE_BUSY, &port->flags))
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);
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);
296 dev_err(&port->dev, "%s - error submitting urb: %d\n",
298 clear_bit_unlock(USB_SERIAL_WRITE_BUSY, &port->flags);
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). */
312 clear_bit_unlock(USB_SERIAL_WRITE_BUSY, &port->flags);
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
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;
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)
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);
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;
363 dbg("%s - port %d", __func__, port->number);
365 if (!port->bulk_out_size)
368 spin_lock_irqsave(&port->lock, flags);
369 if (serial->type->multi_urb_write)
370 room = (MAX_TX_URBS - port->tx_urbs) * PAGE_SIZE;
372 room = kfifo_avail(&port->write_fifo);
373 spin_unlock_irqrestore(&port->lock, flags);
375 dbg("%s - returns %d", __func__, 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;
386 dbg("%s - port %d", __func__, port->number);
388 if (!port->bulk_out_size)
391 spin_lock_irqsave(&port->lock, flags);
392 if (serial->type->multi_urb_write)
393 chars = port->tx_bytes;
395 chars = kfifo_len(&port->write_fifo) + port->tx_bytes;
396 spin_unlock_irqrestore(&port->lock, flags);
398 dbg("%s - returns %d", __func__, chars);
402 int usb_serial_generic_submit_read_urb(struct usb_serial_port *port,
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",
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;
423 tty = tty_port_tty_get(&port->port);
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);
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);
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;
450 dbg("%s - port %d", __func__, port->number);
452 if (unlikely(status != 0)) {
453 dbg("%s - nonzero read bulk status received: %d",
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);
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)
476 struct usb_serial_port *port = urb->context;
477 int status = urb->status;
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;
488 spin_unlock_irqrestore(&port->lock, flags);
490 for (i = 0; i < ARRAY_SIZE(port->write_urbs); ++i)
491 if (port->write_urbs[i] == urb)
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);
500 spin_lock_irqsave(&port->lock, flags);
501 kfifo_reset_out(&port->write_fifo);
502 spin_unlock_irqrestore(&port->lock, flags);
504 usb_serial_generic_write_start(port);
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;
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;
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);
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);
563 int usb_serial_handle_sysrq_char(struct tty_struct *tty,
564 struct usb_serial_port *port, unsigned int ch)
569 EXPORT_SYMBOL_GPL(usb_serial_handle_sysrq_char);
571 int usb_serial_handle_break(struct usb_serial_port *port)
574 port->sysrq = jiffies + HZ*5;
580 EXPORT_SYMBOL_GPL(usb_serial_handle_break);
582 int usb_serial_generic_resume(struct usb_serial *serial)
584 struct usb_serial_port *port;
587 for (i = 0; i < serial->num_ports; i++) {
588 port = serial->port[i];
589 if (!test_bit(ASYNCB_INITIALIZED, &port->port.flags))
592 if (port->read_urb) {
593 r = usb_submit_urb(port->read_urb, GFP_NOIO);
598 if (port->bulk_out_size) {
599 r = usb_serial_generic_write_start(port);
607 EXPORT_SYMBOL_GPL(usb_serial_generic_resume);
609 void usb_serial_generic_disconnect(struct usb_serial *serial)
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)