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 __u16 vendor
= 0x05f9;
32 static __u16 product
= 0xffff;
34 module_param(vendor
, ushort
, 0);
35 MODULE_PARM_DESC(vendor
, "User specified USB idVendor");
37 module_param(product
, ushort
, 0);
38 MODULE_PARM_DESC(product
, "User specified USB idProduct");
40 static struct usb_device_id generic_device_ids
[2]; /* Initially all zeroes. */
42 /* we want to look at all devices, as the vendor/product id can change
43 * depending on the command line argument */
44 static const struct usb_device_id generic_serial_ids
[] = {
49 /* All of the device info needed for the Generic Serial Converter */
50 struct usb_serial_driver usb_serial_generic_device
= {
55 .id_table
= generic_device_ids
,
57 .disconnect
= usb_serial_generic_disconnect
,
58 .release
= usb_serial_generic_release
,
59 .throttle
= usb_serial_generic_throttle
,
60 .unthrottle
= usb_serial_generic_unthrottle
,
61 .resume
= usb_serial_generic_resume
,
64 static struct usb_serial_driver
* const serial_drivers
[] = {
65 &usb_serial_generic_device
, NULL
70 int usb_serial_generic_register(int _debug
)
75 #ifdef CONFIG_USB_SERIAL_GENERIC
76 generic_device_ids
[0].idVendor
= vendor
;
77 generic_device_ids
[0].idProduct
= product
;
78 generic_device_ids
[0].match_flags
=
79 USB_DEVICE_ID_MATCH_VENDOR
| USB_DEVICE_ID_MATCH_PRODUCT
;
81 /* register our generic driver with ourselves */
82 retval
= usb_serial_register_drivers(serial_drivers
, "usbserial_generic", generic_serial_ids
);
87 void usb_serial_generic_deregister(void)
89 #ifdef CONFIG_USB_SERIAL_GENERIC
90 /* remove our generic driver */
91 usb_serial_deregister_drivers(serial_drivers
);
95 int usb_serial_generic_open(struct tty_struct
*tty
, struct usb_serial_port
*port
)
100 /* clear the throttle flags */
101 spin_lock_irqsave(&port
->lock
, flags
);
103 port
->throttle_req
= 0;
104 spin_unlock_irqrestore(&port
->lock
, flags
);
106 /* if we have a bulk endpoint, start reading from it */
107 if (port
->bulk_in_size
)
108 result
= usb_serial_generic_submit_read_urbs(port
, GFP_KERNEL
);
112 EXPORT_SYMBOL_GPL(usb_serial_generic_open
);
114 static void generic_cleanup(struct usb_serial_port
*port
)
116 struct usb_serial
*serial
= port
->serial
;
121 /* shutdown any bulk transfers that might be going on */
122 if (port
->bulk_out_size
) {
123 for (i
= 0; i
< ARRAY_SIZE(port
->write_urbs
); ++i
)
124 usb_kill_urb(port
->write_urbs
[i
]);
126 spin_lock_irqsave(&port
->lock
, flags
);
127 kfifo_reset_out(&port
->write_fifo
);
128 spin_unlock_irqrestore(&port
->lock
, flags
);
130 if (port
->bulk_in_size
) {
131 for (i
= 0; i
< ARRAY_SIZE(port
->read_urbs
); ++i
)
132 usb_kill_urb(port
->read_urbs
[i
]);
137 void usb_serial_generic_close(struct usb_serial_port
*port
)
139 generic_cleanup(port
);
141 EXPORT_SYMBOL_GPL(usb_serial_generic_close
);
143 int usb_serial_generic_prepare_write_buffer(struct usb_serial_port
*port
,
144 void *dest
, size_t size
)
146 return kfifo_out_locked(&port
->write_fifo
, dest
, size
, &port
->lock
);
150 * usb_serial_generic_write_start - kick off an URB write
151 * @port: Pointer to the &struct usb_serial_port data
153 * Returns zero on success, or a negative errno value
155 static int usb_serial_generic_write_start(struct usb_serial_port
*port
)
162 if (test_and_set_bit_lock(USB_SERIAL_WRITE_BUSY
, &port
->flags
))
165 spin_lock_irqsave(&port
->lock
, flags
);
166 if (!port
->write_urbs_free
|| !kfifo_len(&port
->write_fifo
)) {
167 clear_bit_unlock(USB_SERIAL_WRITE_BUSY
, &port
->flags
);
168 spin_unlock_irqrestore(&port
->lock
, flags
);
171 i
= (int)find_first_bit(&port
->write_urbs_free
,
172 ARRAY_SIZE(port
->write_urbs
));
173 spin_unlock_irqrestore(&port
->lock
, flags
);
175 urb
= port
->write_urbs
[i
];
176 count
= port
->serial
->type
->prepare_write_buffer(port
,
177 urb
->transfer_buffer
,
178 port
->bulk_out_size
);
179 urb
->transfer_buffer_length
= count
;
180 usb_serial_debug_data(debug
, &port
->dev
, __func__
, count
,
181 urb
->transfer_buffer
);
182 spin_lock_irqsave(&port
->lock
, flags
);
183 port
->tx_bytes
+= count
;
184 spin_unlock_irqrestore(&port
->lock
, flags
);
186 clear_bit(i
, &port
->write_urbs_free
);
187 result
= usb_submit_urb(urb
, GFP_ATOMIC
);
189 dev_err_console(port
, "%s - error submitting urb: %d\n",
191 set_bit(i
, &port
->write_urbs_free
);
192 spin_lock_irqsave(&port
->lock
, flags
);
193 port
->tx_bytes
-= count
;
194 spin_unlock_irqrestore(&port
->lock
, flags
);
196 clear_bit_unlock(USB_SERIAL_WRITE_BUSY
, &port
->flags
);
200 /* Try sending off another urb, unless in irq context (in which case
201 * there will be no free urb). */
205 clear_bit_unlock(USB_SERIAL_WRITE_BUSY
, &port
->flags
);
211 * usb_serial_generic_write - generic write function for serial USB devices
212 * @tty: Pointer to &struct tty_struct for the device
213 * @port: Pointer to the &usb_serial_port structure for the device
214 * @buf: Pointer to the data to write
215 * @count: Number of bytes to write
217 * Returns the number of characters actually written, which may be anything
218 * from zero to @count. If an error occurs, it returns the negative errno
221 int usb_serial_generic_write(struct tty_struct
*tty
,
222 struct usb_serial_port
*port
, const unsigned char *buf
, int count
)
226 /* only do something if we have a bulk out endpoint */
227 if (!port
->bulk_out_size
)
233 count
= kfifo_in_locked(&port
->write_fifo
, buf
, count
, &port
->lock
);
234 result
= usb_serial_generic_write_start(port
);
240 EXPORT_SYMBOL_GPL(usb_serial_generic_write
);
242 int usb_serial_generic_write_room(struct tty_struct
*tty
)
244 struct usb_serial_port
*port
= tty
->driver_data
;
248 if (!port
->bulk_out_size
)
251 spin_lock_irqsave(&port
->lock
, flags
);
252 room
= kfifo_avail(&port
->write_fifo
);
253 spin_unlock_irqrestore(&port
->lock
, flags
);
255 dev_dbg(&port
->dev
, "%s - returns %d\n", __func__
, room
);
259 int usb_serial_generic_chars_in_buffer(struct tty_struct
*tty
)
261 struct usb_serial_port
*port
= tty
->driver_data
;
265 if (!port
->bulk_out_size
)
268 spin_lock_irqsave(&port
->lock
, flags
);
269 chars
= kfifo_len(&port
->write_fifo
) + port
->tx_bytes
;
270 spin_unlock_irqrestore(&port
->lock
, flags
);
272 dev_dbg(&port
->dev
, "%s - returns %d\n", __func__
, chars
);
276 static int usb_serial_generic_submit_read_urb(struct usb_serial_port
*port
,
277 int index
, gfp_t mem_flags
)
281 if (!test_and_clear_bit(index
, &port
->read_urbs_free
))
284 dev_dbg(&port
->dev
, "%s - port %d, urb %d\n", __func__
,
285 port
->number
, index
);
287 res
= usb_submit_urb(port
->read_urbs
[index
], mem_flags
);
291 "%s - usb_submit_urb failed: %d\n",
294 set_bit(index
, &port
->read_urbs_free
);
301 int usb_serial_generic_submit_read_urbs(struct usb_serial_port
*port
,
307 for (i
= 0; i
< ARRAY_SIZE(port
->read_urbs
); ++i
) {
308 res
= usb_serial_generic_submit_read_urb(port
, i
, mem_flags
);
316 usb_kill_urb(port
->read_urbs
[i
]);
320 EXPORT_SYMBOL_GPL(usb_serial_generic_submit_read_urbs
);
322 void usb_serial_generic_process_read_urb(struct urb
*urb
)
324 struct usb_serial_port
*port
= urb
->context
;
325 struct tty_struct
*tty
;
326 char *ch
= (char *)urb
->transfer_buffer
;
329 if (!urb
->actual_length
)
332 tty
= tty_port_tty_get(&port
->port
);
336 /* The per character mucking around with sysrq path it too slow for
337 stuff like 3G modems, so shortcircuit it in the 99.9999999% of cases
338 where the USB serial is not a console anyway */
339 if (!port
->port
.console
|| !port
->sysrq
)
340 tty_insert_flip_string(tty
, ch
, urb
->actual_length
);
342 for (i
= 0; i
< urb
->actual_length
; i
++, ch
++) {
343 if (!usb_serial_handle_sysrq_char(port
, *ch
))
344 tty_insert_flip_char(tty
, *ch
, TTY_NORMAL
);
347 tty_flip_buffer_push(tty
);
350 EXPORT_SYMBOL_GPL(usb_serial_generic_process_read_urb
);
352 void usb_serial_generic_read_bulk_callback(struct urb
*urb
)
354 struct usb_serial_port
*port
= urb
->context
;
355 unsigned char *data
= urb
->transfer_buffer
;
359 for (i
= 0; i
< ARRAY_SIZE(port
->read_urbs
); ++i
) {
360 if (urb
== port
->read_urbs
[i
])
363 set_bit(i
, &port
->read_urbs_free
);
365 dev_dbg(&port
->dev
, "%s - port %d, urb %d, len %d\n",
366 __func__
, port
->number
, i
, urb
->actual_length
);
369 dev_dbg(&port
->dev
, "%s - non-zero urb status: %d\n",
370 __func__
, urb
->status
);
374 usb_serial_debug_data(debug
, &port
->dev
, __func__
,
375 urb
->actual_length
, data
);
376 port
->serial
->type
->process_read_urb(urb
);
378 /* Throttle the device if requested by tty */
379 spin_lock_irqsave(&port
->lock
, flags
);
380 port
->throttled
= port
->throttle_req
;
381 if (!port
->throttled
) {
382 spin_unlock_irqrestore(&port
->lock
, flags
);
383 usb_serial_generic_submit_read_urb(port
, i
, GFP_ATOMIC
);
385 spin_unlock_irqrestore(&port
->lock
, flags
);
387 EXPORT_SYMBOL_GPL(usb_serial_generic_read_bulk_callback
);
389 void usb_serial_generic_write_bulk_callback(struct urb
*urb
)
392 struct usb_serial_port
*port
= urb
->context
;
393 int status
= urb
->status
;
396 for (i
= 0; i
< ARRAY_SIZE(port
->write_urbs
); ++i
)
397 if (port
->write_urbs
[i
] == urb
)
400 spin_lock_irqsave(&port
->lock
, flags
);
401 port
->tx_bytes
-= urb
->transfer_buffer_length
;
402 set_bit(i
, &port
->write_urbs_free
);
403 spin_unlock_irqrestore(&port
->lock
, flags
);
406 dev_dbg(&port
->dev
, "%s - non-zero urb status: %d\n",
409 spin_lock_irqsave(&port
->lock
, flags
);
410 kfifo_reset_out(&port
->write_fifo
);
411 spin_unlock_irqrestore(&port
->lock
, flags
);
413 usb_serial_generic_write_start(port
);
416 usb_serial_port_softint(port
);
418 EXPORT_SYMBOL_GPL(usb_serial_generic_write_bulk_callback
);
420 void usb_serial_generic_throttle(struct tty_struct
*tty
)
422 struct usb_serial_port
*port
= tty
->driver_data
;
425 /* Set the throttle request flag. It will be picked up
426 * by usb_serial_generic_read_bulk_callback(). */
427 spin_lock_irqsave(&port
->lock
, flags
);
428 port
->throttle_req
= 1;
429 spin_unlock_irqrestore(&port
->lock
, flags
);
431 EXPORT_SYMBOL_GPL(usb_serial_generic_throttle
);
433 void usb_serial_generic_unthrottle(struct tty_struct
*tty
)
435 struct usb_serial_port
*port
= tty
->driver_data
;
438 /* Clear the throttle flags */
439 spin_lock_irq(&port
->lock
);
440 was_throttled
= port
->throttled
;
441 port
->throttled
= port
->throttle_req
= 0;
442 spin_unlock_irq(&port
->lock
);
445 usb_serial_generic_submit_read_urbs(port
, GFP_KERNEL
);
447 EXPORT_SYMBOL_GPL(usb_serial_generic_unthrottle
);
449 #ifdef CONFIG_MAGIC_SYSRQ
450 int usb_serial_handle_sysrq_char(struct usb_serial_port
*port
, unsigned int ch
)
452 if (port
->sysrq
&& port
->port
.console
) {
453 if (ch
&& time_before(jiffies
, port
->sysrq
)) {
463 int usb_serial_handle_sysrq_char(struct usb_serial_port
*port
, unsigned int ch
)
468 EXPORT_SYMBOL_GPL(usb_serial_handle_sysrq_char
);
470 int usb_serial_handle_break(struct usb_serial_port
*port
)
473 port
->sysrq
= jiffies
+ HZ
*5;
479 EXPORT_SYMBOL_GPL(usb_serial_handle_break
);
482 * usb_serial_handle_dcd_change - handle a change of carrier detect state
483 * @port: usb_serial_port structure for the open port
484 * @tty: tty_struct structure for the port
485 * @status: new carrier detect status, nonzero if active
487 void usb_serial_handle_dcd_change(struct usb_serial_port
*usb_port
,
488 struct tty_struct
*tty
, unsigned int status
)
490 struct tty_port
*port
= &usb_port
->port
;
492 dev_dbg(&usb_port
->dev
, "%s - port %d, status %d\n", __func__
,
493 usb_port
->number
, status
);
496 wake_up_interruptible(&port
->open_wait
);
497 else if (tty
&& !C_CLOCAL(tty
))
500 EXPORT_SYMBOL_GPL(usb_serial_handle_dcd_change
);
502 int usb_serial_generic_resume(struct usb_serial
*serial
)
504 struct usb_serial_port
*port
;
507 for (i
= 0; i
< serial
->num_ports
; i
++) {
508 port
= serial
->port
[i
];
509 if (!test_bit(ASYNCB_INITIALIZED
, &port
->port
.flags
))
512 if (port
->bulk_in_size
) {
513 r
= usb_serial_generic_submit_read_urbs(port
,
519 if (port
->bulk_out_size
) {
520 r
= usb_serial_generic_write_start(port
);
528 EXPORT_SYMBOL_GPL(usb_serial_generic_resume
);
530 void usb_serial_generic_disconnect(struct usb_serial
*serial
)
534 /* stop reads and writes on all ports */
535 for (i
= 0; i
< serial
->num_ports
; ++i
)
536 generic_cleanup(serial
->port
[i
]);
538 EXPORT_SYMBOL_GPL(usb_serial_generic_disconnect
);
540 void usb_serial_generic_release(struct usb_serial
*serial
)