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(HTC_VENDOR_ID, HTC_HIMALAYA_ID) },
131 { USB_DEVICE(NEC_VENDOR_ID, NEC_PRODUCT_ID) },
132 { USB_DEVICE(ASUS_VENDOR_ID, ASUS_A600_PRODUCT_ID) },
133 { USB_DEVICE(ASUS_VENDOR_ID, ASUS_A620_PRODUCT_ID) },
134 { } /* Terminating entry */
137 MODULE_DEVICE_TABLE (usb, ipaq_id_table);
139 static struct usb_driver ipaq_driver = {
140 .owner = THIS_MODULE,
142 .probe = usb_serial_probe,
143 .disconnect = usb_serial_disconnect,
144 .id_table = ipaq_id_table,
148 /* All of the device info needed for the Compaq iPAQ */
149 static struct usb_serial_device_type ipaq_device = {
150 .owner = THIS_MODULE,
151 .name = "PocketPC PDA",
152 .id_table = ipaq_id_table,
153 .num_interrupt_in = NUM_DONT_CARE,
159 .attach = ipaq_startup,
160 .shutdown = ipaq_shutdown,
162 .write_room = ipaq_write_room,
163 .chars_in_buffer = ipaq_chars_in_buffer,
164 .read_bulk_callback = ipaq_read_bulk_callback,
165 .write_bulk_callback = ipaq_write_bulk_callback,
168 static spinlock_t write_list_lock;
170 static int bytes_out;
172 static int ipaq_open(struct usb_serial_port *port, struct file *filp)
174 struct usb_serial *serial = port->serial;
175 struct ipaq_private *priv;
176 struct ipaq_packet *pkt;
178 int retries = KP_RETRIES;
180 dbg("%s - port %d", __FUNCTION__, port->number);
184 priv = (struct ipaq_private *)kmalloc(sizeof(struct ipaq_private), GFP_KERNEL);
186 err("%s - Out of memory", __FUNCTION__);
189 usb_set_serial_port_data(port, priv);
193 INIT_LIST_HEAD(&priv->queue);
194 INIT_LIST_HEAD(&priv->freelist);
196 for (i = 0; i < URBDATA_QUEUE_MAX / PACKET_SIZE; i++) {
197 pkt = kmalloc(sizeof(struct ipaq_packet), GFP_KERNEL);
201 pkt->data = kmalloc(PACKET_SIZE, GFP_KERNEL);
202 if (pkt->data == NULL) {
208 INIT_LIST_HEAD(&pkt->list);
209 list_add(&pkt->list, &priv->freelist);
210 priv->free_len += PACKET_SIZE;
214 * Force low latency on. This will immediately push data to the line
215 * discipline instead of queueing.
218 port->tty->low_latency = 1;
220 port->tty->real_raw = 1;
223 * Lose the small buffers usbserial provides. Make larger ones.
226 kfree(port->bulk_in_buffer);
227 kfree(port->bulk_out_buffer);
228 port->bulk_in_buffer = kmalloc(URBDATA_SIZE, GFP_KERNEL);
229 if (port->bulk_in_buffer == NULL) {
232 port->bulk_out_buffer = kmalloc(URBDATA_SIZE, GFP_KERNEL);
233 if (port->bulk_out_buffer == NULL) {
234 kfree(port->bulk_in_buffer);
237 port->read_urb->transfer_buffer = port->bulk_in_buffer;
238 port->write_urb->transfer_buffer = port->bulk_out_buffer;
239 port->read_urb->transfer_buffer_length = URBDATA_SIZE;
240 port->bulk_out_size = port->write_urb->transfer_buffer_length = URBDATA_SIZE;
242 /* Start reading from the device */
243 usb_fill_bulk_urb(port->read_urb, serial->dev,
244 usb_rcvbulkpipe(serial->dev, port->bulk_in_endpointAddress),
245 port->read_urb->transfer_buffer, port->read_urb->transfer_buffer_length,
246 ipaq_read_bulk_callback, port);
247 result = usb_submit_urb(port->read_urb, GFP_KERNEL);
249 err("%s - failed submitting read urb, error %d", __FUNCTION__, result);
254 * Send out control message observed in win98 sniffs. Not sure what
255 * it does, but from empirical observations, it seems that the device
256 * will start the chat sequence once one of these messages gets
257 * through. Since this has a reasonably high failure rate, we retry
262 result = usb_control_msg(serial->dev,
263 usb_sndctrlpipe(serial->dev, 0), 0x22, 0x21,
264 0x1, 0, NULL, 0, HZ / 10 + 1);
269 err("%s - failed doing control urb, error %d", __FUNCTION__, result);
274 err("%s - Out of memory", __FUNCTION__);
276 ipaq_destroy_lists(port);
282 static void ipaq_close(struct usb_serial_port *port, struct file *filp)
284 struct ipaq_private *priv = usb_get_serial_port_data(port);
286 dbg("%s - port %d", __FUNCTION__, port->number);
289 * shut down bulk read and write
291 usb_unlink_urb(port->write_urb);
292 usb_unlink_urb(port->read_urb);
293 ipaq_destroy_lists(port);
295 usb_set_serial_port_data(port, NULL);
297 /* Uncomment the following line if you want to see some statistics in your syslog */
298 /* info ("Bytes In = %d Bytes Out = %d", bytes_in, bytes_out); */
301 static void ipaq_read_bulk_callback(struct urb *urb, struct pt_regs *regs)
303 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
304 struct tty_struct *tty;
305 unsigned char *data = urb->transfer_buffer;
308 dbg("%s - port %d", __FUNCTION__, port->number);
311 dbg("%s - nonzero read bulk status received: %d", __FUNCTION__, urb->status);
315 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, urb->actual_length, data);
318 if (tty && urb->actual_length) {
319 for (i = 0; i < urb->actual_length ; ++i) {
320 /* if we insert more than TTY_FLIPBUF_SIZE characters, we drop them. */
321 if(tty->flip.count >= TTY_FLIPBUF_SIZE) {
322 tty_flip_buffer_push(tty);
324 /* this doesn't actually push the data through unless tty->low_latency is set */
325 tty_insert_flip_char(tty, data[i], 0);
327 tty_flip_buffer_push(tty);
328 bytes_in += urb->actual_length;
331 /* Continue trying to always read */
332 usb_fill_bulk_urb(port->read_urb, port->serial->dev,
333 usb_rcvbulkpipe(port->serial->dev, port->bulk_in_endpointAddress),
334 port->read_urb->transfer_buffer, port->read_urb->transfer_buffer_length,
335 ipaq_read_bulk_callback, port);
336 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
338 err("%s - failed resubmitting read urb, error %d", __FUNCTION__, result);
342 static int ipaq_write(struct usb_serial_port *port, int from_user, const unsigned char *buf,
345 const unsigned char *current_position = buf;
349 dbg("%s - port %d", __FUNCTION__, port->number);
352 transfer_size = min(count, PACKET_SIZE);
353 if (ipaq_write_bulk(port, from_user, current_position, transfer_size)) {
356 current_position += transfer_size;
357 bytes_sent += transfer_size;
358 count -= transfer_size;
359 bytes_out += transfer_size;
365 static int ipaq_write_bulk(struct usb_serial_port *port, int from_user, const unsigned char *buf,
368 struct ipaq_private *priv = usb_get_serial_port_data(port);
369 struct ipaq_packet *pkt = NULL;
373 if (priv->free_len <= 0) {
374 dbg("%s - we're stuffed", __FUNCTION__);
378 spin_lock_irqsave(&write_list_lock, flags);
379 if (!list_empty(&priv->freelist)) {
380 pkt = list_entry(priv->freelist.next, struct ipaq_packet, list);
381 list_del(&pkt->list);
382 priv->free_len -= PACKET_SIZE;
384 spin_unlock_irqrestore(&write_list_lock, flags);
386 dbg("%s - we're stuffed", __FUNCTION__);
391 if (copy_from_user(pkt->data, buf, count))
394 memcpy(pkt->data, buf, count);
396 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, count, pkt->data);
400 spin_lock_irqsave(&write_list_lock, flags);
401 list_add_tail(&pkt->list, &priv->queue);
402 priv->queue_len += count;
403 if (priv->active == 0) {
405 ipaq_write_gather(port);
406 spin_unlock_irqrestore(&write_list_lock, flags);
407 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
409 err("%s - failed submitting write urb, error %d", __FUNCTION__, result);
412 spin_unlock_irqrestore(&write_list_lock, flags);
417 static void ipaq_write_gather(struct usb_serial_port *port)
419 struct ipaq_private *priv = usb_get_serial_port_data(port);
420 struct usb_serial *serial = port->serial;
422 struct ipaq_packet *pkt;
423 struct urb *urb = port->write_urb;
424 struct list_head *tmp;
426 if (urb->status == -EINPROGRESS) {
427 /* Should never happen */
428 err("%s - flushing while urb is active !", __FUNCTION__);
432 for (tmp = priv->queue.next; tmp != &priv->queue;) {
433 pkt = list_entry(tmp, struct ipaq_packet, list);
435 count = min(room, (int)(pkt->len - pkt->written));
436 memcpy(urb->transfer_buffer + (URBDATA_SIZE - room),
437 pkt->data + pkt->written, count);
439 pkt->written += count;
440 priv->queue_len -= count;
441 if (pkt->written == pkt->len) {
442 list_move(&pkt->list, &priv->freelist);
443 priv->free_len += PACKET_SIZE;
450 count = URBDATA_SIZE - room;
451 usb_fill_bulk_urb(port->write_urb, serial->dev,
452 usb_sndbulkpipe(serial->dev, port->bulk_out_endpointAddress),
453 port->write_urb->transfer_buffer, count, ipaq_write_bulk_callback,
458 static void ipaq_write_bulk_callback(struct urb *urb, struct pt_regs *regs)
460 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
461 struct ipaq_private *priv = usb_get_serial_port_data(port);
465 dbg("%s - port %d", __FUNCTION__, port->number);
468 dbg("%s - nonzero write bulk status received: %d", __FUNCTION__, urb->status);
471 spin_lock_irqsave(&write_list_lock, flags);
472 if (!list_empty(&priv->queue)) {
473 ipaq_write_gather(port);
474 spin_unlock_irqrestore(&write_list_lock, flags);
475 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
477 err("%s - failed submitting write urb, error %d", __FUNCTION__, result);
481 spin_unlock_irqrestore(&write_list_lock, flags);
484 schedule_work(&port->work);
487 static int ipaq_write_room(struct usb_serial_port *port)
489 struct ipaq_private *priv = usb_get_serial_port_data(port);
491 dbg("%s - freelen %d", __FUNCTION__, priv->free_len);
492 return priv->free_len;
495 static int ipaq_chars_in_buffer(struct usb_serial_port *port)
497 struct ipaq_private *priv = usb_get_serial_port_data(port);
499 dbg("%s - queuelen %d", __FUNCTION__, priv->queue_len);
500 return priv->queue_len;
503 static void ipaq_destroy_lists(struct usb_serial_port *port)
505 struct ipaq_private *priv = usb_get_serial_port_data(port);
506 struct list_head *tmp;
507 struct ipaq_packet *pkt;
509 for (tmp = priv->queue.next; tmp != &priv->queue;) {
510 pkt = list_entry(tmp, struct ipaq_packet, list);
515 for (tmp = priv->freelist.next; tmp != &priv->freelist;) {
516 pkt = list_entry(tmp, struct ipaq_packet, list);
525 static int ipaq_startup(struct usb_serial *serial)
527 dbg("%s", __FUNCTION__);
528 if (serial->dev->actconfig->desc.bConfigurationValue != 1) {
529 err("active config #%d != 1 ??",
530 serial->dev->actconfig->desc.bConfigurationValue);
533 return usb_reset_configuration (serial->dev);
536 static void ipaq_shutdown(struct usb_serial *serial)
538 dbg("%s", __FUNCTION__);
541 static int __init ipaq_init(void)
544 spin_lock_init(&write_list_lock);
545 retval = usb_serial_register(&ipaq_device);
547 goto failed_usb_serial_register;
548 info(DRIVER_DESC " " DRIVER_VERSION);
550 ipaq_id_table[0].idVendor = vendor;
551 ipaq_id_table[0].idProduct = product;
553 retval = usb_register(&ipaq_driver);
555 goto failed_usb_register;
559 usb_serial_deregister(&ipaq_device);
560 failed_usb_serial_register:
565 static void __exit ipaq_exit(void)
567 usb_deregister(&ipaq_driver);
568 usb_serial_deregister(&ipaq_device);
572 module_init(ipaq_init);
573 module_exit(ipaq_exit);
575 MODULE_AUTHOR( DRIVER_AUTHOR );
576 MODULE_DESCRIPTION( DRIVER_DESC );
577 MODULE_LICENSE("GPL");
579 module_param(debug, bool, S_IRUGO | S_IWUSR);
580 MODULE_PARM_DESC(debug, "Debug enabled or not");
582 module_param(vendor, ushort, 0);
583 MODULE_PARM_DESC(vendor, "User specified USB idVendor");
585 module_param(product, ushort, 0);
586 MODULE_PARM_DESC(product, "User specified USB idProduct");