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,
65 .num_interrupt_in = NUM_DONT_CARE,
66 .num_bulk_in = NUM_DONT_CARE,
67 .num_bulk_out = NUM_DONT_CARE,
69 .shutdown = usb_serial_generic_shutdown,
70 .throttle = usb_serial_generic_throttle,
71 .unthrottle = usb_serial_generic_unthrottle,
74 static int generic_probe(struct usb_interface *interface,
75 const struct usb_device_id *id)
77 const struct usb_device_id *id_pattern;
79 id_pattern = usb_match_id(interface, generic_device_ids);
80 if (id_pattern != NULL)
81 return usb_serial_probe(interface, id);
86 int usb_serial_generic_register (int _debug)
91 #ifdef CONFIG_USB_SERIAL_GENERIC
92 generic_device_ids[0].idVendor = vendor;
93 generic_device_ids[0].idProduct = product;
94 generic_device_ids[0].match_flags = USB_DEVICE_ID_MATCH_VENDOR | USB_DEVICE_ID_MATCH_PRODUCT;
96 /* register our generic driver with ourselves */
97 retval = usb_serial_register (&usb_serial_generic_device);
100 retval = usb_register(&generic_driver);
102 usb_serial_deregister(&usb_serial_generic_device);
108 void usb_serial_generic_deregister (void)
110 #ifdef CONFIG_USB_SERIAL_GENERIC
111 /* remove our generic driver */
112 usb_deregister(&generic_driver);
113 usb_serial_deregister (&usb_serial_generic_device);
117 int usb_serial_generic_open (struct usb_serial_port *port, struct file *filp)
119 struct usb_serial *serial = port->serial;
123 dbg("%s - port %d", __FUNCTION__, port->number);
125 /* force low_latency on so that our tty_push actually forces the data through,
126 otherwise it is scheduled, and with high data rates (like with OHCI) data
129 port->tty->low_latency = 1;
131 /* clear the throttle flags */
132 spin_lock_irqsave(&port->lock, flags);
134 port->throttle_req = 0;
135 spin_unlock_irqrestore(&port->lock, flags);
137 /* if we have a bulk endpoint, start reading from it */
138 if (serial->num_bulk_in) {
139 /* Start reading from the device */
140 usb_fill_bulk_urb (port->read_urb, serial->dev,
141 usb_rcvbulkpipe(serial->dev, port->bulk_in_endpointAddress),
142 port->read_urb->transfer_buffer,
143 port->read_urb->transfer_buffer_length,
144 ((serial->type->read_bulk_callback) ?
145 serial->type->read_bulk_callback :
146 usb_serial_generic_read_bulk_callback),
148 result = usb_submit_urb(port->read_urb, GFP_KERNEL);
150 dev_err(&port->dev, "%s - failed resubmitting read urb, error %d\n", __FUNCTION__, result);
155 EXPORT_SYMBOL_GPL(usb_serial_generic_open);
157 static void generic_cleanup (struct usb_serial_port *port)
159 struct usb_serial *serial = port->serial;
161 dbg("%s - port %d", __FUNCTION__, port->number);
164 /* shutdown any bulk reads that might be going on */
165 if (serial->num_bulk_out)
166 usb_kill_urb(port->write_urb);
167 if (serial->num_bulk_in)
168 usb_kill_urb(port->read_urb);
172 void usb_serial_generic_close (struct usb_serial_port *port, struct file * filp)
174 dbg("%s - port %d", __FUNCTION__, port->number);
175 generic_cleanup (port);
178 int usb_serial_generic_write(struct usb_serial_port *port, const unsigned char *buf, int count)
180 struct usb_serial *serial = port->serial;
184 dbg("%s - port %d", __FUNCTION__, port->number);
187 dbg("%s - write request of 0 bytes", __FUNCTION__);
191 /* only do something if we have a bulk out endpoint */
192 if (serial->num_bulk_out) {
193 spin_lock_bh(&port->lock);
194 if (port->write_urb_busy) {
195 spin_unlock_bh(&port->lock);
196 dbg("%s - already writing", __FUNCTION__);
199 port->write_urb_busy = 1;
200 spin_unlock_bh(&port->lock);
202 count = (count > port->bulk_out_size) ? port->bulk_out_size : count;
204 memcpy (port->write_urb->transfer_buffer, buf, count);
205 data = port->write_urb->transfer_buffer;
206 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, count, data);
209 usb_fill_bulk_urb (port->write_urb, serial->dev,
210 usb_sndbulkpipe (serial->dev,
211 port->bulk_out_endpointAddress),
212 port->write_urb->transfer_buffer, count,
213 ((serial->type->write_bulk_callback) ?
214 serial->type->write_bulk_callback :
215 usb_serial_generic_write_bulk_callback), port);
217 /* send the data out the bulk port */
218 port->write_urb_busy = 1;
219 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
221 dev_err(&port->dev, "%s - failed submitting write urb, error %d\n", __FUNCTION__, result);
222 /* don't have to grab the lock here, as we will retry if != 0 */
223 port->write_urb_busy = 0;
230 /* no bulk out, so return 0 bytes written */
234 int usb_serial_generic_write_room (struct usb_serial_port *port)
236 struct usb_serial *serial = port->serial;
239 dbg("%s - port %d", __FUNCTION__, port->number);
241 if (serial->num_bulk_out) {
242 if (!(port->write_urb_busy))
243 room = port->bulk_out_size;
246 dbg("%s - returns %d", __FUNCTION__, room);
250 int usb_serial_generic_chars_in_buffer (struct usb_serial_port *port)
252 struct usb_serial *serial = port->serial;
255 dbg("%s - port %d", __FUNCTION__, port->number);
257 if (serial->num_bulk_out) {
258 if (port->write_urb_busy)
259 chars = port->write_urb->transfer_buffer_length;
262 dbg("%s - returns %d", __FUNCTION__, chars);
266 /* Push data to tty layer and resubmit the bulk read URB */
267 static void flush_and_resubmit_read_urb (struct usb_serial_port *port)
269 struct usb_serial *serial = port->serial;
270 struct urb *urb = port->read_urb;
271 struct tty_struct *tty = port->tty;
274 /* Push data to tty */
275 if (tty && urb->actual_length) {
276 tty_buffer_request_room(tty, urb->actual_length);
277 tty_insert_flip_string(tty, urb->transfer_buffer, urb->actual_length);
278 tty_flip_buffer_push(tty); /* is this allowed from an URB callback ? */
281 /* Continue reading from device */
282 usb_fill_bulk_urb (port->read_urb, serial->dev,
283 usb_rcvbulkpipe (serial->dev,
284 port->bulk_in_endpointAddress),
285 port->read_urb->transfer_buffer,
286 port->read_urb->transfer_buffer_length,
287 ((serial->type->read_bulk_callback) ?
288 serial->type->read_bulk_callback :
289 usb_serial_generic_read_bulk_callback), port);
290 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
292 dev_err(&port->dev, "%s - failed resubmitting read urb, error %d\n", __FUNCTION__, result);
295 void usb_serial_generic_read_bulk_callback (struct urb *urb)
297 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
298 unsigned char *data = urb->transfer_buffer;
302 dbg("%s - port %d", __FUNCTION__, port->number);
305 dbg("%s - nonzero read bulk status received: %d", __FUNCTION__, urb->status);
309 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, urb->actual_length, data);
311 /* Throttle the device if requested by tty */
312 if (urb->actual_length) {
313 spin_lock_irqsave(&port->lock, flags);
314 is_throttled = port->throttled = port->throttle_req;
315 spin_unlock_irqrestore(&port->lock, flags);
317 /* Let the received data linger in the read URB;
318 * usb_serial_generic_unthrottle() will pick it
320 dbg("%s - throttling device", __FUNCTION__);
325 /* Handle data and continue reading from device */
326 flush_and_resubmit_read_urb(port);
328 EXPORT_SYMBOL_GPL(usb_serial_generic_read_bulk_callback);
330 void usb_serial_generic_write_bulk_callback (struct urb *urb)
332 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
334 dbg("%s - port %d", __FUNCTION__, port->number);
336 port->write_urb_busy = 0;
338 dbg("%s - nonzero write bulk status received: %d", __FUNCTION__, urb->status);
342 usb_serial_port_softint(port);
344 EXPORT_SYMBOL_GPL(usb_serial_generic_write_bulk_callback);
346 void usb_serial_generic_throttle (struct usb_serial_port *port)
350 dbg("%s - port %d", __FUNCTION__, port->number);
352 /* Set the throttle request flag. It will be picked up
353 * by usb_serial_generic_read_bulk_callback(). */
354 spin_lock_irqsave(&port->lock, flags);
355 port->throttle_req = 1;
356 spin_unlock_irqrestore(&port->lock, flags);
359 void usb_serial_generic_unthrottle (struct usb_serial_port *port)
364 dbg("%s - port %d", __FUNCTION__, port->number);
366 /* Clear the throttle flags */
367 spin_lock_irqsave(&port->lock, flags);
368 was_throttled = port->throttled;
369 port->throttled = port->throttle_req = 0;
370 spin_unlock_irqrestore(&port->lock, flags);
373 /* Handle pending data and resume reading from device */
374 flush_and_resubmit_read_urb(port);
378 void usb_serial_generic_shutdown (struct usb_serial *serial)
382 dbg("%s", __FUNCTION__);
384 /* stop reads and writes on all ports */
385 for (i=0; i < serial->num_ports; ++i) {
386 generic_cleanup(serial->port[i]);