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