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