ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / usb / serial / visor.c
1 /*
2  * USB HandSpring Visor, Palm m50x, and Sony Clie driver
3  * (supports all of the Palm OS USB devices)
4  *
5  *      Copyright (C) 1999 - 2003
6  *          Greg Kroah-Hartman (greg@kroah.com)
7  *
8  *      This program is free software; you can redistribute it and/or modify
9  *      it under the terms of the GNU General Public License as published by
10  *      the Free Software Foundation; either version 2 of the License, or
11  *      (at your option) any later version.
12  *
13  * See Documentation/usb/usb-serial.txt for more information on using this driver
14  *
15  * (06/03/2003) Judd Montgomery <judd at jpilot.org>
16  *     Added support for module parameter options for untested/unknown
17  *     devices.
18  *
19  * (03/09/2003) gkh
20  *      Added support for the Sony Clie NZ90V device.  Thanks to Martin Brachtl
21  *      <brachtl@redgrep.cz> for the information.
22  *
23  * (03/05/2003) gkh
24  *      Think Treo support is now working.
25  *
26  * (04/03/2002) gkh
27  *      Added support for the Sony OS 4.1 devices.  Thanks to Hiroyuki ARAKI
28  *      <hiro@zob.ne.jp> for the information.
29  *
30  * (03/27/2002) gkh
31  *      Removed assumptions that port->tty was always valid (is not true
32  *      for usb serial console devices.)
33  *
34  * (03/23/2002) gkh
35  *      Added support for the Palm i705 device, thanks to Thomas Riemer
36  *      <tom@netmech.com> for the information.
37  *
38  * (03/21/2002) gkh
39  *      Added support for the Palm m130 device, thanks to Udo Eisenbarth
40  *      <udo.eisenbarth@web.de> for the information.
41  *
42  * (02/27/2002) gkh
43  *      Reworked the urb handling logic.  We have no more pool, but dynamically
44  *      allocate the urb and the transfer buffer on the fly.  In testing this
45  *      does not incure any measurable overhead.  This also relies on the fact
46  *      that we have proper reference counting logic for urbs.
47  *
48  * (02/21/2002) SilaS
49  *  Added initial support for the Palm m515 devices.
50  *
51  * (02/14/2002) gkh
52  *      Added support for the Clie S-360 device.
53  *
54  * (12/18/2001) gkh
55  *      Added better Clie support for 3.5 devices.  Thanks to Geoffrey Levand
56  *      for the patch.
57  *
58  * (11/11/2001) gkh
59  *      Added support for the m125 devices, and added check to prevent oopses
60  *      for CliĆ© devices that lie about the number of ports they have.
61  *
62  * (08/30/2001) gkh
63  *      Added support for the Clie devices, both the 3.5 and 4.0 os versions.
64  *      Many thanks to Daniel Burke, and Bryan Payne for helping with this.
65  *
66  * (08/23/2001) gkh
67  *      fixed a few potential bugs pointed out by Oliver Neukum.
68  *
69  * (05/30/2001) gkh
70  *      switched from using spinlock to a semaphore, which fixes lots of problems.
71  *
72  * (05/28/2000) gkh
73  *      Added initial support for the Palm m500 and Palm m505 devices.
74  *
75  * (04/08/2001) gb
76  *      Identify version on module load.
77  *
78  * (01/21/2000) gkh
79  *      Added write_room and chars_in_buffer, as they were previously using the
80  *      generic driver versions which is all wrong now that we are using an urb
81  *      pool.  Thanks to Wolfgang Grandegger for pointing this out to me.
82  *      Removed count assignment in the write function, which was not needed anymore
83  *      either.  Thanks to Al Borchers for pointing this out.
84  *
85  * (12/12/2000) gkh
86  *      Moved MOD_DEC to end of visor_close to be nicer, as the final write 
87  *      message can sleep.
88  * 
89  * (11/12/2000) gkh
90  *      Fixed bug with data being dropped on the floor by forcing tty->low_latency
91  *      to be on.  Hopefully this fixes the OHCI issue!
92  *
93  * (11/01/2000) Adam J. Richter
94  *      usb_device_id table support
95  * 
96  * (10/05/2000) gkh
97  *      Fixed bug with urb->dev not being set properly, now that the usb
98  *      core needs it.
99  * 
100  * (09/11/2000) gkh
101  *      Got rid of always calling kmalloc for every urb we wrote out to the
102  *      device.
103  *      Added visor_read_callback so we can keep track of bytes in and out for
104  *      those people who like to know the speed of their device.
105  *      Removed DEBUG #ifdefs with call to usb_serial_debug_data
106  *
107  * (09/06/2000) gkh
108  *      Fixed oops in visor_exit.  Need to uncomment usb_unlink_urb call _after_
109  *      the host controller drivers set urb->dev = NULL when the urb is finished.
110  *
111  * (08/28/2000) gkh
112  *      Added locks for SMP safeness.
113  *
114  * (08/08/2000) gkh
115  *      Fixed endian problem in visor_startup.
116  *      Fixed MOD_INC and MOD_DEC logic and the ability to open a port more 
117  *      than once.
118  * 
119  * (07/23/2000) gkh
120  *      Added pool of write urbs to speed up transfers to the visor.
121  * 
122  * (07/19/2000) gkh
123  *      Added module_init and module_exit functions to handle the fact that this
124  *      driver is a loadable module now.
125  *
126  * (07/03/2000) gkh
127  *      Added visor_set_ioctl and visor_set_termios functions (they don't do much
128  *      of anything, but are good for debugging.)
129  * 
130  * (06/25/2000) gkh
131  *      Fixed bug in visor_unthrottle that should help with the disconnect in PPP
132  *      bug that people have been reporting.
133  *
134  * (06/23/2000) gkh
135  *      Cleaned up debugging statements in a quest to find UHCI timeout bug.
136  *
137  * (04/27/2000) Ryan VanderBijl
138  *      Fixed memory leak in visor_close
139  *
140  * (03/26/2000) gkh
141  *      Split driver up into device specific pieces.
142  * 
143  */
144
145 #include <linux/config.h>
146 #include <linux/kernel.h>
147 #include <linux/errno.h>
148 #include <linux/init.h>
149 #include <linux/slab.h>
150 #include <linux/tty.h>
151 #include <linux/tty_driver.h>
152 #include <linux/tty_flip.h>
153 #include <linux/module.h>
154 #include <linux/spinlock.h>
155 #include <asm/uaccess.h>
156 #include <linux/usb.h>
157
158 #ifdef CONFIG_USB_SERIAL_DEBUG
159         static int debug = 1;
160 #else
161         static int debug;
162 #endif
163
164 #include "usb-serial.h"
165 #include "visor.h"
166
167 /*
168  * Version Information
169  */
170 #define DRIVER_VERSION "v2.1"
171 #define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com>"
172 #define DRIVER_DESC "USB HandSpring Visor / Palm OS driver"
173
174 /* function prototypes for a handspring visor */
175 static int  visor_open          (struct usb_serial_port *port, struct file *filp);
176 static void visor_close         (struct usb_serial_port *port, struct file *filp);
177 static int  visor_write         (struct usb_serial_port *port, int from_user, const unsigned char *buf, int count);
178 static int  visor_write_room            (struct usb_serial_port *port);
179 static int  visor_chars_in_buffer       (struct usb_serial_port *port);
180 static void visor_throttle      (struct usb_serial_port *port);
181 static void visor_unthrottle    (struct usb_serial_port *port);
182 static int  visor_probe         (struct usb_serial *serial, const struct usb_device_id *id);
183 static int  visor_calc_num_ports(struct usb_serial *serial);
184 static void visor_shutdown      (struct usb_serial *serial);
185 static int  visor_ioctl         (struct usb_serial_port *port, struct file * file, unsigned int cmd, unsigned long arg);
186 static void visor_set_termios   (struct usb_serial_port *port, struct termios *old_termios);
187 static void visor_write_bulk_callback   (struct urb *urb, struct pt_regs *regs);
188 static void visor_read_bulk_callback    (struct urb *urb, struct pt_regs *regs);
189 static void visor_read_int_callback     (struct urb *urb, struct pt_regs *regs);
190 static int  clie_3_5_startup    (struct usb_serial *serial);
191 static int  treo_attach         (struct usb_serial *serial);
192 static int clie_5_attach (struct usb_serial *serial);
193 static int palm_os_3_probe (struct usb_serial *serial, const struct usb_device_id *id);
194 static int palm_os_4_probe (struct usb_serial *serial, const struct usb_device_id *id);
195
196 /* Parameters that may be passed into the module. */
197 static int vendor = -1;
198 static int product = -1;
199
200 static struct usb_device_id id_table [] = {
201         { USB_DEVICE(HANDSPRING_VENDOR_ID, HANDSPRING_VISOR_ID),
202                 .driver_info = (kernel_ulong_t)&palm_os_3_probe },
203         { USB_DEVICE(HANDSPRING_VENDOR_ID, HANDSPRING_TREO_ID),
204                 .driver_info = (kernel_ulong_t)&palm_os_4_probe },
205         { USB_DEVICE(HANDSPRING_VENDOR_ID, HANDSPRING_TREO600_ID),
206                 .driver_info = (kernel_ulong_t)&palm_os_4_probe },
207         { USB_DEVICE(PALM_VENDOR_ID, PALM_M500_ID),
208                 .driver_info = (kernel_ulong_t)&palm_os_4_probe },
209         { USB_DEVICE(PALM_VENDOR_ID, PALM_M505_ID),
210                 .driver_info = (kernel_ulong_t)&palm_os_4_probe },
211         { USB_DEVICE(PALM_VENDOR_ID, PALM_M515_ID),
212                 .driver_info = (kernel_ulong_t)&palm_os_4_probe },
213         { USB_DEVICE(PALM_VENDOR_ID, PALM_I705_ID),
214                 .driver_info = (kernel_ulong_t)&palm_os_4_probe },
215         { USB_DEVICE(PALM_VENDOR_ID, PALM_M100_ID),
216                 .driver_info = (kernel_ulong_t)&palm_os_4_probe },
217         { USB_DEVICE(PALM_VENDOR_ID, PALM_M125_ID),
218                 .driver_info = (kernel_ulong_t)&palm_os_4_probe },
219         { USB_DEVICE(PALM_VENDOR_ID, PALM_M130_ID),
220                 .driver_info = (kernel_ulong_t)&palm_os_4_probe },
221         { USB_DEVICE(PALM_VENDOR_ID, PALM_TUNGSTEN_T_ID),
222                 .driver_info = (kernel_ulong_t)&palm_os_4_probe },
223         { USB_DEVICE(PALM_VENDOR_ID, PALM_TUNGSTEN_Z_ID),
224                 .driver_info = (kernel_ulong_t)&palm_os_4_probe },
225         { USB_DEVICE(PALM_VENDOR_ID, PALM_ZIRE_ID),
226                 .driver_info = (kernel_ulong_t)&palm_os_4_probe },
227         { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_4_0_ID),
228                 .driver_info = (kernel_ulong_t)&palm_os_4_probe },
229         { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_S360_ID),
230                 .driver_info = (kernel_ulong_t)&palm_os_4_probe },
231         { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_4_1_ID),
232                 .driver_info = (kernel_ulong_t)&palm_os_4_probe },
233         { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_NX60_ID),
234                 .driver_info = (kernel_ulong_t)&palm_os_4_probe },
235         { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_NZ90V_ID),
236                 .driver_info = (kernel_ulong_t)&palm_os_4_probe },
237         { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_TJ25_ID),
238                 .driver_info = (kernel_ulong_t)&palm_os_4_probe },
239         { USB_DEVICE(SAMSUNG_VENDOR_ID, SAMSUNG_SCH_I330_ID), 
240                 .driver_info = (kernel_ulong_t)&palm_os_4_probe },
241         { USB_DEVICE(SAMSUNG_VENDOR_ID, SAMSUNG_SPH_I500_ID), 
242                 .driver_info = (kernel_ulong_t)&palm_os_4_probe },
243         { USB_DEVICE(GARMIN_VENDOR_ID, GARMIN_IQUE_3600_ID), 
244                 .driver_info = (kernel_ulong_t)&palm_os_4_probe },
245         { USB_DEVICE(ACEECA_VENDOR_ID, ACEECA_MEZ1000_ID),
246                 .driver_info = (kernel_ulong_t)&palm_os_4_probe },
247         { },                                    /* optional parameter entry */
248         { }                                     /* Terminating entry */
249 };
250
251 static struct usb_device_id clie_id_5_table [] = {
252         { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_UX50_ID),
253                 .driver_info = (kernel_ulong_t)&palm_os_4_probe },
254         { },                                    /* optional parameter entry */
255         { }                                     /* Terminating entry */
256 };
257
258 static struct usb_device_id clie_id_3_5_table [] = {
259         { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_3_5_ID) },
260         { }                                     /* Terminating entry */
261 };
262
263 static struct usb_device_id id_table_combined [] = {
264         { USB_DEVICE(HANDSPRING_VENDOR_ID, HANDSPRING_VISOR_ID) },
265         { USB_DEVICE(HANDSPRING_VENDOR_ID, HANDSPRING_TREO_ID) },
266         { USB_DEVICE(HANDSPRING_VENDOR_ID, HANDSPRING_TREO600_ID) },
267         { USB_DEVICE(PALM_VENDOR_ID, PALM_M500_ID) },
268         { USB_DEVICE(PALM_VENDOR_ID, PALM_M505_ID) },
269         { USB_DEVICE(PALM_VENDOR_ID, PALM_M515_ID) },
270         { USB_DEVICE(PALM_VENDOR_ID, PALM_I705_ID) },
271         { USB_DEVICE(PALM_VENDOR_ID, PALM_M100_ID) },
272         { USB_DEVICE(PALM_VENDOR_ID, PALM_M125_ID) },
273         { USB_DEVICE(PALM_VENDOR_ID, PALM_M130_ID) },
274         { USB_DEVICE(PALM_VENDOR_ID, PALM_TUNGSTEN_T_ID) },
275         { USB_DEVICE(PALM_VENDOR_ID, PALM_TUNGSTEN_Z_ID) },
276         { USB_DEVICE(PALM_VENDOR_ID, PALM_ZIRE_ID) },
277         { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_3_5_ID) },
278         { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_4_0_ID) },
279         { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_S360_ID) },
280         { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_4_1_ID) },
281         { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_NX60_ID) },
282         { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_NZ90V_ID) },
283         { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_UX50_ID) },
284         { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_TJ25_ID) },
285         { USB_DEVICE(SAMSUNG_VENDOR_ID, SAMSUNG_SCH_I330_ID) },
286         { USB_DEVICE(SAMSUNG_VENDOR_ID, SAMSUNG_SPH_I500_ID) },
287         { USB_DEVICE(GARMIN_VENDOR_ID, GARMIN_IQUE_3600_ID) },
288         { USB_DEVICE(ACEECA_VENDOR_ID, ACEECA_MEZ1000_ID) },
289         { },                                    /* optional parameter entry */
290         { }                                     /* Terminating entry */
291 };
292
293 MODULE_DEVICE_TABLE (usb, id_table_combined);
294
295 static struct usb_driver visor_driver = {
296         .owner =        THIS_MODULE,
297         .name =         "visor",
298         .probe =        usb_serial_probe,
299         .disconnect =   usb_serial_disconnect,
300         .id_table =     id_table_combined,
301 };
302
303 /* All of the device info needed for the Handspring Visor, and Palm 4.0 devices */
304 static struct usb_serial_device_type handspring_device = {
305         .owner =                THIS_MODULE,
306         .name =                 "Handspring Visor / Palm OS",
307         .short_name =           "visor",
308         .id_table =             id_table,
309         .num_interrupt_in =     NUM_DONT_CARE,
310         .num_bulk_in =          2,
311         .num_bulk_out =         2,
312         .num_ports =            2,
313         .open =                 visor_open,
314         .close =                visor_close,
315         .throttle =             visor_throttle,
316         .unthrottle =           visor_unthrottle,
317         .attach =               treo_attach,
318         .probe =                visor_probe,
319         .calc_num_ports =       visor_calc_num_ports,
320         .shutdown =             visor_shutdown,
321         .ioctl =                visor_ioctl,
322         .set_termios =          visor_set_termios,
323         .write =                visor_write,
324         .write_room =           visor_write_room,
325         .chars_in_buffer =      visor_chars_in_buffer,
326         .write_bulk_callback =  visor_write_bulk_callback,
327         .read_bulk_callback =   visor_read_bulk_callback,
328         .read_int_callback =    visor_read_int_callback,
329 };
330
331 /* All of the device info needed for the Clie UX50, TH55 Palm 5.0 devices */
332 static struct usb_serial_device_type clie_5_device = {
333         .owner =                THIS_MODULE,
334         .name =                 "Sony Clie 5.0",
335         .short_name =           "clie_5",
336         .id_table =             clie_id_5_table,
337         .num_interrupt_in =     NUM_DONT_CARE,
338         .num_bulk_in =          2,
339         .num_bulk_out =         2,
340         .num_ports =            2,
341         .open =                 visor_open,
342         .close =                visor_close,
343         .throttle =             visor_throttle,
344         .unthrottle =           visor_unthrottle,
345         .attach =               clie_5_attach,
346         .probe =                visor_probe,
347         .calc_num_ports =       visor_calc_num_ports,
348         .shutdown =             visor_shutdown,
349         .ioctl =                visor_ioctl,
350         .set_termios =          visor_set_termios,
351         .write =                visor_write,
352         .write_room =           visor_write_room,
353         .chars_in_buffer =      visor_chars_in_buffer,
354         .write_bulk_callback =  visor_write_bulk_callback,
355         .read_bulk_callback =   visor_read_bulk_callback,
356         .read_int_callback =    visor_read_int_callback,
357 };
358
359 /* device info for the Sony Clie OS version 3.5 */
360 static struct usb_serial_device_type clie_3_5_device = {
361         .owner =                THIS_MODULE,
362         .name =                 "Sony Clie 3.5",
363         .short_name =           "clie_3.5",
364         .id_table =             clie_id_3_5_table,
365         .num_interrupt_in =     0,
366         .num_bulk_in =          1,
367         .num_bulk_out =         1,
368         .num_ports =            1,
369         .open =                 visor_open,
370         .close =                visor_close,
371         .throttle =             visor_throttle,
372         .unthrottle =           visor_unthrottle,
373         .attach =               clie_3_5_startup,
374         .ioctl =                visor_ioctl,
375         .set_termios =          visor_set_termios,
376         .write =                visor_write,
377         .write_room =           visor_write_room,
378         .chars_in_buffer =      visor_chars_in_buffer,
379         .write_bulk_callback =  visor_write_bulk_callback,
380         .read_bulk_callback =   visor_read_bulk_callback,
381 };
382
383
384 static int bytes_in;
385 static int bytes_out;
386
387
388 /******************************************************************************
389  * Handspring Visor specific driver functions
390  ******************************************************************************/
391 static int visor_open (struct usb_serial_port *port, struct file *filp)
392 {
393         struct usb_serial *serial = port->serial;
394         int result = 0;
395
396         if (port_paranoia_check (port, __FUNCTION__))
397                 return -ENODEV;
398         
399         dbg("%s - port %d", __FUNCTION__, port->number);
400
401         if (!port->read_urb) {
402                 /* this is needed for some brain dead Sony devices */
403                 dev_err(&port->dev, "Device lied about number of ports, please use a lower one.\n");
404                 return -ENODEV;
405         }
406
407         bytes_in = 0;
408         bytes_out = 0;
409
410         /*
411          * Force low_latency on so that our tty_push actually forces the data
412          * through, otherwise it is scheduled, and with high data rates (like
413          * with OHCI) data can get lost.
414          */
415         if (port->tty)
416                 port->tty->low_latency = 1;
417
418         /* Start reading from the device */
419         usb_fill_bulk_urb (port->read_urb, serial->dev,
420                            usb_rcvbulkpipe (serial->dev, 
421                                             port->bulk_in_endpointAddress),
422                            port->read_urb->transfer_buffer,
423                            port->read_urb->transfer_buffer_length,
424                            visor_read_bulk_callback, port);
425         result = usb_submit_urb(port->read_urb, GFP_KERNEL);
426         if (result) {
427                 dev_err(&port->dev, "%s - failed submitting read urb, error %d\n",
428                         __FUNCTION__, result);
429                 goto exit;
430         }
431         
432         if (port->interrupt_in_urb) {
433                 dbg("%s - adding interrupt input for treo", __FUNCTION__);
434                 result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
435                 if (result)
436                         dev_err(&port->dev, "%s - failed submitting interrupt urb, error %d\n",
437                                 __FUNCTION__, result);
438         }
439 exit:   
440         return result;
441 }
442
443
444 static void visor_close (struct usb_serial_port *port, struct file * filp)
445 {
446         struct usb_serial *serial;
447         unsigned char *transfer_buffer;
448
449         if (port_paranoia_check (port, __FUNCTION__))
450                 return;
451         
452         dbg("%s - port %d", __FUNCTION__, port->number);
453                          
454         serial = get_usb_serial (port, __FUNCTION__);
455         if (!serial)
456                 return;
457         
458         /* shutdown our urbs */
459         usb_unlink_urb (port->read_urb);
460         if (port->interrupt_in_urb)
461                 usb_unlink_urb (port->interrupt_in_urb);
462
463         /* Try to send shutdown message, if the device is gone, this will just fail. */
464         transfer_buffer =  kmalloc (0x12, GFP_KERNEL);
465         if (transfer_buffer) {
466                 usb_control_msg (serial->dev,
467                                  usb_rcvctrlpipe(serial->dev, 0),
468                                  VISOR_CLOSE_NOTIFICATION, 0xc2,
469                                  0x0000, 0x0000, 
470                                  transfer_buffer, 0x12, 300);
471                 kfree (transfer_buffer);
472         }
473
474         /* Uncomment the following line if you want to see some statistics in your syslog */
475         /* dev_info (&port->dev, "Bytes In = %d  Bytes Out = %d\n", bytes_in, bytes_out); */
476 }
477
478
479 static int visor_write (struct usb_serial_port *port, int from_user, const unsigned char *buf, int count)
480 {
481         struct usb_serial *serial = port->serial;
482         struct urb *urb;
483         unsigned char *buffer;
484         int status;
485
486         dbg("%s - port %d", __FUNCTION__, port->number);
487
488         buffer = kmalloc (count, GFP_ATOMIC);
489         if (!buffer) {
490                 dev_err(&port->dev, "out of memory\n");
491                 return -ENOMEM;
492         }
493
494         urb = usb_alloc_urb(0, GFP_ATOMIC);
495         if (!urb) {
496                 dev_err(&port->dev, "no more free urbs\n");
497                 kfree (buffer);
498                 return -ENOMEM;
499         }
500
501         if (from_user) {
502                 if (copy_from_user (buffer, buf, count)) {
503                         kfree (buffer);
504                         usb_free_urb (urb);
505                         return -EFAULT;
506                 }
507         } else {
508                 memcpy (buffer, buf, count);
509         }
510
511         usb_serial_debug_data (__FILE__, __FUNCTION__, count, buffer);
512
513         usb_fill_bulk_urb (urb, serial->dev,
514                            usb_sndbulkpipe (serial->dev,
515                                             port->bulk_out_endpointAddress),
516                            buffer, count, 
517                            visor_write_bulk_callback, port);
518
519         /* send it down the pipe */
520         status = usb_submit_urb(urb, GFP_ATOMIC);
521         if (status) {
522                 dev_err(&port->dev, "%s - usb_submit_urb(write bulk) failed with status = %d\n",
523                         __FUNCTION__, status);
524                 count = status;
525         } else {
526                 bytes_out += count;
527         }
528
529         /* we are done with this urb, so let the host driver
530          * really free it when it is finished with it */
531         usb_free_urb (urb);
532
533         return count;
534 }
535
536
537 static int visor_write_room (struct usb_serial_port *port)
538 {
539         dbg("%s - port %d", __FUNCTION__, port->number);
540
541         /*
542          * We really can take anything the user throws at us
543          * but let's pick a nice big number to tell the tty
544          * layer that we have lots of free space
545          */
546         return 2048;
547 }
548
549
550 static int visor_chars_in_buffer (struct usb_serial_port *port)
551 {
552         dbg("%s - port %d", __FUNCTION__, port->number);
553
554         /* 
555          * We can't really account for how much data we
556          * have sent out, but hasn't made it through to the
557          * device, so just tell the tty layer that everything
558          * is flushed.
559          */
560         return 0;
561 }
562
563
564 static void visor_write_bulk_callback (struct urb *urb, struct pt_regs *regs)
565 {
566         struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
567
568         /* free up the transfer buffer, as usb_free_urb() does not do this */
569         kfree (urb->transfer_buffer);
570
571         if (port_paranoia_check (port, __FUNCTION__))
572                 return;
573         
574         dbg("%s - port %d", __FUNCTION__, port->number);
575         
576         if (urb->status)
577                 dbg("%s - nonzero write bulk status received: %d",
578                     __FUNCTION__, urb->status);
579
580         schedule_work(&port->work);
581 }
582
583
584 static void visor_read_bulk_callback (struct urb *urb, struct pt_regs *regs)
585 {
586         struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
587         struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);
588         struct tty_struct *tty;
589         unsigned char *data = urb->transfer_buffer;
590         int i;
591         int result;
592
593         if (port_paranoia_check (port, __FUNCTION__))
594                 return;
595
596         dbg("%s - port %d", __FUNCTION__, port->number);
597
598         if (!serial) {
599                 dbg("%s - bad serial pointer, exiting", __FUNCTION__);
600                 return;
601         }
602
603         if (urb->status) {
604                 dbg("%s - nonzero read bulk status received: %d", __FUNCTION__, urb->status);
605                 return;
606         }
607
608         usb_serial_debug_data (__FILE__, __FUNCTION__, urb->actual_length, data);
609
610         tty = port->tty;
611         if (tty && urb->actual_length) {
612                 for (i = 0; i < urb->actual_length ; ++i) {
613                         /* if we insert more than TTY_FLIPBUF_SIZE characters, we drop them. */
614                         if(tty->flip.count >= TTY_FLIPBUF_SIZE) {
615                                 tty_flip_buffer_push(tty);
616                         }
617                         /* this doesn't actually push the data through unless tty->low_latency is set */
618                         tty_insert_flip_char(tty, data[i], 0);
619                 }
620                 tty_flip_buffer_push(tty);
621         }
622         bytes_in += urb->actual_length;
623
624         /* Continue trying to always read  */
625         usb_fill_bulk_urb (port->read_urb, serial->dev,
626                            usb_rcvbulkpipe (serial->dev,
627                                             port->bulk_in_endpointAddress),
628                            port->read_urb->transfer_buffer,
629                            port->read_urb->transfer_buffer_length,
630                            visor_read_bulk_callback, port);
631         result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
632         if (result)
633                 dev_err(&port->dev, "%s - failed resubmitting read urb, error %d\n", __FUNCTION__, result);
634         return;
635 }
636
637 static void visor_read_int_callback (struct urb *urb, struct pt_regs *regs)
638 {
639         int result;
640
641         switch (urb->status) {
642         case 0:
643                 /* success */
644                 break;
645         case -ECONNRESET:
646         case -ENOENT:
647         case -ESHUTDOWN:
648                 /* this urb is terminated, clean up */
649                 dbg("%s - urb shutting down with status: %d",
650                     __FUNCTION__, urb->status);
651                 return;
652         default:
653                 dbg("%s - nonzero urb status received: %d",
654                     __FUNCTION__, urb->status);
655                 goto exit;
656         }
657
658         /*
659          * This information is still unknown what it can be used for.
660          * If anyone has an idea, please let the author know...
661          *
662          * Rumor has it this endpoint is used to notify when data
663          * is ready to be read from the bulk ones.
664          */
665         usb_serial_debug_data (__FILE__, __FUNCTION__, urb->actual_length,
666                                urb->transfer_buffer);
667
668 exit:
669         result = usb_submit_urb (urb, GFP_ATOMIC);
670         if (result)
671                 dev_err(&urb->dev->dev, "%s - Error %d submitting interrupt urb\n",
672                         __FUNCTION__, result);
673 }
674
675 static void visor_throttle (struct usb_serial_port *port)
676 {
677         dbg("%s - port %d", __FUNCTION__, port->number);
678         usb_unlink_urb (port->read_urb);
679 }
680
681
682 static void visor_unthrottle (struct usb_serial_port *port)
683 {
684         int result;
685
686         dbg("%s - port %d", __FUNCTION__, port->number);
687
688         port->read_urb->dev = port->serial->dev;
689         result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
690         if (result)
691                 dev_err(&port->dev, "%s - failed submitting read urb, error %d\n", __FUNCTION__, result);
692 }
693
694 static int palm_os_3_probe (struct usb_serial *serial, const struct usb_device_id *id)
695 {
696         struct device *dev = &serial->dev->dev;
697         struct visor_connection_info *connection_info;
698         unsigned char *transfer_buffer;
699         char *string;
700         int retval = 0;
701         int i;
702         int num_ports;
703
704         dbg("%s", __FUNCTION__);
705
706         transfer_buffer = kmalloc (sizeof (*connection_info), GFP_KERNEL);
707         if (!transfer_buffer) {
708                 dev_err(dev, "%s - kmalloc(%Zd) failed.\n", __FUNCTION__,
709                         sizeof(*connection_info));
710                 return -ENOMEM;
711         }
712
713         /* send a get connection info request */
714         retval = usb_control_msg (serial->dev,
715                                   usb_rcvctrlpipe(serial->dev, 0),
716                                   VISOR_GET_CONNECTION_INFORMATION,
717                                   0xc2, 0x0000, 0x0000, transfer_buffer,
718                                   sizeof(*connection_info), 300);
719         if (retval < 0) {
720                 dev_err(dev, "%s - error %d getting connection information\n",
721                         __FUNCTION__, retval);
722                 goto exit;
723         }
724                 
725         connection_info = (struct visor_connection_info *)transfer_buffer;
726
727         le16_to_cpus(&connection_info->num_ports);
728         num_ports = connection_info->num_ports;
729         /* handle devices that report invalid stuff here */
730         if (num_ports > 2)
731                 num_ports = 2;
732         dev_info(dev, "%s: Number of ports: %d\n", serial->type->name,
733                 connection_info->num_ports);
734
735         for (i = 0; i < num_ports; ++i) {
736                 switch (connection_info->connections[i].port_function_id) {
737                         case VISOR_FUNCTION_GENERIC:
738                                 string = "Generic";
739                                 break;
740                         case VISOR_FUNCTION_DEBUGGER:
741                                 string = "Debugger";
742                                 break;
743                         case VISOR_FUNCTION_HOTSYNC:
744                                 string = "HotSync";
745                                 break;
746                         case VISOR_FUNCTION_CONSOLE:
747                                 string = "Console";
748                                 break;
749                         case VISOR_FUNCTION_REMOTE_FILE_SYS:
750                                 string = "Remote File System";
751                                 break;
752                         default:
753                                 string = "unknown";
754                                 break;  
755                 }
756                 dev_info(dev, "%s: port %d, is for %s use\n", serial->type->name,
757                          connection_info->connections[i].port, string);
758         }
759
760         /*
761          * save off our num_ports info so that we can use it in the
762          * calc_num_ports callback
763          */
764         usb_set_serial_data(serial, (void *)(long)num_ports);
765
766         /* ask for the number of bytes available, but ignore the response as it is broken */
767         retval = usb_control_msg (serial->dev,
768                                   usb_rcvctrlpipe(serial->dev, 0),
769                                   VISOR_REQUEST_BYTES_AVAILABLE,
770                                   0xc2, 0x0000, 0x0005, transfer_buffer,
771                                   0x02, 300);
772         if (retval < 0)
773                 dev_err(dev, "%s - error %d getting bytes available request\n",
774                         __FUNCTION__, retval);
775         retval = 0;
776
777 exit:
778         kfree (transfer_buffer);
779
780         return retval;
781 }
782
783 static int palm_os_4_probe (struct usb_serial *serial, const struct usb_device_id *id)
784 {
785         struct device *dev = &serial->dev->dev;
786         struct palm_ext_connection_info *connection_info;
787         unsigned char *transfer_buffer;
788         int retval;
789
790         dbg("%s", __FUNCTION__);
791
792         transfer_buffer =  kmalloc (sizeof (*connection_info), GFP_KERNEL);
793         if (!transfer_buffer) {
794                 dev_err(dev, "%s - kmalloc(%Zd) failed.\n", __FUNCTION__,
795                         sizeof(*connection_info));
796                 return -ENOMEM;
797         }
798
799         retval = usb_control_msg (serial->dev,
800                                   usb_rcvctrlpipe(serial->dev, 0), 
801                                   PALM_GET_EXT_CONNECTION_INFORMATION,
802                                   0xc2, 0x0000, 0x0000, transfer_buffer,
803                                   sizeof (*connection_info), 300);
804         if (retval < 0)
805                 dev_err(dev, "%s - error %d getting connection info\n",
806                         __FUNCTION__, retval);
807         else
808                 usb_serial_debug_data (__FILE__, __FUNCTION__, 0x14, transfer_buffer);
809
810         kfree (transfer_buffer);
811         return 0;
812 }
813
814
815 static int visor_probe (struct usb_serial *serial, const struct usb_device_id *id)
816 {
817         int retval = 0;
818         int (*startup) (struct usb_serial *serial, const struct usb_device_id *id);
819
820         dbg("%s", __FUNCTION__);
821
822         if (serial->dev->actconfig->desc.bConfigurationValue != 1) {
823                 err("active config #%d != 1 ??",
824                         serial->dev->actconfig->desc.bConfigurationValue);
825                 return -ENODEV;
826         }
827
828         if (id->driver_info) {
829                 startup = (void *)id->driver_info;
830                 retval = startup(serial, id);
831         }
832
833         return retval;
834 }
835
836 static int visor_calc_num_ports (struct usb_serial *serial)
837 {
838         int num_ports = (int)(long)(usb_get_serial_data(serial));
839
840         if (num_ports)
841                 usb_set_serial_data(serial, NULL);
842
843         return num_ports;
844 }
845
846 static int clie_3_5_startup (struct usb_serial *serial)
847 {
848         struct device *dev = &serial->dev->dev;
849         int result;
850         u8 data;
851
852         dbg("%s", __FUNCTION__);
853
854         /*
855          * Note that PEG-300 series devices expect the following two calls.
856          */
857
858         /* get the config number */
859         result = usb_control_msg (serial->dev, usb_rcvctrlpipe(serial->dev, 0),
860                                   USB_REQ_GET_CONFIGURATION, USB_DIR_IN,
861                                   0, 0, &data, 1, HZ * 3);
862         if (result < 0) {
863                 dev_err(dev, "%s: get config number failed: %d\n", __FUNCTION__, result);
864                 return result;
865         }
866         if (result != 1) {
867                 dev_err(dev, "%s: get config number bad return length: %d\n", __FUNCTION__, result);
868                 return -EIO;
869         }
870
871         /* get the interface number */
872         result = usb_control_msg (serial->dev, usb_rcvctrlpipe(serial->dev, 0),
873                                   USB_REQ_GET_INTERFACE, 
874                                   USB_DIR_IN | USB_RECIP_INTERFACE,
875                                   0, 0, &data, 1, HZ * 3);
876         if (result < 0) {
877                 dev_err(dev, "%s: get interface number failed: %d\n", __FUNCTION__, result);
878                 return result;
879         }
880         if (result != 1) {
881                 dev_err(dev, "%s: get interface number bad return length: %d\n", __FUNCTION__, result);
882                 return -EIO;
883         }
884
885         return 0;
886 }
887  
888 static int treo_attach (struct usb_serial *serial)
889 {
890         struct usb_serial_port *port;
891         int i;
892
893         /* Only do this endpoint hack for the Handspring devices with
894          * interrupt in endpoints, which for now are the Treo devices. */
895         if ((serial->dev->descriptor.idVendor != HANDSPRING_VENDOR_ID) ||
896             (serial->num_interrupt_in == 0))
897                 return 0;
898
899         dbg("%s", __FUNCTION__);
900
901         /* Ok, this is pretty ugly, but these devices want to use the
902          * interrupt endpoint as paired up with a bulk endpoint for a
903          * "virtual serial port".  So let's force the endpoints to be
904          * where we want them to be. */
905         for (i = serial->num_bulk_in; i < serial->num_ports; ++i) {
906                 port = serial->port[i];
907                 port->read_urb = serial->port[0]->read_urb;
908                 port->bulk_in_endpointAddress = serial->port[0]->bulk_in_endpointAddress;
909                 port->bulk_in_buffer = serial->port[0]->bulk_in_buffer;
910         }
911
912         for (i = serial->num_bulk_out; i < serial->num_ports; ++i) {
913                 port = serial->port[i];
914                 port->write_urb = serial->port[0]->write_urb;
915                 port->bulk_out_size = serial->port[0]->bulk_out_size;
916                 port->bulk_out_endpointAddress = serial->port[0]->bulk_out_endpointAddress;
917                 port->bulk_out_buffer = serial->port[0]->bulk_out_buffer;
918         }
919
920         for (i = serial->num_interrupt_in; i < serial->num_ports; ++i) {
921                 port = serial->port[i];
922                 port->interrupt_in_urb = serial->port[0]->interrupt_in_urb;
923                 port->interrupt_in_endpointAddress = serial->port[0]->interrupt_in_endpointAddress;
924                 port->interrupt_in_buffer = serial->port[0]->interrupt_in_buffer;
925         }
926
927         return 0;
928 }
929
930 static int clie_5_attach (struct usb_serial *serial)
931 {
932         dbg("%s", __FUNCTION__);
933
934         /* TH55 registers 2 ports. 
935            Communication in from the UX50/TH55 uses bulk_in_endpointAddress from port 0 
936            Communication out to the UX50/TH55 uses bulk_out_endpointAddress from port 1 
937            
938            Lets do a quick and dirty mapping
939          */
940         
941         /* some sanity check */
942         if (serial->num_ports < 2)
943                 return -1;
944                 
945         /* port 0 now uses the modified endpoint Address */
946         serial->port[0]->bulk_out_endpointAddress = serial->port[1]->bulk_out_endpointAddress;
947
948         return 0;
949 }
950
951 static void visor_shutdown (struct usb_serial *serial)
952 {
953         dbg("%s", __FUNCTION__);
954 }
955
956 static int visor_ioctl (struct usb_serial_port *port, struct file * file, unsigned int cmd, unsigned long arg)
957 {
958         dbg("%s - port %d, cmd 0x%.4x", __FUNCTION__, port->number, cmd);
959
960         return -ENOIOCTLCMD;
961 }
962
963
964 /* This function is all nice and good, but we don't change anything based on it :) */
965 static void visor_set_termios (struct usb_serial_port *port, struct termios *old_termios)
966 {
967         unsigned int cflag;
968
969         dbg("%s - port %d", __FUNCTION__, port->number);
970
971         if ((!port->tty) || (!port->tty->termios)) {
972                 dbg("%s - no tty structures", __FUNCTION__);
973                 return;
974         }
975
976         cflag = port->tty->termios->c_cflag;
977         /* check that they really want us to change something */
978         if (old_termios) {
979                 if ((cflag == old_termios->c_cflag) &&
980                     (RELEVANT_IFLAG(port->tty->termios->c_iflag) == RELEVANT_IFLAG(old_termios->c_iflag))) {
981                         dbg("%s - nothing to change...", __FUNCTION__);
982                         return;
983                 }
984         }
985
986         /* get the byte size */
987         switch (cflag & CSIZE) {
988                 case CS5:       dbg("%s - data bits = 5", __FUNCTION__);   break;
989                 case CS6:       dbg("%s - data bits = 6", __FUNCTION__);   break;
990                 case CS7:       dbg("%s - data bits = 7", __FUNCTION__);   break;
991                 default:
992                 case CS8:       dbg("%s - data bits = 8", __FUNCTION__);   break;
993         }
994         
995         /* determine the parity */
996         if (cflag & PARENB)
997                 if (cflag & PARODD)
998                         dbg("%s - parity = odd", __FUNCTION__);
999                 else
1000                         dbg("%s - parity = even", __FUNCTION__);
1001         else
1002                 dbg("%s - parity = none", __FUNCTION__);
1003
1004         /* figure out the stop bits requested */
1005         if (cflag & CSTOPB)
1006                 dbg("%s - stop bits = 2", __FUNCTION__);
1007         else
1008                 dbg("%s - stop bits = 1", __FUNCTION__);
1009
1010         
1011         /* figure out the flow control settings */
1012         if (cflag & CRTSCTS)
1013                 dbg("%s - RTS/CTS is enabled", __FUNCTION__);
1014         else
1015                 dbg("%s - RTS/CTS is disabled", __FUNCTION__);
1016         
1017         /* determine software flow control */
1018         if (I_IXOFF(port->tty))
1019                 dbg("%s - XON/XOFF is enabled, XON = %2x, XOFF = %2x",
1020                     __FUNCTION__, START_CHAR(port->tty), STOP_CHAR(port->tty));
1021         else
1022                 dbg("%s - XON/XOFF is disabled", __FUNCTION__);
1023
1024         /* get the baud rate wanted */
1025         dbg("%s - baud rate = %d", __FUNCTION__, tty_get_baud_rate(port->tty));
1026
1027         return;
1028 }
1029
1030
1031 static int __init visor_init (void)
1032 {
1033         int i, retval;
1034         /* Only if parameters were passed to us */
1035         if ((vendor>0) && (product>0)) {
1036                 struct usb_device_id usb_dev_temp[]=
1037                         {{USB_DEVICE(vendor, product),
1038                         .driver_info = (kernel_ulong_t)&palm_os_4_probe }};
1039
1040                 /* Find the last entry in id_table */
1041                 for (i=0; ; i++) {
1042                         if (id_table[i].idVendor==0) {
1043                                 id_table[i] = usb_dev_temp[0];
1044                                 break;
1045                         }
1046                 }
1047                 /* Find the last entry in id_table_combined */
1048                 for (i=0; ; i++) {
1049                         if (id_table_combined[i].idVendor==0) {
1050                                 id_table_combined[i] = usb_dev_temp[0];
1051                                 break;
1052                         }
1053                 }
1054                 info("Untested USB device specified at time of module insertion");
1055                 info("Warning: This is not guaranteed to work");
1056                 info("Using a newer kernel is preferred to this method");
1057                 info("Adding Palm OS protocol 4.x support for unknown device: 0x%x/0x%x",
1058                         vendor, product);
1059         }
1060         retval = usb_serial_register(&handspring_device);
1061         if (retval)
1062                 goto failed_handspring_register;
1063         retval = usb_serial_register(&clie_3_5_device);
1064         if (retval)
1065                 goto failed_clie_3_5_register;
1066         retval = usb_serial_register(&clie_5_device);
1067         if (retval)
1068                 goto failed_clie_5_register;
1069         retval = usb_register(&visor_driver);
1070         if (retval) 
1071                 goto failed_usb_register;
1072         info(DRIVER_DESC " " DRIVER_VERSION);
1073
1074         return 0;
1075 failed_usb_register:
1076         usb_serial_deregister(&clie_5_device);
1077 failed_clie_5_register:
1078         usb_serial_deregister(&clie_3_5_device);
1079 failed_clie_3_5_register:
1080         usb_serial_deregister(&handspring_device);
1081 failed_handspring_register:
1082         return retval;
1083 }
1084
1085
1086 static void __exit visor_exit (void)
1087 {
1088         usb_deregister (&visor_driver);
1089         usb_serial_deregister (&handspring_device);
1090         usb_serial_deregister (&clie_3_5_device);
1091         usb_serial_deregister (&clie_5_device);
1092 }
1093
1094
1095 module_init(visor_init);
1096 module_exit(visor_exit);
1097
1098 MODULE_AUTHOR( DRIVER_AUTHOR );
1099 MODULE_DESCRIPTION( DRIVER_DESC );
1100 MODULE_LICENSE("GPL");
1101
1102 MODULE_PARM(debug, "i");
1103 MODULE_PARM_DESC(debug, "Debug enabled or not");
1104
1105 MODULE_PARM(vendor, "i");
1106 MODULE_PARM_DESC(vendor, "User specified vendor ID");
1107 MODULE_PARM(product, "i");
1108 MODULE_PARM_DESC(product, "User specified product ID");