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. Thanks to Greg for pointing
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);
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);
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 /* 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,
154 .startup = ipaq_startup,
155 .shutdown = ipaq_shutdown,
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,
163 static spinlock_t write_list_lock;
165 static int bytes_out;
167 static int ipaq_open(struct usb_serial_port *port, struct file *filp)
169 struct usb_serial *serial = port->serial;
170 struct ipaq_private *priv;
171 struct ipaq_packet *pkt;
173 int retries = KP_RETRIES;
175 if (port_paranoia_check(port, __FUNCTION__)) {
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 port->private = (void *)priv;
192 INIT_LIST_HEAD(&priv->queue);
193 INIT_LIST_HEAD(&priv->freelist);
195 for (i = 0; i < URBDATA_QUEUE_MAX / PACKET_SIZE; i++) {
196 pkt = kmalloc(sizeof(struct ipaq_packet), GFP_KERNEL);
200 pkt->data = kmalloc(PACKET_SIZE, GFP_KERNEL);
201 if (pkt->data == NULL) {
207 INIT_LIST_HEAD(&pkt->list);
208 list_add(&pkt->list, &priv->freelist);
209 priv->free_len += PACKET_SIZE;
213 * Force low latency on. This will immediately push data to the line
214 * discipline instead of queueing.
217 port->tty->low_latency = 1;
219 port->tty->real_raw = 1;
222 * Lose the small buffers usbserial provides. Make larger ones.
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) {
231 port->bulk_out_buffer = kmalloc(URBDATA_SIZE, GFP_KERNEL);
232 if (port->bulk_out_buffer == NULL) {
233 kfree(port->bulk_in_buffer);
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;
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);
248 err("%s - failed submitting read urb, error %d", __FUNCTION__, result);
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
261 result = usb_control_msg(serial->dev,
262 usb_sndctrlpipe(serial->dev, 0), 0x22, 0x21,
263 0x1, 0, NULL, 0, HZ / 10 + 1);
268 err("%s - failed doing control urb, error %d", __FUNCTION__, result);
273 err("%s - Out of memory", __FUNCTION__);
275 ipaq_destroy_lists(port);
281 static void ipaq_close(struct usb_serial_port *port, struct file *filp)
283 struct usb_serial *serial;
284 struct ipaq_private *priv = port->private;
286 if (port_paranoia_check(port, __FUNCTION__)) {
290 dbg("%s - port %d", __FUNCTION__, port->number);
292 serial = get_usb_serial(port, __FUNCTION__);
297 * shut down bulk read and write
300 usb_unlink_urb(port->write_urb);
301 usb_unlink_urb(port->read_urb);
302 ipaq_destroy_lists(port);
304 port->private = NULL;
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); */
310 static void ipaq_read_bulk_callback(struct urb *urb)
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;
318 if (port_paranoia_check(port, __FUNCTION__))
321 dbg("%s - port %d", __FUNCTION__, port->number);
324 dbg("%s - bad serial pointer, exiting", __FUNCTION__);
329 dbg("%s - nonzero read bulk status received: %d", __FUNCTION__, urb->status);
333 usb_serial_debug_data (__FILE__, __FUNCTION__, urb->actual_length, data);
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);
342 /* this doesn't actually push the data through unless tty->low_latency is set */
343 tty_insert_flip_char(tty, data[i], 0);
345 tty_flip_buffer_push(tty);
346 bytes_in += urb->actual_length;
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);
356 err("%s - failed resubmitting read urb, error %d", __FUNCTION__, result);
360 static int ipaq_write(struct usb_serial_port *port, int from_user, const unsigned char *buf,
363 const unsigned char *current_position = buf;
367 dbg("%s - port %d", __FUNCTION__, port->number);
369 usb_serial_debug_data(__FILE__, __FUNCTION__, count, buf);
372 transfer_size = min(count, PACKET_SIZE);
373 if (ipaq_write_bulk(port, from_user, current_position, transfer_size)) {
376 current_position += transfer_size;
377 bytes_sent += transfer_size;
378 count -= transfer_size;
379 bytes_out += transfer_size;
385 static int ipaq_write_bulk(struct usb_serial_port *port, int from_user, const unsigned char *buf,
388 struct ipaq_private *priv = port->private;
389 struct ipaq_packet *pkt = NULL;
393 if (priv->free_len <= 0) {
394 dbg("%s - we're stuffed", __FUNCTION__);
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;
404 spin_unlock_irqrestore(&write_list_lock, flags);
406 dbg("%s - we're stuffed", __FUNCTION__);
411 if (copy_from_user(pkt->data, buf, count))
414 memcpy(pkt->data, buf, count);
416 usb_serial_debug_data(__FILE__, __FUNCTION__, count, pkt->data);
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) {
425 ipaq_write_gather(port);
426 spin_unlock_irqrestore(&write_list_lock, flags);
427 result = usb_submit_urb(port->write_urb);
429 err("%s - failed submitting write urb, error %d", __FUNCTION__, result);
432 spin_unlock_irqrestore(&write_list_lock, flags);
437 static void ipaq_write_gather(struct usb_serial_port *port)
439 struct ipaq_private *priv = (struct ipaq_private *)port->private;
440 struct usb_serial *serial = port->serial;
442 struct ipaq_packet *pkt;
443 struct urb *urb = port->write_urb;
444 struct list_head *tmp;
446 if (urb->status == -EINPROGRESS) {
447 /* Should never happen */
448 err("%s - flushing while urb is active !", __FUNCTION__);
452 for (tmp = priv->queue.next; tmp != &priv->queue;) {
453 pkt = list_entry(tmp, struct ipaq_packet, list);
455 count = min(room, (int)(pkt->len - pkt->written));
456 memcpy(urb->transfer_buffer + (URBDATA_SIZE - room),
457 pkt->data + pkt->written, 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;
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,
479 static void ipaq_write_bulk_callback(struct urb *urb)
481 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
482 struct ipaq_private *priv = (struct ipaq_private *)port->private;
486 if (port_paranoia_check (port, __FUNCTION__)) {
490 dbg("%s - port %d", __FUNCTION__, port->number);
493 dbg("%s - nonzero write bulk status received: %d", __FUNCTION__, urb->status);
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);
502 err("%s - failed submitting write urb, error %d", __FUNCTION__, result);
506 spin_unlock_irqrestore(&write_list_lock, flags);
508 queue_task(&port->tqueue, &tq_immediate);
509 mark_bh(IMMEDIATE_BH);
514 static int ipaq_write_room(struct usb_serial_port *port)
516 struct ipaq_private *priv = (struct ipaq_private *)port->private;
518 dbg("%s - freelen %d", __FUNCTION__, priv->free_len);
519 return priv->free_len;
522 static int ipaq_chars_in_buffer(struct usb_serial_port *port)
524 struct ipaq_private *priv = (struct ipaq_private *)port->private;
526 dbg("%s - queuelen %d", __FUNCTION__, priv->queue_len);
527 return priv->queue_len;
530 static void ipaq_destroy_lists(struct usb_serial_port *port)
532 struct ipaq_private *priv = (struct ipaq_private *)port->private;
533 struct list_head *tmp;
534 struct ipaq_packet *pkt;
536 for (tmp = priv->queue.next; tmp != &priv->queue;) {
537 pkt = list_entry(tmp, struct ipaq_packet, list);
542 for (tmp = priv->freelist.next; tmp != &priv->freelist;) {
543 pkt = list_entry(tmp, struct ipaq_packet, list);
552 static int ipaq_startup(struct usb_serial *serial)
554 dbg("%s", __FUNCTION__);
555 usb_set_configuration(serial->dev, 1);
559 static void ipaq_shutdown(struct usb_serial *serial)
561 dbg("%s", __FUNCTION__);
564 static int __init ipaq_init(void)
566 spin_lock_init(&write_list_lock);
567 info(DRIVER_DESC " " DRIVER_VERSION);
569 ipaq_id_table[0].idVendor = vendor;
570 ipaq_id_table[0].idProduct = product;
572 usb_serial_register(&ipaq_device);
578 static void __exit ipaq_exit(void)
580 usb_serial_deregister(&ipaq_device);
584 module_init(ipaq_init);
585 module_exit(ipaq_exit);
587 MODULE_AUTHOR( DRIVER_AUTHOR );
588 MODULE_DESCRIPTION( DRIVER_DESC );
589 MODULE_LICENSE("GPL");
591 MODULE_PARM(debug, "i");
592 MODULE_PARM_DESC(debug, "Debug enabled or not");
594 MODULE_PARM(vendor, "h");
595 MODULE_PARM_DESC(vendor, "User specified USB idVendor");
597 MODULE_PARM(product, "h");
598 MODULE_PARM_DESC(product, "User specified USB idProduct");