VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[linux-2.6.git] / drivers / usb / serial / ipaq.c
1 /*
2  * USB Compaq iPAQ driver
3  *
4  *      Copyright (C) 2001 - 2002
5  *          Ganesh Varadarajan <ganesh@veritas.com>
6  *
7  *      This program is free software; you can redistribute it and/or modify
8  *      it under the terms of the GNU General Public License as published by
9  *      the Free Software Foundation; either version 2 of the License, or
10  *      (at your option) any later version.
11  *
12  * (12/12/2002) ganesh
13  *      Added support for practically all devices supported by ActiveSync
14  *      on Windows. Thanks to Wes Cilldhaire <billybobjoehenrybob@hotmail.com>.
15  *
16  * (26/11/2002) ganesh
17  *      Added insmod options to specify product and vendor id.
18  *      Use modprobe ipaq vendor=0xfoo product=0xbar
19  *
20  * (26/7/2002) ganesh
21  *      Fixed up broken error handling in ipaq_open. Retry the "kickstart"
22  *      packet much harder - this drastically reduces connection failures.
23  *
24  * (30/4/2002) ganesh
25  *      Added support for the Casio EM500. Completely untested. Thanks
26  *      to info from Nathan <wfilardo@fuse.net>
27  *
28  * (19/3/2002) ganesh
29  *      Don't submit urbs while holding spinlocks. Not strictly necessary
30  *      in 2.5.x.
31  *
32  * (8/3/2002) ganesh
33  *      The ipaq sometimes emits a '\0' before the CLIENT string. At this
34  *      point of time, the ppp ldisc is not yet attached to the tty, so
35  *      n_tty echoes "^ " to the ipaq, which messes up the chat. In 2.5.6-pre2
36  *      this causes a panic because echo_char() tries to sleep in interrupt
37  *      context.
38  *      The fix is to tell the upper layers that this is a raw device so that
39  *      echoing is suppressed. Thanks to Lyle Lindholm for a detailed bug
40  *      report.
41  *
42  * (25/2/2002) ganesh
43  *      Added support for the HP Jornada 548 and 568. Completely untested.
44  *      Thanks to info from Heath Robinson and Arieh Davidoff.
45  */
46
47 #include <linux/config.h>
48 #include <linux/kernel.h>
49 #include <linux/errno.h>
50 #include <linux/init.h>
51 #include <linux/slab.h>
52 #include <linux/tty.h>
53 #include <linux/tty_driver.h>
54 #include <linux/tty_flip.h>
55 #include <linux/module.h>
56 #include <linux/spinlock.h>
57 #include <asm/uaccess.h>
58 #include <linux/usb.h>
59 #include "usb-serial.h"
60 #include "ipaq.h"
61
62 #define KP_RETRIES      100
63
64 /*
65  * Version Information
66  */
67
68 #define DRIVER_VERSION "v0.5"
69 #define DRIVER_AUTHOR "Ganesh Varadarajan <ganesh@veritas.com>"
70 #define DRIVER_DESC "USB PocketPC PDA driver"
71
72 static __u16 product, vendor;
73 static int debug;
74
75 /* Function prototypes for an ipaq */
76 static int  ipaq_open (struct usb_serial_port *port, struct file *filp);
77 static void ipaq_close (struct usb_serial_port *port, struct file *filp);
78 static int  ipaq_startup (struct usb_serial *serial);
79 static void ipaq_shutdown (struct usb_serial *serial);
80 static int ipaq_write(struct usb_serial_port *port, int from_user, const unsigned char *buf,
81                        int count);
82 static int ipaq_write_bulk(struct usb_serial_port *port, int from_user, const unsigned char *buf,
83                            int count);
84 static void ipaq_write_gather(struct usb_serial_port *port);
85 static void ipaq_read_bulk_callback (struct urb *urb, struct pt_regs *regs);
86 static void ipaq_write_bulk_callback(struct urb *urb, struct pt_regs *regs);
87 static int ipaq_write_room(struct usb_serial_port *port);
88 static int ipaq_chars_in_buffer(struct usb_serial_port *port);
89 static void ipaq_destroy_lists(struct usb_serial_port *port);
90
91
92 static struct usb_device_id ipaq_id_table [] = {
93         /* The first entry is a placeholder for the insmod-specified device */
94         { USB_DEVICE(COMPAQ_VENDOR_ID, COMPAQ_IPAQ_ID) },
95         { USB_DEVICE(ASKEY_VENDOR_ID, ASKEY_PRODUCT_ID) },
96         { USB_DEVICE(BCOM_VENDOR_ID, BCOM_0065_ID) },
97         { USB_DEVICE(BCOM_VENDOR_ID, BCOM_0066_ID) },
98         { USB_DEVICE(BCOM_VENDOR_ID, BCOM_0067_ID) },
99         { USB_DEVICE(CASIO_VENDOR_ID, CASIO_2001_ID) },
100         { USB_DEVICE(CASIO_VENDOR_ID, CASIO_EM500_ID) },
101         { USB_DEVICE(COMPAQ_VENDOR_ID, COMPAQ_IPAQ_ID) },
102         { USB_DEVICE(COMPAQ_VENDOR_ID, COMPAQ_0032_ID) },
103         { USB_DEVICE(DELL_VENDOR_ID, DELL_AXIM_ID) },
104         { USB_DEVICE(FSC_VENDOR_ID, FSC_LOOX_ID) },
105         { USB_DEVICE(HP_VENDOR_ID, HP_JORNADA_548_ID) },
106         { USB_DEVICE(HP_VENDOR_ID, HP_JORNADA_568_ID) },
107         { USB_DEVICE(HP_VENDOR_ID, HP_2016_ID) },
108         { USB_DEVICE(HP_VENDOR_ID, HP_2116_ID) },
109         { USB_DEVICE(HP_VENDOR_ID, HP_2216_ID) },
110         { USB_DEVICE(HP_VENDOR_ID, HP_3016_ID) },
111         { USB_DEVICE(HP_VENDOR_ID, HP_3116_ID) },
112         { USB_DEVICE(HP_VENDOR_ID, HP_3216_ID) },
113         { USB_DEVICE(HP_VENDOR_ID, HP_4016_ID) },
114         { USB_DEVICE(HP_VENDOR_ID, HP_4116_ID) },
115         { USB_DEVICE(HP_VENDOR_ID, HP_4216_ID) },
116         { USB_DEVICE(HP_VENDOR_ID, HP_5016_ID) },
117         { USB_DEVICE(HP_VENDOR_ID, HP_5116_ID) },
118         { USB_DEVICE(HP_VENDOR_ID, HP_5216_ID) },
119         { USB_DEVICE(LINKUP_VENDOR_ID, LINKUP_PRODUCT_ID) },
120         { USB_DEVICE(MICROSOFT_VENDOR_ID, MICROSOFT_00CE_ID) },
121         { USB_DEVICE(PORTATEC_VENDOR_ID, PORTATEC_PRODUCT_ID) },
122         { USB_DEVICE(ROVER_VENDOR_ID, ROVER_P5_ID) },
123         { USB_DEVICE(SAGEM_VENDOR_ID, SAGEM_WIRELESS_ID) },
124         { USB_DEVICE(SOCKET_VENDOR_ID, SOCKET_PRODUCT_ID) },
125         { USB_DEVICE(TOSHIBA_VENDOR_ID, TOSHIBA_PRODUCT_ID) },
126         { USB_DEVICE(TOSHIBA_VENDOR_ID, TOSHIBA_E310_ID) },
127         { USB_DEVICE(TOSHIBA_VENDOR_ID, TOSHIBA_E740_ID) },
128         { USB_DEVICE(TOSHIBA_VENDOR_ID, TOSHIBA_E335_ID) },
129         { USB_DEVICE(HTC_VENDOR_ID, HTC_PRODUCT_ID) },
130         { USB_DEVICE(NEC_VENDOR_ID, NEC_PRODUCT_ID) },
131         { USB_DEVICE(ASUS_VENDOR_ID, ASUS_A600_PRODUCT_ID) },
132         { USB_DEVICE(ASUS_VENDOR_ID, ASUS_A620_PRODUCT_ID) },
133         { }                                     /* Terminating entry */
134 };
135
136 MODULE_DEVICE_TABLE (usb, ipaq_id_table);
137
138 static struct usb_driver ipaq_driver = {
139         .owner =        THIS_MODULE,
140         .name =         "ipaq",
141         .probe =        usb_serial_probe,
142         .disconnect =   usb_serial_disconnect,
143         .id_table =     ipaq_id_table,
144 };
145
146
147 /* All of the device info needed for the Compaq iPAQ */
148 static struct usb_serial_device_type ipaq_device = {
149         .owner =                THIS_MODULE,
150         .name =                 "PocketPC PDA",
151         .id_table =             ipaq_id_table,
152         .num_interrupt_in =     NUM_DONT_CARE,
153         .num_bulk_in =          1,
154         .num_bulk_out =         1,
155         .num_ports =            1,
156         .open =                 ipaq_open,
157         .close =                ipaq_close,
158         .attach =               ipaq_startup,
159         .shutdown =             ipaq_shutdown,
160         .write =                ipaq_write,
161         .write_room =           ipaq_write_room,
162         .chars_in_buffer =      ipaq_chars_in_buffer,
163         .read_bulk_callback =   ipaq_read_bulk_callback,
164         .write_bulk_callback =  ipaq_write_bulk_callback,
165 };
166
167 static spinlock_t       write_list_lock;
168 static int              bytes_in;
169 static int              bytes_out;
170
171 static int ipaq_open(struct usb_serial_port *port, struct file *filp)
172 {
173         struct usb_serial       *serial = port->serial;
174         struct ipaq_private     *priv;
175         struct ipaq_packet      *pkt;
176         int                     i, result = 0;
177         int                     retries = KP_RETRIES;
178
179         dbg("%s - port %d", __FUNCTION__, port->number);
180
181         bytes_in = 0;
182         bytes_out = 0;
183         priv = (struct ipaq_private *)kmalloc(sizeof(struct ipaq_private), GFP_KERNEL);
184         if (priv == NULL) {
185                 err("%s - Out of memory", __FUNCTION__);
186                 return -ENOMEM;
187         }
188         usb_set_serial_port_data(port, priv);
189         priv->active = 0;
190         priv->queue_len = 0;
191         INIT_LIST_HEAD(&priv->queue);
192         INIT_LIST_HEAD(&priv->freelist);
193
194         for (i = 0; i < URBDATA_QUEUE_MAX / PACKET_SIZE; i++) {
195                 pkt = kmalloc(sizeof(struct ipaq_packet), GFP_KERNEL);
196                 if (pkt == NULL) {
197                         goto enomem;
198                 }
199                 pkt->data = kmalloc(PACKET_SIZE, GFP_KERNEL);
200                 if (pkt->data == NULL) {
201                         kfree(pkt);
202                         goto enomem;
203                 }
204                 pkt->len = 0;
205                 pkt->written = 0;
206                 INIT_LIST_HEAD(&pkt->list);
207                 list_add(&pkt->list, &priv->freelist);
208                 priv->free_len += PACKET_SIZE;
209         }
210
211         /*
212          * Force low latency on. This will immediately push data to the line
213          * discipline instead of queueing.
214          */
215
216         port->tty->low_latency = 1;
217         port->tty->raw = 1;
218         port->tty->real_raw = 1;
219
220         /*
221          * Lose the small buffers usbserial provides. Make larger ones.
222          */
223
224         kfree(port->bulk_in_buffer);
225         kfree(port->bulk_out_buffer);
226         port->bulk_in_buffer = kmalloc(URBDATA_SIZE, GFP_KERNEL);
227         if (port->bulk_in_buffer == NULL) {
228                 goto enomem;
229         }
230         port->bulk_out_buffer = kmalloc(URBDATA_SIZE, GFP_KERNEL);
231         if (port->bulk_out_buffer == NULL) {
232                 kfree(port->bulk_in_buffer);
233                 goto enomem;
234         }
235         port->read_urb->transfer_buffer = port->bulk_in_buffer;
236         port->write_urb->transfer_buffer = port->bulk_out_buffer;
237         port->read_urb->transfer_buffer_length = URBDATA_SIZE;
238         port->bulk_out_size = port->write_urb->transfer_buffer_length = URBDATA_SIZE;
239         
240         /* Start reading from the device */
241         usb_fill_bulk_urb(port->read_urb, serial->dev, 
242                       usb_rcvbulkpipe(serial->dev, port->bulk_in_endpointAddress),
243                       port->read_urb->transfer_buffer, port->read_urb->transfer_buffer_length,
244                       ipaq_read_bulk_callback, port);
245         result = usb_submit_urb(port->read_urb, GFP_KERNEL);
246         if (result) {
247                 err("%s - failed submitting read urb, error %d", __FUNCTION__, result);
248                 goto error;
249         }
250
251         /*
252          * Send out control message observed in win98 sniffs. Not sure what
253          * it does, but from empirical observations, it seems that the device
254          * will start the chat sequence once one of these messages gets
255          * through. Since this has a reasonably high failure rate, we retry
256          * several times.
257          */
258
259         while (retries--) {
260                 result = usb_control_msg(serial->dev,
261                                 usb_sndctrlpipe(serial->dev, 0), 0x22, 0x21,
262                                 0x1, 0, NULL, 0, HZ / 10 + 1);
263                 if (result == 0) {
264                         return 0;
265                 }
266         }
267         err("%s - failed doing control urb, error %d", __FUNCTION__, result);
268         goto error;
269
270 enomem:
271         result = -ENOMEM;
272         err("%s - Out of memory", __FUNCTION__);
273 error:
274         ipaq_destroy_lists(port);
275         kfree(priv);
276         return result;
277 }
278
279
280 static void ipaq_close(struct usb_serial_port *port, struct file *filp)
281 {
282         struct ipaq_private     *priv = usb_get_serial_port_data(port);
283
284         dbg("%s - port %d", __FUNCTION__, port->number);
285                          
286         /*
287          * shut down bulk read and write
288          */
289         usb_unlink_urb(port->write_urb);
290         usb_unlink_urb(port->read_urb);
291         ipaq_destroy_lists(port);
292         kfree(priv);
293         usb_set_serial_port_data(port, NULL);
294
295         /* Uncomment the following line if you want to see some statistics in your syslog */
296         /* info ("Bytes In = %d  Bytes Out = %d", bytes_in, bytes_out); */
297 }
298
299 static void ipaq_read_bulk_callback(struct urb *urb, struct pt_regs *regs)
300 {
301         struct usb_serial_port  *port = (struct usb_serial_port *)urb->context;
302         struct tty_struct       *tty;
303         unsigned char           *data = urb->transfer_buffer;
304         int                     i, result;
305
306         dbg("%s - port %d", __FUNCTION__, port->number);
307
308         if (urb->status) {
309                 dbg("%s - nonzero read bulk status received: %d", __FUNCTION__, urb->status);
310                 return;
311         }
312
313         usb_serial_debug_data(debug, &port->dev, __FUNCTION__, urb->actual_length, data);
314
315         tty = port->tty;
316         if (tty && urb->actual_length) {
317                 for (i = 0; i < urb->actual_length ; ++i) {
318                         /* if we insert more than TTY_FLIPBUF_SIZE characters, we drop them. */
319                         if(tty->flip.count >= TTY_FLIPBUF_SIZE) {
320                                 tty_flip_buffer_push(tty);
321                         }
322                         /* this doesn't actually push the data through unless tty->low_latency is set */
323                         tty_insert_flip_char(tty, data[i], 0);
324                 }
325                 tty_flip_buffer_push(tty);
326                 bytes_in += urb->actual_length;
327         }
328
329         /* Continue trying to always read  */
330         usb_fill_bulk_urb(port->read_urb, port->serial->dev, 
331                       usb_rcvbulkpipe(port->serial->dev, port->bulk_in_endpointAddress),
332                       port->read_urb->transfer_buffer, port->read_urb->transfer_buffer_length,
333                       ipaq_read_bulk_callback, port);
334         result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
335         if (result)
336                 err("%s - failed resubmitting read urb, error %d", __FUNCTION__, result);
337         return;
338 }
339
340 static int ipaq_write(struct usb_serial_port *port, int from_user, const unsigned char *buf,
341                        int count)
342 {
343         const unsigned char     *current_position = buf;
344         int                     bytes_sent = 0;
345         int                     transfer_size;
346
347         dbg("%s - port %d", __FUNCTION__, port->number);
348
349         while (count > 0) {
350                 transfer_size = min(count, PACKET_SIZE);
351                 if (ipaq_write_bulk(port, from_user, current_position, transfer_size)) {
352                         break;
353                 }
354                 current_position += transfer_size;
355                 bytes_sent += transfer_size;
356                 count -= transfer_size;
357                 bytes_out += transfer_size;
358         }
359
360         return bytes_sent;
361
362
363 static int ipaq_write_bulk(struct usb_serial_port *port, int from_user, const unsigned char *buf,
364                            int count)
365 {
366         struct ipaq_private     *priv = usb_get_serial_port_data(port);
367         struct ipaq_packet      *pkt = NULL;
368         int                     result = 0;
369         unsigned long           flags;
370
371         if (priv->free_len <= 0) {
372                 dbg("%s - we're stuffed", __FUNCTION__);
373                 return -EAGAIN;
374         }
375
376         spin_lock_irqsave(&write_list_lock, flags);
377         if (!list_empty(&priv->freelist)) {
378                 pkt = list_entry(priv->freelist.next, struct ipaq_packet, list);
379                 list_del(&pkt->list);
380                 priv->free_len -= PACKET_SIZE;
381         }
382         spin_unlock_irqrestore(&write_list_lock, flags);
383         if (pkt == NULL) {
384                 dbg("%s - we're stuffed", __FUNCTION__);
385                 return -EAGAIN;
386         }
387
388         if (from_user) {
389                 if (copy_from_user(pkt->data, buf, count))
390                         return -EFAULT;
391         } else {
392                 memcpy(pkt->data, buf, count);
393         }
394         usb_serial_debug_data(debug, &port->dev, __FUNCTION__, count, pkt->data);
395
396         pkt->len = count;
397         pkt->written = 0;
398         spin_lock_irqsave(&write_list_lock, flags);
399         list_add_tail(&pkt->list, &priv->queue);
400         priv->queue_len += count;
401         if (priv->active == 0) {
402                 priv->active = 1;
403                 ipaq_write_gather(port);
404                 spin_unlock_irqrestore(&write_list_lock, flags);
405                 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
406                 if (result) {
407                         err("%s - failed submitting write urb, error %d", __FUNCTION__, result);
408                 }
409         } else {
410                 spin_unlock_irqrestore(&write_list_lock, flags);
411         }
412         return result;
413 }
414
415 static void ipaq_write_gather(struct usb_serial_port *port)
416 {
417         struct ipaq_private     *priv = usb_get_serial_port_data(port);
418         struct usb_serial       *serial = port->serial;
419         int                     count, room;
420         struct ipaq_packet      *pkt;
421         struct urb              *urb = port->write_urb;
422         struct list_head        *tmp;
423
424         if (urb->status == -EINPROGRESS) {
425                 /* Should never happen */
426                 err("%s - flushing while urb is active !", __FUNCTION__);
427                 return;
428         }
429         room = URBDATA_SIZE;
430         for (tmp = priv->queue.next; tmp != &priv->queue;) {
431                 pkt = list_entry(tmp, struct ipaq_packet, list);
432                 tmp = tmp->next;
433                 count = min(room, (int)(pkt->len - pkt->written));
434                 memcpy(urb->transfer_buffer + (URBDATA_SIZE - room),
435                        pkt->data + pkt->written, count);
436                 room -= count;
437                 pkt->written += count;
438                 priv->queue_len -= count;
439                 if (pkt->written == pkt->len) {
440                         list_move(&pkt->list, &priv->freelist);
441                         priv->free_len += PACKET_SIZE;
442                 }
443                 if (room == 0) {
444                         break;
445                 }
446         }
447
448         count = URBDATA_SIZE - room;
449         usb_fill_bulk_urb(port->write_urb, serial->dev, 
450                       usb_sndbulkpipe(serial->dev, port->bulk_out_endpointAddress),
451                       port->write_urb->transfer_buffer, count, ipaq_write_bulk_callback,
452                       port);
453         return;
454 }
455
456 static void ipaq_write_bulk_callback(struct urb *urb, struct pt_regs *regs)
457 {
458         struct usb_serial_port  *port = (struct usb_serial_port *)urb->context;
459         struct ipaq_private     *priv = usb_get_serial_port_data(port);
460         unsigned long           flags;
461         int                     result;
462
463         dbg("%s - port %d", __FUNCTION__, port->number);
464         
465         if (urb->status) {
466                 dbg("%s - nonzero write bulk status received: %d", __FUNCTION__, urb->status);
467         }
468
469         spin_lock_irqsave(&write_list_lock, flags);
470         if (!list_empty(&priv->queue)) {
471                 ipaq_write_gather(port);
472                 spin_unlock_irqrestore(&write_list_lock, flags);
473                 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
474                 if (result) {
475                         err("%s - failed submitting write urb, error %d", __FUNCTION__, result);
476                 }
477         } else {
478                 priv->active = 0;
479                 spin_unlock_irqrestore(&write_list_lock, flags);
480         }
481
482         schedule_work(&port->work);
483 }
484
485 static int ipaq_write_room(struct usb_serial_port *port)
486 {
487         struct ipaq_private     *priv = usb_get_serial_port_data(port);
488
489         dbg("%s - freelen %d", __FUNCTION__, priv->free_len);
490         return priv->free_len;
491 }
492
493 static int ipaq_chars_in_buffer(struct usb_serial_port *port)
494 {
495         struct ipaq_private     *priv = usb_get_serial_port_data(port);
496
497         dbg("%s - queuelen %d", __FUNCTION__, priv->queue_len);
498         return priv->queue_len;
499 }
500
501 static void ipaq_destroy_lists(struct usb_serial_port *port)
502 {
503         struct ipaq_private     *priv = usb_get_serial_port_data(port);
504         struct list_head        *tmp;
505         struct ipaq_packet      *pkt;
506
507         for (tmp = priv->queue.next; tmp != &priv->queue;) {
508                 pkt = list_entry(tmp, struct ipaq_packet, list);
509                 tmp = tmp->next;
510                 kfree(pkt->data);
511                 kfree(pkt);
512         }
513         for (tmp = priv->freelist.next; tmp != &priv->freelist;) {
514                 pkt = list_entry(tmp, struct ipaq_packet, list);
515                 tmp = tmp->next;
516                 kfree(pkt->data);
517                 kfree(pkt);
518         }
519         return;
520 }
521
522
523 static int ipaq_startup(struct usb_serial *serial)
524 {
525         dbg("%s", __FUNCTION__);
526         if (serial->dev->actconfig->desc.bConfigurationValue != 1) {
527                 err("active config #%d != 1 ??",
528                         serial->dev->actconfig->desc.bConfigurationValue);
529                 return -ENODEV;
530         }
531         return usb_reset_configuration (serial->dev);
532 }
533
534 static void ipaq_shutdown(struct usb_serial *serial)
535 {
536         dbg("%s", __FUNCTION__);
537 }
538
539 static int __init ipaq_init(void)
540 {
541         int retval;
542         spin_lock_init(&write_list_lock);
543         retval = usb_serial_register(&ipaq_device);
544         if (retval) 
545                 goto failed_usb_serial_register;
546         info(DRIVER_DESC " " DRIVER_VERSION);
547         if (vendor) {
548                 ipaq_id_table[0].idVendor = vendor;
549                 ipaq_id_table[0].idProduct = product;
550         }
551         retval = usb_register(&ipaq_driver);
552         if (retval)
553                 goto failed_usb_register;
554                   
555         return 0;
556 failed_usb_register:
557         usb_serial_deregister(&ipaq_device);
558 failed_usb_serial_register:
559         return retval;
560 }
561
562
563 static void __exit ipaq_exit(void)
564 {
565         usb_deregister(&ipaq_driver);
566         usb_serial_deregister(&ipaq_device);
567 }
568
569
570 module_init(ipaq_init);
571 module_exit(ipaq_exit);
572
573 MODULE_AUTHOR( DRIVER_AUTHOR );
574 MODULE_DESCRIPTION( DRIVER_DESC );
575 MODULE_LICENSE("GPL");
576
577 module_param(debug, bool, S_IRUGO | S_IWUSR);
578 MODULE_PARM_DESC(debug, "Debug enabled or not");
579
580 module_param(vendor, ushort, 0);
581 MODULE_PARM_DESC(vendor, "User specified USB idVendor");
582
583 module_param(product, ushort, 0);
584 MODULE_PARM_DESC(product, "User specified USB idProduct");