29b64a49e741451fa175347c4758368ea235a4dc
[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 - 2004
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/moduleparam.h>
155 #include <linux/spinlock.h>
156 #include <asm/uaccess.h>
157 #include <linux/usb.h>
158
159 #ifdef CONFIG_USB_SERIAL_DEBUG
160         static int debug = 1;
161 #else
162         static int debug;
163 #endif
164
165 #include "usb-serial.h"
166 #include "visor.h"
167
168 /*
169  * Version Information
170  */
171 #define DRIVER_VERSION "v2.1"
172 #define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com>"
173 #define DRIVER_DESC "USB HandSpring Visor / Palm OS driver"
174
175 /* function prototypes for a handspring visor */
176 static int  visor_open          (struct usb_serial_port *port, struct file *filp);
177 static void visor_close         (struct usb_serial_port *port, struct file *filp);
178 static int  visor_write         (struct usb_serial_port *port, int from_user, const unsigned char *buf, int count);
179 static int  visor_write_room            (struct usb_serial_port *port);
180 static int  visor_chars_in_buffer       (struct usb_serial_port *port);
181 static void visor_throttle      (struct usb_serial_port *port);
182 static void visor_unthrottle    (struct usb_serial_port *port);
183 static int  visor_probe         (struct usb_serial *serial, const struct usb_device_id *id);
184 static int  visor_calc_num_ports(struct usb_serial *serial);
185 static void visor_shutdown      (struct usb_serial *serial);
186 static int  visor_ioctl         (struct usb_serial_port *port, struct file * file, unsigned int cmd, unsigned long arg);
187 static void visor_set_termios   (struct usb_serial_port *port, struct termios *old_termios);
188 static void visor_write_bulk_callback   (struct urb *urb, struct pt_regs *regs);
189 static void visor_read_bulk_callback    (struct urb *urb, struct pt_regs *regs);
190 static void visor_read_int_callback     (struct urb *urb, struct pt_regs *regs);
191 static int  clie_3_5_startup    (struct usb_serial *serial);
192 static int  treo_attach         (struct usb_serial *serial);
193 static int clie_5_attach (struct usb_serial *serial);
194 static int palm_os_3_probe (struct usb_serial *serial, const struct usb_device_id *id);
195 static int palm_os_4_probe (struct usb_serial *serial, const struct usb_device_id *id);
196
197 /* Parameters that may be passed into the module. */
198 static __u16 vendor;
199 static __u16 product;
200
201 static struct usb_device_id id_table [] = {
202         { USB_DEVICE(HANDSPRING_VENDOR_ID, HANDSPRING_VISOR_ID),
203                 .driver_info = (kernel_ulong_t)&palm_os_3_probe },
204         { USB_DEVICE(HANDSPRING_VENDOR_ID, HANDSPRING_TREO_ID),
205                 .driver_info = (kernel_ulong_t)&palm_os_4_probe },
206         { USB_DEVICE(HANDSPRING_VENDOR_ID, HANDSPRING_TREO600_ID),
207                 .driver_info = (kernel_ulong_t)&palm_os_4_probe },
208         { USB_DEVICE(PALM_VENDOR_ID, PALM_M500_ID),
209                 .driver_info = (kernel_ulong_t)&palm_os_4_probe },
210         { USB_DEVICE(PALM_VENDOR_ID, PALM_M505_ID),
211                 .driver_info = (kernel_ulong_t)&palm_os_4_probe },
212         { USB_DEVICE(PALM_VENDOR_ID, PALM_M515_ID),
213                 .driver_info = (kernel_ulong_t)&palm_os_4_probe },
214         { USB_DEVICE(PALM_VENDOR_ID, PALM_I705_ID),
215                 .driver_info = (kernel_ulong_t)&palm_os_4_probe },
216         { USB_DEVICE(PALM_VENDOR_ID, PALM_M100_ID),
217                 .driver_info = (kernel_ulong_t)&palm_os_4_probe },
218         { USB_DEVICE(PALM_VENDOR_ID, PALM_M125_ID),
219                 .driver_info = (kernel_ulong_t)&palm_os_4_probe },
220         { USB_DEVICE(PALM_VENDOR_ID, PALM_M130_ID),
221                 .driver_info = (kernel_ulong_t)&palm_os_4_probe },
222         { USB_DEVICE(PALM_VENDOR_ID, PALM_TUNGSTEN_T_ID),
223                 .driver_info = (kernel_ulong_t)&palm_os_4_probe },
224         { USB_DEVICE(PALM_VENDOR_ID, PALM_TUNGSTEN_Z_ID),
225                 .driver_info = (kernel_ulong_t)&palm_os_4_probe },
226         { USB_DEVICE(PALM_VENDOR_ID, PALM_ZIRE31_ID),
227                 .driver_info = (kernel_ulong_t)&palm_os_4_probe },
228         { USB_DEVICE(PALM_VENDOR_ID, PALM_ZIRE_ID),
229                 .driver_info = (kernel_ulong_t)&palm_os_4_probe },
230         { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_4_0_ID),
231                 .driver_info = (kernel_ulong_t)&palm_os_4_probe },
232         { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_S360_ID),
233                 .driver_info = (kernel_ulong_t)&palm_os_4_probe },
234         { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_4_1_ID),
235                 .driver_info = (kernel_ulong_t)&palm_os_4_probe },
236         { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_NX60_ID),
237                 .driver_info = (kernel_ulong_t)&palm_os_4_probe },
238         { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_NZ90V_ID),
239                 .driver_info = (kernel_ulong_t)&palm_os_4_probe },
240         { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_TJ25_ID),
241                 .driver_info = (kernel_ulong_t)&palm_os_4_probe },
242         { USB_DEVICE(SAMSUNG_VENDOR_ID, SAMSUNG_SCH_I330_ID), 
243                 .driver_info = (kernel_ulong_t)&palm_os_4_probe },
244         { USB_DEVICE(SAMSUNG_VENDOR_ID, SAMSUNG_SPH_I500_ID), 
245                 .driver_info = (kernel_ulong_t)&palm_os_4_probe },
246         { USB_DEVICE(GARMIN_VENDOR_ID, GARMIN_IQUE_3600_ID), 
247                 .driver_info = (kernel_ulong_t)&palm_os_4_probe },
248         { USB_DEVICE(ACEECA_VENDOR_ID, ACEECA_MEZ1000_ID),
249                 .driver_info = (kernel_ulong_t)&palm_os_4_probe },
250         { USB_DEVICE(KYOCERA_VENDOR_ID, KYOCERA_7135_ID),
251                 .driver_info = (kernel_ulong_t)&palm_os_4_probe },
252         { },                                    /* optional parameter entry */
253         { }                                     /* Terminating entry */
254 };
255
256 static struct usb_device_id clie_id_5_table [] = {
257         { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_UX50_ID),
258                 .driver_info = (kernel_ulong_t)&palm_os_4_probe },
259         { },                                    /* optional parameter entry */
260         { }                                     /* Terminating entry */
261 };
262
263 static struct usb_device_id clie_id_3_5_table [] = {
264         { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_3_5_ID) },
265         { }                                     /* Terminating entry */
266 };
267
268 static struct usb_device_id id_table_combined [] = {
269         { USB_DEVICE(HANDSPRING_VENDOR_ID, HANDSPRING_VISOR_ID) },
270         { USB_DEVICE(HANDSPRING_VENDOR_ID, HANDSPRING_TREO_ID) },
271         { USB_DEVICE(HANDSPRING_VENDOR_ID, HANDSPRING_TREO600_ID) },
272         { USB_DEVICE(PALM_VENDOR_ID, PALM_M500_ID) },
273         { USB_DEVICE(PALM_VENDOR_ID, PALM_M505_ID) },
274         { USB_DEVICE(PALM_VENDOR_ID, PALM_M515_ID) },
275         { USB_DEVICE(PALM_VENDOR_ID, PALM_I705_ID) },
276         { USB_DEVICE(PALM_VENDOR_ID, PALM_M100_ID) },
277         { USB_DEVICE(PALM_VENDOR_ID, PALM_M125_ID) },
278         { USB_DEVICE(PALM_VENDOR_ID, PALM_M130_ID) },
279         { USB_DEVICE(PALM_VENDOR_ID, PALM_TUNGSTEN_T_ID) },
280         { USB_DEVICE(PALM_VENDOR_ID, PALM_TUNGSTEN_Z_ID) },
281         { USB_DEVICE(PALM_VENDOR_ID, PALM_ZIRE31_ID) },
282         { USB_DEVICE(PALM_VENDOR_ID, PALM_ZIRE_ID) },
283         { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_3_5_ID) },
284         { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_4_0_ID) },
285         { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_S360_ID) },
286         { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_4_1_ID) },
287         { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_NX60_ID) },
288         { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_NZ90V_ID) },
289         { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_UX50_ID) },
290         { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_TJ25_ID) },
291         { USB_DEVICE(SAMSUNG_VENDOR_ID, SAMSUNG_SCH_I330_ID) },
292         { USB_DEVICE(SAMSUNG_VENDOR_ID, SAMSUNG_SPH_I500_ID) },
293         { USB_DEVICE(GARMIN_VENDOR_ID, GARMIN_IQUE_3600_ID) },
294         { USB_DEVICE(ACEECA_VENDOR_ID, ACEECA_MEZ1000_ID) },
295         { USB_DEVICE(KYOCERA_VENDOR_ID, KYOCERA_7135_ID) },
296         { },                                    /* optional parameter entry */
297         { }                                     /* Terminating entry */
298 };
299
300 MODULE_DEVICE_TABLE (usb, id_table_combined);
301
302 static struct usb_driver visor_driver = {
303         .owner =        THIS_MODULE,
304         .name =         "visor",
305         .probe =        usb_serial_probe,
306         .disconnect =   usb_serial_disconnect,
307         .id_table =     id_table_combined,
308 };
309
310 /* All of the device info needed for the Handspring Visor, and Palm 4.0 devices */
311 static struct usb_serial_device_type handspring_device = {
312         .owner =                THIS_MODULE,
313         .name =                 "Handspring Visor / Palm OS",
314         .short_name =           "visor",
315         .id_table =             id_table,
316         .num_interrupt_in =     NUM_DONT_CARE,
317         .num_bulk_in =          2,
318         .num_bulk_out =         2,
319         .num_ports =            2,
320         .open =                 visor_open,
321         .close =                visor_close,
322         .throttle =             visor_throttle,
323         .unthrottle =           visor_unthrottle,
324         .attach =               treo_attach,
325         .probe =                visor_probe,
326         .calc_num_ports =       visor_calc_num_ports,
327         .shutdown =             visor_shutdown,
328         .ioctl =                visor_ioctl,
329         .set_termios =          visor_set_termios,
330         .write =                visor_write,
331         .write_room =           visor_write_room,
332         .chars_in_buffer =      visor_chars_in_buffer,
333         .write_bulk_callback =  visor_write_bulk_callback,
334         .read_bulk_callback =   visor_read_bulk_callback,
335         .read_int_callback =    visor_read_int_callback,
336 };
337
338 /* All of the device info needed for the Clie UX50, TH55 Palm 5.0 devices */
339 static struct usb_serial_device_type clie_5_device = {
340         .owner =                THIS_MODULE,
341         .name =                 "Sony Clie 5.0",
342         .short_name =           "clie_5",
343         .id_table =             clie_id_5_table,
344         .num_interrupt_in =     NUM_DONT_CARE,
345         .num_bulk_in =          2,
346         .num_bulk_out =         2,
347         .num_ports =            2,
348         .open =                 visor_open,
349         .close =                visor_close,
350         .throttle =             visor_throttle,
351         .unthrottle =           visor_unthrottle,
352         .attach =               clie_5_attach,
353         .probe =                visor_probe,
354         .calc_num_ports =       visor_calc_num_ports,
355         .shutdown =             visor_shutdown,
356         .ioctl =                visor_ioctl,
357         .set_termios =          visor_set_termios,
358         .write =                visor_write,
359         .write_room =           visor_write_room,
360         .chars_in_buffer =      visor_chars_in_buffer,
361         .write_bulk_callback =  visor_write_bulk_callback,
362         .read_bulk_callback =   visor_read_bulk_callback,
363         .read_int_callback =    visor_read_int_callback,
364 };
365
366 /* device info for the Sony Clie OS version 3.5 */
367 static struct usb_serial_device_type clie_3_5_device = {
368         .owner =                THIS_MODULE,
369         .name =                 "Sony Clie 3.5",
370         .short_name =           "clie_3.5",
371         .id_table =             clie_id_3_5_table,
372         .num_interrupt_in =     0,
373         .num_bulk_in =          1,
374         .num_bulk_out =         1,
375         .num_ports =            1,
376         .open =                 visor_open,
377         .close =                visor_close,
378         .throttle =             visor_throttle,
379         .unthrottle =           visor_unthrottle,
380         .attach =               clie_3_5_startup,
381         .ioctl =                visor_ioctl,
382         .set_termios =          visor_set_termios,
383         .write =                visor_write,
384         .write_room =           visor_write_room,
385         .chars_in_buffer =      visor_chars_in_buffer,
386         .write_bulk_callback =  visor_write_bulk_callback,
387         .read_bulk_callback =   visor_read_bulk_callback,
388 };
389
390
391 static int bytes_in;
392 static int bytes_out;
393
394
395 /******************************************************************************
396  * Handspring Visor specific driver functions
397  ******************************************************************************/
398 static int visor_open (struct usb_serial_port *port, struct file *filp)
399 {
400         struct usb_serial *serial = port->serial;
401         int result = 0;
402
403         dbg("%s - port %d", __FUNCTION__, port->number);
404
405         if (!port->read_urb) {
406                 /* this is needed for some brain dead Sony devices */
407                 dev_err(&port->dev, "Device lied about number of ports, please use a lower one.\n");
408                 return -ENODEV;
409         }
410
411         bytes_in = 0;
412         bytes_out = 0;
413
414         /*
415          * Force low_latency on so that our tty_push actually forces the data
416          * through, otherwise it is scheduled, and with high data rates (like
417          * with OHCI) data can get lost.
418          */
419         if (port->tty)
420                 port->tty->low_latency = 1;
421
422         /* Start reading from the device */
423         usb_fill_bulk_urb (port->read_urb, serial->dev,
424                            usb_rcvbulkpipe (serial->dev, 
425                                             port->bulk_in_endpointAddress),
426                            port->read_urb->transfer_buffer,
427                            port->read_urb->transfer_buffer_length,
428                            visor_read_bulk_callback, port);
429         result = usb_submit_urb(port->read_urb, GFP_KERNEL);
430         if (result) {
431                 dev_err(&port->dev, "%s - failed submitting read urb, error %d\n",
432                         __FUNCTION__, result);
433                 goto exit;
434         }
435         
436         if (port->interrupt_in_urb) {
437                 dbg("%s - adding interrupt input for treo", __FUNCTION__);
438                 result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
439                 if (result)
440                         dev_err(&port->dev, "%s - failed submitting interrupt urb, error %d\n",
441                                 __FUNCTION__, result);
442         }
443 exit:   
444         return result;
445 }
446
447
448 static void visor_close (struct usb_serial_port *port, struct file * filp)
449 {
450         unsigned char *transfer_buffer;
451
452         dbg("%s - port %d", __FUNCTION__, port->number);
453                          
454         /* shutdown our urbs */
455         usb_unlink_urb (port->read_urb);
456         if (port->interrupt_in_urb)
457                 usb_unlink_urb (port->interrupt_in_urb);
458
459         /* Try to send shutdown message, if the device is gone, this will just fail. */
460         transfer_buffer =  kmalloc (0x12, GFP_KERNEL);
461         if (transfer_buffer) {
462                 usb_control_msg (port->serial->dev,
463                                  usb_rcvctrlpipe(port->serial->dev, 0),
464                                  VISOR_CLOSE_NOTIFICATION, 0xc2,
465                                  0x0000, 0x0000, 
466                                  transfer_buffer, 0x12, 300);
467                 kfree (transfer_buffer);
468         }
469
470         /* Uncomment the following line if you want to see some statistics in your syslog */
471         /* dev_info (&port->dev, "Bytes In = %d  Bytes Out = %d\n", bytes_in, bytes_out); */
472 }
473
474
475 static int visor_write (struct usb_serial_port *port, int from_user, const unsigned char *buf, int count)
476 {
477         struct usb_serial *serial = port->serial;
478         struct urb *urb;
479         unsigned char *buffer;
480         int status;
481
482         dbg("%s - port %d", __FUNCTION__, port->number);
483
484         buffer = kmalloc (count, GFP_ATOMIC);
485         if (!buffer) {
486                 dev_err(&port->dev, "out of memory\n");
487                 return -ENOMEM;
488         }
489
490         urb = usb_alloc_urb(0, GFP_ATOMIC);
491         if (!urb) {
492                 dev_err(&port->dev, "no more free urbs\n");
493                 kfree (buffer);
494                 return -ENOMEM;
495         }
496
497         if (from_user) {
498                 if (copy_from_user (buffer, buf, count)) {
499                         kfree (buffer);
500                         usb_free_urb (urb);
501                         return -EFAULT;
502                 }
503         } else {
504                 memcpy (buffer, buf, count);
505         }
506
507         usb_serial_debug_data (__FILE__, __FUNCTION__, count, buffer);
508
509         usb_fill_bulk_urb (urb, serial->dev,
510                            usb_sndbulkpipe (serial->dev,
511                                             port->bulk_out_endpointAddress),
512                            buffer, count, 
513                            visor_write_bulk_callback, port);
514
515         /* send it down the pipe */
516         status = usb_submit_urb(urb, GFP_ATOMIC);
517         if (status) {
518                 dev_err(&port->dev, "%s - usb_submit_urb(write bulk) failed with status = %d\n",
519                         __FUNCTION__, status);
520                 count = status;
521                 kfree (buffer);
522         } else {
523                 bytes_out += count;
524         }
525
526         /* we are done with this urb, so let the host driver
527          * really free it when it is finished with it */
528         usb_free_urb (urb);
529
530         return count;
531 }
532
533
534 static int visor_write_room (struct usb_serial_port *port)
535 {
536         dbg("%s - port %d", __FUNCTION__, port->number);
537
538         /*
539          * We really can take anything the user throws at us
540          * but let's pick a nice big number to tell the tty
541          * layer that we have lots of free space
542          */
543         return 2048;
544 }
545
546
547 static int visor_chars_in_buffer (struct usb_serial_port *port)
548 {
549         dbg("%s - port %d", __FUNCTION__, port->number);
550
551         /* 
552          * We can't really account for how much data we
553          * have sent out, but hasn't made it through to the
554          * device, so just tell the tty layer that everything
555          * is flushed.
556          */
557         return 0;
558 }
559
560
561 static void visor_write_bulk_callback (struct urb *urb, struct pt_regs *regs)
562 {
563         struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
564
565         /* free up the transfer buffer, as usb_free_urb() does not do this */
566         kfree (urb->transfer_buffer);
567
568         dbg("%s - port %d", __FUNCTION__, port->number);
569         
570         if (urb->status)
571                 dbg("%s - nonzero write bulk status received: %d",
572                     __FUNCTION__, urb->status);
573
574         schedule_work(&port->work);
575 }
576
577
578 static void visor_read_bulk_callback (struct urb *urb, struct pt_regs *regs)
579 {
580         struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
581         struct tty_struct *tty;
582         unsigned char *data = urb->transfer_buffer;
583         int i;
584         int result;
585
586         dbg("%s - port %d", __FUNCTION__, port->number);
587
588         if (urb->status) {
589                 dbg("%s - nonzero read bulk status received: %d", __FUNCTION__, urb->status);
590                 return;
591         }
592
593         usb_serial_debug_data (__FILE__, __FUNCTION__, urb->actual_length, data);
594
595         tty = port->tty;
596         if (tty && urb->actual_length) {
597                 for (i = 0; i < urb->actual_length ; ++i) {
598                         /* if we insert more than TTY_FLIPBUF_SIZE characters, we drop them. */
599                         if(tty->flip.count >= TTY_FLIPBUF_SIZE) {
600                                 tty_flip_buffer_push(tty);
601                         }
602                         /* this doesn't actually push the data through unless tty->low_latency is set */
603                         tty_insert_flip_char(tty, data[i], 0);
604                 }
605                 tty_flip_buffer_push(tty);
606         }
607         bytes_in += urb->actual_length;
608
609         /* Continue trying to always read  */
610         usb_fill_bulk_urb (port->read_urb, port->serial->dev,
611                            usb_rcvbulkpipe(port->serial->dev,
612                                            port->bulk_in_endpointAddress),
613                            port->read_urb->transfer_buffer,
614                            port->read_urb->transfer_buffer_length,
615                            visor_read_bulk_callback, port);
616         result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
617         if (result)
618                 dev_err(&port->dev, "%s - failed resubmitting read urb, error %d\n", __FUNCTION__, result);
619         return;
620 }
621
622 static void visor_read_int_callback (struct urb *urb, struct pt_regs *regs)
623 {
624         int result;
625
626         switch (urb->status) {
627         case 0:
628                 /* success */
629                 break;
630         case -ECONNRESET:
631         case -ENOENT:
632         case -ESHUTDOWN:
633                 /* this urb is terminated, clean up */
634                 dbg("%s - urb shutting down with status: %d",
635                     __FUNCTION__, urb->status);
636                 return;
637         default:
638                 dbg("%s - nonzero urb status received: %d",
639                     __FUNCTION__, urb->status);
640                 goto exit;
641         }
642
643         /*
644          * This information is still unknown what it can be used for.
645          * If anyone has an idea, please let the author know...
646          *
647          * Rumor has it this endpoint is used to notify when data
648          * is ready to be read from the bulk ones.
649          */
650         usb_serial_debug_data (__FILE__, __FUNCTION__, urb->actual_length,
651                                urb->transfer_buffer);
652
653 exit:
654         result = usb_submit_urb (urb, GFP_ATOMIC);
655         if (result)
656                 dev_err(&urb->dev->dev, "%s - Error %d submitting interrupt urb\n",
657                         __FUNCTION__, result);
658 }
659
660 static void visor_throttle (struct usb_serial_port *port)
661 {
662         dbg("%s - port %d", __FUNCTION__, port->number);
663         usb_unlink_urb (port->read_urb);
664 }
665
666
667 static void visor_unthrottle (struct usb_serial_port *port)
668 {
669         int result;
670
671         dbg("%s - port %d", __FUNCTION__, port->number);
672
673         port->read_urb->dev = port->serial->dev;
674         result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
675         if (result)
676                 dev_err(&port->dev, "%s - failed submitting read urb, error %d\n", __FUNCTION__, result);
677 }
678
679 static int palm_os_3_probe (struct usb_serial *serial, const struct usb_device_id *id)
680 {
681         struct device *dev = &serial->dev->dev;
682         struct visor_connection_info *connection_info;
683         unsigned char *transfer_buffer;
684         char *string;
685         int retval = 0;
686         int i;
687         int num_ports = 0;
688
689         dbg("%s", __FUNCTION__);
690
691         transfer_buffer = kmalloc (sizeof (*connection_info), GFP_KERNEL);
692         if (!transfer_buffer) {
693                 dev_err(dev, "%s - kmalloc(%Zd) failed.\n", __FUNCTION__,
694                         sizeof(*connection_info));
695                 return -ENOMEM;
696         }
697
698         /* send a get connection info request */
699         retval = usb_control_msg (serial->dev,
700                                   usb_rcvctrlpipe(serial->dev, 0),
701                                   VISOR_GET_CONNECTION_INFORMATION,
702                                   0xc2, 0x0000, 0x0000, transfer_buffer,
703                                   sizeof(*connection_info), 300);
704         if (retval < 0) {
705                 dev_err(dev, "%s - error %d getting connection information\n",
706                         __FUNCTION__, retval);
707                 goto exit;
708         }
709
710         if (retval == sizeof(*connection_info)) {
711                 connection_info = (struct visor_connection_info *)transfer_buffer;
712
713                 le16_to_cpus(&connection_info->num_ports);
714                 num_ports = connection_info->num_ports;
715
716                 for (i = 0; i < num_ports; ++i) {
717                         switch (connection_info->connections[i].port_function_id) {
718                                 case VISOR_FUNCTION_GENERIC:
719                                         string = "Generic";
720                                         break;
721                                 case VISOR_FUNCTION_DEBUGGER:
722                                         string = "Debugger";
723                                         break;
724                                 case VISOR_FUNCTION_HOTSYNC:
725                                         string = "HotSync";
726                                         break;
727                                 case VISOR_FUNCTION_CONSOLE:
728                                         string = "Console";
729                                         break;
730                                 case VISOR_FUNCTION_REMOTE_FILE_SYS:
731                                         string = "Remote File System";
732                                         break;
733                                 default:
734                                         string = "unknown";
735                                         break;
736                         }
737                         dev_info(dev, "%s: port %d, is for %s use\n",
738                                 serial->type->name,
739                                 connection_info->connections[i].port, string);
740                 }
741         }
742         /*
743         * Handle devices that report invalid stuff here.
744         */
745         if (num_ports == 0 || num_ports > 2) {
746                 dev_warn (dev, "%s: No valid connect info available\n",
747                         serial->type->name);
748                 num_ports = 2;
749         }
750   
751         dev_info(dev, "%s: Number of ports: %d\n", serial->type->name,
752                 num_ports);
753
754         /*
755          * save off our num_ports info so that we can use it in the
756          * calc_num_ports callback
757          */
758         usb_set_serial_data(serial, (void *)(long)num_ports);
759
760         /* ask for the number of bytes available, but ignore the response as it is broken */
761         retval = usb_control_msg (serial->dev,
762                                   usb_rcvctrlpipe(serial->dev, 0),
763                                   VISOR_REQUEST_BYTES_AVAILABLE,
764                                   0xc2, 0x0000, 0x0005, transfer_buffer,
765                                   0x02, 300);
766         if (retval < 0)
767                 dev_err(dev, "%s - error %d getting bytes available request\n",
768                         __FUNCTION__, retval);
769         retval = 0;
770
771 exit:
772         kfree (transfer_buffer);
773
774         return retval;
775 }
776
777 static int palm_os_4_probe (struct usb_serial *serial, const struct usb_device_id *id)
778 {
779         struct device *dev = &serial->dev->dev;
780         struct palm_ext_connection_info *connection_info;
781         unsigned char *transfer_buffer;
782         int retval;
783
784         dbg("%s", __FUNCTION__);
785
786         transfer_buffer =  kmalloc (sizeof (*connection_info), GFP_KERNEL);
787         if (!transfer_buffer) {
788                 dev_err(dev, "%s - kmalloc(%Zd) failed.\n", __FUNCTION__,
789                         sizeof(*connection_info));
790                 return -ENOMEM;
791         }
792
793         retval = usb_control_msg (serial->dev,
794                                   usb_rcvctrlpipe(serial->dev, 0), 
795                                   PALM_GET_EXT_CONNECTION_INFORMATION,
796                                   0xc2, 0x0000, 0x0000, transfer_buffer,
797                                   sizeof (*connection_info), 300);
798         if (retval < 0)
799                 dev_err(dev, "%s - error %d getting connection info\n",
800                         __FUNCTION__, retval);
801         else
802                 usb_serial_debug_data (__FILE__, __FUNCTION__, retval, transfer_buffer);
803
804         kfree (transfer_buffer);
805         return 0;
806 }
807
808
809 static int visor_probe (struct usb_serial *serial, const struct usb_device_id *id)
810 {
811         int retval = 0;
812         int (*startup) (struct usb_serial *serial, const struct usb_device_id *id);
813
814         dbg("%s", __FUNCTION__);
815
816         if (serial->dev->actconfig->desc.bConfigurationValue != 1) {
817                 err("active config #%d != 1 ??",
818                         serial->dev->actconfig->desc.bConfigurationValue);
819                 return -ENODEV;
820         }
821
822         if (id->driver_info) {
823                 startup = (void *)id->driver_info;
824                 retval = startup(serial, id);
825         }
826
827         return retval;
828 }
829
830 static int visor_calc_num_ports (struct usb_serial *serial)
831 {
832         int num_ports = (int)(long)(usb_get_serial_data(serial));
833
834         if (num_ports)
835                 usb_set_serial_data(serial, NULL);
836
837         return num_ports;
838 }
839
840 static int clie_3_5_startup (struct usb_serial *serial)
841 {
842         struct device *dev = &serial->dev->dev;
843         int result;
844         u8 data;
845
846         dbg("%s", __FUNCTION__);
847
848         /*
849          * Note that PEG-300 series devices expect the following two calls.
850          */
851
852         /* get the config number */
853         result = usb_control_msg (serial->dev, usb_rcvctrlpipe(serial->dev, 0),
854                                   USB_REQ_GET_CONFIGURATION, USB_DIR_IN,
855                                   0, 0, &data, 1, HZ * 3);
856         if (result < 0) {
857                 dev_err(dev, "%s: get config number failed: %d\n", __FUNCTION__, result);
858                 return result;
859         }
860         if (result != 1) {
861                 dev_err(dev, "%s: get config number bad return length: %d\n", __FUNCTION__, result);
862                 return -EIO;
863         }
864
865         /* get the interface number */
866         result = usb_control_msg (serial->dev, usb_rcvctrlpipe(serial->dev, 0),
867                                   USB_REQ_GET_INTERFACE, 
868                                   USB_DIR_IN | USB_RECIP_INTERFACE,
869                                   0, 0, &data, 1, HZ * 3);
870         if (result < 0) {
871                 dev_err(dev, "%s: get interface number failed: %d\n", __FUNCTION__, result);
872                 return result;
873         }
874         if (result != 1) {
875                 dev_err(dev, "%s: get interface number bad return length: %d\n", __FUNCTION__, result);
876                 return -EIO;
877         }
878
879         return 0;
880 }
881  
882 static int treo_attach (struct usb_serial *serial)
883 {
884         struct usb_serial_port *swap_port;
885
886         /* Only do this endpoint hack for the Handspring devices with
887          * interrupt in endpoints, which for now are the Treo devices. */
888         if (!((serial->dev->descriptor.idVendor == HANDSPRING_VENDOR_ID) ||
889               (serial->dev->descriptor.idVendor == KYOCERA_VENDOR_ID)) ||
890             (serial->num_interrupt_in == 0))
891                 return 0;
892
893         dbg("%s", __FUNCTION__);
894
895         /*
896         * It appears that Treos and Kyoceras want to use the 
897         * 1st bulk in endpoint to communicate with the 2nd bulk out endpoint, 
898         * so let's swap the 1st and 2nd bulk in and interrupt endpoints.  
899         * Note that swapping the bulk out endpoints would break lots of 
900         * apps that want to communicate on the second port.
901         */
902 #define COPY_PORT(dest, src)                                            \
903         dest->read_urb = src->read_urb;                                 \
904         dest->bulk_in_endpointAddress = src->bulk_in_endpointAddress;   \
905         dest->bulk_in_buffer = src->bulk_in_buffer;                     \
906         dest->interrupt_in_urb = src->interrupt_in_urb;                 \
907         dest->interrupt_in_endpointAddress = src->interrupt_in_endpointAddress; \
908         dest->interrupt_in_buffer = src->interrupt_in_buffer;
909
910         swap_port = kmalloc(sizeof(*swap_port), GFP_KERNEL);
911         if (!swap_port)
912                 return -ENOMEM;
913         COPY_PORT(swap_port, serial->port[0]);
914         COPY_PORT(serial->port[0], serial->port[1]);
915         COPY_PORT(serial->port[1], swap_port);
916         kfree(swap_port);
917
918         return 0;
919 }
920
921 static int clie_5_attach (struct usb_serial *serial)
922 {
923         dbg("%s", __FUNCTION__);
924
925         /* TH55 registers 2 ports. 
926            Communication in from the UX50/TH55 uses bulk_in_endpointAddress from port 0 
927            Communication out to the UX50/TH55 uses bulk_out_endpointAddress from port 1 
928            
929            Lets do a quick and dirty mapping
930          */
931         
932         /* some sanity check */
933         if (serial->num_ports < 2)
934                 return -1;
935                 
936         /* port 0 now uses the modified endpoint Address */
937         serial->port[0]->bulk_out_endpointAddress = serial->port[1]->bulk_out_endpointAddress;
938
939         return 0;
940 }
941
942 static void visor_shutdown (struct usb_serial *serial)
943 {
944         dbg("%s", __FUNCTION__);
945 }
946
947 static int visor_ioctl (struct usb_serial_port *port, struct file * file, unsigned int cmd, unsigned long arg)
948 {
949         dbg("%s - port %d, cmd 0x%.4x", __FUNCTION__, port->number, cmd);
950
951         return -ENOIOCTLCMD;
952 }
953
954
955 /* This function is all nice and good, but we don't change anything based on it :) */
956 static void visor_set_termios (struct usb_serial_port *port, struct termios *old_termios)
957 {
958         unsigned int cflag;
959
960         dbg("%s - port %d", __FUNCTION__, port->number);
961
962         if ((!port->tty) || (!port->tty->termios)) {
963                 dbg("%s - no tty structures", __FUNCTION__);
964                 return;
965         }
966
967         cflag = port->tty->termios->c_cflag;
968         /* check that they really want us to change something */
969         if (old_termios) {
970                 if ((cflag == old_termios->c_cflag) &&
971                     (RELEVANT_IFLAG(port->tty->termios->c_iflag) == RELEVANT_IFLAG(old_termios->c_iflag))) {
972                         dbg("%s - nothing to change...", __FUNCTION__);
973                         return;
974                 }
975         }
976
977         /* get the byte size */
978         switch (cflag & CSIZE) {
979                 case CS5:       dbg("%s - data bits = 5", __FUNCTION__);   break;
980                 case CS6:       dbg("%s - data bits = 6", __FUNCTION__);   break;
981                 case CS7:       dbg("%s - data bits = 7", __FUNCTION__);   break;
982                 default:
983                 case CS8:       dbg("%s - data bits = 8", __FUNCTION__);   break;
984         }
985         
986         /* determine the parity */
987         if (cflag & PARENB)
988                 if (cflag & PARODD)
989                         dbg("%s - parity = odd", __FUNCTION__);
990                 else
991                         dbg("%s - parity = even", __FUNCTION__);
992         else
993                 dbg("%s - parity = none", __FUNCTION__);
994
995         /* figure out the stop bits requested */
996         if (cflag & CSTOPB)
997                 dbg("%s - stop bits = 2", __FUNCTION__);
998         else
999                 dbg("%s - stop bits = 1", __FUNCTION__);
1000
1001         
1002         /* figure out the flow control settings */
1003         if (cflag & CRTSCTS)
1004                 dbg("%s - RTS/CTS is enabled", __FUNCTION__);
1005         else
1006                 dbg("%s - RTS/CTS is disabled", __FUNCTION__);
1007         
1008         /* determine software flow control */
1009         if (I_IXOFF(port->tty))
1010                 dbg("%s - XON/XOFF is enabled, XON = %2x, XOFF = %2x",
1011                     __FUNCTION__, START_CHAR(port->tty), STOP_CHAR(port->tty));
1012         else
1013                 dbg("%s - XON/XOFF is disabled", __FUNCTION__);
1014
1015         /* get the baud rate wanted */
1016         dbg("%s - baud rate = %d", __FUNCTION__, tty_get_baud_rate(port->tty));
1017
1018         return;
1019 }
1020
1021
1022 static int __init visor_init (void)
1023 {
1024         int i, retval;
1025         /* Only if parameters were passed to us */
1026         if ((vendor>0) && (product>0)) {
1027                 struct usb_device_id usb_dev_temp[]=
1028                         {{USB_DEVICE(vendor, product),
1029                         .driver_info = (kernel_ulong_t)&palm_os_4_probe }};
1030
1031                 /* Find the last entry in id_table */
1032                 for (i=0; ; i++) {
1033                         if (id_table[i].idVendor==0) {
1034                                 id_table[i] = usb_dev_temp[0];
1035                                 break;
1036                         }
1037                 }
1038                 /* Find the last entry in id_table_combined */
1039                 for (i=0; ; i++) {
1040                         if (id_table_combined[i].idVendor==0) {
1041                                 id_table_combined[i] = usb_dev_temp[0];
1042                                 break;
1043                         }
1044                 }
1045                 info("Untested USB device specified at time of module insertion");
1046                 info("Warning: This is not guaranteed to work");
1047                 info("Using a newer kernel is preferred to this method");
1048                 info("Adding Palm OS protocol 4.x support for unknown device: 0x%x/0x%x",
1049                         vendor, product);
1050         }
1051         retval = usb_serial_register(&handspring_device);
1052         if (retval)
1053                 goto failed_handspring_register;
1054         retval = usb_serial_register(&clie_3_5_device);
1055         if (retval)
1056                 goto failed_clie_3_5_register;
1057         retval = usb_serial_register(&clie_5_device);
1058         if (retval)
1059                 goto failed_clie_5_register;
1060         retval = usb_register(&visor_driver);
1061         if (retval) 
1062                 goto failed_usb_register;
1063         info(DRIVER_DESC " " DRIVER_VERSION);
1064
1065         return 0;
1066 failed_usb_register:
1067         usb_serial_deregister(&clie_5_device);
1068 failed_clie_5_register:
1069         usb_serial_deregister(&clie_3_5_device);
1070 failed_clie_3_5_register:
1071         usb_serial_deregister(&handspring_device);
1072 failed_handspring_register:
1073         return retval;
1074 }
1075
1076
1077 static void __exit visor_exit (void)
1078 {
1079         usb_deregister (&visor_driver);
1080         usb_serial_deregister (&handspring_device);
1081         usb_serial_deregister (&clie_3_5_device);
1082         usb_serial_deregister (&clie_5_device);
1083 }
1084
1085
1086 module_init(visor_init);
1087 module_exit(visor_exit);
1088
1089 MODULE_AUTHOR( DRIVER_AUTHOR );
1090 MODULE_DESCRIPTION( DRIVER_DESC );
1091 MODULE_LICENSE("GPL");
1092
1093 module_param(debug, bool, S_IRUGO | S_IWUSR);
1094 MODULE_PARM_DESC(debug, "Debug enabled or not");
1095
1096 module_param(vendor, ushort, 0);
1097 MODULE_PARM_DESC(vendor, "User specified vendor ID");
1098 module_param(product, ushort, 0);
1099 MODULE_PARM_DESC(product, "User specified product ID");