c00a440dc421a781aa0f44227c0ab385d5449b26
[linux-2.6.git] / drivers / usb / serial / generic.c
1 /*
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
12 #include <linux/config.h>
13 #include <linux/kernel.h>
14 #include <linux/errno.h>
15 #include <linux/slab.h>
16 #include <linux/tty.h>
17 #include <linux/tty_flip.h>
18 #include <linux/module.h>
19 #include <linux/moduleparam.h>
20 #include <linux/usb.h>
21 #include <asm/uaccess.h>
22 #include "usb-serial.h"
23
24 static int debug;
25
26 #ifdef CONFIG_USB_SERIAL_GENERIC
27 static __u16 vendor  = 0x05f9;
28 static __u16 product = 0xffff;
29
30 module_param(vendor, ushort, 0);
31 MODULE_PARM_DESC(vendor, "User specified USB idVendor");
32
33 module_param(product, ushort, 0);
34 MODULE_PARM_DESC(product, "User specified USB idProduct");
35
36 static struct usb_device_id generic_device_ids[2]; /* Initially all zeroes. */
37
38 /* All of the device info needed for the Generic Serial Converter */
39 struct usb_serial_driver usb_serial_generic_device = {
40         .driver = {
41                 .owner =        THIS_MODULE,
42                 .name =         "generic",
43         },
44         .id_table =             generic_device_ids,
45         .num_interrupt_in =     NUM_DONT_CARE,
46         .num_bulk_in =          NUM_DONT_CARE,
47         .num_bulk_out =         NUM_DONT_CARE,
48         .num_ports =            1,
49         .shutdown =             usb_serial_generic_shutdown,
50 };
51
52 /* we want to look at all devices, as the vendor/product id can change
53  * depending on the command line argument */
54 static struct usb_device_id generic_serial_ids[] = {
55         {.driver_info = 42},
56         {}
57 };
58
59 static int generic_probe(struct usb_interface *interface,
60                                const struct usb_device_id *id)
61 {
62         const struct usb_device_id *id_pattern;
63
64         id_pattern = usb_match_id(interface, generic_device_ids);
65         if (id_pattern != NULL)
66                 return usb_serial_probe(interface, id);
67         return -ENODEV;
68 }
69
70 static struct usb_driver generic_driver = {
71         .owner =        THIS_MODULE,
72         .name =         "usbserial_generic",
73         .probe =        generic_probe,
74         .disconnect =   usb_serial_disconnect,
75         .id_table =     generic_serial_ids,
76         .no_dynamic_id =        1,
77 };
78 #endif
79
80 int usb_serial_generic_register (int _debug)
81 {
82         int retval = 0;
83
84         debug = _debug;
85 #ifdef CONFIG_USB_SERIAL_GENERIC
86         generic_device_ids[0].idVendor = vendor;
87         generic_device_ids[0].idProduct = product;
88         generic_device_ids[0].match_flags = USB_DEVICE_ID_MATCH_VENDOR | USB_DEVICE_ID_MATCH_PRODUCT;
89
90         /* register our generic driver with ourselves */
91         retval = usb_serial_register (&usb_serial_generic_device);
92         if (retval)
93                 goto exit;
94         retval = usb_register(&generic_driver);
95         if (retval)
96                 usb_serial_deregister(&usb_serial_generic_device);
97 exit:
98 #endif
99         return retval;
100 }
101
102 void usb_serial_generic_deregister (void)
103 {
104 #ifdef CONFIG_USB_SERIAL_GENERIC
105         /* remove our generic driver */
106         usb_deregister(&generic_driver);
107         usb_serial_deregister (&usb_serial_generic_device);
108 #endif
109 }
110
111 int usb_serial_generic_open (struct usb_serial_port *port, struct file *filp)
112 {
113         struct usb_serial *serial = port->serial;
114         int result = 0;
115
116         dbg("%s - port %d", __FUNCTION__, port->number);
117
118         /* force low_latency on so that our tty_push actually forces the data through, 
119            otherwise it is scheduled, and with high data rates (like with OHCI) data
120            can get lost. */
121         if (port->tty)
122                 port->tty->low_latency = 1;
123
124         /* if we have a bulk interrupt, start reading from it */
125         if (serial->num_bulk_in) {
126                 /* Start reading from the device */
127                 usb_fill_bulk_urb (port->read_urb, serial->dev,
128                                    usb_rcvbulkpipe(serial->dev, port->bulk_in_endpointAddress),
129                                    port->read_urb->transfer_buffer,
130                                    port->read_urb->transfer_buffer_length,
131                                    ((serial->type->read_bulk_callback) ?
132                                      serial->type->read_bulk_callback :
133                                      usb_serial_generic_read_bulk_callback),
134                                    port);
135                 result = usb_submit_urb(port->read_urb, GFP_KERNEL);
136                 if (result)
137                         dev_err(&port->dev, "%s - failed resubmitting read urb, error %d\n", __FUNCTION__, result);
138         }
139
140         return result;
141 }
142
143 static void generic_cleanup (struct usb_serial_port *port)
144 {
145         struct usb_serial *serial = port->serial;
146
147         dbg("%s - port %d", __FUNCTION__, port->number);
148
149         if (serial->dev) {
150                 /* shutdown any bulk reads that might be going on */
151                 if (serial->num_bulk_out)
152                         usb_kill_urb(port->write_urb);
153                 if (serial->num_bulk_in)
154                         usb_kill_urb(port->read_urb);
155         }
156 }
157
158 void usb_serial_generic_close (struct usb_serial_port *port, struct file * filp)
159 {
160         dbg("%s - port %d", __FUNCTION__, port->number);
161         generic_cleanup (port);
162 }
163
164 int usb_serial_generic_write(struct usb_serial_port *port, const unsigned char *buf, int count)
165 {
166         struct usb_serial *serial = port->serial;
167         int result;
168         unsigned char *data;
169
170         dbg("%s - port %d", __FUNCTION__, port->number);
171
172         if (count == 0) {
173                 dbg("%s - write request of 0 bytes", __FUNCTION__);
174                 return (0);
175         }
176
177         /* only do something if we have a bulk out endpoint */
178         if (serial->num_bulk_out) {
179                 spin_lock(&port->lock);
180                 if (port->write_urb_busy) {
181                         spin_unlock(&port->lock);
182                         dbg("%s - already writing", __FUNCTION__);
183                         return 0;
184                 }
185                 port->write_urb_busy = 1;
186                 spin_unlock(&port->lock);
187
188                 count = (count > port->bulk_out_size) ? port->bulk_out_size : count;
189
190                 memcpy (port->write_urb->transfer_buffer, buf, count);
191                 data = port->write_urb->transfer_buffer;
192                 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, count, data);
193
194                 /* set up our urb */
195                 usb_fill_bulk_urb (port->write_urb, serial->dev,
196                                    usb_sndbulkpipe (serial->dev,
197                                                     port->bulk_out_endpointAddress),
198                                    port->write_urb->transfer_buffer, count,
199                                    ((serial->type->write_bulk_callback) ? 
200                                      serial->type->write_bulk_callback :
201                                      usb_serial_generic_write_bulk_callback), port);
202
203                 /* send the data out the bulk port */
204                 port->write_urb_busy = 1;
205                 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
206                 if (result) {
207                         dev_err(&port->dev, "%s - failed submitting write urb, error %d\n", __FUNCTION__, result);
208                         /* don't have to grab the lock here, as we will retry if != 0 */
209                         port->write_urb_busy = 0;
210                 } else
211                         result = count;
212
213                 return result;
214         }
215
216         /* no bulk out, so return 0 bytes written */
217         return 0;
218 }
219
220 int usb_serial_generic_write_room (struct usb_serial_port *port)
221 {
222         struct usb_serial *serial = port->serial;
223         int room = 0;
224
225         dbg("%s - port %d", __FUNCTION__, port->number);
226
227         if (serial->num_bulk_out) {
228                 if (!(port->write_urb_busy))
229                         room = port->bulk_out_size;
230         }
231
232         dbg("%s - returns %d", __FUNCTION__, room);
233         return (room);
234 }
235
236 int usb_serial_generic_chars_in_buffer (struct usb_serial_port *port)
237 {
238         struct usb_serial *serial = port->serial;
239         int chars = 0;
240
241         dbg("%s - port %d", __FUNCTION__, port->number);
242
243         if (serial->num_bulk_out) {
244                 if (port->write_urb_busy)
245                         chars = port->write_urb->transfer_buffer_length;
246         }
247
248         dbg("%s - returns %d", __FUNCTION__, chars);
249         return (chars);
250 }
251
252 void usb_serial_generic_read_bulk_callback (struct urb *urb, struct pt_regs *regs)
253 {
254         struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
255         struct usb_serial *serial = port->serial;
256         struct tty_struct *tty;
257         unsigned char *data = urb->transfer_buffer;
258         int i;
259         int result;
260
261         dbg("%s - port %d", __FUNCTION__, port->number);
262
263         if (urb->status) {
264                 dbg("%s - nonzero read bulk status received: %d", __FUNCTION__, urb->status);
265                 return;
266         }
267
268         usb_serial_debug_data(debug, &port->dev, __FUNCTION__, urb->actual_length, data);
269
270         tty = port->tty;
271         if (tty && urb->actual_length) {
272                 for (i = 0; i < urb->actual_length ; ++i) {
273                         /* if we insert more than TTY_FLIPBUF_SIZE characters, we drop them. */
274                         if(tty->flip.count >= TTY_FLIPBUF_SIZE) {
275                                 tty_flip_buffer_push(tty);
276                         }
277                         /* this doesn't actually push the data through unless tty->low_latency is set */
278                         tty_insert_flip_char(tty, data[i], 0);
279                 }
280                 tty_flip_buffer_push(tty);
281         }
282
283         /* Continue trying to always read  */
284         usb_fill_bulk_urb (port->read_urb, serial->dev,
285                            usb_rcvbulkpipe (serial->dev,
286                                             port->bulk_in_endpointAddress),
287                            port->read_urb->transfer_buffer,
288                            port->read_urb->transfer_buffer_length,
289                            ((serial->type->read_bulk_callback) ? 
290                              serial->type->read_bulk_callback : 
291                              usb_serial_generic_read_bulk_callback), port);
292         result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
293         if (result)
294                 dev_err(&port->dev, "%s - failed resubmitting read urb, error %d\n", __FUNCTION__, result);
295 }
296
297 void usb_serial_generic_write_bulk_callback (struct urb *urb, struct pt_regs *regs)
298 {
299         struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
300
301         dbg("%s - port %d", __FUNCTION__, port->number);
302
303         port->write_urb_busy = 0;
304         if (urb->status) {
305                 dbg("%s - nonzero write bulk status received: %d", __FUNCTION__, urb->status);
306                 return;
307         }
308
309         usb_serial_port_softint((void *)port);
310
311         schedule_work(&port->work);
312 }
313 EXPORT_SYMBOL_GPL(usb_serial_generic_write_bulk_callback);
314
315 void usb_serial_generic_shutdown (struct usb_serial *serial)
316 {
317         int i;
318
319         dbg("%s", __FUNCTION__);
320
321         /* stop reads and writes on all ports */
322         for (i=0; i < serial->num_ports; ++i) {
323                 generic_cleanup(serial->port[i]);
324         }
325 }
326