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,
118 struct usb_serial_port *port, struct file *filp)
120 struct usb_serial *serial = port->serial;
124 dbg("%s - port %d", __func__, port->number);
126 /* clear the throttle flags */
127 spin_lock_irqsave(&port->lock, flags);
129 port->throttle_req = 0;
130 spin_unlock_irqrestore(&port->lock, flags);
132 /* if we have a bulk endpoint, start reading from it */
133 if (serial->num_bulk_in) {
134 /* Start reading from the device */
135 usb_fill_bulk_urb(port->read_urb, serial->dev,
136 usb_rcvbulkpipe(serial->dev,
137 port->bulk_in_endpointAddress),
138 port->read_urb->transfer_buffer,
139 port->read_urb->transfer_buffer_length,
140 ((serial->type->read_bulk_callback) ?
141 serial->type->read_bulk_callback :
142 usb_serial_generic_read_bulk_callback),
144 result = usb_submit_urb(port->read_urb, GFP_KERNEL);
147 "%s - failed resubmitting read urb, error %d\n",
153 EXPORT_SYMBOL_GPL(usb_serial_generic_open);
155 static void generic_cleanup(struct usb_serial_port *port)
157 struct usb_serial *serial = port->serial;
159 dbg("%s - port %d", __func__, port->number);
162 /* shutdown any bulk reads that might be going on */
163 if (serial->num_bulk_out)
164 usb_kill_urb(port->write_urb);
165 if (serial->num_bulk_in)
166 usb_kill_urb(port->read_urb);
170 int usb_serial_generic_resume(struct usb_serial *serial)
172 struct usb_serial_port *port;
175 for (i = 0; i < serial->num_ports; i++) {
176 port = serial->port[i];
177 if (port->port.count && port->read_urb) {
178 r = usb_submit_urb(port->read_urb, GFP_NOIO);
186 EXPORT_SYMBOL_GPL(usb_serial_generic_resume);
188 void usb_serial_generic_close(struct usb_serial_port *port)
190 dbg("%s - port %d", __func__, port->number);
191 generic_cleanup(port);
194 static int usb_serial_multi_urb_write(struct tty_struct *tty,
195 struct usb_serial_port *port, const unsigned char *buf, int count)
199 unsigned char *buffer;
204 dbg("%s - port %d", __func__, port->number);
207 dbg("%s - write request of 0 bytes", __func__);
210 towrite = (count > port->bulk_out_size) ?
211 port->bulk_out_size : count;
212 spin_lock_irqsave(&port->lock, flags);
213 if (port->urbs_in_flight >
214 port->serial->type->max_in_flight_urbs) {
215 spin_unlock_irqrestore(&port->lock, flags);
216 dbg("%s - write limit hit\n", __func__);
219 port->tx_bytes_flight += towrite;
220 port->urbs_in_flight++;
221 spin_unlock_irqrestore(&port->lock, flags);
223 buffer = kmalloc(towrite, GFP_ATOMIC);
226 "%s ran out of kernel memory for urb ...\n", __func__);
227 goto error_no_buffer;
230 urb = usb_alloc_urb(0, GFP_ATOMIC);
232 dev_err(&port->dev, "%s - no more free urbs\n",
238 memcpy(buffer, buf + bwrite, towrite);
239 usb_serial_debug_data(debug, &port->dev, __func__,
241 /* fill the buffer and send it */
242 usb_fill_bulk_urb(urb, port->serial->dev,
243 usb_sndbulkpipe(port->serial->dev,
244 port->bulk_out_endpointAddress),
246 usb_serial_generic_write_bulk_callback, port);
248 status = usb_submit_urb(urb, GFP_ATOMIC);
251 "%s - failed submitting write urb, error %d\n",
256 /* This urb is the responsibility of the host driver now */
258 dbg("%s write: %d", __func__, towrite);
269 spin_lock_irqsave(&port->lock, flags);
270 port->urbs_in_flight--;
271 port->tx_bytes_flight -= towrite;
272 spin_unlock_irqrestore(&port->lock, flags);
276 int usb_serial_generic_write(struct tty_struct *tty,
277 struct usb_serial_port *port, const unsigned char *buf, int count)
279 struct usb_serial *serial = port->serial;
283 dbg("%s - port %d", __func__, port->number);
286 dbg("%s - write request of 0 bytes", __func__);
290 /* only do something if we have a bulk out endpoint */
291 if (serial->num_bulk_out) {
294 if (serial->type->max_in_flight_urbs)
295 return usb_serial_multi_urb_write(tty, port,
298 spin_lock_irqsave(&port->lock, flags);
299 if (port->write_urb_busy) {
300 spin_unlock_irqrestore(&port->lock, flags);
301 dbg("%s - already writing", __func__);
304 port->write_urb_busy = 1;
305 spin_unlock_irqrestore(&port->lock, flags);
307 count = (count > port->bulk_out_size) ?
308 port->bulk_out_size : count;
310 memcpy(port->write_urb->transfer_buffer, buf, count);
311 data = port->write_urb->transfer_buffer;
312 usb_serial_debug_data(debug, &port->dev, __func__, count, data);
315 usb_fill_bulk_urb(port->write_urb, serial->dev,
316 usb_sndbulkpipe(serial->dev,
317 port->bulk_out_endpointAddress),
318 port->write_urb->transfer_buffer, count,
319 ((serial->type->write_bulk_callback) ?
320 serial->type->write_bulk_callback :
321 usb_serial_generic_write_bulk_callback),
324 /* send the data out the bulk port */
325 port->write_urb_busy = 1;
326 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
329 "%s - failed submitting write urb, error %d\n",
331 /* don't have to grab the lock here, as we will
333 port->write_urb_busy = 0;
340 /* no bulk out, so return 0 bytes written */
343 EXPORT_SYMBOL_GPL(usb_serial_generic_write);
345 int usb_serial_generic_write_room(struct tty_struct *tty)
347 struct usb_serial_port *port = tty->driver_data;
348 struct usb_serial *serial = port->serial;
352 dbg("%s - port %d", __func__, port->number);
353 spin_lock_irqsave(&port->lock, flags);
354 if (serial->type->max_in_flight_urbs) {
355 if (port->urbs_in_flight < serial->type->max_in_flight_urbs)
356 room = port->bulk_out_size *
357 (serial->type->max_in_flight_urbs -
358 port->urbs_in_flight);
359 } else if (serial->num_bulk_out && !(port->write_urb_busy)) {
360 room = port->bulk_out_size;
362 spin_unlock_irqrestore(&port->lock, flags);
364 dbg("%s - returns %d", __func__, room);
368 int usb_serial_generic_chars_in_buffer(struct tty_struct *tty)
370 struct usb_serial_port *port = tty->driver_data;
371 struct usb_serial *serial = port->serial;
375 dbg("%s - port %d", __func__, port->number);
377 if (serial->type->max_in_flight_urbs) {
378 spin_lock_irqsave(&port->lock, flags);
379 chars = port->tx_bytes_flight;
380 spin_unlock_irqrestore(&port->lock, flags);
381 } else if (serial->num_bulk_out) {
383 if (port->write_urb_busy)
384 chars = port->write_urb->transfer_buffer_length;
387 dbg("%s - returns %d", __func__, chars);
392 void usb_serial_generic_resubmit_read_urb(struct usb_serial_port *port,
395 struct urb *urb = port->read_urb;
396 struct usb_serial *serial = port->serial;
399 /* Continue reading from device */
400 usb_fill_bulk_urb(urb, serial->dev,
401 usb_rcvbulkpipe(serial->dev,
402 port->bulk_in_endpointAddress),
403 urb->transfer_buffer,
404 urb->transfer_buffer_length,
405 ((serial->type->read_bulk_callback) ?
406 serial->type->read_bulk_callback :
407 usb_serial_generic_read_bulk_callback), port);
408 result = usb_submit_urb(urb, mem_flags);
411 "%s - failed resubmitting read urb, error %d\n",
414 EXPORT_SYMBOL_GPL(usb_serial_generic_resubmit_read_urb);
416 /* Push data to tty layer and resubmit the bulk read URB */
417 static void flush_and_resubmit_read_urb(struct usb_serial_port *port)
419 struct urb *urb = port->read_urb;
420 struct tty_struct *tty = tty_port_tty_get(&port->port);
421 char *ch = (char *)urb->transfer_buffer;
427 /* Push data to tty */
428 for (i = 0; i < urb->actual_length; i++, ch++) {
429 if (!usb_serial_handle_sysrq_char(port, *ch))
430 tty_insert_flip_char(tty, *ch, TTY_NORMAL);
432 tty_flip_buffer_push(tty);
435 usb_serial_generic_resubmit_read_urb(port, GFP_ATOMIC);
438 void usb_serial_generic_read_bulk_callback(struct urb *urb)
440 struct usb_serial_port *port = urb->context;
441 unsigned char *data = urb->transfer_buffer;
442 int status = urb->status;
445 dbg("%s - port %d", __func__, port->number);
447 if (unlikely(status != 0)) {
448 dbg("%s - nonzero read bulk status received: %d",
453 usb_serial_debug_data(debug, &port->dev, __func__,
454 urb->actual_length, data);
456 /* Throttle the device if requested by tty */
457 spin_lock_irqsave(&port->lock, flags);
458 port->throttled = port->throttle_req;
459 if (!port->throttled) {
460 spin_unlock_irqrestore(&port->lock, flags);
461 flush_and_resubmit_read_urb(port);
463 spin_unlock_irqrestore(&port->lock, flags);
465 EXPORT_SYMBOL_GPL(usb_serial_generic_read_bulk_callback);
467 void usb_serial_generic_write_bulk_callback(struct urb *urb)
470 struct usb_serial_port *port = urb->context;
471 int status = urb->status;
473 dbg("%s - port %d", __func__, port->number);
475 if (port->serial->type->max_in_flight_urbs) {
476 spin_lock_irqsave(&port->lock, flags);
477 --port->urbs_in_flight;
478 port->tx_bytes_flight -= urb->transfer_buffer_length;
479 if (port->urbs_in_flight < 0)
480 port->urbs_in_flight = 0;
481 spin_unlock_irqrestore(&port->lock, flags);
483 /* Handle the case for single urb mode */
484 port->write_urb_busy = 0;
488 dbg("%s - nonzero write bulk status received: %d",
492 usb_serial_port_softint(port);
494 EXPORT_SYMBOL_GPL(usb_serial_generic_write_bulk_callback);
496 void usb_serial_generic_throttle(struct tty_struct *tty)
498 struct usb_serial_port *port = tty->driver_data;
501 dbg("%s - port %d", __func__, port->number);
503 /* Set the throttle request flag. It will be picked up
504 * by usb_serial_generic_read_bulk_callback(). */
505 spin_lock_irqsave(&port->lock, flags);
506 port->throttle_req = 1;
507 spin_unlock_irqrestore(&port->lock, flags);
510 void usb_serial_generic_unthrottle(struct tty_struct *tty)
512 struct usb_serial_port *port = tty->driver_data;
516 dbg("%s - port %d", __func__, port->number);
518 /* Clear the throttle flags */
519 spin_lock_irqsave(&port->lock, flags);
520 was_throttled = port->throttled;
521 port->throttled = port->throttle_req = 0;
522 spin_unlock_irqrestore(&port->lock, flags);
525 /* Resume reading from device */
526 usb_serial_generic_resubmit_read_urb(port, GFP_KERNEL);
530 int usb_serial_handle_sysrq_char(struct usb_serial_port *port, unsigned int ch)
532 if (port->sysrq && port->console) {
533 if (ch && time_before(jiffies, port->sysrq)) {
534 handle_sysrq(ch, tty_port_tty_get(&port->port));
542 EXPORT_SYMBOL_GPL(usb_serial_handle_sysrq_char);
544 int usb_serial_handle_break(struct usb_serial_port *port)
547 port->sysrq = jiffies + HZ*5;
553 EXPORT_SYMBOL_GPL(usb_serial_handle_break);
555 void usb_serial_generic_disconnect(struct usb_serial *serial)
561 /* stop reads and writes on all ports */
562 for (i = 0; i < serial->num_ports; ++i)
563 generic_cleanup(serial->port[i]);
566 void usb_serial_generic_release(struct usb_serial *serial)