USB: visor: fix trivial accounting bug in visor driver
[safe/jmp/linux-2.6] / 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
9  *      modify it under the terms of the GNU General Public License version
10  *      2 as published by the Free Software Foundation.
11  *
12  * See Documentation/usb/usb-serial.txt for more information on using this
13  * driver
14  *
15  */
16
17 #include <linux/kernel.h>
18 #include <linux/errno.h>
19 #include <linux/init.h>
20 #include <linux/slab.h>
21 #include <linux/tty.h>
22 #include <linux/tty_driver.h>
23 #include <linux/tty_flip.h>
24 #include <linux/module.h>
25 #include <linux/moduleparam.h>
26 #include <linux/spinlock.h>
27 #include <linux/uaccess.h>
28 #include <linux/usb.h>
29 #include <linux/usb/serial.h>
30 #include "visor.h"
31
32 /*
33  * Version Information
34  */
35 #define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com>"
36 #define DRIVER_DESC "USB HandSpring Visor / Palm OS driver"
37
38 /* function prototypes for a handspring visor */
39 static int  visor_open(struct tty_struct *tty, struct usb_serial_port *port);
40 static void visor_close(struct usb_serial_port *port);
41 static int  visor_write(struct tty_struct *tty, struct usb_serial_port *port,
42                                         const unsigned char *buf, int count);
43 static int  visor_write_room(struct tty_struct *tty);
44 static void visor_throttle(struct tty_struct *tty);
45 static void visor_unthrottle(struct tty_struct *tty);
46 static int  visor_probe(struct usb_serial *serial,
47                                         const struct usb_device_id *id);
48 static int  visor_calc_num_ports(struct usb_serial *serial);
49 static void visor_release(struct usb_serial *serial);
50 static void visor_write_bulk_callback(struct urb *urb);
51 static void visor_read_bulk_callback(struct urb *urb);
52 static void visor_read_int_callback(struct urb *urb);
53 static int  clie_3_5_startup(struct usb_serial *serial);
54 static int  treo_attach(struct usb_serial *serial);
55 static int clie_5_attach(struct usb_serial *serial);
56 static int palm_os_3_probe(struct usb_serial *serial,
57                                         const struct usb_device_id *id);
58 static int palm_os_4_probe(struct usb_serial *serial,
59                                         const struct usb_device_id *id);
60
61 /* Parameters that may be passed into the module. */
62 static int debug;
63 static __u16 vendor;
64 static __u16 product;
65
66 static struct usb_device_id id_table [] = {
67         { USB_DEVICE(HANDSPRING_VENDOR_ID, HANDSPRING_VISOR_ID),
68                 .driver_info = (kernel_ulong_t)&palm_os_3_probe },
69         { USB_DEVICE(HANDSPRING_VENDOR_ID, HANDSPRING_TREO_ID),
70                 .driver_info = (kernel_ulong_t)&palm_os_4_probe },
71         { USB_DEVICE(HANDSPRING_VENDOR_ID, HANDSPRING_TREO600_ID),
72                 .driver_info = (kernel_ulong_t)&palm_os_4_probe },
73         { USB_DEVICE(GSPDA_VENDOR_ID, GSPDA_XPLORE_M68_ID),
74                 .driver_info = (kernel_ulong_t)&palm_os_4_probe },
75         { USB_DEVICE(PALM_VENDOR_ID, PALM_M500_ID),
76                 .driver_info = (kernel_ulong_t)&palm_os_4_probe },
77         { USB_DEVICE(PALM_VENDOR_ID, PALM_M505_ID),
78                 .driver_info = (kernel_ulong_t)&palm_os_4_probe },
79         { USB_DEVICE(PALM_VENDOR_ID, PALM_M515_ID),
80                 .driver_info = (kernel_ulong_t)&palm_os_4_probe },
81         { USB_DEVICE(PALM_VENDOR_ID, PALM_I705_ID),
82                 .driver_info = (kernel_ulong_t)&palm_os_4_probe },
83         { USB_DEVICE(PALM_VENDOR_ID, PALM_M100_ID),
84                 .driver_info = (kernel_ulong_t)&palm_os_4_probe },
85         { USB_DEVICE(PALM_VENDOR_ID, PALM_M125_ID),
86                 .driver_info = (kernel_ulong_t)&palm_os_4_probe },
87         { USB_DEVICE(PALM_VENDOR_ID, PALM_M130_ID),
88                 .driver_info = (kernel_ulong_t)&palm_os_4_probe },
89         { USB_DEVICE(PALM_VENDOR_ID, PALM_TUNGSTEN_T_ID),
90                 .driver_info = (kernel_ulong_t)&palm_os_4_probe },
91         { USB_DEVICE(PALM_VENDOR_ID, PALM_TREO_650),
92                 .driver_info = (kernel_ulong_t)&palm_os_4_probe },
93         { USB_DEVICE(PALM_VENDOR_ID, PALM_TUNGSTEN_Z_ID),
94                 .driver_info = (kernel_ulong_t)&palm_os_4_probe },
95         { USB_DEVICE(PALM_VENDOR_ID, PALM_ZIRE_ID),
96                 .driver_info = (kernel_ulong_t)&palm_os_4_probe },
97         { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_4_0_ID),
98                 .driver_info = (kernel_ulong_t)&palm_os_4_probe },
99         { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_S360_ID),
100                 .driver_info = (kernel_ulong_t)&palm_os_4_probe },
101         { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_4_1_ID),
102                 .driver_info = (kernel_ulong_t)&palm_os_4_probe },
103         { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_NX60_ID),
104                 .driver_info = (kernel_ulong_t)&palm_os_4_probe },
105         { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_NZ90V_ID),
106                 .driver_info = (kernel_ulong_t)&palm_os_4_probe },
107         { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_TJ25_ID),
108                 .driver_info = (kernel_ulong_t)&palm_os_4_probe },
109         { USB_DEVICE(ACER_VENDOR_ID, ACER_S10_ID),
110                 .driver_info = (kernel_ulong_t)&palm_os_4_probe },
111         { USB_DEVICE(SAMSUNG_VENDOR_ID, SAMSUNG_SCH_I330_ID),
112                 .driver_info = (kernel_ulong_t)&palm_os_4_probe },
113         { USB_DEVICE(SAMSUNG_VENDOR_ID, SAMSUNG_SPH_I500_ID),
114                 .driver_info = (kernel_ulong_t)&palm_os_4_probe },
115         { USB_DEVICE(TAPWAVE_VENDOR_ID, TAPWAVE_ZODIAC_ID),
116                 .driver_info = (kernel_ulong_t)&palm_os_4_probe },
117         { USB_DEVICE(GARMIN_VENDOR_ID, GARMIN_IQUE_3600_ID),
118                 .driver_info = (kernel_ulong_t)&palm_os_4_probe },
119         { USB_DEVICE(ACEECA_VENDOR_ID, ACEECA_MEZ1000_ID),
120                 .driver_info = (kernel_ulong_t)&palm_os_4_probe },
121         { USB_DEVICE(KYOCERA_VENDOR_ID, KYOCERA_7135_ID),
122                 .driver_info = (kernel_ulong_t)&palm_os_4_probe },
123         { USB_DEVICE(FOSSIL_VENDOR_ID, FOSSIL_ABACUS_ID),
124                 .driver_info = (kernel_ulong_t)&palm_os_4_probe },
125         { },                                    /* optional parameter entry */
126         { }                                     /* Terminating entry */
127 };
128
129 static struct usb_device_id clie_id_5_table [] = {
130         { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_UX50_ID),
131                 .driver_info = (kernel_ulong_t)&palm_os_4_probe },
132         { },                                    /* optional parameter entry */
133         { }                                     /* Terminating entry */
134 };
135
136 static struct usb_device_id clie_id_3_5_table [] = {
137         { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_3_5_ID) },
138         { }                                     /* Terminating entry */
139 };
140
141 static struct usb_device_id id_table_combined [] = {
142         { USB_DEVICE(HANDSPRING_VENDOR_ID, HANDSPRING_VISOR_ID) },
143         { USB_DEVICE(HANDSPRING_VENDOR_ID, HANDSPRING_TREO_ID) },
144         { USB_DEVICE(HANDSPRING_VENDOR_ID, HANDSPRING_TREO600_ID) },
145         { USB_DEVICE(GSPDA_VENDOR_ID, GSPDA_XPLORE_M68_ID) },
146         { USB_DEVICE(PALM_VENDOR_ID, PALM_M500_ID) },
147         { USB_DEVICE(PALM_VENDOR_ID, PALM_M505_ID) },
148         { USB_DEVICE(PALM_VENDOR_ID, PALM_M515_ID) },
149         { USB_DEVICE(PALM_VENDOR_ID, PALM_I705_ID) },
150         { USB_DEVICE(PALM_VENDOR_ID, PALM_M100_ID) },
151         { USB_DEVICE(PALM_VENDOR_ID, PALM_M125_ID) },
152         { USB_DEVICE(PALM_VENDOR_ID, PALM_M130_ID) },
153         { USB_DEVICE(PALM_VENDOR_ID, PALM_TUNGSTEN_T_ID) },
154         { USB_DEVICE(PALM_VENDOR_ID, PALM_TREO_650) },
155         { USB_DEVICE(PALM_VENDOR_ID, PALM_TUNGSTEN_Z_ID) },
156         { USB_DEVICE(PALM_VENDOR_ID, PALM_ZIRE_ID) },
157         { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_3_5_ID) },
158         { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_4_0_ID) },
159         { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_S360_ID) },
160         { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_4_1_ID) },
161         { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_NX60_ID) },
162         { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_NZ90V_ID) },
163         { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_UX50_ID) },
164         { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_TJ25_ID) },
165         { USB_DEVICE(SAMSUNG_VENDOR_ID, SAMSUNG_SCH_I330_ID) },
166         { USB_DEVICE(SAMSUNG_VENDOR_ID, SAMSUNG_SPH_I500_ID) },
167         { USB_DEVICE(TAPWAVE_VENDOR_ID, TAPWAVE_ZODIAC_ID) },
168         { USB_DEVICE(GARMIN_VENDOR_ID, GARMIN_IQUE_3600_ID) },
169         { USB_DEVICE(ACEECA_VENDOR_ID, ACEECA_MEZ1000_ID) },
170         { USB_DEVICE(KYOCERA_VENDOR_ID, KYOCERA_7135_ID) },
171         { USB_DEVICE(FOSSIL_VENDOR_ID, FOSSIL_ABACUS_ID) },
172         { },                                    /* optional parameter entry */
173         { }                                     /* Terminating entry */
174 };
175
176 MODULE_DEVICE_TABLE(usb, id_table_combined);
177
178 static struct usb_driver visor_driver = {
179         .name =         "visor",
180         .probe =        usb_serial_probe,
181         .disconnect =   usb_serial_disconnect,
182         .id_table =     id_table_combined,
183         .no_dynamic_id =        1,
184 };
185
186 /* All of the device info needed for the Handspring Visor,
187    and Palm 4.0 devices */
188 static struct usb_serial_driver handspring_device = {
189         .driver = {
190                 .owner =        THIS_MODULE,
191                 .name =         "visor",
192         },
193         .description =          "Handspring Visor / Palm OS",
194         .usb_driver =           &visor_driver,
195         .id_table =             id_table,
196         .num_ports =            2,
197         .open =                 visor_open,
198         .close =                visor_close,
199         .throttle =             visor_throttle,
200         .unthrottle =           visor_unthrottle,
201         .attach =               treo_attach,
202         .probe =                visor_probe,
203         .calc_num_ports =       visor_calc_num_ports,
204         .release =              visor_release,
205         .write =                visor_write,
206         .write_room =           visor_write_room,
207         .write_bulk_callback =  visor_write_bulk_callback,
208         .read_bulk_callback =   visor_read_bulk_callback,
209         .read_int_callback =    visor_read_int_callback,
210 };
211
212 /* All of the device info needed for the Clie UX50, TH55 Palm 5.0 devices */
213 static struct usb_serial_driver clie_5_device = {
214         .driver = {
215                 .owner =        THIS_MODULE,
216                 .name =         "clie_5",
217         },
218         .description =          "Sony Clie 5.0",
219         .usb_driver =           &visor_driver,
220         .id_table =             clie_id_5_table,
221         .num_ports =            2,
222         .open =                 visor_open,
223         .close =                visor_close,
224         .throttle =             visor_throttle,
225         .unthrottle =           visor_unthrottle,
226         .attach =               clie_5_attach,
227         .probe =                visor_probe,
228         .calc_num_ports =       visor_calc_num_ports,
229         .release =              visor_release,
230         .write =                visor_write,
231         .write_room =           visor_write_room,
232         .write_bulk_callback =  visor_write_bulk_callback,
233         .read_bulk_callback =   visor_read_bulk_callback,
234         .read_int_callback =    visor_read_int_callback,
235 };
236
237 /* device info for the Sony Clie OS version 3.5 */
238 static struct usb_serial_driver clie_3_5_device = {
239         .driver = {
240                 .owner =        THIS_MODULE,
241                 .name =         "clie_3.5",
242         },
243         .description =          "Sony Clie 3.5",
244         .usb_driver =           &visor_driver,
245         .id_table =             clie_id_3_5_table,
246         .num_ports =            1,
247         .open =                 visor_open,
248         .close =                visor_close,
249         .throttle =             visor_throttle,
250         .unthrottle =           visor_unthrottle,
251         .attach =               clie_3_5_startup,
252         .write =                visor_write,
253         .write_room =           visor_write_room,
254         .write_bulk_callback =  visor_write_bulk_callback,
255         .read_bulk_callback =   visor_read_bulk_callback,
256 };
257
258 struct visor_private {
259         spinlock_t lock;
260         int bytes_in;
261         int bytes_out;
262         int outstanding_urbs;
263         unsigned char throttled;
264         unsigned char actually_throttled;
265 };
266
267 /* number of outstanding urbs to prevent userspace DoS from happening */
268 #define URB_UPPER_LIMIT 42
269
270 static int stats;
271
272 /******************************************************************************
273  * Handspring Visor specific driver functions
274  ******************************************************************************/
275 static int visor_open(struct tty_struct *tty, struct usb_serial_port *port)
276 {
277         struct usb_serial *serial = port->serial;
278         struct visor_private *priv = usb_get_serial_port_data(port);
279         unsigned long flags;
280         int result = 0;
281
282         dbg("%s - port %d", __func__, port->number);
283
284         if (!port->read_urb) {
285                 /* this is needed for some brain dead Sony devices */
286                 dev_err(&port->dev, "Device lied about number of ports, please use a lower one.\n");
287                 return -ENODEV;
288         }
289
290         spin_lock_irqsave(&priv->lock, flags);
291         priv->bytes_in = 0;
292         priv->bytes_out = 0;
293         priv->throttled = 0;
294         spin_unlock_irqrestore(&priv->lock, flags);
295
296         /* Start reading from the device */
297         usb_fill_bulk_urb(port->read_urb, serial->dev,
298                            usb_rcvbulkpipe(serial->dev,
299                                             port->bulk_in_endpointAddress),
300                            port->read_urb->transfer_buffer,
301                            port->read_urb->transfer_buffer_length,
302                            visor_read_bulk_callback, port);
303         result = usb_submit_urb(port->read_urb, GFP_KERNEL);
304         if (result) {
305                 dev_err(&port->dev,
306                         "%s - failed submitting read urb, error %d\n",
307                                                         __func__, result);
308                 goto exit;
309         }
310
311         if (port->interrupt_in_urb) {
312                 dbg("%s - adding interrupt input for treo", __func__);
313                 result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
314                 if (result)
315                         dev_err(&port->dev,
316                             "%s - failed submitting interrupt urb, error %d\n",
317                                                         __func__, result);
318         }
319 exit:
320         return result;
321 }
322
323
324 static void visor_close(struct usb_serial_port *port)
325 {
326         struct visor_private *priv = usb_get_serial_port_data(port);
327         unsigned char *transfer_buffer;
328
329         dbg("%s - port %d", __func__, port->number);
330
331         /* shutdown our urbs */
332         usb_kill_urb(port->read_urb);
333         usb_kill_urb(port->interrupt_in_urb);
334
335         mutex_lock(&port->serial->disc_mutex);
336         if (!port->serial->disconnected) {
337                 /* Try to send shutdown message, unless the device is gone */
338                 transfer_buffer =  kmalloc(0x12, GFP_KERNEL);
339                 if (transfer_buffer) {
340                         usb_control_msg(port->serial->dev,
341                                          usb_rcvctrlpipe(port->serial->dev, 0),
342                                          VISOR_CLOSE_NOTIFICATION, 0xc2,
343                                          0x0000, 0x0000,
344                                          transfer_buffer, 0x12, 300);
345                         kfree(transfer_buffer);
346                 }
347         }
348         mutex_unlock(&port->serial->disc_mutex);
349
350         if (stats)
351                 dev_info(&port->dev, "Bytes In = %d  Bytes Out = %d\n",
352                          priv->bytes_in, priv->bytes_out);
353 }
354
355
356 static int visor_write(struct tty_struct *tty, struct usb_serial_port *port,
357                                         const unsigned char *buf, int count)
358 {
359         struct visor_private *priv = usb_get_serial_port_data(port);
360         struct usb_serial *serial = port->serial;
361         struct urb *urb;
362         unsigned char *buffer;
363         unsigned long flags;
364         int status;
365
366         dbg("%s - port %d", __func__, port->number);
367
368         spin_lock_irqsave(&priv->lock, flags);
369         if (priv->outstanding_urbs > URB_UPPER_LIMIT) {
370                 spin_unlock_irqrestore(&priv->lock, flags);
371                 dbg("%s - write limit hit\n", __func__);
372                 return 0;
373         }
374         priv->outstanding_urbs++;
375         spin_unlock_irqrestore(&priv->lock, flags);
376
377         buffer = kmalloc(count, GFP_ATOMIC);
378         if (!buffer) {
379                 dev_err(&port->dev, "out of memory\n");
380                 count = -ENOMEM;
381                 goto error_no_buffer;
382         }
383
384         urb = usb_alloc_urb(0, GFP_ATOMIC);
385         if (!urb) {
386                 dev_err(&port->dev, "no more free urbs\n");
387                 count = -ENOMEM;
388                 goto error_no_urb;
389         }
390
391         memcpy(buffer, buf, count);
392
393         usb_serial_debug_data(debug, &port->dev, __func__, count, buffer);
394
395         usb_fill_bulk_urb(urb, serial->dev,
396                            usb_sndbulkpipe(serial->dev,
397                                             port->bulk_out_endpointAddress),
398                            buffer, count,
399                            visor_write_bulk_callback, port);
400
401         /* send it down the pipe */
402         status = usb_submit_urb(urb, GFP_ATOMIC);
403         if (status) {
404                 dev_err(&port->dev,
405                    "%s - usb_submit_urb(write bulk) failed with status = %d\n",
406                                                         __func__, status);
407                 count = status;
408                 goto error;
409         } else {
410                 spin_lock_irqsave(&priv->lock, flags);
411                 priv->bytes_out += count;
412                 spin_unlock_irqrestore(&priv->lock, flags);
413         }
414
415         /* we are done with this urb, so let the host driver
416          * really free it when it is finished with it */
417         usb_free_urb(urb);
418
419         return count;
420 error:
421         usb_free_urb(urb);
422 error_no_urb:
423         kfree(buffer);
424 error_no_buffer:
425         spin_lock_irqsave(&priv->lock, flags);
426         --priv->outstanding_urbs;
427         spin_unlock_irqrestore(&priv->lock, flags);
428         return count;
429 }
430
431
432 static int visor_write_room(struct tty_struct *tty)
433 {
434         struct usb_serial_port *port = tty->driver_data;
435         struct visor_private *priv = usb_get_serial_port_data(port);
436         unsigned long flags;
437
438         dbg("%s - port %d", __func__, port->number);
439
440         /*
441          * We really can take anything the user throws at us
442          * but let's pick a nice big number to tell the tty
443          * layer that we have lots of free space, unless we don't.
444          */
445
446         spin_lock_irqsave(&priv->lock, flags);
447         if (priv->outstanding_urbs > URB_UPPER_LIMIT * 2 / 3) {
448                 spin_unlock_irqrestore(&priv->lock, flags);
449                 dbg("%s - write limit hit\n", __func__);
450                 return 0;
451         }
452         spin_unlock_irqrestore(&priv->lock, flags);
453
454         return 2048;
455 }
456
457
458 static void visor_write_bulk_callback(struct urb *urb)
459 {
460         struct usb_serial_port *port = urb->context;
461         struct visor_private *priv = usb_get_serial_port_data(port);
462         int status = urb->status;
463         unsigned long flags;
464
465         /* free up the transfer buffer, as usb_free_urb() does not do this */
466         kfree(urb->transfer_buffer);
467
468         dbg("%s - port %d", __func__, port->number);
469
470         if (status)
471                 dbg("%s - nonzero write bulk status received: %d",
472                     __func__, status);
473
474         spin_lock_irqsave(&priv->lock, flags);
475         --priv->outstanding_urbs;
476         spin_unlock_irqrestore(&priv->lock, flags);
477
478         usb_serial_port_softint(port);
479 }
480
481
482 static void visor_read_bulk_callback(struct urb *urb)
483 {
484         struct usb_serial_port *port = urb->context;
485         struct visor_private *priv = usb_get_serial_port_data(port);
486         unsigned char *data = urb->transfer_buffer;
487         int status = urb->status;
488         struct tty_struct *tty;
489         int result;
490         int available_room = 0;
491
492         dbg("%s - port %d", __func__, port->number);
493
494         if (status) {
495                 dbg("%s - nonzero read bulk status received: %d",
496                     __func__, status);
497                 return;
498         }
499
500         usb_serial_debug_data(debug, &port->dev, __func__,
501                                                 urb->actual_length, data);
502
503         if (urb->actual_length) {
504                 tty = tty_port_tty_get(&port->port);
505                 if (tty) {
506                         available_room = tty_buffer_request_room(tty,
507                                                         urb->actual_length);
508                         if (available_room) {
509                                 tty_insert_flip_string(tty, data,
510                                                         available_room);
511                                 tty_flip_buffer_push(tty);
512                         }
513                         tty_kref_put(tty);
514                 }
515                 spin_lock(&priv->lock);
516                 if (tty)
517                         priv->bytes_in += available_room;
518
519         } else {
520                 spin_lock(&priv->lock);
521         }
522
523         /* Continue trying to always read if we should */
524         if (!priv->throttled) {
525                 usb_fill_bulk_urb(port->read_urb, port->serial->dev,
526                                    usb_rcvbulkpipe(port->serial->dev,
527                                            port->bulk_in_endpointAddress),
528                                    port->read_urb->transfer_buffer,
529                                    port->read_urb->transfer_buffer_length,
530                                    visor_read_bulk_callback, port);
531                 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
532                 if (result)
533                         dev_err(&port->dev,
534                             "%s - failed resubmitting read urb, error %d\n",
535                                                         __func__, result);
536         } else
537                 priv->actually_throttled = 1;
538         spin_unlock(&priv->lock);
539 }
540
541 static void visor_read_int_callback(struct urb *urb)
542 {
543         struct usb_serial_port *port = urb->context;
544         int status = urb->status;
545         int result;
546
547         switch (status) {
548         case 0:
549                 /* success */
550                 break;
551         case -ECONNRESET:
552         case -ENOENT:
553         case -ESHUTDOWN:
554                 /* this urb is terminated, clean up */
555                 dbg("%s - urb shutting down with status: %d",
556                     __func__, status);
557                 return;
558         default:
559                 dbg("%s - nonzero urb status received: %d",
560                     __func__, status);
561                 goto exit;
562         }
563
564         /*
565          * This information is still unknown what it can be used for.
566          * If anyone has an idea, please let the author know...
567          *
568          * Rumor has it this endpoint is used to notify when data
569          * is ready to be read from the bulk ones.
570          */
571         usb_serial_debug_data(debug, &port->dev, __func__,
572                               urb->actual_length, urb->transfer_buffer);
573
574 exit:
575         result = usb_submit_urb(urb, GFP_ATOMIC);
576         if (result)
577                 dev_err(&urb->dev->dev,
578                                 "%s - Error %d submitting interrupt urb\n",
579                                                         __func__, result);
580 }
581
582 static void visor_throttle(struct tty_struct *tty)
583 {
584         struct usb_serial_port *port = tty->driver_data;
585         struct visor_private *priv = usb_get_serial_port_data(port);
586         unsigned long flags;
587
588         dbg("%s - port %d", __func__, port->number);
589         spin_lock_irqsave(&priv->lock, flags);
590         priv->throttled = 1;
591         spin_unlock_irqrestore(&priv->lock, flags);
592 }
593
594
595 static void visor_unthrottle(struct tty_struct *tty)
596 {
597         struct usb_serial_port *port = tty->driver_data;
598         struct visor_private *priv = usb_get_serial_port_data(port);
599         unsigned long flags;
600         int result;
601
602         dbg("%s - port %d", __func__, port->number);
603         spin_lock_irqsave(&priv->lock, flags);
604         priv->throttled = 0;
605         priv->actually_throttled = 0;
606         spin_unlock_irqrestore(&priv->lock, flags);
607
608         port->read_urb->dev = port->serial->dev;
609         result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
610         if (result)
611                 dev_err(&port->dev,
612                         "%s - failed submitting read urb, error %d\n",
613                                                         __func__, result);
614 }
615
616 static int palm_os_3_probe(struct usb_serial *serial,
617                                                 const struct usb_device_id *id)
618 {
619         struct device *dev = &serial->dev->dev;
620         struct visor_connection_info *connection_info;
621         unsigned char *transfer_buffer;
622         char *string;
623         int retval = 0;
624         int i;
625         int num_ports = 0;
626
627         dbg("%s", __func__);
628
629         transfer_buffer = kmalloc(sizeof(*connection_info), GFP_KERNEL);
630         if (!transfer_buffer) {
631                 dev_err(dev, "%s - kmalloc(%Zd) failed.\n", __func__,
632                         sizeof(*connection_info));
633                 return -ENOMEM;
634         }
635
636         /* send a get connection info request */
637         retval = usb_control_msg(serial->dev,
638                                   usb_rcvctrlpipe(serial->dev, 0),
639                                   VISOR_GET_CONNECTION_INFORMATION,
640                                   0xc2, 0x0000, 0x0000, transfer_buffer,
641                                   sizeof(*connection_info), 300);
642         if (retval < 0) {
643                 dev_err(dev, "%s - error %d getting connection information\n",
644                         __func__, retval);
645                 goto exit;
646         }
647
648         if (retval == sizeof(*connection_info)) {
649                         connection_info = (struct visor_connection_info *)
650                                                         transfer_buffer;
651
652                 num_ports = le16_to_cpu(connection_info->num_ports);
653                 for (i = 0; i < num_ports; ++i) {
654                         switch (
655                            connection_info->connections[i].port_function_id) {
656                         case VISOR_FUNCTION_GENERIC:
657                                 string = "Generic";
658                                 break;
659                         case VISOR_FUNCTION_DEBUGGER:
660                                 string = "Debugger";
661                                 break;
662                         case VISOR_FUNCTION_HOTSYNC:
663                                 string = "HotSync";
664                                 break;
665                         case VISOR_FUNCTION_CONSOLE:
666                                 string = "Console";
667                                 break;
668                         case VISOR_FUNCTION_REMOTE_FILE_SYS:
669                                 string = "Remote File System";
670                                 break;
671                         default:
672                                 string = "unknown";
673                                 break;
674                         }
675                         dev_info(dev, "%s: port %d, is for %s use\n",
676                                 serial->type->description,
677                                 connection_info->connections[i].port, string);
678                 }
679         }
680         /*
681         * Handle devices that report invalid stuff here.
682         */
683         if (num_ports == 0 || num_ports > 2) {
684                 dev_warn(dev, "%s: No valid connect info available\n",
685                         serial->type->description);
686                 num_ports = 2;
687         }
688
689         dev_info(dev, "%s: Number of ports: %d\n", serial->type->description,
690                 num_ports);
691
692         /*
693          * save off our num_ports info so that we can use it in the
694          * calc_num_ports callback
695          */
696         usb_set_serial_data(serial, (void *)(long)num_ports);
697
698         /* ask for the number of bytes available, but ignore the
699            response as it is broken */
700         retval = usb_control_msg(serial->dev,
701                                   usb_rcvctrlpipe(serial->dev, 0),
702                                   VISOR_REQUEST_BYTES_AVAILABLE,
703                                   0xc2, 0x0000, 0x0005, transfer_buffer,
704                                   0x02, 300);
705         if (retval < 0)
706                 dev_err(dev, "%s - error %d getting bytes available request\n",
707                         __func__, retval);
708         retval = 0;
709
710 exit:
711         kfree(transfer_buffer);
712
713         return retval;
714 }
715
716 static int palm_os_4_probe(struct usb_serial *serial,
717                                                 const struct usb_device_id *id)
718 {
719         struct device *dev = &serial->dev->dev;
720         struct palm_ext_connection_info *connection_info;
721         unsigned char *transfer_buffer;
722         int retval;
723
724         dbg("%s", __func__);
725
726         transfer_buffer =  kmalloc(sizeof(*connection_info), GFP_KERNEL);
727         if (!transfer_buffer) {
728                 dev_err(dev, "%s - kmalloc(%Zd) failed.\n", __func__,
729                         sizeof(*connection_info));
730                 return -ENOMEM;
731         }
732
733         retval = usb_control_msg(serial->dev,
734                                   usb_rcvctrlpipe(serial->dev, 0),
735                                   PALM_GET_EXT_CONNECTION_INFORMATION,
736                                   0xc2, 0x0000, 0x0000, transfer_buffer,
737                                   sizeof(*connection_info), 300);
738         if (retval < 0)
739                 dev_err(dev, "%s - error %d getting connection info\n",
740                         __func__, retval);
741         else
742                 usb_serial_debug_data(debug, &serial->dev->dev, __func__,
743                                       retval, transfer_buffer);
744
745         kfree(transfer_buffer);
746         return 0;
747 }
748
749
750 static int visor_probe(struct usb_serial *serial,
751                                         const struct usb_device_id *id)
752 {
753         int retval = 0;
754         int (*startup)(struct usb_serial *serial,
755                                         const struct usb_device_id *id);
756
757         dbg("%s", __func__);
758
759         if (serial->dev->actconfig->desc.bConfigurationValue != 1) {
760                 dev_err(&serial->dev->dev, "active config #%d != 1 ??\n",
761                         serial->dev->actconfig->desc.bConfigurationValue);
762                 return -ENODEV;
763         }
764
765         if (id->driver_info) {
766                 startup = (void *)id->driver_info;
767                 retval = startup(serial, id);
768         }
769
770         return retval;
771 }
772
773 static int visor_calc_num_ports(struct usb_serial *serial)
774 {
775         int num_ports = (int)(long)(usb_get_serial_data(serial));
776
777         if (num_ports)
778                 usb_set_serial_data(serial, NULL);
779
780         return num_ports;
781 }
782
783 static int generic_startup(struct usb_serial *serial)
784 {
785         struct usb_serial_port **ports = serial->port;
786         struct visor_private *priv;
787         int i;
788
789         for (i = 0; i < serial->num_ports; ++i) {
790                 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
791                 if (!priv) {
792                         while (i-- != 0) {
793                                 priv = usb_get_serial_port_data(ports[i]);
794                                 usb_set_serial_port_data(ports[i], NULL);
795                                 kfree(priv);
796                         }
797                         return -ENOMEM;
798                 }
799                 spin_lock_init(&priv->lock);
800                 usb_set_serial_port_data(ports[i], priv);
801         }
802         return 0;
803 }
804
805 static int clie_3_5_startup(struct usb_serial *serial)
806 {
807         struct device *dev = &serial->dev->dev;
808         int result;
809         u8 data;
810
811         dbg("%s", __func__);
812
813         /*
814          * Note that PEG-300 series devices expect the following two calls.
815          */
816
817         /* get the config number */
818         result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
819                                   USB_REQ_GET_CONFIGURATION, USB_DIR_IN,
820                                   0, 0, &data, 1, 3000);
821         if (result < 0) {
822                 dev_err(dev, "%s: get config number failed: %d\n",
823                                                         __func__, result);
824                 return result;
825         }
826         if (result != 1) {
827                 dev_err(dev, "%s: get config number bad return length: %d\n",
828                                                         __func__, result);
829                 return -EIO;
830         }
831
832         /* get the interface number */
833         result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
834                                   USB_REQ_GET_INTERFACE,
835                                   USB_DIR_IN | USB_RECIP_INTERFACE,
836                                   0, 0, &data, 1, 3000);
837         if (result < 0) {
838                 dev_err(dev, "%s: get interface number failed: %d\n",
839                                                         __func__, result);
840                 return result;
841         }
842         if (result != 1) {
843                 dev_err(dev,
844                         "%s: get interface number bad return length: %d\n",
845                                                         __func__, result);
846                 return -EIO;
847         }
848
849         return generic_startup(serial);
850 }
851
852 static int treo_attach(struct usb_serial *serial)
853 {
854         struct usb_serial_port *swap_port;
855
856         /* Only do this endpoint hack for the Handspring devices with
857          * interrupt in endpoints, which for now are the Treo devices. */
858         if (!((le16_to_cpu(serial->dev->descriptor.idVendor)
859                                                 == HANDSPRING_VENDOR_ID) ||
860                 (le16_to_cpu(serial->dev->descriptor.idVendor)
861                                                 == KYOCERA_VENDOR_ID)) ||
862                 (serial->num_interrupt_in == 0))
863                 goto generic_startup;
864
865         dbg("%s", __func__);
866
867         /*
868         * It appears that Treos and Kyoceras want to use the
869         * 1st bulk in endpoint to communicate with the 2nd bulk out endpoint,
870         * so let's swap the 1st and 2nd bulk in and interrupt endpoints.
871         * Note that swapping the bulk out endpoints would break lots of
872         * apps that want to communicate on the second port.
873         */
874 #define COPY_PORT(dest, src)                                            \
875         do { \
876                 dest->read_urb = src->read_urb;                         \
877                 dest->bulk_in_endpointAddress = src->bulk_in_endpointAddress;\
878                 dest->bulk_in_buffer = src->bulk_in_buffer;             \
879                 dest->interrupt_in_urb = src->interrupt_in_urb;         \
880                 dest->interrupt_in_endpointAddress = \
881                                         src->interrupt_in_endpointAddress;\
882                 dest->interrupt_in_buffer = src->interrupt_in_buffer;   \
883         } while (0);
884
885         swap_port = kmalloc(sizeof(*swap_port), GFP_KERNEL);
886         if (!swap_port)
887                 return -ENOMEM;
888         COPY_PORT(swap_port, serial->port[0]);
889         COPY_PORT(serial->port[0], serial->port[1]);
890         COPY_PORT(serial->port[1], swap_port);
891         kfree(swap_port);
892
893 generic_startup:
894         return generic_startup(serial);
895 }
896
897 static int clie_5_attach(struct usb_serial *serial)
898 {
899         dbg("%s", __func__);
900
901         /* TH55 registers 2 ports.
902            Communication in from the UX50/TH55 uses bulk_in_endpointAddress
903            from port 0. Communication out to the UX50/TH55 uses
904            bulk_out_endpointAddress from port 1
905
906            Lets do a quick and dirty mapping
907          */
908
909         /* some sanity check */
910         if (serial->num_ports < 2)
911                 return -1;
912
913         /* port 0 now uses the modified endpoint Address */
914         serial->port[0]->bulk_out_endpointAddress =
915                                 serial->port[1]->bulk_out_endpointAddress;
916
917         return generic_startup(serial);
918 }
919
920 static void visor_release(struct usb_serial *serial)
921 {
922         struct visor_private *priv;
923         int i;
924
925         dbg("%s", __func__);
926
927         for (i = 0; i < serial->num_ports; i++) {
928                 priv = usb_get_serial_port_data(serial->port[i]);
929                 kfree(priv);
930         }
931 }
932
933 static int __init visor_init(void)
934 {
935         int i, retval;
936         /* Only if parameters were passed to us */
937         if (vendor > 0 && product > 0) {
938                 struct usb_device_id usb_dev_temp[] = {
939                         {
940                                 USB_DEVICE(vendor, product),
941                                 .driver_info =
942                                         (kernel_ulong_t) &palm_os_4_probe
943                         }
944                 };
945
946                 /* Find the last entry in id_table */
947                 for (i = 0;; i++) {
948                         if (id_table[i].idVendor == 0) {
949                                 id_table[i] = usb_dev_temp[0];
950                                 break;
951                         }
952                 }
953                 /* Find the last entry in id_table_combined */
954                 for (i = 0;; i++) {
955                         if (id_table_combined[i].idVendor == 0) {
956                                 id_table_combined[i] = usb_dev_temp[0];
957                                 break;
958                         }
959                 }
960                 printk(KERN_INFO KBUILD_MODNAME
961                        ": Untested USB device specified at time of module insertion\n");
962                 printk(KERN_INFO KBUILD_MODNAME
963                        ": Warning: This is not guaranteed to work\n");
964                 printk(KERN_INFO KBUILD_MODNAME
965                        ": Using a newer kernel is preferred to this method\n");
966                 printk(KERN_INFO KBUILD_MODNAME
967                        ": Adding Palm OS protocol 4.x support for unknown device: 0x%x/0x%x\n",
968                         vendor, product);
969         }
970         retval = usb_serial_register(&handspring_device);
971         if (retval)
972                 goto failed_handspring_register;
973         retval = usb_serial_register(&clie_3_5_device);
974         if (retval)
975                 goto failed_clie_3_5_register;
976         retval = usb_serial_register(&clie_5_device);
977         if (retval)
978                 goto failed_clie_5_register;
979         retval = usb_register(&visor_driver);
980         if (retval)
981                 goto failed_usb_register;
982         printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_DESC "\n");
983
984         return 0;
985 failed_usb_register:
986         usb_serial_deregister(&clie_5_device);
987 failed_clie_5_register:
988         usb_serial_deregister(&clie_3_5_device);
989 failed_clie_3_5_register:
990         usb_serial_deregister(&handspring_device);
991 failed_handspring_register:
992         return retval;
993 }
994
995
996 static void __exit visor_exit (void)
997 {
998         usb_deregister(&visor_driver);
999         usb_serial_deregister(&handspring_device);
1000         usb_serial_deregister(&clie_3_5_device);
1001         usb_serial_deregister(&clie_5_device);
1002 }
1003
1004
1005 module_init(visor_init);
1006 module_exit(visor_exit);
1007
1008 MODULE_AUTHOR(DRIVER_AUTHOR);
1009 MODULE_DESCRIPTION(DRIVER_DESC);
1010 MODULE_LICENSE("GPL");
1011
1012 module_param(debug, bool, S_IRUGO | S_IWUSR);
1013 MODULE_PARM_DESC(debug, "Debug enabled or not");
1014 module_param(stats, bool, S_IRUGO | S_IWUSR);
1015 MODULE_PARM_DESC(stats, "Enables statistics or not");
1016
1017 module_param(vendor, ushort, 0);
1018 MODULE_PARM_DESC(vendor, "User specified vendor ID");
1019 module_param(product, ushort, 0);
1020 MODULE_PARM_DESC(product, "User specified product ID");
1021