2 * USB Serial Converter Generic functions
4 * Copyright (C) 2010 - 2013 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 .throttle
= usb_serial_generic_throttle
,
49 .unthrottle
= usb_serial_generic_unthrottle
,
50 .resume
= usb_serial_generic_resume
,
53 static struct usb_serial_driver
* const serial_drivers
[] = {
54 &usb_serial_generic_device
, NULL
59 int usb_serial_generic_register(void)
63 #ifdef CONFIG_USB_SERIAL_GENERIC
64 generic_device_ids
[0].idVendor
= vendor
;
65 generic_device_ids
[0].idProduct
= product
;
66 generic_device_ids
[0].match_flags
=
67 USB_DEVICE_ID_MATCH_VENDOR
| USB_DEVICE_ID_MATCH_PRODUCT
;
69 /* register our generic driver with ourselves */
70 retval
= usb_serial_register_drivers(serial_drivers
,
71 "usbserial_generic", generic_device_ids
);
76 void usb_serial_generic_deregister(void)
78 #ifdef CONFIG_USB_SERIAL_GENERIC
79 /* remove our generic driver */
80 usb_serial_deregister_drivers(serial_drivers
);
84 int usb_serial_generic_open(struct tty_struct
*tty
, struct usb_serial_port
*port
)
89 /* clear the throttle flags */
90 spin_lock_irqsave(&port
->lock
, flags
);
92 port
->throttle_req
= 0;
93 spin_unlock_irqrestore(&port
->lock
, flags
);
95 /* if we have a bulk endpoint, start reading from it */
96 if (port
->bulk_in_size
)
97 result
= usb_serial_generic_submit_read_urbs(port
, GFP_KERNEL
);
101 EXPORT_SYMBOL_GPL(usb_serial_generic_open
);
103 void usb_serial_generic_close(struct usb_serial_port
*port
)
108 if (port
->bulk_out_size
) {
109 for (i
= 0; i
< ARRAY_SIZE(port
->write_urbs
); ++i
)
110 usb_kill_urb(port
->write_urbs
[i
]);
112 spin_lock_irqsave(&port
->lock
, flags
);
113 kfifo_reset_out(&port
->write_fifo
);
114 spin_unlock_irqrestore(&port
->lock
, flags
);
116 if (port
->bulk_in_size
) {
117 for (i
= 0; i
< ARRAY_SIZE(port
->read_urbs
); ++i
)
118 usb_kill_urb(port
->read_urbs
[i
]);
121 EXPORT_SYMBOL_GPL(usb_serial_generic_close
);
123 int usb_serial_generic_prepare_write_buffer(struct usb_serial_port
*port
,
124 void *dest
, size_t size
)
126 return kfifo_out_locked(&port
->write_fifo
, dest
, size
, &port
->lock
);
130 * usb_serial_generic_write_start - kick off an URB write
131 * @port: Pointer to the &struct usb_serial_port data
133 * Returns zero on success, or a negative errno value
135 static int usb_serial_generic_write_start(struct usb_serial_port
*port
)
142 if (test_and_set_bit_lock(USB_SERIAL_WRITE_BUSY
, &port
->flags
))
145 spin_lock_irqsave(&port
->lock
, flags
);
146 if (!port
->write_urbs_free
|| !kfifo_len(&port
->write_fifo
)) {
147 clear_bit_unlock(USB_SERIAL_WRITE_BUSY
, &port
->flags
);
148 spin_unlock_irqrestore(&port
->lock
, flags
);
151 i
= (int)find_first_bit(&port
->write_urbs_free
,
152 ARRAY_SIZE(port
->write_urbs
));
153 spin_unlock_irqrestore(&port
->lock
, flags
);
155 urb
= port
->write_urbs
[i
];
156 count
= port
->serial
->type
->prepare_write_buffer(port
,
157 urb
->transfer_buffer
,
158 port
->bulk_out_size
);
159 urb
->transfer_buffer_length
= count
;
160 usb_serial_debug_data(&port
->dev
, __func__
, count
, urb
->transfer_buffer
);
161 spin_lock_irqsave(&port
->lock
, flags
);
162 port
->tx_bytes
+= count
;
163 spin_unlock_irqrestore(&port
->lock
, flags
);
165 clear_bit(i
, &port
->write_urbs_free
);
166 result
= usb_submit_urb(urb
, GFP_ATOMIC
);
168 dev_err_console(port
, "%s - error submitting urb: %d\n",
170 set_bit(i
, &port
->write_urbs_free
);
171 spin_lock_irqsave(&port
->lock
, flags
);
172 port
->tx_bytes
-= count
;
173 spin_unlock_irqrestore(&port
->lock
, flags
);
175 clear_bit_unlock(USB_SERIAL_WRITE_BUSY
, &port
->flags
);
179 /* Try sending off another urb, unless in irq context (in which case
180 * there will be no free urb). */
184 clear_bit_unlock(USB_SERIAL_WRITE_BUSY
, &port
->flags
);
190 * usb_serial_generic_write - generic write function for serial USB devices
191 * @tty: Pointer to &struct tty_struct for the device
192 * @port: Pointer to the &usb_serial_port structure for the device
193 * @buf: Pointer to the data to write
194 * @count: Number of bytes to write
196 * Returns the number of characters actually written, which may be anything
197 * from zero to @count. If an error occurs, it returns the negative errno
200 int usb_serial_generic_write(struct tty_struct
*tty
,
201 struct usb_serial_port
*port
, const unsigned char *buf
, int count
)
205 /* only do something if we have a bulk out endpoint */
206 if (!port
->bulk_out_size
)
212 count
= kfifo_in_locked(&port
->write_fifo
, buf
, count
, &port
->lock
);
213 result
= usb_serial_generic_write_start(port
);
219 EXPORT_SYMBOL_GPL(usb_serial_generic_write
);
221 int usb_serial_generic_write_room(struct tty_struct
*tty
)
223 struct usb_serial_port
*port
= tty
->driver_data
;
227 if (!port
->bulk_out_size
)
230 spin_lock_irqsave(&port
->lock
, flags
);
231 room
= kfifo_avail(&port
->write_fifo
);
232 spin_unlock_irqrestore(&port
->lock
, flags
);
234 dev_dbg(&port
->dev
, "%s - returns %d\n", __func__
, room
);
238 int usb_serial_generic_chars_in_buffer(struct tty_struct
*tty
)
240 struct usb_serial_port
*port
= tty
->driver_data
;
244 if (!port
->bulk_out_size
)
247 spin_lock_irqsave(&port
->lock
, flags
);
248 chars
= kfifo_len(&port
->write_fifo
) + port
->tx_bytes
;
249 spin_unlock_irqrestore(&port
->lock
, flags
);
251 dev_dbg(&port
->dev
, "%s - returns %d\n", __func__
, chars
);
254 EXPORT_SYMBOL_GPL(usb_serial_generic_chars_in_buffer
);
256 static int usb_serial_generic_submit_read_urb(struct usb_serial_port
*port
,
257 int index
, gfp_t mem_flags
)
261 if (!test_and_clear_bit(index
, &port
->read_urbs_free
))
264 dev_dbg(&port
->dev
, "%s - urb %d\n", __func__
, index
);
266 res
= usb_submit_urb(port
->read_urbs
[index
], mem_flags
);
270 "%s - usb_submit_urb failed: %d\n",
273 set_bit(index
, &port
->read_urbs_free
);
280 int usb_serial_generic_submit_read_urbs(struct usb_serial_port
*port
,
286 for (i
= 0; i
< ARRAY_SIZE(port
->read_urbs
); ++i
) {
287 res
= usb_serial_generic_submit_read_urb(port
, i
, mem_flags
);
295 usb_kill_urb(port
->read_urbs
[i
]);
299 EXPORT_SYMBOL_GPL(usb_serial_generic_submit_read_urbs
);
301 void usb_serial_generic_process_read_urb(struct urb
*urb
)
303 struct usb_serial_port
*port
= urb
->context
;
304 char *ch
= (char *)urb
->transfer_buffer
;
307 if (!urb
->actual_length
)
310 /* The per character mucking around with sysrq path it too slow for
311 stuff like 3G modems, so shortcircuit it in the 99.9999999% of cases
312 where the USB serial is not a console anyway */
313 if (!port
->port
.console
|| !port
->sysrq
)
314 tty_insert_flip_string(&port
->port
, ch
, urb
->actual_length
);
316 for (i
= 0; i
< urb
->actual_length
; i
++, ch
++) {
317 if (!usb_serial_handle_sysrq_char(port
, *ch
))
318 tty_insert_flip_char(&port
->port
, *ch
, TTY_NORMAL
);
321 tty_flip_buffer_push(&port
->port
);
323 EXPORT_SYMBOL_GPL(usb_serial_generic_process_read_urb
);
325 void usb_serial_generic_read_bulk_callback(struct urb
*urb
)
327 struct usb_serial_port
*port
= urb
->context
;
328 unsigned char *data
= urb
->transfer_buffer
;
332 for (i
= 0; i
< ARRAY_SIZE(port
->read_urbs
); ++i
) {
333 if (urb
== port
->read_urbs
[i
])
336 set_bit(i
, &port
->read_urbs_free
);
338 dev_dbg(&port
->dev
, "%s - urb %d, len %d\n", __func__
, i
,
342 dev_dbg(&port
->dev
, "%s - non-zero urb status: %d\n",
343 __func__
, urb
->status
);
347 usb_serial_debug_data(&port
->dev
, __func__
, urb
->actual_length
, data
);
348 port
->serial
->type
->process_read_urb(urb
);
350 /* Throttle the device if requested by tty */
351 spin_lock_irqsave(&port
->lock
, flags
);
352 port
->throttled
= port
->throttle_req
;
353 if (!port
->throttled
) {
354 spin_unlock_irqrestore(&port
->lock
, flags
);
355 usb_serial_generic_submit_read_urb(port
, i
, GFP_ATOMIC
);
357 spin_unlock_irqrestore(&port
->lock
, flags
);
359 EXPORT_SYMBOL_GPL(usb_serial_generic_read_bulk_callback
);
361 void usb_serial_generic_write_bulk_callback(struct urb
*urb
)
364 struct usb_serial_port
*port
= urb
->context
;
365 int status
= urb
->status
;
368 for (i
= 0; i
< ARRAY_SIZE(port
->write_urbs
); ++i
)
369 if (port
->write_urbs
[i
] == urb
)
372 spin_lock_irqsave(&port
->lock
, flags
);
373 port
->tx_bytes
-= urb
->transfer_buffer_length
;
374 set_bit(i
, &port
->write_urbs_free
);
375 spin_unlock_irqrestore(&port
->lock
, flags
);
378 dev_dbg(&port
->dev
, "%s - non-zero urb status: %d\n",
381 spin_lock_irqsave(&port
->lock
, flags
);
382 kfifo_reset_out(&port
->write_fifo
);
383 spin_unlock_irqrestore(&port
->lock
, flags
);
385 usb_serial_generic_write_start(port
);
388 usb_serial_port_softint(port
);
390 EXPORT_SYMBOL_GPL(usb_serial_generic_write_bulk_callback
);
392 void usb_serial_generic_throttle(struct tty_struct
*tty
)
394 struct usb_serial_port
*port
= tty
->driver_data
;
397 /* Set the throttle request flag. It will be picked up
398 * by usb_serial_generic_read_bulk_callback(). */
399 spin_lock_irqsave(&port
->lock
, flags
);
400 port
->throttle_req
= 1;
401 spin_unlock_irqrestore(&port
->lock
, flags
);
403 EXPORT_SYMBOL_GPL(usb_serial_generic_throttle
);
405 void usb_serial_generic_unthrottle(struct tty_struct
*tty
)
407 struct usb_serial_port
*port
= tty
->driver_data
;
410 /* Clear the throttle flags */
411 spin_lock_irq(&port
->lock
);
412 was_throttled
= port
->throttled
;
413 port
->throttled
= port
->throttle_req
= 0;
414 spin_unlock_irq(&port
->lock
);
417 usb_serial_generic_submit_read_urbs(port
, GFP_KERNEL
);
419 EXPORT_SYMBOL_GPL(usb_serial_generic_unthrottle
);
421 static bool usb_serial_generic_msr_changed(struct tty_struct
*tty
,
422 unsigned long arg
, struct async_icount
*cprev
)
424 struct usb_serial_port
*port
= tty
->driver_data
;
425 struct async_icount cnow
;
430 * Use tty-port initialised flag to detect all hangups including the
431 * one generated at USB-device disconnect.
433 * FIXME: Remove hupping check once tty_port_hangup calls shutdown
434 * (which clears the initialised flag) before wake up.
436 if (test_bit(TTY_HUPPING
, &tty
->flags
))
438 if (!test_bit(ASYNCB_INITIALIZED
, &port
->port
.flags
))
441 spin_lock_irqsave(&port
->lock
, flags
);
442 cnow
= port
->icount
; /* atomic copy*/
443 spin_unlock_irqrestore(&port
->lock
, flags
);
445 ret
= ((arg
& TIOCM_RNG
) && (cnow
.rng
!= cprev
->rng
)) ||
446 ((arg
& TIOCM_DSR
) && (cnow
.dsr
!= cprev
->dsr
)) ||
447 ((arg
& TIOCM_CD
) && (cnow
.dcd
!= cprev
->dcd
)) ||
448 ((arg
& TIOCM_CTS
) && (cnow
.cts
!= cprev
->cts
));
455 int usb_serial_generic_tiocmiwait(struct tty_struct
*tty
, unsigned long arg
)
457 struct usb_serial_port
*port
= tty
->driver_data
;
458 struct async_icount cnow
;
462 spin_lock_irqsave(&port
->lock
, flags
);
463 cnow
= port
->icount
; /* atomic copy */
464 spin_unlock_irqrestore(&port
->lock
, flags
);
466 ret
= wait_event_interruptible(port
->port
.delta_msr_wait
,
467 usb_serial_generic_msr_changed(tty
, arg
, &cnow
));
469 if (test_bit(TTY_HUPPING
, &tty
->flags
))
471 if (!test_bit(ASYNCB_INITIALIZED
, &port
->port
.flags
))
477 EXPORT_SYMBOL_GPL(usb_serial_generic_tiocmiwait
);
479 int usb_serial_generic_get_icount(struct tty_struct
*tty
,
480 struct serial_icounter_struct
*icount
)
482 struct usb_serial_port
*port
= tty
->driver_data
;
483 struct async_icount cnow
;
486 spin_lock_irqsave(&port
->lock
, flags
);
487 cnow
= port
->icount
; /* atomic copy */
488 spin_unlock_irqrestore(&port
->lock
, flags
);
490 icount
->cts
= cnow
.cts
;
491 icount
->dsr
= cnow
.dsr
;
492 icount
->rng
= cnow
.rng
;
493 icount
->dcd
= cnow
.dcd
;
494 icount
->tx
= cnow
.tx
;
495 icount
->rx
= cnow
.rx
;
496 icount
->frame
= cnow
.frame
;
497 icount
->parity
= cnow
.parity
;
498 icount
->overrun
= cnow
.overrun
;
499 icount
->brk
= cnow
.brk
;
500 icount
->buf_overrun
= cnow
.buf_overrun
;
504 EXPORT_SYMBOL_GPL(usb_serial_generic_get_icount
);
506 #ifdef CONFIG_MAGIC_SYSRQ
507 int usb_serial_handle_sysrq_char(struct usb_serial_port
*port
, unsigned int ch
)
509 if (port
->sysrq
&& port
->port
.console
) {
510 if (ch
&& time_before(jiffies
, port
->sysrq
)) {
520 int usb_serial_handle_sysrq_char(struct usb_serial_port
*port
, unsigned int ch
)
525 EXPORT_SYMBOL_GPL(usb_serial_handle_sysrq_char
);
527 int usb_serial_handle_break(struct usb_serial_port
*port
)
530 port
->sysrq
= jiffies
+ HZ
*5;
536 EXPORT_SYMBOL_GPL(usb_serial_handle_break
);
539 * usb_serial_handle_dcd_change - handle a change of carrier detect state
540 * @port: usb_serial_port structure for the open port
541 * @tty: tty_struct structure for the port
542 * @status: new carrier detect status, nonzero if active
544 void usb_serial_handle_dcd_change(struct usb_serial_port
*usb_port
,
545 struct tty_struct
*tty
, unsigned int status
)
547 struct tty_port
*port
= &usb_port
->port
;
549 dev_dbg(&usb_port
->dev
, "%s - status %d\n", __func__
, status
);
552 wake_up_interruptible(&port
->open_wait
);
553 else if (tty
&& !C_CLOCAL(tty
))
556 EXPORT_SYMBOL_GPL(usb_serial_handle_dcd_change
);
558 int usb_serial_generic_resume(struct usb_serial
*serial
)
560 struct usb_serial_port
*port
;
563 for (i
= 0; i
< serial
->num_ports
; i
++) {
564 port
= serial
->port
[i
];
565 if (!test_bit(ASYNCB_INITIALIZED
, &port
->port
.flags
))
568 if (port
->bulk_in_size
) {
569 r
= usb_serial_generic_submit_read_urbs(port
,
575 if (port
->bulk_out_size
) {
576 r
= usb_serial_generic_write_start(port
);
584 EXPORT_SYMBOL_GPL(usb_serial_generic_resume
);