Merge master.kernel.org:/pub/scm/linux/kernel/git/kyle/parisc-2.6
[linux-2.6] / 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/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>
20 #include <linux/usb/serial.h>
21 #include <asm/uaccess.h>
22
23 static int generic_probe(struct usb_interface *interface,
24                          const struct usb_device_id *id);
25
26
27 static int debug;
28
29 #ifdef CONFIG_USB_SERIAL_GENERIC
30 static __u16 vendor  = 0x05f9;
31 static __u16 product = 0xffff;
32
33 module_param(vendor, ushort, 0);
34 MODULE_PARM_DESC(vendor, "User specified USB idVendor");
35
36 module_param(product, ushort, 0);
37 MODULE_PARM_DESC(product, "User specified USB idProduct");
38
39 static struct usb_device_id generic_device_ids[2]; /* Initially all zeroes. */
40
41 /* we want to look at all devices, as the vendor/product id can change
42  * depending on the command line argument */
43 static struct usb_device_id generic_serial_ids[] = {
44         {.driver_info = 42},
45         {}
46 };
47
48 static 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
56 /* All of the device info needed for the Generic Serial Converter */
57 struct usb_serial_driver usb_serial_generic_device = {
58         .driver = {
59                 .owner =        THIS_MODULE,
60                 .name =         "generic",
61         },
62         .id_table =             generic_device_ids,
63         .usb_driver =           &generic_driver,
64         .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,
69         .throttle =             usb_serial_generic_throttle,
70         .unthrottle =           usb_serial_generic_unthrottle,
71 };
72
73 static 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 }
83 #endif
84
85 int 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);
102 exit:
103 #endif
104         return retval;
105 }
106
107 void 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
116 int usb_serial_generic_open (struct usb_serial_port *port, struct file *filp)
117 {
118         struct usb_serial *serial = port->serial;
119         int result = 0;
120         unsigned long flags;
121
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
130         /* 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 */
137         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 }
154 EXPORT_SYMBOL_GPL(usb_serial_generic_open);
155
156 static 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
171 void 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
177 int 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) {
192                 spin_lock_bh(&port->lock);
193                 if (port->write_urb_busy) {
194                         spin_unlock_bh(&port->lock);
195                         dbg("%s - already writing", __FUNCTION__);
196                         return 0;
197                 }
198                 port->write_urb_busy = 1;
199                 spin_unlock_bh(&port->lock);
200
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 */
217                 port->write_urb_busy = 1;
218                 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
219                 if (result) {
220                         dev_err(&port->dev, "%s - failed submitting write urb, error %d\n", __FUNCTION__, result);
221                         /* don't have to grab the lock here, as we will retry if != 0 */
222                         port->write_urb_busy = 0;
223                 } else
224                         result = count;
225
226                 return result;
227         }
228
229         /* no bulk out, so return 0 bytes written */
230         return 0;
231 }
232
233 int 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);
239
240         if (serial->num_bulk_out) {
241                 if (!(port->write_urb_busy))
242                         room = port->bulk_out_size;
243         }
244
245         dbg("%s - returns %d", __FUNCTION__, room);
246         return (room);
247 }
248
249 int 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) {
257                 if (port->write_urb_busy)
258                         chars = port->write_urb->transfer_buffer_length;
259         }
260
261         dbg("%s - returns %d", __FUNCTION__, chars);
262         return (chars);
263 }
264
265 /* Push data to tty layer and resubmit the bulk read URB */
266 static void flush_and_resubmit_read_urb (struct usb_serial_port *port)
267 {
268         struct usb_serial *serial = port->serial;
269         struct urb *urb = port->read_urb;
270         struct tty_struct *tty = port->tty;
271         int result;
272
273         /* Push data to tty */
274         if (tty && urb->actual_length) {
275                 tty_buffer_request_room(tty, urb->actual_length);
276                 tty_insert_flip_string(tty, urb->transfer_buffer, urb->actual_length);
277                 tty_flip_buffer_push(tty); /* is this allowed from an URB callback ? */
278         }
279
280         /* Continue reading from device */
281         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 }
293
294 void 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 }
327 EXPORT_SYMBOL_GPL(usb_serial_generic_read_bulk_callback);
328
329 void usb_serial_generic_write_bulk_callback (struct urb *urb)
330 {
331         struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
332
333         dbg("%s - port %d", __FUNCTION__, port->number);
334
335         port->write_urb_busy = 0;
336         if (urb->status) {
337                 dbg("%s - nonzero write bulk status received: %d", __FUNCTION__, urb->status);
338                 return;
339         }
340
341         usb_serial_port_softint(port);
342 }
343 EXPORT_SYMBOL_GPL(usb_serial_generic_write_bulk_callback);
344
345 void 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
358 void 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
377 void 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