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>
27 #ifdef CONFIG_USB_SERIAL_GENERIC
29 static __u16 vendor = 0x05f9;
30 static __u16 product = 0xffff;
32 module_param(vendor, ushort, 0);
33 MODULE_PARM_DESC(vendor, "User specified USB idVendor");
35 module_param(product, ushort, 0);
36 MODULE_PARM_DESC(product, "User specified USB idProduct");
38 static struct usb_device_id generic_device_ids[2]; /* Initially all zeroes. */
40 /* All of the device info needed for the Generic Serial Converter */
41 struct usb_serial_driver usb_serial_generic_device = {
46 .id_table = generic_device_ids,
48 .disconnect = usb_serial_generic_disconnect,
49 .release = usb_serial_generic_release,
50 .throttle = usb_serial_generic_throttle,
51 .unthrottle = usb_serial_generic_unthrottle,
52 .resume = usb_serial_generic_resume,
55 static struct usb_serial_driver * const serial_drivers[] = {
56 &usb_serial_generic_device, NULL
61 int usb_serial_generic_register(void)
65 #ifdef CONFIG_USB_SERIAL_GENERIC
66 generic_device_ids[0].idVendor = vendor;
67 generic_device_ids[0].idProduct = product;
68 generic_device_ids[0].match_flags =
69 USB_DEVICE_ID_MATCH_VENDOR | USB_DEVICE_ID_MATCH_PRODUCT;
71 /* register our generic driver with ourselves */
72 retval = usb_serial_register_drivers(serial_drivers,
73 "usbserial_generic", generic_device_ids);
78 void usb_serial_generic_deregister(void)
80 #ifdef CONFIG_USB_SERIAL_GENERIC
81 /* remove our generic driver */
82 usb_serial_deregister_drivers(serial_drivers);
86 int usb_serial_generic_open(struct tty_struct *tty, struct usb_serial_port *port)
91 /* clear the throttle flags */
92 spin_lock_irqsave(&port->lock, flags);
94 port->throttle_req = 0;
95 spin_unlock_irqrestore(&port->lock, flags);
97 /* if we have a bulk endpoint, start reading from it */
98 if (port->bulk_in_size)
99 result = usb_serial_generic_submit_read_urbs(port, GFP_KERNEL);
103 EXPORT_SYMBOL_GPL(usb_serial_generic_open);
105 static void generic_cleanup(struct usb_serial_port *port)
107 struct usb_serial *serial = port->serial;
112 /* shutdown any bulk transfers that might be going on */
113 if (port->bulk_out_size) {
114 for (i = 0; i < ARRAY_SIZE(port->write_urbs); ++i)
115 usb_kill_urb(port->write_urbs[i]);
117 spin_lock_irqsave(&port->lock, flags);
118 kfifo_reset_out(&port->write_fifo);
119 spin_unlock_irqrestore(&port->lock, flags);
121 if (port->bulk_in_size) {
122 for (i = 0; i < ARRAY_SIZE(port->read_urbs); ++i)
123 usb_kill_urb(port->read_urbs[i]);
128 void usb_serial_generic_close(struct usb_serial_port *port)
130 generic_cleanup(port);
132 EXPORT_SYMBOL_GPL(usb_serial_generic_close);
134 int usb_serial_generic_prepare_write_buffer(struct usb_serial_port *port,
135 void *dest, size_t size)
137 return kfifo_out_locked(&port->write_fifo, dest, size, &port->lock);
141 * usb_serial_generic_write_start - kick off an URB write
142 * @port: Pointer to the &struct usb_serial_port data
144 * Returns zero on success, or a negative errno value
146 static int usb_serial_generic_write_start(struct usb_serial_port *port)
153 if (test_and_set_bit_lock(USB_SERIAL_WRITE_BUSY, &port->flags))
156 spin_lock_irqsave(&port->lock, flags);
157 if (!port->write_urbs_free || !kfifo_len(&port->write_fifo)) {
158 clear_bit_unlock(USB_SERIAL_WRITE_BUSY, &port->flags);
159 spin_unlock_irqrestore(&port->lock, flags);
162 i = (int)find_first_bit(&port->write_urbs_free,
163 ARRAY_SIZE(port->write_urbs));
164 spin_unlock_irqrestore(&port->lock, flags);
166 urb = port->write_urbs[i];
167 count = port->serial->type->prepare_write_buffer(port,
168 urb->transfer_buffer,
169 port->bulk_out_size);
170 urb->transfer_buffer_length = count;
171 usb_serial_debug_data(&port->dev, __func__, count, urb->transfer_buffer);
172 spin_lock_irqsave(&port->lock, flags);
173 port->tx_bytes += count;
174 spin_unlock_irqrestore(&port->lock, flags);
176 clear_bit(i, &port->write_urbs_free);
177 result = usb_submit_urb(urb, GFP_ATOMIC);
179 dev_err_console(port, "%s - error submitting urb: %d\n",
181 set_bit(i, &port->write_urbs_free);
182 spin_lock_irqsave(&port->lock, flags);
183 port->tx_bytes -= count;
184 spin_unlock_irqrestore(&port->lock, flags);
186 clear_bit_unlock(USB_SERIAL_WRITE_BUSY, &port->flags);
190 /* Try sending off another urb, unless in irq context (in which case
191 * there will be no free urb). */
195 clear_bit_unlock(USB_SERIAL_WRITE_BUSY, &port->flags);
201 * usb_serial_generic_write - generic write function for serial USB devices
202 * @tty: Pointer to &struct tty_struct for the device
203 * @port: Pointer to the &usb_serial_port structure for the device
204 * @buf: Pointer to the data to write
205 * @count: Number of bytes to write
207 * Returns the number of characters actually written, which may be anything
208 * from zero to @count. If an error occurs, it returns the negative errno
211 int usb_serial_generic_write(struct tty_struct *tty,
212 struct usb_serial_port *port, const unsigned char *buf, int count)
216 /* only do something if we have a bulk out endpoint */
217 if (!port->bulk_out_size)
223 count = kfifo_in_locked(&port->write_fifo, buf, count, &port->lock);
224 result = usb_serial_generic_write_start(port);
230 EXPORT_SYMBOL_GPL(usb_serial_generic_write);
232 int usb_serial_generic_write_room(struct tty_struct *tty)
234 struct usb_serial_port *port = tty->driver_data;
238 if (!port->bulk_out_size)
241 spin_lock_irqsave(&port->lock, flags);
242 room = kfifo_avail(&port->write_fifo);
243 spin_unlock_irqrestore(&port->lock, flags);
245 dev_dbg(&port->dev, "%s - returns %d\n", __func__, room);
249 int usb_serial_generic_chars_in_buffer(struct tty_struct *tty)
251 struct usb_serial_port *port = tty->driver_data;
255 if (!port->bulk_out_size)
258 spin_lock_irqsave(&port->lock, flags);
259 chars = kfifo_len(&port->write_fifo) + port->tx_bytes;
260 spin_unlock_irqrestore(&port->lock, flags);
262 dev_dbg(&port->dev, "%s - returns %d\n", __func__, chars);
265 EXPORT_SYMBOL_GPL(usb_serial_generic_chars_in_buffer);
267 static int usb_serial_generic_submit_read_urb(struct usb_serial_port *port,
268 int index, gfp_t mem_flags)
272 if (!test_and_clear_bit(index, &port->read_urbs_free))
275 dev_dbg(&port->dev, "%s - port %d, urb %d\n", __func__,
276 port->number, index);
278 res = usb_submit_urb(port->read_urbs[index], mem_flags);
282 "%s - usb_submit_urb failed: %d\n",
285 set_bit(index, &port->read_urbs_free);
292 int usb_serial_generic_submit_read_urbs(struct usb_serial_port *port,
298 for (i = 0; i < ARRAY_SIZE(port->read_urbs); ++i) {
299 res = usb_serial_generic_submit_read_urb(port, i, mem_flags);
307 usb_kill_urb(port->read_urbs[i]);
311 EXPORT_SYMBOL_GPL(usb_serial_generic_submit_read_urbs);
313 void usb_serial_generic_process_read_urb(struct urb *urb)
315 struct usb_serial_port *port = urb->context;
316 struct tty_struct *tty;
317 char *ch = (char *)urb->transfer_buffer;
320 if (!urb->actual_length)
323 tty = tty_port_tty_get(&port->port);
327 /* The per character mucking around with sysrq path it too slow for
328 stuff like 3G modems, so shortcircuit it in the 99.9999999% of cases
329 where the USB serial is not a console anyway */
330 if (!port->port.console || !port->sysrq)
331 tty_insert_flip_string(tty, ch, urb->actual_length);
333 for (i = 0; i < urb->actual_length; i++, ch++) {
334 if (!usb_serial_handle_sysrq_char(port, *ch))
335 tty_insert_flip_char(tty, *ch, TTY_NORMAL);
338 tty_flip_buffer_push(tty);
341 EXPORT_SYMBOL_GPL(usb_serial_generic_process_read_urb);
343 void usb_serial_generic_read_bulk_callback(struct urb *urb)
345 struct usb_serial_port *port = urb->context;
346 unsigned char *data = urb->transfer_buffer;
350 for (i = 0; i < ARRAY_SIZE(port->read_urbs); ++i) {
351 if (urb == port->read_urbs[i])
354 set_bit(i, &port->read_urbs_free);
356 dev_dbg(&port->dev, "%s - port %d, urb %d, len %d\n",
357 __func__, port->number, i, urb->actual_length);
360 dev_dbg(&port->dev, "%s - non-zero urb status: %d\n",
361 __func__, urb->status);
365 usb_serial_debug_data(&port->dev, __func__, urb->actual_length, data);
366 port->serial->type->process_read_urb(urb);
368 /* Throttle the device if requested by tty */
369 spin_lock_irqsave(&port->lock, flags);
370 port->throttled = port->throttle_req;
371 if (!port->throttled) {
372 spin_unlock_irqrestore(&port->lock, flags);
373 usb_serial_generic_submit_read_urb(port, i, GFP_ATOMIC);
375 spin_unlock_irqrestore(&port->lock, flags);
377 EXPORT_SYMBOL_GPL(usb_serial_generic_read_bulk_callback);
379 void usb_serial_generic_write_bulk_callback(struct urb *urb)
382 struct usb_serial_port *port = urb->context;
383 int status = urb->status;
386 for (i = 0; i < ARRAY_SIZE(port->write_urbs); ++i)
387 if (port->write_urbs[i] == urb)
390 spin_lock_irqsave(&port->lock, flags);
391 port->tx_bytes -= urb->transfer_buffer_length;
392 set_bit(i, &port->write_urbs_free);
393 spin_unlock_irqrestore(&port->lock, flags);
396 dev_dbg(&port->dev, "%s - non-zero urb status: %d\n",
399 spin_lock_irqsave(&port->lock, flags);
400 kfifo_reset_out(&port->write_fifo);
401 spin_unlock_irqrestore(&port->lock, flags);
403 usb_serial_generic_write_start(port);
406 usb_serial_port_softint(port);
408 EXPORT_SYMBOL_GPL(usb_serial_generic_write_bulk_callback);
410 void usb_serial_generic_throttle(struct tty_struct *tty)
412 struct usb_serial_port *port = tty->driver_data;
415 /* Set the throttle request flag. It will be picked up
416 * by usb_serial_generic_read_bulk_callback(). */
417 spin_lock_irqsave(&port->lock, flags);
418 port->throttle_req = 1;
419 spin_unlock_irqrestore(&port->lock, flags);
421 EXPORT_SYMBOL_GPL(usb_serial_generic_throttle);
423 void usb_serial_generic_unthrottle(struct tty_struct *tty)
425 struct usb_serial_port *port = tty->driver_data;
428 /* Clear the throttle flags */
429 spin_lock_irq(&port->lock);
430 was_throttled = port->throttled;
431 port->throttled = port->throttle_req = 0;
432 spin_unlock_irq(&port->lock);
435 usb_serial_generic_submit_read_urbs(port, GFP_KERNEL);
437 EXPORT_SYMBOL_GPL(usb_serial_generic_unthrottle);
439 #ifdef CONFIG_MAGIC_SYSRQ
440 int usb_serial_handle_sysrq_char(struct usb_serial_port *port, unsigned int ch)
442 if (port->sysrq && port->port.console) {
443 if (ch && time_before(jiffies, port->sysrq)) {
453 int usb_serial_handle_sysrq_char(struct usb_serial_port *port, unsigned int ch)
458 EXPORT_SYMBOL_GPL(usb_serial_handle_sysrq_char);
460 int usb_serial_handle_break(struct usb_serial_port *port)
463 port->sysrq = jiffies + HZ*5;
469 EXPORT_SYMBOL_GPL(usb_serial_handle_break);
472 * usb_serial_handle_dcd_change - handle a change of carrier detect state
473 * @port: usb_serial_port structure for the open port
474 * @tty: tty_struct structure for the port
475 * @status: new carrier detect status, nonzero if active
477 void usb_serial_handle_dcd_change(struct usb_serial_port *usb_port,
478 struct tty_struct *tty, unsigned int status)
480 struct tty_port *port = &usb_port->port;
482 dev_dbg(&usb_port->dev, "%s - port %d, status %d\n", __func__,
483 usb_port->number, status);
486 wake_up_interruptible(&port->open_wait);
487 else if (tty && !C_CLOCAL(tty))
490 EXPORT_SYMBOL_GPL(usb_serial_handle_dcd_change);
492 int usb_serial_generic_resume(struct usb_serial *serial)
494 struct usb_serial_port *port;
497 for (i = 0; i < serial->num_ports; i++) {
498 port = serial->port[i];
499 if (!test_bit(ASYNCB_INITIALIZED, &port->port.flags))
502 if (port->bulk_in_size) {
503 r = usb_serial_generic_submit_read_urbs(port,
509 if (port->bulk_out_size) {
510 r = usb_serial_generic_write_start(port);
518 EXPORT_SYMBOL_GPL(usb_serial_generic_resume);
520 void usb_serial_generic_disconnect(struct usb_serial *serial)
524 /* stop reads and writes on all ports */
525 for (i = 0; i < serial->num_ports; ++i)
526 generic_cleanup(serial->port[i]);
528 EXPORT_SYMBOL_GPL(usb_serial_generic_disconnect);
530 void usb_serial_generic_release(struct usb_serial *serial)