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,
71 static int generic_probe(struct usb_interface *interface,
72 const struct usb_device_id *id)
74 const struct usb_device_id *id_pattern;
76 id_pattern = usb_match_id(interface, generic_device_ids);
77 if (id_pattern != NULL)
78 return usb_serial_probe(interface, id);
83 int usb_serial_generic_register (int _debug)
88 #ifdef CONFIG_USB_SERIAL_GENERIC
89 generic_device_ids[0].idVendor = vendor;
90 generic_device_ids[0].idProduct = product;
91 generic_device_ids[0].match_flags = USB_DEVICE_ID_MATCH_VENDOR | USB_DEVICE_ID_MATCH_PRODUCT;
93 /* register our generic driver with ourselves */
94 retval = usb_serial_register (&usb_serial_generic_device);
97 retval = usb_register(&generic_driver);
99 usb_serial_deregister(&usb_serial_generic_device);
105 void usb_serial_generic_deregister (void)
107 #ifdef CONFIG_USB_SERIAL_GENERIC
108 /* remove our generic driver */
109 usb_deregister(&generic_driver);
110 usb_serial_deregister (&usb_serial_generic_device);
114 int usb_serial_generic_open (struct usb_serial_port *port, struct file *filp)
116 struct usb_serial *serial = port->serial;
119 dbg("%s - port %d", __FUNCTION__, port->number);
121 /* force low_latency on so that our tty_push actually forces the data through,
122 otherwise it is scheduled, and with high data rates (like with OHCI) data
125 port->tty->low_latency = 1;
127 /* if we have a bulk interrupt, start reading from it */
128 if (serial->num_bulk_in) {
129 /* Start reading from the device */
130 usb_fill_bulk_urb (port->read_urb, serial->dev,
131 usb_rcvbulkpipe(serial->dev, port->bulk_in_endpointAddress),
132 port->read_urb->transfer_buffer,
133 port->read_urb->transfer_buffer_length,
134 ((serial->type->read_bulk_callback) ?
135 serial->type->read_bulk_callback :
136 usb_serial_generic_read_bulk_callback),
138 result = usb_submit_urb(port->read_urb, GFP_KERNEL);
140 dev_err(&port->dev, "%s - failed resubmitting read urb, error %d\n", __FUNCTION__, result);
145 EXPORT_SYMBOL_GPL(usb_serial_generic_open);
147 static void generic_cleanup (struct usb_serial_port *port)
149 struct usb_serial *serial = port->serial;
151 dbg("%s - port %d", __FUNCTION__, port->number);
154 /* shutdown any bulk reads that might be going on */
155 if (serial->num_bulk_out)
156 usb_kill_urb(port->write_urb);
157 if (serial->num_bulk_in)
158 usb_kill_urb(port->read_urb);
162 void usb_serial_generic_close (struct usb_serial_port *port, struct file * filp)
164 dbg("%s - port %d", __FUNCTION__, port->number);
165 generic_cleanup (port);
168 int usb_serial_generic_write(struct usb_serial_port *port, const unsigned char *buf, int count)
170 struct usb_serial *serial = port->serial;
174 dbg("%s - port %d", __FUNCTION__, port->number);
177 dbg("%s - write request of 0 bytes", __FUNCTION__);
181 /* only do something if we have a bulk out endpoint */
182 if (serial->num_bulk_out) {
183 spin_lock_bh(&port->lock);
184 if (port->write_urb_busy) {
185 spin_unlock_bh(&port->lock);
186 dbg("%s - already writing", __FUNCTION__);
189 port->write_urb_busy = 1;
190 spin_unlock_bh(&port->lock);
192 count = (count > port->bulk_out_size) ? port->bulk_out_size : count;
194 memcpy (port->write_urb->transfer_buffer, buf, count);
195 data = port->write_urb->transfer_buffer;
196 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, count, data);
199 usb_fill_bulk_urb (port->write_urb, serial->dev,
200 usb_sndbulkpipe (serial->dev,
201 port->bulk_out_endpointAddress),
202 port->write_urb->transfer_buffer, count,
203 ((serial->type->write_bulk_callback) ?
204 serial->type->write_bulk_callback :
205 usb_serial_generic_write_bulk_callback), port);
207 /* send the data out the bulk port */
208 port->write_urb_busy = 1;
209 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
211 dev_err(&port->dev, "%s - failed submitting write urb, error %d\n", __FUNCTION__, result);
212 /* don't have to grab the lock here, as we will retry if != 0 */
213 port->write_urb_busy = 0;
220 /* no bulk out, so return 0 bytes written */
224 int usb_serial_generic_write_room (struct usb_serial_port *port)
226 struct usb_serial *serial = port->serial;
229 dbg("%s - port %d", __FUNCTION__, port->number);
231 if (serial->num_bulk_out) {
232 if (!(port->write_urb_busy))
233 room = port->bulk_out_size;
236 dbg("%s - returns %d", __FUNCTION__, room);
240 int usb_serial_generic_chars_in_buffer (struct usb_serial_port *port)
242 struct usb_serial *serial = port->serial;
245 dbg("%s - port %d", __FUNCTION__, port->number);
247 if (serial->num_bulk_out) {
248 if (port->write_urb_busy)
249 chars = port->write_urb->transfer_buffer_length;
252 dbg("%s - returns %d", __FUNCTION__, chars);
256 void usb_serial_generic_read_bulk_callback (struct urb *urb)
258 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
259 struct usb_serial *serial = port->serial;
260 struct tty_struct *tty;
261 unsigned char *data = urb->transfer_buffer;
264 dbg("%s - port %d", __FUNCTION__, port->number);
267 dbg("%s - nonzero read bulk status received: %d", __FUNCTION__, urb->status);
271 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, urb->actual_length, data);
274 if (tty && urb->actual_length) {
275 tty_buffer_request_room(tty, urb->actual_length);
276 tty_insert_flip_string(tty, data, urb->actual_length);
277 tty_flip_buffer_push(tty);
280 /* Continue trying to always read */
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);
293 EXPORT_SYMBOL_GPL(usb_serial_generic_read_bulk_callback);
295 void usb_serial_generic_write_bulk_callback (struct urb *urb)
297 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
299 dbg("%s - port %d", __FUNCTION__, port->number);
301 port->write_urb_busy = 0;
303 dbg("%s - nonzero write bulk status received: %d", __FUNCTION__, urb->status);
307 usb_serial_port_softint(port);
309 EXPORT_SYMBOL_GPL(usb_serial_generic_write_bulk_callback);
311 void usb_serial_generic_shutdown (struct usb_serial *serial)
315 dbg("%s", __FUNCTION__);
317 /* stop reads and writes on all ports */
318 for (i=0; i < serial->num_ports; ++i) {
319 generic_cleanup(serial->port[i]);