2 * USB Serial Converter Generic functions
4 * Copyright (C) 1999 - 2002 Greg Kroah-Hartman (greg@kroah.com)
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License version
8 * 2 as published by the Free Software Foundation.
12 #include <linux/kernel.h>
13 #include <linux/errno.h>
14 #include <linux/slab.h>
15 #include <linux/sysrq.h>
16 #include <linux/tty.h>
17 #include <linux/tty_flip.h>
18 #include <linux/module.h>
19 #include <linux/moduleparam.h>
20 #include <linux/usb.h>
21 #include <linux/usb/serial.h>
22 #include <linux/uaccess.h>
23 #include <linux/kfifo.h>
24 #include <linux/serial.h>
28 #ifdef CONFIG_USB_SERIAL_GENERIC
30 static int generic_probe(struct usb_interface *interface,
31 const struct usb_device_id *id);
33 static __u16 vendor = 0x05f9;
34 static __u16 product = 0xffff;
36 module_param(vendor, ushort, 0);
37 MODULE_PARM_DESC(vendor, "User specified USB idVendor");
39 module_param(product, ushort, 0);
40 MODULE_PARM_DESC(product, "User specified USB idProduct");
42 static struct usb_device_id generic_device_ids[2]; /* Initially all zeroes. */
44 /* we want to look at all devices, as the vendor/product id can change
45 * depending on the command line argument */
46 static const struct usb_device_id generic_serial_ids[] = {
51 static struct usb_driver generic_driver = {
52 .name = "usbserial_generic",
53 .probe = generic_probe,
54 .disconnect = usb_serial_disconnect,
55 .id_table = generic_serial_ids,
59 /* All of the device info needed for the Generic Serial Converter */
60 struct usb_serial_driver usb_serial_generic_device = {
65 .id_table = generic_device_ids,
66 .usb_driver = &generic_driver,
68 .disconnect = usb_serial_generic_disconnect,
69 .release = usb_serial_generic_release,
70 .throttle = usb_serial_generic_throttle,
71 .unthrottle = usb_serial_generic_unthrottle,
72 .resume = usb_serial_generic_resume,
75 static int generic_probe(struct usb_interface *interface,
76 const struct usb_device_id *id)
78 const struct usb_device_id *id_pattern;
80 id_pattern = usb_match_id(interface, generic_device_ids);
81 if (id_pattern != NULL)
82 return usb_serial_probe(interface, id);
87 int usb_serial_generic_register(int _debug)
92 #ifdef CONFIG_USB_SERIAL_GENERIC
93 generic_device_ids[0].idVendor = vendor;
94 generic_device_ids[0].idProduct = product;
95 generic_device_ids[0].match_flags =
96 USB_DEVICE_ID_MATCH_VENDOR | USB_DEVICE_ID_MATCH_PRODUCT;
98 /* register our generic driver with ourselves */
99 retval = usb_serial_register(&usb_serial_generic_device);
102 retval = usb_register(&generic_driver);
104 usb_serial_deregister(&usb_serial_generic_device);
110 void usb_serial_generic_deregister(void)
112 #ifdef CONFIG_USB_SERIAL_GENERIC
113 /* remove our generic driver */
114 usb_deregister(&generic_driver);
115 usb_serial_deregister(&usb_serial_generic_device);
119 int usb_serial_generic_open(struct tty_struct *tty, struct usb_serial_port *port)
121 struct usb_serial *serial = port->serial;
125 dbg("%s - port %d", __func__, port->number);
127 /* clear the throttle flags */
128 spin_lock_irqsave(&port->lock, flags);
130 port->throttle_req = 0;
131 spin_unlock_irqrestore(&port->lock, flags);
133 /* if we have a bulk endpoint, start reading from it */
134 if (port->bulk_in_size) {
135 /* Start reading from the device */
136 usb_fill_bulk_urb(port->read_urb, serial->dev,
137 usb_rcvbulkpipe(serial->dev,
138 port->bulk_in_endpointAddress),
139 port->read_urb->transfer_buffer,
140 port->read_urb->transfer_buffer_length,
141 ((serial->type->read_bulk_callback) ?
142 serial->type->read_bulk_callback :
143 usb_serial_generic_read_bulk_callback),
145 result = usb_submit_urb(port->read_urb, GFP_KERNEL);
148 "%s - failed resubmitting read urb, error %d\n",
154 EXPORT_SYMBOL_GPL(usb_serial_generic_open);
156 static void generic_cleanup(struct usb_serial_port *port)
158 struct usb_serial *serial = port->serial;
160 dbg("%s - port %d", __func__, port->number);
163 /* shutdown any bulk transfers that might be going on */
164 if (port->bulk_out_size)
165 usb_kill_urb(port->write_urb);
166 if (port->bulk_in_size)
167 usb_kill_urb(port->read_urb);
171 void usb_serial_generic_close(struct usb_serial_port *port)
173 dbg("%s - port %d", __func__, port->number);
174 generic_cleanup(port);
177 static int usb_serial_multi_urb_write(struct tty_struct *tty,
178 struct usb_serial_port *port, const unsigned char *buf, int count)
182 unsigned char *buffer;
187 dbg("%s - port %d", __func__, port->number);
190 dbg("%s - write request of 0 bytes", __func__);
193 towrite = (count > port->bulk_out_size) ?
194 port->bulk_out_size : count;
195 spin_lock_irqsave(&port->lock, flags);
196 if (port->urbs_in_flight >
197 port->serial->type->max_in_flight_urbs) {
198 spin_unlock_irqrestore(&port->lock, flags);
199 dbg("%s - write limit hit", __func__);
202 port->tx_bytes_flight += towrite;
203 port->urbs_in_flight++;
204 spin_unlock_irqrestore(&port->lock, flags);
206 buffer = kmalloc(towrite, GFP_ATOMIC);
209 "%s ran out of kernel memory for urb ...\n", __func__);
210 goto error_no_buffer;
213 urb = usb_alloc_urb(0, GFP_ATOMIC);
215 dev_err(&port->dev, "%s - no more free urbs\n",
221 memcpy(buffer, buf + bwrite, towrite);
222 usb_serial_debug_data(debug, &port->dev, __func__,
224 /* fill the buffer and send it */
225 usb_fill_bulk_urb(urb, port->serial->dev,
226 usb_sndbulkpipe(port->serial->dev,
227 port->bulk_out_endpointAddress),
229 usb_serial_generic_write_bulk_callback, port);
231 status = usb_submit_urb(urb, GFP_ATOMIC);
234 "%s - failed submitting write urb, error %d\n",
239 /* This urb is the responsibility of the host driver now */
241 dbg("%s write: %d", __func__, towrite);
252 spin_lock_irqsave(&port->lock, flags);
253 port->urbs_in_flight--;
254 port->tx_bytes_flight -= towrite;
255 spin_unlock_irqrestore(&port->lock, flags);
260 * usb_serial_generic_write_start - kick off an URB write
261 * @port: Pointer to the &struct usb_serial_port data
263 * Returns the number of bytes queued on success. This will be zero if there
264 * was nothing to send. Otherwise, it returns a negative errno value
266 static int usb_serial_generic_write_start(struct usb_serial_port *port)
268 struct usb_serial *serial = port->serial;
275 /* Atomically determine whether we can and need to start a USB
277 spin_lock_irqsave(&port->lock, flags);
278 if (port->write_urb_busy)
281 start_io = (kfifo_len(&port->write_fifo) != 0);
282 port->write_urb_busy = start_io;
284 spin_unlock_irqrestore(&port->lock, flags);
289 data = port->write_urb->transfer_buffer;
290 count = kfifo_out_locked(&port->write_fifo, data, port->bulk_out_size, &port->lock);
291 usb_serial_debug_data(debug, &port->dev, __func__, count, data);
294 usb_fill_bulk_urb(port->write_urb, serial->dev,
295 usb_sndbulkpipe(serial->dev,
296 port->bulk_out_endpointAddress),
297 port->write_urb->transfer_buffer, count,
298 ((serial->type->write_bulk_callback) ?
299 serial->type->write_bulk_callback :
300 usb_serial_generic_write_bulk_callback),
303 /* send the data out the bulk port */
304 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
307 "%s - failed submitting write urb, error %d\n",
309 /* don't have to grab the lock here, as we will
311 port->write_urb_busy = 0;
315 spin_lock_irqsave(&port->lock, flags);
316 port->tx_bytes_flight += count;
317 spin_unlock_irqrestore(&port->lock, flags);
323 * usb_serial_generic_write - generic write function for serial USB devices
324 * @tty: Pointer to &struct tty_struct for the device
325 * @port: Pointer to the &usb_serial_port structure for the device
326 * @buf: Pointer to the data to write
327 * @count: Number of bytes to write
329 * Returns the number of characters actually written, which may be anything
330 * from zero to @count. If an error occurs, it returns the negative errno
333 int usb_serial_generic_write(struct tty_struct *tty,
334 struct usb_serial_port *port, const unsigned char *buf, int count)
336 struct usb_serial *serial = port->serial;
339 dbg("%s - port %d", __func__, port->number);
341 /* only do something if we have a bulk out endpoint */
342 if (!port->bulk_out_size)
346 dbg("%s - write request of 0 bytes", __func__);
350 if (serial->type->max_in_flight_urbs)
351 return usb_serial_multi_urb_write(tty, port,
354 count = kfifo_in_locked(&port->write_fifo, buf, count, &port->lock);
355 result = usb_serial_generic_write_start(port);
362 EXPORT_SYMBOL_GPL(usb_serial_generic_write);
364 int usb_serial_generic_write_room(struct tty_struct *tty)
366 struct usb_serial_port *port = tty->driver_data;
367 struct usb_serial *serial = port->serial;
371 dbg("%s - port %d", __func__, port->number);
373 if (!port->bulk_out_size)
376 spin_lock_irqsave(&port->lock, flags);
377 if (serial->type->max_in_flight_urbs) {
378 if (port->urbs_in_flight < serial->type->max_in_flight_urbs)
379 room = port->bulk_out_size *
380 (serial->type->max_in_flight_urbs -
381 port->urbs_in_flight);
383 room = kfifo_avail(&port->write_fifo);
385 spin_unlock_irqrestore(&port->lock, flags);
387 dbg("%s - returns %d", __func__, room);
391 int usb_serial_generic_chars_in_buffer(struct tty_struct *tty)
393 struct usb_serial_port *port = tty->driver_data;
394 struct usb_serial *serial = port->serial;
398 dbg("%s - port %d", __func__, port->number);
400 if (!port->bulk_out_size)
403 spin_lock_irqsave(&port->lock, flags);
404 if (serial->type->max_in_flight_urbs)
405 chars = port->tx_bytes_flight;
407 chars = kfifo_len(&port->write_fifo) + port->tx_bytes_flight;
408 spin_unlock_irqrestore(&port->lock, flags);
410 dbg("%s - returns %d", __func__, chars);
415 void usb_serial_generic_resubmit_read_urb(struct usb_serial_port *port,
418 struct urb *urb = port->read_urb;
419 struct usb_serial *serial = port->serial;
422 /* Continue reading from device */
423 usb_fill_bulk_urb(urb, serial->dev,
424 usb_rcvbulkpipe(serial->dev,
425 port->bulk_in_endpointAddress),
426 urb->transfer_buffer,
427 urb->transfer_buffer_length,
428 ((serial->type->read_bulk_callback) ?
429 serial->type->read_bulk_callback :
430 usb_serial_generic_read_bulk_callback), port);
432 result = usb_submit_urb(urb, mem_flags);
433 if (result && result != -EPERM) {
435 "%s - failed resubmitting read urb, error %d\n",
439 EXPORT_SYMBOL_GPL(usb_serial_generic_resubmit_read_urb);
441 /* Push data to tty layer and resubmit the bulk read URB */
442 static void flush_and_resubmit_read_urb(struct usb_serial_port *port)
444 struct urb *urb = port->read_urb;
445 struct tty_struct *tty = tty_port_tty_get(&port->port);
446 char *ch = (char *)urb->transfer_buffer;
452 /* The per character mucking around with sysrq path it too slow for
453 stuff like 3G modems, so shortcircuit it in the 99.9999999% of cases
454 where the USB serial is not a console anyway */
455 if (!port->port.console || !port->sysrq)
456 tty_insert_flip_string(tty, ch, urb->actual_length);
458 /* Push data to tty */
459 for (i = 0; i < urb->actual_length; i++, ch++) {
460 if (!usb_serial_handle_sysrq_char(tty, port, *ch))
461 tty_insert_flip_char(tty, *ch, TTY_NORMAL);
464 tty_flip_buffer_push(tty);
467 usb_serial_generic_resubmit_read_urb(port, GFP_ATOMIC);
470 void usb_serial_generic_read_bulk_callback(struct urb *urb)
472 struct usb_serial_port *port = urb->context;
473 unsigned char *data = urb->transfer_buffer;
474 int status = urb->status;
477 dbg("%s - port %d", __func__, port->number);
479 if (unlikely(status != 0)) {
480 dbg("%s - nonzero read bulk status received: %d",
485 usb_serial_debug_data(debug, &port->dev, __func__,
486 urb->actual_length, data);
488 /* Throttle the device if requested by tty */
489 spin_lock_irqsave(&port->lock, flags);
490 port->throttled = port->throttle_req;
491 if (!port->throttled) {
492 spin_unlock_irqrestore(&port->lock, flags);
493 flush_and_resubmit_read_urb(port);
495 spin_unlock_irqrestore(&port->lock, flags);
497 EXPORT_SYMBOL_GPL(usb_serial_generic_read_bulk_callback);
499 void usb_serial_generic_write_bulk_callback(struct urb *urb)
502 struct usb_serial_port *port = urb->context;
503 int status = urb->status;
505 dbg("%s - port %d", __func__, port->number);
507 if (port->serial->type->max_in_flight_urbs) {
508 kfree(urb->transfer_buffer);
510 spin_lock_irqsave(&port->lock, flags);
511 --port->urbs_in_flight;
512 port->tx_bytes_flight -= urb->transfer_buffer_length;
513 if (port->urbs_in_flight < 0)
514 port->urbs_in_flight = 0;
515 spin_unlock_irqrestore(&port->lock, flags);
517 spin_lock_irqsave(&port->lock, flags);
518 port->tx_bytes_flight -= urb->transfer_buffer_length;
519 port->write_urb_busy = 0;
520 spin_unlock_irqrestore(&port->lock, flags);
523 spin_lock_irqsave(&port->lock, flags);
524 kfifo_reset_out(&port->write_fifo);
525 spin_unlock_irqrestore(&port->lock, flags);
527 usb_serial_generic_write_start(port);
532 dbg("%s - non-zero urb status: %d", __func__, status);
534 usb_serial_port_softint(port);
536 EXPORT_SYMBOL_GPL(usb_serial_generic_write_bulk_callback);
538 void usb_serial_generic_throttle(struct tty_struct *tty)
540 struct usb_serial_port *port = tty->driver_data;
543 dbg("%s - port %d", __func__, port->number);
545 /* Set the throttle request flag. It will be picked up
546 * by usb_serial_generic_read_bulk_callback(). */
547 spin_lock_irqsave(&port->lock, flags);
548 port->throttle_req = 1;
549 spin_unlock_irqrestore(&port->lock, flags);
552 void usb_serial_generic_unthrottle(struct tty_struct *tty)
554 struct usb_serial_port *port = tty->driver_data;
558 dbg("%s - port %d", __func__, port->number);
560 /* Clear the throttle flags */
561 spin_lock_irqsave(&port->lock, flags);
562 was_throttled = port->throttled;
563 port->throttled = port->throttle_req = 0;
564 spin_unlock_irqrestore(&port->lock, flags);
567 /* Resume reading from device */
568 flush_and_resubmit_read_urb(port);
572 #ifdef CONFIG_MAGIC_SYSRQ
573 int usb_serial_handle_sysrq_char(struct tty_struct *tty,
574 struct usb_serial_port *port, unsigned int ch)
576 if (port->sysrq && port->port.console) {
577 if (ch && time_before(jiffies, port->sysrq)) {
578 handle_sysrq(ch, tty);
587 int usb_serial_handle_sysrq_char(struct tty_struct *tty,
588 struct usb_serial_port *port, unsigned int ch)
593 EXPORT_SYMBOL_GPL(usb_serial_handle_sysrq_char);
595 int usb_serial_handle_break(struct usb_serial_port *port)
598 port->sysrq = jiffies + HZ*5;
604 EXPORT_SYMBOL_GPL(usb_serial_handle_break);
606 int usb_serial_generic_resume(struct usb_serial *serial)
608 struct usb_serial_port *port;
611 for (i = 0; i < serial->num_ports; i++) {
612 port = serial->port[i];
613 if (!test_bit(ASYNCB_INITIALIZED, &port->port.flags))
616 if (port->read_urb) {
617 r = usb_submit_urb(port->read_urb, GFP_NOIO);
622 if (port->write_urb) {
623 r = usb_serial_generic_write_start(port);
631 EXPORT_SYMBOL_GPL(usb_serial_generic_resume);
633 void usb_serial_generic_disconnect(struct usb_serial *serial)
639 /* stop reads and writes on all ports */
640 for (i = 0; i < serial->num_ports; ++i)
641 generic_cleanup(serial->port[i]);
644 void usb_serial_generic_release(struct usb_serial *serial)