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/usb.h>
20 #include <asm/uaccess.h>
22 #ifdef CONFIG_USB_SERIAL_DEBUG
28 #include "usb-serial.h"
31 #ifdef CONFIG_USB_SERIAL_GENERIC
32 static __u16 vendor = 0x05f9;
33 static __u16 product = 0xffff;
35 MODULE_PARM(vendor, "h");
36 MODULE_PARM_DESC(vendor, "User specified USB idVendor");
38 MODULE_PARM(product, "h");
39 MODULE_PARM_DESC(product, "User specified USB idProduct");
41 static struct usb_device_id generic_device_ids[2]; /* Initially all zeroes. */
43 /* All of the device info needed for the Generic Serial Converter */
44 struct usb_serial_device_type usb_serial_generic_device = {
47 .short_name = "generic",
48 .id_table = generic_device_ids,
49 .num_interrupt_in = NUM_DONT_CARE,
50 .num_bulk_in = NUM_DONT_CARE,
51 .num_bulk_out = NUM_DONT_CARE,
53 .shutdown = usb_serial_generic_shutdown,
57 int usb_serial_generic_register (int _debug)
62 #ifdef CONFIG_USB_SERIAL_GENERIC
63 generic_device_ids[0].idVendor = vendor;
64 generic_device_ids[0].idProduct = product;
65 generic_device_ids[0].match_flags = USB_DEVICE_ID_MATCH_VENDOR | USB_DEVICE_ID_MATCH_PRODUCT;
67 /* register our generic driver with ourselves */
68 retval = usb_serial_register (&usb_serial_generic_device);
73 void usb_serial_generic_deregister (void)
75 #ifdef CONFIG_USB_SERIAL_GENERIC
76 /* remove our generic driver */
77 usb_serial_deregister (&usb_serial_generic_device);
81 int usb_serial_generic_open (struct usb_serial_port *port, struct file *filp)
83 struct usb_serial *serial = port->serial;
86 if (port_paranoia_check (port, __FUNCTION__))
89 dbg("%s - port %d", __FUNCTION__, port->number);
91 /* force low_latency on so that our tty_push actually forces the data through,
92 otherwise it is scheduled, and with high data rates (like with OHCI) data
95 port->tty->low_latency = 1;
97 /* if we have a bulk interrupt, start reading from it */
98 if (serial->num_bulk_in) {
99 /* Start reading from the device */
100 usb_fill_bulk_urb (port->read_urb, serial->dev,
101 usb_rcvbulkpipe(serial->dev, port->bulk_in_endpointAddress),
102 port->read_urb->transfer_buffer,
103 port->read_urb->transfer_buffer_length,
104 ((serial->type->read_bulk_callback) ?
105 serial->type->read_bulk_callback :
106 usb_serial_generic_read_bulk_callback),
108 result = usb_submit_urb(port->read_urb, GFP_KERNEL);
110 dev_err(&port->dev, "%s - failed resubmitting read urb, error %d\n", __FUNCTION__, result);
116 static void generic_cleanup (struct usb_serial_port *port)
118 struct usb_serial *serial = port->serial;
120 dbg("%s - port %d", __FUNCTION__, port->number);
123 /* shutdown any bulk reads that might be going on */
124 if (serial->num_bulk_out)
125 usb_unlink_urb (port->write_urb);
126 if (serial->num_bulk_in)
127 usb_unlink_urb (port->read_urb);
131 void usb_serial_generic_close (struct usb_serial_port *port, struct file * filp)
133 dbg("%s - port %d", __FUNCTION__, port->number);
134 generic_cleanup (port);
137 int usb_serial_generic_write (struct usb_serial_port *port, int from_user, const unsigned char *buf, int count)
139 struct usb_serial *serial = port->serial;
142 dbg("%s - port %d", __FUNCTION__, port->number);
145 dbg("%s - write request of 0 bytes", __FUNCTION__);
149 /* only do something if we have a bulk out endpoint */
150 if (serial->num_bulk_out) {
151 if (port->write_urb->status == -EINPROGRESS) {
152 dbg("%s - already writing", __FUNCTION__);
156 count = (count > port->bulk_out_size) ? port->bulk_out_size : count;
159 if (copy_from_user(port->write_urb->transfer_buffer, buf, count))
163 memcpy (port->write_urb->transfer_buffer, buf, count);
166 usb_serial_debug_data (__FILE__, __FUNCTION__, count, port->write_urb->transfer_buffer);
169 usb_fill_bulk_urb (port->write_urb, serial->dev,
170 usb_sndbulkpipe (serial->dev,
171 port->bulk_out_endpointAddress),
172 port->write_urb->transfer_buffer, count,
173 ((serial->type->write_bulk_callback) ?
174 serial->type->write_bulk_callback :
175 usb_serial_generic_write_bulk_callback), port);
177 /* send the data out the bulk port */
178 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
180 dev_err(&port->dev, "%s - failed submitting write urb, error %d\n", __FUNCTION__, result);
187 /* no bulk out, so return 0 bytes written */
191 int usb_serial_generic_write_room (struct usb_serial_port *port)
193 struct usb_serial *serial = port->serial;
196 dbg("%s - port %d", __FUNCTION__, port->number);
198 if (serial->num_bulk_out) {
199 if (port->write_urb->status != -EINPROGRESS)
200 room = port->bulk_out_size;
203 dbg("%s - returns %d", __FUNCTION__, room);
207 int usb_serial_generic_chars_in_buffer (struct usb_serial_port *port)
209 struct usb_serial *serial = port->serial;
212 dbg("%s - port %d", __FUNCTION__, port->number);
214 if (serial->num_bulk_out) {
215 if (port->write_urb->status == -EINPROGRESS)
216 chars = port->write_urb->transfer_buffer_length;
219 dbg("%s - returns %d", __FUNCTION__, chars);
223 void usb_serial_generic_read_bulk_callback (struct urb *urb, struct pt_regs *regs)
225 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
226 struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);
227 struct tty_struct *tty;
228 unsigned char *data = urb->transfer_buffer;
232 dbg("%s - port %d", __FUNCTION__, port->number);
235 dbg("%s - bad serial pointer, exiting", __FUNCTION__);
240 dbg("%s - nonzero read bulk status received: %d", __FUNCTION__, urb->status);
244 usb_serial_debug_data (__FILE__, __FUNCTION__, urb->actual_length, data);
247 if (tty && urb->actual_length) {
248 for (i = 0; i < urb->actual_length ; ++i) {
249 /* if we insert more than TTY_FLIPBUF_SIZE characters, we drop them. */
250 if(tty->flip.count >= TTY_FLIPBUF_SIZE) {
251 tty_flip_buffer_push(tty);
253 /* this doesn't actually push the data through unless tty->low_latency is set */
254 tty_insert_flip_char(tty, data[i], 0);
256 tty_flip_buffer_push(tty);
259 /* Continue trying to always read */
260 usb_fill_bulk_urb (port->read_urb, serial->dev,
261 usb_rcvbulkpipe (serial->dev,
262 port->bulk_in_endpointAddress),
263 port->read_urb->transfer_buffer,
264 port->read_urb->transfer_buffer_length,
265 ((serial->type->read_bulk_callback) ?
266 serial->type->read_bulk_callback :
267 usb_serial_generic_read_bulk_callback), port);
268 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
270 dev_err(&port->dev, "%s - failed resubmitting read urb, error %d\n", __FUNCTION__, result);
273 void usb_serial_generic_write_bulk_callback (struct urb *urb, struct pt_regs *regs)
275 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
276 struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);
278 dbg("%s - port %d", __FUNCTION__, port->number);
281 dbg("%s - bad serial pointer, exiting", __FUNCTION__);
286 dbg("%s - nonzero write bulk status received: %d", __FUNCTION__, urb->status);
290 usb_serial_port_softint((void *)port);
292 schedule_work(&port->work);
295 void usb_serial_generic_shutdown (struct usb_serial *serial)
299 dbg("%s", __FUNCTION__);
301 /* stop reads and writes on all ports */
302 for (i=0; i < serial->num_ports; ++i) {
303 generic_cleanup(serial->port[i]);