USB: serial: pl2303: new device id for Chilitag
[pandora-kernel.git] / drivers / usb / serial / keyspan_pda.c
1 /*
2  * USB Keyspan PDA / Xircom / Entrega Converter driver
3  *
4  * Copyright (C) 1999 - 2001 Greg Kroah-Hartman <greg@kroah.com>
5  * Copyright (C) 1999, 2000 Brian Warner        <warner@lothar.com>
6  * Copyright (C) 2000 Al Borchers               <borchers@steinerpoint.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
14  * driver
15  *
16  * (09/07/2001) gkh
17  *      cleaned up the Xircom support.  Added ids for Entregra device which is
18  *      the same as the Xircom device.  Enabled the code to be compiled for
19  *      either Xircom or Keyspan devices.
20  *
21  * (08/11/2001) Cristian M. Craciunescu
22  *      support for Xircom PGSDB9
23  *
24  * (05/31/2001) gkh
25  *      switched from using spinlock to a semaphore, which fixes lots of
26  *      problems.
27  *
28  * (04/08/2001) gb
29  *      Identify version on module load.
30  *
31  * (11/01/2000) Adam J. Richter
32  *      usb_device_id table support
33  *
34  * (10/05/2000) gkh
35  *      Fixed bug with urb->dev not being set properly, now that the usb
36  *      core needs it.
37  *
38  * (08/28/2000) gkh
39  *      Added locks for SMP safeness.
40  *      Fixed MOD_INC and MOD_DEC logic and the ability to open a port more
41  *      than once.
42  *
43  * (07/20/2000) borchers
44  *      - keyspan_pda_write no longer sleeps if it is called on interrupt time;
45  *        PPP and the line discipline with stty echo on can call write on
46  *        interrupt time and this would cause an oops if write slept
47  *      - if keyspan_pda_write is in an interrupt, it will not call
48  *        usb_control_msg (which sleeps) to query the room in the device
49  *        buffer, it simply uses the current room value it has
50  *      - if the urb is busy or if it is throttled keyspan_pda_write just
51  *        returns 0, rather than sleeping to wait for this to change; the
52  *        write_chan code in n_tty.c will sleep if needed before calling
53  *        keyspan_pda_write again
54  *      - if the device needs to be unthrottled, write now queues up the
55  *        call to usb_control_msg (which sleeps) to unthrottle the device
56  *      - the wakeups from keyspan_pda_write_bulk_callback are queued rather
57  *        than done directly from the callback to avoid the race in write_chan
58  *      - keyspan_pda_chars_in_buffer also indicates its buffer is full if the
59  *        urb status is -EINPROGRESS, meaning it cannot write at the moment
60  *
61  * (07/19/2000) gkh
62  *      Added module_init and module_exit functions to handle the fact that this
63  *      driver is a loadable module now.
64  *
65  * (03/26/2000) gkh
66  *      Split driver up into device specific pieces.
67  *
68  */
69
70
71 #include <linux/kernel.h>
72 #include <linux/errno.h>
73 #include <linux/init.h>
74 #include <linux/slab.h>
75 #include <linux/tty.h>
76 #include <linux/tty_driver.h>
77 #include <linux/tty_flip.h>
78 #include <linux/module.h>
79 #include <linux/spinlock.h>
80 #include <linux/workqueue.h>
81 #include <linux/firmware.h>
82 #include <linux/ihex.h>
83 #include <linux/uaccess.h>
84 #include <linux/usb.h>
85 #include <linux/usb/serial.h>
86
87 static int debug;
88
89 /* make a simple define to handle if we are compiling keyspan_pda or xircom support */
90 #if defined(CONFIG_USB_SERIAL_KEYSPAN_PDA) || defined(CONFIG_USB_SERIAL_KEYSPAN_PDA_MODULE)
91         #define KEYSPAN
92 #else
93         #undef KEYSPAN
94 #endif
95 #if defined(CONFIG_USB_SERIAL_XIRCOM) || defined(CONFIG_USB_SERIAL_XIRCOM_MODULE)
96         #define XIRCOM
97 #else
98         #undef XIRCOM
99 #endif
100
101 /*
102  * Version Information
103  */
104 #define DRIVER_VERSION "v1.1"
105 #define DRIVER_AUTHOR "Brian Warner <warner@lothar.com>"
106 #define DRIVER_DESC "USB Keyspan PDA Converter driver"
107
108 struct keyspan_pda_private {
109         int                     tx_room;
110         int                     tx_throttled;
111         struct work_struct                      wakeup_work;
112         struct work_struct                      unthrottle_work;
113         struct usb_serial       *serial;
114         struct usb_serial_port  *port;
115 };
116
117
118 #define KEYSPAN_VENDOR_ID               0x06cd
119 #define KEYSPAN_PDA_FAKE_ID             0x0103
120 #define KEYSPAN_PDA_ID                  0x0104 /* no clue */
121
122 /* For Xircom PGSDB9 and older Entrega version of the same device */
123 #define XIRCOM_VENDOR_ID                0x085a
124 #define XIRCOM_FAKE_ID                  0x8027
125 #define XIRCOM_FAKE_ID_2                0x8025 /* "PGMFHUB" serial */
126 #define ENTREGA_VENDOR_ID               0x1645
127 #define ENTREGA_FAKE_ID                 0x8093
128
129 static const struct usb_device_id id_table_combined[] = {
130 #ifdef KEYSPAN
131         { USB_DEVICE(KEYSPAN_VENDOR_ID, KEYSPAN_PDA_FAKE_ID) },
132 #endif
133 #ifdef XIRCOM
134         { USB_DEVICE(XIRCOM_VENDOR_ID, XIRCOM_FAKE_ID) },
135         { USB_DEVICE(XIRCOM_VENDOR_ID, XIRCOM_FAKE_ID_2) },
136         { USB_DEVICE(ENTREGA_VENDOR_ID, ENTREGA_FAKE_ID) },
137 #endif
138         { USB_DEVICE(KEYSPAN_VENDOR_ID, KEYSPAN_PDA_ID) },
139         { }                                             /* Terminating entry */
140 };
141
142 MODULE_DEVICE_TABLE(usb, id_table_combined);
143
144 static struct usb_driver keyspan_pda_driver = {
145         .name =         "keyspan_pda",
146         .probe =        usb_serial_probe,
147         .disconnect =   usb_serial_disconnect,
148         .id_table =     id_table_combined,
149         .no_dynamic_id =        1,
150 };
151
152 static const struct usb_device_id id_table_std[] = {
153         { USB_DEVICE(KEYSPAN_VENDOR_ID, KEYSPAN_PDA_ID) },
154         { }                                             /* Terminating entry */
155 };
156
157 #ifdef KEYSPAN
158 static const struct usb_device_id id_table_fake[] = {
159         { USB_DEVICE(KEYSPAN_VENDOR_ID, KEYSPAN_PDA_FAKE_ID) },
160         { }                                             /* Terminating entry */
161 };
162 #endif
163
164 #ifdef XIRCOM
165 static const struct usb_device_id id_table_fake_xircom[] = {
166         { USB_DEVICE(XIRCOM_VENDOR_ID, XIRCOM_FAKE_ID) },
167         { USB_DEVICE(XIRCOM_VENDOR_ID, XIRCOM_FAKE_ID_2) },
168         { USB_DEVICE(ENTREGA_VENDOR_ID, ENTREGA_FAKE_ID) },
169         { }
170 };
171 #endif
172
173 static void keyspan_pda_wakeup_write(struct work_struct *work)
174 {
175         struct keyspan_pda_private *priv =
176                 container_of(work, struct keyspan_pda_private, wakeup_work);
177         struct usb_serial_port *port = priv->port;
178         struct tty_struct *tty = tty_port_tty_get(&port->port);
179         if (tty)
180                 tty_wakeup(tty);
181         tty_kref_put(tty);
182 }
183
184 static void keyspan_pda_request_unthrottle(struct work_struct *work)
185 {
186         struct keyspan_pda_private *priv =
187                 container_of(work, struct keyspan_pda_private, unthrottle_work);
188         struct usb_serial *serial = priv->serial;
189         int result;
190
191         dbg(" request_unthrottle");
192         /* ask the device to tell us when the tx buffer becomes
193            sufficiently empty */
194         result = usb_control_msg(serial->dev,
195                                  usb_sndctrlpipe(serial->dev, 0),
196                                  7, /* request_unthrottle */
197                                  USB_TYPE_VENDOR | USB_RECIP_INTERFACE
198                                  | USB_DIR_OUT,
199                                  16, /* value: threshold */
200                                  0, /* index */
201                                  NULL,
202                                  0,
203                                  2000);
204         if (result < 0)
205                 dbg("%s - error %d from usb_control_msg",
206                     __func__, result);
207 }
208
209
210 static void keyspan_pda_rx_interrupt(struct urb *urb)
211 {
212         struct usb_serial_port *port = urb->context;
213         struct tty_struct *tty;
214         unsigned char *data = urb->transfer_buffer;
215         unsigned int len = urb->actual_length;
216         int retval;
217         int status = urb->status;
218         struct keyspan_pda_private *priv;
219         priv = usb_get_serial_port_data(port);
220
221         switch (status) {
222         case 0:
223                 /* success */
224                 break;
225         case -ECONNRESET:
226         case -ENOENT:
227         case -ESHUTDOWN:
228                 /* this urb is terminated, clean up */
229                 dbg("%s - urb shutting down with status: %d",
230                     __func__, status);
231                 return;
232         default:
233                 dbg("%s - nonzero urb status received: %d",
234                     __func__, status);
235                 goto exit;
236         }
237
238         if (len < 1) {
239                 dev_warn(&port->dev, "short message received\n");
240                 goto exit;
241         }
242
243         /* see if the message is data or a status interrupt */
244         switch (data[0]) {
245         case 0:
246                 tty = tty_port_tty_get(&port->port);
247                  /* rest of message is rx data */
248                 if (!tty || len < 2)
249                         break;
250                 tty_insert_flip_string(tty, data + 1, len - 1);
251                 tty_flip_buffer_push(tty);
252                 tty_kref_put(tty);
253                 break;
254         case 1:
255                 /* status interrupt */
256                 if (len < 3) {
257                         dev_warn(&port->dev, "short interrupt message received\n");
258                         break;
259                 }
260                 dbg(" rx int, d1=%d, d2=%d", data[1], data[2]);
261                 switch (data[1]) {
262                 case 1: /* modemline change */
263                         break;
264                 case 2: /* tx unthrottle interrupt */
265                         priv->tx_throttled = 0;
266                         /* queue up a wakeup at scheduler time */
267                         schedule_work(&priv->wakeup_work);
268                         break;
269                 default:
270                         break;
271                 }
272                 break;
273         default:
274                 break;
275         }
276
277 exit:
278         retval = usb_submit_urb(urb, GFP_ATOMIC);
279         if (retval)
280                 dev_err(&port->dev,
281                         "%s - usb_submit_urb failed with result %d",
282                         __func__, retval);
283 }
284
285
286 static void keyspan_pda_rx_throttle(struct tty_struct *tty)
287 {
288         /* stop receiving characters. We just turn off the URB request, and
289            let chars pile up in the device. If we're doing hardware
290            flowcontrol, the device will signal the other end when its buffer
291            fills up. If we're doing XON/XOFF, this would be a good time to
292            send an XOFF, although it might make sense to foist that off
293            upon the device too. */
294         struct usb_serial_port *port = tty->driver_data;
295         dbg("keyspan_pda_rx_throttle port %d", port->number);
296         usb_kill_urb(port->interrupt_in_urb);
297 }
298
299
300 static void keyspan_pda_rx_unthrottle(struct tty_struct *tty)
301 {
302         struct usb_serial_port *port = tty->driver_data;
303         /* just restart the receive interrupt URB */
304         dbg("keyspan_pda_rx_unthrottle port %d", port->number);
305         port->interrupt_in_urb->dev = port->serial->dev;
306         if (usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL))
307                 dbg(" usb_submit_urb(read urb) failed");
308 }
309
310
311 static speed_t keyspan_pda_setbaud(struct usb_serial *serial, speed_t baud)
312 {
313         int rc;
314         int bindex;
315
316         switch (baud) {
317         case 110:
318                 bindex = 0;
319                 break;
320         case 300:
321                 bindex = 1;
322                 break;
323         case 1200:
324                 bindex = 2;
325                 break;
326         case 2400:
327                 bindex = 3;
328                 break;
329         case 4800:
330                 bindex = 4;
331                 break;
332         case 9600:
333                 bindex = 5;
334                 break;
335         case 19200:
336                 bindex = 6;
337                 break;
338         case 38400:
339                 bindex = 7;
340                 break;
341         case 57600:
342                 bindex = 8;
343                 break;
344         case 115200:
345                 bindex = 9;
346                 break;
347         default:
348                 bindex = 5;     /* Default to 9600 */
349                 baud = 9600;
350         }
351
352         /* rather than figure out how to sleep while waiting for this
353            to complete, I just use the "legacy" API. */
354         rc = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
355                              0, /* set baud */
356                              USB_TYPE_VENDOR
357                              | USB_RECIP_INTERFACE
358                              | USB_DIR_OUT, /* type */
359                              bindex, /* value */
360                              0, /* index */
361                              NULL, /* &data */
362                              0, /* size */
363                              2000); /* timeout */
364         if (rc < 0)
365                 return 0;
366         return baud;
367 }
368
369
370 static void keyspan_pda_break_ctl(struct tty_struct *tty, int break_state)
371 {
372         struct usb_serial_port *port = tty->driver_data;
373         struct usb_serial *serial = port->serial;
374         int value;
375         int result;
376
377         if (break_state == -1)
378                 value = 1; /* start break */
379         else
380                 value = 0; /* clear break */
381         result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
382                         4, /* set break */
383                         USB_TYPE_VENDOR | USB_RECIP_INTERFACE | USB_DIR_OUT,
384                         value, 0, NULL, 0, 2000);
385         if (result < 0)
386                 dbg("%s - error %d from usb_control_msg",
387                     __func__, result);
388         /* there is something funky about this.. the TCSBRK that 'cu' performs
389            ought to translate into a break_ctl(-1),break_ctl(0) pair HZ/4
390            seconds apart, but it feels like the break sent isn't as long as it
391            is on /dev/ttyS0 */
392 }
393
394
395 static void keyspan_pda_set_termios(struct tty_struct *tty,
396                 struct usb_serial_port *port, struct ktermios *old_termios)
397 {
398         struct usb_serial *serial = port->serial;
399         speed_t speed;
400
401         /* cflag specifies lots of stuff: number of stop bits, parity, number
402            of data bits, baud. What can the device actually handle?:
403            CSTOPB (1 stop bit or 2)
404            PARENB (parity)
405            CSIZE (5bit .. 8bit)
406            There is minimal hw support for parity (a PSW bit seems to hold the
407            parity of whatever is in the accumulator). The UART either deals
408            with 10 bits (start, 8 data, stop) or 11 bits (start, 8 data,
409            1 special, stop). So, with firmware changes, we could do:
410            8N1: 10 bit
411            8N2: 11 bit, extra bit always (mark?)
412            8[EOMS]1: 11 bit, extra bit is parity
413            7[EOMS]1: 10 bit, b0/b7 is parity
414            7[EOMS]2: 11 bit, b0/b7 is parity, extra bit always (mark?)
415
416            HW flow control is dictated by the tty->termios->c_cflags & CRTSCTS
417            bit.
418
419            For now, just do baud. */
420
421         speed = tty_get_baud_rate(tty);
422         speed = keyspan_pda_setbaud(serial, speed);
423
424         if (speed == 0) {
425                 dbg("can't handle requested baud rate");
426                 /* It hasn't changed so.. */
427                 speed = tty_termios_baud_rate(old_termios);
428         }
429         /* Only speed can change so copy the old h/w parameters
430            then encode the new speed */
431         tty_termios_copy_hw(tty->termios, old_termios);
432         tty_encode_baud_rate(tty, speed, speed);
433 }
434
435
436 /* modem control pins: DTR and RTS are outputs and can be controlled.
437    DCD, RI, DSR, CTS are inputs and can be read. All outputs can also be
438    read. The byte passed is: DTR(b7) DCD RI DSR CTS RTS(b2) unused unused */
439
440 static int keyspan_pda_get_modem_info(struct usb_serial *serial,
441                                       unsigned char *value)
442 {
443         int rc;
444         u8 *data;
445
446         data = kmalloc(1, GFP_KERNEL);
447         if (!data)
448                 return -ENOMEM;
449
450         rc = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
451                              3, /* get pins */
452                              USB_TYPE_VENDOR|USB_RECIP_INTERFACE|USB_DIR_IN,
453                              0, 0, data, 1, 2000);
454         if (rc >= 0)
455                 *value = *data;
456
457         kfree(data);
458         return rc;
459 }
460
461
462 static int keyspan_pda_set_modem_info(struct usb_serial *serial,
463                                       unsigned char value)
464 {
465         int rc;
466         rc = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
467                              3, /* set pins */
468                              USB_TYPE_VENDOR|USB_RECIP_INTERFACE|USB_DIR_OUT,
469                              value, 0, NULL, 0, 2000);
470         return rc;
471 }
472
473 static int keyspan_pda_tiocmget(struct tty_struct *tty)
474 {
475         struct usb_serial_port *port = tty->driver_data;
476         struct usb_serial *serial = port->serial;
477         int rc;
478         unsigned char status;
479         int value;
480
481         rc = keyspan_pda_get_modem_info(serial, &status);
482         if (rc < 0)
483                 return rc;
484         value =
485                 ((status & (1<<7)) ? TIOCM_DTR : 0) |
486                 ((status & (1<<6)) ? TIOCM_CAR : 0) |
487                 ((status & (1<<5)) ? TIOCM_RNG : 0) |
488                 ((status & (1<<4)) ? TIOCM_DSR : 0) |
489                 ((status & (1<<3)) ? TIOCM_CTS : 0) |
490                 ((status & (1<<2)) ? TIOCM_RTS : 0);
491         return value;
492 }
493
494 static int keyspan_pda_tiocmset(struct tty_struct *tty,
495                                 unsigned int set, unsigned int clear)
496 {
497         struct usb_serial_port *port = tty->driver_data;
498         struct usb_serial *serial = port->serial;
499         int rc;
500         unsigned char status;
501
502         rc = keyspan_pda_get_modem_info(serial, &status);
503         if (rc < 0)
504                 return rc;
505
506         if (set & TIOCM_RTS)
507                 status |= (1<<2);
508         if (set & TIOCM_DTR)
509                 status |= (1<<7);
510
511         if (clear & TIOCM_RTS)
512                 status &= ~(1<<2);
513         if (clear & TIOCM_DTR)
514                 status &= ~(1<<7);
515         rc = keyspan_pda_set_modem_info(serial, status);
516         return rc;
517 }
518
519 static int keyspan_pda_write(struct tty_struct *tty,
520         struct usb_serial_port *port, const unsigned char *buf, int count)
521 {
522         struct usb_serial *serial = port->serial;
523         int request_unthrottle = 0;
524         int rc = 0;
525         struct keyspan_pda_private *priv;
526
527         priv = usb_get_serial_port_data(port);
528         /* guess how much room is left in the device's ring buffer, and if we
529            want to send more than that, check first, updating our notion of
530            what is left. If our write will result in no room left, ask the
531            device to give us an interrupt when the room available rises above
532            a threshold, and hold off all writers (eventually, those using
533            select() or poll() too) until we receive that unthrottle interrupt.
534            Block if we can't write anything at all, otherwise write as much as
535            we can. */
536         dbg("keyspan_pda_write(%d)", count);
537         if (count == 0) {
538                 dbg(" write request of 0 bytes");
539                 return 0;
540         }
541
542         /* we might block because of:
543            the TX urb is in-flight (wait until it completes)
544            the device is full (wait until it says there is room)
545         */
546         spin_lock_bh(&port->lock);
547         if (port->write_urb_busy || priv->tx_throttled) {
548                 spin_unlock_bh(&port->lock);
549                 return 0;
550         }
551         port->write_urb_busy = 1;
552         spin_unlock_bh(&port->lock);
553
554         /* At this point the URB is in our control, nobody else can submit it
555            again (the only sudden transition was the one from EINPROGRESS to
556            finished).  Also, the tx process is not throttled. So we are
557            ready to write. */
558
559         count = (count > port->bulk_out_size) ? port->bulk_out_size : count;
560
561         /* Check if we might overrun the Tx buffer.   If so, ask the
562            device how much room it really has.  This is done only on
563            scheduler time, since usb_control_msg() sleeps. */
564         if (count > priv->tx_room && !in_interrupt()) {
565                 u8 *room;
566
567                 room = kmalloc(1, GFP_KERNEL);
568                 if (!room) {
569                         rc = -ENOMEM;
570                         goto exit;
571                 }
572
573                 rc = usb_control_msg(serial->dev,
574                                      usb_rcvctrlpipe(serial->dev, 0),
575                                      6, /* write_room */
576                                      USB_TYPE_VENDOR | USB_RECIP_INTERFACE
577                                      | USB_DIR_IN,
578                                      0, /* value: 0 means "remaining room" */
579                                      0, /* index */
580                                      room,
581                                      1,
582                                      2000);
583                 if (rc > 0) {
584                         dbg(" roomquery says %d", *room);
585                         priv->tx_room = *room;
586                 }
587                 kfree(room);
588                 if (rc < 0) {
589                         dbg(" roomquery failed");
590                         goto exit;
591                 }
592                 if (rc == 0) {
593                         dbg(" roomquery returned 0 bytes");
594                         rc = -EIO; /* device didn't return any data */
595                         goto exit;
596                 }
597         }
598         if (count > priv->tx_room) {
599                 /* we're about to completely fill the Tx buffer, so
600                    we'll be throttled afterwards. */
601                 count = priv->tx_room;
602                 request_unthrottle = 1;
603         }
604
605         if (count) {
606                 /* now transfer data */
607                 memcpy(port->write_urb->transfer_buffer, buf, count);
608                 /* send the data out the bulk port */
609                 port->write_urb->transfer_buffer_length = count;
610
611                 priv->tx_room -= count;
612
613                 port->write_urb->dev = port->serial->dev;
614                 rc = usb_submit_urb(port->write_urb, GFP_ATOMIC);
615                 if (rc) {
616                         dbg(" usb_submit_urb(write bulk) failed");
617                         goto exit;
618                 }
619         } else {
620                 /* There wasn't any room left, so we are throttled until
621                    the buffer empties a bit */
622                 request_unthrottle = 1;
623         }
624
625         if (request_unthrottle) {
626                 priv->tx_throttled = 1; /* block writers */
627                 schedule_work(&priv->unthrottle_work);
628         }
629
630         rc = count;
631 exit:
632         if (rc < 0)
633                 port->write_urb_busy = 0;
634         return rc;
635 }
636
637
638 static void keyspan_pda_write_bulk_callback(struct urb *urb)
639 {
640         struct usb_serial_port *port = urb->context;
641         struct keyspan_pda_private *priv;
642
643         port->write_urb_busy = 0;
644         priv = usb_get_serial_port_data(port);
645
646         /* queue up a wakeup at scheduler time */
647         schedule_work(&priv->wakeup_work);
648 }
649
650
651 static int keyspan_pda_write_room(struct tty_struct *tty)
652 {
653         struct usb_serial_port *port = tty->driver_data;
654         struct keyspan_pda_private *priv;
655         priv = usb_get_serial_port_data(port);
656         /* used by n_tty.c for processing of tabs and such. Giving it our
657            conservative guess is probably good enough, but needs testing by
658            running a console through the device. */
659         return priv->tx_room;
660 }
661
662
663 static int keyspan_pda_chars_in_buffer(struct tty_struct *tty)
664 {
665         struct usb_serial_port *port = tty->driver_data;
666         struct keyspan_pda_private *priv;
667         unsigned long flags;
668         int ret = 0;
669
670         priv = usb_get_serial_port_data(port);
671
672         /* when throttled, return at least WAKEUP_CHARS to tell select() (via
673            n_tty.c:normal_poll() ) that we're not writeable. */
674
675         spin_lock_irqsave(&port->lock, flags);
676         if (port->write_urb_busy || priv->tx_throttled)
677                 ret = 256;
678         spin_unlock_irqrestore(&port->lock, flags);
679         return ret;
680 }
681
682
683 static void keyspan_pda_dtr_rts(struct usb_serial_port *port, int on)
684 {
685         struct usb_serial *serial = port->serial;
686
687         if (serial->dev) {
688                 if (on)
689                         keyspan_pda_set_modem_info(serial, (1<<7) | (1<< 2));
690                 else
691                         keyspan_pda_set_modem_info(serial, 0);
692         }
693 }
694
695
696 static int keyspan_pda_open(struct tty_struct *tty,
697                                         struct usb_serial_port *port)
698 {
699         struct usb_serial *serial = port->serial;
700         u8 *room;
701         int rc = 0;
702         struct keyspan_pda_private *priv;
703
704         /* find out how much room is in the Tx ring */
705         room = kmalloc(1, GFP_KERNEL);
706         if (!room)
707                 return -ENOMEM;
708
709         rc = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
710                              6, /* write_room */
711                              USB_TYPE_VENDOR | USB_RECIP_INTERFACE
712                              | USB_DIR_IN,
713                              0, /* value */
714                              0, /* index */
715                              room,
716                              1,
717                              2000);
718         if (rc < 0) {
719                 dbg("%s - roomquery failed", __func__);
720                 goto error;
721         }
722         if (rc == 0) {
723                 dbg("%s - roomquery returned 0 bytes", __func__);
724                 rc = -EIO;
725                 goto error;
726         }
727         priv = usb_get_serial_port_data(port);
728         priv->tx_room = *room;
729         priv->tx_throttled = *room ? 0 : 1;
730
731         /*Start reading from the device*/
732         port->interrupt_in_urb->dev = serial->dev;
733         rc = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
734         if (rc) {
735                 dbg("%s - usb_submit_urb(read int) failed", __func__);
736                 goto error;
737         }
738 error:
739         kfree(room);
740         return rc;
741 }
742 static void keyspan_pda_close(struct usb_serial_port *port)
743 {
744         struct usb_serial *serial = port->serial;
745
746         if (serial->dev) {
747                 /* shutdown our bulk reads and writes */
748                 usb_kill_urb(port->write_urb);
749                 usb_kill_urb(port->interrupt_in_urb);
750         }
751 }
752
753
754 /* download the firmware to a "fake" device (pre-renumeration) */
755 static int keyspan_pda_fake_startup(struct usb_serial *serial)
756 {
757         int response;
758         const char *fw_name;
759         const struct ihex_binrec *record;
760         const struct firmware *fw;
761
762         /* download the firmware here ... */
763         response = ezusb_set_reset(serial, 1);
764
765         if (0) { ; }
766 #ifdef KEYSPAN
767         else if (le16_to_cpu(serial->dev->descriptor.idVendor) == KEYSPAN_VENDOR_ID)
768                 fw_name = "keyspan_pda/keyspan_pda.fw";
769 #endif
770 #ifdef XIRCOM
771         else if ((le16_to_cpu(serial->dev->descriptor.idVendor) == XIRCOM_VENDOR_ID) ||
772                  (le16_to_cpu(serial->dev->descriptor.idVendor) == ENTREGA_VENDOR_ID))
773                 fw_name = "keyspan_pda/xircom_pgs.fw";
774 #endif
775         else {
776                 dev_err(&serial->dev->dev, "%s: unknown vendor, aborting.\n",
777                         __func__);
778                 return -ENODEV;
779         }
780         if (request_ihex_firmware(&fw, fw_name, &serial->dev->dev)) {
781                 dev_err(&serial->dev->dev, "failed to load firmware \"%s\"\n",
782                         fw_name);
783                 return -ENOENT;
784         }
785         record = (const struct ihex_binrec *)fw->data;
786
787         while (record) {
788                 response = ezusb_writememory(serial, be32_to_cpu(record->addr),
789                                              (unsigned char *)record->data,
790                                              be16_to_cpu(record->len), 0xa0);
791                 if (response < 0) {
792                         dev_err(&serial->dev->dev, "ezusb_writememory failed "
793                                 "for Keyspan PDA firmware (%d %04X %p %d)\n",
794                                 response, be32_to_cpu(record->addr),
795                                 record->data, be16_to_cpu(record->len));
796                         break;
797                 }
798                 record = ihex_next_binrec(record);
799         }
800         release_firmware(fw);
801         /* bring device out of reset. Renumeration will occur in a moment
802            and the new device will bind to the real driver */
803         response = ezusb_set_reset(serial, 0);
804
805         /* we want this device to fail to have a driver assigned to it. */
806         return 1;
807 }
808
809 #ifdef KEYSPAN
810 MODULE_FIRMWARE("keyspan_pda/keyspan_pda.fw");
811 #endif
812 #ifdef XIRCOM
813 MODULE_FIRMWARE("keyspan_pda/xircom_pgs.fw");
814 #endif
815
816 static int keyspan_pda_startup(struct usb_serial *serial)
817 {
818         unsigned char num_ports = serial->num_ports;
819         struct keyspan_pda_private *priv;
820
821         if (serial->num_bulk_out < num_ports ||
822                         serial->num_interrupt_in < num_ports) {
823                 dev_err(&serial->interface->dev, "missing endpoints\n");
824                 return -ENODEV;
825         }
826
827         /* allocate the private data structures for all ports. Well, for all
828            one ports. */
829
830         priv = kmalloc(sizeof(struct keyspan_pda_private), GFP_KERNEL);
831         if (!priv)
832                 return 1; /* error */
833         usb_set_serial_port_data(serial->port[0], priv);
834         init_waitqueue_head(&serial->port[0]->write_wait);
835         INIT_WORK(&priv->wakeup_work, keyspan_pda_wakeup_write);
836         INIT_WORK(&priv->unthrottle_work, keyspan_pda_request_unthrottle);
837         priv->serial = serial;
838         priv->port = serial->port[0];
839         return 0;
840 }
841
842 static void keyspan_pda_release(struct usb_serial *serial)
843 {
844         dbg("%s", __func__);
845
846         kfree(usb_get_serial_port_data(serial->port[0]));
847 }
848
849 #ifdef KEYSPAN
850 static struct usb_serial_driver keyspan_pda_fake_device = {
851         .driver = {
852                 .owner =        THIS_MODULE,
853                 .name =         "keyspan_pda_pre",
854         },
855         .description =          "Keyspan PDA - (prerenumeration)",
856         .usb_driver =           &keyspan_pda_driver,
857         .id_table =             id_table_fake,
858         .num_ports =            1,
859         .attach =               keyspan_pda_fake_startup,
860 };
861 #endif
862
863 #ifdef XIRCOM
864 static struct usb_serial_driver xircom_pgs_fake_device = {
865         .driver = {
866                 .owner =        THIS_MODULE,
867                 .name =         "xircom_no_firm",
868         },
869         .description =          "Xircom / Entrega PGS - (prerenumeration)",
870         .usb_driver =           &keyspan_pda_driver,
871         .id_table =             id_table_fake_xircom,
872         .num_ports =            1,
873         .attach =               keyspan_pda_fake_startup,
874 };
875 #endif
876
877 static struct usb_serial_driver keyspan_pda_device = {
878         .driver = {
879                 .owner =        THIS_MODULE,
880                 .name =         "keyspan_pda",
881         },
882         .description =          "Keyspan PDA",
883         .usb_driver =           &keyspan_pda_driver,
884         .id_table =             id_table_std,
885         .num_ports =            1,
886         .dtr_rts =              keyspan_pda_dtr_rts,
887         .open =                 keyspan_pda_open,
888         .close =                keyspan_pda_close,
889         .write =                keyspan_pda_write,
890         .write_room =           keyspan_pda_write_room,
891         .write_bulk_callback =  keyspan_pda_write_bulk_callback,
892         .read_int_callback =    keyspan_pda_rx_interrupt,
893         .chars_in_buffer =      keyspan_pda_chars_in_buffer,
894         .throttle =             keyspan_pda_rx_throttle,
895         .unthrottle =           keyspan_pda_rx_unthrottle,
896         .set_termios =          keyspan_pda_set_termios,
897         .break_ctl =            keyspan_pda_break_ctl,
898         .tiocmget =             keyspan_pda_tiocmget,
899         .tiocmset =             keyspan_pda_tiocmset,
900         .attach =               keyspan_pda_startup,
901         .release =              keyspan_pda_release,
902 };
903
904
905 static int __init keyspan_pda_init(void)
906 {
907         int retval;
908         retval = usb_serial_register(&keyspan_pda_device);
909         if (retval)
910                 goto failed_pda_register;
911 #ifdef KEYSPAN
912         retval = usb_serial_register(&keyspan_pda_fake_device);
913         if (retval)
914                 goto failed_pda_fake_register;
915 #endif
916 #ifdef XIRCOM
917         retval = usb_serial_register(&xircom_pgs_fake_device);
918         if (retval)
919                 goto failed_xircom_register;
920 #endif
921         retval = usb_register(&keyspan_pda_driver);
922         if (retval)
923                 goto failed_usb_register;
924         printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
925                DRIVER_DESC "\n");
926         return 0;
927 failed_usb_register:
928 #ifdef XIRCOM
929         usb_serial_deregister(&xircom_pgs_fake_device);
930 failed_xircom_register:
931 #endif /* XIRCOM */
932 #ifdef KEYSPAN
933         usb_serial_deregister(&keyspan_pda_fake_device);
934 #endif
935 #ifdef KEYSPAN
936 failed_pda_fake_register:
937 #endif
938         usb_serial_deregister(&keyspan_pda_device);
939 failed_pda_register:
940         return retval;
941 }
942
943
944 static void __exit keyspan_pda_exit(void)
945 {
946         usb_deregister(&keyspan_pda_driver);
947         usb_serial_deregister(&keyspan_pda_device);
948 #ifdef KEYSPAN
949         usb_serial_deregister(&keyspan_pda_fake_device);
950 #endif
951 #ifdef XIRCOM
952         usb_serial_deregister(&xircom_pgs_fake_device);
953 #endif
954 }
955
956
957 module_init(keyspan_pda_init);
958 module_exit(keyspan_pda_exit);
959
960 MODULE_AUTHOR(DRIVER_AUTHOR);
961 MODULE_DESCRIPTION(DRIVER_DESC);
962 MODULE_LICENSE("GPL");
963
964 module_param(debug, bool, S_IRUGO | S_IWUSR);
965 MODULE_PARM_DESC(debug, "Debug enabled or not");
966