blob: 53a47c31cd0ea32f6e2985e85fcb0304d0a49820 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * USB Serial Converter Generic functions
3 *
4 * Copyright (C) 1999 - 2002 Greg Kroah-Hartman (greg@kroah.com)
5 *
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.
9 *
10 */
11
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"
23
24static int debug;
25
26#ifdef CONFIG_USB_SERIAL_GENERIC
27static __u16 vendor = 0x05f9;
28static __u16 product = 0xffff;
29
30module_param(vendor, ushort, 0);
31MODULE_PARM_DESC(vendor, "User specified USB idVendor");
32
33module_param(product, ushort, 0);
34MODULE_PARM_DESC(product, "User specified USB idProduct");
35
36static struct usb_device_id generic_device_ids[2]; /* Initially all zeroes. */
37
38/* All of the device info needed for the Generic Serial Converter */
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -070039struct usb_serial_driver usb_serial_generic_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -070040 .driver = {
41 .owner = THIS_MODULE,
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -070042 .name = "generic",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -070043 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 .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,
48 .num_ports = 1,
49 .shutdown = usb_serial_generic_shutdown,
50};
51
52/* we want to look at all devices, as the vendor/product id can change
53 * depending on the command line argument */
54static struct usb_device_id generic_serial_ids[] = {
55 {.driver_info = 42},
56 {}
57};
58
59static int generic_probe(struct usb_interface *interface,
60 const struct usb_device_id *id)
61{
62 const struct usb_device_id *id_pattern;
63
64 id_pattern = usb_match_id(interface, generic_device_ids);
65 if (id_pattern != NULL)
66 return usb_serial_probe(interface, id);
67 return -ENODEV;
68}
69
70static struct usb_driver generic_driver = {
71 .owner = THIS_MODULE,
72 .name = "usbserial_generic",
73 .probe = generic_probe,
74 .disconnect = usb_serial_disconnect,
75 .id_table = generic_serial_ids,
76};
77#endif
78
79int usb_serial_generic_register (int _debug)
80{
81 int retval = 0;
82
83 debug = _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;
88
89 /* register our generic driver with ourselves */
90 retval = usb_serial_register (&usb_serial_generic_device);
91 if (retval)
92 goto exit;
93 retval = usb_register(&generic_driver);
94 if (retval)
95 usb_serial_deregister(&usb_serial_generic_device);
96exit:
97#endif
98 return retval;
99}
100
101void usb_serial_generic_deregister (void)
102{
103#ifdef CONFIG_USB_SERIAL_GENERIC
104 /* remove our generic driver */
105 usb_deregister(&generic_driver);
106 usb_serial_deregister (&usb_serial_generic_device);
107#endif
108}
109
110int usb_serial_generic_open (struct usb_serial_port *port, struct file *filp)
111{
112 struct usb_serial *serial = port->serial;
113 int result = 0;
114
115 dbg("%s - port %d", __FUNCTION__, port->number);
116
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
119 can get lost. */
120 if (port->tty)
121 port->tty->low_latency = 1;
122
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),
133 port);
134 result = usb_submit_urb(port->read_urb, GFP_KERNEL);
135 if (result)
136 dev_err(&port->dev, "%s - failed resubmitting read urb, error %d\n", __FUNCTION__, result);
137 }
138
139 return result;
140}
141
142static void generic_cleanup (struct usb_serial_port *port)
143{
144 struct usb_serial *serial = port->serial;
145
146 dbg("%s - port %d", __FUNCTION__, port->number);
147
148 if (serial->dev) {
149 /* shutdown any bulk reads that might be going on */
150 if (serial->num_bulk_out)
151 usb_kill_urb(port->write_urb);
152 if (serial->num_bulk_in)
153 usb_kill_urb(port->read_urb);
154 }
155}
156
157void usb_serial_generic_close (struct usb_serial_port *port, struct file * filp)
158{
159 dbg("%s - port %d", __FUNCTION__, port->number);
160 generic_cleanup (port);
161}
162
163int usb_serial_generic_write(struct usb_serial_port *port, const unsigned char *buf, int count)
164{
165 struct usb_serial *serial = port->serial;
166 int result;
167 unsigned char *data;
168
169 dbg("%s - port %d", __FUNCTION__, port->number);
170
171 if (count == 0) {
172 dbg("%s - write request of 0 bytes", __FUNCTION__);
173 return (0);
174 }
175
176 /* only do something if we have a bulk out endpoint */
177 if (serial->num_bulk_out) {
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700178 spin_lock(&port->lock);
179 if (port->write_urb_busy) {
180 spin_unlock(&port->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 dbg("%s - already writing", __FUNCTION__);
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700182 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 }
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700184 port->write_urb_busy = 1;
185 spin_unlock(&port->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
187 count = (count > port->bulk_out_size) ? port->bulk_out_size : count;
188
189 memcpy (port->write_urb->transfer_buffer, buf, count);
190 data = port->write_urb->transfer_buffer;
191 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, count, data);
192
193 /* set up our urb */
194 usb_fill_bulk_urb (port->write_urb, serial->dev,
195 usb_sndbulkpipe (serial->dev,
196 port->bulk_out_endpointAddress),
197 port->write_urb->transfer_buffer, count,
198 ((serial->type->write_bulk_callback) ?
199 serial->type->write_bulk_callback :
200 usb_serial_generic_write_bulk_callback), port);
201
202 /* send the data out the bulk port */
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700203 port->write_urb_busy = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700205 if (result) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 dev_err(&port->dev, "%s - failed submitting write urb, error %d\n", __FUNCTION__, result);
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700207 /* don't have to grab the lock here, as we will retry if != 0 */
208 port->write_urb_busy = 0;
209 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 result = count;
211
212 return result;
213 }
214
215 /* no bulk out, so return 0 bytes written */
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700216 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217}
218
219int usb_serial_generic_write_room (struct usb_serial_port *port)
220{
221 struct usb_serial *serial = port->serial;
222 int room = 0;
223
224 dbg("%s - port %d", __FUNCTION__, port->number);
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700225
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 if (serial->num_bulk_out) {
Randall Nortman7a3ca7d2005-10-14 17:21:50 -0700227 if (!(port->write_urb_busy))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 room = port->bulk_out_size;
229 }
230
231 dbg("%s - returns %d", __FUNCTION__, room);
232 return (room);
233}
234
235int usb_serial_generic_chars_in_buffer (struct usb_serial_port *port)
236{
237 struct usb_serial *serial = port->serial;
238 int chars = 0;
239
240 dbg("%s - port %d", __FUNCTION__, port->number);
241
242 if (serial->num_bulk_out) {
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700243 if (port->write_urb_busy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 chars = port->write_urb->transfer_buffer_length;
245 }
246
247 dbg("%s - returns %d", __FUNCTION__, chars);
248 return (chars);
249}
250
251void usb_serial_generic_read_bulk_callback (struct urb *urb, struct pt_regs *regs)
252{
253 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
254 struct usb_serial *serial = port->serial;
255 struct tty_struct *tty;
256 unsigned char *data = urb->transfer_buffer;
257 int i;
258 int result;
259
260 dbg("%s - port %d", __FUNCTION__, port->number);
261
262 if (urb->status) {
263 dbg("%s - nonzero read bulk status received: %d", __FUNCTION__, urb->status);
264 return;
265 }
266
267 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, urb->actual_length, data);
268
269 tty = port->tty;
270 if (tty && urb->actual_length) {
271 for (i = 0; i < urb->actual_length ; ++i) {
272 /* if we insert more than TTY_FLIPBUF_SIZE characters, we drop them. */
273 if(tty->flip.count >= TTY_FLIPBUF_SIZE) {
274 tty_flip_buffer_push(tty);
275 }
276 /* this doesn't actually push the data through unless tty->low_latency is set */
277 tty_insert_flip_char(tty, data[i], 0);
278 }
279 tty_flip_buffer_push(tty);
280 }
281
282 /* Continue trying to always read */
283 usb_fill_bulk_urb (port->read_urb, serial->dev,
284 usb_rcvbulkpipe (serial->dev,
285 port->bulk_in_endpointAddress),
286 port->read_urb->transfer_buffer,
287 port->read_urb->transfer_buffer_length,
288 ((serial->type->read_bulk_callback) ?
289 serial->type->read_bulk_callback :
290 usb_serial_generic_read_bulk_callback), port);
291 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
292 if (result)
293 dev_err(&port->dev, "%s - failed resubmitting read urb, error %d\n", __FUNCTION__, result);
294}
295
296void usb_serial_generic_write_bulk_callback (struct urb *urb, struct pt_regs *regs)
297{
298 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
299
300 dbg("%s - port %d", __FUNCTION__, port->number);
301
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700302 port->write_urb_busy = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 if (urb->status) {
304 dbg("%s - nonzero write bulk status received: %d", __FUNCTION__, urb->status);
305 return;
306 }
307
308 usb_serial_port_softint((void *)port);
309
310 schedule_work(&port->work);
311}
Greg Kroah-Hartmanbb833982005-11-17 09:48:18 -0800312EXPORT_SYMBOL_GPL(usb_serial_generic_write_bulk_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
314void usb_serial_generic_shutdown (struct usb_serial *serial)
315{
316 int i;
317
318 dbg("%s", __FUNCTION__);
319
320 /* stop reads and writes on all ports */
321 for (i=0; i < serial->num_ports; ++i) {
322 generic_cleanup(serial->port[i]);
323 }
324}
325