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/config.h>
13 #include <linux/kernel.h>
14 #include <linux/errno.h>
15 #include <linux/slab.h>
16 #include <linux/tty.h>
17 #include <linux/tty_flip.h>
18 #include <linux/module.h>
19 #include <linux/moduleparam.h>
20 #include <linux/usb.h>
21 #include <asm/uaccess.h>
22 #include "usb-serial.h"
26 #ifdef CONFIG_USB_SERIAL_GENERIC
27 static __u16 vendor = 0x05f9;
28 static __u16 product = 0xffff;
30 module_param(vendor, ushort, 0);
31 MODULE_PARM_DESC(vendor, "User specified USB idVendor");
33 module_param(product, ushort, 0);
34 MODULE_PARM_DESC(product, "User specified USB idProduct");
36 static struct usb_device_id generic_device_ids[2]; /* Initially all zeroes. */
38 /* All of the device info needed for the Generic Serial Converter */
39 struct usb_serial_driver usb_serial_generic_device = {
44 .id_table = generic_device_ids,
45 .num_interrupt_in = NUM_DONT_CARE,
46 .num_bulk_in = NUM_DONT_CARE,
47 .num_bulk_out = NUM_DONT_CARE,
49 .shutdown = usb_serial_generic_shutdown,
52 /* we want to look at all devices, as the vendor/product id can change
53 * depending on the command line argument */
54 static struct usb_device_id generic_serial_ids[] = {
59 static int generic_probe(struct usb_interface *interface,
60 const struct usb_device_id *id)
62 const struct usb_device_id *id_pattern;
64 id_pattern = usb_match_id(interface, generic_device_ids);
65 if (id_pattern != NULL)
66 return usb_serial_probe(interface, id);
70 static struct usb_driver generic_driver = {
71 .name = "usbserial_generic",
72 .probe = generic_probe,
73 .disconnect = usb_serial_disconnect,
74 .id_table = generic_serial_ids,
79 int usb_serial_generic_register (int _debug)
84 #ifdef CONFIG_USB_SERIAL_GENERIC
85 generic_device_ids[0].idVendor = vendor;
86 generic_device_ids[0].idProduct = product;
87 generic_device_ids[0].match_flags = USB_DEVICE_ID_MATCH_VENDOR | USB_DEVICE_ID_MATCH_PRODUCT;
89 /* register our generic driver with ourselves */
90 retval = usb_serial_register (&usb_serial_generic_device);
93 retval = usb_register(&generic_driver);
95 usb_serial_deregister(&usb_serial_generic_device);
101 void usb_serial_generic_deregister (void)
103 #ifdef CONFIG_USB_SERIAL_GENERIC
104 /* remove our generic driver */
105 usb_deregister(&generic_driver);
106 usb_serial_deregister (&usb_serial_generic_device);
110 int usb_serial_generic_open (struct usb_serial_port *port, struct file *filp)
112 struct usb_serial *serial = port->serial;
115 dbg("%s - port %d", __FUNCTION__, port->number);
117 /* force low_latency on so that our tty_push actually forces the data through,
118 otherwise it is scheduled, and with high data rates (like with OHCI) data
121 port->tty->low_latency = 1;
123 /* if we have a bulk interrupt, start reading from it */
124 if (serial->num_bulk_in) {
125 /* Start reading from the device */
126 usb_fill_bulk_urb (port->read_urb, serial->dev,
127 usb_rcvbulkpipe(serial->dev, port->bulk_in_endpointAddress),
128 port->read_urb->transfer_buffer,
129 port->read_urb->transfer_buffer_length,
130 ((serial->type->read_bulk_callback) ?
131 serial->type->read_bulk_callback :
132 usb_serial_generic_read_bulk_callback),
134 result = usb_submit_urb(port->read_urb, GFP_KERNEL);
136 dev_err(&port->dev, "%s - failed resubmitting read urb, error %d\n", __FUNCTION__, result);
141 EXPORT_SYMBOL_GPL(usb_serial_generic_open);
143 static void generic_cleanup (struct usb_serial_port *port)
145 struct usb_serial *serial = port->serial;
147 dbg("%s - port %d", __FUNCTION__, port->number);
150 /* shutdown any bulk reads that might be going on */
151 if (serial->num_bulk_out)
152 usb_kill_urb(port->write_urb);
153 if (serial->num_bulk_in)
154 usb_kill_urb(port->read_urb);
158 void usb_serial_generic_close (struct usb_serial_port *port, struct file * filp)
160 dbg("%s - port %d", __FUNCTION__, port->number);
161 generic_cleanup (port);
164 int usb_serial_generic_write(struct usb_serial_port *port, const unsigned char *buf, int count)
166 struct usb_serial *serial = port->serial;
170 dbg("%s - port %d", __FUNCTION__, port->number);
173 dbg("%s - write request of 0 bytes", __FUNCTION__);
177 /* only do something if we have a bulk out endpoint */
178 if (serial->num_bulk_out) {
179 spin_lock(&port->lock);
180 if (port->write_urb_busy) {
181 spin_unlock(&port->lock);
182 dbg("%s - already writing", __FUNCTION__);
185 port->write_urb_busy = 1;
186 spin_unlock(&port->lock);
188 count = (count > port->bulk_out_size) ? port->bulk_out_size : count;
190 memcpy (port->write_urb->transfer_buffer, buf, count);
191 data = port->write_urb->transfer_buffer;
192 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, count, data);
195 usb_fill_bulk_urb (port->write_urb, serial->dev,
196 usb_sndbulkpipe (serial->dev,
197 port->bulk_out_endpointAddress),
198 port->write_urb->transfer_buffer, count,
199 ((serial->type->write_bulk_callback) ?
200 serial->type->write_bulk_callback :
201 usb_serial_generic_write_bulk_callback), port);
203 /* send the data out the bulk port */
204 port->write_urb_busy = 1;
205 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
207 dev_err(&port->dev, "%s - failed submitting write urb, error %d\n", __FUNCTION__, result);
208 /* don't have to grab the lock here, as we will retry if != 0 */
209 port->write_urb_busy = 0;
216 /* no bulk out, so return 0 bytes written */
220 int usb_serial_generic_write_room (struct usb_serial_port *port)
222 struct usb_serial *serial = port->serial;
225 dbg("%s - port %d", __FUNCTION__, port->number);
227 if (serial->num_bulk_out) {
228 if (!(port->write_urb_busy))
229 room = port->bulk_out_size;
232 dbg("%s - returns %d", __FUNCTION__, room);
236 int usb_serial_generic_chars_in_buffer (struct usb_serial_port *port)
238 struct usb_serial *serial = port->serial;
241 dbg("%s - port %d", __FUNCTION__, port->number);
243 if (serial->num_bulk_out) {
244 if (port->write_urb_busy)
245 chars = port->write_urb->transfer_buffer_length;
248 dbg("%s - returns %d", __FUNCTION__, chars);
252 void usb_serial_generic_read_bulk_callback (struct urb *urb, struct pt_regs *regs)
254 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
255 struct usb_serial *serial = port->serial;
256 struct tty_struct *tty;
257 unsigned char *data = urb->transfer_buffer;
260 dbg("%s - port %d", __FUNCTION__, port->number);
263 dbg("%s - nonzero read bulk status received: %d", __FUNCTION__, urb->status);
267 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, urb->actual_length, data);
270 if (tty && urb->actual_length) {
271 tty_buffer_request_room(tty, urb->actual_length);
272 tty_insert_flip_string(tty, data, urb->actual_length);
273 tty_flip_buffer_push(tty);
276 /* Continue trying to always read */
277 usb_fill_bulk_urb (port->read_urb, serial->dev,
278 usb_rcvbulkpipe (serial->dev,
279 port->bulk_in_endpointAddress),
280 port->read_urb->transfer_buffer,
281 port->read_urb->transfer_buffer_length,
282 ((serial->type->read_bulk_callback) ?
283 serial->type->read_bulk_callback :
284 usb_serial_generic_read_bulk_callback), port);
285 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
287 dev_err(&port->dev, "%s - failed resubmitting read urb, error %d\n", __FUNCTION__, result);
290 void usb_serial_generic_write_bulk_callback (struct urb *urb, struct pt_regs *regs)
292 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
294 dbg("%s - port %d", __FUNCTION__, port->number);
296 port->write_urb_busy = 0;
298 dbg("%s - nonzero write bulk status received: %d", __FUNCTION__, urb->status);
302 usb_serial_port_softint((void *)port);
304 schedule_work(&port->work);
306 EXPORT_SYMBOL_GPL(usb_serial_generic_write_bulk_callback);
308 void usb_serial_generic_shutdown (struct usb_serial *serial)
312 dbg("%s", __FUNCTION__);
314 /* stop reads and writes on all ports */
315 for (i=0; i < serial->num_ports; ++i) {
316 generic_cleanup(serial->port[i]);