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>
26 #ifdef CONFIG_USB_SERIAL_GENERIC
28 static int generic_probe(struct usb_interface
*interface
,
29 const struct usb_device_id
*id
);
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 struct usb_device_id generic_serial_ids
[] = {
49 static struct usb_driver generic_driver
= {
50 .name
= "usbserial_generic",
51 .probe
= generic_probe
,
52 .disconnect
= usb_serial_disconnect
,
53 .id_table
= generic_serial_ids
,
57 /* All of the device info needed for the Generic Serial Converter */
58 struct usb_serial_driver usb_serial_generic_device
= {
63 .id_table
= generic_device_ids
,
64 .usb_driver
= &generic_driver
,
66 .shutdown
= usb_serial_generic_shutdown
,
67 .throttle
= usb_serial_generic_throttle
,
68 .unthrottle
= usb_serial_generic_unthrottle
,
69 .resume
= usb_serial_generic_resume
,
72 static int generic_probe(struct usb_interface
*interface
,
73 const struct usb_device_id
*id
)
75 const struct usb_device_id
*id_pattern
;
77 id_pattern
= usb_match_id(interface
, generic_device_ids
);
78 if (id_pattern
!= NULL
)
79 return usb_serial_probe(interface
, id
);
84 int usb_serial_generic_register (int _debug
)
89 #ifdef CONFIG_USB_SERIAL_GENERIC
90 generic_device_ids
[0].idVendor
= vendor
;
91 generic_device_ids
[0].idProduct
= product
;
92 generic_device_ids
[0].match_flags
= USB_DEVICE_ID_MATCH_VENDOR
| USB_DEVICE_ID_MATCH_PRODUCT
;
94 /* register our generic driver with ourselves */
95 retval
= usb_serial_register (&usb_serial_generic_device
);
98 retval
= usb_register(&generic_driver
);
100 usb_serial_deregister(&usb_serial_generic_device
);
106 void usb_serial_generic_deregister (void)
108 #ifdef CONFIG_USB_SERIAL_GENERIC
109 /* remove our generic driver */
110 usb_deregister(&generic_driver
);
111 usb_serial_deregister (&usb_serial_generic_device
);
115 int usb_serial_generic_open(struct tty_struct
*tty
,
116 struct usb_serial_port
*port
, struct file
*filp
)
118 struct usb_serial
*serial
= port
->serial
;
122 dbg("%s - port %d", __func__
, 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 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", __func__
, 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", __func__
, 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 int usb_serial_generic_resume(struct usb_serial
*serial
)
173 struct usb_serial_port
*port
;
178 * If this is an autoresume, don't submit URBs.
179 * They will be submitted in the open function instead.
181 if (serial
->dev
->auto_pm
)
184 for (i
= 0; i
< serial
->num_ports
; i
++) {
185 port
= serial
->port
[i
];
186 if (port
->port
.count
&& port
->read_urb
) {
187 r
= usb_submit_urb(port
->read_urb
, GFP_NOIO
);
196 void usb_serial_generic_close(struct tty_struct
*tty
,
197 struct usb_serial_port
*port
, struct file
* filp
)
199 dbg("%s - port %d", __func__
, port
->number
);
200 generic_cleanup (port
);
203 int usb_serial_generic_write(struct tty_struct
*tty
,
204 struct usb_serial_port
*port
, const unsigned char *buf
, int count
)
206 struct usb_serial
*serial
= port
->serial
;
210 dbg("%s - port %d", __func__
, port
->number
);
213 dbg("%s - write request of 0 bytes", __func__
);
217 /* only do something if we have a bulk out endpoint */
218 if (serial
->num_bulk_out
) {
220 spin_lock_irqsave(&port
->lock
, flags
);
221 if (port
->write_urb_busy
) {
222 spin_unlock_irqrestore(&port
->lock
, flags
);
223 dbg("%s - already writing", __func__
);
226 port
->write_urb_busy
= 1;
227 spin_unlock_irqrestore(&port
->lock
, flags
);
229 count
= (count
> port
->bulk_out_size
) ? port
->bulk_out_size
: count
;
231 memcpy (port
->write_urb
->transfer_buffer
, buf
, count
);
232 data
= port
->write_urb
->transfer_buffer
;
233 usb_serial_debug_data(debug
, &port
->dev
, __func__
, count
, data
);
236 usb_fill_bulk_urb (port
->write_urb
, serial
->dev
,
237 usb_sndbulkpipe (serial
->dev
,
238 port
->bulk_out_endpointAddress
),
239 port
->write_urb
->transfer_buffer
, count
,
240 ((serial
->type
->write_bulk_callback
) ?
241 serial
->type
->write_bulk_callback
:
242 usb_serial_generic_write_bulk_callback
), port
);
244 /* send the data out the bulk port */
245 port
->write_urb_busy
= 1;
246 result
= usb_submit_urb(port
->write_urb
, GFP_ATOMIC
);
248 dev_err(&port
->dev
, "%s - failed submitting write urb, error %d\n", __func__
, result
);
249 /* don't have to grab the lock here, as we will retry if != 0 */
250 port
->write_urb_busy
= 0;
257 /* no bulk out, so return 0 bytes written */
261 int usb_serial_generic_write_room (struct tty_struct
*tty
)
263 struct usb_serial_port
*port
= tty
->driver_data
;
264 struct usb_serial
*serial
= port
->serial
;
267 dbg("%s - port %d", __func__
, port
->number
);
270 if (serial
->num_bulk_out
) {
271 if (!(port
->write_urb_busy
))
272 room
= port
->bulk_out_size
;
275 dbg("%s - returns %d", __func__
, room
);
279 int usb_serial_generic_chars_in_buffer(struct tty_struct
*tty
)
281 struct usb_serial_port
*port
= tty
->driver_data
;
282 struct usb_serial
*serial
= port
->serial
;
285 dbg("%s - port %d", __func__
, port
->number
);
288 if (serial
->num_bulk_out
) {
289 if (port
->write_urb_busy
)
290 chars
= port
->write_urb
->transfer_buffer_length
;
293 dbg("%s - returns %d", __func__
, chars
);
298 static void resubmit_read_urb(struct usb_serial_port
*port
, gfp_t mem_flags
)
300 struct urb
*urb
= port
->read_urb
;
301 struct usb_serial
*serial
= port
->serial
;
304 /* Continue reading from device */
305 usb_fill_bulk_urb (urb
, serial
->dev
,
306 usb_rcvbulkpipe (serial
->dev
,
307 port
->bulk_in_endpointAddress
),
308 urb
->transfer_buffer
,
309 urb
->transfer_buffer_length
,
310 ((serial
->type
->read_bulk_callback
) ?
311 serial
->type
->read_bulk_callback
:
312 usb_serial_generic_read_bulk_callback
), port
);
313 result
= usb_submit_urb(urb
, mem_flags
);
315 dev_err(&port
->dev
, "%s - failed resubmitting read urb, error %d\n", __func__
, result
);
318 /* Push data to tty layer and resubmit the bulk read URB */
319 static void flush_and_resubmit_read_urb(struct usb_serial_port
*port
)
321 struct urb
*urb
= port
->read_urb
;
322 struct tty_struct
*tty
= port
->port
.tty
;
325 /* Push data to tty */
326 if (tty
&& urb
->actual_length
) {
327 room
= tty_buffer_request_room(tty
, urb
->actual_length
);
329 tty_insert_flip_string(tty
, urb
->transfer_buffer
, room
);
330 tty_flip_buffer_push(tty
);
334 resubmit_read_urb(port
, GFP_ATOMIC
);
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
;
341 int status
= urb
->status
;
344 dbg("%s - port %d", __func__
, port
->number
);
346 if (unlikely(status
!= 0)) {
347 dbg("%s - nonzero read bulk status received: %d",
352 usb_serial_debug_data(debug
, &port
->dev
, __func__
, urb
->actual_length
, data
);
354 /* Throttle the device if requested by tty */
355 spin_lock_irqsave(&port
->lock
, flags
);
356 if (!(port
->throttled
= port
->throttle_req
)) {
357 spin_unlock_irqrestore(&port
->lock
, flags
);
358 flush_and_resubmit_read_urb(port
);
360 spin_unlock_irqrestore(&port
->lock
, flags
);
363 EXPORT_SYMBOL_GPL(usb_serial_generic_read_bulk_callback
);
365 void usb_serial_generic_write_bulk_callback(struct urb
*urb
)
367 struct usb_serial_port
*port
= urb
->context
;
368 int status
= urb
->status
;
370 dbg("%s - port %d", __func__
, port
->number
);
372 port
->write_urb_busy
= 0;
374 dbg("%s - nonzero write bulk status received: %d",
378 usb_serial_port_softint(port
);
380 EXPORT_SYMBOL_GPL(usb_serial_generic_write_bulk_callback
);
382 void usb_serial_generic_throttle(struct tty_struct
*tty
)
384 struct usb_serial_port
*port
= tty
->driver_data
;
387 dbg("%s - port %d", __func__
, port
->number
);
389 /* Set the throttle request flag. It will be picked up
390 * by usb_serial_generic_read_bulk_callback(). */
391 spin_lock_irqsave(&port
->lock
, flags
);
392 port
->throttle_req
= 1;
393 spin_unlock_irqrestore(&port
->lock
, flags
);
396 void usb_serial_generic_unthrottle(struct tty_struct
*tty
)
398 struct usb_serial_port
*port
= tty
->driver_data
;
402 dbg("%s - port %d", __func__
, port
->number
);
404 /* Clear the throttle flags */
405 spin_lock_irqsave(&port
->lock
, flags
);
406 was_throttled
= port
->throttled
;
407 port
->throttled
= port
->throttle_req
= 0;
408 spin_unlock_irqrestore(&port
->lock
, flags
);
411 /* Resume reading from device */
412 resubmit_read_urb(port
, GFP_KERNEL
);
416 void usb_serial_generic_shutdown (struct usb_serial
*serial
)
422 /* stop reads and writes on all ports */
423 for (i
=0; i
< serial
->num_ports
; ++i
) {
424 generic_cleanup(serial
->port
[i
]);