USB: TI 3410/5052 USB Serial: convert td_open_close_lock to mutex
[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
24 static int debug;
25
26 #ifdef CONFIG_USB_SERIAL_GENERIC
27
28 static int generic_probe(struct usb_interface *interface,
29                          const struct usb_device_id *id);
30
31 static __u16 vendor  = 0x05f9;
32 static __u16 product = 0xffff;
33
34 module_param(vendor, ushort, 0);
35 MODULE_PARM_DESC(vendor, "User specified USB idVendor");
36
37 module_param(product, ushort, 0);
38 MODULE_PARM_DESC(product, "User specified USB idProduct");
39
40 static struct usb_device_id generic_device_ids[2]; /* Initially all zeroes. */
41
42 /* we want to look at all devices, as the vendor/product id can change
43  * depending on the command line argument */
44 static struct usb_device_id generic_serial_ids[] = {
45         {.driver_info = 42},
46         {}
47 };
48
49 static 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
57 /* All of the device info needed for the Generic Serial Converter */
58 struct usb_serial_driver usb_serial_generic_device = {
59         .driver = {
60                 .owner =        THIS_MODULE,
61                 .name =         "generic",
62         },
63         .id_table =             generic_device_ids,
64         .usb_driver =           &generic_driver,
65         .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,
70         .throttle =             usb_serial_generic_throttle,
71         .unthrottle =           usb_serial_generic_unthrottle,
72         .resume =               usb_serial_generic_resume,
73 };
74
75 static int generic_probe(struct usb_interface *interface,
76                                const struct usb_device_id *id)
77 {
78         const struct usb_device_id *id_pattern;
79
80         id_pattern = usb_match_id(interface, generic_device_ids);
81         if (id_pattern != NULL)
82                 return usb_serial_probe(interface, id);
83         return -ENODEV;
84 }
85 #endif
86
87 int usb_serial_generic_register (int _debug)
88 {
89         int retval = 0;
90
91         debug = _debug;
92 #ifdef CONFIG_USB_SERIAL_GENERIC
93         generic_device_ids[0].idVendor = vendor;
94         generic_device_ids[0].idProduct = product;
95         generic_device_ids[0].match_flags = USB_DEVICE_ID_MATCH_VENDOR | USB_DEVICE_ID_MATCH_PRODUCT;
96
97         /* register our generic driver with ourselves */
98         retval = usb_serial_register (&usb_serial_generic_device);
99         if (retval)
100                 goto exit;
101         retval = usb_register(&generic_driver);
102         if (retval)
103                 usb_serial_deregister(&usb_serial_generic_device);
104 exit:
105 #endif
106         return retval;
107 }
108
109 void usb_serial_generic_deregister (void)
110 {
111 #ifdef CONFIG_USB_SERIAL_GENERIC
112         /* remove our generic driver */
113         usb_deregister(&generic_driver);
114         usb_serial_deregister (&usb_serial_generic_device);
115 #endif
116 }
117
118 int usb_serial_generic_open (struct usb_serial_port *port, struct file *filp)
119 {
120         struct usb_serial *serial = port->serial;
121         int result = 0;
122         unsigned long flags;
123
124         dbg("%s - port %d", __FUNCTION__, port->number);
125
126         /* force low_latency on so that our tty_push actually forces the data through, 
127            otherwise it is scheduled, and with high data rates (like with OHCI) data
128            can get lost. */
129         if (port->tty)
130                 port->tty->low_latency = 1;
131
132         /* clear the throttle flags */
133         spin_lock_irqsave(&port->lock, flags);
134         port->throttled = 0;
135         port->throttle_req = 0;
136         spin_unlock_irqrestore(&port->lock, flags);
137
138         /* if we have a bulk endpoint, start reading from it */
139         if (serial->num_bulk_in) {
140                 /* Start reading from the device */
141                 usb_fill_bulk_urb (port->read_urb, serial->dev,
142                                    usb_rcvbulkpipe(serial->dev, port->bulk_in_endpointAddress),
143                                    port->read_urb->transfer_buffer,
144                                    port->read_urb->transfer_buffer_length,
145                                    ((serial->type->read_bulk_callback) ?
146                                      serial->type->read_bulk_callback :
147                                      usb_serial_generic_read_bulk_callback),
148                                    port);
149                 result = usb_submit_urb(port->read_urb, GFP_KERNEL);
150                 if (result)
151                         dev_err(&port->dev, "%s - failed resubmitting read urb, error %d\n", __FUNCTION__, result);
152         }
153
154         return result;
155 }
156 EXPORT_SYMBOL_GPL(usb_serial_generic_open);
157
158 static void generic_cleanup (struct usb_serial_port *port)
159 {
160         struct usb_serial *serial = port->serial;
161
162         dbg("%s - port %d", __FUNCTION__, port->number);
163
164         if (serial->dev) {
165                 /* shutdown any bulk reads that might be going on */
166                 if (serial->num_bulk_out)
167                         usb_kill_urb(port->write_urb);
168                 if (serial->num_bulk_in)
169                         usb_kill_urb(port->read_urb);
170         }
171 }
172
173 int usb_serial_generic_resume(struct usb_serial *serial)
174 {
175         struct usb_serial_port *port;
176         int i, c = 0, r;
177
178         for (i = 0; i < serial->num_ports; i++) {
179                 port = serial->port[i];
180                 if (port->open_count && port->read_urb) {
181                         r = usb_submit_urb(port->read_urb, GFP_NOIO);
182                         if (r < 0)
183                                 c++;
184                 }
185         }
186
187         return c ? -EIO : 0;
188 }
189
190 void usb_serial_generic_close (struct usb_serial_port *port, struct file * filp)
191 {
192         dbg("%s - port %d", __FUNCTION__, port->number);
193         generic_cleanup (port);
194 }
195
196 int usb_serial_generic_write(struct usb_serial_port *port, const unsigned char *buf, int count)
197 {
198         struct usb_serial *serial = port->serial;
199         int result;
200         unsigned char *data;
201
202         dbg("%s - port %d", __FUNCTION__, port->number);
203
204         if (count == 0) {
205                 dbg("%s - write request of 0 bytes", __FUNCTION__);
206                 return (0);
207         }
208
209         /* only do something if we have a bulk out endpoint */
210         if (serial->num_bulk_out) {
211                 unsigned long flags;
212                 spin_lock_irqsave(&port->lock, flags);
213                 if (port->write_urb_busy) {
214                         spin_unlock_irqrestore(&port->lock, flags);
215                         dbg("%s - already writing", __FUNCTION__);
216                         return 0;
217                 }
218                 port->write_urb_busy = 1;
219                 spin_unlock_irqrestore(&port->lock, flags);
220
221                 count = (count > port->bulk_out_size) ? port->bulk_out_size : count;
222
223                 memcpy (port->write_urb->transfer_buffer, buf, count);
224                 data = port->write_urb->transfer_buffer;
225                 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, count, data);
226
227                 /* set up our urb */
228                 usb_fill_bulk_urb (port->write_urb, serial->dev,
229                                    usb_sndbulkpipe (serial->dev,
230                                                     port->bulk_out_endpointAddress),
231                                    port->write_urb->transfer_buffer, count,
232                                    ((serial->type->write_bulk_callback) ? 
233                                      serial->type->write_bulk_callback :
234                                      usb_serial_generic_write_bulk_callback), port);
235
236                 /* send the data out the bulk port */
237                 port->write_urb_busy = 1;
238                 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
239                 if (result) {
240                         dev_err(&port->dev, "%s - failed submitting write urb, error %d\n", __FUNCTION__, result);
241                         /* don't have to grab the lock here, as we will retry if != 0 */
242                         port->write_urb_busy = 0;
243                 } else
244                         result = count;
245
246                 return result;
247         }
248
249         /* no bulk out, so return 0 bytes written */
250         return 0;
251 }
252
253 int usb_serial_generic_write_room (struct usb_serial_port *port)
254 {
255         struct usb_serial *serial = port->serial;
256         int room = 0;
257
258         dbg("%s - port %d", __FUNCTION__, port->number);
259
260         if (serial->num_bulk_out) {
261                 if (!(port->write_urb_busy))
262                         room = port->bulk_out_size;
263         }
264
265         dbg("%s - returns %d", __FUNCTION__, room);
266         return (room);
267 }
268
269 int usb_serial_generic_chars_in_buffer (struct usb_serial_port *port)
270 {
271         struct usb_serial *serial = port->serial;
272         int chars = 0;
273
274         dbg("%s - port %d", __FUNCTION__, port->number);
275
276         if (serial->num_bulk_out) {
277                 if (port->write_urb_busy)
278                         chars = port->write_urb->transfer_buffer_length;
279         }
280
281         dbg("%s - returns %d", __FUNCTION__, chars);
282         return (chars);
283 }
284
285
286 static void resubmit_read_urb(struct usb_serial_port *port, gfp_t mem_flags)
287 {
288         struct urb *urb = port->read_urb;
289         struct usb_serial *serial = port->serial;
290         int result;
291
292         /* Continue reading from device */
293         usb_fill_bulk_urb (urb, serial->dev,
294                            usb_rcvbulkpipe (serial->dev,
295                                             port->bulk_in_endpointAddress),
296                            urb->transfer_buffer,
297                            urb->transfer_buffer_length,
298                            ((serial->type->read_bulk_callback) ? 
299                              serial->type->read_bulk_callback : 
300                              usb_serial_generic_read_bulk_callback), port);
301         result = usb_submit_urb(urb, mem_flags);
302         if (result)
303                 dev_err(&port->dev, "%s - failed resubmitting read urb, error %d\n", __FUNCTION__, result);
304 }
305
306 /* Push data to tty layer and resubmit the bulk read URB */
307 static void flush_and_resubmit_read_urb (struct usb_serial_port *port)
308 {
309         struct urb *urb = port->read_urb;
310         struct tty_struct *tty = port->tty;
311         int room;
312
313         /* Push data to tty */
314         if (tty && urb->actual_length) {
315                 room = tty_buffer_request_room(tty, urb->actual_length);
316                 if (room) {
317                         tty_insert_flip_string(tty, urb->transfer_buffer, room);
318                         tty_flip_buffer_push(tty); /* is this allowed from an URB callback ? */
319                 }
320         }
321
322         resubmit_read_urb(port, GFP_ATOMIC);
323 }
324
325 void usb_serial_generic_read_bulk_callback (struct urb *urb)
326 {
327         struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
328         unsigned char *data = urb->transfer_buffer;
329         int status = urb->status;
330         unsigned long flags;
331
332         dbg("%s - port %d", __FUNCTION__, port->number);
333
334         if (unlikely(status != 0)) {
335                 dbg("%s - nonzero read bulk status received: %d",
336                     __FUNCTION__, status);
337                 return;
338         }
339
340         usb_serial_debug_data(debug, &port->dev, __FUNCTION__, urb->actual_length, data);
341
342         /* Throttle the device if requested by tty */
343         spin_lock_irqsave(&port->lock, flags);
344         if (!(port->throttled = port->throttle_req))
345                 /* Handle data and continue reading from device */
346                 flush_and_resubmit_read_urb(port);
347         spin_unlock_irqrestore(&port->lock, flags);
348 }
349 EXPORT_SYMBOL_GPL(usb_serial_generic_read_bulk_callback);
350
351 void usb_serial_generic_write_bulk_callback (struct urb *urb)
352 {
353         struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
354         int status = urb->status;
355
356         dbg("%s - port %d", __FUNCTION__, port->number);
357
358         port->write_urb_busy = 0;
359         if (status) {
360                 dbg("%s - nonzero write bulk status received: %d",
361                     __FUNCTION__, status);
362                 return;
363         }
364
365         usb_serial_port_softint(port);
366 }
367 EXPORT_SYMBOL_GPL(usb_serial_generic_write_bulk_callback);
368
369 void usb_serial_generic_throttle (struct usb_serial_port *port)
370 {
371         unsigned long flags;
372
373         dbg("%s - port %d", __FUNCTION__, port->number);
374
375         /* Set the throttle request flag. It will be picked up
376          * by usb_serial_generic_read_bulk_callback(). */
377         spin_lock_irqsave(&port->lock, flags);
378         port->throttle_req = 1;
379         spin_unlock_irqrestore(&port->lock, flags);
380 }
381
382 void usb_serial_generic_unthrottle (struct usb_serial_port *port)
383 {
384         int was_throttled;
385         unsigned long flags;
386
387         dbg("%s - port %d", __FUNCTION__, port->number);
388
389         /* Clear the throttle flags */
390         spin_lock_irqsave(&port->lock, flags);
391         was_throttled = port->throttled;
392         port->throttled = port->throttle_req = 0;
393         spin_unlock_irqrestore(&port->lock, flags);
394
395         if (was_throttled) {
396                 /* Resume reading from device */
397                 resubmit_read_urb(port, GFP_KERNEL);
398         }
399 }
400
401 void usb_serial_generic_shutdown (struct usb_serial *serial)
402 {
403         int i;
404
405         dbg("%s", __FUNCTION__);
406
407         /* stop reads and writes on all ports */
408         for (i=0; i < serial->num_ports; ++i) {
409                 generic_cleanup(serial->port[i]);
410         }
411 }
412