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_device_type usb_serial_generic_device = {
42 .short_name = "generic",
43 .id_table = generic_device_ids,
44 .num_interrupt_in = NUM_DONT_CARE,
45 .num_bulk_in = NUM_DONT_CARE,
46 .num_bulk_out = NUM_DONT_CARE,
48 .shutdown = usb_serial_generic_shutdown,
51 /* we want to look at all devices, as the vendor/product id can change
52 * depending on the command line argument */
53 static struct usb_device_id generic_serial_ids[] = {
58 static int generic_probe(struct usb_interface *interface,
59 const struct usb_device_id *id)
61 const struct usb_device_id *id_pattern;
63 id_pattern = usb_match_id(interface, generic_device_ids);
64 if (id_pattern != NULL)
65 return usb_serial_probe(interface, id);
69 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,
78 int usb_serial_generic_register (int _debug)
83 #ifdef CONFIG_USB_SERIAL_GENERIC
84 generic_device_ids[0].idVendor = vendor;
85 generic_device_ids[0].idProduct = product;
86 generic_device_ids[0].match_flags = USB_DEVICE_ID_MATCH_VENDOR | USB_DEVICE_ID_MATCH_PRODUCT;
88 /* register our generic driver with ourselves */
89 retval = usb_serial_register (&usb_serial_generic_device);
92 retval = usb_register(&generic_driver);
94 usb_serial_deregister(&usb_serial_generic_device);
100 void usb_serial_generic_deregister (void)
102 #ifdef CONFIG_USB_SERIAL_GENERIC
103 /* remove our generic driver */
104 usb_deregister(&generic_driver);
105 usb_serial_deregister (&usb_serial_generic_device);
109 int usb_serial_generic_open (struct usb_serial_port *port, struct file *filp)
111 struct usb_serial *serial = port->serial;
114 dbg("%s - port %d", __FUNCTION__, port->number);
116 /* force low_latency on so that our tty_push actually forces the data through,
117 otherwise it is scheduled, and with high data rates (like with OHCI) data
120 port->tty->low_latency = 1;
122 /* if we have a bulk interrupt, start reading from it */
123 if (serial->num_bulk_in) {
124 /* Start reading from the device */
125 usb_fill_bulk_urb (port->read_urb, serial->dev,
126 usb_rcvbulkpipe(serial->dev, port->bulk_in_endpointAddress),
127 port->read_urb->transfer_buffer,
128 port->read_urb->transfer_buffer_length,
129 ((serial->type->read_bulk_callback) ?
130 serial->type->read_bulk_callback :
131 usb_serial_generic_read_bulk_callback),
133 result = usb_submit_urb(port->read_urb, GFP_KERNEL);
135 dev_err(&port->dev, "%s - failed resubmitting read urb, error %d\n", __FUNCTION__, result);
141 static void generic_cleanup (struct usb_serial_port *port)
143 struct usb_serial *serial = port->serial;
145 dbg("%s - port %d", __FUNCTION__, port->number);
148 /* shutdown any bulk reads that might be going on */
149 if (serial->num_bulk_out)
150 usb_kill_urb(port->write_urb);
151 if (serial->num_bulk_in)
152 usb_kill_urb(port->read_urb);
156 void usb_serial_generic_close (struct usb_serial_port *port, struct file * filp)
158 dbg("%s - port %d", __FUNCTION__, port->number);
159 generic_cleanup (port);
162 int usb_serial_generic_write(struct usb_serial_port *port, const unsigned char *buf, int count)
164 struct usb_serial *serial = port->serial;
168 dbg("%s - port %d", __FUNCTION__, port->number);
171 dbg("%s - write request of 0 bytes", __FUNCTION__);
175 /* only do something if we have a bulk out endpoint */
176 if (serial->num_bulk_out) {
177 if (port->write_urb->status == -EINPROGRESS) {
178 dbg("%s - already writing", __FUNCTION__);
182 count = (count > port->bulk_out_size) ? port->bulk_out_size : count;
184 memcpy (port->write_urb->transfer_buffer, buf, count);
185 data = port->write_urb->transfer_buffer;
186 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, count, data);
189 usb_fill_bulk_urb (port->write_urb, serial->dev,
190 usb_sndbulkpipe (serial->dev,
191 port->bulk_out_endpointAddress),
192 port->write_urb->transfer_buffer, count,
193 ((serial->type->write_bulk_callback) ?
194 serial->type->write_bulk_callback :
195 usb_serial_generic_write_bulk_callback), port);
197 /* send the data out the bulk port */
198 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
200 dev_err(&port->dev, "%s - failed submitting write urb, error %d\n", __FUNCTION__, result);
207 /* no bulk out, so return 0 bytes written */
211 int usb_serial_generic_write_room (struct usb_serial_port *port)
213 struct usb_serial *serial = port->serial;
216 dbg("%s - port %d", __FUNCTION__, port->number);
218 if (serial->num_bulk_out) {
219 if (port->write_urb->status != -EINPROGRESS)
220 room = port->bulk_out_size;
223 dbg("%s - returns %d", __FUNCTION__, room);
227 int usb_serial_generic_chars_in_buffer (struct usb_serial_port *port)
229 struct usb_serial *serial = port->serial;
232 dbg("%s - port %d", __FUNCTION__, port->number);
234 if (serial->num_bulk_out) {
235 if (port->write_urb->status == -EINPROGRESS)
236 chars = port->write_urb->transfer_buffer_length;
239 dbg("%s - returns %d", __FUNCTION__, chars);
243 void usb_serial_generic_read_bulk_callback (struct urb *urb, struct pt_regs *regs)
245 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
246 struct usb_serial *serial = port->serial;
247 struct tty_struct *tty;
248 unsigned char *data = urb->transfer_buffer;
252 dbg("%s - port %d", __FUNCTION__, port->number);
255 dbg("%s - nonzero read bulk status received: %d", __FUNCTION__, urb->status);
259 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, urb->actual_length, data);
262 if (tty && urb->actual_length) {
263 for (i = 0; i < urb->actual_length ; ++i) {
264 /* if we insert more than TTY_FLIPBUF_SIZE characters, we drop them. */
265 if(tty->flip.count >= TTY_FLIPBUF_SIZE) {
266 tty_flip_buffer_push(tty);
268 /* this doesn't actually push the data through unless tty->low_latency is set */
269 tty_insert_flip_char(tty, data[i], 0);
271 tty_flip_buffer_push(tty);
274 /* Continue trying to always read */
275 usb_fill_bulk_urb (port->read_urb, serial->dev,
276 usb_rcvbulkpipe (serial->dev,
277 port->bulk_in_endpointAddress),
278 port->read_urb->transfer_buffer,
279 port->read_urb->transfer_buffer_length,
280 ((serial->type->read_bulk_callback) ?
281 serial->type->read_bulk_callback :
282 usb_serial_generic_read_bulk_callback), port);
283 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
285 dev_err(&port->dev, "%s - failed resubmitting read urb, error %d\n", __FUNCTION__, result);
288 void usb_serial_generic_write_bulk_callback (struct urb *urb, struct pt_regs *regs)
290 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
292 dbg("%s - port %d", __FUNCTION__, port->number);
295 dbg("%s - nonzero write bulk status received: %d", __FUNCTION__, urb->status);
299 usb_serial_port_softint((void *)port);
301 schedule_work(&port->work);
304 void usb_serial_generic_shutdown (struct usb_serial *serial)
308 dbg("%s", __FUNCTION__);
310 /* stop reads and writes on all ports */
311 for (i=0; i < serial->num_ports; ++i) {
312 generic_cleanup(serial->port[i]);