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 .disconnect = usb_serial_disconnect,
56 .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,
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 struct usb_serial_driver * const serial_drivers[] = {
75 &usb_serial_generic_device, NULL
78 static int generic_probe(struct usb_interface *interface,
79 const struct usb_device_id *id)
81 const struct usb_device_id *id_pattern;
83 id_pattern = usb_match_id(interface, generic_device_ids);
84 if (id_pattern != NULL)
85 return usb_serial_probe(interface, id);
90 int usb_serial_generic_register(int _debug)
95 #ifdef CONFIG_USB_SERIAL_GENERIC
96 generic_device_ids[0].idVendor = vendor;
97 generic_device_ids[0].idProduct = product;
98 generic_device_ids[0].match_flags =
99 USB_DEVICE_ID_MATCH_VENDOR | USB_DEVICE_ID_MATCH_PRODUCT;
101 /* register our generic driver with ourselves */
102 retval = usb_serial_register_drivers(&generic_driver, serial_drivers);
107 void usb_serial_generic_deregister(void)
109 #ifdef CONFIG_USB_SERIAL_GENERIC
110 /* remove our generic driver */
111 usb_serial_deregister_drivers(&generic_driver, serial_drivers);
115 int usb_serial_generic_open(struct tty_struct *tty, struct usb_serial_port *port)
120 dbg("%s - port %d", __func__, port->number);
122 /* clear the throttle flags */
123 spin_lock_irqsave(&port->lock, flags);
125 port->throttle_req = 0;
126 spin_unlock_irqrestore(&port->lock, flags);
128 /* if we have a bulk endpoint, start reading from it */
129 if (port->bulk_in_size)
130 result = usb_serial_generic_submit_read_urbs(port, GFP_KERNEL);
134 EXPORT_SYMBOL_GPL(usb_serial_generic_open);
136 static void generic_cleanup(struct usb_serial_port *port)
138 struct usb_serial *serial = port->serial;
142 dbg("%s - port %d", __func__, port->number);
145 /* shutdown any bulk transfers that might be going on */
146 if (port->bulk_out_size) {
147 usb_kill_urb(port->write_urb);
148 for (i = 0; i < ARRAY_SIZE(port->write_urbs); ++i)
149 usb_kill_urb(port->write_urbs[i]);
151 spin_lock_irqsave(&port->lock, flags);
152 kfifo_reset_out(&port->write_fifo);
153 spin_unlock_irqrestore(&port->lock, flags);
155 if (port->bulk_in_size) {
156 for (i = 0; i < ARRAY_SIZE(port->read_urbs); ++i)
157 usb_kill_urb(port->read_urbs[i]);
162 void usb_serial_generic_close(struct usb_serial_port *port)
164 dbg("%s - port %d", __func__, port->number);
165 generic_cleanup(port);
167 EXPORT_SYMBOL_GPL(usb_serial_generic_close);
169 int usb_serial_generic_prepare_write_buffer(struct usb_serial_port *port,
170 void *dest, size_t size)
172 return kfifo_out_locked(&port->write_fifo, dest, size, &port->lock);
176 * usb_serial_generic_write_start - kick off an URB write
177 * @port: Pointer to the &struct usb_serial_port data
179 * Returns zero on success, or a negative errno value
181 static int usb_serial_generic_write_start(struct usb_serial_port *port)
188 if (test_and_set_bit_lock(USB_SERIAL_WRITE_BUSY, &port->flags))
191 spin_lock_irqsave(&port->lock, flags);
192 if (!port->write_urbs_free || !kfifo_len(&port->write_fifo)) {
193 clear_bit_unlock(USB_SERIAL_WRITE_BUSY, &port->flags);
194 spin_unlock_irqrestore(&port->lock, flags);
197 i = (int)find_first_bit(&port->write_urbs_free,
198 ARRAY_SIZE(port->write_urbs));
199 spin_unlock_irqrestore(&port->lock, flags);
201 urb = port->write_urbs[i];
202 count = port->serial->type->prepare_write_buffer(port,
203 urb->transfer_buffer,
204 port->bulk_out_size);
205 urb->transfer_buffer_length = count;
206 usb_serial_debug_data(debug, &port->dev, __func__, count,
207 urb->transfer_buffer);
208 spin_lock_irqsave(&port->lock, flags);
209 port->tx_bytes += count;
210 spin_unlock_irqrestore(&port->lock, flags);
212 clear_bit(i, &port->write_urbs_free);
213 result = usb_submit_urb(urb, GFP_ATOMIC);
215 dev_err_console(port, "%s - error submitting urb: %d\n",
217 set_bit(i, &port->write_urbs_free);
218 spin_lock_irqsave(&port->lock, flags);
219 port->tx_bytes -= count;
220 spin_unlock_irqrestore(&port->lock, flags);
222 clear_bit_unlock(USB_SERIAL_WRITE_BUSY, &port->flags);
226 /* Try sending off another urb, unless in irq context (in which case
227 * there will be no free urb). */
231 clear_bit_unlock(USB_SERIAL_WRITE_BUSY, &port->flags);
237 * usb_serial_generic_write - generic write function for serial USB devices
238 * @tty: Pointer to &struct tty_struct for the device
239 * @port: Pointer to the &usb_serial_port structure for the device
240 * @buf: Pointer to the data to write
241 * @count: Number of bytes to write
243 * Returns the number of characters actually written, which may be anything
244 * from zero to @count. If an error occurs, it returns the negative errno
247 int usb_serial_generic_write(struct tty_struct *tty,
248 struct usb_serial_port *port, const unsigned char *buf, int count)
252 dbg("%s - port %d", __func__, port->number);
254 /* only do something if we have a bulk out endpoint */
255 if (!port->bulk_out_size)
261 count = kfifo_in_locked(&port->write_fifo, buf, count, &port->lock);
262 result = usb_serial_generic_write_start(port);
268 EXPORT_SYMBOL_GPL(usb_serial_generic_write);
270 int usb_serial_generic_write_room(struct tty_struct *tty)
272 struct usb_serial_port *port = tty->driver_data;
276 dbg("%s - port %d", __func__, port->number);
278 if (!port->bulk_out_size)
281 spin_lock_irqsave(&port->lock, flags);
282 room = kfifo_avail(&port->write_fifo);
283 spin_unlock_irqrestore(&port->lock, flags);
285 dbg("%s - returns %d", __func__, room);
289 int usb_serial_generic_chars_in_buffer(struct tty_struct *tty)
291 struct usb_serial_port *port = tty->driver_data;
295 dbg("%s - port %d", __func__, port->number);
297 if (!port->bulk_out_size)
300 spin_lock_irqsave(&port->lock, flags);
301 chars = kfifo_len(&port->write_fifo) + port->tx_bytes;
302 spin_unlock_irqrestore(&port->lock, flags);
304 dbg("%s - returns %d", __func__, chars);
308 static int usb_serial_generic_submit_read_urb(struct usb_serial_port *port,
309 int index, gfp_t mem_flags)
313 if (!test_and_clear_bit(index, &port->read_urbs_free))
316 dbg("%s - port %d, urb %d\n", __func__, port->number, index);
318 res = usb_submit_urb(port->read_urbs[index], mem_flags);
322 "%s - usb_submit_urb failed: %d\n",
325 set_bit(index, &port->read_urbs_free);
332 int usb_serial_generic_submit_read_urbs(struct usb_serial_port *port,
338 dbg("%s - port %d", __func__, port->number);
340 for (i = 0; i < ARRAY_SIZE(port->read_urbs); ++i) {
341 res = usb_serial_generic_submit_read_urb(port, i, mem_flags);
349 usb_kill_urb(port->read_urbs[i]);
353 EXPORT_SYMBOL_GPL(usb_serial_generic_submit_read_urbs);
355 void usb_serial_generic_process_read_urb(struct urb *urb)
357 struct usb_serial_port *port = urb->context;
358 struct tty_struct *tty;
359 char *ch = (char *)urb->transfer_buffer;
362 if (!urb->actual_length)
365 tty = tty_port_tty_get(&port->port);
369 /* The per character mucking around with sysrq path it too slow for
370 stuff like 3G modems, so shortcircuit it in the 99.9999999% of cases
371 where the USB serial is not a console anyway */
372 if (!port->port.console || !port->sysrq)
373 tty_insert_flip_string(tty, ch, urb->actual_length);
375 for (i = 0; i < urb->actual_length; i++, ch++) {
376 if (!usb_serial_handle_sysrq_char(port, *ch))
377 tty_insert_flip_char(tty, *ch, TTY_NORMAL);
380 tty_flip_buffer_push(tty);
383 EXPORT_SYMBOL_GPL(usb_serial_generic_process_read_urb);
385 void usb_serial_generic_read_bulk_callback(struct urb *urb)
387 struct usb_serial_port *port = urb->context;
388 unsigned char *data = urb->transfer_buffer;
392 for (i = 0; i < ARRAY_SIZE(port->read_urbs); ++i) {
393 if (urb == port->read_urbs[i])
396 set_bit(i, &port->read_urbs_free);
398 dbg("%s - port %d, urb %d, len %d\n", __func__, port->number, i,
401 dbg("%s - non-zero urb status: %d\n", __func__, urb->status);
405 usb_serial_debug_data(debug, &port->dev, __func__,
406 urb->actual_length, data);
407 port->serial->type->process_read_urb(urb);
409 /* Throttle the device if requested by tty */
410 spin_lock_irqsave(&port->lock, flags);
411 port->throttled = port->throttle_req;
412 if (!port->throttled) {
413 spin_unlock_irqrestore(&port->lock, flags);
414 usb_serial_generic_submit_read_urb(port, i, GFP_ATOMIC);
416 spin_unlock_irqrestore(&port->lock, flags);
418 EXPORT_SYMBOL_GPL(usb_serial_generic_read_bulk_callback);
420 void usb_serial_generic_write_bulk_callback(struct urb *urb)
423 struct usb_serial_port *port = urb->context;
424 int status = urb->status;
427 dbg("%s - port %d", __func__, port->number);
429 for (i = 0; i < ARRAY_SIZE(port->write_urbs); ++i)
430 if (port->write_urbs[i] == urb)
433 spin_lock_irqsave(&port->lock, flags);
434 port->tx_bytes -= urb->transfer_buffer_length;
435 set_bit(i, &port->write_urbs_free);
436 spin_unlock_irqrestore(&port->lock, flags);
439 dbg("%s - non-zero urb status: %d", __func__, status);
441 spin_lock_irqsave(&port->lock, flags);
442 kfifo_reset_out(&port->write_fifo);
443 spin_unlock_irqrestore(&port->lock, flags);
445 usb_serial_generic_write_start(port);
448 usb_serial_port_softint(port);
450 EXPORT_SYMBOL_GPL(usb_serial_generic_write_bulk_callback);
452 void usb_serial_generic_throttle(struct tty_struct *tty)
454 struct usb_serial_port *port = tty->driver_data;
457 dbg("%s - port %d", __func__, port->number);
459 /* Set the throttle request flag. It will be picked up
460 * by usb_serial_generic_read_bulk_callback(). */
461 spin_lock_irqsave(&port->lock, flags);
462 port->throttle_req = 1;
463 spin_unlock_irqrestore(&port->lock, flags);
465 EXPORT_SYMBOL_GPL(usb_serial_generic_throttle);
467 void usb_serial_generic_unthrottle(struct tty_struct *tty)
469 struct usb_serial_port *port = tty->driver_data;
472 dbg("%s - port %d", __func__, port->number);
474 /* Clear the throttle flags */
475 spin_lock_irq(&port->lock);
476 was_throttled = port->throttled;
477 port->throttled = port->throttle_req = 0;
478 spin_unlock_irq(&port->lock);
481 usb_serial_generic_submit_read_urbs(port, GFP_KERNEL);
483 EXPORT_SYMBOL_GPL(usb_serial_generic_unthrottle);
485 #ifdef CONFIG_MAGIC_SYSRQ
486 int usb_serial_handle_sysrq_char(struct usb_serial_port *port, unsigned int ch)
488 if (port->sysrq && port->port.console) {
489 if (ch && time_before(jiffies, port->sysrq)) {
499 int usb_serial_handle_sysrq_char(struct usb_serial_port *port, unsigned int ch)
504 EXPORT_SYMBOL_GPL(usb_serial_handle_sysrq_char);
506 int usb_serial_handle_break(struct usb_serial_port *port)
509 port->sysrq = jiffies + HZ*5;
515 EXPORT_SYMBOL_GPL(usb_serial_handle_break);
518 * usb_serial_handle_dcd_change - handle a change of carrier detect state
519 * @port: usb_serial_port structure for the open port
520 * @tty: tty_struct structure for the port
521 * @status: new carrier detect status, nonzero if active
523 void usb_serial_handle_dcd_change(struct usb_serial_port *usb_port,
524 struct tty_struct *tty, unsigned int status)
526 struct tty_port *port = &usb_port->port;
528 dbg("%s - port %d, status %d", __func__, usb_port->number, status);
531 wake_up_interruptible(&port->open_wait);
532 else if (tty && !C_CLOCAL(tty))
535 EXPORT_SYMBOL_GPL(usb_serial_handle_dcd_change);
537 int usb_serial_generic_resume(struct usb_serial *serial)
539 struct usb_serial_port *port;
542 for (i = 0; i < serial->num_ports; i++) {
543 port = serial->port[i];
544 if (!test_bit(ASYNCB_INITIALIZED, &port->port.flags))
547 if (port->bulk_in_size) {
548 r = usb_serial_generic_submit_read_urbs(port,
554 if (port->bulk_out_size) {
555 r = usb_serial_generic_write_start(port);
563 EXPORT_SYMBOL_GPL(usb_serial_generic_resume);
565 void usb_serial_generic_disconnect(struct usb_serial *serial)
571 /* stop reads and writes on all ports */
572 for (i = 0; i < serial->num_ports; ++i)
573 generic_cleanup(serial->port[i]);
575 EXPORT_SYMBOL_GPL(usb_serial_generic_disconnect);
577 void usb_serial_generic_release(struct usb_serial *serial)