Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dtor/input
[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 <linux/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_ports =            1,
66         .shutdown =             usb_serial_generic_shutdown,
67         .throttle =             usb_serial_generic_throttle,
68         .unthrottle =           usb_serial_generic_unthrottle,
69         .resume =               usb_serial_generic_resume,
70 };
71
72 static 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 }
82 #endif
83
84 int 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 =
93                 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 tty_struct *tty,
117                         struct usb_serial_port *port, struct file *filp)
118 {
119         struct usb_serial *serial = port->serial;
120         int result = 0;
121         unsigned long flags;
122
123         dbg("%s - port %d", __func__, port->number);
124
125         /* clear the throttle flags */
126         spin_lock_irqsave(&port->lock, flags);
127         port->throttled = 0;
128         port->throttle_req = 0;
129         spin_unlock_irqrestore(&port->lock, flags);
130
131         /* if we have a bulk endpoint, start reading from it */
132         if (serial->num_bulk_in) {
133                 /* Start reading from the device */
134                 usb_fill_bulk_urb(port->read_urb, serial->dev,
135                                    usb_rcvbulkpipe(serial->dev,
136                                                 port->bulk_in_endpointAddress),
137                                    port->read_urb->transfer_buffer,
138                                    port->read_urb->transfer_buffer_length,
139                                    ((serial->type->read_bulk_callback) ?
140                                      serial->type->read_bulk_callback :
141                                      usb_serial_generic_read_bulk_callback),
142                                    port);
143                 result = usb_submit_urb(port->read_urb, GFP_KERNEL);
144                 if (result)
145                         dev_err(&port->dev,
146                             "%s - failed resubmitting read urb, error %d\n",
147                                                         __func__, result);
148         }
149
150         return result;
151 }
152 EXPORT_SYMBOL_GPL(usb_serial_generic_open);
153
154 static void generic_cleanup(struct usb_serial_port *port)
155 {
156         struct usb_serial *serial = port->serial;
157
158         dbg("%s - port %d", __func__, port->number);
159
160         if (serial->dev) {
161                 /* shutdown any bulk reads that might be going on */
162                 if (serial->num_bulk_out)
163                         usb_kill_urb(port->write_urb);
164                 if (serial->num_bulk_in)
165                         usb_kill_urb(port->read_urb);
166         }
167 }
168
169 int usb_serial_generic_resume(struct usb_serial *serial)
170 {
171         struct usb_serial_port *port;
172         int i, c = 0, r;
173
174         for (i = 0; i < serial->num_ports; i++) {
175                 port = serial->port[i];
176                 if (port->port.count && port->read_urb) {
177                         r = usb_submit_urb(port->read_urb, GFP_NOIO);
178                         if (r < 0)
179                                 c++;
180                 }
181         }
182
183         return c ? -EIO : 0;
184 }
185 EXPORT_SYMBOL_GPL(usb_serial_generic_resume);
186
187 void usb_serial_generic_close(struct tty_struct *tty,
188                         struct usb_serial_port *port, struct file *filp)
189 {
190         dbg("%s - port %d", __func__, port->number);
191         generic_cleanup(port);
192 }
193
194 int usb_serial_generic_write(struct tty_struct *tty,
195         struct usb_serial_port *port, const unsigned char *buf, int count)
196 {
197         struct usb_serial *serial = port->serial;
198         int result;
199         unsigned char *data;
200
201         dbg("%s - port %d", __func__, port->number);
202
203         if (count == 0) {
204                 dbg("%s - write request of 0 bytes", __func__);
205                 return 0;
206         }
207
208         /* only do something if we have a bulk out endpoint */
209         if (serial->num_bulk_out) {
210                 unsigned long flags;
211                 spin_lock_irqsave(&port->lock, flags);
212                 if (port->write_urb_busy) {
213                         spin_unlock_irqrestore(&port->lock, flags);
214                         dbg("%s - already writing", __func__);
215                         return 0;
216                 }
217                 port->write_urb_busy = 1;
218                 spin_unlock_irqrestore(&port->lock, flags);
219
220                 count = (count > port->bulk_out_size) ?
221                                         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, __func__, 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),
235                                    port);
236
237                 /* send the data out the bulk port */
238                 port->write_urb_busy = 1;
239                 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
240                 if (result) {
241                         dev_err(&port->dev,
242                                 "%s - failed submitting write urb, error %d\n",
243                                                         __func__, result);
244                         /* don't have to grab the lock here, as we will
245                            retry if != 0 */
246                         port->write_urb_busy = 0;
247                 } else
248                         result = count;
249
250                 return result;
251         }
252
253         /* no bulk out, so return 0 bytes written */
254         return 0;
255 }
256
257 int usb_serial_generic_write_room(struct tty_struct *tty)
258 {
259         struct usb_serial_port *port = tty->driver_data;
260         struct usb_serial *serial = port->serial;
261         int room = 0;
262
263         dbg("%s - port %d", __func__, port->number);
264
265         /* FIXME: Locking */
266         if (serial->num_bulk_out) {
267                 if (!(port->write_urb_busy))
268                         room = port->bulk_out_size;
269         }
270
271         dbg("%s - returns %d", __func__, room);
272         return room;
273 }
274
275 int usb_serial_generic_chars_in_buffer(struct tty_struct *tty)
276 {
277         struct usb_serial_port *port = tty->driver_data;
278         struct usb_serial *serial = port->serial;
279         int chars = 0;
280
281         dbg("%s - port %d", __func__, port->number);
282
283         /* FIXME: Locking */
284         if (serial->num_bulk_out) {
285                 if (port->write_urb_busy)
286                         chars = port->write_urb->transfer_buffer_length;
287         }
288
289         dbg("%s - returns %d", __func__, chars);
290         return chars;
291 }
292
293
294 static void resubmit_read_urb(struct usb_serial_port *port, gfp_t mem_flags)
295 {
296         struct urb *urb = port->read_urb;
297         struct usb_serial *serial = port->serial;
298         int result;
299
300         /* Continue reading from device */
301         usb_fill_bulk_urb(urb, serial->dev,
302                            usb_rcvbulkpipe(serial->dev,
303                                         port->bulk_in_endpointAddress),
304                            urb->transfer_buffer,
305                            urb->transfer_buffer_length,
306                            ((serial->type->read_bulk_callback) ?
307                              serial->type->read_bulk_callback :
308                              usb_serial_generic_read_bulk_callback), port);
309         result = usb_submit_urb(urb, mem_flags);
310         if (result)
311                 dev_err(&port->dev,
312                         "%s - failed resubmitting read urb, error %d\n",
313                                                         __func__, result);
314 }
315
316 /* Push data to tty layer and resubmit the bulk read URB */
317 static void flush_and_resubmit_read_urb(struct usb_serial_port *port)
318 {
319         struct urb *urb = port->read_urb;
320         struct tty_struct *tty = tty_port_tty_get(&port->port);
321         int room;
322
323         /* Push data to tty */
324         if (tty && urb->actual_length) {
325                 room = tty_buffer_request_room(tty, urb->actual_length);
326                 if (room) {
327                         tty_insert_flip_string(tty, urb->transfer_buffer, room);
328                         tty_flip_buffer_push(tty);
329                 }
330         }
331         tty_kref_put(tty);
332
333         resubmit_read_urb(port, GFP_ATOMIC);
334 }
335
336 void usb_serial_generic_read_bulk_callback(struct urb *urb)
337 {
338         struct usb_serial_port *port = urb->context;
339         unsigned char *data = urb->transfer_buffer;
340         int status = urb->status;
341         unsigned long flags;
342
343         dbg("%s - port %d", __func__, port->number);
344
345         if (unlikely(status != 0)) {
346                 dbg("%s - nonzero read bulk status received: %d",
347                     __func__, status);
348                 return;
349         }
350
351         usb_serial_debug_data(debug, &port->dev, __func__,
352                                                 urb->actual_length, data);
353
354         /* Throttle the device if requested by tty */
355         spin_lock_irqsave(&port->lock, flags);
356         port->throttled = port->throttle_req;
357         if (!port->throttled) {
358                 spin_unlock_irqrestore(&port->lock, flags);
359                 flush_and_resubmit_read_urb(port);
360         } else
361                 spin_unlock_irqrestore(&port->lock, flags);
362 }
363 EXPORT_SYMBOL_GPL(usb_serial_generic_read_bulk_callback);
364
365 void usb_serial_generic_write_bulk_callback(struct urb *urb)
366 {
367         struct usb_serial_port *port = urb->context;
368         int status = urb->status;
369
370         dbg("%s - port %d", __func__, port->number);
371
372         port->write_urb_busy = 0;
373         if (status) {
374                 dbg("%s - nonzero write bulk status received: %d",
375                     __func__, status);
376                 return;
377         }
378         usb_serial_port_softint(port);
379 }
380 EXPORT_SYMBOL_GPL(usb_serial_generic_write_bulk_callback);
381
382 void usb_serial_generic_throttle(struct tty_struct *tty)
383 {
384         struct usb_serial_port *port = tty->driver_data;
385         unsigned long flags;
386
387         dbg("%s - port %d", __func__, port->number);
388
389         /* Set the throttle request flag. It will be picked up
390          * by usb_serial_generic_read_bulk_callback(). */
391         spin_lock_irqsave(&port->lock, flags);
392         port->throttle_req = 1;
393         spin_unlock_irqrestore(&port->lock, flags);
394 }
395
396 void usb_serial_generic_unthrottle(struct tty_struct *tty)
397 {
398         struct usb_serial_port *port = tty->driver_data;
399         int was_throttled;
400         unsigned long flags;
401
402         dbg("%s - port %d", __func__, port->number);
403
404         /* Clear the throttle flags */
405         spin_lock_irqsave(&port->lock, flags);
406         was_throttled = port->throttled;
407         port->throttled = port->throttle_req = 0;
408         spin_unlock_irqrestore(&port->lock, flags);
409
410         if (was_throttled) {
411                 /* Resume reading from device */
412                 resubmit_read_urb(port, GFP_KERNEL);
413         }
414 }
415
416 void usb_serial_generic_shutdown(struct usb_serial *serial)
417 {
418         int i;
419
420         dbg("%s", __func__);
421
422         /* stop reads and writes on all ports */
423         for (i = 0; i < serial->num_ports; ++i)
424                 generic_cleanup(serial->port[i]);
425 }
426