blob: 53baeec8f265a041bc45ae3a3010cd6c30d71c7d [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,
Joris van Rantwijk253ca922007-02-01 20:08:18 +010069 .throttle = usb_serial_generic_throttle,
70 .unthrottle = usb_serial_generic_unthrottle,
Linus Torvalds1da177e2005-04-16 15:20:36 -070071};
72
Linus Torvalds1da177e2005-04-16 15:20:36 -070073static int generic_probe(struct usb_interface *interface,
74 const struct usb_device_id *id)
75{
76 const struct usb_device_id *id_pattern;
77
78 id_pattern = usb_match_id(interface, generic_device_ids);
79 if (id_pattern != NULL)
80 return usb_serial_probe(interface, id);
81 return -ENODEV;
82}
Linus Torvalds1da177e2005-04-16 15:20:36 -070083#endif
84
85int usb_serial_generic_register (int _debug)
86{
87 int retval = 0;
88
89 debug = _debug;
90#ifdef CONFIG_USB_SERIAL_GENERIC
91 generic_device_ids[0].idVendor = vendor;
92 generic_device_ids[0].idProduct = product;
93 generic_device_ids[0].match_flags = USB_DEVICE_ID_MATCH_VENDOR | USB_DEVICE_ID_MATCH_PRODUCT;
94
95 /* register our generic driver with ourselves */
96 retval = usb_serial_register (&usb_serial_generic_device);
97 if (retval)
98 goto exit;
99 retval = usb_register(&generic_driver);
100 if (retval)
101 usb_serial_deregister(&usb_serial_generic_device);
102exit:
103#endif
104 return retval;
105}
106
107void usb_serial_generic_deregister (void)
108{
109#ifdef CONFIG_USB_SERIAL_GENERIC
110 /* remove our generic driver */
111 usb_deregister(&generic_driver);
112 usb_serial_deregister (&usb_serial_generic_device);
113#endif
114}
115
116int usb_serial_generic_open (struct usb_serial_port *port, struct file *filp)
117{
118 struct usb_serial *serial = port->serial;
119 int result = 0;
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100120 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
122 dbg("%s - port %d", __FUNCTION__, port->number);
123
124 /* force low_latency on so that our tty_push actually forces the data through,
125 otherwise it is scheduled, and with high data rates (like with OHCI) data
126 can get lost. */
127 if (port->tty)
128 port->tty->low_latency = 1;
129
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100130 /* clear the throttle flags */
131 spin_lock_irqsave(&port->lock, flags);
132 port->throttled = 0;
133 port->throttle_req = 0;
134 spin_unlock_irqrestore(&port->lock, flags);
135
136 /* if we have a bulk endpoint, start reading from it */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 if (serial->num_bulk_in) {
138 /* Start reading from the device */
139 usb_fill_bulk_urb (port->read_urb, serial->dev,
140 usb_rcvbulkpipe(serial->dev, port->bulk_in_endpointAddress),
141 port->read_urb->transfer_buffer,
142 port->read_urb->transfer_buffer_length,
143 ((serial->type->read_bulk_callback) ?
144 serial->type->read_bulk_callback :
145 usb_serial_generic_read_bulk_callback),
146 port);
147 result = usb_submit_urb(port->read_urb, GFP_KERNEL);
148 if (result)
149 dev_err(&port->dev, "%s - failed resubmitting read urb, error %d\n", __FUNCTION__, result);
150 }
151
152 return result;
153}
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700154EXPORT_SYMBOL_GPL(usb_serial_generic_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
156static void generic_cleanup (struct usb_serial_port *port)
157{
158 struct usb_serial *serial = port->serial;
159
160 dbg("%s - port %d", __FUNCTION__, port->number);
161
162 if (serial->dev) {
163 /* shutdown any bulk reads that might be going on */
164 if (serial->num_bulk_out)
165 usb_kill_urb(port->write_urb);
166 if (serial->num_bulk_in)
167 usb_kill_urb(port->read_urb);
168 }
169}
170
171void usb_serial_generic_close (struct usb_serial_port *port, struct file * filp)
172{
173 dbg("%s - port %d", __FUNCTION__, port->number);
174 generic_cleanup (port);
175}
176
177int usb_serial_generic_write(struct usb_serial_port *port, const unsigned char *buf, int count)
178{
179 struct usb_serial *serial = port->serial;
180 int result;
181 unsigned char *data;
182
183 dbg("%s - port %d", __FUNCTION__, port->number);
184
185 if (count == 0) {
186 dbg("%s - write request of 0 bytes", __FUNCTION__);
187 return (0);
188 }
189
190 /* only do something if we have a bulk out endpoint */
191 if (serial->num_bulk_out) {
Peter Zijlstrae81ee632006-09-25 12:51:41 +0200192 spin_lock_bh(&port->lock);
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700193 if (port->write_urb_busy) {
Peter Zijlstrae81ee632006-09-25 12:51:41 +0200194 spin_unlock_bh(&port->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 dbg("%s - already writing", __FUNCTION__);
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700196 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 }
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700198 port->write_urb_busy = 1;
Peter Zijlstrae81ee632006-09-25 12:51:41 +0200199 spin_unlock_bh(&port->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
201 count = (count > port->bulk_out_size) ? port->bulk_out_size : count;
202
203 memcpy (port->write_urb->transfer_buffer, buf, count);
204 data = port->write_urb->transfer_buffer;
205 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, count, data);
206
207 /* set up our urb */
208 usb_fill_bulk_urb (port->write_urb, serial->dev,
209 usb_sndbulkpipe (serial->dev,
210 port->bulk_out_endpointAddress),
211 port->write_urb->transfer_buffer, count,
212 ((serial->type->write_bulk_callback) ?
213 serial->type->write_bulk_callback :
214 usb_serial_generic_write_bulk_callback), port);
215
216 /* send the data out the bulk port */
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700217 port->write_urb_busy = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700219 if (result) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 dev_err(&port->dev, "%s - failed submitting write urb, error %d\n", __FUNCTION__, result);
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700221 /* don't have to grab the lock here, as we will retry if != 0 */
222 port->write_urb_busy = 0;
223 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 result = count;
225
226 return result;
227 }
228
229 /* no bulk out, so return 0 bytes written */
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700230 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231}
232
233int usb_serial_generic_write_room (struct usb_serial_port *port)
234{
235 struct usb_serial *serial = port->serial;
236 int room = 0;
237
238 dbg("%s - port %d", __FUNCTION__, port->number);
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700239
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 if (serial->num_bulk_out) {
Randall Nortman7a3ca7d2005-10-14 17:21:50 -0700241 if (!(port->write_urb_busy))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 room = port->bulk_out_size;
243 }
244
245 dbg("%s - returns %d", __FUNCTION__, room);
246 return (room);
247}
248
249int usb_serial_generic_chars_in_buffer (struct usb_serial_port *port)
250{
251 struct usb_serial *serial = port->serial;
252 int chars = 0;
253
254 dbg("%s - port %d", __FUNCTION__, port->number);
255
256 if (serial->num_bulk_out) {
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700257 if (port->write_urb_busy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 chars = port->write_urb->transfer_buffer_length;
259 }
260
261 dbg("%s - returns %d", __FUNCTION__, chars);
262 return (chars);
263}
264
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100265/* Push data to tty layer and resubmit the bulk read URB */
266static void flush_and_resubmit_read_urb (struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 struct usb_serial *serial = port->serial;
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100269 struct urb *urb = port->read_urb;
270 struct tty_struct *tty = port->tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 int result;
272
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100273 /* Push data to tty */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 if (tty && urb->actual_length) {
Alan Cox33f0f882006-01-09 20:54:13 -0800275 tty_buffer_request_room(tty, urb->actual_length);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100276 tty_insert_flip_string(tty, urb->transfer_buffer, urb->actual_length);
277 tty_flip_buffer_push(tty); /* is this allowed from an URB callback ? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 }
279
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100280 /* Continue reading from device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 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}
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100293
294void usb_serial_generic_read_bulk_callback (struct urb *urb)
295{
296 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
297 unsigned char *data = urb->transfer_buffer;
298 int is_throttled;
299 unsigned long flags;
300
301 dbg("%s - port %d", __FUNCTION__, port->number);
302
303 if (urb->status) {
304 dbg("%s - nonzero read bulk status received: %d", __FUNCTION__, urb->status);
305 return;
306 }
307
308 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, urb->actual_length, data);
309
310 /* Throttle the device if requested by tty */
311 if (urb->actual_length) {
312 spin_lock_irqsave(&port->lock, flags);
313 is_throttled = port->throttled = port->throttle_req;
314 spin_unlock_irqrestore(&port->lock, flags);
315 if (is_throttled) {
316 /* Let the received data linger in the read URB;
317 * usb_serial_generic_unthrottle() will pick it
318 * up later. */
319 dbg("%s - throttling device", __FUNCTION__);
320 return;
321 }
322 }
323
324 /* Handle data and continue reading from device */
325 flush_and_resubmit_read_urb(port);
326}
Luiz Fernando N. Capitulino166ffcc2006-07-11 14:19:25 -0300327EXPORT_SYMBOL_GPL(usb_serial_generic_read_bulk_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
David Howells7d12e782006-10-05 14:55:46 +0100329void usb_serial_generic_write_bulk_callback (struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330{
331 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
332
333 dbg("%s - port %d", __FUNCTION__, port->number);
334
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700335 port->write_urb_busy = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 if (urb->status) {
337 dbg("%s - nonzero write bulk status received: %d", __FUNCTION__, urb->status);
338 return;
339 }
340
Pete Zaitcevcf2c7482006-05-22 21:58:49 -0700341 usb_serial_port_softint(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342}
Greg Kroah-Hartmanbb833982005-11-17 09:48:18 -0800343EXPORT_SYMBOL_GPL(usb_serial_generic_write_bulk_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100345void usb_serial_generic_throttle (struct usb_serial_port *port)
346{
347 unsigned long flags;
348
349 dbg("%s - port %d", __FUNCTION__, port->number);
350
351 /* Set the throttle request flag. It will be picked up
352 * by usb_serial_generic_read_bulk_callback(). */
353 spin_lock_irqsave(&port->lock, flags);
354 port->throttle_req = 1;
355 spin_unlock_irqrestore(&port->lock, flags);
356}
357
358void usb_serial_generic_unthrottle (struct usb_serial_port *port)
359{
360 int was_throttled;
361 unsigned long flags;
362
363 dbg("%s - port %d", __FUNCTION__, port->number);
364
365 /* Clear the throttle flags */
366 spin_lock_irqsave(&port->lock, flags);
367 was_throttled = port->throttled;
368 port->throttled = port->throttle_req = 0;
369 spin_unlock_irqrestore(&port->lock, flags);
370
371 if (was_throttled) {
372 /* Handle pending data and resume reading from device */
373 flush_and_resubmit_read_urb(port);
374 }
375}
376
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377void usb_serial_generic_shutdown (struct usb_serial *serial)
378{
379 int i;
380
381 dbg("%s", __FUNCTION__);
382
383 /* stop reads and writes on all ports */
384 for (i=0; i < serial->num_ports; ++i) {
385 generic_cleanup(serial->port[i]);
386 }
387}
388