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 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,
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 dbg("%s - port %d", __FUNCTION__, port->number);
188 priv = (struct ipaq_private *)kmalloc(sizeof(struct ipaq_private), GFP_KERNEL);
190 err("%s - Out of memory", __FUNCTION__);
193 usb_set_serial_port_data(port, priv);
196 INIT_LIST_HEAD(&priv->queue);
197 INIT_LIST_HEAD(&priv->freelist);
199 for (i = 0; i < URBDATA_QUEUE_MAX / PACKET_SIZE; i++) {
200 pkt = kmalloc(sizeof(struct ipaq_packet), GFP_KERNEL);
204 pkt->data = kmalloc(PACKET_SIZE, GFP_KERNEL);
205 if (pkt->data == NULL) {
211 INIT_LIST_HEAD(&pkt->list);
212 list_add(&pkt->list, &priv->freelist);
213 priv->free_len += PACKET_SIZE;
217 * Force low latency on. This will immediately push data to the line
218 * discipline instead of queueing.
221 port->tty->low_latency = 1;
223 port->tty->real_raw = 1;
226 * Lose the small buffers usbserial provides. Make larger ones.
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) {
235 port->bulk_out_buffer = kmalloc(URBDATA_SIZE, GFP_KERNEL);
236 if (port->bulk_out_buffer == NULL) {
237 kfree(port->bulk_in_buffer);
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;
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);
252 err("%s - failed submitting read urb, error %d", __FUNCTION__, result);
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
265 result = usb_control_msg(serial->dev,
266 usb_sndctrlpipe(serial->dev, 0), 0x22, 0x21,
267 0x1, 0, NULL, 0, HZ / 10 + 1);
272 err("%s - failed doing control urb, error %d", __FUNCTION__, result);
277 err("%s - Out of memory", __FUNCTION__);
279 ipaq_destroy_lists(port);
285 static void ipaq_close(struct usb_serial_port *port, struct file *filp)
287 struct ipaq_private *priv = usb_get_serial_port_data(port);
289 dbg("%s - port %d", __FUNCTION__, port->number);
292 * shut down bulk read and write
294 usb_unlink_urb(port->write_urb);
295 usb_unlink_urb(port->read_urb);
296 ipaq_destroy_lists(port);
298 usb_set_serial_port_data(port, NULL);
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); */
304 static void ipaq_read_bulk_callback(struct urb *urb, struct pt_regs *regs)
306 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
307 struct tty_struct *tty;
308 unsigned char *data = urb->transfer_buffer;
311 dbg("%s - port %d", __FUNCTION__, port->number);
314 dbg("%s - nonzero read bulk status received: %d", __FUNCTION__, urb->status);
318 usb_serial_debug_data (__FILE__, __FUNCTION__, urb->actual_length, data);
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);
327 /* this doesn't actually push the data through unless tty->low_latency is set */
328 tty_insert_flip_char(tty, data[i], 0);
330 tty_flip_buffer_push(tty);
331 bytes_in += urb->actual_length;
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);
341 err("%s - failed resubmitting read urb, error %d", __FUNCTION__, result);
345 static int ipaq_write(struct usb_serial_port *port, int from_user, const unsigned char *buf,
348 const unsigned char *current_position = buf;
352 dbg("%s - port %d", __FUNCTION__, port->number);
355 transfer_size = min(count, PACKET_SIZE);
356 if (ipaq_write_bulk(port, from_user, current_position, transfer_size)) {
359 current_position += transfer_size;
360 bytes_sent += transfer_size;
361 count -= transfer_size;
362 bytes_out += transfer_size;
368 static int ipaq_write_bulk(struct usb_serial_port *port, int from_user, const unsigned char *buf,
371 struct ipaq_private *priv = usb_get_serial_port_data(port);
372 struct ipaq_packet *pkt = NULL;
376 if (priv->free_len <= 0) {
377 dbg("%s - we're stuffed", __FUNCTION__);
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;
387 spin_unlock_irqrestore(&write_list_lock, flags);
389 dbg("%s - we're stuffed", __FUNCTION__);
394 if (copy_from_user(pkt->data, buf, count))
397 memcpy(pkt->data, buf, count);
399 usb_serial_debug_data(__FILE__, __FUNCTION__, count, pkt->data);
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) {
408 ipaq_write_gather(port);
409 spin_unlock_irqrestore(&write_list_lock, flags);
410 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
412 err("%s - failed submitting write urb, error %d", __FUNCTION__, result);
415 spin_unlock_irqrestore(&write_list_lock, flags);
420 static void ipaq_write_gather(struct usb_serial_port *port)
422 struct ipaq_private *priv = usb_get_serial_port_data(port);
423 struct usb_serial *serial = port->serial;
425 struct ipaq_packet *pkt;
426 struct urb *urb = port->write_urb;
427 struct list_head *tmp;
429 if (urb->status == -EINPROGRESS) {
430 /* Should never happen */
431 err("%s - flushing while urb is active !", __FUNCTION__);
435 for (tmp = priv->queue.next; tmp != &priv->queue;) {
436 pkt = list_entry(tmp, struct ipaq_packet, list);
438 count = min(room, (int)(pkt->len - pkt->written));
439 memcpy(urb->transfer_buffer + (URBDATA_SIZE - room),
440 pkt->data + pkt->written, 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;
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,
461 static void ipaq_write_bulk_callback(struct urb *urb, struct pt_regs *regs)
463 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
464 struct ipaq_private *priv = usb_get_serial_port_data(port);
468 dbg("%s - port %d", __FUNCTION__, port->number);
471 dbg("%s - nonzero write bulk status received: %d", __FUNCTION__, urb->status);
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);
480 err("%s - failed submitting write urb, error %d", __FUNCTION__, result);
484 spin_unlock_irqrestore(&write_list_lock, flags);
487 schedule_work(&port->work);
490 static int ipaq_write_room(struct usb_serial_port *port)
492 struct ipaq_private *priv = usb_get_serial_port_data(port);
494 dbg("%s - freelen %d", __FUNCTION__, priv->free_len);
495 return priv->free_len;
498 static int ipaq_chars_in_buffer(struct usb_serial_port *port)
500 struct ipaq_private *priv = usb_get_serial_port_data(port);
502 dbg("%s - queuelen %d", __FUNCTION__, priv->queue_len);
503 return priv->queue_len;
506 static void ipaq_destroy_lists(struct usb_serial_port *port)
508 struct ipaq_private *priv = usb_get_serial_port_data(port);
509 struct list_head *tmp;
510 struct ipaq_packet *pkt;
512 for (tmp = priv->queue.next; tmp != &priv->queue;) {
513 pkt = list_entry(tmp, struct ipaq_packet, list);
518 for (tmp = priv->freelist.next; tmp != &priv->freelist;) {
519 pkt = list_entry(tmp, struct ipaq_packet, list);
528 static int ipaq_startup(struct usb_serial *serial)
530 dbg("%s", __FUNCTION__);
531 if (serial->dev->actconfig->desc.bConfigurationValue != 1) {
532 err("active config #%d != 1 ??",
533 serial->dev->actconfig->desc.bConfigurationValue);
536 return usb_reset_configuration (serial->dev);
539 static void ipaq_shutdown(struct usb_serial *serial)
541 dbg("%s", __FUNCTION__);
544 static int __init ipaq_init(void)
547 spin_lock_init(&write_list_lock);
548 retval = usb_serial_register(&ipaq_device);
550 goto failed_usb_serial_register;
551 info(DRIVER_DESC " " DRIVER_VERSION);
553 ipaq_id_table[0].idVendor = vendor;
554 ipaq_id_table[0].idProduct = product;
556 retval = usb_register(&ipaq_driver);
558 goto failed_usb_register;
562 usb_serial_deregister(&ipaq_device);
563 failed_usb_serial_register:
568 static void __exit ipaq_exit(void)
570 usb_deregister(&ipaq_driver);
571 usb_serial_deregister(&ipaq_device);
575 module_init(ipaq_init);
576 module_exit(ipaq_exit);
578 MODULE_AUTHOR( DRIVER_AUTHOR );
579 MODULE_DESCRIPTION( DRIVER_DESC );
580 MODULE_LICENSE("GPL");
582 MODULE_PARM(debug, "i");
583 MODULE_PARM_DESC(debug, "Debug enabled or not");
585 MODULE_PARM(vendor, "h");
586 MODULE_PARM_DESC(vendor, "User specified USB idVendor");
588 MODULE_PARM(product, "h");
589 MODULE_PARM_DESC(product, "User specified USB idProduct");