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>
59 #include "usb-serial.h"
62 #define KP_RETRIES 100
68 #define DRIVER_VERSION "v0.5"
69 #define DRIVER_AUTHOR "Ganesh Varadarajan <ganesh@veritas.com>"
70 #define DRIVER_DESC "USB PocketPC PDA driver"
72 static __u16 product, vendor;
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,
82 static int ipaq_write_bulk(struct usb_serial_port *port, int from_user, const unsigned char *buf,
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);
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 */
136 MODULE_DEVICE_TABLE (usb, ipaq_id_table);
138 static struct usb_driver ipaq_driver = {
139 .owner = THIS_MODULE,
141 .probe = usb_serial_probe,
142 .disconnect = usb_serial_disconnect,
143 .id_table = ipaq_id_table,
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,
158 .attach = ipaq_startup,
159 .shutdown = ipaq_shutdown,
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,
167 static spinlock_t write_list_lock;
169 static int bytes_out;
171 static int ipaq_open(struct usb_serial_port *port, struct file *filp)
173 struct usb_serial *serial = port->serial;
174 struct ipaq_private *priv;
175 struct ipaq_packet *pkt;
177 int retries = KP_RETRIES;
179 dbg("%s - port %d", __FUNCTION__, port->number);
183 priv = (struct ipaq_private *)kmalloc(sizeof(struct ipaq_private), GFP_KERNEL);
185 err("%s - Out of memory", __FUNCTION__);
188 usb_set_serial_port_data(port, priv);
191 INIT_LIST_HEAD(&priv->queue);
192 INIT_LIST_HEAD(&priv->freelist);
194 for (i = 0; i < URBDATA_QUEUE_MAX / PACKET_SIZE; i++) {
195 pkt = kmalloc(sizeof(struct ipaq_packet), GFP_KERNEL);
199 pkt->data = kmalloc(PACKET_SIZE, GFP_KERNEL);
200 if (pkt->data == NULL) {
206 INIT_LIST_HEAD(&pkt->list);
207 list_add(&pkt->list, &priv->freelist);
208 priv->free_len += PACKET_SIZE;
212 * Force low latency on. This will immediately push data to the line
213 * discipline instead of queueing.
216 port->tty->low_latency = 1;
218 port->tty->real_raw = 1;
221 * Lose the small buffers usbserial provides. Make larger ones.
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) {
230 port->bulk_out_buffer = kmalloc(URBDATA_SIZE, GFP_KERNEL);
231 if (port->bulk_out_buffer == NULL) {
232 kfree(port->bulk_in_buffer);
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;
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);
247 err("%s - failed submitting read urb, error %d", __FUNCTION__, result);
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
260 result = usb_control_msg(serial->dev,
261 usb_sndctrlpipe(serial->dev, 0), 0x22, 0x21,
262 0x1, 0, NULL, 0, HZ / 10 + 1);
267 err("%s - failed doing control urb, error %d", __FUNCTION__, result);
272 err("%s - Out of memory", __FUNCTION__);
274 ipaq_destroy_lists(port);
280 static void ipaq_close(struct usb_serial_port *port, struct file *filp)
282 struct ipaq_private *priv = usb_get_serial_port_data(port);
284 dbg("%s - port %d", __FUNCTION__, port->number);
287 * shut down bulk read and write
289 usb_unlink_urb(port->write_urb);
290 usb_unlink_urb(port->read_urb);
291 ipaq_destroy_lists(port);
293 usb_set_serial_port_data(port, NULL);
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); */
299 static void ipaq_read_bulk_callback(struct urb *urb, struct pt_regs *regs)
301 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
302 struct tty_struct *tty;
303 unsigned char *data = urb->transfer_buffer;
306 dbg("%s - port %d", __FUNCTION__, port->number);
309 dbg("%s - nonzero read bulk status received: %d", __FUNCTION__, urb->status);
313 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, urb->actual_length, data);
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);
322 /* this doesn't actually push the data through unless tty->low_latency is set */
323 tty_insert_flip_char(tty, data[i], 0);
325 tty_flip_buffer_push(tty);
326 bytes_in += urb->actual_length;
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);
336 err("%s - failed resubmitting read urb, error %d", __FUNCTION__, result);
340 static int ipaq_write(struct usb_serial_port *port, int from_user, const unsigned char *buf,
343 const unsigned char *current_position = buf;
347 dbg("%s - port %d", __FUNCTION__, port->number);
350 transfer_size = min(count, PACKET_SIZE);
351 if (ipaq_write_bulk(port, from_user, current_position, transfer_size)) {
354 current_position += transfer_size;
355 bytes_sent += transfer_size;
356 count -= transfer_size;
357 bytes_out += transfer_size;
363 static int ipaq_write_bulk(struct usb_serial_port *port, int from_user, const unsigned char *buf,
366 struct ipaq_private *priv = usb_get_serial_port_data(port);
367 struct ipaq_packet *pkt = NULL;
371 if (priv->free_len <= 0) {
372 dbg("%s - we're stuffed", __FUNCTION__);
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;
382 spin_unlock_irqrestore(&write_list_lock, flags);
384 dbg("%s - we're stuffed", __FUNCTION__);
389 if (copy_from_user(pkt->data, buf, count))
392 memcpy(pkt->data, buf, count);
394 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, count, pkt->data);
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) {
403 ipaq_write_gather(port);
404 spin_unlock_irqrestore(&write_list_lock, flags);
405 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
407 err("%s - failed submitting write urb, error %d", __FUNCTION__, result);
410 spin_unlock_irqrestore(&write_list_lock, flags);
415 static void ipaq_write_gather(struct usb_serial_port *port)
417 struct ipaq_private *priv = usb_get_serial_port_data(port);
418 struct usb_serial *serial = port->serial;
420 struct ipaq_packet *pkt;
421 struct urb *urb = port->write_urb;
422 struct list_head *tmp;
424 if (urb->status == -EINPROGRESS) {
425 /* Should never happen */
426 err("%s - flushing while urb is active !", __FUNCTION__);
430 for (tmp = priv->queue.next; tmp != &priv->queue;) {
431 pkt = list_entry(tmp, struct ipaq_packet, list);
433 count = min(room, (int)(pkt->len - pkt->written));
434 memcpy(urb->transfer_buffer + (URBDATA_SIZE - room),
435 pkt->data + pkt->written, 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;
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,
456 static void ipaq_write_bulk_callback(struct urb *urb, struct pt_regs *regs)
458 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
459 struct ipaq_private *priv = usb_get_serial_port_data(port);
463 dbg("%s - port %d", __FUNCTION__, port->number);
466 dbg("%s - nonzero write bulk status received: %d", __FUNCTION__, urb->status);
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);
475 err("%s - failed submitting write urb, error %d", __FUNCTION__, result);
479 spin_unlock_irqrestore(&write_list_lock, flags);
482 schedule_work(&port->work);
485 static int ipaq_write_room(struct usb_serial_port *port)
487 struct ipaq_private *priv = usb_get_serial_port_data(port);
489 dbg("%s - freelen %d", __FUNCTION__, priv->free_len);
490 return priv->free_len;
493 static int ipaq_chars_in_buffer(struct usb_serial_port *port)
495 struct ipaq_private *priv = usb_get_serial_port_data(port);
497 dbg("%s - queuelen %d", __FUNCTION__, priv->queue_len);
498 return priv->queue_len;
501 static void ipaq_destroy_lists(struct usb_serial_port *port)
503 struct ipaq_private *priv = usb_get_serial_port_data(port);
504 struct list_head *tmp;
505 struct ipaq_packet *pkt;
507 for (tmp = priv->queue.next; tmp != &priv->queue;) {
508 pkt = list_entry(tmp, struct ipaq_packet, list);
513 for (tmp = priv->freelist.next; tmp != &priv->freelist;) {
514 pkt = list_entry(tmp, struct ipaq_packet, list);
523 static int ipaq_startup(struct usb_serial *serial)
525 dbg("%s", __FUNCTION__);
526 if (serial->dev->actconfig->desc.bConfigurationValue != 1) {
527 err("active config #%d != 1 ??",
528 serial->dev->actconfig->desc.bConfigurationValue);
531 return usb_reset_configuration (serial->dev);
534 static void ipaq_shutdown(struct usb_serial *serial)
536 dbg("%s", __FUNCTION__);
539 static int __init ipaq_init(void)
542 spin_lock_init(&write_list_lock);
543 retval = usb_serial_register(&ipaq_device);
545 goto failed_usb_serial_register;
546 info(DRIVER_DESC " " DRIVER_VERSION);
548 ipaq_id_table[0].idVendor = vendor;
549 ipaq_id_table[0].idProduct = product;
551 retval = usb_register(&ipaq_driver);
553 goto failed_usb_register;
557 usb_serial_deregister(&ipaq_device);
558 failed_usb_serial_register:
563 static void __exit ipaq_exit(void)
565 usb_deregister(&ipaq_driver);
566 usb_serial_deregister(&ipaq_device);
570 module_init(ipaq_init);
571 module_exit(ipaq_exit);
573 MODULE_AUTHOR( DRIVER_AUTHOR );
574 MODULE_DESCRIPTION( DRIVER_DESC );
575 MODULE_LICENSE("GPL");
577 module_param(debug, bool, S_IRUGO | S_IWUSR);
578 MODULE_PARM_DESC(debug, "Debug enabled or not");
580 module_param(vendor, ushort, 0);
581 MODULE_PARM_DESC(vendor, "User specified USB idVendor");
583 module_param(product, ushort, 0);
584 MODULE_PARM_DESC(product, "User specified USB idProduct");