2 * USB Serial Converter Generic functions
4 * Copyright (C) 2010 - 2011 Johan Hovold (jhovold@gmail.com)
5 * Copyright (C) 1999 - 2002 Greg Kroah-Hartman (greg@kroah.com)
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License version
9 * 2 as published by the Free Software Foundation.
13 #include <linux/kernel.h>
14 #include <linux/errno.h>
15 #include <linux/slab.h>
16 #include <linux/sysrq.h>
17 #include <linux/tty.h>
18 #include <linux/tty_flip.h>
19 #include <linux/module.h>
20 #include <linux/moduleparam.h>
21 #include <linux/usb.h>
22 #include <linux/usb/serial.h>
23 #include <linux/uaccess.h>
24 #include <linux/kfifo.h>
25 #include <linux/serial.h>
29 #ifdef CONFIG_USB_SERIAL_GENERIC
31 static int generic_probe(struct usb_interface *interface,
32 const struct usb_device_id *id);
34 static __u16 vendor = 0x05f9;
35 static __u16 product = 0xffff;
37 module_param(vendor, ushort, 0);
38 MODULE_PARM_DESC(vendor, "User specified USB idVendor");
40 module_param(product, ushort, 0);
41 MODULE_PARM_DESC(product, "User specified USB idProduct");
43 static struct usb_device_id generic_device_ids[2]; /* Initially all zeroes. */
45 /* we want to look at all devices, as the vendor/product id can change
46 * depending on the command line argument */
47 static const struct usb_device_id generic_serial_ids[] = {
52 static struct usb_driver generic_driver = {
53 .name = "usbserial_generic",
54 .probe = generic_probe,
55 .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,
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 struct usb_serial_driver * const serial_drivers[] = {
74 &usb_serial_generic_device, NULL
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_drivers(&generic_driver, serial_drivers);
106 void usb_serial_generic_deregister(void)
108 #ifdef CONFIG_USB_SERIAL_GENERIC
109 /* remove our generic driver */
110 usb_serial_deregister_drivers(&generic_driver, serial_drivers);
114 int usb_serial_generic_open(struct tty_struct *tty, struct usb_serial_port *port)
119 /* clear the throttle flags */
120 spin_lock_irqsave(&port->lock, flags);
122 port->throttle_req = 0;
123 spin_unlock_irqrestore(&port->lock, flags);
125 /* if we have a bulk endpoint, start reading from it */
126 if (port->bulk_in_size)
127 result = usb_serial_generic_submit_read_urbs(port, GFP_KERNEL);
131 EXPORT_SYMBOL_GPL(usb_serial_generic_open);
133 static void generic_cleanup(struct usb_serial_port *port)
135 struct usb_serial *serial = port->serial;
140 /* shutdown any bulk transfers that might be going on */
141 if (port->bulk_out_size) {
142 for (i = 0; i < ARRAY_SIZE(port->write_urbs); ++i)
143 usb_kill_urb(port->write_urbs[i]);
145 spin_lock_irqsave(&port->lock, flags);
146 kfifo_reset_out(&port->write_fifo);
147 spin_unlock_irqrestore(&port->lock, flags);
149 if (port->bulk_in_size) {
150 for (i = 0; i < ARRAY_SIZE(port->read_urbs); ++i)
151 usb_kill_urb(port->read_urbs[i]);
156 void usb_serial_generic_close(struct usb_serial_port *port)
158 generic_cleanup(port);
160 EXPORT_SYMBOL_GPL(usb_serial_generic_close);
162 int usb_serial_generic_prepare_write_buffer(struct usb_serial_port *port,
163 void *dest, size_t size)
165 return kfifo_out_locked(&port->write_fifo, dest, size, &port->lock);
169 * usb_serial_generic_write_start - kick off an URB write
170 * @port: Pointer to the &struct usb_serial_port data
172 * Returns zero on success, or a negative errno value
174 static int usb_serial_generic_write_start(struct usb_serial_port *port)
181 if (test_and_set_bit_lock(USB_SERIAL_WRITE_BUSY, &port->flags))
184 spin_lock_irqsave(&port->lock, flags);
185 if (!port->write_urbs_free || !kfifo_len(&port->write_fifo)) {
186 clear_bit_unlock(USB_SERIAL_WRITE_BUSY, &port->flags);
187 spin_unlock_irqrestore(&port->lock, flags);
190 i = (int)find_first_bit(&port->write_urbs_free,
191 ARRAY_SIZE(port->write_urbs));
192 spin_unlock_irqrestore(&port->lock, flags);
194 urb = port->write_urbs[i];
195 count = port->serial->type->prepare_write_buffer(port,
196 urb->transfer_buffer,
197 port->bulk_out_size);
198 urb->transfer_buffer_length = count;
199 usb_serial_debug_data(debug, &port->dev, __func__, count,
200 urb->transfer_buffer);
201 spin_lock_irqsave(&port->lock, flags);
202 port->tx_bytes += count;
203 spin_unlock_irqrestore(&port->lock, flags);
205 clear_bit(i, &port->write_urbs_free);
206 result = usb_submit_urb(urb, GFP_ATOMIC);
208 dev_err_console(port, "%s - error submitting urb: %d\n",
210 set_bit(i, &port->write_urbs_free);
211 spin_lock_irqsave(&port->lock, flags);
212 port->tx_bytes -= count;
213 spin_unlock_irqrestore(&port->lock, flags);
215 clear_bit_unlock(USB_SERIAL_WRITE_BUSY, &port->flags);
219 /* Try sending off another urb, unless in irq context (in which case
220 * there will be no free urb). */
224 clear_bit_unlock(USB_SERIAL_WRITE_BUSY, &port->flags);
230 * usb_serial_generic_write - generic write function for serial USB devices
231 * @tty: Pointer to &struct tty_struct for the device
232 * @port: Pointer to the &usb_serial_port structure for the device
233 * @buf: Pointer to the data to write
234 * @count: Number of bytes to write
236 * Returns the number of characters actually written, which may be anything
237 * from zero to @count. If an error occurs, it returns the negative errno
240 int usb_serial_generic_write(struct tty_struct *tty,
241 struct usb_serial_port *port, const unsigned char *buf, int count)
245 /* only do something if we have a bulk out endpoint */
246 if (!port->bulk_out_size)
252 count = kfifo_in_locked(&port->write_fifo, buf, count, &port->lock);
253 result = usb_serial_generic_write_start(port);
259 EXPORT_SYMBOL_GPL(usb_serial_generic_write);
261 int usb_serial_generic_write_room(struct tty_struct *tty)
263 struct usb_serial_port *port = tty->driver_data;
267 if (!port->bulk_out_size)
270 spin_lock_irqsave(&port->lock, flags);
271 room = kfifo_avail(&port->write_fifo);
272 spin_unlock_irqrestore(&port->lock, flags);
274 dbg("%s - returns %d", __func__, room);
278 int usb_serial_generic_chars_in_buffer(struct tty_struct *tty)
280 struct usb_serial_port *port = tty->driver_data;
284 if (!port->bulk_out_size)
287 spin_lock_irqsave(&port->lock, flags);
288 chars = kfifo_len(&port->write_fifo) + port->tx_bytes;
289 spin_unlock_irqrestore(&port->lock, flags);
291 dbg("%s - returns %d", __func__, chars);
295 static int usb_serial_generic_submit_read_urb(struct usb_serial_port *port,
296 int index, gfp_t mem_flags)
300 if (!test_and_clear_bit(index, &port->read_urbs_free))
303 dbg("%s - port %d, urb %d", __func__, port->number, index);
305 res = usb_submit_urb(port->read_urbs[index], mem_flags);
309 "%s - usb_submit_urb failed: %d\n",
312 set_bit(index, &port->read_urbs_free);
319 int usb_serial_generic_submit_read_urbs(struct usb_serial_port *port,
325 for (i = 0; i < ARRAY_SIZE(port->read_urbs); ++i) {
326 res = usb_serial_generic_submit_read_urb(port, i, mem_flags);
334 usb_kill_urb(port->read_urbs[i]);
338 EXPORT_SYMBOL_GPL(usb_serial_generic_submit_read_urbs);
340 void usb_serial_generic_process_read_urb(struct urb *urb)
342 struct usb_serial_port *port = urb->context;
343 struct tty_struct *tty;
344 char *ch = (char *)urb->transfer_buffer;
347 if (!urb->actual_length)
350 tty = tty_port_tty_get(&port->port);
354 /* The per character mucking around with sysrq path it too slow for
355 stuff like 3G modems, so shortcircuit it in the 99.9999999% of cases
356 where the USB serial is not a console anyway */
357 if (!port->port.console || !port->sysrq)
358 tty_insert_flip_string(tty, ch, urb->actual_length);
360 for (i = 0; i < urb->actual_length; i++, ch++) {
361 if (!usb_serial_handle_sysrq_char(port, *ch))
362 tty_insert_flip_char(tty, *ch, TTY_NORMAL);
365 tty_flip_buffer_push(tty);
368 EXPORT_SYMBOL_GPL(usb_serial_generic_process_read_urb);
370 void usb_serial_generic_read_bulk_callback(struct urb *urb)
372 struct usb_serial_port *port = urb->context;
373 unsigned char *data = urb->transfer_buffer;
377 for (i = 0; i < ARRAY_SIZE(port->read_urbs); ++i) {
378 if (urb == port->read_urbs[i])
381 set_bit(i, &port->read_urbs_free);
383 dbg("%s - port %d, urb %d, len %d", __func__, port->number, i,
386 dbg("%s - non-zero urb status: %d", __func__, urb->status);
390 usb_serial_debug_data(debug, &port->dev, __func__,
391 urb->actual_length, data);
392 port->serial->type->process_read_urb(urb);
394 /* Throttle the device if requested by tty */
395 spin_lock_irqsave(&port->lock, flags);
396 port->throttled = port->throttle_req;
397 if (!port->throttled) {
398 spin_unlock_irqrestore(&port->lock, flags);
399 usb_serial_generic_submit_read_urb(port, i, GFP_ATOMIC);
401 spin_unlock_irqrestore(&port->lock, flags);
403 EXPORT_SYMBOL_GPL(usb_serial_generic_read_bulk_callback);
405 void usb_serial_generic_write_bulk_callback(struct urb *urb)
408 struct usb_serial_port *port = urb->context;
409 int status = urb->status;
412 for (i = 0; i < ARRAY_SIZE(port->write_urbs); ++i)
413 if (port->write_urbs[i] == urb)
416 spin_lock_irqsave(&port->lock, flags);
417 port->tx_bytes -= urb->transfer_buffer_length;
418 set_bit(i, &port->write_urbs_free);
419 spin_unlock_irqrestore(&port->lock, flags);
422 dbg("%s - non-zero urb status: %d", __func__, status);
424 spin_lock_irqsave(&port->lock, flags);
425 kfifo_reset_out(&port->write_fifo);
426 spin_unlock_irqrestore(&port->lock, flags);
428 usb_serial_generic_write_start(port);
431 usb_serial_port_softint(port);
433 EXPORT_SYMBOL_GPL(usb_serial_generic_write_bulk_callback);
435 void usb_serial_generic_throttle(struct tty_struct *tty)
437 struct usb_serial_port *port = tty->driver_data;
440 /* Set the throttle request flag. It will be picked up
441 * by usb_serial_generic_read_bulk_callback(). */
442 spin_lock_irqsave(&port->lock, flags);
443 port->throttle_req = 1;
444 spin_unlock_irqrestore(&port->lock, flags);
446 EXPORT_SYMBOL_GPL(usb_serial_generic_throttle);
448 void usb_serial_generic_unthrottle(struct tty_struct *tty)
450 struct usb_serial_port *port = tty->driver_data;
453 /* Clear the throttle flags */
454 spin_lock_irq(&port->lock);
455 was_throttled = port->throttled;
456 port->throttled = port->throttle_req = 0;
457 spin_unlock_irq(&port->lock);
460 usb_serial_generic_submit_read_urbs(port, GFP_KERNEL);
462 EXPORT_SYMBOL_GPL(usb_serial_generic_unthrottle);
464 #ifdef CONFIG_MAGIC_SYSRQ
465 int usb_serial_handle_sysrq_char(struct usb_serial_port *port, unsigned int ch)
467 if (port->sysrq && port->port.console) {
468 if (ch && time_before(jiffies, port->sysrq)) {
478 int usb_serial_handle_sysrq_char(struct usb_serial_port *port, unsigned int ch)
483 EXPORT_SYMBOL_GPL(usb_serial_handle_sysrq_char);
485 int usb_serial_handle_break(struct usb_serial_port *port)
488 port->sysrq = jiffies + HZ*5;
494 EXPORT_SYMBOL_GPL(usb_serial_handle_break);
497 * usb_serial_handle_dcd_change - handle a change of carrier detect state
498 * @port: usb_serial_port structure for the open port
499 * @tty: tty_struct structure for the port
500 * @status: new carrier detect status, nonzero if active
502 void usb_serial_handle_dcd_change(struct usb_serial_port *usb_port,
503 struct tty_struct *tty, unsigned int status)
505 struct tty_port *port = &usb_port->port;
507 dbg("%s - port %d, status %d", __func__, usb_port->number, status);
510 wake_up_interruptible(&port->open_wait);
511 else if (tty && !C_CLOCAL(tty))
514 EXPORT_SYMBOL_GPL(usb_serial_handle_dcd_change);
516 int usb_serial_generic_resume(struct usb_serial *serial)
518 struct usb_serial_port *port;
521 for (i = 0; i < serial->num_ports; i++) {
522 port = serial->port[i];
523 if (!test_bit(ASYNCB_INITIALIZED, &port->port.flags))
526 if (port->bulk_in_size) {
527 r = usb_serial_generic_submit_read_urbs(port,
533 if (port->bulk_out_size) {
534 r = usb_serial_generic_write_start(port);
542 EXPORT_SYMBOL_GPL(usb_serial_generic_resume);
544 void usb_serial_generic_disconnect(struct usb_serial *serial)
548 /* stop reads and writes on all ports */
549 for (i = 0; i < serial->num_ports; ++i)
550 generic_cleanup(serial->port[i]);
552 EXPORT_SYMBOL_GPL(usb_serial_generic_disconnect);
554 void usb_serial_generic_release(struct usb_serial *serial)