blob: 4f8282ad77208b66e4c406f826100cb300b5247a [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,
Linus Torvalds1da177e2005-04-16 15:20:36 -070072};
73
Linus Torvalds1da177e2005-04-16 15:20:36 -070074static int generic_probe(struct usb_interface *interface,
75 const struct usb_device_id *id)
76{
77 const struct usb_device_id *id_pattern;
78
79 id_pattern = usb_match_id(interface, generic_device_ids);
80 if (id_pattern != NULL)
81 return usb_serial_probe(interface, id);
82 return -ENODEV;
83}
Linus Torvalds1da177e2005-04-16 15:20:36 -070084#endif
85
86int usb_serial_generic_register (int _debug)
87{
88 int retval = 0;
89
90 debug = _debug;
91#ifdef CONFIG_USB_SERIAL_GENERIC
92 generic_device_ids[0].idVendor = vendor;
93 generic_device_ids[0].idProduct = product;
94 generic_device_ids[0].match_flags = USB_DEVICE_ID_MATCH_VENDOR | USB_DEVICE_ID_MATCH_PRODUCT;
95
96 /* register our generic driver with ourselves */
97 retval = usb_serial_register (&usb_serial_generic_device);
98 if (retval)
99 goto exit;
100 retval = usb_register(&generic_driver);
101 if (retval)
102 usb_serial_deregister(&usb_serial_generic_device);
103exit:
104#endif
105 return retval;
106}
107
108void usb_serial_generic_deregister (void)
109{
110#ifdef CONFIG_USB_SERIAL_GENERIC
111 /* remove our generic driver */
112 usb_deregister(&generic_driver);
113 usb_serial_deregister (&usb_serial_generic_device);
114#endif
115}
116
117int usb_serial_generic_open (struct usb_serial_port *port, struct file *filp)
118{
119 struct usb_serial *serial = port->serial;
120 int result = 0;
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100121 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
123 dbg("%s - port %d", __FUNCTION__, port->number);
124
125 /* force low_latency on so that our tty_push actually forces the data through,
126 otherwise it is scheduled, and with high data rates (like with OHCI) data
127 can get lost. */
128 if (port->tty)
129 port->tty->low_latency = 1;
130
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100131 /* clear the throttle flags */
132 spin_lock_irqsave(&port->lock, flags);
133 port->throttled = 0;
134 port->throttle_req = 0;
135 spin_unlock_irqrestore(&port->lock, flags);
136
137 /* if we have a bulk endpoint, start reading from it */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 if (serial->num_bulk_in) {
139 /* Start reading from the device */
140 usb_fill_bulk_urb (port->read_urb, serial->dev,
141 usb_rcvbulkpipe(serial->dev, port->bulk_in_endpointAddress),
142 port->read_urb->transfer_buffer,
143 port->read_urb->transfer_buffer_length,
144 ((serial->type->read_bulk_callback) ?
145 serial->type->read_bulk_callback :
146 usb_serial_generic_read_bulk_callback),
147 port);
148 result = usb_submit_urb(port->read_urb, GFP_KERNEL);
149 if (result)
150 dev_err(&port->dev, "%s - failed resubmitting read urb, error %d\n", __FUNCTION__, result);
151 }
152
153 return result;
154}
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700155EXPORT_SYMBOL_GPL(usb_serial_generic_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
157static void generic_cleanup (struct usb_serial_port *port)
158{
159 struct usb_serial *serial = port->serial;
160
161 dbg("%s - port %d", __FUNCTION__, port->number);
162
163 if (serial->dev) {
164 /* shutdown any bulk reads that might be going on */
165 if (serial->num_bulk_out)
166 usb_kill_urb(port->write_urb);
167 if (serial->num_bulk_in)
168 usb_kill_urb(port->read_urb);
169 }
170}
171
172void usb_serial_generic_close (struct usb_serial_port *port, struct file * filp)
173{
174 dbg("%s - port %d", __FUNCTION__, port->number);
175 generic_cleanup (port);
176}
177
178int usb_serial_generic_write(struct usb_serial_port *port, const unsigned char *buf, int count)
179{
180 struct usb_serial *serial = port->serial;
181 int result;
182 unsigned char *data;
183
184 dbg("%s - port %d", __FUNCTION__, port->number);
185
186 if (count == 0) {
187 dbg("%s - write request of 0 bytes", __FUNCTION__);
188 return (0);
189 }
190
191 /* only do something if we have a bulk out endpoint */
192 if (serial->num_bulk_out) {
Peter Zijlstrae81ee632006-09-25 12:51:41 +0200193 spin_lock_bh(&port->lock);
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700194 if (port->write_urb_busy) {
Peter Zijlstrae81ee632006-09-25 12:51:41 +0200195 spin_unlock_bh(&port->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 dbg("%s - already writing", __FUNCTION__);
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700197 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 }
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700199 port->write_urb_busy = 1;
Peter Zijlstrae81ee632006-09-25 12:51:41 +0200200 spin_unlock_bh(&port->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
202 count = (count > port->bulk_out_size) ? port->bulk_out_size : count;
203
204 memcpy (port->write_urb->transfer_buffer, buf, count);
205 data = port->write_urb->transfer_buffer;
206 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, count, data);
207
208 /* set up our urb */
209 usb_fill_bulk_urb (port->write_urb, serial->dev,
210 usb_sndbulkpipe (serial->dev,
211 port->bulk_out_endpointAddress),
212 port->write_urb->transfer_buffer, count,
213 ((serial->type->write_bulk_callback) ?
214 serial->type->write_bulk_callback :
215 usb_serial_generic_write_bulk_callback), port);
216
217 /* send the data out the bulk port */
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700218 port->write_urb_busy = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700220 if (result) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 dev_err(&port->dev, "%s - failed submitting write urb, error %d\n", __FUNCTION__, result);
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700222 /* don't have to grab the lock here, as we will retry if != 0 */
223 port->write_urb_busy = 0;
224 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 result = count;
226
227 return result;
228 }
229
230 /* no bulk out, so return 0 bytes written */
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700231 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232}
233
234int usb_serial_generic_write_room (struct usb_serial_port *port)
235{
236 struct usb_serial *serial = port->serial;
237 int room = 0;
238
239 dbg("%s - port %d", __FUNCTION__, port->number);
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700240
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 if (serial->num_bulk_out) {
Randall Nortman7a3ca7d2005-10-14 17:21:50 -0700242 if (!(port->write_urb_busy))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 room = port->bulk_out_size;
244 }
245
246 dbg("%s - returns %d", __FUNCTION__, room);
247 return (room);
248}
249
250int usb_serial_generic_chars_in_buffer (struct usb_serial_port *port)
251{
252 struct usb_serial *serial = port->serial;
253 int chars = 0;
254
255 dbg("%s - port %d", __FUNCTION__, port->number);
256
257 if (serial->num_bulk_out) {
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700258 if (port->write_urb_busy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 chars = port->write_urb->transfer_buffer_length;
260 }
261
262 dbg("%s - returns %d", __FUNCTION__, chars);
263 return (chars);
264}
265
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100266/* Push data to tty layer and resubmit the bulk read URB */
267static void flush_and_resubmit_read_urb (struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 struct usb_serial *serial = port->serial;
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100270 struct urb *urb = port->read_urb;
271 struct tty_struct *tty = port->tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 int result;
273
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100274 /* Push data to tty */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 if (tty && urb->actual_length) {
Alan Cox33f0f882006-01-09 20:54:13 -0800276 tty_buffer_request_room(tty, urb->actual_length);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100277 tty_insert_flip_string(tty, urb->transfer_buffer, urb->actual_length);
278 tty_flip_buffer_push(tty); /* is this allowed from an URB callback ? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 }
280
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100281 /* Continue reading from device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 usb_fill_bulk_urb (port->read_urb, serial->dev,
283 usb_rcvbulkpipe (serial->dev,
284 port->bulk_in_endpointAddress),
285 port->read_urb->transfer_buffer,
286 port->read_urb->transfer_buffer_length,
287 ((serial->type->read_bulk_callback) ?
288 serial->type->read_bulk_callback :
289 usb_serial_generic_read_bulk_callback), port);
290 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
291 if (result)
292 dev_err(&port->dev, "%s - failed resubmitting read urb, error %d\n", __FUNCTION__, result);
293}
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100294
295void usb_serial_generic_read_bulk_callback (struct urb *urb)
296{
297 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
298 unsigned char *data = urb->transfer_buffer;
299 int is_throttled;
300 unsigned long flags;
301
302 dbg("%s - port %d", __FUNCTION__, port->number);
303
304 if (urb->status) {
305 dbg("%s - nonzero read bulk status received: %d", __FUNCTION__, urb->status);
306 return;
307 }
308
309 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, urb->actual_length, data);
310
311 /* Throttle the device if requested by tty */
312 if (urb->actual_length) {
313 spin_lock_irqsave(&port->lock, flags);
314 is_throttled = port->throttled = port->throttle_req;
315 spin_unlock_irqrestore(&port->lock, flags);
316 if (is_throttled) {
317 /* Let the received data linger in the read URB;
318 * usb_serial_generic_unthrottle() will pick it
319 * up later. */
320 dbg("%s - throttling device", __FUNCTION__);
321 return;
322 }
323 }
324
325 /* Handle data and continue reading from device */
326 flush_and_resubmit_read_urb(port);
327}
Luiz Fernando N. Capitulino166ffcc2006-07-11 14:19:25 -0300328EXPORT_SYMBOL_GPL(usb_serial_generic_read_bulk_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
David Howells7d12e782006-10-05 14:55:46 +0100330void usb_serial_generic_write_bulk_callback (struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331{
332 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
333
334 dbg("%s - port %d", __FUNCTION__, port->number);
335
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700336 port->write_urb_busy = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 if (urb->status) {
338 dbg("%s - nonzero write bulk status received: %d", __FUNCTION__, urb->status);
339 return;
340 }
341
Pete Zaitcevcf2c7482006-05-22 21:58:49 -0700342 usb_serial_port_softint(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343}
Greg Kroah-Hartmanbb833982005-11-17 09:48:18 -0800344EXPORT_SYMBOL_GPL(usb_serial_generic_write_bulk_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100346void usb_serial_generic_throttle (struct usb_serial_port *port)
347{
348 unsigned long flags;
349
350 dbg("%s - port %d", __FUNCTION__, port->number);
351
352 /* Set the throttle request flag. It will be picked up
353 * by usb_serial_generic_read_bulk_callback(). */
354 spin_lock_irqsave(&port->lock, flags);
355 port->throttle_req = 1;
356 spin_unlock_irqrestore(&port->lock, flags);
357}
358
359void usb_serial_generic_unthrottle (struct usb_serial_port *port)
360{
361 int was_throttled;
362 unsigned long flags;
363
364 dbg("%s - port %d", __FUNCTION__, port->number);
365
366 /* Clear the throttle flags */
367 spin_lock_irqsave(&port->lock, flags);
368 was_throttled = port->throttled;
369 port->throttled = port->throttle_req = 0;
370 spin_unlock_irqrestore(&port->lock, flags);
371
372 if (was_throttled) {
373 /* Handle pending data and resume reading from device */
374 flush_and_resubmit_read_urb(port);
375 }
376}
377
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378void usb_serial_generic_shutdown (struct usb_serial *serial)
379{
380 int i;
381
382 dbg("%s", __FUNCTION__);
383
384 /* stop reads and writes on all ports */
385 for (i=0; i < serial->num_ports; ++i) {
386 generic_cleanup(serial->port[i]);
387 }
388}
389