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)
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 (port->bulk_in_size)
134 result = usb_serial_generic_submit_read_urb(port, GFP_KERNEL);
138 EXPORT_SYMBOL_GPL(usb_serial_generic_open);
140 static void generic_cleanup(struct usb_serial_port *port)
142 struct usb_serial *serial = port->serial;
145 dbg("%s - port %d", __func__, port->number);
148 /* shutdown any bulk transfers that might be going on */
149 if (port->bulk_out_size) {
150 usb_kill_urb(port->write_urb);
152 spin_lock_irqsave(&port->lock, flags);
153 kfifo_reset_out(&port->write_fifo);
154 spin_unlock_irqrestore(&port->lock, flags);
156 if (port->bulk_in_size)
157 usb_kill_urb(port->read_urb);
161 void usb_serial_generic_close(struct usb_serial_port *port)
163 dbg("%s - port %d", __func__, port->number);
164 generic_cleanup(port);
166 EXPORT_SYMBOL_GPL(usb_serial_generic_close);
168 static int usb_serial_multi_urb_write(struct tty_struct *tty,
169 struct usb_serial_port *port, const unsigned char *buf, int count)
173 unsigned char *buffer;
178 dbg("%s - port %d", __func__, port->number);
181 dbg("%s - write request of 0 bytes", __func__);
184 towrite = (count > port->bulk_out_size) ?
185 port->bulk_out_size : count;
186 spin_lock_irqsave(&port->lock, flags);
187 if (port->urbs_in_flight >
188 port->serial->type->max_in_flight_urbs) {
189 spin_unlock_irqrestore(&port->lock, flags);
190 dbg("%s - write limit hit", __func__);
193 port->tx_bytes_flight += towrite;
194 port->urbs_in_flight++;
195 spin_unlock_irqrestore(&port->lock, flags);
197 buffer = kmalloc(towrite, GFP_ATOMIC);
200 "%s ran out of kernel memory for urb ...\n", __func__);
201 goto error_no_buffer;
204 urb = usb_alloc_urb(0, GFP_ATOMIC);
206 dev_err(&port->dev, "%s - no more free urbs\n",
212 memcpy(buffer, buf + bwrite, towrite);
213 usb_serial_debug_data(debug, &port->dev, __func__,
215 /* fill the buffer and send it */
216 usb_fill_bulk_urb(urb, port->serial->dev,
217 usb_sndbulkpipe(port->serial->dev,
218 port->bulk_out_endpointAddress),
220 usb_serial_generic_write_bulk_callback, port);
222 status = usb_submit_urb(urb, GFP_ATOMIC);
225 "%s - failed submitting write urb, error %d\n",
230 /* This urb is the responsibility of the host driver now */
232 dbg("%s write: %d", __func__, towrite);
243 spin_lock_irqsave(&port->lock, flags);
244 port->urbs_in_flight--;
245 port->tx_bytes_flight -= towrite;
246 spin_unlock_irqrestore(&port->lock, flags);
251 * usb_serial_generic_write_start - kick off an URB write
252 * @port: Pointer to the &struct usb_serial_port data
254 * Returns the number of bytes queued on success. This will be zero if there
255 * was nothing to send. Otherwise, it returns a negative errno value
257 static int usb_serial_generic_write_start(struct usb_serial_port *port)
265 /* Atomically determine whether we can and need to start a USB
267 spin_lock_irqsave(&port->lock, flags);
268 if (port->write_urb_busy)
271 start_io = (kfifo_len(&port->write_fifo) != 0);
272 port->write_urb_busy = start_io;
274 spin_unlock_irqrestore(&port->lock, flags);
279 data = port->write_urb->transfer_buffer;
280 count = kfifo_out_locked(&port->write_fifo, data, port->bulk_out_size, &port->lock);
281 usb_serial_debug_data(debug, &port->dev, __func__, count, data);
283 port->write_urb->transfer_buffer_length = count;
285 /* send the data out the bulk port */
286 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
289 "%s - failed submitting write urb, error %d\n",
291 /* don't have to grab the lock here, as we will
293 port->write_urb_busy = 0;
297 spin_lock_irqsave(&port->lock, flags);
298 port->tx_bytes_flight += count;
299 spin_unlock_irqrestore(&port->lock, flags);
305 * usb_serial_generic_write - generic write function for serial USB devices
306 * @tty: Pointer to &struct tty_struct for the device
307 * @port: Pointer to the &usb_serial_port structure for the device
308 * @buf: Pointer to the data to write
309 * @count: Number of bytes to write
311 * Returns the number of characters actually written, which may be anything
312 * from zero to @count. If an error occurs, it returns the negative errno
315 int usb_serial_generic_write(struct tty_struct *tty,
316 struct usb_serial_port *port, const unsigned char *buf, int count)
318 struct usb_serial *serial = port->serial;
321 dbg("%s - port %d", __func__, port->number);
323 /* only do something if we have a bulk out endpoint */
324 if (!port->bulk_out_size)
328 dbg("%s - write request of 0 bytes", __func__);
332 if (serial->type->max_in_flight_urbs)
333 return usb_serial_multi_urb_write(tty, port,
336 count = kfifo_in_locked(&port->write_fifo, buf, count, &port->lock);
337 result = usb_serial_generic_write_start(port);
344 EXPORT_SYMBOL_GPL(usb_serial_generic_write);
346 int usb_serial_generic_write_room(struct tty_struct *tty)
348 struct usb_serial_port *port = tty->driver_data;
349 struct usb_serial *serial = port->serial;
353 dbg("%s - port %d", __func__, port->number);
355 if (!port->bulk_out_size)
358 spin_lock_irqsave(&port->lock, flags);
359 if (serial->type->max_in_flight_urbs) {
360 if (port->urbs_in_flight < serial->type->max_in_flight_urbs)
361 room = port->bulk_out_size *
362 (serial->type->max_in_flight_urbs -
363 port->urbs_in_flight);
365 room = kfifo_avail(&port->write_fifo);
367 spin_unlock_irqrestore(&port->lock, flags);
369 dbg("%s - returns %d", __func__, room);
373 int usb_serial_generic_chars_in_buffer(struct tty_struct *tty)
375 struct usb_serial_port *port = tty->driver_data;
376 struct usb_serial *serial = port->serial;
380 dbg("%s - port %d", __func__, port->number);
382 if (!port->bulk_out_size)
385 spin_lock_irqsave(&port->lock, flags);
386 if (serial->type->max_in_flight_urbs)
387 chars = port->tx_bytes_flight;
389 chars = kfifo_len(&port->write_fifo) + port->tx_bytes_flight;
390 spin_unlock_irqrestore(&port->lock, flags);
392 dbg("%s - returns %d", __func__, chars);
396 int usb_serial_generic_submit_read_urb(struct usb_serial_port *port,
401 result = usb_submit_urb(port->read_urb, mem_flags);
402 if (result && result != -EPERM) {
404 "%s - failed submitting read urb, error %d\n",
409 EXPORT_SYMBOL_GPL(usb_serial_generic_submit_read_urb);
411 void usb_serial_generic_process_read_urb(struct urb *urb)
413 struct usb_serial_port *port = urb->context;
414 struct tty_struct *tty;
415 char *ch = (char *)urb->transfer_buffer;
418 tty = tty_port_tty_get(&port->port);
422 /* The per character mucking around with sysrq path it too slow for
423 stuff like 3G modems, so shortcircuit it in the 99.9999999% of cases
424 where the USB serial is not a console anyway */
425 if (!port->port.console || !port->sysrq)
426 tty_insert_flip_string(tty, ch, urb->actual_length);
428 for (i = 0; i < urb->actual_length; i++, ch++) {
429 if (!usb_serial_handle_sysrq_char(tty, port, *ch))
430 tty_insert_flip_char(tty, *ch, TTY_NORMAL);
433 tty_flip_buffer_push(tty);
436 EXPORT_SYMBOL_GPL(usb_serial_generic_process_read_urb);
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);
455 port->serial->type->process_read_urb(urb);
457 /* Throttle the device if requested by tty */
458 spin_lock_irqsave(&port->lock, flags);
459 port->throttled = port->throttle_req;
460 if (!port->throttled) {
461 spin_unlock_irqrestore(&port->lock, flags);
462 usb_serial_generic_submit_read_urb(port, GFP_ATOMIC);
464 spin_unlock_irqrestore(&port->lock, flags);
466 EXPORT_SYMBOL_GPL(usb_serial_generic_read_bulk_callback);
468 void usb_serial_generic_write_bulk_callback(struct urb *urb)
471 struct usb_serial_port *port = urb->context;
472 int status = urb->status;
474 dbg("%s - port %d", __func__, port->number);
476 if (port->serial->type->max_in_flight_urbs) {
477 kfree(urb->transfer_buffer);
479 spin_lock_irqsave(&port->lock, flags);
480 --port->urbs_in_flight;
481 port->tx_bytes_flight -= urb->transfer_buffer_length;
482 if (port->urbs_in_flight < 0)
483 port->urbs_in_flight = 0;
484 spin_unlock_irqrestore(&port->lock, flags);
486 spin_lock_irqsave(&port->lock, flags);
487 port->tx_bytes_flight -= urb->transfer_buffer_length;
488 port->write_urb_busy = 0;
489 spin_unlock_irqrestore(&port->lock, flags);
492 spin_lock_irqsave(&port->lock, flags);
493 kfifo_reset_out(&port->write_fifo);
494 spin_unlock_irqrestore(&port->lock, flags);
496 usb_serial_generic_write_start(port);
501 dbg("%s - non-zero urb status: %d", __func__, status);
503 usb_serial_port_softint(port);
505 EXPORT_SYMBOL_GPL(usb_serial_generic_write_bulk_callback);
507 void usb_serial_generic_throttle(struct tty_struct *tty)
509 struct usb_serial_port *port = tty->driver_data;
512 dbg("%s - port %d", __func__, port->number);
514 /* Set the throttle request flag. It will be picked up
515 * by usb_serial_generic_read_bulk_callback(). */
516 spin_lock_irqsave(&port->lock, flags);
517 port->throttle_req = 1;
518 spin_unlock_irqrestore(&port->lock, flags);
521 void usb_serial_generic_unthrottle(struct tty_struct *tty)
523 struct usb_serial_port *port = tty->driver_data;
526 dbg("%s - port %d", __func__, port->number);
528 /* Clear the throttle flags */
529 spin_lock_irq(&port->lock);
530 was_throttled = port->throttled;
531 port->throttled = port->throttle_req = 0;
532 spin_unlock_irq(&port->lock);
535 usb_serial_generic_submit_read_urb(port, GFP_KERNEL);
538 #ifdef CONFIG_MAGIC_SYSRQ
539 int usb_serial_handle_sysrq_char(struct tty_struct *tty,
540 struct usb_serial_port *port, unsigned int ch)
542 if (port->sysrq && port->port.console) {
543 if (ch && time_before(jiffies, port->sysrq)) {
544 handle_sysrq(ch, tty);
553 int usb_serial_handle_sysrq_char(struct tty_struct *tty,
554 struct usb_serial_port *port, unsigned int ch)
559 EXPORT_SYMBOL_GPL(usb_serial_handle_sysrq_char);
561 int usb_serial_handle_break(struct usb_serial_port *port)
564 port->sysrq = jiffies + HZ*5;
570 EXPORT_SYMBOL_GPL(usb_serial_handle_break);
572 int usb_serial_generic_resume(struct usb_serial *serial)
574 struct usb_serial_port *port;
577 for (i = 0; i < serial->num_ports; i++) {
578 port = serial->port[i];
579 if (!test_bit(ASYNCB_INITIALIZED, &port->port.flags))
582 if (port->read_urb) {
583 r = usb_submit_urb(port->read_urb, GFP_NOIO);
588 if (port->write_urb) {
589 r = usb_serial_generic_write_start(port);
597 EXPORT_SYMBOL_GPL(usb_serial_generic_resume);
599 void usb_serial_generic_disconnect(struct usb_serial *serial)
605 /* stop reads and writes on all ports */
606 for (i = 0; i < serial->num_ports; ++i)
607 generic_cleanup(serial->port[i]);
610 void usb_serial_generic_release(struct usb_serial *serial)