v2.4.9.9 -> v2.4.9.10
[opensuse:kernel.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 - 2001
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  * (08/30/2001) gkh
16  *      Added support for the Clie devices, both the 3.5 and 4.0 os versions.
17  *      Many thanks to Daniel Burke, and Bryan Payne for helping with this.
18  *
19  * (08/23/2001) gkh
20  *      fixed a few potential bugs pointed out by Oliver Neukum.
21  *
22  * (05/30/2001) gkh
23  *      switched from using spinlock to a semaphore, which fixes lots of problems.
24  *
25  * (05/28/2000) gkh
26  *      Added initial support for the Palm m500 and Palm m505 devices.
27  *
28  * (04/08/2001) gb
29  *      Identify version on module load.
30  *
31  * (01/21/2000) gkh
32  *      Added write_room and chars_in_buffer, as they were previously using the
33  *      generic driver versions which is all wrong now that we are using an urb
34  *      pool.  Thanks to Wolfgang Grandegger for pointing this out to me.
35  *      Removed count assignment in the write function, which was not needed anymore
36  *      either.  Thanks to Al Borchers for pointing this out.
37  *
38  * (12/12/2000) gkh
39  *      Moved MOD_DEC to end of visor_close to be nicer, as the final write 
40  *      message can sleep.
41  * 
42  * (11/12/2000) gkh
43  *      Fixed bug with data being dropped on the floor by forcing tty->low_latency
44  *      to be on.  Hopefully this fixes the OHCI issue!
45  *
46  * (11/01/2000) Adam J. Richter
47  *      usb_device_id table support
48  * 
49  * (10/05/2000) gkh
50  *      Fixed bug with urb->dev not being set properly, now that the usb
51  *      core needs it.
52  * 
53  * (09/11/2000) gkh
54  *      Got rid of always calling kmalloc for every urb we wrote out to the
55  *      device.
56  *      Added visor_read_callback so we can keep track of bytes in and out for
57  *      those people who like to know the speed of their device.
58  *      Removed DEBUG #ifdefs with call to usb_serial_debug_data
59  *
60  * (09/06/2000) gkh
61  *      Fixed oops in visor_exit.  Need to uncomment usb_unlink_urb call _after_
62  *      the host controller drivers set urb->dev = NULL when the urb is finished.
63  *
64  * (08/28/2000) gkh
65  *      Added locks for SMP safeness.
66  *
67  * (08/08/2000) gkh
68  *      Fixed endian problem in visor_startup.
69  *      Fixed MOD_INC and MOD_DEC logic and the ability to open a port more 
70  *      than once.
71  * 
72  * (07/23/2000) gkh
73  *      Added pool of write urbs to speed up transfers to the visor.
74  * 
75  * (07/19/2000) gkh
76  *      Added module_init and module_exit functions to handle the fact that this
77  *      driver is a loadable module now.
78  *
79  * (07/03/2000) gkh
80  *      Added visor_set_ioctl and visor_set_termios functions (they don't do much
81  *      of anything, but are good for debugging.)
82  * 
83  * (06/25/2000) gkh
84  *      Fixed bug in visor_unthrottle that should help with the disconnect in PPP
85  *      bug that people have been reporting.
86  *
87  * (06/23/2000) gkh
88  *      Cleaned up debugging statements in a quest to find UHCI timeout bug.
89  *
90  * (04/27/2000) Ryan VanderBijl
91  *      Fixed memory leak in visor_close
92  *
93  * (03/26/2000) gkh
94  *      Split driver up into device specific pieces.
95  * 
96  */
97
98 #include <linux/config.h>
99 #include <linux/kernel.h>
100 #include <linux/sched.h>
101 #include <linux/signal.h>
102 #include <linux/errno.h>
103 #include <linux/poll.h>
104 #include <linux/init.h>
105 #include <linux/slab.h>
106 #include <linux/fcntl.h>
107 #include <linux/tty.h>
108 #include <linux/tty_driver.h>
109 #include <linux/tty_flip.h>
110 #include <linux/module.h>
111 #include <linux/spinlock.h>
112 #include <linux/usb.h>
113
114 #ifdef CONFIG_USB_SERIAL_DEBUG
115         static int debug = 1;
116 #else
117         static int debug;
118 #endif
119
120 #include "usb-serial.h"
121 #include "visor.h"
122
123 /*
124  * Version Information
125  */
126 #define DRIVER_VERSION "v1.4"
127 #define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com>"
128 #define DRIVER_DESC "USB HandSpring Visor, Palm m50x, Sony Clie driver"
129
130 /* function prototypes for a handspring visor */
131 static int  visor_open          (struct usb_serial_port *port, struct file *filp);
132 static void visor_close         (struct usb_serial_port *port, struct file *filp);
133 static int  visor_write         (struct usb_serial_port *port, int from_user, const unsigned char *buf, int count);
134 static int  visor_write_room            (struct usb_serial_port *port);
135 static int  visor_chars_in_buffer       (struct usb_serial_port *port);
136 static void visor_throttle      (struct usb_serial_port *port);
137 static void visor_unthrottle    (struct usb_serial_port *port);
138 static int  visor_startup       (struct usb_serial *serial);
139 static void visor_shutdown      (struct usb_serial *serial);
140 static int  visor_ioctl         (struct usb_serial_port *port, struct file * file, unsigned int cmd, unsigned long arg);
141 static void visor_set_termios   (struct usb_serial_port *port, struct termios *old_termios);
142 static void visor_write_bulk_callback   (struct urb *urb);
143 static void visor_read_bulk_callback    (struct urb *urb);
144
145
146 static __devinitdata struct usb_device_id visor_id_table [] = {
147         { USB_DEVICE(HANDSPRING_VENDOR_ID, HANDSPRING_VISOR_ID) },
148         { }                                     /* Terminating entry */
149 };
150
151 static __devinitdata struct usb_device_id palm_m500_id_table [] = {
152         { USB_DEVICE(PALM_VENDOR_ID, PALM_M500_ID) },
153         { }                                     /* Terminating entry */
154 };
155
156 static __devinitdata struct usb_device_id palm_m505_id_table [] = {
157         { USB_DEVICE(PALM_VENDOR_ID, PALM_M505_ID) },
158         { }                                     /* Terminating entry */
159 };
160
161 static __devinitdata struct usb_device_id clie_id_3_5_table [] = {
162         { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_3_5_ID) },
163         { }                                     /* Terminating entry */
164 };
165
166 static __devinitdata struct usb_device_id clie_id_4_0_table [] = {
167         { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_4_0_ID) },
168         { }                                     /* Terminating entry */
169 };
170
171 static __devinitdata struct usb_device_id id_table [] = {
172         { USB_DEVICE(HANDSPRING_VENDOR_ID, HANDSPRING_VISOR_ID) },
173         { USB_DEVICE(PALM_VENDOR_ID, PALM_M500_ID) },
174         { USB_DEVICE(PALM_VENDOR_ID, PALM_M505_ID) },
175         { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_3_5_ID) },
176         { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_4_0_ID) },
177         { }                                     /* Terminating entry */
178 };
179
180 MODULE_DEVICE_TABLE (usb, id_table);
181
182
183
184 /* All of the device info needed for the Handspring Visor */
185 struct usb_serial_device_type handspring_device = {
186         name:                   "Handspring Visor",
187         id_table:               visor_id_table,
188         needs_interrupt_in:     MUST_HAVE_NOT,          /* this device must not have an interrupt in endpoint */
189         needs_bulk_in:          MUST_HAVE,              /* this device must have a bulk in endpoint */
190         needs_bulk_out:         MUST_HAVE,              /* this device must have a bulk out endpoint */
191         num_interrupt_in:       0,
192         num_bulk_in:            2,
193         num_bulk_out:           2,
194         num_ports:              2,
195         open:                   visor_open,
196         close:                  visor_close,
197         throttle:               visor_throttle,
198         unthrottle:             visor_unthrottle,
199         startup:                visor_startup,
200         shutdown:               visor_shutdown,
201         ioctl:                  visor_ioctl,
202         set_termios:            visor_set_termios,
203         write:                  visor_write,
204         write_room:             visor_write_room,
205         chars_in_buffer:        visor_chars_in_buffer,
206         write_bulk_callback:    visor_write_bulk_callback,
207         read_bulk_callback:     visor_read_bulk_callback,
208 };
209
210 /* device info for the Palm M500 */
211 struct usb_serial_device_type palm_m500_device = {
212         name:                   "Palm M500",
213         id_table:               palm_m500_id_table,
214         needs_interrupt_in:     MUST_HAVE_NOT,          /* this device must not have an interrupt in endpoint */
215         needs_bulk_in:          MUST_HAVE,              /* this device must have a bulk in endpoint */
216         needs_bulk_out:         MUST_HAVE,              /* this device must have a bulk out endpoint */
217         num_interrupt_in:       0,
218         num_bulk_in:            2,
219         num_bulk_out:           2,
220         num_ports:              2,
221         open:                   visor_open,
222         close:                  visor_close,
223         throttle:               visor_throttle,
224         unthrottle:             visor_unthrottle,
225         startup:                visor_startup,
226         shutdown:               visor_shutdown,
227         ioctl:                  visor_ioctl,
228         set_termios:            visor_set_termios,
229         write:                  visor_write,
230         write_room:             visor_write_room,
231         chars_in_buffer:        visor_chars_in_buffer,
232         write_bulk_callback:    visor_write_bulk_callback,
233         read_bulk_callback:     visor_read_bulk_callback,
234 };
235
236 /* device info for the Palm M505 */
237 struct usb_serial_device_type palm_m505_device = {
238         name:                   "Palm M505",
239         id_table:               palm_m505_id_table,
240         needs_interrupt_in:     MUST_HAVE_NOT,          /* this device must not have an interrupt in endpoint */
241         needs_bulk_in:          MUST_HAVE,              /* this device must have a bulk in endpoint */
242         needs_bulk_out:         MUST_HAVE,              /* this device must have a bulk out endpoint */
243         num_interrupt_in:       0,
244         num_bulk_in:            2,
245         num_bulk_out:           2,
246         num_ports:              2,
247         open:                   visor_open,
248         close:                  visor_close,
249         throttle:               visor_throttle,
250         unthrottle:             visor_unthrottle,
251         startup:                visor_startup,
252         shutdown:               visor_shutdown,
253         ioctl:                  visor_ioctl,
254         set_termios:            visor_set_termios,
255         write:                  visor_write,
256         write_room:             visor_write_room,
257         chars_in_buffer:        visor_chars_in_buffer,
258         write_bulk_callback:    visor_write_bulk_callback,
259         read_bulk_callback:     visor_read_bulk_callback,
260 };
261
262 /* device info for the Sony Clie OS version 3.5 */
263 static struct usb_serial_device_type clie_3_5_device = {
264         name:                   "Sony Clie 3.5",
265         id_table:               clie_id_3_5_table,
266         needs_interrupt_in:     MUST_HAVE_NOT,          /* this device must not have an interrupt in endpoint */
267         needs_bulk_in:          MUST_HAVE,              /* this device must have a bulk in endpoint */
268         needs_bulk_out:         MUST_HAVE,              /* this device must have a bulk out endpoint */
269         num_interrupt_in:       0,
270         num_bulk_in:            1,
271         num_bulk_out:           1,
272         num_ports:              1,
273         open:                   visor_open,
274         close:                  visor_close,
275         throttle:               visor_throttle,
276         unthrottle:             visor_unthrottle,
277         ioctl:                  visor_ioctl,
278         set_termios:            visor_set_termios,
279         write:                  visor_write,
280         write_room:             visor_write_room,
281         chars_in_buffer:        visor_chars_in_buffer,
282         write_bulk_callback:    visor_write_bulk_callback,
283         read_bulk_callback:     visor_read_bulk_callback,
284 };
285
286 /* device info for the Sony Clie OS version 4.0 */
287 static struct usb_serial_device_type clie_4_0_device = {
288         name:                   "Sony Clie 4.0",
289         id_table:               clie_id_4_0_table,
290         needs_interrupt_in:     MUST_HAVE_NOT,          /* this device must not have an interrupt in endpoint */
291         needs_bulk_in:          MUST_HAVE,              /* this device must have a bulk in endpoint */
292         needs_bulk_out:         MUST_HAVE,              /* this device must have a bulk out endpoint */
293         num_interrupt_in:       0,
294         num_bulk_in:            2,
295         num_bulk_out:           2,
296         num_ports:              2,
297         open:                   visor_open,
298         close:                  visor_close,
299         throttle:               visor_throttle,
300         unthrottle:             visor_unthrottle,
301         startup:                visor_startup,
302         shutdown:               visor_shutdown,
303         ioctl:                  visor_ioctl,
304         set_termios:            visor_set_termios,
305         write:                  visor_write,
306         write_room:             visor_write_room,
307         chars_in_buffer:        visor_chars_in_buffer,
308         write_bulk_callback:    visor_write_bulk_callback,
309         read_bulk_callback:     visor_read_bulk_callback,
310 };
311
312 #define NUM_URBS                        24
313 #define URB_TRANSFER_BUFFER_SIZE        768
314 static struct urb       *write_urb_pool[NUM_URBS];
315 static spinlock_t       write_urb_pool_lock;
316 static int              bytes_in;
317 static int              bytes_out;
318
319
320 /******************************************************************************
321  * Handspring Visor specific driver functions
322  ******************************************************************************/
323 static int visor_open (struct usb_serial_port *port, struct file *filp)
324 {
325         struct usb_serial *serial = port->serial;
326         int result = 0;
327
328         if (port_paranoia_check (port, __FUNCTION__))
329                 return -ENODEV;
330         
331         dbg(__FUNCTION__ " - port %d", port->number);
332
333         down (&port->sem);
334         
335         ++port->open_count;
336         MOD_INC_USE_COUNT;
337         
338         if (!port->active) {
339                 port->active = 1;
340                 bytes_in = 0;
341                 bytes_out = 0;
342
343                 /* force low_latency on so that our tty_push actually forces the data through, 
344                    otherwise it is scheduled, and with high data rates (like with OHCI) data
345                    can get lost. */
346                 port->tty->low_latency = 1;
347                 
348                 /* Start reading from the device */
349                 FILL_BULK_URB(port->read_urb, serial->dev, 
350                               usb_rcvbulkpipe(serial->dev, port->bulk_in_endpointAddress),
351                               port->read_urb->transfer_buffer, port->read_urb->transfer_buffer_length,
352                               visor_read_bulk_callback, port);
353                 result = usb_submit_urb(port->read_urb);
354                 if (result)
355                         err(__FUNCTION__ " - failed submitting read urb, error %d", result);
356         }
357         
358         up (&port->sem);
359         
360         return result;
361 }
362
363
364 static void visor_close (struct usb_serial_port *port, struct file * filp)
365 {
366         struct usb_serial *serial;
367         unsigned char *transfer_buffer;
368
369         if (port_paranoia_check (port, __FUNCTION__))
370                 return;
371         
372         dbg(__FUNCTION__ " - port %d", port->number);
373                          
374         serial = get_usb_serial (port, __FUNCTION__);
375         if (!serial)
376                 return;
377         
378         down (&port->sem);
379
380         --port->open_count;
381
382         if (port->open_count <= 0) {
383                 transfer_buffer =  kmalloc (0x12, GFP_KERNEL);
384                 if (!transfer_buffer) {
385                         err(__FUNCTION__ " - kmalloc(%d) failed.", 0x12);
386                 } else {
387                         /* send a shutdown message to the device */
388                         usb_control_msg (serial->dev, usb_rcvctrlpipe(serial->dev, 0), VISOR_CLOSE_NOTIFICATION,
389                                         0xc2, 0x0000, 0x0000, transfer_buffer, 0x12, 300);
390                         kfree (transfer_buffer);
391                 }
392
393                 /* shutdown our bulk read */
394                 usb_unlink_urb (port->read_urb);
395                 port->active = 0;
396                 port->open_count = 0;
397
398         }
399         up (&port->sem);
400
401         /* Uncomment the following line if you want to see some statistics in your syslog */
402         /* info ("Bytes In = %d  Bytes Out = %d", bytes_in, bytes_out); */
403
404         MOD_DEC_USE_COUNT;
405 }
406
407
408 static int visor_write (struct usb_serial_port *port, int from_user, const unsigned char *buf, int count)
409 {
410         struct usb_serial *serial = port->serial;
411         struct urb *urb;
412         const unsigned char *current_position = buf;
413         unsigned long flags;
414         int status;
415         int i;
416         int bytes_sent = 0;
417         int transfer_size;
418
419         dbg(__FUNCTION__ " - port %d", port->number);
420
421         while (count > 0) {
422                 /* try to find a free urb in our list of them */
423                 urb = NULL;
424                 spin_lock_irqsave (&write_urb_pool_lock, flags);
425                 for (i = 0; i < NUM_URBS; ++i) {
426                         if (write_urb_pool[i]->status != -EINPROGRESS) {
427                                 urb = write_urb_pool[i];
428                                 break;
429                         }
430                 }
431                 spin_unlock_irqrestore (&write_urb_pool_lock, flags);
432                 if (urb == NULL) {
433                         dbg (__FUNCTION__ " - no more free urbs");
434                         goto exit;
435                 }
436                 if (urb->transfer_buffer == NULL) {
437                         urb->transfer_buffer = kmalloc (URB_TRANSFER_BUFFER_SIZE, GFP_KERNEL);
438                         if (urb->transfer_buffer == NULL) {
439                                 err(__FUNCTION__" no more kernel memory...");
440                                 goto exit;
441                         }
442                 }
443                 
444                 transfer_size = min (count, URB_TRANSFER_BUFFER_SIZE);
445                 if (from_user) {
446                         if (copy_from_user (urb->transfer_buffer, current_position, transfer_size)) {
447                                 bytes_sent = -EFAULT;
448                                 break;
449                         }
450                 } else {
451                         memcpy (urb->transfer_buffer, current_position, transfer_size);
452                 }
453
454                 usb_serial_debug_data (__FILE__, __FUNCTION__, transfer_size, urb->transfer_buffer);
455
456                 /* build up our urb */
457                 FILL_BULK_URB (urb, serial->dev, usb_sndbulkpipe(serial->dev, port->bulk_out_endpointAddress), 
458                                 urb->transfer_buffer, transfer_size, visor_write_bulk_callback, port);
459                 urb->transfer_flags |= USB_QUEUE_BULK;
460
461                 /* send it down the pipe */
462                 status = usb_submit_urb(urb);
463                 if (status) {
464                         err(__FUNCTION__ " - usb_submit_urb(write bulk) failed with status = %d", status);
465                         bytes_sent = status;
466                         break;
467                 }
468
469                 current_position += transfer_size;
470                 bytes_sent += transfer_size;
471                 count -= transfer_size;
472                 bytes_out += transfer_size;
473         }
474
475 exit:
476         return bytes_sent;
477
478
479
480 static int visor_write_room (struct usb_serial_port *port)
481 {
482         unsigned long flags;
483         int i;
484         int room = 0;
485
486         dbg(__FUNCTION__ " - port %d", port->number);
487         
488         spin_lock_irqsave (&write_urb_pool_lock, flags);
489
490         for (i = 0; i < NUM_URBS; ++i) {
491                 if (write_urb_pool[i]->status != -EINPROGRESS) {
492                         room += URB_TRANSFER_BUFFER_SIZE;
493                 }
494         }
495         
496         spin_unlock_irqrestore (&write_urb_pool_lock, flags);
497         
498         dbg(__FUNCTION__ " - returns %d", room);
499         return (room);
500 }
501
502
503 static int visor_chars_in_buffer (struct usb_serial_port *port)
504 {
505         unsigned long flags;
506         int i;
507         int chars = 0;
508
509         dbg(__FUNCTION__ " - port %d", port->number);
510         
511         spin_lock_irqsave (&write_urb_pool_lock, flags);
512
513         for (i = 0; i < NUM_URBS; ++i) {
514                 if (write_urb_pool[i]->status == -EINPROGRESS) {
515                         chars += URB_TRANSFER_BUFFER_SIZE;
516                 }
517         }
518         
519         spin_unlock_irqrestore (&write_urb_pool_lock, flags);
520
521         dbg (__FUNCTION__ " - returns %d", chars);
522         return (chars);
523 }
524
525
526 static void visor_write_bulk_callback (struct urb *urb)
527 {
528         struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
529
530         if (port_paranoia_check (port, __FUNCTION__))
531                 return;
532         
533         dbg(__FUNCTION__ " - port %d", port->number);
534         
535         if (urb->status) {
536                 dbg(__FUNCTION__ " - nonzero write bulk status received: %d", urb->status);
537                 return;
538         }
539
540         queue_task(&port->tqueue, &tq_immediate);
541         mark_bh(IMMEDIATE_BH);
542         
543         return;
544 }
545
546
547 static void visor_read_bulk_callback (struct urb *urb)
548 {
549         struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
550         struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);
551         struct tty_struct *tty;
552         unsigned char *data = urb->transfer_buffer;
553         int i;
554         int result;
555
556         if (port_paranoia_check (port, __FUNCTION__))
557                 return;
558
559         dbg(__FUNCTION__ " - port %d", port->number);
560
561         if (!serial) {
562                 dbg(__FUNCTION__ " - bad serial pointer, exiting");
563                 return;
564         }
565
566         if (urb->status) {
567                 dbg(__FUNCTION__ " - nonzero read bulk status received: %d", urb->status);
568                 return;
569         }
570
571         usb_serial_debug_data (__FILE__, __FUNCTION__, urb->actual_length, data);
572
573         tty = port->tty;
574         if (urb->actual_length) {
575                 for (i = 0; i < urb->actual_length ; ++i) {
576                         /* if we insert more than TTY_FLIPBUF_SIZE characters, we drop them. */
577                         if(tty->flip.count >= TTY_FLIPBUF_SIZE) {
578                                 tty_flip_buffer_push(tty);
579                         }
580                         /* this doesn't actually push the data through unless tty->low_latency is set */
581                         tty_insert_flip_char(tty, data[i], 0);
582                 }
583                 tty_flip_buffer_push(tty);
584                 bytes_in += urb->actual_length;
585         }
586
587         /* Continue trying to always read  */
588         FILL_BULK_URB(port->read_urb, serial->dev, 
589                       usb_rcvbulkpipe(serial->dev, port->bulk_in_endpointAddress),
590                       port->read_urb->transfer_buffer, port->read_urb->transfer_buffer_length,
591                       visor_read_bulk_callback, port);
592         result = usb_submit_urb(port->read_urb);
593         if (result)
594                 err(__FUNCTION__ " - failed resubmitting read urb, error %d", result);
595         return;
596 }
597
598
599 static void visor_throttle (struct usb_serial_port *port)
600 {
601
602         dbg(__FUNCTION__ " - port %d", port->number);
603
604         down (&port->sem);
605
606         usb_unlink_urb (port->read_urb);
607
608         up (&port->sem);
609
610         return;
611 }
612
613
614 static void visor_unthrottle (struct usb_serial_port *port)
615 {
616         int result;
617
618         dbg(__FUNCTION__ " - port %d", port->number);
619
620         down (&port->sem);
621
622         port->read_urb->dev = port->serial->dev;
623         result = usb_submit_urb(port->read_urb);
624         if (result)
625                 err(__FUNCTION__ " - failed submitting read urb, error %d", result);
626
627         up (&port->sem);
628
629         return;
630 }
631
632
633 static int  visor_startup (struct usb_serial *serial)
634 {
635         int response;
636         int i;
637         unsigned char *transfer_buffer =  kmalloc (256, GFP_KERNEL);
638
639         if (!transfer_buffer) {
640                 err(__FUNCTION__ " - kmalloc(%d) failed.", 256);
641                 return -ENOMEM;
642         }
643
644         dbg(__FUNCTION__);
645
646         dbg(__FUNCTION__ " - Set config to 1");
647         usb_set_configuration (serial->dev, 1);
648
649         /* send a get connection info request */
650         response = usb_control_msg (serial->dev, usb_rcvctrlpipe(serial->dev, 0), VISOR_GET_CONNECTION_INFORMATION,
651                                         0xc2, 0x0000, 0x0000, transfer_buffer, 0x12, 300);
652         if (response < 0) {
653                 err(__FUNCTION__ " - error getting connection information");
654         } else {
655                 struct visor_connection_info *connection_info = (struct visor_connection_info *)transfer_buffer;
656                 char *string;
657
658                 le16_to_cpus(&connection_info->num_ports);
659                 info("%s: Number of ports: %d", serial->type->name, connection_info->num_ports);
660                 for (i = 0; i < connection_info->num_ports; ++i) {
661                         switch (connection_info->connections[i].port_function_id) {
662                                 case VISOR_FUNCTION_GENERIC:
663                                         string = "Generic";
664                                         break;
665                                 case VISOR_FUNCTION_DEBUGGER:
666                                         string = "Debugger";
667                                         break;
668                                 case VISOR_FUNCTION_HOTSYNC:
669                                         string = "HotSync";
670                                         break;
671                                 case VISOR_FUNCTION_CONSOLE:
672                                         string = "Console";
673                                         break;
674                                 case VISOR_FUNCTION_REMOTE_FILE_SYS:
675                                         string = "Remote File System";
676                                         break;
677                                 default:
678                                         string = "unknown";
679                                         break;  
680                         }
681                         info("%s: port %d, is for %s use and is bound to ttyUSB%d", serial->type->name, connection_info->connections[i].port, string, serial->minor + i);
682                 }
683         }
684
685         if ((serial->dev->descriptor.idVendor == PALM_VENDOR_ID) ||
686             (serial->dev->descriptor.idVendor == SONY_VENDOR_ID)) {
687                 /* Palm OS 4.0 Hack */
688                 response = usb_control_msg (serial->dev, usb_rcvctrlpipe(serial->dev, 0), 
689                                             PALM_GET_SOME_UNKNOWN_INFORMATION,
690                                             0xc2, 0x0000, 0x0000, transfer_buffer, 
691                                             0x14, 300);
692                 if (response < 0) {
693                         err(__FUNCTION__ " - error getting first unknown palm command");
694                 } else {
695                         usb_serial_debug_data (__FILE__, __FUNCTION__, 0x14, transfer_buffer);
696                 }
697                 response = usb_control_msg (serial->dev, usb_rcvctrlpipe(serial->dev, 0), 
698                                             PALM_GET_SOME_UNKNOWN_INFORMATION,
699                                             0xc2, 0x0000, 0x0000, transfer_buffer, 
700                                             0x14, 300);
701                 if (response < 0) {
702                         err(__FUNCTION__ " - error getting second unknown palm command");
703                 } else {
704                         usb_serial_debug_data (__FILE__, __FUNCTION__, 0x14, transfer_buffer);
705                 }
706         }
707
708         /* ask for the number of bytes available, but ignore the response as it is broken */
709         response = usb_control_msg (serial->dev, usb_rcvctrlpipe(serial->dev, 0), VISOR_REQUEST_BYTES_AVAILABLE,
710                                         0xc2, 0x0000, 0x0005, transfer_buffer, 0x02, 300);
711         if (response < 0) {
712                 err(__FUNCTION__ " - error getting bytes available request");
713         }
714
715         kfree (transfer_buffer);
716
717         /* continue on with initialization */
718         return 0;
719 }
720
721
722 static void visor_shutdown (struct usb_serial *serial)
723 {
724         int i;
725
726         dbg (__FUNCTION__);
727
728         /* stop reads and writes on all ports */
729         for (i=0; i < serial->num_ports; ++i) {
730                 while (serial->port[i].open_count > 0) {
731                         visor_close (&serial->port[i], NULL);
732                 }
733         }
734 }
735
736
737 static int visor_ioctl (struct usb_serial_port *port, struct file * file, unsigned int cmd, unsigned long arg)
738 {
739         dbg(__FUNCTION__ " - port %d, cmd 0x%.4x", port->number, cmd);
740
741         return -ENOIOCTLCMD;
742 }
743
744
745 /* This function is all nice and good, but we don't change anything based on it :) */
746 static void visor_set_termios (struct usb_serial_port *port, struct termios *old_termios)
747 {
748         unsigned int cflag;
749
750         dbg(__FUNCTION__ " - port %d", port->number);
751
752         if ((!port->tty) || (!port->tty->termios)) {
753                 dbg(__FUNCTION__" - no tty structures");
754                 return;
755         }
756
757         cflag = port->tty->termios->c_cflag;
758         /* check that they really want us to change something */
759         if (old_termios) {
760                 if ((cflag == old_termios->c_cflag) &&
761                     (RELEVANT_IFLAG(port->tty->termios->c_iflag) == RELEVANT_IFLAG(old_termios->c_iflag))) {
762                         dbg(__FUNCTION__ " - nothing to change...");
763                         return;
764                 }
765         }
766
767         /* get the byte size */
768         switch (cflag & CSIZE) {
769                 case CS5:       dbg(__FUNCTION__ " - data bits = 5");   break;
770                 case CS6:       dbg(__FUNCTION__ " - data bits = 6");   break;
771                 case CS7:       dbg(__FUNCTION__ " - data bits = 7");   break;
772                 default:
773                 case CS8:       dbg(__FUNCTION__ " - data bits = 8");   break;
774         }
775         
776         /* determine the parity */
777         if (cflag & PARENB)
778                 if (cflag & PARODD)
779                         dbg(__FUNCTION__ " - parity = odd");
780                 else
781                         dbg(__FUNCTION__ " - parity = even");
782         else
783                 dbg(__FUNCTION__ " - parity = none");
784
785         /* figure out the stop bits requested */
786         if (cflag & CSTOPB)
787                 dbg(__FUNCTION__ " - stop bits = 2");
788         else
789                 dbg(__FUNCTION__ " - stop bits = 1");
790
791         
792         /* figure out the flow control settings */
793         if (cflag & CRTSCTS)
794                 dbg(__FUNCTION__ " - RTS/CTS is enabled");
795         else
796                 dbg(__FUNCTION__ " - RTS/CTS is disabled");
797         
798         /* determine software flow control */
799         if (I_IXOFF(port->tty))
800                 dbg(__FUNCTION__ " - XON/XOFF is enabled, XON = %2x, XOFF = %2x", START_CHAR(port->tty), STOP_CHAR(port->tty));
801         else
802                 dbg(__FUNCTION__ " - XON/XOFF is disabled");
803
804         /* get the baud rate wanted */
805         dbg(__FUNCTION__ " - baud rate = %d", tty_get_baud_rate(port->tty));
806
807         return;
808 }
809
810
811 static int __init visor_init (void)
812 {
813         struct urb *urb;
814         int i;
815
816         usb_serial_register (&handspring_device);
817         usb_serial_register (&palm_m500_device);
818         usb_serial_register (&palm_m505_device);
819         usb_serial_register (&clie_3_5_device);
820         usb_serial_register (&clie_4_0_device);
821         
822         /* create our write urb pool and transfer buffers */ 
823         spin_lock_init (&write_urb_pool_lock);
824         for (i = 0; i < NUM_URBS; ++i) {
825                 urb = usb_alloc_urb(0);
826                 write_urb_pool[i] = urb;
827                 if (urb == NULL) {
828                         err("No more urbs???");
829                         continue;
830                 }
831
832                 urb->transfer_buffer = NULL;
833                 urb->transfer_buffer = kmalloc (URB_TRANSFER_BUFFER_SIZE, GFP_KERNEL);
834                 if (!urb->transfer_buffer) {
835                         err (__FUNCTION__ " - out of memory for urb buffers.");
836                         continue;
837                 }
838         }
839
840         info(DRIVER_DESC " " DRIVER_VERSION);
841
842         return 0;
843 }
844
845
846 static void __exit visor_exit (void)
847 {
848         int i;
849         unsigned long flags;
850
851         usb_serial_deregister (&handspring_device);
852         usb_serial_deregister (&palm_m500_device);
853         usb_serial_deregister (&palm_m505_device);
854         usb_serial_deregister (&clie_3_5_device);
855         usb_serial_deregister (&clie_4_0_device);
856
857         spin_lock_irqsave (&write_urb_pool_lock, flags);
858
859         for (i = 0; i < NUM_URBS; ++i) {
860                 if (write_urb_pool[i]) {
861                         /* FIXME - uncomment the following usb_unlink_urb call when
862                          * the host controllers get fixed to set urb->dev = NULL after
863                          * the urb is finished.  Otherwise this call oopses. */
864                         /* usb_unlink_urb(write_urb_pool[i]); */
865                         if (write_urb_pool[i]->transfer_buffer)
866                                 kfree(write_urb_pool[i]->transfer_buffer);
867                         usb_free_urb (write_urb_pool[i]);
868                 }
869         }
870
871         spin_unlock_irqrestore (&write_urb_pool_lock, flags);
872 }
873
874
875 module_init(visor_init);
876 module_exit(visor_exit);
877
878 MODULE_AUTHOR( DRIVER_AUTHOR );
879 MODULE_DESCRIPTION( DRIVER_DESC );
880 MODULE_LICENSE("GPL");
881
882 MODULE_PARM(debug, "i");
883 MODULE_PARM_DESC(debug, "Debug enabled or not");
884