blob: 36042937e77f80f3c536e3f1aea64e99d161d949 [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
23static int debug;
24
25#ifdef CONFIG_USB_SERIAL_GENERIC
26static __u16 vendor = 0x05f9;
27static __u16 product = 0xffff;
28
29module_param(vendor, ushort, 0);
30MODULE_PARM_DESC(vendor, "User specified USB idVendor");
31
32module_param(product, ushort, 0);
33MODULE_PARM_DESC(product, "User specified USB idProduct");
34
35static struct usb_device_id generic_device_ids[2]; /* Initially all zeroes. */
36
37/* All of the device info needed for the Generic Serial Converter */
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -070038struct usb_serial_driver usb_serial_generic_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -070039 .driver = {
40 .owner = THIS_MODULE,
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -070041 .name = "generic",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -070042 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 .id_table = generic_device_ids,
44 .num_interrupt_in = NUM_DONT_CARE,
45 .num_bulk_in = NUM_DONT_CARE,
46 .num_bulk_out = NUM_DONT_CARE,
47 .num_ports = 1,
48 .shutdown = usb_serial_generic_shutdown,
49};
50
51/* we want to look at all devices, as the vendor/product id can change
52 * depending on the command line argument */
53static struct usb_device_id generic_serial_ids[] = {
54 {.driver_info = 42},
55 {}
56};
57
58static int generic_probe(struct usb_interface *interface,
59 const struct usb_device_id *id)
60{
61 const struct usb_device_id *id_pattern;
62
63 id_pattern = usb_match_id(interface, generic_device_ids);
64 if (id_pattern != NULL)
65 return usb_serial_probe(interface, id);
66 return -ENODEV;
67}
68
69static struct usb_driver generic_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 .name = "usbserial_generic",
71 .probe = generic_probe,
72 .disconnect = usb_serial_disconnect,
73 .id_table = generic_serial_ids,
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -080074 .no_dynamic_id = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -070075};
76#endif
77
78int usb_serial_generic_register (int _debug)
79{
80 int retval = 0;
81
82 debug = _debug;
83#ifdef CONFIG_USB_SERIAL_GENERIC
84 generic_device_ids[0].idVendor = vendor;
85 generic_device_ids[0].idProduct = product;
86 generic_device_ids[0].match_flags = USB_DEVICE_ID_MATCH_VENDOR | USB_DEVICE_ID_MATCH_PRODUCT;
87
88 /* register our generic driver with ourselves */
89 retval = usb_serial_register (&usb_serial_generic_device);
90 if (retval)
91 goto exit;
92 retval = usb_register(&generic_driver);
93 if (retval)
94 usb_serial_deregister(&usb_serial_generic_device);
95exit:
96#endif
97 return retval;
98}
99
100void usb_serial_generic_deregister (void)
101{
102#ifdef CONFIG_USB_SERIAL_GENERIC
103 /* remove our generic driver */
104 usb_deregister(&generic_driver);
105 usb_serial_deregister (&usb_serial_generic_device);
106#endif
107}
108
109int usb_serial_generic_open (struct usb_serial_port *port, struct file *filp)
110{
111 struct usb_serial *serial = port->serial;
112 int result = 0;
113
114 dbg("%s - port %d", __FUNCTION__, port->number);
115
116 /* force low_latency on so that our tty_push actually forces the data through,
117 otherwise it is scheduled, and with high data rates (like with OHCI) data
118 can get lost. */
119 if (port->tty)
120 port->tty->low_latency = 1;
121
122 /* if we have a bulk interrupt, start reading from it */
123 if (serial->num_bulk_in) {
124 /* Start reading from the device */
125 usb_fill_bulk_urb (port->read_urb, serial->dev,
126 usb_rcvbulkpipe(serial->dev, port->bulk_in_endpointAddress),
127 port->read_urb->transfer_buffer,
128 port->read_urb->transfer_buffer_length,
129 ((serial->type->read_bulk_callback) ?
130 serial->type->read_bulk_callback :
131 usb_serial_generic_read_bulk_callback),
132 port);
133 result = usb_submit_urb(port->read_urb, GFP_KERNEL);
134 if (result)
135 dev_err(&port->dev, "%s - failed resubmitting read urb, error %d\n", __FUNCTION__, result);
136 }
137
138 return result;
139}
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700140EXPORT_SYMBOL_GPL(usb_serial_generic_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
142static void generic_cleanup (struct usb_serial_port *port)
143{
144 struct usb_serial *serial = port->serial;
145
146 dbg("%s - port %d", __FUNCTION__, port->number);
147
148 if (serial->dev) {
149 /* shutdown any bulk reads that might be going on */
150 if (serial->num_bulk_out)
151 usb_kill_urb(port->write_urb);
152 if (serial->num_bulk_in)
153 usb_kill_urb(port->read_urb);
154 }
155}
156
157void usb_serial_generic_close (struct usb_serial_port *port, struct file * filp)
158{
159 dbg("%s - port %d", __FUNCTION__, port->number);
160 generic_cleanup (port);
161}
162
163int usb_serial_generic_write(struct usb_serial_port *port, const unsigned char *buf, int count)
164{
165 struct usb_serial *serial = port->serial;
166 int result;
167 unsigned char *data;
168
169 dbg("%s - port %d", __FUNCTION__, port->number);
170
171 if (count == 0) {
172 dbg("%s - write request of 0 bytes", __FUNCTION__);
173 return (0);
174 }
175
176 /* only do something if we have a bulk out endpoint */
177 if (serial->num_bulk_out) {
Peter Zijlstrae81ee632006-09-25 12:51:41 +0200178 spin_lock_bh(&port->lock);
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700179 if (port->write_urb_busy) {
Peter Zijlstrae81ee632006-09-25 12:51:41 +0200180 spin_unlock_bh(&port->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 dbg("%s - already writing", __FUNCTION__);
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700182 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 }
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700184 port->write_urb_busy = 1;
Peter Zijlstrae81ee632006-09-25 12:51:41 +0200185 spin_unlock_bh(&port->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
187 count = (count > port->bulk_out_size) ? port->bulk_out_size : count;
188
189 memcpy (port->write_urb->transfer_buffer, buf, count);
190 data = port->write_urb->transfer_buffer;
191 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, count, data);
192
193 /* set up our urb */
194 usb_fill_bulk_urb (port->write_urb, serial->dev,
195 usb_sndbulkpipe (serial->dev,
196 port->bulk_out_endpointAddress),
197 port->write_urb->transfer_buffer, count,
198 ((serial->type->write_bulk_callback) ?
199 serial->type->write_bulk_callback :
200 usb_serial_generic_write_bulk_callback), port);
201
202 /* send the data out the bulk port */
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700203 port->write_urb_busy = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700205 if (result) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 dev_err(&port->dev, "%s - failed submitting write urb, error %d\n", __FUNCTION__, result);
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700207 /* don't have to grab the lock here, as we will retry if != 0 */
208 port->write_urb_busy = 0;
209 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 result = count;
211
212 return result;
213 }
214
215 /* no bulk out, so return 0 bytes written */
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700216 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217}
218
219int usb_serial_generic_write_room (struct usb_serial_port *port)
220{
221 struct usb_serial *serial = port->serial;
222 int room = 0;
223
224 dbg("%s - port %d", __FUNCTION__, port->number);
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700225
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 if (serial->num_bulk_out) {
Randall Nortman7a3ca7d2005-10-14 17:21:50 -0700227 if (!(port->write_urb_busy))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 room = port->bulk_out_size;
229 }
230
231 dbg("%s - returns %d", __FUNCTION__, room);
232 return (room);
233}
234
235int usb_serial_generic_chars_in_buffer (struct usb_serial_port *port)
236{
237 struct usb_serial *serial = port->serial;
238 int chars = 0;
239
240 dbg("%s - port %d", __FUNCTION__, port->number);
241
242 if (serial->num_bulk_out) {
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700243 if (port->write_urb_busy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 chars = port->write_urb->transfer_buffer_length;
245 }
246
247 dbg("%s - returns %d", __FUNCTION__, chars);
248 return (chars);
249}
250
David Howells7d12e782006-10-05 14:55:46 +0100251void usb_serial_generic_read_bulk_callback (struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252{
253 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
254 struct usb_serial *serial = port->serial;
255 struct tty_struct *tty;
256 unsigned char *data = urb->transfer_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 int result;
258
259 dbg("%s - port %d", __FUNCTION__, port->number);
260
261 if (urb->status) {
262 dbg("%s - nonzero read bulk status received: %d", __FUNCTION__, urb->status);
263 return;
264 }
265
266 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, urb->actual_length, data);
267
268 tty = port->tty;
269 if (tty && urb->actual_length) {
Alan Cox33f0f882006-01-09 20:54:13 -0800270 tty_buffer_request_room(tty, urb->actual_length);
271 tty_insert_flip_string(tty, data, urb->actual_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 tty_flip_buffer_push(tty);
273 }
274
275 /* Continue trying to always read */
276 usb_fill_bulk_urb (port->read_urb, serial->dev,
277 usb_rcvbulkpipe (serial->dev,
278 port->bulk_in_endpointAddress),
279 port->read_urb->transfer_buffer,
280 port->read_urb->transfer_buffer_length,
281 ((serial->type->read_bulk_callback) ?
282 serial->type->read_bulk_callback :
283 usb_serial_generic_read_bulk_callback), port);
284 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
285 if (result)
286 dev_err(&port->dev, "%s - failed resubmitting read urb, error %d\n", __FUNCTION__, result);
287}
Luiz Fernando N. Capitulino166ffcc2006-07-11 14:19:25 -0300288EXPORT_SYMBOL_GPL(usb_serial_generic_read_bulk_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
David Howells7d12e782006-10-05 14:55:46 +0100290void usb_serial_generic_write_bulk_callback (struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291{
292 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
293
294 dbg("%s - port %d", __FUNCTION__, port->number);
295
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700296 port->write_urb_busy = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 if (urb->status) {
298 dbg("%s - nonzero write bulk status received: %d", __FUNCTION__, urb->status);
299 return;
300 }
301
Pete Zaitcevcf2c7482006-05-22 21:58:49 -0700302 usb_serial_port_softint(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303}
Greg Kroah-Hartmanbb833982005-11-17 09:48:18 -0800304EXPORT_SYMBOL_GPL(usb_serial_generic_write_bulk_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
306void usb_serial_generic_shutdown (struct usb_serial *serial)
307{
308 int i;
309
310 dbg("%s", __FUNCTION__);
311
312 /* stop reads and writes on all ports */
313 for (i=0; i < serial->num_ports; ++i) {
314 generic_cleanup(serial->port[i]);
315 }
316}
317