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