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 #define MAX_TX_URBS 40
30 #ifdef CONFIG_USB_SERIAL_GENERIC
32 static int generic_probe(struct usb_interface *interface,
33 const struct usb_device_id *id);
35 static __u16 vendor = 0x05f9;
36 static __u16 product = 0xffff;
38 module_param(vendor, ushort, 0);
39 MODULE_PARM_DESC(vendor, "User specified USB idVendor");
41 module_param(product, ushort, 0);
42 MODULE_PARM_DESC(product, "User specified USB idProduct");
44 static struct usb_device_id generic_device_ids[2]; /* Initially all zeroes. */
46 /* we want to look at all devices, as the vendor/product id can change
47 * depending on the command line argument */
48 static const struct usb_device_id generic_serial_ids[] = {
53 static struct usb_driver generic_driver = {
54 .name = "usbserial_generic",
55 .probe = generic_probe,
56 .disconnect = usb_serial_disconnect,
57 .id_table = generic_serial_ids,
61 /* All of the device info needed for the Generic Serial Converter */
62 struct usb_serial_driver usb_serial_generic_device = {
67 .id_table = generic_device_ids,
68 .usb_driver = &generic_driver,
70 .disconnect = usb_serial_generic_disconnect,
71 .release = usb_serial_generic_release,
72 .throttle = usb_serial_generic_throttle,
73 .unthrottle = usb_serial_generic_unthrottle,
74 .resume = usb_serial_generic_resume,
77 static int generic_probe(struct usb_interface *interface,
78 const struct usb_device_id *id)
80 const struct usb_device_id *id_pattern;
82 id_pattern = usb_match_id(interface, generic_device_ids);
83 if (id_pattern != NULL)
84 return usb_serial_probe(interface, id);
89 int usb_serial_generic_register(int _debug)
94 #ifdef CONFIG_USB_SERIAL_GENERIC
95 generic_device_ids[0].idVendor = vendor;
96 generic_device_ids[0].idProduct = product;
97 generic_device_ids[0].match_flags =
98 USB_DEVICE_ID_MATCH_VENDOR | USB_DEVICE_ID_MATCH_PRODUCT;
100 /* register our generic driver with ourselves */
101 retval = usb_serial_register(&usb_serial_generic_device);
104 retval = usb_register(&generic_driver);
106 usb_serial_deregister(&usb_serial_generic_device);
112 void usb_serial_generic_deregister(void)
114 #ifdef CONFIG_USB_SERIAL_GENERIC
115 /* remove our generic driver */
116 usb_deregister(&generic_driver);
117 usb_serial_deregister(&usb_serial_generic_device);
121 int usb_serial_generic_open(struct tty_struct *tty, struct usb_serial_port *port)
126 dbg("%s - port %d", __func__, port->number);
128 /* clear the throttle flags */
129 spin_lock_irqsave(&port->lock, flags);
131 port->throttle_req = 0;
132 spin_unlock_irqrestore(&port->lock, flags);
134 /* if we have a bulk endpoint, start reading from it */
135 if (port->bulk_in_size)
136 result = usb_serial_generic_submit_read_urb(port, GFP_KERNEL);
140 EXPORT_SYMBOL_GPL(usb_serial_generic_open);
142 static void generic_cleanup(struct usb_serial_port *port)
144 struct usb_serial *serial = port->serial;
147 dbg("%s - port %d", __func__, port->number);
150 /* shutdown any bulk transfers that might be going on */
151 if (port->bulk_out_size) {
152 usb_kill_urb(port->write_urb);
154 spin_lock_irqsave(&port->lock, flags);
155 kfifo_reset_out(&port->write_fifo);
156 spin_unlock_irqrestore(&port->lock, flags);
158 if (port->bulk_in_size)
159 usb_kill_urb(port->read_urb);
163 void usb_serial_generic_close(struct usb_serial_port *port)
165 dbg("%s - port %d", __func__, port->number);
166 generic_cleanup(port);
168 EXPORT_SYMBOL_GPL(usb_serial_generic_close);
170 static int usb_serial_multi_urb_write(struct tty_struct *tty,
171 struct usb_serial_port *port, const unsigned char *buf, int count)
175 unsigned char *buffer;
178 spin_lock_irqsave(&port->lock, flags);
179 if (port->tx_urbs == MAX_TX_URBS) {
180 spin_unlock_irqrestore(&port->lock, flags);
181 dbg("%s - write limit hit", __func__);
185 spin_unlock_irqrestore(&port->lock, flags);
187 urb = usb_alloc_urb(0, GFP_ATOMIC);
189 dev_err(&port->dev, "%s - no free urbs available\n", __func__);
194 count = min_t(int, count, PAGE_SIZE);
195 buffer = kmalloc(count, GFP_ATOMIC);
197 dev_err(&port->dev, "%s - could not allocate buffer\n",
203 memcpy(buffer, buf, count);
204 usb_serial_debug_data(debug, &port->dev, __func__, count, buffer);
205 usb_fill_bulk_urb(urb, port->serial->dev,
206 usb_sndbulkpipe(port->serial->dev,
207 port->bulk_out_endpointAddress),
209 port->serial->type->write_bulk_callback, port);
211 status = usb_submit_urb(urb, GFP_ATOMIC);
213 dev_err(&port->dev, "%s - error submitting urb: %d\n",
217 spin_lock_irqsave(&port->lock, flags);
218 port->tx_bytes += urb->transfer_buffer_length;
219 spin_unlock_irqrestore(&port->lock, flags);
229 spin_lock_irqsave(&port->lock, flags);
231 spin_unlock_irqrestore(&port->lock, flags);
237 * usb_serial_generic_write_start - kick off an URB write
238 * @port: Pointer to the &struct usb_serial_port data
240 * Returns the number of bytes queued on success. This will be zero if there
241 * was nothing to send. Otherwise, it returns a negative errno value
243 static int usb_serial_generic_write_start(struct usb_serial_port *port)
250 spin_lock_irqsave(&port->lock, flags);
251 if (port->write_urb_busy || !kfifo_len(&port->write_fifo)) {
252 spin_unlock_irqrestore(&port->lock, flags);
255 port->write_urb_busy = 1;
256 spin_unlock_irqrestore(&port->lock, flags);
258 data = port->write_urb->transfer_buffer;
259 count = kfifo_out_locked(&port->write_fifo, data, port->bulk_out_size, &port->lock);
260 usb_serial_debug_data(debug, &port->dev, __func__, count, data);
262 port->write_urb->transfer_buffer_length = count;
264 /* send the data out the bulk port */
265 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
267 dev_err(&port->dev, "%s - error submitting urb: %d\n",
269 /* don't have to grab the lock here, as we will
271 port->write_urb_busy = 0;
275 spin_lock_irqsave(&port->lock, flags);
276 port->tx_bytes += count;
277 spin_unlock_irqrestore(&port->lock, flags);
283 * usb_serial_generic_write - generic write function for serial USB devices
284 * @tty: Pointer to &struct tty_struct for the device
285 * @port: Pointer to the &usb_serial_port structure for the device
286 * @buf: Pointer to the data to write
287 * @count: Number of bytes to write
289 * Returns the number of characters actually written, which may be anything
290 * from zero to @count. If an error occurs, it returns the negative errno
293 int usb_serial_generic_write(struct tty_struct *tty,
294 struct usb_serial_port *port, const unsigned char *buf, int count)
296 struct usb_serial *serial = port->serial;
299 dbg("%s - port %d", __func__, port->number);
301 /* only do something if we have a bulk out endpoint */
302 if (!port->bulk_out_size)
308 if (serial->type->multi_urb_write)
309 return usb_serial_multi_urb_write(tty, port, buf, count);
311 count = kfifo_in_locked(&port->write_fifo, buf, count, &port->lock);
312 result = usb_serial_generic_write_start(port);
319 EXPORT_SYMBOL_GPL(usb_serial_generic_write);
321 int usb_serial_generic_write_room(struct tty_struct *tty)
323 struct usb_serial_port *port = tty->driver_data;
324 struct usb_serial *serial = port->serial;
328 dbg("%s - port %d", __func__, port->number);
330 if (!port->bulk_out_size)
333 spin_lock_irqsave(&port->lock, flags);
334 if (serial->type->multi_urb_write)
335 room = (MAX_TX_URBS - port->tx_urbs) * PAGE_SIZE;
337 room = kfifo_avail(&port->write_fifo);
338 spin_unlock_irqrestore(&port->lock, flags);
340 dbg("%s - returns %d", __func__, room);
344 int usb_serial_generic_chars_in_buffer(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);
353 if (!port->bulk_out_size)
356 spin_lock_irqsave(&port->lock, flags);
357 if (serial->type->multi_urb_write)
358 chars = port->tx_bytes;
360 chars = kfifo_len(&port->write_fifo) + port->tx_bytes;
361 spin_unlock_irqrestore(&port->lock, flags);
363 dbg("%s - returns %d", __func__, chars);
367 int usb_serial_generic_submit_read_urb(struct usb_serial_port *port,
372 result = usb_submit_urb(port->read_urb, mem_flags);
373 if (result && result != -EPERM) {
374 dev_err(&port->dev, "%s - error submitting urb: %d\n",
379 EXPORT_SYMBOL_GPL(usb_serial_generic_submit_read_urb);
381 void usb_serial_generic_process_read_urb(struct urb *urb)
383 struct usb_serial_port *port = urb->context;
384 struct tty_struct *tty;
385 char *ch = (char *)urb->transfer_buffer;
388 tty = tty_port_tty_get(&port->port);
392 /* The per character mucking around with sysrq path it too slow for
393 stuff like 3G modems, so shortcircuit it in the 99.9999999% of cases
394 where the USB serial is not a console anyway */
395 if (!port->port.console || !port->sysrq)
396 tty_insert_flip_string(tty, ch, urb->actual_length);
398 for (i = 0; i < urb->actual_length; i++, ch++) {
399 if (!usb_serial_handle_sysrq_char(tty, port, *ch))
400 tty_insert_flip_char(tty, *ch, TTY_NORMAL);
403 tty_flip_buffer_push(tty);
406 EXPORT_SYMBOL_GPL(usb_serial_generic_process_read_urb);
408 void usb_serial_generic_read_bulk_callback(struct urb *urb)
410 struct usb_serial_port *port = urb->context;
411 unsigned char *data = urb->transfer_buffer;
412 int status = urb->status;
415 dbg("%s - port %d", __func__, port->number);
417 if (unlikely(status != 0)) {
418 dbg("%s - nonzero read bulk status received: %d",
423 usb_serial_debug_data(debug, &port->dev, __func__,
424 urb->actual_length, data);
425 port->serial->type->process_read_urb(urb);
427 /* Throttle the device if requested by tty */
428 spin_lock_irqsave(&port->lock, flags);
429 port->throttled = port->throttle_req;
430 if (!port->throttled) {
431 spin_unlock_irqrestore(&port->lock, flags);
432 usb_serial_generic_submit_read_urb(port, GFP_ATOMIC);
434 spin_unlock_irqrestore(&port->lock, flags);
436 EXPORT_SYMBOL_GPL(usb_serial_generic_read_bulk_callback);
438 void usb_serial_generic_write_bulk_callback(struct urb *urb)
441 struct usb_serial_port *port = urb->context;
442 int status = urb->status;
444 dbg("%s - port %d", __func__, port->number);
446 if (port->serial->type->multi_urb_write) {
447 kfree(urb->transfer_buffer);
449 spin_lock_irqsave(&port->lock, flags);
450 port->tx_bytes -= urb->transfer_buffer_length;
452 spin_unlock_irqrestore(&port->lock, flags);
454 spin_lock_irqsave(&port->lock, flags);
455 port->tx_bytes -= urb->transfer_buffer_length;
456 port->write_urb_busy = 0;
457 spin_unlock_irqrestore(&port->lock, flags);
460 spin_lock_irqsave(&port->lock, flags);
461 kfifo_reset_out(&port->write_fifo);
462 spin_unlock_irqrestore(&port->lock, flags);
464 usb_serial_generic_write_start(port);
469 dbg("%s - non-zero urb status: %d", __func__, status);
471 usb_serial_port_softint(port);
473 EXPORT_SYMBOL_GPL(usb_serial_generic_write_bulk_callback);
475 void usb_serial_generic_throttle(struct tty_struct *tty)
477 struct usb_serial_port *port = tty->driver_data;
480 dbg("%s - port %d", __func__, port->number);
482 /* Set the throttle request flag. It will be picked up
483 * by usb_serial_generic_read_bulk_callback(). */
484 spin_lock_irqsave(&port->lock, flags);
485 port->throttle_req = 1;
486 spin_unlock_irqrestore(&port->lock, flags);
488 EXPORT_SYMBOL_GPL(usb_serial_generic_throttle);
490 void usb_serial_generic_unthrottle(struct tty_struct *tty)
492 struct usb_serial_port *port = tty->driver_data;
495 dbg("%s - port %d", __func__, port->number);
497 /* Clear the throttle flags */
498 spin_lock_irq(&port->lock);
499 was_throttled = port->throttled;
500 port->throttled = port->throttle_req = 0;
501 spin_unlock_irq(&port->lock);
504 usb_serial_generic_submit_read_urb(port, GFP_KERNEL);
506 EXPORT_SYMBOL_GPL(usb_serial_generic_unthrottle);
508 #ifdef CONFIG_MAGIC_SYSRQ
509 int usb_serial_handle_sysrq_char(struct tty_struct *tty,
510 struct usb_serial_port *port, unsigned int ch)
512 if (port->sysrq && port->port.console) {
513 if (ch && time_before(jiffies, port->sysrq)) {
514 handle_sysrq(ch, tty);
523 int usb_serial_handle_sysrq_char(struct tty_struct *tty,
524 struct usb_serial_port *port, unsigned int ch)
529 EXPORT_SYMBOL_GPL(usb_serial_handle_sysrq_char);
531 int usb_serial_handle_break(struct usb_serial_port *port)
534 port->sysrq = jiffies + HZ*5;
540 EXPORT_SYMBOL_GPL(usb_serial_handle_break);
542 int usb_serial_generic_resume(struct usb_serial *serial)
544 struct usb_serial_port *port;
547 for (i = 0; i < serial->num_ports; i++) {
548 port = serial->port[i];
549 if (!test_bit(ASYNCB_INITIALIZED, &port->port.flags))
552 if (port->read_urb) {
553 r = usb_submit_urb(port->read_urb, GFP_NOIO);
558 if (port->write_urb) {
559 r = usb_serial_generic_write_start(port);
567 EXPORT_SYMBOL_GPL(usb_serial_generic_resume);
569 void usb_serial_generic_disconnect(struct usb_serial *serial)
575 /* stop reads and writes on all ports */
576 for (i = 0; i < serial->num_ports; ++i)
577 generic_cleanup(serial->port[i]);
580 void usb_serial_generic_release(struct usb_serial *serial)