special usb hub handling, IDE disks, and retries all over the place
[linux-2.4.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. Thanks to Greg for pointing
30  *      this out.
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);
92 static void ipaq_write_bulk_callback(struct urb *urb);
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 /* All of the device info needed for the Compaq iPAQ */
144 struct usb_serial_device_type ipaq_device = {
145         .owner =                THIS_MODULE,
146         .name =                 "PocketPC PDA",
147         .id_table =             ipaq_id_table,
148         .num_interrupt_in =     NUM_DONT_CARE,
149         .num_bulk_in =          1,
150         .num_bulk_out =         1,
151         .num_ports =            1,
152         .open =                 ipaq_open,
153         .close =                ipaq_close,
154         .startup =              ipaq_startup,
155         .shutdown =             ipaq_shutdown,
156         .write =                ipaq_write,
157         .write_room =           ipaq_write_room,
158         .chars_in_buffer =      ipaq_chars_in_buffer,
159         .read_bulk_callback =   ipaq_read_bulk_callback,
160         .write_bulk_callback =  ipaq_write_bulk_callback,
161 };
162
163 static spinlock_t       write_list_lock;
164 static int              bytes_in;
165 static int              bytes_out;
166
167 static int ipaq_open(struct usb_serial_port *port, struct file *filp)
168 {
169         struct usb_serial       *serial = port->serial;
170         struct ipaq_private     *priv;
171         struct ipaq_packet      *pkt;
172         int                     i, result = 0;
173         int                     retries = KP_RETRIES;
174
175         if (port_paranoia_check(port, __FUNCTION__)) {
176                 return -ENODEV;
177         }
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         port->private = (void *)priv;
189         priv->active = 0;
190         priv->queue_len = 0;
191         priv->free_len = 0;
192         INIT_LIST_HEAD(&priv->queue);
193         INIT_LIST_HEAD(&priv->freelist);
194
195         for (i = 0; i < URBDATA_QUEUE_MAX / PACKET_SIZE; i++) {
196                 pkt = kmalloc(sizeof(struct ipaq_packet), GFP_KERNEL);
197                 if (pkt == NULL) {
198                         goto enomem;
199                 }
200                 pkt->data = kmalloc(PACKET_SIZE, GFP_KERNEL);
201                 if (pkt->data == NULL) {
202                         kfree(pkt);
203                         goto enomem;
204                 }
205                 pkt->len = 0;
206                 pkt->written = 0;
207                 INIT_LIST_HEAD(&pkt->list);
208                 list_add(&pkt->list, &priv->freelist);
209                 priv->free_len += PACKET_SIZE;
210         }
211
212         /*
213          * Force low latency on. This will immediately push data to the line
214          * discipline instead of queueing.
215          */
216
217         port->tty->low_latency = 1;
218         port->tty->raw = 1;
219         port->tty->real_raw = 1;
220
221         /*
222          * Lose the small buffers usbserial provides. Make larger ones.
223          */
224
225         kfree(port->bulk_in_buffer);
226         kfree(port->bulk_out_buffer);
227         port->bulk_in_buffer = kmalloc(URBDATA_SIZE, GFP_KERNEL);
228         if (port->bulk_in_buffer == NULL) {
229                 goto enomem;
230         }
231         port->bulk_out_buffer = kmalloc(URBDATA_SIZE, GFP_KERNEL);
232         if (port->bulk_out_buffer == NULL) {
233                 kfree(port->bulk_in_buffer);
234                 goto enomem;
235         }
236         port->read_urb->transfer_buffer = port->bulk_in_buffer;
237         port->write_urb->transfer_buffer = port->bulk_out_buffer;
238         port->read_urb->transfer_buffer_length = URBDATA_SIZE;
239         port->bulk_out_size = port->write_urb->transfer_buffer_length = URBDATA_SIZE;
240         
241         /* Start reading from the device */
242         FILL_BULK_URB(port->read_urb, serial->dev, 
243                       usb_rcvbulkpipe(serial->dev, port->bulk_in_endpointAddress),
244                       port->read_urb->transfer_buffer, port->read_urb->transfer_buffer_length,
245                       ipaq_read_bulk_callback, port);
246         result = usb_submit_urb(port->read_urb);
247         if (result) {
248                 err("%s - failed submitting read urb, error %d", __FUNCTION__, result);
249                 goto error;
250         }
251
252         /*
253          * Send out control message observed in win98 sniffs. Not sure what
254          * it does, but from empirical observations, it seems that the device
255          * will start the chat sequence once one of these messages gets
256          * through. Since this has a reasonably high failure rate, we retry
257          * several times.
258          */
259
260         while (retries--) {
261                 result = usb_control_msg(serial->dev,
262                                 usb_sndctrlpipe(serial->dev, 0), 0x22, 0x21,
263                                 0x1, 0, NULL, 0, HZ / 10 + 1);
264                 if (result == 0) {
265                         return 0;
266                 }
267         }
268         err("%s - failed doing control urb, error %d", __FUNCTION__, result);
269         goto error;
270
271 enomem:
272         result = -ENOMEM;
273         err("%s - Out of memory", __FUNCTION__);
274 error:
275         ipaq_destroy_lists(port);
276         kfree(priv);
277         return result;
278 }
279
280
281 static void ipaq_close(struct usb_serial_port *port, struct file *filp)
282 {
283         struct usb_serial       *serial;
284         struct ipaq_private     *priv = port->private;
285
286         if (port_paranoia_check(port, __FUNCTION__)) {
287                 return; 
288         }
289         
290         dbg("%s - port %d", __FUNCTION__, port->number);
291                          
292         serial = get_usb_serial(port, __FUNCTION__);
293         if (!serial)
294                 return;
295
296         /*
297          * shut down bulk read and write
298          */
299
300         usb_unlink_urb(port->write_urb);
301         usb_unlink_urb(port->read_urb);
302         ipaq_destroy_lists(port);
303         kfree(priv);
304         port->private = NULL;
305
306         /* Uncomment the following line if you want to see some statistics in your syslog */
307         /* info ("Bytes In = %d  Bytes Out = %d", bytes_in, bytes_out); */
308 }
309
310 static void ipaq_read_bulk_callback(struct urb *urb)
311 {
312         struct usb_serial_port  *port = (struct usb_serial_port *)urb->context;
313         struct usb_serial       *serial = get_usb_serial (port, __FUNCTION__);
314         struct tty_struct       *tty;
315         unsigned char           *data = urb->transfer_buffer;
316         int                     i, result;
317
318         if (port_paranoia_check(port, __FUNCTION__))
319                 return;
320
321         dbg("%s - port %d", __FUNCTION__, port->number);
322
323         if (!serial) {
324                 dbg("%s - bad serial pointer, exiting", __FUNCTION__);
325                 return;
326         }
327
328         if (urb->status) {
329                 dbg("%s - nonzero read bulk status received: %d", __FUNCTION__, urb->status);
330                 return;
331         }
332
333         usb_serial_debug_data (__FILE__, __FUNCTION__, urb->actual_length, data);
334
335         tty = port->tty;
336         if (tty && urb->actual_length) {
337                 for (i = 0; i < urb->actual_length ; ++i) {
338                         /* if we insert more than TTY_FLIPBUF_SIZE characters, we drop them. */
339                         if(tty->flip.count >= TTY_FLIPBUF_SIZE) {
340                                 tty_flip_buffer_push(tty);
341                         }
342                         /* this doesn't actually push the data through unless tty->low_latency is set */
343                         tty_insert_flip_char(tty, data[i], 0);
344                 }
345                 tty_flip_buffer_push(tty);
346                 bytes_in += urb->actual_length;
347         }
348
349         /* Continue trying to always read  */
350         FILL_BULK_URB(port->read_urb, serial->dev, 
351                       usb_rcvbulkpipe(serial->dev, port->bulk_in_endpointAddress),
352                       port->read_urb->transfer_buffer, port->read_urb->transfer_buffer_length,
353                       ipaq_read_bulk_callback, port);
354         result = usb_submit_urb(port->read_urb);
355         if (result)
356                 err("%s - failed resubmitting read urb, error %d", __FUNCTION__, result);
357         return;
358 }
359
360 static int ipaq_write(struct usb_serial_port *port, int from_user, const unsigned char *buf,
361                        int count)
362 {
363         const unsigned char     *current_position = buf;
364         int                     bytes_sent = 0;
365         int                     transfer_size;
366
367         dbg("%s - port %d", __FUNCTION__, port->number);
368
369         usb_serial_debug_data(__FILE__, __FUNCTION__, count, buf);
370         
371         while (count > 0) {
372                 transfer_size = min(count, PACKET_SIZE);
373                 if (ipaq_write_bulk(port, from_user, current_position, transfer_size)) {
374                         break;
375                 }
376                 current_position += transfer_size;
377                 bytes_sent += transfer_size;
378                 count -= transfer_size;
379                 bytes_out += transfer_size;
380         }
381
382         return bytes_sent;
383
384
385 static int ipaq_write_bulk(struct usb_serial_port *port, int from_user, const unsigned char *buf,
386                            int count)
387 {
388         struct ipaq_private     *priv = port->private;
389         struct ipaq_packet      *pkt = NULL;
390         int                     result = 0;
391         unsigned long           flags;
392
393         if (priv->free_len <= 0) {
394                 dbg("%s - we're stuffed", __FUNCTION__);
395                 return -EAGAIN;
396         }
397
398         spin_lock_irqsave(&write_list_lock, flags);
399         if (!list_empty(&priv->freelist)) {
400                 pkt = list_entry(priv->freelist.next, struct ipaq_packet, list);
401                 list_del(&pkt->list);
402                 priv->free_len -= PACKET_SIZE;
403         }
404         spin_unlock_irqrestore(&write_list_lock, flags);
405         if (pkt == NULL) {
406                 dbg("%s - we're stuffed", __FUNCTION__);
407                 return -EAGAIN;
408         }
409
410         if (from_user) {
411                 if (copy_from_user(pkt->data, buf, count))
412                         return -EFAULT;
413         } else {
414                 memcpy(pkt->data, buf, count);
415         }
416         usb_serial_debug_data(__FILE__, __FUNCTION__, count, pkt->data);
417
418         pkt->len = count;
419         pkt->written = 0;
420         spin_lock_irqsave(&write_list_lock, flags);
421         list_add_tail(&pkt->list, &priv->queue);
422         priv->queue_len += count;
423         if (priv->active == 0) {
424                 priv->active = 1;
425                 ipaq_write_gather(port);
426                 spin_unlock_irqrestore(&write_list_lock, flags);
427                 result = usb_submit_urb(port->write_urb);
428                 if (result) {
429                         err("%s - failed submitting write urb, error %d", __FUNCTION__, result);
430                 }
431         } else {
432                 spin_unlock_irqrestore(&write_list_lock, flags);
433         }
434         return result;
435 }
436
437 static void ipaq_write_gather(struct usb_serial_port *port)
438 {
439         struct ipaq_private     *priv = (struct ipaq_private *)port->private;
440         struct usb_serial       *serial = port->serial;
441         int                     count, room;
442         struct ipaq_packet      *pkt;
443         struct urb              *urb = port->write_urb;
444         struct list_head        *tmp;
445
446         if (urb->status == -EINPROGRESS) {
447                 /* Should never happen */
448                 err("%s - flushing while urb is active !", __FUNCTION__);
449                 return;
450         }
451         room = URBDATA_SIZE;
452         for (tmp = priv->queue.next; tmp != &priv->queue;) {
453                 pkt = list_entry(tmp, struct ipaq_packet, list);
454                 tmp = tmp->next;
455                 count = min(room, (int)(pkt->len - pkt->written));
456                 memcpy(urb->transfer_buffer + (URBDATA_SIZE - room),
457                        pkt->data + pkt->written, count);
458                 room -= count;
459                 pkt->written += count;
460                 priv->queue_len -= count;
461                 if (pkt->written == pkt->len) {
462                         list_del(&pkt->list);
463                         list_add(&pkt->list, &priv->freelist);
464                         priv->free_len += PACKET_SIZE;
465                 }
466                 if (room == 0) {
467                         break;
468                 }
469         }
470
471         count = URBDATA_SIZE - room;
472         FILL_BULK_URB(port->write_urb, serial->dev, 
473                       usb_sndbulkpipe(serial->dev, port->bulk_out_endpointAddress),
474                       port->write_urb->transfer_buffer, count, ipaq_write_bulk_callback,
475                       port);
476         return;
477 }
478
479 static void ipaq_write_bulk_callback(struct urb *urb)
480 {
481         struct usb_serial_port  *port = (struct usb_serial_port *)urb->context;
482         struct ipaq_private     *priv = (struct ipaq_private *)port->private;
483         unsigned long           flags;
484         int                     result;
485
486         if (port_paranoia_check (port, __FUNCTION__)) {
487                 return;
488         }
489         
490         dbg("%s - port %d", __FUNCTION__, port->number);
491         
492         if (urb->status) {
493                 dbg("%s - nonzero write bulk status received: %d", __FUNCTION__, urb->status);
494         }
495
496         spin_lock_irqsave(&write_list_lock, flags);
497         if (!list_empty(&priv->queue)) {
498                 ipaq_write_gather(port);
499                 spin_unlock_irqrestore(&write_list_lock, flags);
500                 result = usb_submit_urb(port->write_urb);
501                 if (result) {
502                         err("%s - failed submitting write urb, error %d", __FUNCTION__, result);
503                 }
504         } else {
505                 priv->active = 0;
506                 spin_unlock_irqrestore(&write_list_lock, flags);
507         }
508         queue_task(&port->tqueue, &tq_immediate);
509         mark_bh(IMMEDIATE_BH);
510         
511         return;
512 }
513
514 static int ipaq_write_room(struct usb_serial_port *port)
515 {
516         struct ipaq_private     *priv = (struct ipaq_private *)port->private;
517
518         dbg("%s - freelen %d", __FUNCTION__, priv->free_len);
519         return priv->free_len;
520 }
521
522 static int ipaq_chars_in_buffer(struct usb_serial_port *port)
523 {
524         struct ipaq_private     *priv = (struct ipaq_private *)port->private;
525
526         dbg("%s - queuelen %d", __FUNCTION__, priv->queue_len);
527         return priv->queue_len;
528 }
529
530 static void ipaq_destroy_lists(struct usb_serial_port *port)
531 {
532         struct ipaq_private     *priv = (struct ipaq_private *)port->private;
533         struct list_head        *tmp;
534         struct ipaq_packet      *pkt;
535
536         for (tmp = priv->queue.next; tmp != &priv->queue;) {
537                 pkt = list_entry(tmp, struct ipaq_packet, list);
538                 tmp = tmp->next;
539                 kfree(pkt->data);
540                 kfree(pkt);
541         }
542         for (tmp = priv->freelist.next; tmp != &priv->freelist;) {
543                 pkt = list_entry(tmp, struct ipaq_packet, list);
544                 tmp = tmp->next;
545                 kfree(pkt->data);
546                 kfree(pkt);
547         }
548         return;
549 }
550
551
552 static int ipaq_startup(struct usb_serial *serial)
553 {
554         dbg("%s", __FUNCTION__);
555         usb_set_configuration(serial->dev, 1);
556         return 0;
557 }
558
559 static void ipaq_shutdown(struct usb_serial *serial)
560 {
561         dbg("%s", __FUNCTION__);
562 }
563
564 static int __init ipaq_init(void)
565 {
566         spin_lock_init(&write_list_lock);
567         info(DRIVER_DESC " " DRIVER_VERSION);
568         if (vendor) {
569                 ipaq_id_table[0].idVendor = vendor;
570                 ipaq_id_table[0].idProduct = product;
571         }
572         usb_serial_register(&ipaq_device);
573
574         return 0;
575 }
576
577
578 static void __exit ipaq_exit(void)
579 {
580         usb_serial_deregister(&ipaq_device);
581 }
582
583
584 module_init(ipaq_init);
585 module_exit(ipaq_exit);
586
587 MODULE_AUTHOR( DRIVER_AUTHOR );
588 MODULE_DESCRIPTION( DRIVER_DESC );
589 MODULE_LICENSE("GPL");
590
591 MODULE_PARM(debug, "i");
592 MODULE_PARM_DESC(debug, "Debug enabled or not");
593
594 MODULE_PARM(vendor, "h");
595 MODULE_PARM_DESC(vendor, "User specified USB idVendor");
596
597 MODULE_PARM(product, "h");
598 MODULE_PARM_DESC(product, "User specified USB idProduct");