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 /* clear the throttle flags */
121 spin_lock_irqsave(&port->lock, flags);
123 port->throttle_req = 0;
124 spin_unlock_irqrestore(&port->lock, flags);
126 /* if we have a bulk endpoint, start reading from it */
127 if (port->bulk_in_size)
128 result = usb_serial_generic_submit_read_urbs(port, GFP_KERNEL);
132 EXPORT_SYMBOL_GPL(usb_serial_generic_open);
134 static void generic_cleanup(struct usb_serial_port *port)
136 struct usb_serial *serial = port->serial;
141 /* shutdown any bulk transfers that might be going on */
142 if (port->bulk_out_size) {
143 for (i = 0; i < ARRAY_SIZE(port->write_urbs); ++i)
144 usb_kill_urb(port->write_urbs[i]);
146 spin_lock_irqsave(&port->lock, flags);
147 kfifo_reset_out(&port->write_fifo);
148 spin_unlock_irqrestore(&port->lock, flags);
150 if (port->bulk_in_size) {
151 for (i = 0; i < ARRAY_SIZE(port->read_urbs); ++i)
152 usb_kill_urb(port->read_urbs[i]);
157 void usb_serial_generic_close(struct usb_serial_port *port)
159 generic_cleanup(port);
161 EXPORT_SYMBOL_GPL(usb_serial_generic_close);
163 int usb_serial_generic_prepare_write_buffer(struct usb_serial_port *port,
164 void *dest, size_t size)
166 return kfifo_out_locked(&port->write_fifo, dest, size, &port->lock);
170 * usb_serial_generic_write_start - kick off an URB write
171 * @port: Pointer to the &struct usb_serial_port data
173 * Returns zero on success, or a negative errno value
175 static int usb_serial_generic_write_start(struct usb_serial_port *port)
182 if (test_and_set_bit_lock(USB_SERIAL_WRITE_BUSY, &port->flags))
185 spin_lock_irqsave(&port->lock, flags);
186 if (!port->write_urbs_free || !kfifo_len(&port->write_fifo)) {
187 clear_bit_unlock(USB_SERIAL_WRITE_BUSY, &port->flags);
188 spin_unlock_irqrestore(&port->lock, flags);
191 i = (int)find_first_bit(&port->write_urbs_free,
192 ARRAY_SIZE(port->write_urbs));
193 spin_unlock_irqrestore(&port->lock, flags);
195 urb = port->write_urbs[i];
196 count = port->serial->type->prepare_write_buffer(port,
197 urb->transfer_buffer,
198 port->bulk_out_size);
199 urb->transfer_buffer_length = count;
200 usb_serial_debug_data(debug, &port->dev, __func__, count,
201 urb->transfer_buffer);
202 spin_lock_irqsave(&port->lock, flags);
203 port->tx_bytes += count;
204 spin_unlock_irqrestore(&port->lock, flags);
206 clear_bit(i, &port->write_urbs_free);
207 result = usb_submit_urb(urb, GFP_ATOMIC);
209 dev_err_console(port, "%s - error submitting urb: %d\n",
211 set_bit(i, &port->write_urbs_free);
212 spin_lock_irqsave(&port->lock, flags);
213 port->tx_bytes -= count;
214 spin_unlock_irqrestore(&port->lock, flags);
216 clear_bit_unlock(USB_SERIAL_WRITE_BUSY, &port->flags);
220 /* Try sending off another urb, unless in irq context (in which case
221 * there will be no free urb). */
225 clear_bit_unlock(USB_SERIAL_WRITE_BUSY, &port->flags);
231 * usb_serial_generic_write - generic write function for serial USB devices
232 * @tty: Pointer to &struct tty_struct for the device
233 * @port: Pointer to the &usb_serial_port structure for the device
234 * @buf: Pointer to the data to write
235 * @count: Number of bytes to write
237 * Returns the number of characters actually written, which may be anything
238 * from zero to @count. If an error occurs, it returns the negative errno
241 int usb_serial_generic_write(struct tty_struct *tty,
242 struct usb_serial_port *port, const unsigned char *buf, int count)
246 /* only do something if we have a bulk out endpoint */
247 if (!port->bulk_out_size)
253 count = kfifo_in_locked(&port->write_fifo, buf, count, &port->lock);
254 result = usb_serial_generic_write_start(port);
260 EXPORT_SYMBOL_GPL(usb_serial_generic_write);
262 int usb_serial_generic_write_room(struct tty_struct *tty)
264 struct usb_serial_port *port = tty->driver_data;
268 if (!port->bulk_out_size)
271 spin_lock_irqsave(&port->lock, flags);
272 room = kfifo_avail(&port->write_fifo);
273 spin_unlock_irqrestore(&port->lock, flags);
275 dbg("%s - returns %d", __func__, room);
279 int usb_serial_generic_chars_in_buffer(struct tty_struct *tty)
281 struct usb_serial_port *port = tty->driver_data;
285 if (!port->bulk_out_size)
288 spin_lock_irqsave(&port->lock, flags);
289 chars = kfifo_len(&port->write_fifo) + port->tx_bytes;
290 spin_unlock_irqrestore(&port->lock, flags);
292 dbg("%s - returns %d", __func__, chars);
296 static int usb_serial_generic_submit_read_urb(struct usb_serial_port *port,
297 int index, gfp_t mem_flags)
301 if (!test_and_clear_bit(index, &port->read_urbs_free))
304 dbg("%s - port %d, urb %d", __func__, port->number, index);
306 res = usb_submit_urb(port->read_urbs[index], mem_flags);
310 "%s - usb_submit_urb failed: %d\n",
313 set_bit(index, &port->read_urbs_free);
320 int usb_serial_generic_submit_read_urbs(struct usb_serial_port *port,
326 for (i = 0; i < ARRAY_SIZE(port->read_urbs); ++i) {
327 res = usb_serial_generic_submit_read_urb(port, i, mem_flags);
335 usb_kill_urb(port->read_urbs[i]);
339 EXPORT_SYMBOL_GPL(usb_serial_generic_submit_read_urbs);
341 void usb_serial_generic_process_read_urb(struct urb *urb)
343 struct usb_serial_port *port = urb->context;
344 struct tty_struct *tty;
345 char *ch = (char *)urb->transfer_buffer;
348 if (!urb->actual_length)
351 tty = tty_port_tty_get(&port->port);
355 /* The per character mucking around with sysrq path it too slow for
356 stuff like 3G modems, so shortcircuit it in the 99.9999999% of cases
357 where the USB serial is not a console anyway */
358 if (!port->port.console || !port->sysrq)
359 tty_insert_flip_string(tty, ch, urb->actual_length);
361 for (i = 0; i < urb->actual_length; i++, ch++) {
362 if (!usb_serial_handle_sysrq_char(port, *ch))
363 tty_insert_flip_char(tty, *ch, TTY_NORMAL);
366 tty_flip_buffer_push(tty);
369 EXPORT_SYMBOL_GPL(usb_serial_generic_process_read_urb);
371 void usb_serial_generic_read_bulk_callback(struct urb *urb)
373 struct usb_serial_port *port = urb->context;
374 unsigned char *data = urb->transfer_buffer;
378 for (i = 0; i < ARRAY_SIZE(port->read_urbs); ++i) {
379 if (urb == port->read_urbs[i])
382 set_bit(i, &port->read_urbs_free);
384 dbg("%s - port %d, urb %d, len %d", __func__, port->number, i,
387 dbg("%s - non-zero urb status: %d", __func__, urb->status);
391 usb_serial_debug_data(debug, &port->dev, __func__,
392 urb->actual_length, data);
393 port->serial->type->process_read_urb(urb);
395 /* Throttle the device if requested by tty */
396 spin_lock_irqsave(&port->lock, flags);
397 port->throttled = port->throttle_req;
398 if (!port->throttled) {
399 spin_unlock_irqrestore(&port->lock, flags);
400 usb_serial_generic_submit_read_urb(port, i, GFP_ATOMIC);
402 spin_unlock_irqrestore(&port->lock, flags);
404 EXPORT_SYMBOL_GPL(usb_serial_generic_read_bulk_callback);
406 void usb_serial_generic_write_bulk_callback(struct urb *urb)
409 struct usb_serial_port *port = urb->context;
410 int status = urb->status;
413 for (i = 0; i < ARRAY_SIZE(port->write_urbs); ++i)
414 if (port->write_urbs[i] == urb)
417 spin_lock_irqsave(&port->lock, flags);
418 port->tx_bytes -= urb->transfer_buffer_length;
419 set_bit(i, &port->write_urbs_free);
420 spin_unlock_irqrestore(&port->lock, flags);
423 dbg("%s - non-zero urb status: %d", __func__, status);
425 spin_lock_irqsave(&port->lock, flags);
426 kfifo_reset_out(&port->write_fifo);
427 spin_unlock_irqrestore(&port->lock, flags);
429 usb_serial_generic_write_start(port);
432 usb_serial_port_softint(port);
434 EXPORT_SYMBOL_GPL(usb_serial_generic_write_bulk_callback);
436 void usb_serial_generic_throttle(struct tty_struct *tty)
438 struct usb_serial_port *port = tty->driver_data;
441 /* Set the throttle request flag. It will be picked up
442 * by usb_serial_generic_read_bulk_callback(). */
443 spin_lock_irqsave(&port->lock, flags);
444 port->throttle_req = 1;
445 spin_unlock_irqrestore(&port->lock, flags);
447 EXPORT_SYMBOL_GPL(usb_serial_generic_throttle);
449 void usb_serial_generic_unthrottle(struct tty_struct *tty)
451 struct usb_serial_port *port = tty->driver_data;
454 /* Clear the throttle flags */
455 spin_lock_irq(&port->lock);
456 was_throttled = port->throttled;
457 port->throttled = port->throttle_req = 0;
458 spin_unlock_irq(&port->lock);
461 usb_serial_generic_submit_read_urbs(port, GFP_KERNEL);
463 EXPORT_SYMBOL_GPL(usb_serial_generic_unthrottle);
465 #ifdef CONFIG_MAGIC_SYSRQ
466 int usb_serial_handle_sysrq_char(struct usb_serial_port *port, unsigned int ch)
468 if (port->sysrq && port->port.console) {
469 if (ch && time_before(jiffies, port->sysrq)) {
479 int usb_serial_handle_sysrq_char(struct usb_serial_port *port, unsigned int ch)
484 EXPORT_SYMBOL_GPL(usb_serial_handle_sysrq_char);
486 int usb_serial_handle_break(struct usb_serial_port *port)
489 port->sysrq = jiffies + HZ*5;
495 EXPORT_SYMBOL_GPL(usb_serial_handle_break);
498 * usb_serial_handle_dcd_change - handle a change of carrier detect state
499 * @port: usb_serial_port structure for the open port
500 * @tty: tty_struct structure for the port
501 * @status: new carrier detect status, nonzero if active
503 void usb_serial_handle_dcd_change(struct usb_serial_port *usb_port,
504 struct tty_struct *tty, unsigned int status)
506 struct tty_port *port = &usb_port->port;
508 dbg("%s - port %d, status %d", __func__, usb_port->number, status);
511 wake_up_interruptible(&port->open_wait);
512 else if (tty && !C_CLOCAL(tty))
515 EXPORT_SYMBOL_GPL(usb_serial_handle_dcd_change);
517 int usb_serial_generic_resume(struct usb_serial *serial)
519 struct usb_serial_port *port;
522 for (i = 0; i < serial->num_ports; i++) {
523 port = serial->port[i];
524 if (!test_bit(ASYNCB_INITIALIZED, &port->port.flags))
527 if (port->bulk_in_size) {
528 r = usb_serial_generic_submit_read_urbs(port,
534 if (port->bulk_out_size) {
535 r = usb_serial_generic_write_start(port);
543 EXPORT_SYMBOL_GPL(usb_serial_generic_resume);
545 void usb_serial_generic_disconnect(struct usb_serial *serial)
549 /* stop reads and writes on all ports */
550 for (i = 0; i < serial->num_ports; ++i)
551 generic_cleanup(serial->port[i]);
553 EXPORT_SYMBOL_GPL(usb_serial_generic_disconnect);
555 void usb_serial_generic_release(struct usb_serial *serial)