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 <linux/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 =
93 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 tty_struct *tty,
117 struct usb_serial_port *port, struct file *filp)
119 struct usb_serial *serial = port->serial;
123 dbg("%s - port %d", __func__, port->number);
125 /* force low_latency on so that our tty_push actually forces the data
126 through, otherwise it is scheduled, and with high data rates (like
127 with OHCI) data can get lost. */
129 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,
142 port->bulk_in_endpointAddress),
143 port->read_urb->transfer_buffer,
144 port->read_urb->transfer_buffer_length,
145 ((serial->type->read_bulk_callback) ?
146 serial->type->read_bulk_callback :
147 usb_serial_generic_read_bulk_callback),
149 result = usb_submit_urb(port->read_urb, GFP_KERNEL);
152 "%s - failed resubmitting read urb, error %d\n",
158 EXPORT_SYMBOL_GPL(usb_serial_generic_open);
160 static void generic_cleanup(struct usb_serial_port *port)
162 struct usb_serial *serial = port->serial;
164 dbg("%s - port %d", __func__, port->number);
167 /* shutdown any bulk reads that might be going on */
168 if (serial->num_bulk_out)
169 usb_kill_urb(port->write_urb);
170 if (serial->num_bulk_in)
171 usb_kill_urb(port->read_urb);
175 int usb_serial_generic_resume(struct usb_serial *serial)
177 struct usb_serial_port *port;
182 * If this is an autoresume, don't submit URBs.
183 * They will be submitted in the open function instead.
185 if (serial->dev->auto_pm)
188 for (i = 0; i < serial->num_ports; i++) {
189 port = serial->port[i];
190 if (port->port.count && port->read_urb) {
191 r = usb_submit_urb(port->read_urb, GFP_NOIO);
200 void usb_serial_generic_close(struct tty_struct *tty,
201 struct usb_serial_port *port, struct file *filp)
203 dbg("%s - port %d", __func__, port->number);
204 generic_cleanup(port);
207 int usb_serial_generic_write(struct tty_struct *tty,
208 struct usb_serial_port *port, const unsigned char *buf, int count)
210 struct usb_serial *serial = port->serial;
214 dbg("%s - port %d", __func__, port->number);
217 dbg("%s - write request of 0 bytes", __func__);
221 /* only do something if we have a bulk out endpoint */
222 if (serial->num_bulk_out) {
224 spin_lock_irqsave(&port->lock, flags);
225 if (port->write_urb_busy) {
226 spin_unlock_irqrestore(&port->lock, flags);
227 dbg("%s - already writing", __func__);
230 port->write_urb_busy = 1;
231 spin_unlock_irqrestore(&port->lock, flags);
233 count = (count > port->bulk_out_size) ?
234 port->bulk_out_size : count;
236 memcpy(port->write_urb->transfer_buffer, buf, count);
237 data = port->write_urb->transfer_buffer;
238 usb_serial_debug_data(debug, &port->dev, __func__, count, data);
241 usb_fill_bulk_urb(port->write_urb, serial->dev,
242 usb_sndbulkpipe(serial->dev,
243 port->bulk_out_endpointAddress),
244 port->write_urb->transfer_buffer, count,
245 ((serial->type->write_bulk_callback) ?
246 serial->type->write_bulk_callback :
247 usb_serial_generic_write_bulk_callback),
250 /* send the data out the bulk port */
251 port->write_urb_busy = 1;
252 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
255 "%s - failed submitting write urb, error %d\n",
257 /* don't have to grab the lock here, as we will
259 port->write_urb_busy = 0;
266 /* no bulk out, so return 0 bytes written */
270 int usb_serial_generic_write_room(struct tty_struct *tty)
272 struct usb_serial_port *port = tty->driver_data;
273 struct usb_serial *serial = port->serial;
276 dbg("%s - port %d", __func__, port->number);
279 if (serial->num_bulk_out) {
280 if (!(port->write_urb_busy))
281 room = port->bulk_out_size;
284 dbg("%s - returns %d", __func__, room);
288 int usb_serial_generic_chars_in_buffer(struct tty_struct *tty)
290 struct usb_serial_port *port = tty->driver_data;
291 struct usb_serial *serial = port->serial;
294 dbg("%s - port %d", __func__, port->number);
297 if (serial->num_bulk_out) {
298 if (port->write_urb_busy)
299 chars = port->write_urb->transfer_buffer_length;
302 dbg("%s - returns %d", __func__, chars);
307 static void resubmit_read_urb(struct usb_serial_port *port, gfp_t mem_flags)
309 struct urb *urb = port->read_urb;
310 struct usb_serial *serial = port->serial;
313 /* Continue reading from device */
314 usb_fill_bulk_urb(urb, serial->dev,
315 usb_rcvbulkpipe(serial->dev,
316 port->bulk_in_endpointAddress),
317 urb->transfer_buffer,
318 urb->transfer_buffer_length,
319 ((serial->type->read_bulk_callback) ?
320 serial->type->read_bulk_callback :
321 usb_serial_generic_read_bulk_callback), port);
322 result = usb_submit_urb(urb, mem_flags);
325 "%s - failed resubmitting read urb, error %d\n",
329 /* Push data to tty layer and resubmit the bulk read URB */
330 static void flush_and_resubmit_read_urb(struct usb_serial_port *port)
332 struct urb *urb = port->read_urb;
333 struct tty_struct *tty = tty_port_tty_get(&port->port);
336 /* Push data to tty */
337 if (tty && urb->actual_length) {
338 room = tty_buffer_request_room(tty, urb->actual_length);
340 tty_insert_flip_string(tty, urb->transfer_buffer, room);
341 tty_flip_buffer_push(tty);
346 resubmit_read_urb(port, GFP_ATOMIC);
349 void usb_serial_generic_read_bulk_callback(struct urb *urb)
351 struct usb_serial_port *port = urb->context;
352 unsigned char *data = urb->transfer_buffer;
353 int status = urb->status;
356 dbg("%s - port %d", __func__, port->number);
358 if (unlikely(status != 0)) {
359 dbg("%s - nonzero read bulk status received: %d",
364 usb_serial_debug_data(debug, &port->dev, __func__,
365 urb->actual_length, data);
367 /* Throttle the device if requested by tty */
368 spin_lock_irqsave(&port->lock, flags);
369 port->throttled = port->throttle_req;
370 if (!port->throttled) {
371 spin_unlock_irqrestore(&port->lock, flags);
372 flush_and_resubmit_read_urb(port);
374 spin_unlock_irqrestore(&port->lock, flags);
376 EXPORT_SYMBOL_GPL(usb_serial_generic_read_bulk_callback);
378 void usb_serial_generic_write_bulk_callback(struct urb *urb)
380 struct usb_serial_port *port = urb->context;
381 int status = urb->status;
383 dbg("%s - port %d", __func__, port->number);
385 port->write_urb_busy = 0;
387 dbg("%s - nonzero write bulk status received: %d",
391 usb_serial_port_softint(port);
393 EXPORT_SYMBOL_GPL(usb_serial_generic_write_bulk_callback);
395 void usb_serial_generic_throttle(struct tty_struct *tty)
397 struct usb_serial_port *port = tty->driver_data;
400 dbg("%s - port %d", __func__, port->number);
402 /* Set the throttle request flag. It will be picked up
403 * by usb_serial_generic_read_bulk_callback(). */
404 spin_lock_irqsave(&port->lock, flags);
405 port->throttle_req = 1;
406 spin_unlock_irqrestore(&port->lock, flags);
409 void usb_serial_generic_unthrottle(struct tty_struct *tty)
411 struct usb_serial_port *port = tty->driver_data;
415 dbg("%s - port %d", __func__, port->number);
417 /* Clear the throttle flags */
418 spin_lock_irqsave(&port->lock, flags);
419 was_throttled = port->throttled;
420 port->throttled = port->throttle_req = 0;
421 spin_unlock_irqrestore(&port->lock, flags);
424 /* Resume reading from device */
425 resubmit_read_urb(port, GFP_KERNEL);
429 void usb_serial_generic_shutdown(struct usb_serial *serial)
435 /* stop reads and writes on all ports */
436 for (i = 0; i < serial->num_ports; ++i)
437 generic_cleanup(serial->port[i]);