2 * USB Compaq iPAQ driver
4 * Copyright (C) 2001 - 2002
5 * Ganesh Varadarajan <ganesh@veritas.com>
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.
13 * Added support for practically all devices supported by ActiveSync
14 * on Windows. Thanks to Wes Cilldhaire <billybobjoehenrybob@hotmail.com>.
17 * Added insmod options to specify product and vendor id.
18 * Use modprobe ipaq vendor=0xfoo product=0xbar
21 * Fixed up broken error handling in ipaq_open. Retry the "kickstart"
22 * packet much harder - this drastically reduces connection failures.
25 * Added support for the Casio EM500. Completely untested. Thanks
26 * to info from Nathan <wfilardo@fuse.net>
29 * Don't submit urbs while holding spinlocks. Not strictly necessary
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
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
43 * Added support for the HP Jornada 548 and 568. Completely untested.
44 * Thanks to info from Heath Robinson and Arieh Davidoff.
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>
60 #ifdef CONFIG_USB_SERIAL_DEBUG
66 #include "usb-serial.h"
69 #define KP_RETRIES 100
75 #define DRIVER_VERSION "v0.5"
76 #define DRIVER_AUTHOR "Ganesh Varadarajan <ganesh@veritas.com>"
77 #define DRIVER_DESC "USB PocketPC PDA driver"
79 static int product, vendor;
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,
88 static int ipaq_write_bulk(struct usb_serial_port *port, int from_user, const unsigned char *buf,
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);
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 */
141 MODULE_DEVICE_TABLE (usb, ipaq_id_table);
143 static struct usb_driver ipaq_driver = {
144 .owner = THIS_MODULE,
146 .probe = usb_serial_probe,
147 .disconnect = usb_serial_disconnect,
148 .id_table = ipaq_id_table,
152 /* All of the device info needed for the Compaq iPAQ */
153 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,
163 .attach = ipaq_startup,
164 .shutdown = ipaq_shutdown,
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,
172 static spinlock_t write_list_lock;
174 static int bytes_out;
176 static int ipaq_open(struct usb_serial_port *port, struct file *filp)
178 struct usb_serial *serial = port->serial;
179 struct ipaq_private *priv;
180 struct ipaq_packet *pkt;
182 int retries = KP_RETRIES;
184 if (port_paranoia_check(port, __FUNCTION__)) {
188 dbg("%s - port %d", __FUNCTION__, port->number);
192 priv = (struct ipaq_private *)kmalloc(sizeof(struct ipaq_private), GFP_KERNEL);
194 err("%s - Out of memory", __FUNCTION__);
197 usb_set_serial_port_data(port, priv);
200 INIT_LIST_HEAD(&priv->queue);
201 INIT_LIST_HEAD(&priv->freelist);
203 for (i = 0; i < URBDATA_QUEUE_MAX / PACKET_SIZE; i++) {
204 pkt = kmalloc(sizeof(struct ipaq_packet), GFP_KERNEL);
208 pkt->data = kmalloc(PACKET_SIZE, GFP_KERNEL);
209 if (pkt->data == NULL) {
215 INIT_LIST_HEAD(&pkt->list);
216 list_add(&pkt->list, &priv->freelist);
217 priv->free_len += PACKET_SIZE;
221 * Force low latency on. This will immediately push data to the line
222 * discipline instead of queueing.
225 port->tty->low_latency = 1;
227 port->tty->real_raw = 1;
230 * Lose the small buffers usbserial provides. Make larger ones.
233 kfree(port->bulk_in_buffer);
234 kfree(port->bulk_out_buffer);
235 port->bulk_in_buffer = kmalloc(URBDATA_SIZE, GFP_KERNEL);
236 if (port->bulk_in_buffer == NULL) {
239 port->bulk_out_buffer = kmalloc(URBDATA_SIZE, GFP_KERNEL);
240 if (port->bulk_out_buffer == NULL) {
241 kfree(port->bulk_in_buffer);
244 port->read_urb->transfer_buffer = port->bulk_in_buffer;
245 port->write_urb->transfer_buffer = port->bulk_out_buffer;
246 port->read_urb->transfer_buffer_length = URBDATA_SIZE;
247 port->bulk_out_size = port->write_urb->transfer_buffer_length = URBDATA_SIZE;
249 /* Start reading from the device */
250 usb_fill_bulk_urb(port->read_urb, serial->dev,
251 usb_rcvbulkpipe(serial->dev, port->bulk_in_endpointAddress),
252 port->read_urb->transfer_buffer, port->read_urb->transfer_buffer_length,
253 ipaq_read_bulk_callback, port);
254 result = usb_submit_urb(port->read_urb, GFP_KERNEL);
256 err("%s - failed submitting read urb, error %d", __FUNCTION__, result);
261 * Send out control message observed in win98 sniffs. Not sure what
262 * it does, but from empirical observations, it seems that the device
263 * will start the chat sequence once one of these messages gets
264 * through. Since this has a reasonably high failure rate, we retry
269 result = usb_control_msg(serial->dev,
270 usb_sndctrlpipe(serial->dev, 0), 0x22, 0x21,
271 0x1, 0, NULL, 0, HZ / 10 + 1);
276 err("%s - failed doing control urb, error %d", __FUNCTION__, result);
281 err("%s - Out of memory", __FUNCTION__);
283 ipaq_destroy_lists(port);
289 static void ipaq_close(struct usb_serial_port *port, struct file *filp)
291 struct usb_serial *serial;
292 struct ipaq_private *priv = usb_get_serial_port_data(port);
294 if (port_paranoia_check(port, __FUNCTION__)) {
298 dbg("%s - port %d", __FUNCTION__, port->number);
300 serial = get_usb_serial(port, __FUNCTION__);
305 * shut down bulk read and write
308 usb_unlink_urb(port->write_urb);
309 usb_unlink_urb(port->read_urb);
310 ipaq_destroy_lists(port);
312 usb_set_serial_port_data(port, NULL);
314 /* Uncomment the following line if you want to see some statistics in your syslog */
315 /* info ("Bytes In = %d Bytes Out = %d", bytes_in, bytes_out); */
318 static void ipaq_read_bulk_callback(struct urb *urb, struct pt_regs *regs)
320 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
321 struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);
322 struct tty_struct *tty;
323 unsigned char *data = urb->transfer_buffer;
326 if (port_paranoia_check(port, __FUNCTION__))
329 dbg("%s - port %d", __FUNCTION__, port->number);
332 dbg("%s - bad serial pointer, exiting", __FUNCTION__);
337 dbg("%s - nonzero read bulk status received: %d", __FUNCTION__, urb->status);
341 usb_serial_debug_data (__FILE__, __FUNCTION__, urb->actual_length, data);
344 if (tty && urb->actual_length) {
345 for (i = 0; i < urb->actual_length ; ++i) {
346 /* if we insert more than TTY_FLIPBUF_SIZE characters, we drop them. */
347 if(tty->flip.count >= TTY_FLIPBUF_SIZE) {
348 tty_flip_buffer_push(tty);
350 /* this doesn't actually push the data through unless tty->low_latency is set */
351 tty_insert_flip_char(tty, data[i], 0);
353 tty_flip_buffer_push(tty);
354 bytes_in += urb->actual_length;
357 /* Continue trying to always read */
358 usb_fill_bulk_urb(port->read_urb, serial->dev,
359 usb_rcvbulkpipe(serial->dev, port->bulk_in_endpointAddress),
360 port->read_urb->transfer_buffer, port->read_urb->transfer_buffer_length,
361 ipaq_read_bulk_callback, port);
362 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
364 err("%s - failed resubmitting read urb, error %d", __FUNCTION__, result);
368 static int ipaq_write(struct usb_serial_port *port, int from_user, const unsigned char *buf,
371 const unsigned char *current_position = buf;
375 dbg("%s - port %d", __FUNCTION__, port->number);
378 transfer_size = min(count, PACKET_SIZE);
379 if (ipaq_write_bulk(port, from_user, current_position, transfer_size)) {
382 current_position += transfer_size;
383 bytes_sent += transfer_size;
384 count -= transfer_size;
385 bytes_out += transfer_size;
391 static int ipaq_write_bulk(struct usb_serial_port *port, int from_user, const unsigned char *buf,
394 struct ipaq_private *priv = usb_get_serial_port_data(port);
395 struct ipaq_packet *pkt = NULL;
399 if (priv->free_len <= 0) {
400 dbg("%s - we're stuffed", __FUNCTION__);
404 spin_lock_irqsave(&write_list_lock, flags);
405 if (!list_empty(&priv->freelist)) {
406 pkt = list_entry(priv->freelist.next, struct ipaq_packet, list);
407 list_del(&pkt->list);
408 priv->free_len -= PACKET_SIZE;
410 spin_unlock_irqrestore(&write_list_lock, flags);
412 dbg("%s - we're stuffed", __FUNCTION__);
417 if (copy_from_user(pkt->data, buf, count))
420 memcpy(pkt->data, buf, count);
422 usb_serial_debug_data(__FILE__, __FUNCTION__, count, pkt->data);
426 spin_lock_irqsave(&write_list_lock, flags);
427 list_add_tail(&pkt->list, &priv->queue);
428 priv->queue_len += count;
429 if (priv->active == 0) {
431 ipaq_write_gather(port);
432 spin_unlock_irqrestore(&write_list_lock, flags);
433 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
435 err("%s - failed submitting write urb, error %d", __FUNCTION__, result);
438 spin_unlock_irqrestore(&write_list_lock, flags);
443 static void ipaq_write_gather(struct usb_serial_port *port)
445 struct ipaq_private *priv = usb_get_serial_port_data(port);
446 struct usb_serial *serial = port->serial;
448 struct ipaq_packet *pkt;
449 struct urb *urb = port->write_urb;
450 struct list_head *tmp;
452 if (urb->status == -EINPROGRESS) {
453 /* Should never happen */
454 err("%s - flushing while urb is active !", __FUNCTION__);
458 for (tmp = priv->queue.next; tmp != &priv->queue;) {
459 pkt = list_entry(tmp, struct ipaq_packet, list);
461 count = min(room, (int)(pkt->len - pkt->written));
462 memcpy(urb->transfer_buffer + (URBDATA_SIZE - room),
463 pkt->data + pkt->written, count);
465 pkt->written += count;
466 priv->queue_len -= count;
467 if (pkt->written == pkt->len) {
468 list_move(&pkt->list, &priv->freelist);
469 priv->free_len += PACKET_SIZE;
476 count = URBDATA_SIZE - room;
477 usb_fill_bulk_urb(port->write_urb, serial->dev,
478 usb_sndbulkpipe(serial->dev, port->bulk_out_endpointAddress),
479 port->write_urb->transfer_buffer, count, ipaq_write_bulk_callback,
484 static void ipaq_write_bulk_callback(struct urb *urb, struct pt_regs *regs)
486 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
487 struct ipaq_private *priv = usb_get_serial_port_data(port);
491 if (port_paranoia_check (port, __FUNCTION__)) {
495 dbg("%s - port %d", __FUNCTION__, port->number);
498 dbg("%s - nonzero write bulk status received: %d", __FUNCTION__, urb->status);
501 spin_lock_irqsave(&write_list_lock, flags);
502 if (!list_empty(&priv->queue)) {
503 ipaq_write_gather(port);
504 spin_unlock_irqrestore(&write_list_lock, flags);
505 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
507 err("%s - failed submitting write urb, error %d", __FUNCTION__, result);
511 spin_unlock_irqrestore(&write_list_lock, flags);
514 schedule_work(&port->work);
517 static int ipaq_write_room(struct usb_serial_port *port)
519 struct ipaq_private *priv = usb_get_serial_port_data(port);
521 dbg("%s - freelen %d", __FUNCTION__, priv->free_len);
522 return priv->free_len;
525 static int ipaq_chars_in_buffer(struct usb_serial_port *port)
527 struct ipaq_private *priv = usb_get_serial_port_data(port);
529 dbg("%s - queuelen %d", __FUNCTION__, priv->queue_len);
530 return priv->queue_len;
533 static void ipaq_destroy_lists(struct usb_serial_port *port)
535 struct ipaq_private *priv = usb_get_serial_port_data(port);
536 struct list_head *tmp;
537 struct ipaq_packet *pkt;
539 for (tmp = priv->queue.next; tmp != &priv->queue;) {
540 pkt = list_entry(tmp, struct ipaq_packet, list);
545 for (tmp = priv->freelist.next; tmp != &priv->freelist;) {
546 pkt = list_entry(tmp, struct ipaq_packet, list);
555 static int ipaq_startup(struct usb_serial *serial)
557 dbg("%s", __FUNCTION__);
558 if (serial->dev->actconfig->desc.bConfigurationValue != 1) {
559 err("active config #%d != 1 ??",
560 serial->dev->actconfig->desc.bConfigurationValue);
563 return usb_reset_configuration (serial->dev);
566 static void ipaq_shutdown(struct usb_serial *serial)
568 dbg("%s", __FUNCTION__);
571 static int __init ipaq_init(void)
574 spin_lock_init(&write_list_lock);
575 retval = usb_serial_register(&ipaq_device);
577 goto failed_usb_serial_register;
578 info(DRIVER_DESC " " DRIVER_VERSION);
580 ipaq_id_table[0].idVendor = vendor;
581 ipaq_id_table[0].idProduct = product;
583 retval = usb_register(&ipaq_driver);
585 goto failed_usb_register;
589 usb_serial_deregister(&ipaq_device);
590 failed_usb_serial_register:
595 static void __exit ipaq_exit(void)
597 usb_deregister(&ipaq_driver);
598 usb_serial_deregister(&ipaq_device);
602 module_init(ipaq_init);
603 module_exit(ipaq_exit);
605 MODULE_AUTHOR( DRIVER_AUTHOR );
606 MODULE_DESCRIPTION( DRIVER_DESC );
607 MODULE_LICENSE("GPL");
609 MODULE_PARM(debug, "i");
610 MODULE_PARM_DESC(debug, "Debug enabled or not");
612 MODULE_PARM(vendor, "h");
613 MODULE_PARM_DESC(vendor, "User specified USB idVendor");
615 MODULE_PARM(product, "h");
616 MODULE_PARM_DESC(product, "User specified USB idProduct");