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 char *ch = (char *)urb->transfer_buffer;
319 if (!urb->actual_length)
322 /* The per character mucking around with sysrq path it too slow for
323 stuff like 3G modems, so shortcircuit it in the 99.9999999% of cases
324 where the USB serial is not a console anyway */
325 if (!port->port.console || !port->sysrq)
326 tty_insert_flip_string(&port->port, ch, urb->actual_length);
328 for (i = 0; i < urb->actual_length; i++, ch++) {
329 if (!usb_serial_handle_sysrq_char(port, *ch))
330 tty_insert_flip_char(&port->port, *ch, TTY_NORMAL);
333 tty_flip_buffer_push(&port->port);
335 EXPORT_SYMBOL_GPL(usb_serial_generic_process_read_urb);
337 void usb_serial_generic_read_bulk_callback(struct urb *urb)
339 struct usb_serial_port *port = urb->context;
340 unsigned char *data = urb->transfer_buffer;
344 for (i = 0; i < ARRAY_SIZE(port->read_urbs); ++i) {
345 if (urb == port->read_urbs[i])
348 set_bit(i, &port->read_urbs_free);
350 dev_dbg(&port->dev, "%s - port %d, urb %d, len %d\n",
351 __func__, port->number, i, urb->actual_length);
354 dev_dbg(&port->dev, "%s - non-zero urb status: %d\n",
355 __func__, urb->status);
359 usb_serial_debug_data(&port->dev, __func__, urb->actual_length, data);
360 port->serial->type->process_read_urb(urb);
362 /* Throttle the device if requested by tty */
363 spin_lock_irqsave(&port->lock, flags);
364 port->throttled = port->throttle_req;
365 if (!port->throttled) {
366 spin_unlock_irqrestore(&port->lock, flags);
367 usb_serial_generic_submit_read_urb(port, i, GFP_ATOMIC);
369 spin_unlock_irqrestore(&port->lock, flags);
371 EXPORT_SYMBOL_GPL(usb_serial_generic_read_bulk_callback);
373 void usb_serial_generic_write_bulk_callback(struct urb *urb)
376 struct usb_serial_port *port = urb->context;
377 int status = urb->status;
380 for (i = 0; i < ARRAY_SIZE(port->write_urbs); ++i)
381 if (port->write_urbs[i] == urb)
384 spin_lock_irqsave(&port->lock, flags);
385 port->tx_bytes -= urb->transfer_buffer_length;
386 set_bit(i, &port->write_urbs_free);
387 spin_unlock_irqrestore(&port->lock, flags);
390 dev_dbg(&port->dev, "%s - non-zero urb status: %d\n",
393 spin_lock_irqsave(&port->lock, flags);
394 kfifo_reset_out(&port->write_fifo);
395 spin_unlock_irqrestore(&port->lock, flags);
397 usb_serial_generic_write_start(port);
400 usb_serial_port_softint(port);
402 EXPORT_SYMBOL_GPL(usb_serial_generic_write_bulk_callback);
404 void usb_serial_generic_throttle(struct tty_struct *tty)
406 struct usb_serial_port *port = tty->driver_data;
409 /* Set the throttle request flag. It will be picked up
410 * by usb_serial_generic_read_bulk_callback(). */
411 spin_lock_irqsave(&port->lock, flags);
412 port->throttle_req = 1;
413 spin_unlock_irqrestore(&port->lock, flags);
415 EXPORT_SYMBOL_GPL(usb_serial_generic_throttle);
417 void usb_serial_generic_unthrottle(struct tty_struct *tty)
419 struct usb_serial_port *port = tty->driver_data;
422 /* Clear the throttle flags */
423 spin_lock_irq(&port->lock);
424 was_throttled = port->throttled;
425 port->throttled = port->throttle_req = 0;
426 spin_unlock_irq(&port->lock);
429 usb_serial_generic_submit_read_urbs(port, GFP_KERNEL);
431 EXPORT_SYMBOL_GPL(usb_serial_generic_unthrottle);
433 #ifdef CONFIG_MAGIC_SYSRQ
434 int usb_serial_handle_sysrq_char(struct usb_serial_port *port, unsigned int ch)
436 if (port->sysrq && port->port.console) {
437 if (ch && time_before(jiffies, port->sysrq)) {
447 int usb_serial_handle_sysrq_char(struct usb_serial_port *port, unsigned int ch)
452 EXPORT_SYMBOL_GPL(usb_serial_handle_sysrq_char);
454 int usb_serial_handle_break(struct usb_serial_port *port)
457 port->sysrq = jiffies + HZ*5;
463 EXPORT_SYMBOL_GPL(usb_serial_handle_break);
466 * usb_serial_handle_dcd_change - handle a change of carrier detect state
467 * @port: usb_serial_port structure for the open port
468 * @tty: tty_struct structure for the port
469 * @status: new carrier detect status, nonzero if active
471 void usb_serial_handle_dcd_change(struct usb_serial_port *usb_port,
472 struct tty_struct *tty, unsigned int status)
474 struct tty_port *port = &usb_port->port;
476 dev_dbg(&usb_port->dev, "%s - port %d, status %d\n", __func__,
477 usb_port->number, status);
480 wake_up_interruptible(&port->open_wait);
481 else if (tty && !C_CLOCAL(tty))
484 EXPORT_SYMBOL_GPL(usb_serial_handle_dcd_change);
486 int usb_serial_generic_resume(struct usb_serial *serial)
488 struct usb_serial_port *port;
491 for (i = 0; i < serial->num_ports; i++) {
492 port = serial->port[i];
493 if (!test_bit(ASYNCB_INITIALIZED, &port->port.flags))
496 if (port->bulk_in_size) {
497 r = usb_serial_generic_submit_read_urbs(port,
503 if (port->bulk_out_size) {
504 r = usb_serial_generic_write_start(port);
512 EXPORT_SYMBOL_GPL(usb_serial_generic_resume);
514 void usb_serial_generic_disconnect(struct usb_serial *serial)
518 /* stop reads and writes on all ports */
519 for (i = 0; i < serial->num_ports; ++i)
520 generic_cleanup(serial->port[i]);
522 EXPORT_SYMBOL_GPL(usb_serial_generic_disconnect);
524 void usb_serial_generic_release(struct usb_serial *serial)