blob: b90ef3f70f4c1cbd43d034532e5b0ba33f810769 [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 +010023
Linus Torvalds1da177e2005-04-16 15:20:36 -070024static int debug;
25
26#ifdef CONFIG_USB_SERIAL_GENERIC
David Brownellb46d60f2007-03-23 12:51:55 -070027
28static int generic_probe(struct usb_interface *interface,
29 const struct usb_device_id *id);
30
Linus Torvalds1da177e2005-04-16 15:20:36 -070031static __u16 vendor = 0x05f9;
32static __u16 product = 0xffff;
33
34module_param(vendor, ushort, 0);
35MODULE_PARM_DESC(vendor, "User specified USB idVendor");
36
37module_param(product, ushort, 0);
38MODULE_PARM_DESC(product, "User specified USB idProduct");
39
40static struct usb_device_id generic_device_ids[2]; /* Initially all zeroes. */
41
Johannes Hölzld9b1b782006-12-17 21:50:24 +010042/* we want to look at all devices, as the vendor/product id can change
43 * depending on the command line argument */
44static struct usb_device_id generic_serial_ids[] = {
45 {.driver_info = 42},
46 {}
47};
48
49static struct usb_driver generic_driver = {
50 .name = "usbserial_generic",
51 .probe = generic_probe,
52 .disconnect = usb_serial_disconnect,
53 .id_table = generic_serial_ids,
54 .no_dynamic_id = 1,
55};
56
Linus Torvalds1da177e2005-04-16 15:20:36 -070057/* All of the device info needed for the Generic Serial Converter */
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -070058struct usb_serial_driver usb_serial_generic_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -070059 .driver = {
60 .owner = THIS_MODULE,
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -070061 .name = "generic",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -070062 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 .id_table = generic_device_ids,
Johannes Hölzld9b1b782006-12-17 21:50:24 +010064 .usb_driver = &generic_driver,
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 .num_interrupt_in = NUM_DONT_CARE,
66 .num_bulk_in = NUM_DONT_CARE,
67 .num_bulk_out = NUM_DONT_CARE,
68 .num_ports = 1,
69 .shutdown = usb_serial_generic_shutdown,
Joris van Rantwijk253ca922007-02-01 20:08:18 +010070 .throttle = usb_serial_generic_throttle,
71 .unthrottle = usb_serial_generic_unthrottle,
Oliver Neukumec225592007-04-27 20:54:57 +020072 .resume = usb_serial_generic_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -070073};
74
Linus Torvalds1da177e2005-04-16 15:20:36 -070075static int generic_probe(struct usb_interface *interface,
76 const struct usb_device_id *id)
77{
78 const struct usb_device_id *id_pattern;
79
80 id_pattern = usb_match_id(interface, generic_device_ids);
81 if (id_pattern != NULL)
82 return usb_serial_probe(interface, id);
83 return -ENODEV;
84}
Linus Torvalds1da177e2005-04-16 15:20:36 -070085#endif
86
87int usb_serial_generic_register (int _debug)
88{
89 int retval = 0;
90
91 debug = _debug;
92#ifdef CONFIG_USB_SERIAL_GENERIC
93 generic_device_ids[0].idVendor = vendor;
94 generic_device_ids[0].idProduct = product;
95 generic_device_ids[0].match_flags = USB_DEVICE_ID_MATCH_VENDOR | USB_DEVICE_ID_MATCH_PRODUCT;
96
97 /* register our generic driver with ourselves */
98 retval = usb_serial_register (&usb_serial_generic_device);
99 if (retval)
100 goto exit;
101 retval = usb_register(&generic_driver);
102 if (retval)
103 usb_serial_deregister(&usb_serial_generic_device);
104exit:
105#endif
106 return retval;
107}
108
109void usb_serial_generic_deregister (void)
110{
111#ifdef CONFIG_USB_SERIAL_GENERIC
112 /* remove our generic driver */
113 usb_deregister(&generic_driver);
114 usb_serial_deregister (&usb_serial_generic_device);
115#endif
116}
117
118int usb_serial_generic_open (struct usb_serial_port *port, struct file *filp)
119{
120 struct usb_serial *serial = port->serial;
121 int result = 0;
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100122 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
124 dbg("%s - port %d", __FUNCTION__, port->number);
125
126 /* force low_latency on so that our tty_push actually forces the data through,
127 otherwise it is scheduled, and with high data rates (like with OHCI) data
128 can get lost. */
129 if (port->tty)
130 port->tty->low_latency = 1;
131
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100132 /* clear the throttle flags */
133 spin_lock_irqsave(&port->lock, flags);
134 port->throttled = 0;
135 port->throttle_req = 0;
136 spin_unlock_irqrestore(&port->lock, flags);
137
138 /* if we have a bulk endpoint, start reading from it */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 if (serial->num_bulk_in) {
140 /* Start reading from the device */
141 usb_fill_bulk_urb (port->read_urb, serial->dev,
142 usb_rcvbulkpipe(serial->dev, port->bulk_in_endpointAddress),
143 port->read_urb->transfer_buffer,
144 port->read_urb->transfer_buffer_length,
145 ((serial->type->read_bulk_callback) ?
146 serial->type->read_bulk_callback :
147 usb_serial_generic_read_bulk_callback),
148 port);
149 result = usb_submit_urb(port->read_urb, GFP_KERNEL);
150 if (result)
151 dev_err(&port->dev, "%s - failed resubmitting read urb, error %d\n", __FUNCTION__, result);
152 }
153
154 return result;
155}
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700156EXPORT_SYMBOL_GPL(usb_serial_generic_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
158static void generic_cleanup (struct usb_serial_port *port)
159{
160 struct usb_serial *serial = port->serial;
161
162 dbg("%s - port %d", __FUNCTION__, port->number);
163
164 if (serial->dev) {
165 /* shutdown any bulk reads that might be going on */
166 if (serial->num_bulk_out)
167 usb_kill_urb(port->write_urb);
168 if (serial->num_bulk_in)
169 usb_kill_urb(port->read_urb);
170 }
171}
172
Oliver Neukumec225592007-04-27 20:54:57 +0200173int usb_serial_generic_resume(struct usb_serial *serial)
174{
175 struct usb_serial_port *port;
176 int i, c = 0, r;
177
178 for (i = 0; i < serial->num_ports; i++) {
179 port = serial->port[i];
180 if (port->open_count && port->read_urb) {
181 r = usb_submit_urb(port->read_urb, GFP_NOIO);
182 if (r < 0)
183 c++;
184 }
185 }
186
187 return c ? -EIO : 0;
188}
189
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190void usb_serial_generic_close (struct usb_serial_port *port, struct file * filp)
191{
192 dbg("%s - port %d", __FUNCTION__, port->number);
193 generic_cleanup (port);
194}
195
196int usb_serial_generic_write(struct usb_serial_port *port, const unsigned char *buf, int count)
197{
198 struct usb_serial *serial = port->serial;
199 int result;
200 unsigned char *data;
201
202 dbg("%s - port %d", __FUNCTION__, port->number);
203
204 if (count == 0) {
205 dbg("%s - write request of 0 bytes", __FUNCTION__);
206 return (0);
207 }
208
209 /* only do something if we have a bulk out endpoint */
210 if (serial->num_bulk_out) {
Peter Zijlstrae81ee632006-09-25 12:51:41 +0200211 spin_lock_bh(&port->lock);
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700212 if (port->write_urb_busy) {
Peter Zijlstrae81ee632006-09-25 12:51:41 +0200213 spin_unlock_bh(&port->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 dbg("%s - already writing", __FUNCTION__);
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700215 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 }
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700217 port->write_urb_busy = 1;
Peter Zijlstrae81ee632006-09-25 12:51:41 +0200218 spin_unlock_bh(&port->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
220 count = (count > port->bulk_out_size) ? port->bulk_out_size : count;
221
222 memcpy (port->write_urb->transfer_buffer, buf, count);
223 data = port->write_urb->transfer_buffer;
224 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, count, data);
225
226 /* set up our urb */
227 usb_fill_bulk_urb (port->write_urb, serial->dev,
228 usb_sndbulkpipe (serial->dev,
229 port->bulk_out_endpointAddress),
230 port->write_urb->transfer_buffer, count,
231 ((serial->type->write_bulk_callback) ?
232 serial->type->write_bulk_callback :
233 usb_serial_generic_write_bulk_callback), port);
234
235 /* send the data out the bulk port */
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700236 port->write_urb_busy = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700238 if (result) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 dev_err(&port->dev, "%s - failed submitting write urb, error %d\n", __FUNCTION__, result);
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700240 /* don't have to grab the lock here, as we will retry if != 0 */
241 port->write_urb_busy = 0;
242 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 result = count;
244
245 return result;
246 }
247
248 /* no bulk out, so return 0 bytes written */
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700249 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250}
251
252int usb_serial_generic_write_room (struct usb_serial_port *port)
253{
254 struct usb_serial *serial = port->serial;
255 int room = 0;
256
257 dbg("%s - port %d", __FUNCTION__, port->number);
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700258
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 if (serial->num_bulk_out) {
Randall Nortman7a3ca7d2005-10-14 17:21:50 -0700260 if (!(port->write_urb_busy))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 room = port->bulk_out_size;
262 }
263
264 dbg("%s - returns %d", __FUNCTION__, room);
265 return (room);
266}
267
268int usb_serial_generic_chars_in_buffer (struct usb_serial_port *port)
269{
270 struct usb_serial *serial = port->serial;
271 int chars = 0;
272
273 dbg("%s - port %d", __FUNCTION__, port->number);
274
275 if (serial->num_bulk_out) {
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700276 if (port->write_urb_busy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 chars = port->write_urb->transfer_buffer_length;
278 }
279
280 dbg("%s - returns %d", __FUNCTION__, chars);
281 return (chars);
282}
283
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100284/* Push data to tty layer and resubmit the bulk read URB */
285static void flush_and_resubmit_read_urb (struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 struct usb_serial *serial = port->serial;
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100288 struct urb *urb = port->read_urb;
289 struct tty_struct *tty = port->tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 int result;
291
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100292 /* Push data to tty */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 if (tty && urb->actual_length) {
Alan Cox33f0f882006-01-09 20:54:13 -0800294 tty_buffer_request_room(tty, urb->actual_length);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100295 tty_insert_flip_string(tty, urb->transfer_buffer, urb->actual_length);
296 tty_flip_buffer_push(tty); /* is this allowed from an URB callback ? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 }
298
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100299 /* Continue reading from device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 usb_fill_bulk_urb (port->read_urb, serial->dev,
301 usb_rcvbulkpipe (serial->dev,
302 port->bulk_in_endpointAddress),
303 port->read_urb->transfer_buffer,
304 port->read_urb->transfer_buffer_length,
305 ((serial->type->read_bulk_callback) ?
306 serial->type->read_bulk_callback :
307 usb_serial_generic_read_bulk_callback), port);
308 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
309 if (result)
310 dev_err(&port->dev, "%s - failed resubmitting read urb, error %d\n", __FUNCTION__, result);
311}
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100312
313void usb_serial_generic_read_bulk_callback (struct urb *urb)
314{
315 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
316 unsigned char *data = urb->transfer_buffer;
317 int is_throttled;
318 unsigned long flags;
319
320 dbg("%s - port %d", __FUNCTION__, port->number);
321
322 if (urb->status) {
323 dbg("%s - nonzero read bulk status received: %d", __FUNCTION__, urb->status);
324 return;
325 }
326
327 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, urb->actual_length, data);
328
329 /* Throttle the device if requested by tty */
330 if (urb->actual_length) {
331 spin_lock_irqsave(&port->lock, flags);
332 is_throttled = port->throttled = port->throttle_req;
333 spin_unlock_irqrestore(&port->lock, flags);
334 if (is_throttled) {
335 /* Let the received data linger in the read URB;
336 * usb_serial_generic_unthrottle() will pick it
337 * up later. */
338 dbg("%s - throttling device", __FUNCTION__);
339 return;
340 }
341 }
342
343 /* Handle data and continue reading from device */
344 flush_and_resubmit_read_urb(port);
345}
Luiz Fernando N. Capitulino166ffcc2006-07-11 14:19:25 -0300346EXPORT_SYMBOL_GPL(usb_serial_generic_read_bulk_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
David Howells7d12e782006-10-05 14:55:46 +0100348void usb_serial_generic_write_bulk_callback (struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349{
350 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
351
352 dbg("%s - port %d", __FUNCTION__, port->number);
353
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700354 port->write_urb_busy = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 if (urb->status) {
356 dbg("%s - nonzero write bulk status received: %d", __FUNCTION__, urb->status);
357 return;
358 }
359
Pete Zaitcevcf2c7482006-05-22 21:58:49 -0700360 usb_serial_port_softint(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361}
Greg Kroah-Hartmanbb833982005-11-17 09:48:18 -0800362EXPORT_SYMBOL_GPL(usb_serial_generic_write_bulk_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100364void usb_serial_generic_throttle (struct usb_serial_port *port)
365{
366 unsigned long flags;
367
368 dbg("%s - port %d", __FUNCTION__, port->number);
369
370 /* Set the throttle request flag. It will be picked up
371 * by usb_serial_generic_read_bulk_callback(). */
372 spin_lock_irqsave(&port->lock, flags);
373 port->throttle_req = 1;
374 spin_unlock_irqrestore(&port->lock, flags);
375}
376
377void usb_serial_generic_unthrottle (struct usb_serial_port *port)
378{
379 int was_throttled;
380 unsigned long flags;
381
382 dbg("%s - port %d", __FUNCTION__, port->number);
383
384 /* Clear the throttle flags */
385 spin_lock_irqsave(&port->lock, flags);
386 was_throttled = port->throttled;
387 port->throttled = port->throttle_req = 0;
388 spin_unlock_irqrestore(&port->lock, flags);
389
390 if (was_throttled) {
391 /* Handle pending data and resume reading from device */
392 flush_and_resubmit_read_urb(port);
393 }
394}
395
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396void usb_serial_generic_shutdown (struct usb_serial *serial)
397{
398 int i;
399
400 dbg("%s", __FUNCTION__);
401
402 /* stop reads and writes on all ports */
403 for (i=0; i < serial->num_ports; ++i) {
404 generic_cleanup(serial->port[i]);
405 }
406}
407