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