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/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>
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 int usb_serial_generic_resume(struct usb_serial *serial)
171 struct usb_serial_port *port;
174 for (i = 0; i < serial->num_ports; i++) {
175 port = serial->port[i];
176 if (port->port.count && port->read_urb) {
177 r = usb_submit_urb(port->read_urb, GFP_NOIO);
185 EXPORT_SYMBOL_GPL(usb_serial_generic_resume);
187 void usb_serial_generic_close(struct usb_serial_port *port)
189 dbg("%s - port %d", __func__, port->number);
190 generic_cleanup(port);
193 static int usb_serial_multi_urb_write(struct tty_struct *tty,
194 struct usb_serial_port *port, const unsigned char *buf, int count)
198 unsigned char *buffer;
203 dbg("%s - port %d", __func__, port->number);
206 dbg("%s - write request of 0 bytes", __func__);
209 towrite = (count > port->bulk_out_size) ?
210 port->bulk_out_size : count;
211 spin_lock_irqsave(&port->lock, flags);
212 if (port->urbs_in_flight >
213 port->serial->type->max_in_flight_urbs) {
214 spin_unlock_irqrestore(&port->lock, flags);
215 dbg("%s - write limit hit\n", __func__);
218 port->tx_bytes_flight += towrite;
219 port->urbs_in_flight++;
220 spin_unlock_irqrestore(&port->lock, flags);
222 buffer = kmalloc(towrite, GFP_ATOMIC);
225 "%s ran out of kernel memory for urb ...\n", __func__);
226 goto error_no_buffer;
229 urb = usb_alloc_urb(0, GFP_ATOMIC);
231 dev_err(&port->dev, "%s - no more free urbs\n",
237 memcpy(buffer, buf + bwrite, towrite);
238 usb_serial_debug_data(debug, &port->dev, __func__,
240 /* fill the buffer and send it */
241 usb_fill_bulk_urb(urb, port->serial->dev,
242 usb_sndbulkpipe(port->serial->dev,
243 port->bulk_out_endpointAddress),
245 usb_serial_generic_write_bulk_callback, port);
247 status = usb_submit_urb(urb, GFP_ATOMIC);
250 "%s - failed submitting write urb, error %d\n",
255 /* This urb is the responsibility of the host driver now */
257 dbg("%s write: %d", __func__, towrite);
268 spin_lock_irqsave(&port->lock, flags);
269 port->urbs_in_flight--;
270 port->tx_bytes_flight -= towrite;
271 spin_unlock_irqrestore(&port->lock, flags);
275 int usb_serial_generic_write(struct tty_struct *tty,
276 struct usb_serial_port *port, const unsigned char *buf, int count)
278 struct usb_serial *serial = port->serial;
282 dbg("%s - port %d", __func__, port->number);
285 dbg("%s - write request of 0 bytes", __func__);
289 /* only do something if we have a bulk out endpoint */
290 if (serial->num_bulk_out) {
293 if (serial->type->max_in_flight_urbs)
294 return usb_serial_multi_urb_write(tty, port,
297 spin_lock_irqsave(&port->lock, flags);
298 if (port->write_urb_busy) {
299 spin_unlock_irqrestore(&port->lock, flags);
300 dbg("%s - already writing", __func__);
303 port->write_urb_busy = 1;
304 spin_unlock_irqrestore(&port->lock, flags);
306 count = (count > port->bulk_out_size) ?
307 port->bulk_out_size : count;
309 memcpy(port->write_urb->transfer_buffer, buf, count);
310 data = port->write_urb->transfer_buffer;
311 usb_serial_debug_data(debug, &port->dev, __func__, count, data);
314 usb_fill_bulk_urb(port->write_urb, serial->dev,
315 usb_sndbulkpipe(serial->dev,
316 port->bulk_out_endpointAddress),
317 port->write_urb->transfer_buffer, count,
318 ((serial->type->write_bulk_callback) ?
319 serial->type->write_bulk_callback :
320 usb_serial_generic_write_bulk_callback),
323 /* send the data out the bulk port */
324 port->write_urb_busy = 1;
325 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
328 "%s - failed submitting write urb, error %d\n",
330 /* don't have to grab the lock here, as we will
332 port->write_urb_busy = 0;
339 /* no bulk out, so return 0 bytes written */
342 EXPORT_SYMBOL_GPL(usb_serial_generic_write);
344 int usb_serial_generic_write_room(struct tty_struct *tty)
346 struct usb_serial_port *port = tty->driver_data;
347 struct usb_serial *serial = port->serial;
351 dbg("%s - port %d", __func__, port->number);
352 spin_lock_irqsave(&port->lock, flags);
353 if (serial->type->max_in_flight_urbs) {
354 if (port->urbs_in_flight < serial->type->max_in_flight_urbs)
355 room = port->bulk_out_size *
356 (serial->type->max_in_flight_urbs -
357 port->urbs_in_flight);
358 } else if (serial->num_bulk_out && !(port->write_urb_busy)) {
359 room = port->bulk_out_size;
361 spin_unlock_irqrestore(&port->lock, flags);
363 dbg("%s - returns %d", __func__, room);
367 int usb_serial_generic_chars_in_buffer(struct tty_struct *tty)
369 struct usb_serial_port *port = tty->driver_data;
370 struct usb_serial *serial = port->serial;
374 dbg("%s - port %d", __func__, port->number);
376 if (serial->type->max_in_flight_urbs) {
377 spin_lock_irqsave(&port->lock, flags);
378 chars = port->tx_bytes_flight;
379 spin_unlock_irqrestore(&port->lock, flags);
380 } else if (serial->num_bulk_out) {
382 if (port->write_urb_busy)
383 chars = port->write_urb->transfer_buffer_length;
386 dbg("%s - returns %d", __func__, chars);
391 void usb_serial_generic_resubmit_read_urb(struct usb_serial_port *port,
394 struct urb *urb = port->read_urb;
395 struct usb_serial *serial = port->serial;
398 /* Continue reading from device */
399 usb_fill_bulk_urb(urb, serial->dev,
400 usb_rcvbulkpipe(serial->dev,
401 port->bulk_in_endpointAddress),
402 urb->transfer_buffer,
403 urb->transfer_buffer_length,
404 ((serial->type->read_bulk_callback) ?
405 serial->type->read_bulk_callback :
406 usb_serial_generic_read_bulk_callback), port);
407 result = usb_submit_urb(urb, mem_flags);
410 "%s - failed resubmitting read urb, error %d\n",
413 EXPORT_SYMBOL_GPL(usb_serial_generic_resubmit_read_urb);
415 /* Push data to tty layer and resubmit the bulk read URB */
416 static void flush_and_resubmit_read_urb(struct usb_serial_port *port)
418 struct urb *urb = port->read_urb;
419 struct tty_struct *tty = tty_port_tty_get(&port->port);
420 char *ch = (char *)urb->transfer_buffer;
426 /* The per character mucking around with sysrq path it too slow for
427 stuff like 3G modems, so shortcircuit it in the 99.9999999% of cases
428 where the USB serial is not a console anyway */
429 if (!port->console || !port->sysrq)
430 tty_insert_flip_string(tty, ch, urb->actual_length);
432 /* Push data to tty */
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 usb_serial_generic_resubmit_read_urb(port, GFP_ATOMIC);
444 void usb_serial_generic_read_bulk_callback(struct urb *urb)
446 struct usb_serial_port *port = urb->context;
447 unsigned char *data = urb->transfer_buffer;
448 int status = urb->status;
451 dbg("%s - port %d", __func__, port->number);
453 if (unlikely(status != 0)) {
454 dbg("%s - nonzero read bulk status received: %d",
459 usb_serial_debug_data(debug, &port->dev, __func__,
460 urb->actual_length, data);
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 flush_and_resubmit_read_urb(port);
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;
479 dbg("%s - port %d", __func__, port->number);
481 if (port->serial->type->max_in_flight_urbs) {
482 spin_lock_irqsave(&port->lock, flags);
483 --port->urbs_in_flight;
484 port->tx_bytes_flight -= urb->transfer_buffer_length;
485 if (port->urbs_in_flight < 0)
486 port->urbs_in_flight = 0;
487 spin_unlock_irqrestore(&port->lock, flags);
489 /* Handle the case for single urb mode */
490 port->write_urb_busy = 0;
494 dbg("%s - nonzero write bulk status received: %d",
498 usb_serial_port_softint(port);
500 EXPORT_SYMBOL_GPL(usb_serial_generic_write_bulk_callback);
502 void usb_serial_generic_throttle(struct tty_struct *tty)
504 struct usb_serial_port *port = tty->driver_data;
507 dbg("%s - port %d", __func__, port->number);
509 /* Set the throttle request flag. It will be picked up
510 * by usb_serial_generic_read_bulk_callback(). */
511 spin_lock_irqsave(&port->lock, flags);
512 port->throttle_req = 1;
513 spin_unlock_irqrestore(&port->lock, flags);
516 void usb_serial_generic_unthrottle(struct tty_struct *tty)
518 struct usb_serial_port *port = tty->driver_data;
522 dbg("%s - port %d", __func__, port->number);
524 /* Clear the throttle flags */
525 spin_lock_irqsave(&port->lock, flags);
526 was_throttled = port->throttled;
527 port->throttled = port->throttle_req = 0;
528 spin_unlock_irqrestore(&port->lock, flags);
531 /* Resume reading from device */
532 usb_serial_generic_resubmit_read_urb(port, GFP_KERNEL);
536 int usb_serial_handle_sysrq_char(struct tty_struct *tty,
537 struct usb_serial_port *port, unsigned int ch)
539 if (port->sysrq && port->console) {
540 if (ch && time_before(jiffies, port->sysrq)) {
541 handle_sysrq(ch, tty);
549 EXPORT_SYMBOL_GPL(usb_serial_handle_sysrq_char);
551 int usb_serial_handle_break(struct usb_serial_port *port)
554 port->sysrq = jiffies + HZ*5;
560 EXPORT_SYMBOL_GPL(usb_serial_handle_break);
562 void usb_serial_generic_disconnect(struct usb_serial *serial)
568 /* stop reads and writes on all ports */
569 for (i = 0; i < serial->num_ports; ++i)
570 generic_cleanup(serial->port[i]);
573 void usb_serial_generic_release(struct usb_serial *serial)