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 usb_serial_port *port, struct file *filp)
117 struct usb_serial *serial = port->serial;
121 dbg("%s - port %d", __func__, port->number);
123 /* force low_latency on so that our tty_push actually forces the data through,
124 otherwise it is scheduled, and with high data rates (like with OHCI) data
127 port->tty->low_latency = 1;
129 /* clear the throttle flags */
130 spin_lock_irqsave(&port->lock, flags);
132 port->throttle_req = 0;
133 spin_unlock_irqrestore(&port->lock, flags);
135 /* if we have a bulk endpoint, start reading from it */
136 if (serial->num_bulk_in) {
137 /* Start reading from the device */
138 usb_fill_bulk_urb (port->read_urb, serial->dev,
139 usb_rcvbulkpipe(serial->dev, port->bulk_in_endpointAddress),
140 port->read_urb->transfer_buffer,
141 port->read_urb->transfer_buffer_length,
142 ((serial->type->read_bulk_callback) ?
143 serial->type->read_bulk_callback :
144 usb_serial_generic_read_bulk_callback),
146 result = usb_submit_urb(port->read_urb, GFP_KERNEL);
148 dev_err(&port->dev, "%s - failed resubmitting read urb, error %d\n", __func__, result);
153 EXPORT_SYMBOL_GPL(usb_serial_generic_open);
155 static void generic_cleanup (struct usb_serial_port *port)
157 struct usb_serial *serial = port->serial;
159 dbg("%s - port %d", __func__, port->number);
162 /* shutdown any bulk reads that might be going on */
163 if (serial->num_bulk_out)
164 usb_kill_urb(port->write_urb);
165 if (serial->num_bulk_in)
166 usb_kill_urb(port->read_urb);
170 int usb_serial_generic_resume(struct usb_serial *serial)
172 struct usb_serial_port *port;
177 * If this is an autoresume, don't submit URBs.
178 * They will be submitted in the open function instead.
180 if (serial->dev->auto_pm)
183 for (i = 0; i < serial->num_ports; i++) {
184 port = serial->port[i];
185 if (port->open_count && port->read_urb) {
186 r = usb_submit_urb(port->read_urb, GFP_NOIO);
195 void usb_serial_generic_close (struct usb_serial_port *port, struct file * filp)
197 dbg("%s - port %d", __func__, port->number);
198 generic_cleanup (port);
201 int usb_serial_generic_write(struct usb_serial_port *port, const unsigned char *buf, int count)
203 struct usb_serial *serial = port->serial;
207 dbg("%s - port %d", __func__, port->number);
210 dbg("%s - write request of 0 bytes", __func__);
214 /* only do something if we have a bulk out endpoint */
215 if (serial->num_bulk_out) {
217 spin_lock_irqsave(&port->lock, flags);
218 if (port->write_urb_busy) {
219 spin_unlock_irqrestore(&port->lock, flags);
220 dbg("%s - already writing", __func__);
223 port->write_urb_busy = 1;
224 spin_unlock_irqrestore(&port->lock, flags);
226 count = (count > port->bulk_out_size) ? port->bulk_out_size : count;
228 memcpy (port->write_urb->transfer_buffer, buf, count);
229 data = port->write_urb->transfer_buffer;
230 usb_serial_debug_data(debug, &port->dev, __func__, count, data);
233 usb_fill_bulk_urb (port->write_urb, serial->dev,
234 usb_sndbulkpipe (serial->dev,
235 port->bulk_out_endpointAddress),
236 port->write_urb->transfer_buffer, count,
237 ((serial->type->write_bulk_callback) ?
238 serial->type->write_bulk_callback :
239 usb_serial_generic_write_bulk_callback), port);
241 /* send the data out the bulk port */
242 port->write_urb_busy = 1;
243 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
245 dev_err(&port->dev, "%s - failed submitting write urb, error %d\n", __func__, result);
246 /* don't have to grab the lock here, as we will retry if != 0 */
247 port->write_urb_busy = 0;
254 /* no bulk out, so return 0 bytes written */
258 int usb_serial_generic_write_room (struct usb_serial_port *port)
260 struct usb_serial *serial = port->serial;
263 dbg("%s - port %d", __func__, port->number);
266 if (serial->num_bulk_out) {
267 if (!(port->write_urb_busy))
268 room = port->bulk_out_size;
271 dbg("%s - returns %d", __func__, room);
275 int usb_serial_generic_chars_in_buffer (struct usb_serial_port *port)
277 struct usb_serial *serial = port->serial;
280 dbg("%s - port %d", __func__, port->number);
283 if (serial->num_bulk_out) {
284 if (port->write_urb_busy)
285 chars = port->write_urb->transfer_buffer_length;
288 dbg("%s - returns %d", __func__, chars);
293 static void resubmit_read_urb(struct usb_serial_port *port, gfp_t mem_flags)
295 struct urb *urb = port->read_urb;
296 struct usb_serial *serial = port->serial;
299 /* Continue reading from device */
300 usb_fill_bulk_urb (urb, serial->dev,
301 usb_rcvbulkpipe (serial->dev,
302 port->bulk_in_endpointAddress),
303 urb->transfer_buffer,
304 urb->transfer_buffer_length,
305 ((serial->type->read_bulk_callback) ?
306 serial->type->read_bulk_callback :
307 usb_serial_generic_read_bulk_callback), port);
308 result = usb_submit_urb(urb, mem_flags);
310 dev_err(&port->dev, "%s - failed resubmitting read urb, error %d\n", __func__, result);
313 /* Push data to tty layer and resubmit the bulk read URB */
314 static void flush_and_resubmit_read_urb (struct usb_serial_port *port)
316 struct urb *urb = port->read_urb;
317 struct tty_struct *tty = port->tty;
320 /* Push data to tty */
321 if (tty && urb->actual_length) {
322 room = tty_buffer_request_room(tty, urb->actual_length);
324 tty_insert_flip_string(tty, urb->transfer_buffer, room);
325 tty_flip_buffer_push(tty);
329 resubmit_read_urb(port, GFP_ATOMIC);
332 void usb_serial_generic_read_bulk_callback (struct urb *urb)
334 struct usb_serial_port *port = urb->context;
335 unsigned char *data = urb->transfer_buffer;
336 int status = urb->status;
339 dbg("%s - port %d", __func__, port->number);
341 if (unlikely(status != 0)) {
342 dbg("%s - nonzero read bulk status received: %d",
347 usb_serial_debug_data(debug, &port->dev, __func__, urb->actual_length, data);
349 /* Throttle the device if requested by tty */
350 spin_lock_irqsave(&port->lock, flags);
351 if (!(port->throttled = port->throttle_req)) {
352 spin_unlock_irqrestore(&port->lock, flags);
353 flush_and_resubmit_read_urb(port);
355 spin_unlock_irqrestore(&port->lock, flags);
358 EXPORT_SYMBOL_GPL(usb_serial_generic_read_bulk_callback);
360 void usb_serial_generic_write_bulk_callback (struct urb *urb)
362 struct usb_serial_port *port = urb->context;
363 int status = urb->status;
365 dbg("%s - port %d", __func__, port->number);
367 port->write_urb_busy = 0;
369 dbg("%s - nonzero write bulk status received: %d",
373 usb_serial_port_softint(port);
375 EXPORT_SYMBOL_GPL(usb_serial_generic_write_bulk_callback);
377 void usb_serial_generic_throttle (struct usb_serial_port *port)
381 dbg("%s - port %d", __func__, port->number);
383 /* Set the throttle request flag. It will be picked up
384 * by usb_serial_generic_read_bulk_callback(). */
385 spin_lock_irqsave(&port->lock, flags);
386 port->throttle_req = 1;
387 spin_unlock_irqrestore(&port->lock, flags);
390 void usb_serial_generic_unthrottle (struct usb_serial_port *port)
395 dbg("%s - port %d", __func__, port->number);
397 /* Clear the throttle flags */
398 spin_lock_irqsave(&port->lock, flags);
399 was_throttled = port->throttled;
400 port->throttled = port->throttle_req = 0;
401 spin_unlock_irqrestore(&port->lock, flags);
404 /* Resume reading from device */
405 resubmit_read_urb(port, GFP_KERNEL);
409 void usb_serial_generic_shutdown (struct usb_serial *serial)
415 /* stop reads and writes on all ports */
416 for (i=0; i < serial->num_ports; ++i) {
417 generic_cleanup(serial->port[i]);