2 * USB Serial Converter Generic functions
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/tty.h>
16 #include <linux/tty_flip.h>
17 #include <linux/module.h>
18 #include <linux/moduleparam.h>
19 #include <linux/usb.h>
20 #include <linux/usb/serial.h>
21 #include <linux/uaccess.h>
22 #include <linux/kfifo.h>
26 #ifdef CONFIG_USB_SERIAL_GENERIC
28 static int generic_probe(struct usb_interface *interface,
29 const struct usb_device_id *id);
31 static __u16 vendor = 0x05f9;
32 static __u16 product = 0xffff;
34 module_param(vendor, ushort, 0);
35 MODULE_PARM_DESC(vendor, "User specified USB idVendor");
37 module_param(product, ushort, 0);
38 MODULE_PARM_DESC(product, "User specified USB idProduct");
40 static struct usb_device_id generic_device_ids[2]; /* Initially all zeroes. */
42 /* we want to look at all devices, as the vendor/product id can change
43 * depending on the command line argument */
44 static struct usb_device_id generic_serial_ids[] = {
49 static struct usb_driver generic_driver = {
50 .name = "usbserial_generic",
51 .probe = generic_probe,
52 .disconnect = usb_serial_disconnect,
53 .id_table = generic_serial_ids,
57 /* All of the device info needed for the Generic Serial Converter */
58 struct usb_serial_driver usb_serial_generic_device = {
63 .id_table = generic_device_ids,
64 .usb_driver = &generic_driver,
66 .disconnect = usb_serial_generic_disconnect,
67 .release = usb_serial_generic_release,
68 .throttle = usb_serial_generic_throttle,
69 .unthrottle = usb_serial_generic_unthrottle,
70 .resume = usb_serial_generic_resume,
73 static int generic_probe(struct usb_interface *interface,
74 const struct usb_device_id *id)
76 const struct usb_device_id *id_pattern;
78 id_pattern = usb_match_id(interface, generic_device_ids);
79 if (id_pattern != NULL)
80 return usb_serial_probe(interface, id);
85 int usb_serial_generic_register(int _debug)
90 #ifdef CONFIG_USB_SERIAL_GENERIC
91 generic_device_ids[0].idVendor = vendor;
92 generic_device_ids[0].idProduct = product;
93 generic_device_ids[0].match_flags =
94 USB_DEVICE_ID_MATCH_VENDOR | USB_DEVICE_ID_MATCH_PRODUCT;
96 /* register our generic driver with ourselves */
97 retval = usb_serial_register(&usb_serial_generic_device);
100 retval = usb_register(&generic_driver);
102 usb_serial_deregister(&usb_serial_generic_device);
108 void usb_serial_generic_deregister(void)
110 #ifdef CONFIG_USB_SERIAL_GENERIC
111 /* remove our generic driver */
112 usb_deregister(&generic_driver);
113 usb_serial_deregister(&usb_serial_generic_device);
117 int usb_serial_generic_open(struct tty_struct *tty, struct usb_serial_port *port)
119 struct usb_serial *serial = port->serial;
123 dbg("%s - port %d", __func__, port->number);
125 /* clear the throttle flags */
126 spin_lock_irqsave(&port->lock, flags);
128 port->throttle_req = 0;
129 spin_unlock_irqrestore(&port->lock, flags);
131 /* if we have a bulk endpoint, start reading from it */
132 if (serial->num_bulk_in) {
133 /* Start reading from the device */
134 usb_fill_bulk_urb(port->read_urb, serial->dev,
135 usb_rcvbulkpipe(serial->dev,
136 port->bulk_in_endpointAddress),
137 port->read_urb->transfer_buffer,
138 port->read_urb->transfer_buffer_length,
139 ((serial->type->read_bulk_callback) ?
140 serial->type->read_bulk_callback :
141 usb_serial_generic_read_bulk_callback),
143 result = usb_submit_urb(port->read_urb, GFP_KERNEL);
146 "%s - failed resubmitting read urb, error %d\n",
152 EXPORT_SYMBOL_GPL(usb_serial_generic_open);
154 static void generic_cleanup(struct usb_serial_port *port)
156 struct usb_serial *serial = port->serial;
158 dbg("%s - port %d", __func__, port->number);
161 /* shutdown any bulk reads that might be going on */
162 if (serial->num_bulk_out)
163 usb_kill_urb(port->write_urb);
164 if (serial->num_bulk_in)
165 usb_kill_urb(port->read_urb);
169 void usb_serial_generic_close(struct usb_serial_port *port)
171 dbg("%s - port %d", __func__, port->number);
172 generic_cleanup(port);
175 static int usb_serial_multi_urb_write(struct tty_struct *tty,
176 struct usb_serial_port *port, const unsigned char *buf, int count)
180 unsigned char *buffer;
185 dbg("%s - port %d", __func__, port->number);
188 dbg("%s - write request of 0 bytes", __func__);
191 towrite = (count > port->bulk_out_size) ?
192 port->bulk_out_size : count;
193 spin_lock_irqsave(&port->lock, flags);
194 if (port->urbs_in_flight >
195 port->serial->type->max_in_flight_urbs) {
196 spin_unlock_irqrestore(&port->lock, flags);
197 dbg("%s - write limit hit\n", __func__);
200 port->tx_bytes_flight += towrite;
201 port->urbs_in_flight++;
202 spin_unlock_irqrestore(&port->lock, flags);
204 buffer = kmalloc(towrite, GFP_ATOMIC);
207 "%s ran out of kernel memory for urb ...\n", __func__);
208 goto error_no_buffer;
211 urb = usb_alloc_urb(0, GFP_ATOMIC);
213 dev_err(&port->dev, "%s - no more free urbs\n",
219 memcpy(buffer, buf + bwrite, towrite);
220 usb_serial_debug_data(debug, &port->dev, __func__,
222 /* fill the buffer and send it */
223 usb_fill_bulk_urb(urb, port->serial->dev,
224 usb_sndbulkpipe(port->serial->dev,
225 port->bulk_out_endpointAddress),
227 usb_serial_generic_write_bulk_callback, port);
229 status = usb_submit_urb(urb, GFP_ATOMIC);
232 "%s - failed submitting write urb, error %d\n",
237 /* This urb is the responsibility of the host driver now */
239 dbg("%s write: %d", __func__, towrite);
250 spin_lock_irqsave(&port->lock, flags);
251 port->urbs_in_flight--;
252 port->tx_bytes_flight -= towrite;
253 spin_unlock_irqrestore(&port->lock, flags);
258 * usb_serial_generic_write_start - kick off an URB write
259 * @port: Pointer to the &struct usb_serial_port data
261 * Returns the number of bytes queued on success. This will be zero if there
262 * was nothing to send. Otherwise, it returns a negative errno value
264 static int usb_serial_generic_write_start(struct usb_serial_port *port)
266 struct usb_serial *serial = port->serial;
273 /* Atomically determine whether we can and need to start a USB
275 spin_lock_irqsave(&port->lock, flags);
276 if (port->write_urb_busy)
279 start_io = (__kfifo_len(port->write_fifo) != 0);
280 port->write_urb_busy = start_io;
282 spin_unlock_irqrestore(&port->lock, flags);
287 data = port->write_urb->transfer_buffer;
288 count = kfifo_get(port->write_fifo, data, port->bulk_out_size);
289 usb_serial_debug_data(debug, &port->dev, __func__, count, data);
292 usb_fill_bulk_urb(port->write_urb, serial->dev,
293 usb_sndbulkpipe(serial->dev,
294 port->bulk_out_endpointAddress),
295 port->write_urb->transfer_buffer, count,
296 ((serial->type->write_bulk_callback) ?
297 serial->type->write_bulk_callback :
298 usb_serial_generic_write_bulk_callback),
301 /* send the data out the bulk port */
302 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
305 "%s - failed submitting write urb, error %d\n",
307 /* don't have to grab the lock here, as we will
309 port->write_urb_busy = 0;
317 * usb_serial_generic_write - generic write function for serial USB devices
318 * @tty: Pointer to &struct tty_struct for the device
319 * @port: Pointer to the &usb_serial_port structure for the device
320 * @buf: Pointer to the data to write
321 * @count: Number of bytes to write
323 * Returns the number of characters actually written, which may be anything
324 * from zero to @count. If an error occurs, it returns the negative errno
327 int usb_serial_generic_write(struct tty_struct *tty,
328 struct usb_serial_port *port, const unsigned char *buf, int count)
330 struct usb_serial *serial = port->serial;
333 dbg("%s - port %d", __func__, port->number);
336 dbg("%s - write request of 0 bytes", __func__);
340 /* only do something if we have a bulk out endpoint */
341 if (!serial->num_bulk_out)
344 if (serial->type->max_in_flight_urbs)
345 return usb_serial_multi_urb_write(tty, port,
348 count = kfifo_put(port->write_fifo, buf, count);
349 result = usb_serial_generic_write_start(port);
356 EXPORT_SYMBOL_GPL(usb_serial_generic_write);
358 int usb_serial_generic_write_room(struct tty_struct *tty)
360 struct usb_serial_port *port = tty->driver_data;
361 struct usb_serial *serial = port->serial;
365 dbg("%s - port %d", __func__, port->number);
366 spin_lock_irqsave(&port->lock, flags);
367 if (serial->type->max_in_flight_urbs) {
368 if (port->urbs_in_flight < serial->type->max_in_flight_urbs)
369 room = port->bulk_out_size *
370 (serial->type->max_in_flight_urbs -
371 port->urbs_in_flight);
372 } else if (serial->num_bulk_out)
373 room = port->write_fifo->size - __kfifo_len(port->write_fifo);
374 spin_unlock_irqrestore(&port->lock, flags);
376 dbg("%s - returns %d", __func__, room);
380 int usb_serial_generic_chars_in_buffer(struct tty_struct *tty)
382 struct usb_serial_port *port = tty->driver_data;
383 struct usb_serial *serial = port->serial;
387 dbg("%s - port %d", __func__, port->number);
389 if (serial->type->max_in_flight_urbs) {
390 spin_lock_irqsave(&port->lock, flags);
391 chars = port->tx_bytes_flight;
392 spin_unlock_irqrestore(&port->lock, flags);
393 } else if (serial->num_bulk_out)
394 chars = kfifo_len(port->write_fifo);
396 dbg("%s - returns %d", __func__, chars);
401 void usb_serial_generic_resubmit_read_urb(struct usb_serial_port *port,
404 struct urb *urb = port->read_urb;
405 struct usb_serial *serial = port->serial;
408 /* Continue reading from device */
409 usb_fill_bulk_urb(urb, serial->dev,
410 usb_rcvbulkpipe(serial->dev,
411 port->bulk_in_endpointAddress),
412 urb->transfer_buffer,
413 urb->transfer_buffer_length,
414 ((serial->type->read_bulk_callback) ?
415 serial->type->read_bulk_callback :
416 usb_serial_generic_read_bulk_callback), port);
417 result = usb_submit_urb(urb, mem_flags);
420 "%s - failed resubmitting read urb, error %d\n",
423 EXPORT_SYMBOL_GPL(usb_serial_generic_resubmit_read_urb);
425 /* Push data to tty layer and resubmit the bulk read URB */
426 static void flush_and_resubmit_read_urb(struct usb_serial_port *port)
428 struct urb *urb = port->read_urb;
429 struct tty_struct *tty = tty_port_tty_get(&port->port);
430 char *ch = (char *)urb->transfer_buffer;
436 /* The per character mucking around with sysrq path it too slow for
437 stuff like 3G modems, so shortcircuit it in the 99.9999999% of cases
438 where the USB serial is not a console anyway */
439 if (!port->console || !port->sysrq)
440 tty_insert_flip_string(tty, ch, urb->actual_length);
442 /* Push data to tty */
443 for (i = 0; i < urb->actual_length; i++, ch++) {
444 if (!usb_serial_handle_sysrq_char(tty, port, *ch))
445 tty_insert_flip_char(tty, *ch, TTY_NORMAL);
448 tty_flip_buffer_push(tty);
451 usb_serial_generic_resubmit_read_urb(port, GFP_ATOMIC);
454 void usb_serial_generic_read_bulk_callback(struct urb *urb)
456 struct usb_serial_port *port = urb->context;
457 unsigned char *data = urb->transfer_buffer;
458 int status = urb->status;
461 dbg("%s - port %d", __func__, port->number);
463 if (unlikely(status != 0)) {
464 dbg("%s - nonzero read bulk status received: %d",
469 usb_serial_debug_data(debug, &port->dev, __func__,
470 urb->actual_length, data);
472 /* Throttle the device if requested by tty */
473 spin_lock_irqsave(&port->lock, flags);
474 port->throttled = port->throttle_req;
475 if (!port->throttled) {
476 spin_unlock_irqrestore(&port->lock, flags);
477 flush_and_resubmit_read_urb(port);
479 spin_unlock_irqrestore(&port->lock, flags);
481 EXPORT_SYMBOL_GPL(usb_serial_generic_read_bulk_callback);
483 void usb_serial_generic_write_bulk_callback(struct urb *urb)
486 struct usb_serial_port *port = urb->context;
487 int status = urb->status;
489 dbg("%s - port %d", __func__, port->number);
491 if (port->serial->type->max_in_flight_urbs) {
492 spin_lock_irqsave(&port->lock, flags);
493 --port->urbs_in_flight;
494 port->tx_bytes_flight -= urb->transfer_buffer_length;
495 if (port->urbs_in_flight < 0)
496 port->urbs_in_flight = 0;
497 spin_unlock_irqrestore(&port->lock, flags);
500 dbg("%s - nonzero multi-urb write bulk status "
501 "received: %d", __func__, status);
505 port->write_urb_busy = 0;
508 dbg("%s - nonzero multi-urb write bulk status "
509 "received: %d", __func__, status);
510 kfifo_reset(port->write_fifo);
512 usb_serial_generic_write_start(port);
515 usb_serial_port_softint(port);
517 EXPORT_SYMBOL_GPL(usb_serial_generic_write_bulk_callback);
519 void usb_serial_generic_throttle(struct tty_struct *tty)
521 struct usb_serial_port *port = tty->driver_data;
524 dbg("%s - port %d", __func__, port->number);
526 /* Set the throttle request flag. It will be picked up
527 * by usb_serial_generic_read_bulk_callback(). */
528 spin_lock_irqsave(&port->lock, flags);
529 port->throttle_req = 1;
530 spin_unlock_irqrestore(&port->lock, flags);
533 void usb_serial_generic_unthrottle(struct tty_struct *tty)
535 struct usb_serial_port *port = tty->driver_data;
539 dbg("%s - port %d", __func__, port->number);
541 /* Clear the throttle flags */
542 spin_lock_irqsave(&port->lock, flags);
543 was_throttled = port->throttled;
544 port->throttled = port->throttle_req = 0;
545 spin_unlock_irqrestore(&port->lock, flags);
548 /* Resume reading from device */
549 usb_serial_generic_resubmit_read_urb(port, GFP_KERNEL);
553 int usb_serial_handle_sysrq_char(struct tty_struct *tty,
554 struct usb_serial_port *port, unsigned int ch)
556 if (port->sysrq && port->console) {
557 if (ch && time_before(jiffies, port->sysrq)) {
558 handle_sysrq(ch, tty);
566 EXPORT_SYMBOL_GPL(usb_serial_handle_sysrq_char);
568 int usb_serial_handle_break(struct usb_serial_port *port)
571 port->sysrq = jiffies + HZ*5;
577 EXPORT_SYMBOL_GPL(usb_serial_handle_break);
579 int usb_serial_generic_resume(struct usb_serial *serial)
581 struct usb_serial_port *port;
584 for (i = 0; i < serial->num_ports; i++) {
585 port = serial->port[i];
586 if (!port->port.count)
589 if (port->read_urb) {
590 r = usb_submit_urb(port->read_urb, GFP_NOIO);
595 if (port->write_urb) {
596 r = usb_serial_generic_write_start(port);
604 EXPORT_SYMBOL_GPL(usb_serial_generic_resume);
606 void usb_serial_generic_disconnect(struct usb_serial *serial)
612 /* stop reads and writes on all ports */
613 for (i = 0; i < serial->num_ports; ++i)
614 generic_cleanup(serial->port[i]);
617 void usb_serial_generic_release(struct usb_serial *serial)