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>
22 #include <linux/kfifo.h>
23 #include <linux/serial.h>
27 #ifdef CONFIG_USB_SERIAL_GENERIC
29 static int generic_probe(struct usb_interface *interface,
30 const struct usb_device_id *id);
32 static __u16 vendor = 0x05f9;
33 static __u16 product = 0xffff;
35 module_param(vendor, ushort, 0);
36 MODULE_PARM_DESC(vendor, "User specified USB idVendor");
38 module_param(product, ushort, 0);
39 MODULE_PARM_DESC(product, "User specified USB idProduct");
41 static struct usb_device_id generic_device_ids[2]; /* Initially all zeroes. */
43 /* we want to look at all devices, as the vendor/product id can change
44 * depending on the command line argument */
45 static const struct usb_device_id generic_serial_ids[] = {
50 static struct usb_driver generic_driver = {
51 .name = "usbserial_generic",
52 .probe = generic_probe,
53 .disconnect = usb_serial_disconnect,
54 .id_table = generic_serial_ids,
58 /* All of the device info needed for the Generic Serial Converter */
59 struct usb_serial_driver usb_serial_generic_device = {
64 .id_table = generic_device_ids,
65 .usb_driver = &generic_driver,
67 .disconnect = usb_serial_generic_disconnect,
68 .release = usb_serial_generic_release,
69 .throttle = usb_serial_generic_throttle,
70 .unthrottle = usb_serial_generic_unthrottle,
71 .resume = usb_serial_generic_resume,
74 static int generic_probe(struct usb_interface *interface,
75 const struct usb_device_id *id)
77 const struct usb_device_id *id_pattern;
79 id_pattern = usb_match_id(interface, generic_device_ids);
80 if (id_pattern != NULL)
81 return usb_serial_probe(interface, id);
86 int usb_serial_generic_register(int _debug)
91 #ifdef CONFIG_USB_SERIAL_GENERIC
92 generic_device_ids[0].idVendor = vendor;
93 generic_device_ids[0].idProduct = product;
94 generic_device_ids[0].match_flags =
95 USB_DEVICE_ID_MATCH_VENDOR | USB_DEVICE_ID_MATCH_PRODUCT;
97 /* register our generic driver with ourselves */
98 retval = usb_serial_register(&usb_serial_generic_device);
101 retval = usb_register(&generic_driver);
103 usb_serial_deregister(&usb_serial_generic_device);
109 void usb_serial_generic_deregister(void)
111 #ifdef CONFIG_USB_SERIAL_GENERIC
112 /* remove our generic driver */
113 usb_deregister(&generic_driver);
114 usb_serial_deregister(&usb_serial_generic_device);
118 int usb_serial_generic_open(struct tty_struct *tty, struct usb_serial_port *port)
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 void usb_serial_generic_close(struct usb_serial_port *port)
172 dbg("%s - port %d", __func__, port->number);
173 generic_cleanup(port);
176 static int usb_serial_multi_urb_write(struct tty_struct *tty,
177 struct usb_serial_port *port, const unsigned char *buf, int count)
181 unsigned char *buffer;
186 dbg("%s - port %d", __func__, port->number);
189 dbg("%s - write request of 0 bytes", __func__);
192 towrite = (count > port->bulk_out_size) ?
193 port->bulk_out_size : count;
194 spin_lock_irqsave(&port->lock, flags);
195 if (port->urbs_in_flight >
196 port->serial->type->max_in_flight_urbs) {
197 spin_unlock_irqrestore(&port->lock, flags);
198 dbg("%s - write limit hit", __func__);
201 port->tx_bytes_flight += towrite;
202 port->urbs_in_flight++;
203 spin_unlock_irqrestore(&port->lock, flags);
205 buffer = kmalloc(towrite, GFP_ATOMIC);
208 "%s ran out of kernel memory for urb ...\n", __func__);
209 goto error_no_buffer;
212 urb = usb_alloc_urb(0, GFP_ATOMIC);
214 dev_err(&port->dev, "%s - no more free urbs\n",
220 memcpy(buffer, buf + bwrite, towrite);
221 usb_serial_debug_data(debug, &port->dev, __func__,
223 /* fill the buffer and send it */
224 usb_fill_bulk_urb(urb, port->serial->dev,
225 usb_sndbulkpipe(port->serial->dev,
226 port->bulk_out_endpointAddress),
228 usb_serial_generic_write_bulk_callback, port);
230 status = usb_submit_urb(urb, GFP_ATOMIC);
233 "%s - failed submitting write urb, error %d\n",
238 /* This urb is the responsibility of the host driver now */
240 dbg("%s write: %d", __func__, towrite);
251 spin_lock_irqsave(&port->lock, flags);
252 port->urbs_in_flight--;
253 port->tx_bytes_flight -= towrite;
254 spin_unlock_irqrestore(&port->lock, flags);
259 * usb_serial_generic_write_start - kick off an URB write
260 * @port: Pointer to the &struct usb_serial_port data
262 * Returns the number of bytes queued on success. This will be zero if there
263 * was nothing to send. Otherwise, it returns a negative errno value
265 static int usb_serial_generic_write_start(struct usb_serial_port *port)
267 struct usb_serial *serial = port->serial;
274 /* Atomically determine whether we can and need to start a USB
276 spin_lock_irqsave(&port->lock, flags);
277 if (port->write_urb_busy)
280 start_io = (kfifo_len(&port->write_fifo) != 0);
281 port->write_urb_busy = start_io;
283 spin_unlock_irqrestore(&port->lock, flags);
288 data = port->write_urb->transfer_buffer;
289 count = kfifo_out_locked(&port->write_fifo, data, port->bulk_out_size, &port->lock);
290 usb_serial_debug_data(debug, &port->dev, __func__, count, data);
293 usb_fill_bulk_urb(port->write_urb, serial->dev,
294 usb_sndbulkpipe(serial->dev,
295 port->bulk_out_endpointAddress),
296 port->write_urb->transfer_buffer, count,
297 ((serial->type->write_bulk_callback) ?
298 serial->type->write_bulk_callback :
299 usb_serial_generic_write_bulk_callback),
302 /* send the data out the bulk port */
303 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
306 "%s - failed submitting write urb, error %d\n",
308 /* don't have to grab the lock here, as we will
310 port->write_urb_busy = 0;
318 * usb_serial_generic_write - generic write function for serial USB devices
319 * @tty: Pointer to &struct tty_struct for the device
320 * @port: Pointer to the &usb_serial_port structure for the device
321 * @buf: Pointer to the data to write
322 * @count: Number of bytes to write
324 * Returns the number of characters actually written, which may be anything
325 * from zero to @count. If an error occurs, it returns the negative errno
328 int usb_serial_generic_write(struct tty_struct *tty,
329 struct usb_serial_port *port, const unsigned char *buf, int count)
331 struct usb_serial *serial = port->serial;
334 dbg("%s - port %d", __func__, port->number);
337 dbg("%s - write request of 0 bytes", __func__);
341 /* only do something if we have a bulk out endpoint */
342 if (!serial->num_bulk_out)
345 if (serial->type->max_in_flight_urbs)
346 return usb_serial_multi_urb_write(tty, port,
349 count = kfifo_in_locked(&port->write_fifo, buf, count, &port->lock);
350 result = usb_serial_generic_write_start(port);
357 EXPORT_SYMBOL_GPL(usb_serial_generic_write);
359 int usb_serial_generic_write_room(struct tty_struct *tty)
361 struct usb_serial_port *port = tty->driver_data;
362 struct usb_serial *serial = port->serial;
366 dbg("%s - port %d", __func__, port->number);
367 spin_lock_irqsave(&port->lock, flags);
368 if (serial->type->max_in_flight_urbs) {
369 if (port->urbs_in_flight < serial->type->max_in_flight_urbs)
370 room = port->bulk_out_size *
371 (serial->type->max_in_flight_urbs -
372 port->urbs_in_flight);
373 } else if (serial->num_bulk_out)
374 room = kfifo_avail(&port->write_fifo);
375 spin_unlock_irqrestore(&port->lock, flags);
377 dbg("%s - returns %d", __func__, room);
381 int usb_serial_generic_chars_in_buffer(struct tty_struct *tty)
383 struct usb_serial_port *port = tty->driver_data;
384 struct usb_serial *serial = port->serial;
388 dbg("%s - port %d", __func__, port->number);
390 spin_lock_irqsave(&port->lock, flags);
391 if (serial->type->max_in_flight_urbs)
392 chars = port->tx_bytes_flight;
393 else if (serial->num_bulk_out)
394 chars = kfifo_len(&port->write_fifo);
395 spin_unlock_irqrestore(&port->lock, flags);
397 dbg("%s - returns %d", __func__, chars);
402 void usb_serial_generic_resubmit_read_urb(struct usb_serial_port *port,
405 struct urb *urb = port->read_urb;
406 struct usb_serial *serial = port->serial;
409 /* Continue reading from device */
410 usb_fill_bulk_urb(urb, serial->dev,
411 usb_rcvbulkpipe(serial->dev,
412 port->bulk_in_endpointAddress),
413 urb->transfer_buffer,
414 urb->transfer_buffer_length,
415 ((serial->type->read_bulk_callback) ?
416 serial->type->read_bulk_callback :
417 usb_serial_generic_read_bulk_callback), port);
419 result = usb_submit_urb(urb, mem_flags);
420 if (result && result != -EPERM) {
422 "%s - failed resubmitting read urb, error %d\n",
426 EXPORT_SYMBOL_GPL(usb_serial_generic_resubmit_read_urb);
428 /* Push data to tty layer and resubmit the bulk read URB */
429 static void flush_and_resubmit_read_urb(struct usb_serial_port *port)
431 struct urb *urb = port->read_urb;
432 struct tty_struct *tty = tty_port_tty_get(&port->port);
433 char *ch = (char *)urb->transfer_buffer;
439 /* The per character mucking around with sysrq path it too slow for
440 stuff like 3G modems, so shortcircuit it in the 99.9999999% of cases
441 where the USB serial is not a console anyway */
442 if (!port->console || !port->sysrq)
443 tty_insert_flip_string(tty, ch, urb->actual_length);
445 /* Push data to tty */
446 for (i = 0; i < urb->actual_length; i++, ch++) {
447 if (!usb_serial_handle_sysrq_char(tty, port, *ch))
448 tty_insert_flip_char(tty, *ch, TTY_NORMAL);
451 tty_flip_buffer_push(tty);
454 usb_serial_generic_resubmit_read_urb(port, GFP_ATOMIC);
457 void usb_serial_generic_read_bulk_callback(struct urb *urb)
459 struct usb_serial_port *port = urb->context;
460 unsigned char *data = urb->transfer_buffer;
461 int status = urb->status;
464 dbg("%s - port %d", __func__, port->number);
466 if (unlikely(status != 0)) {
467 dbg("%s - nonzero read bulk status received: %d",
472 usb_serial_debug_data(debug, &port->dev, __func__,
473 urb->actual_length, data);
475 /* Throttle the device if requested by tty */
476 spin_lock_irqsave(&port->lock, flags);
477 port->throttled = port->throttle_req;
478 if (!port->throttled) {
479 spin_unlock_irqrestore(&port->lock, flags);
480 flush_and_resubmit_read_urb(port);
482 spin_unlock_irqrestore(&port->lock, flags);
484 EXPORT_SYMBOL_GPL(usb_serial_generic_read_bulk_callback);
486 void usb_serial_generic_write_bulk_callback(struct urb *urb)
489 struct usb_serial_port *port = urb->context;
490 int status = urb->status;
492 dbg("%s - port %d", __func__, port->number);
494 if (port->serial->type->max_in_flight_urbs) {
495 kfree(urb->transfer_buffer);
497 spin_lock_irqsave(&port->lock, flags);
498 --port->urbs_in_flight;
499 port->tx_bytes_flight -= urb->transfer_buffer_length;
500 if (port->urbs_in_flight < 0)
501 port->urbs_in_flight = 0;
502 spin_unlock_irqrestore(&port->lock, flags);
505 dbg("%s - nonzero multi-urb write bulk status "
506 "received: %d", __func__, status);
510 port->write_urb_busy = 0;
513 dbg("%s - nonzero multi-urb write bulk status "
514 "received: %d", __func__, status);
515 kfifo_reset_out(&port->write_fifo);
517 usb_serial_generic_write_start(port);
520 usb_serial_port_softint(port);
522 EXPORT_SYMBOL_GPL(usb_serial_generic_write_bulk_callback);
524 void usb_serial_generic_throttle(struct tty_struct *tty)
526 struct usb_serial_port *port = tty->driver_data;
529 dbg("%s - port %d", __func__, port->number);
531 /* Set the throttle request flag. It will be picked up
532 * by usb_serial_generic_read_bulk_callback(). */
533 spin_lock_irqsave(&port->lock, flags);
534 port->throttle_req = 1;
535 spin_unlock_irqrestore(&port->lock, flags);
538 void usb_serial_generic_unthrottle(struct tty_struct *tty)
540 struct usb_serial_port *port = tty->driver_data;
544 dbg("%s - port %d", __func__, port->number);
546 /* Clear the throttle flags */
547 spin_lock_irqsave(&port->lock, flags);
548 was_throttled = port->throttled;
549 port->throttled = port->throttle_req = 0;
550 spin_unlock_irqrestore(&port->lock, flags);
553 /* Resume reading from device */
554 flush_and_resubmit_read_urb(port);
558 int usb_serial_handle_sysrq_char(struct tty_struct *tty,
559 struct usb_serial_port *port, unsigned int ch)
561 if (port->sysrq && port->console) {
562 if (ch && time_before(jiffies, port->sysrq)) {
563 handle_sysrq(ch, tty);
571 EXPORT_SYMBOL_GPL(usb_serial_handle_sysrq_char);
573 int usb_serial_handle_break(struct usb_serial_port *port)
576 port->sysrq = jiffies + HZ*5;
582 EXPORT_SYMBOL_GPL(usb_serial_handle_break);
584 int usb_serial_generic_resume(struct usb_serial *serial)
586 struct usb_serial_port *port;
589 for (i = 0; i < serial->num_ports; i++) {
590 port = serial->port[i];
591 if (!test_bit(ASYNCB_INITIALIZED, &port->port.flags))
594 if (port->read_urb) {
595 r = usb_submit_urb(port->read_urb, GFP_NOIO);
600 if (port->write_urb) {
601 r = usb_serial_generic_write_start(port);
609 EXPORT_SYMBOL_GPL(usb_serial_generic_resume);
611 void usb_serial_generic_disconnect(struct usb_serial *serial)
617 /* stop reads and writes on all ports */
618 for (i = 0; i < serial->num_ports; ++i)
619 generic_cleanup(serial->port[i]);
622 void usb_serial_generic_release(struct usb_serial *serial)