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>
23 #ifdef CONFIG_USB_SERIAL_DEBUG
29 #include "usb-serial.h"
32 #ifdef CONFIG_USB_SERIAL_GENERIC
33 static __u16 vendor = 0x05f9;
34 static __u16 product = 0xffff;
36 module_param(vendor, ushort, 0);
37 MODULE_PARM_DESC(vendor, "User specified USB idVendor");
39 module_param(product, ushort, 0);
40 MODULE_PARM_DESC(product, "User specified USB idProduct");
42 static struct usb_device_id generic_device_ids[2]; /* Initially all zeroes. */
44 /* All of the device info needed for the Generic Serial Converter */
45 struct usb_serial_device_type usb_serial_generic_device = {
48 .short_name = "generic",
49 .id_table = generic_device_ids,
50 .num_interrupt_in = NUM_DONT_CARE,
51 .num_bulk_in = NUM_DONT_CARE,
52 .num_bulk_out = NUM_DONT_CARE,
54 .shutdown = usb_serial_generic_shutdown,
58 int usb_serial_generic_register (int _debug)
63 #ifdef CONFIG_USB_SERIAL_GENERIC
64 generic_device_ids[0].idVendor = vendor;
65 generic_device_ids[0].idProduct = product;
66 generic_device_ids[0].match_flags = USB_DEVICE_ID_MATCH_VENDOR | USB_DEVICE_ID_MATCH_PRODUCT;
68 /* register our generic driver with ourselves */
69 retval = usb_serial_register (&usb_serial_generic_device);
74 void usb_serial_generic_deregister (void)
76 #ifdef CONFIG_USB_SERIAL_GENERIC
77 /* remove our generic driver */
78 usb_serial_deregister (&usb_serial_generic_device);
82 int usb_serial_generic_open (struct usb_serial_port *port, struct file *filp)
84 struct usb_serial *serial = port->serial;
87 dbg("%s - port %d", __FUNCTION__, port->number);
89 /* force low_latency on so that our tty_push actually forces the data through,
90 otherwise it is scheduled, and with high data rates (like with OHCI) data
93 port->tty->low_latency = 1;
95 /* if we have a bulk interrupt, start reading from it */
96 if (serial->num_bulk_in) {
97 /* Start reading from the device */
98 usb_fill_bulk_urb (port->read_urb, serial->dev,
99 usb_rcvbulkpipe(serial->dev, port->bulk_in_endpointAddress),
100 port->read_urb->transfer_buffer,
101 port->read_urb->transfer_buffer_length,
102 ((serial->type->read_bulk_callback) ?
103 serial->type->read_bulk_callback :
104 usb_serial_generic_read_bulk_callback),
106 result = usb_submit_urb(port->read_urb, GFP_KERNEL);
108 dev_err(&port->dev, "%s - failed resubmitting read urb, error %d\n", __FUNCTION__, result);
114 static void generic_cleanup (struct usb_serial_port *port)
116 struct usb_serial *serial = port->serial;
118 dbg("%s - port %d", __FUNCTION__, port->number);
121 /* shutdown any bulk reads that might be going on */
122 if (serial->num_bulk_out)
123 usb_unlink_urb (port->write_urb);
124 if (serial->num_bulk_in)
125 usb_unlink_urb (port->read_urb);
129 void usb_serial_generic_close (struct usb_serial_port *port, struct file * filp)
131 dbg("%s - port %d", __FUNCTION__, port->number);
132 generic_cleanup (port);
135 int usb_serial_generic_write (struct usb_serial_port *port, int from_user, const unsigned char *buf, int count)
137 struct usb_serial *serial = port->serial;
140 dbg("%s - port %d", __FUNCTION__, port->number);
143 dbg("%s - write request of 0 bytes", __FUNCTION__);
147 /* only do something if we have a bulk out endpoint */
148 if (serial->num_bulk_out) {
149 if (port->write_urb->status == -EINPROGRESS) {
150 dbg("%s - already writing", __FUNCTION__);
154 count = (count > port->bulk_out_size) ? port->bulk_out_size : count;
157 if (copy_from_user(port->write_urb->transfer_buffer, buf, count))
161 memcpy (port->write_urb->transfer_buffer, buf, count);
164 usb_serial_debug_data (__FILE__, __FUNCTION__, count, port->write_urb->transfer_buffer);
167 usb_fill_bulk_urb (port->write_urb, serial->dev,
168 usb_sndbulkpipe (serial->dev,
169 port->bulk_out_endpointAddress),
170 port->write_urb->transfer_buffer, count,
171 ((serial->type->write_bulk_callback) ?
172 serial->type->write_bulk_callback :
173 usb_serial_generic_write_bulk_callback), port);
175 /* send the data out the bulk port */
176 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
178 dev_err(&port->dev, "%s - failed submitting write urb, error %d\n", __FUNCTION__, result);
185 /* no bulk out, so return 0 bytes written */
189 int usb_serial_generic_write_room (struct usb_serial_port *port)
191 struct usb_serial *serial = port->serial;
194 dbg("%s - port %d", __FUNCTION__, port->number);
196 if (serial->num_bulk_out) {
197 if (port->write_urb->status != -EINPROGRESS)
198 room = port->bulk_out_size;
201 dbg("%s - returns %d", __FUNCTION__, room);
205 int usb_serial_generic_chars_in_buffer (struct usb_serial_port *port)
207 struct usb_serial *serial = port->serial;
210 dbg("%s - port %d", __FUNCTION__, port->number);
212 if (serial->num_bulk_out) {
213 if (port->write_urb->status == -EINPROGRESS)
214 chars = port->write_urb->transfer_buffer_length;
217 dbg("%s - returns %d", __FUNCTION__, chars);
221 void usb_serial_generic_read_bulk_callback (struct urb *urb, struct pt_regs *regs)
223 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
224 struct usb_serial *serial = port->serial;
225 struct tty_struct *tty;
226 unsigned char *data = urb->transfer_buffer;
230 dbg("%s - port %d", __FUNCTION__, port->number);
233 dbg("%s - nonzero read bulk status received: %d", __FUNCTION__, urb->status);
237 usb_serial_debug_data (__FILE__, __FUNCTION__, urb->actual_length, data);
240 if (tty && urb->actual_length) {
241 for (i = 0; i < urb->actual_length ; ++i) {
242 /* if we insert more than TTY_FLIPBUF_SIZE characters, we drop them. */
243 if(tty->flip.count >= TTY_FLIPBUF_SIZE) {
244 tty_flip_buffer_push(tty);
246 /* this doesn't actually push the data through unless tty->low_latency is set */
247 tty_insert_flip_char(tty, data[i], 0);
249 tty_flip_buffer_push(tty);
252 /* Continue trying to always read */
253 usb_fill_bulk_urb (port->read_urb, serial->dev,
254 usb_rcvbulkpipe (serial->dev,
255 port->bulk_in_endpointAddress),
256 port->read_urb->transfer_buffer,
257 port->read_urb->transfer_buffer_length,
258 ((serial->type->read_bulk_callback) ?
259 serial->type->read_bulk_callback :
260 usb_serial_generic_read_bulk_callback), port);
261 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
263 dev_err(&port->dev, "%s - failed resubmitting read urb, error %d\n", __FUNCTION__, result);
266 void usb_serial_generic_write_bulk_callback (struct urb *urb, struct pt_regs *regs)
268 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
270 dbg("%s - port %d", __FUNCTION__, port->number);
273 dbg("%s - nonzero write bulk status received: %d", __FUNCTION__, urb->status);
277 usb_serial_port_softint((void *)port);
279 schedule_work(&port->work);
282 void usb_serial_generic_shutdown (struct usb_serial *serial)
286 dbg("%s", __FUNCTION__);
288 /* stop reads and writes on all ports */
289 for (i=0; i < serial->num_ports; ++i) {
290 generic_cleanup(serial->port[i]);