blob: 537f12a027c224e2a2253224ae020e515714416d [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_ports = 1,
66 .shutdown = usb_serial_generic_shutdown,
Joris van Rantwijk253ca922007-02-01 20:08:18 +010067 .throttle = usb_serial_generic_throttle,
68 .unthrottle = usb_serial_generic_unthrottle,
Oliver Neukumec225592007-04-27 20:54:57 +020069 .resume = usb_serial_generic_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -070070};
71
Linus Torvalds1da177e2005-04-16 15:20:36 -070072static int generic_probe(struct usb_interface *interface,
73 const struct usb_device_id *id)
74{
75 const struct usb_device_id *id_pattern;
76
77 id_pattern = usb_match_id(interface, generic_device_ids);
78 if (id_pattern != NULL)
79 return usb_serial_probe(interface, id);
80 return -ENODEV;
81}
Linus Torvalds1da177e2005-04-16 15:20:36 -070082#endif
83
84int usb_serial_generic_register (int _debug)
85{
86 int retval = 0;
87
88 debug = _debug;
89#ifdef CONFIG_USB_SERIAL_GENERIC
90 generic_device_ids[0].idVendor = vendor;
91 generic_device_ids[0].idProduct = product;
92 generic_device_ids[0].match_flags = USB_DEVICE_ID_MATCH_VENDOR | USB_DEVICE_ID_MATCH_PRODUCT;
93
94 /* register our generic driver with ourselves */
95 retval = usb_serial_register (&usb_serial_generic_device);
96 if (retval)
97 goto exit;
98 retval = usb_register(&generic_driver);
99 if (retval)
100 usb_serial_deregister(&usb_serial_generic_device);
101exit:
102#endif
103 return retval;
104}
105
106void usb_serial_generic_deregister (void)
107{
108#ifdef CONFIG_USB_SERIAL_GENERIC
109 /* remove our generic driver */
110 usb_deregister(&generic_driver);
111 usb_serial_deregister (&usb_serial_generic_device);
112#endif
113}
114
115int usb_serial_generic_open (struct usb_serial_port *port, struct file *filp)
116{
117 struct usb_serial *serial = port->serial;
118 int result = 0;
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100119 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
Harvey Harrison441b62c2008-03-03 16:08:34 -0800121 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
123 /* force low_latency on so that our tty_push actually forces the data through,
124 otherwise it is scheduled, and with high data rates (like with OHCI) data
125 can get lost. */
126 if (port->tty)
127 port->tty->low_latency = 1;
128
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100129 /* clear the throttle flags */
130 spin_lock_irqsave(&port->lock, flags);
131 port->throttled = 0;
132 port->throttle_req = 0;
133 spin_unlock_irqrestore(&port->lock, flags);
134
135 /* if we have a bulk endpoint, start reading from it */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 if (serial->num_bulk_in) {
137 /* Start reading from the device */
138 usb_fill_bulk_urb (port->read_urb, serial->dev,
139 usb_rcvbulkpipe(serial->dev, port->bulk_in_endpointAddress),
140 port->read_urb->transfer_buffer,
141 port->read_urb->transfer_buffer_length,
142 ((serial->type->read_bulk_callback) ?
143 serial->type->read_bulk_callback :
144 usb_serial_generic_read_bulk_callback),
145 port);
146 result = usb_submit_urb(port->read_urb, GFP_KERNEL);
147 if (result)
Harvey Harrison441b62c2008-03-03 16:08:34 -0800148 dev_err(&port->dev, "%s - failed resubmitting read urb, error %d\n", __func__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 }
150
151 return result;
152}
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700153EXPORT_SYMBOL_GPL(usb_serial_generic_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
155static void generic_cleanup (struct usb_serial_port *port)
156{
157 struct usb_serial *serial = port->serial;
158
Harvey Harrison441b62c2008-03-03 16:08:34 -0800159 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
161 if (serial->dev) {
162 /* shutdown any bulk reads that might be going on */
163 if (serial->num_bulk_out)
164 usb_kill_urb(port->write_urb);
165 if (serial->num_bulk_in)
166 usb_kill_urb(port->read_urb);
167 }
168}
169
Oliver Neukumec225592007-04-27 20:54:57 +0200170int usb_serial_generic_resume(struct usb_serial *serial)
171{
172 struct usb_serial_port *port;
173 int i, c = 0, r;
174
Sarah Sharpf0fbd5b2007-11-13 17:10:09 -0800175#ifdef CONFIG_PM
176 /*
177 * If this is an autoresume, don't submit URBs.
178 * They will be submitted in the open function instead.
179 */
180 if (serial->dev->auto_pm)
181 return 0;
182#endif
Oliver Neukumec225592007-04-27 20:54:57 +0200183 for (i = 0; i < serial->num_ports; i++) {
184 port = serial->port[i];
185 if (port->open_count && port->read_urb) {
186 r = usb_submit_urb(port->read_urb, GFP_NOIO);
187 if (r < 0)
188 c++;
189 }
190 }
191
192 return c ? -EIO : 0;
193}
194
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195void usb_serial_generic_close (struct usb_serial_port *port, struct file * filp)
196{
Harvey Harrison441b62c2008-03-03 16:08:34 -0800197 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 generic_cleanup (port);
199}
200
201int usb_serial_generic_write(struct usb_serial_port *port, const unsigned char *buf, int count)
202{
203 struct usb_serial *serial = port->serial;
204 int result;
205 unsigned char *data;
206
Harvey Harrison441b62c2008-03-03 16:08:34 -0800207 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
209 if (count == 0) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800210 dbg("%s - write request of 0 bytes", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 return (0);
212 }
213
214 /* only do something if we have a bulk out endpoint */
215 if (serial->num_bulk_out) {
Jiri Kosinaacd2a842007-10-20 00:05:19 +0200216 unsigned long flags;
217 spin_lock_irqsave(&port->lock, flags);
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700218 if (port->write_urb_busy) {
Jiri Kosinaacd2a842007-10-20 00:05:19 +0200219 spin_unlock_irqrestore(&port->lock, flags);
Harvey Harrison441b62c2008-03-03 16:08:34 -0800220 dbg("%s - already writing", __func__);
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700221 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 }
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700223 port->write_urb_busy = 1;
Jiri Kosinaacd2a842007-10-20 00:05:19 +0200224 spin_unlock_irqrestore(&port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
226 count = (count > port->bulk_out_size) ? port->bulk_out_size : count;
227
228 memcpy (port->write_urb->transfer_buffer, buf, count);
229 data = port->write_urb->transfer_buffer;
Harvey Harrison441b62c2008-03-03 16:08:34 -0800230 usb_serial_debug_data(debug, &port->dev, __func__, count, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
232 /* set up our urb */
233 usb_fill_bulk_urb (port->write_urb, serial->dev,
234 usb_sndbulkpipe (serial->dev,
235 port->bulk_out_endpointAddress),
236 port->write_urb->transfer_buffer, count,
237 ((serial->type->write_bulk_callback) ?
238 serial->type->write_bulk_callback :
239 usb_serial_generic_write_bulk_callback), port);
240
241 /* send the data out the bulk port */
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700242 port->write_urb_busy = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700244 if (result) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800245 dev_err(&port->dev, "%s - failed submitting write urb, error %d\n", __func__, result);
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700246 /* don't have to grab the lock here, as we will retry if != 0 */
247 port->write_urb_busy = 0;
248 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 result = count;
250
251 return result;
252 }
253
254 /* no bulk out, so return 0 bytes written */
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700255 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256}
257
258int usb_serial_generic_write_room (struct usb_serial_port *port)
259{
260 struct usb_serial *serial = port->serial;
261 int room = 0;
262
Harvey Harrison441b62c2008-03-03 16:08:34 -0800263 dbg("%s - port %d", __func__, port->number);
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700264
Alan Coxa5b6f602008-04-08 17:16:06 +0100265 /* FIXME: Locking */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 if (serial->num_bulk_out) {
Randall Nortman7a3ca7d2005-10-14 17:21:50 -0700267 if (!(port->write_urb_busy))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 room = port->bulk_out_size;
269 }
270
Harvey Harrison441b62c2008-03-03 16:08:34 -0800271 dbg("%s - returns %d", __func__, room);
Alan Coxa5b6f602008-04-08 17:16:06 +0100272 return room;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273}
274
275int usb_serial_generic_chars_in_buffer (struct usb_serial_port *port)
276{
277 struct usb_serial *serial = port->serial;
278 int chars = 0;
279
Harvey Harrison441b62c2008-03-03 16:08:34 -0800280 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
Alan Coxa5b6f602008-04-08 17:16:06 +0100282 /* FIXME: Locking */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 if (serial->num_bulk_out) {
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700284 if (port->write_urb_busy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 chars = port->write_urb->transfer_buffer_length;
286 }
287
Harvey Harrison441b62c2008-03-03 16:08:34 -0800288 dbg("%s - returns %d", __func__, chars);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 return (chars);
290}
291
Oliver Neukum1abdeeb2007-05-07 12:09:33 +0200292
293static void resubmit_read_urb(struct usb_serial_port *port, gfp_t mem_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294{
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100295 struct urb *urb = port->read_urb;
Oliver Neukum1abdeeb2007-05-07 12:09:33 +0200296 struct usb_serial *serial = port->serial;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 int result;
298
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100299 /* Continue reading from device */
Oliver Neukum1abdeeb2007-05-07 12:09:33 +0200300 usb_fill_bulk_urb (urb, serial->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 usb_rcvbulkpipe (serial->dev,
302 port->bulk_in_endpointAddress),
Oliver Neukum1abdeeb2007-05-07 12:09:33 +0200303 urb->transfer_buffer,
304 urb->transfer_buffer_length,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 ((serial->type->read_bulk_callback) ?
306 serial->type->read_bulk_callback :
307 usb_serial_generic_read_bulk_callback), port);
Oliver Neukum1abdeeb2007-05-07 12:09:33 +0200308 result = usb_submit_urb(urb, mem_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 if (result)
Harvey Harrison441b62c2008-03-03 16:08:34 -0800310 dev_err(&port->dev, "%s - failed resubmitting read urb, error %d\n", __func__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311}
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100312
Oliver Neukum1abdeeb2007-05-07 12:09:33 +0200313/* Push data to tty layer and resubmit the bulk read URB */
314static void flush_and_resubmit_read_urb (struct usb_serial_port *port)
315{
316 struct urb *urb = port->read_urb;
317 struct tty_struct *tty = port->tty;
318 int room;
319
320 /* Push data to tty */
321 if (tty && urb->actual_length) {
322 room = tty_buffer_request_room(tty, urb->actual_length);
323 if (room) {
324 tty_insert_flip_string(tty, urb->transfer_buffer, room);
Pete Zaitcevb507cc92008-03-04 23:28:42 -0800325 tty_flip_buffer_push(tty);
Oliver Neukum1abdeeb2007-05-07 12:09:33 +0200326 }
327 }
328
329 resubmit_read_urb(port, GFP_ATOMIC);
330}
331
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100332void usb_serial_generic_read_bulk_callback (struct urb *urb)
333{
Ming Leicdc97792008-02-24 18:41:47 +0800334 struct usb_serial_port *port = urb->context;
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100335 unsigned char *data = urb->transfer_buffer;
Greg Kroah-Hartmanfbd272252007-06-15 15:44:13 -0700336 int status = urb->status;
Borislav Petkovbfaeafc2007-10-28 13:24:16 +0100337 unsigned long flags;
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100338
Harvey Harrison441b62c2008-03-03 16:08:34 -0800339 dbg("%s - port %d", __func__, port->number);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100340
Greg Kroah-Hartmanfbd272252007-06-15 15:44:13 -0700341 if (unlikely(status != 0)) {
342 dbg("%s - nonzero read bulk status received: %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800343 __func__, status);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100344 return;
345 }
346
Harvey Harrison441b62c2008-03-03 16:08:34 -0800347 usb_serial_debug_data(debug, &port->dev, __func__, urb->actual_length, data);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100348
349 /* Throttle the device if requested by tty */
Borislav Petkovbfaeafc2007-10-28 13:24:16 +0100350 spin_lock_irqsave(&port->lock, flags);
Pete Zaitcevb507cc92008-03-04 23:28:42 -0800351 if (!(port->throttled = port->throttle_req)) {
352 spin_unlock_irqrestore(&port->lock, flags);
Oliver Neukum1abdeeb2007-05-07 12:09:33 +0200353 flush_and_resubmit_read_urb(port);
Pete Zaitcevb507cc92008-03-04 23:28:42 -0800354 } else {
355 spin_unlock_irqrestore(&port->lock, flags);
356 }
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100357}
Luiz Fernando N. Capitulino166ffcc2006-07-11 14:19:25 -0300358EXPORT_SYMBOL_GPL(usb_serial_generic_read_bulk_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
David Howells7d12e782006-10-05 14:55:46 +0100360void usb_serial_generic_write_bulk_callback (struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361{
Ming Leicdc97792008-02-24 18:41:47 +0800362 struct usb_serial_port *port = urb->context;
Greg Kroah-Hartmanfbd272252007-06-15 15:44:13 -0700363 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
Harvey Harrison441b62c2008-03-03 16:08:34 -0800365 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700367 port->write_urb_busy = 0;
Greg Kroah-Hartmanfbd272252007-06-15 15:44:13 -0700368 if (status) {
369 dbg("%s - nonzero write bulk status received: %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800370 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 return;
372 }
Pete Zaitcevcf2c7482006-05-22 21:58:49 -0700373 usb_serial_port_softint(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374}
Greg Kroah-Hartmanbb833982005-11-17 09:48:18 -0800375EXPORT_SYMBOL_GPL(usb_serial_generic_write_bulk_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100377void usb_serial_generic_throttle (struct usb_serial_port *port)
378{
379 unsigned long flags;
380
Harvey Harrison441b62c2008-03-03 16:08:34 -0800381 dbg("%s - port %d", __func__, port->number);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100382
383 /* Set the throttle request flag. It will be picked up
384 * by usb_serial_generic_read_bulk_callback(). */
385 spin_lock_irqsave(&port->lock, flags);
386 port->throttle_req = 1;
387 spin_unlock_irqrestore(&port->lock, flags);
388}
389
390void usb_serial_generic_unthrottle (struct usb_serial_port *port)
391{
392 int was_throttled;
393 unsigned long flags;
394
Harvey Harrison441b62c2008-03-03 16:08:34 -0800395 dbg("%s - port %d", __func__, port->number);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100396
397 /* Clear the throttle flags */
398 spin_lock_irqsave(&port->lock, flags);
399 was_throttled = port->throttled;
400 port->throttled = port->throttle_req = 0;
401 spin_unlock_irqrestore(&port->lock, flags);
402
403 if (was_throttled) {
Oliver Neukum1abdeeb2007-05-07 12:09:33 +0200404 /* Resume reading from device */
405 resubmit_read_urb(port, GFP_KERNEL);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100406 }
407}
408
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409void usb_serial_generic_shutdown (struct usb_serial *serial)
410{
411 int i;
412
Harvey Harrison441b62c2008-03-03 16:08:34 -0800413 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
415 /* stop reads and writes on all ports */
416 for (i=0; i < serial->num_ports; ++i) {
417 generic_cleanup(serial->port[i]);
418 }
419}
420