2 * USB Serial Converter Generic functions
4 * Copyright (C) 1999 - 2002 Greg Kroah-Hartman (greg@kroah.com)
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License version
8 * 2 as published by the Free Software Foundation.
12 #include <linux/kernel.h>
13 #include <linux/errno.h>
14 #include <linux/slab.h>
15 #include <linux/tty.h>
16 #include <linux/tty_flip.h>
17 #include <linux/module.h>
18 #include <linux/moduleparam.h>
19 #include <linux/usb.h>
20 #include <linux/usb/serial.h>
21 #include <asm/uaccess.h>
23 static int generic_probe(struct usb_interface
*interface
,
24 const struct usb_device_id
*id
);
29 #ifdef CONFIG_USB_SERIAL_GENERIC
30 static __u16 vendor
= 0x05f9;
31 static __u16 product
= 0xffff;
33 module_param(vendor
, ushort
, 0);
34 MODULE_PARM_DESC(vendor
, "User specified USB idVendor");
36 module_param(product
, ushort
, 0);
37 MODULE_PARM_DESC(product
, "User specified USB idProduct");
39 static struct usb_device_id generic_device_ids
[2]; /* Initially all zeroes. */
41 /* we want to look at all devices, as the vendor/product id can change
42 * depending on the command line argument */
43 static struct usb_device_id generic_serial_ids
[] = {
48 static struct usb_driver generic_driver
= {
49 .name
= "usbserial_generic",
50 .probe
= generic_probe
,
51 .disconnect
= usb_serial_disconnect
,
52 .id_table
= generic_serial_ids
,
56 /* All of the device info needed for the Generic Serial Converter */
57 struct usb_serial_driver usb_serial_generic_device
= {
62 .id_table
= generic_device_ids
,
63 .usb_driver
= &generic_driver
,
64 .num_interrupt_in
= NUM_DONT_CARE
,
65 .num_bulk_in
= NUM_DONT_CARE
,
66 .num_bulk_out
= NUM_DONT_CARE
,
68 .shutdown
= usb_serial_generic_shutdown
,
69 .throttle
= usb_serial_generic_throttle
,
70 .unthrottle
= usb_serial_generic_unthrottle
,
73 static int generic_probe(struct usb_interface
*interface
,
74 const struct usb_device_id
*id
)
76 const struct usb_device_id
*id_pattern
;
78 id_pattern
= usb_match_id(interface
, generic_device_ids
);
79 if (id_pattern
!= NULL
)
80 return usb_serial_probe(interface
, id
);
85 int usb_serial_generic_register (int _debug
)
90 #ifdef CONFIG_USB_SERIAL_GENERIC
91 generic_device_ids
[0].idVendor
= vendor
;
92 generic_device_ids
[0].idProduct
= product
;
93 generic_device_ids
[0].match_flags
= USB_DEVICE_ID_MATCH_VENDOR
| USB_DEVICE_ID_MATCH_PRODUCT
;
95 /* register our generic driver with ourselves */
96 retval
= usb_serial_register (&usb_serial_generic_device
);
99 retval
= usb_register(&generic_driver
);
101 usb_serial_deregister(&usb_serial_generic_device
);
107 void usb_serial_generic_deregister (void)
109 #ifdef CONFIG_USB_SERIAL_GENERIC
110 /* remove our generic driver */
111 usb_deregister(&generic_driver
);
112 usb_serial_deregister (&usb_serial_generic_device
);
116 int usb_serial_generic_open (struct usb_serial_port
*port
, struct file
*filp
)
118 struct usb_serial
*serial
= port
->serial
;
122 dbg("%s - port %d", __FUNCTION__
, port
->number
);
124 /* force low_latency on so that our tty_push actually forces the data through,
125 otherwise it is scheduled, and with high data rates (like with OHCI) data
128 port
->tty
->low_latency
= 1;
130 /* clear the throttle flags */
131 spin_lock_irqsave(&port
->lock
, flags
);
133 port
->throttle_req
= 0;
134 spin_unlock_irqrestore(&port
->lock
, flags
);
136 /* if we have a bulk endpoint, start reading from it */
137 if (serial
->num_bulk_in
) {
138 /* Start reading from the device */
139 usb_fill_bulk_urb (port
->read_urb
, serial
->dev
,
140 usb_rcvbulkpipe(serial
->dev
, port
->bulk_in_endpointAddress
),
141 port
->read_urb
->transfer_buffer
,
142 port
->read_urb
->transfer_buffer_length
,
143 ((serial
->type
->read_bulk_callback
) ?
144 serial
->type
->read_bulk_callback
:
145 usb_serial_generic_read_bulk_callback
),
147 result
= usb_submit_urb(port
->read_urb
, GFP_KERNEL
);
149 dev_err(&port
->dev
, "%s - failed resubmitting read urb, error %d\n", __FUNCTION__
, result
);
154 EXPORT_SYMBOL_GPL(usb_serial_generic_open
);
156 static void generic_cleanup (struct usb_serial_port
*port
)
158 struct usb_serial
*serial
= port
->serial
;
160 dbg("%s - port %d", __FUNCTION__
, port
->number
);
163 /* shutdown any bulk reads that might be going on */
164 if (serial
->num_bulk_out
)
165 usb_kill_urb(port
->write_urb
);
166 if (serial
->num_bulk_in
)
167 usb_kill_urb(port
->read_urb
);
171 void usb_serial_generic_close (struct usb_serial_port
*port
, struct file
* filp
)
173 dbg("%s - port %d", __FUNCTION__
, port
->number
);
174 generic_cleanup (port
);
177 int usb_serial_generic_write(struct usb_serial_port
*port
, const unsigned char *buf
, int count
)
179 struct usb_serial
*serial
= port
->serial
;
183 dbg("%s - port %d", __FUNCTION__
, port
->number
);
186 dbg("%s - write request of 0 bytes", __FUNCTION__
);
190 /* only do something if we have a bulk out endpoint */
191 if (serial
->num_bulk_out
) {
192 spin_lock_bh(&port
->lock
);
193 if (port
->write_urb_busy
) {
194 spin_unlock_bh(&port
->lock
);
195 dbg("%s - already writing", __FUNCTION__
);
198 port
->write_urb_busy
= 1;
199 spin_unlock_bh(&port
->lock
);
201 count
= (count
> port
->bulk_out_size
) ? port
->bulk_out_size
: count
;
203 memcpy (port
->write_urb
->transfer_buffer
, buf
, count
);
204 data
= port
->write_urb
->transfer_buffer
;
205 usb_serial_debug_data(debug
, &port
->dev
, __FUNCTION__
, count
, data
);
208 usb_fill_bulk_urb (port
->write_urb
, serial
->dev
,
209 usb_sndbulkpipe (serial
->dev
,
210 port
->bulk_out_endpointAddress
),
211 port
->write_urb
->transfer_buffer
, count
,
212 ((serial
->type
->write_bulk_callback
) ?
213 serial
->type
->write_bulk_callback
:
214 usb_serial_generic_write_bulk_callback
), port
);
216 /* send the data out the bulk port */
217 port
->write_urb_busy
= 1;
218 result
= usb_submit_urb(port
->write_urb
, GFP_ATOMIC
);
220 dev_err(&port
->dev
, "%s - failed submitting write urb, error %d\n", __FUNCTION__
, result
);
221 /* don't have to grab the lock here, as we will retry if != 0 */
222 port
->write_urb_busy
= 0;
229 /* no bulk out, so return 0 bytes written */
233 int usb_serial_generic_write_room (struct usb_serial_port
*port
)
235 struct usb_serial
*serial
= port
->serial
;
238 dbg("%s - port %d", __FUNCTION__
, port
->number
);
240 if (serial
->num_bulk_out
) {
241 if (!(port
->write_urb_busy
))
242 room
= port
->bulk_out_size
;
245 dbg("%s - returns %d", __FUNCTION__
, room
);
249 int usb_serial_generic_chars_in_buffer (struct usb_serial_port
*port
)
251 struct usb_serial
*serial
= port
->serial
;
254 dbg("%s - port %d", __FUNCTION__
, port
->number
);
256 if (serial
->num_bulk_out
) {
257 if (port
->write_urb_busy
)
258 chars
= port
->write_urb
->transfer_buffer_length
;
261 dbg("%s - returns %d", __FUNCTION__
, chars
);
265 /* Push data to tty layer and resubmit the bulk read URB */
266 static void flush_and_resubmit_read_urb (struct usb_serial_port
*port
)
268 struct usb_serial
*serial
= port
->serial
;
269 struct urb
*urb
= port
->read_urb
;
270 struct tty_struct
*tty
= port
->tty
;
273 /* Push data to tty */
274 if (tty
&& urb
->actual_length
) {
275 tty_buffer_request_room(tty
, urb
->actual_length
);
276 tty_insert_flip_string(tty
, urb
->transfer_buffer
, urb
->actual_length
);
277 tty_flip_buffer_push(tty
); /* is this allowed from an URB callback ? */
280 /* Continue reading from device */
281 usb_fill_bulk_urb (port
->read_urb
, serial
->dev
,
282 usb_rcvbulkpipe (serial
->dev
,
283 port
->bulk_in_endpointAddress
),
284 port
->read_urb
->transfer_buffer
,
285 port
->read_urb
->transfer_buffer_length
,
286 ((serial
->type
->read_bulk_callback
) ?
287 serial
->type
->read_bulk_callback
:
288 usb_serial_generic_read_bulk_callback
), port
);
289 result
= usb_submit_urb(port
->read_urb
, GFP_ATOMIC
);
291 dev_err(&port
->dev
, "%s - failed resubmitting read urb, error %d\n", __FUNCTION__
, result
);
294 void usb_serial_generic_read_bulk_callback (struct urb
*urb
)
296 struct usb_serial_port
*port
= (struct usb_serial_port
*)urb
->context
;
297 unsigned char *data
= urb
->transfer_buffer
;
301 dbg("%s - port %d", __FUNCTION__
, port
->number
);
304 dbg("%s - nonzero read bulk status received: %d", __FUNCTION__
, urb
->status
);
308 usb_serial_debug_data(debug
, &port
->dev
, __FUNCTION__
, urb
->actual_length
, data
);
310 /* Throttle the device if requested by tty */
311 if (urb
->actual_length
) {
312 spin_lock_irqsave(&port
->lock
, flags
);
313 is_throttled
= port
->throttled
= port
->throttle_req
;
314 spin_unlock_irqrestore(&port
->lock
, flags
);
316 /* Let the received data linger in the read URB;
317 * usb_serial_generic_unthrottle() will pick it
319 dbg("%s - throttling device", __FUNCTION__
);
324 /* Handle data and continue reading from device */
325 flush_and_resubmit_read_urb(port
);
327 EXPORT_SYMBOL_GPL(usb_serial_generic_read_bulk_callback
);
329 void usb_serial_generic_write_bulk_callback (struct urb
*urb
)
331 struct usb_serial_port
*port
= (struct usb_serial_port
*)urb
->context
;
333 dbg("%s - port %d", __FUNCTION__
, port
->number
);
335 port
->write_urb_busy
= 0;
337 dbg("%s - nonzero write bulk status received: %d", __FUNCTION__
, urb
->status
);
341 usb_serial_port_softint(port
);
343 EXPORT_SYMBOL_GPL(usb_serial_generic_write_bulk_callback
);
345 void usb_serial_generic_throttle (struct usb_serial_port
*port
)
349 dbg("%s - port %d", __FUNCTION__
, port
->number
);
351 /* Set the throttle request flag. It will be picked up
352 * by usb_serial_generic_read_bulk_callback(). */
353 spin_lock_irqsave(&port
->lock
, flags
);
354 port
->throttle_req
= 1;
355 spin_unlock_irqrestore(&port
->lock
, flags
);
358 void usb_serial_generic_unthrottle (struct usb_serial_port
*port
)
363 dbg("%s - port %d", __FUNCTION__
, port
->number
);
365 /* Clear the throttle flags */
366 spin_lock_irqsave(&port
->lock
, flags
);
367 was_throttled
= port
->throttled
;
368 port
->throttled
= port
->throttle_req
= 0;
369 spin_unlock_irqrestore(&port
->lock
, flags
);
372 /* Handle pending data and resume reading from device */
373 flush_and_resubmit_read_urb(port
);
377 void usb_serial_generic_shutdown (struct usb_serial
*serial
)
381 dbg("%s", __FUNCTION__
);
383 /* stop reads and writes on all ports */
384 for (i
=0; i
< serial
->num_ports
; ++i
) {
385 generic_cleanup(serial
->port
[i
]);