blob: 601e0648dec68fbaaa4ecc10e3306820ed9ae154 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/kernel.h>
13#include <linux/errno.h>
14#include <linux/slab.h>
15#include <linux/tty.h>
16#include <linux/tty_flip.h>
17#include <linux/module.h>
18#include <linux/moduleparam.h>
19#include <linux/usb.h>
Greg Kroah-Hartmana9698882006-07-11 21:22:58 -070020#include <linux/usb/serial.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <asm/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
Johannes Hölzld9b1b782006-12-17 21:50:24 +010023static int generic_probe(struct usb_interface *interface,
24 const struct usb_device_id *id);
25
26
Linus Torvalds1da177e2005-04-16 15:20:36 -070027static int debug;
28
29#ifdef CONFIG_USB_SERIAL_GENERIC
30static __u16 vendor = 0x05f9;
31static __u16 product = 0xffff;
32
33module_param(vendor, ushort, 0);
34MODULE_PARM_DESC(vendor, "User specified USB idVendor");
35
36module_param(product, ushort, 0);
37MODULE_PARM_DESC(product, "User specified USB idProduct");
38
39static struct usb_device_id generic_device_ids[2]; /* Initially all zeroes. */
40
Johannes Hölzld9b1b782006-12-17 21:50:24 +010041/* we want to look at all devices, as the vendor/product id can change
42 * depending on the command line argument */
43static struct usb_device_id generic_serial_ids[] = {
44 {.driver_info = 42},
45 {}
46};
47
48static struct usb_driver generic_driver = {
49 .name = "usbserial_generic",
50 .probe = generic_probe,
51 .disconnect = usb_serial_disconnect,
52 .id_table = generic_serial_ids,
53 .no_dynamic_id = 1,
54};
55
Linus Torvalds1da177e2005-04-16 15:20:36 -070056/* All of the device info needed for the Generic Serial Converter */
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -070057struct usb_serial_driver usb_serial_generic_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -070058 .driver = {
59 .owner = THIS_MODULE,
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -070060 .name = "generic",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -070061 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 .id_table = generic_device_ids,
Johannes Hölzld9b1b782006-12-17 21:50:24 +010063 .usb_driver = &generic_driver,
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 .num_interrupt_in = NUM_DONT_CARE,
65 .num_bulk_in = NUM_DONT_CARE,
66 .num_bulk_out = NUM_DONT_CARE,
67 .num_ports = 1,
68 .shutdown = usb_serial_generic_shutdown,
69};
70
Linus Torvalds1da177e2005-04-16 15:20:36 -070071static int generic_probe(struct usb_interface *interface,
72 const struct usb_device_id *id)
73{
74 const struct usb_device_id *id_pattern;
75
76 id_pattern = usb_match_id(interface, generic_device_ids);
77 if (id_pattern != NULL)
78 return usb_serial_probe(interface, id);
79 return -ENODEV;
80}
Linus Torvalds1da177e2005-04-16 15:20:36 -070081#endif
82
83int usb_serial_generic_register (int _debug)
84{
85 int retval = 0;
86
87 debug = _debug;
88#ifdef CONFIG_USB_SERIAL_GENERIC
89 generic_device_ids[0].idVendor = vendor;
90 generic_device_ids[0].idProduct = product;
91 generic_device_ids[0].match_flags = USB_DEVICE_ID_MATCH_VENDOR | USB_DEVICE_ID_MATCH_PRODUCT;
92
93 /* register our generic driver with ourselves */
94 retval = usb_serial_register (&usb_serial_generic_device);
95 if (retval)
96 goto exit;
97 retval = usb_register(&generic_driver);
98 if (retval)
99 usb_serial_deregister(&usb_serial_generic_device);
100exit:
101#endif
102 return retval;
103}
104
105void usb_serial_generic_deregister (void)
106{
107#ifdef CONFIG_USB_SERIAL_GENERIC
108 /* remove our generic driver */
109 usb_deregister(&generic_driver);
110 usb_serial_deregister (&usb_serial_generic_device);
111#endif
112}
113
114int usb_serial_generic_open (struct usb_serial_port *port, struct file *filp)
115{
116 struct usb_serial *serial = port->serial;
117 int result = 0;
118
119 dbg("%s - port %d", __FUNCTION__, port->number);
120
121 /* force low_latency on so that our tty_push actually forces the data through,
122 otherwise it is scheduled, and with high data rates (like with OHCI) data
123 can get lost. */
124 if (port->tty)
125 port->tty->low_latency = 1;
126
127 /* if we have a bulk interrupt, start reading from it */
128 if (serial->num_bulk_in) {
129 /* Start reading from the device */
130 usb_fill_bulk_urb (port->read_urb, serial->dev,
131 usb_rcvbulkpipe(serial->dev, port->bulk_in_endpointAddress),
132 port->read_urb->transfer_buffer,
133 port->read_urb->transfer_buffer_length,
134 ((serial->type->read_bulk_callback) ?
135 serial->type->read_bulk_callback :
136 usb_serial_generic_read_bulk_callback),
137 port);
138 result = usb_submit_urb(port->read_urb, GFP_KERNEL);
139 if (result)
140 dev_err(&port->dev, "%s - failed resubmitting read urb, error %d\n", __FUNCTION__, result);
141 }
142
143 return result;
144}
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700145EXPORT_SYMBOL_GPL(usb_serial_generic_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
147static void generic_cleanup (struct usb_serial_port *port)
148{
149 struct usb_serial *serial = port->serial;
150
151 dbg("%s - port %d", __FUNCTION__, port->number);
152
153 if (serial->dev) {
154 /* shutdown any bulk reads that might be going on */
155 if (serial->num_bulk_out)
156 usb_kill_urb(port->write_urb);
157 if (serial->num_bulk_in)
158 usb_kill_urb(port->read_urb);
159 }
160}
161
162void usb_serial_generic_close (struct usb_serial_port *port, struct file * filp)
163{
164 dbg("%s - port %d", __FUNCTION__, port->number);
165 generic_cleanup (port);
166}
167
168int usb_serial_generic_write(struct usb_serial_port *port, const unsigned char *buf, int count)
169{
170 struct usb_serial *serial = port->serial;
171 int result;
172 unsigned char *data;
173
174 dbg("%s - port %d", __FUNCTION__, port->number);
175
176 if (count == 0) {
177 dbg("%s - write request of 0 bytes", __FUNCTION__);
178 return (0);
179 }
180
181 /* only do something if we have a bulk out endpoint */
182 if (serial->num_bulk_out) {
Peter Zijlstrae81ee632006-09-25 12:51:41 +0200183 spin_lock_bh(&port->lock);
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700184 if (port->write_urb_busy) {
Peter Zijlstrae81ee632006-09-25 12:51:41 +0200185 spin_unlock_bh(&port->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 dbg("%s - already writing", __FUNCTION__);
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700187 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 }
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700189 port->write_urb_busy = 1;
Peter Zijlstrae81ee632006-09-25 12:51:41 +0200190 spin_unlock_bh(&port->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
192 count = (count > port->bulk_out_size) ? port->bulk_out_size : count;
193
194 memcpy (port->write_urb->transfer_buffer, buf, count);
195 data = port->write_urb->transfer_buffer;
196 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, count, data);
197
198 /* set up our urb */
199 usb_fill_bulk_urb (port->write_urb, serial->dev,
200 usb_sndbulkpipe (serial->dev,
201 port->bulk_out_endpointAddress),
202 port->write_urb->transfer_buffer, count,
203 ((serial->type->write_bulk_callback) ?
204 serial->type->write_bulk_callback :
205 usb_serial_generic_write_bulk_callback), port);
206
207 /* send the data out the bulk port */
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700208 port->write_urb_busy = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700210 if (result) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 dev_err(&port->dev, "%s - failed submitting write urb, error %d\n", __FUNCTION__, result);
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700212 /* don't have to grab the lock here, as we will retry if != 0 */
213 port->write_urb_busy = 0;
214 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 result = count;
216
217 return result;
218 }
219
220 /* no bulk out, so return 0 bytes written */
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700221 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222}
223
224int usb_serial_generic_write_room (struct usb_serial_port *port)
225{
226 struct usb_serial *serial = port->serial;
227 int room = 0;
228
229 dbg("%s - port %d", __FUNCTION__, port->number);
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700230
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 if (serial->num_bulk_out) {
Randall Nortman7a3ca7d2005-10-14 17:21:50 -0700232 if (!(port->write_urb_busy))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 room = port->bulk_out_size;
234 }
235
236 dbg("%s - returns %d", __FUNCTION__, room);
237 return (room);
238}
239
240int usb_serial_generic_chars_in_buffer (struct usb_serial_port *port)
241{
242 struct usb_serial *serial = port->serial;
243 int chars = 0;
244
245 dbg("%s - port %d", __FUNCTION__, port->number);
246
247 if (serial->num_bulk_out) {
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700248 if (port->write_urb_busy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 chars = port->write_urb->transfer_buffer_length;
250 }
251
252 dbg("%s - returns %d", __FUNCTION__, chars);
253 return (chars);
254}
255
David Howells7d12e782006-10-05 14:55:46 +0100256void usb_serial_generic_read_bulk_callback (struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257{
258 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
259 struct usb_serial *serial = port->serial;
260 struct tty_struct *tty;
261 unsigned char *data = urb->transfer_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 int result;
263
264 dbg("%s - port %d", __FUNCTION__, port->number);
265
266 if (urb->status) {
267 dbg("%s - nonzero read bulk status received: %d", __FUNCTION__, urb->status);
268 return;
269 }
270
271 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, urb->actual_length, data);
272
273 tty = port->tty;
274 if (tty && urb->actual_length) {
Alan Cox33f0f882006-01-09 20:54:13 -0800275 tty_buffer_request_room(tty, urb->actual_length);
276 tty_insert_flip_string(tty, data, urb->actual_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 tty_flip_buffer_push(tty);
278 }
279
280 /* Continue trying to always read */
281 usb_fill_bulk_urb (port->read_urb, serial->dev,
282 usb_rcvbulkpipe (serial->dev,
283 port->bulk_in_endpointAddress),
284 port->read_urb->transfer_buffer,
285 port->read_urb->transfer_buffer_length,
286 ((serial->type->read_bulk_callback) ?
287 serial->type->read_bulk_callback :
288 usb_serial_generic_read_bulk_callback), port);
289 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
290 if (result)
291 dev_err(&port->dev, "%s - failed resubmitting read urb, error %d\n", __FUNCTION__, result);
292}
Luiz Fernando N. Capitulino166ffcc2006-07-11 14:19:25 -0300293EXPORT_SYMBOL_GPL(usb_serial_generic_read_bulk_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
David Howells7d12e782006-10-05 14:55:46 +0100295void usb_serial_generic_write_bulk_callback (struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296{
297 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
298
299 dbg("%s - port %d", __FUNCTION__, port->number);
300
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700301 port->write_urb_busy = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 if (urb->status) {
303 dbg("%s - nonzero write bulk status received: %d", __FUNCTION__, urb->status);
304 return;
305 }
306
Pete Zaitcevcf2c7482006-05-22 21:58:49 -0700307 usb_serial_port_softint(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308}
Greg Kroah-Hartmanbb833982005-11-17 09:48:18 -0800309EXPORT_SYMBOL_GPL(usb_serial_generic_write_bulk_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
311void usb_serial_generic_shutdown (struct usb_serial *serial)
312{
313 int i;
314
315 dbg("%s", __FUNCTION__);
316
317 /* stop reads and writes on all ports */
318 for (i=0; i < serial->num_ports; ++i) {
319 generic_cleanup(serial->port[i]);
320 }
321}
322