]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/usb/serial/keyspan_pda.c
b60012ce4c91237e1d82846686d03d181e5901e5
[linux-2.6-omap-h63xx.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
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 Entregra version of the same device */
123 #define XIRCOM_VENDOR_ID                0x085a
124 #define XIRCOM_FAKE_ID                  0x8027
125 #define ENTREGRA_VENDOR_ID              0x1645
126 #define ENTREGRA_FAKE_ID                0x8093
127
128 static struct usb_device_id id_table_combined [] = {
129 #ifdef KEYSPAN
130         { USB_DEVICE(KEYSPAN_VENDOR_ID, KEYSPAN_PDA_FAKE_ID) },
131 #endif
132 #ifdef XIRCOM
133         { USB_DEVICE(XIRCOM_VENDOR_ID, XIRCOM_FAKE_ID) },
134         { USB_DEVICE(ENTREGRA_VENDOR_ID, ENTREGRA_FAKE_ID) },
135 #endif
136         { USB_DEVICE(KEYSPAN_VENDOR_ID, KEYSPAN_PDA_ID) },
137         { }                                             /* Terminating entry */
138 };
139
140 MODULE_DEVICE_TABLE(usb, id_table_combined);
141
142 static struct usb_driver keyspan_pda_driver = {
143         .name =         "keyspan_pda",
144         .probe =        usb_serial_probe,
145         .disconnect =   usb_serial_disconnect,
146         .id_table =     id_table_combined,
147         .no_dynamic_id =        1,
148 };
149
150 static struct usb_device_id id_table_std [] = {
151         { USB_DEVICE(KEYSPAN_VENDOR_ID, KEYSPAN_PDA_ID) },
152         { }                                             /* Terminating entry */
153 };
154
155 #ifdef KEYSPAN
156 static struct usb_device_id id_table_fake [] = {
157         { USB_DEVICE(KEYSPAN_VENDOR_ID, KEYSPAN_PDA_FAKE_ID) },
158         { }                                             /* Terminating entry */
159 };
160 #endif
161
162 #ifdef XIRCOM
163 static struct usb_device_id id_table_fake_xircom [] = {
164         { USB_DEVICE(XIRCOM_VENDOR_ID, XIRCOM_FAKE_ID) },
165         { USB_DEVICE(ENTREGRA_VENDOR_ID, ENTREGRA_FAKE_ID) },
166         { }
167 };
168 #endif
169
170 static void keyspan_pda_wakeup_write(struct work_struct *work)
171 {
172         struct keyspan_pda_private *priv =
173                 container_of(work, struct keyspan_pda_private, wakeup_work);
174         struct usb_serial_port *port = priv->port;
175
176         tty_wakeup(port->port.tty);
177 }
178
179 static void keyspan_pda_request_unthrottle(struct work_struct *work)
180 {
181         struct keyspan_pda_private *priv =
182                 container_of(work, struct keyspan_pda_private, unthrottle_work);
183         struct usb_serial *serial = priv->serial;
184         int result;
185
186         dbg(" request_unthrottle");
187         /* ask the device to tell us when the tx buffer becomes
188            sufficiently empty */
189         result = usb_control_msg(serial->dev,
190                                  usb_sndctrlpipe(serial->dev, 0),
191                                  7, /* request_unthrottle */
192                                  USB_TYPE_VENDOR | USB_RECIP_INTERFACE
193                                  | USB_DIR_OUT,
194                                  16, /* value: threshold */
195                                  0, /* index */
196                                  NULL,
197                                  0,
198                                  2000);
199         if (result < 0)
200                 dbg("%s - error %d from usb_control_msg",
201                     __func__, result);
202 }
203
204
205 static void keyspan_pda_rx_interrupt(struct urb *urb)
206 {
207         struct usb_serial_port *port = urb->context;
208         struct tty_struct *tty = port->port.tty;
209         unsigned char *data = urb->transfer_buffer;
210         int i;
211         int retval;
212         int status = urb->status;
213         struct keyspan_pda_private *priv;
214         priv = usb_get_serial_port_data(port);
215
216         switch (status) {
217         case 0:
218                 /* success */
219                 break;
220         case -ECONNRESET:
221         case -ENOENT:
222         case -ESHUTDOWN:
223                 /* this urb is terminated, clean up */
224                 dbg("%s - urb shutting down with status: %d",
225                     __func__, status);
226                 return;
227         default:
228                 dbg("%s - nonzero urb status received: %d",
229                     __func__, status);
230                 goto exit;
231         }
232
233         /* see if the message is data or a status interrupt */
234         switch (data[0]) {
235         case 0:
236                 /* rest of message is rx data */
237                 if (urb->actual_length) {
238                         for (i = 1; i < urb->actual_length ; ++i)
239                                 tty_insert_flip_char(tty, data[i], 0);
240                         tty_flip_buffer_push(tty);
241                 }
242                 break;
243         case 1:
244                 /* status interrupt */
245                 dbg(" rx int, d1=%d, d2=%d", data[1], data[2]);
246                 switch (data[1]) {
247                 case 1: /* modemline change */
248                         break;
249                 case 2: /* tx unthrottle interrupt */
250                         priv->tx_throttled = 0;
251                         /* queue up a wakeup at scheduler time */
252                         schedule_work(&priv->wakeup_work);
253                         break;
254                 default:
255                         break;
256                 }
257                 break;
258         default:
259                 break;
260         }
261
262 exit:
263         retval = usb_submit_urb(urb, GFP_ATOMIC);
264         if (retval)
265                 err("%s - usb_submit_urb failed with result %d",
266                      __func__, retval);
267 }
268
269
270 static void keyspan_pda_rx_throttle(struct tty_struct *tty)
271 {
272         /* stop receiving characters. We just turn off the URB request, and
273            let chars pile up in the device. If we're doing hardware
274            flowcontrol, the device will signal the other end when its buffer
275            fills up. If we're doing XON/XOFF, this would be a good time to
276            send an XOFF, although it might make sense to foist that off
277            upon the device too. */
278         struct usb_serial_port *port = tty->driver_data;
279         dbg("keyspan_pda_rx_throttle port %d", port->number);
280         usb_kill_urb(port->interrupt_in_urb);
281 }
282
283
284 static void keyspan_pda_rx_unthrottle(struct tty_struct *tty)
285 {
286         struct usb_serial_port *port = tty->driver_data;
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, GFP_ATOMIC))
291                 dbg(" usb_submit_urb(read urb) failed");
292         return;
293 }
294
295
296 static speed_t keyspan_pda_setbaud(struct usb_serial *serial, speed_t baud)
297 {
298         int rc;
299         int bindex;
300
301         switch (baud) {
302         case 110:
303                 bindex = 0;
304                 break;
305         case 300:
306                 bindex = 1;
307                 break;
308         case 1200:
309                 bindex = 2;
310                 break;
311         case 2400:
312                 bindex = 3;
313                 break;
314         case 4800:
315                 bindex = 4;
316                 break;
317         case 9600:
318                 bindex = 5;
319                 break;
320         case 19200:
321                 bindex = 6;
322                 break;
323         case 38400:
324                 bindex = 7;
325                 break;
326         case 57600:
327                 bindex = 8;
328                 break;
329         case 115200:
330                 bindex = 9;
331                 break;
332         default:
333                 bindex = 5;     /* Default to 9600 */
334                 baud = 9600;
335         }
336
337         /* rather than figure out how to sleep while waiting for this
338            to complete, I just use the "legacy" API. */
339         rc = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
340                              0, /* set baud */
341                              USB_TYPE_VENDOR
342                              | USB_RECIP_INTERFACE
343                              | USB_DIR_OUT, /* type */
344                              bindex, /* value */
345                              0, /* index */
346                              NULL, /* &data */
347                              0, /* size */
348                              2000); /* timeout */
349         if (rc < 0)
350                 return 0;
351         return baud;
352 }
353
354
355 static void keyspan_pda_break_ctl(struct tty_struct *tty, int break_state)
356 {
357         struct usb_serial_port *port = tty->driver_data;
358         struct usb_serial *serial = port->serial;
359         int value;
360         int result;
361
362         if (break_state == -1)
363                 value = 1; /* start break */
364         else
365                 value = 0; /* clear break */
366         result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
367                         4, /* set break */
368                         USB_TYPE_VENDOR | USB_RECIP_INTERFACE | USB_DIR_OUT,
369                         value, 0, NULL, 0, 2000);
370         if (result < 0)
371                 dbg("%s - error %d from usb_control_msg",
372                     __func__, result);
373         /* there is something funky about this.. the TCSBRK that 'cu' performs
374            ought to translate into a break_ctl(-1),break_ctl(0) pair HZ/4
375            seconds apart, but it feels like the break sent isn't as long as it
376            is on /dev/ttyS0 */
377 }
378
379
380 static void keyspan_pda_set_termios(struct tty_struct *tty,
381                 struct usb_serial_port *port, struct ktermios *old_termios)
382 {
383         struct usb_serial *serial = port->serial;
384         speed_t speed;
385
386         /* cflag specifies lots of stuff: number of stop bits, parity, number
387            of data bits, baud. What can the device actually handle?:
388            CSTOPB (1 stop bit or 2)
389            PARENB (parity)
390            CSIZE (5bit .. 8bit)
391            There is minimal hw support for parity (a PSW bit seems to hold the
392            parity of whatever is in the accumulator). The UART either deals
393            with 10 bits (start, 8 data, stop) or 11 bits (start, 8 data,
394            1 special, stop). So, with firmware changes, we could do:
395            8N1: 10 bit
396            8N2: 11 bit, extra bit always (mark?)
397            8[EOMS]1: 11 bit, extra bit is parity
398            7[EOMS]1: 10 bit, b0/b7 is parity
399            7[EOMS]2: 11 bit, b0/b7 is parity, extra bit always (mark?)
400
401            HW flow control is dictated by the tty->termios->c_cflags & CRTSCTS
402            bit.
403
404            For now, just do baud. */
405
406         speed = tty_get_baud_rate(tty);
407         speed = keyspan_pda_setbaud(serial, speed);
408
409         if (speed == 0) {
410                 dbg("can't handle requested baud rate");
411                 /* It hasn't changed so.. */
412                 speed = tty_termios_baud_rate(old_termios);
413         }
414         /* Only speed can change so copy the old h/w parameters
415            then encode the new speed */
416         tty_termios_copy_hw(tty->termios, old_termios);
417         tty_encode_baud_rate(tty, speed, speed);
418 }
419
420
421 /* modem control pins: DTR and RTS are outputs and can be controlled.
422    DCD, RI, DSR, CTS are inputs and can be read. All outputs can also be
423    read. The byte passed is: DTR(b7) DCD RI DSR CTS RTS(b2) unused unused */
424
425 static int keyspan_pda_get_modem_info(struct usb_serial *serial,
426                                       unsigned char *value)
427 {
428         int rc;
429         unsigned char data;
430         rc = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
431                              3, /* get pins */
432                              USB_TYPE_VENDOR|USB_RECIP_INTERFACE|USB_DIR_IN,
433                              0, 0, &data, 1, 2000);
434         if (rc >= 0)
435                 *value = data;
436         return rc;
437 }
438
439
440 static int keyspan_pda_set_modem_info(struct usb_serial *serial,
441                                       unsigned char value)
442 {
443         int rc;
444         rc = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
445                              3, /* set pins */
446                              USB_TYPE_VENDOR|USB_RECIP_INTERFACE|USB_DIR_OUT,
447                              value, 0, NULL, 0, 2000);
448         return rc;
449 }
450
451 static int keyspan_pda_tiocmget(struct tty_struct *tty, struct file *file)
452 {
453         struct usb_serial_port *port = tty->driver_data;
454         struct usb_serial *serial = port->serial;
455         int rc;
456         unsigned char status;
457         int value;
458
459         rc = keyspan_pda_get_modem_info(serial, &status);
460         if (rc < 0)
461                 return rc;
462         value =
463                 ((status & (1<<7)) ? TIOCM_DTR : 0) |
464                 ((status & (1<<6)) ? TIOCM_CAR : 0) |
465                 ((status & (1<<5)) ? TIOCM_RNG : 0) |
466                 ((status & (1<<4)) ? TIOCM_DSR : 0) |
467                 ((status & (1<<3)) ? TIOCM_CTS : 0) |
468                 ((status & (1<<2)) ? TIOCM_RTS : 0);
469         return value;
470 }
471
472 static int keyspan_pda_tiocmset(struct tty_struct *tty, struct file *file,
473                                 unsigned int set, unsigned int clear)
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
480         rc = keyspan_pda_get_modem_info(serial, &status);
481         if (rc < 0)
482                 return rc;
483
484         if (set & TIOCM_RTS)
485                 status |= (1<<2);
486         if (set & TIOCM_DTR)
487                 status |= (1<<7);
488
489         if (clear & TIOCM_RTS)
490                 status &= ~(1<<2);
491         if (clear & TIOCM_DTR)
492                 status &= ~(1<<7);
493         rc = keyspan_pda_set_modem_info(serial, status);
494         return rc;
495 }
496
497 static int keyspan_pda_write(struct tty_struct *tty,
498         struct usb_serial_port *port, 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 = usb_get_serial_port_data(port);
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         spin_lock_bh(&port->lock);
525         if (port->write_urb_busy || priv->tx_throttled) {
526                 spin_unlock_bh(&port->lock);
527                 return 0;
528         }
529         port->write_urb_busy = 1;
530         spin_unlock_bh(&port->lock);
531
532         /* At this point the URB is in our control, nobody else can submit it
533            again (the only sudden transition was the one from EINPROGRESS to
534            finished).  Also, the tx process is not throttled. So we are
535            ready to write. */
536
537         count = (count > port->bulk_out_size) ? port->bulk_out_size : count;
538
539         /* Check if we might overrun the Tx buffer.   If so, ask the
540            device how much room it really has.  This is done only on
541            scheduler time, since usb_control_msg() sleeps. */
542         if (count > priv->tx_room && !in_interrupt()) {
543                 unsigned char room;
544                 rc = usb_control_msg(serial->dev,
545                                      usb_rcvctrlpipe(serial->dev, 0),
546                                      6, /* write_room */
547                                      USB_TYPE_VENDOR | USB_RECIP_INTERFACE
548                                      | USB_DIR_IN,
549                                      0, /* value: 0 means "remaining room" */
550                                      0, /* index */
551                                      &room,
552                                      1,
553                                      2000);
554                 if (rc < 0) {
555                         dbg(" roomquery failed");
556                         goto exit;
557                 }
558                 if (rc == 0) {
559                         dbg(" roomquery returned 0 bytes");
560                         rc = -EIO; /* device didn't return any data */
561                         goto exit;
562                 }
563                 dbg(" roomquery says %d", room);
564                 priv->tx_room = room;
565         }
566         if (count > priv->tx_room) {
567                 /* we're about to completely fill the Tx buffer, so
568                    we'll be throttled afterwards. */
569                 count = priv->tx_room;
570                 request_unthrottle = 1;
571         }
572
573         if (count) {
574                 /* now transfer data */
575                 memcpy(port->write_urb->transfer_buffer, buf, count);
576                 /* send the data out the bulk port */
577                 port->write_urb->transfer_buffer_length = count;
578
579                 priv->tx_room -= count;
580
581                 port->write_urb->dev = port->serial->dev;
582                 rc = usb_submit_urb(port->write_urb, GFP_ATOMIC);
583                 if (rc) {
584                         dbg(" usb_submit_urb(write bulk) failed");
585                         goto exit;
586                 }
587         } else {
588                 /* There wasn't any room left, so we are throttled until
589                    the buffer empties a bit */
590                 request_unthrottle = 1;
591         }
592
593         if (request_unthrottle) {
594                 priv->tx_throttled = 1; /* block writers */
595                 schedule_work(&priv->unthrottle_work);
596         }
597
598         rc = count;
599 exit:
600         if (rc < 0)
601                 port->write_urb_busy = 0;
602         return rc;
603 }
604
605
606 static void keyspan_pda_write_bulk_callback(struct urb *urb)
607 {
608         struct usb_serial_port *port = urb->context;
609         struct keyspan_pda_private *priv;
610
611         port->write_urb_busy = 0;
612         priv = usb_get_serial_port_data(port);
613
614         /* queue up a wakeup at scheduler time */
615         schedule_work(&priv->wakeup_work);
616 }
617
618
619 static int keyspan_pda_write_room(struct tty_struct *tty)
620 {
621         struct usb_serial_port *port = tty->driver_data;
622         struct keyspan_pda_private *priv;
623         priv = usb_get_serial_port_data(port);
624         /* used by n_tty.c for processing of tabs and such. Giving it our
625            conservative guess is probably good enough, but needs testing by
626            running a console through the device. */
627         return priv->tx_room;
628 }
629
630
631 static int keyspan_pda_chars_in_buffer(struct tty_struct *tty)
632 {
633         struct usb_serial_port *port = tty->driver_data;
634         struct keyspan_pda_private *priv;
635         unsigned long flags;
636         int ret = 0;
637
638         priv = usb_get_serial_port_data(port);
639
640         /* when throttled, return at least WAKEUP_CHARS to tell select() (via
641            n_tty.c:normal_poll() ) that we're not writeable. */
642
643         spin_lock_irqsave(&port->lock, flags);
644         if (port->write_urb_busy || priv->tx_throttled)
645                 ret = 256;
646         spin_unlock_irqrestore(&port->lock, flags);
647         return ret;
648 }
649
650
651 static int keyspan_pda_open(struct tty_struct *tty,
652                         struct usb_serial_port *port, struct file *filp)
653 {
654         struct usb_serial *serial = port->serial;
655         unsigned char room;
656         int rc = 0;
657         struct keyspan_pda_private *priv;
658
659         /* find out how much room is in the Tx ring */
660         rc = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
661                              6, /* write_room */
662                              USB_TYPE_VENDOR | USB_RECIP_INTERFACE
663                              | USB_DIR_IN,
664                              0, /* value */
665                              0, /* index */
666                              &room,
667                              1,
668                              2000);
669         if (rc < 0) {
670                 dbg("%s - roomquery failed", __func__);
671                 goto error;
672         }
673         if (rc == 0) {
674                 dbg("%s - roomquery returned 0 bytes", __func__);
675                 rc = -EIO;
676                 goto error;
677         }
678         priv = usb_get_serial_port_data(port);
679         priv->tx_room = room;
680         priv->tx_throttled = room ? 0 : 1;
681
682         /* the normal serial device seems to always turn on DTR and RTS here,
683            so do the same */
684         if (tty && (tty->termios->c_cflag & CBAUD))
685                 keyspan_pda_set_modem_info(serial, (1<<7) | (1<<2));
686         else
687                 keyspan_pda_set_modem_info(serial, 0);
688
689         /*Start reading from the device*/
690         port->interrupt_in_urb->dev = serial->dev;
691         rc = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
692         if (rc) {
693                 dbg("%s - usb_submit_urb(read int) failed", __func__);
694                 goto error;
695         }
696
697 error:
698         return rc;
699 }
700
701
702 static void keyspan_pda_close(struct tty_struct *tty,
703                         struct usb_serial_port *port, struct file *filp)
704 {
705         struct usb_serial *serial = port->serial;
706
707         if (serial->dev) {
708                 /* the normal serial device seems to always shut
709                    off DTR and RTS now */
710                 if (tty->termios->c_cflag & HUPCL)
711                         keyspan_pda_set_modem_info(serial, 0);
712
713                 /* shutdown our bulk reads and writes */
714                 usb_kill_urb(port->write_urb);
715                 usb_kill_urb(port->interrupt_in_urb);
716         }
717 }
718
719
720 /* download the firmware to a "fake" device (pre-renumeration) */
721 static int keyspan_pda_fake_startup(struct usb_serial *serial)
722 {
723         int response;
724         const char *fw_name;
725         const struct ihex_binrec *record;
726         const struct firmware *fw;
727
728         /* download the firmware here ... */
729         response = ezusb_set_reset(serial, 1);
730
731         if (0) { ; }
732 #ifdef KEYSPAN
733         else if (le16_to_cpu(serial->dev->descriptor.idVendor) == KEYSPAN_VENDOR_ID)
734                 fw_name = "keyspan_pda/keyspan_pda.fw";
735 #endif
736 #ifdef XIRCOM
737         else if ((le16_to_cpu(serial->dev->descriptor.idVendor) == XIRCOM_VENDOR_ID) ||
738                  (le16_to_cpu(serial->dev->descriptor.idVendor) == ENTREGRA_VENDOR_ID))
739                 fw_name = "keyspan_pda/xircom_pgs.fw";
740 #endif
741         else {
742                 err("%s: unknown vendor, aborting.", __func__);
743                 return -ENODEV;
744         }
745         if (request_ihex_firmware(&fw, fw_name, &serial->dev->dev)) {
746                 err("failed to load firmware \"%s\"\n", fw_name);
747                 return -ENOENT;
748         }
749         record = (const struct ihex_binrec *)fw->data;
750
751         while (record) {
752                 response = ezusb_writememory(serial, be32_to_cpu(record->addr),
753                                              (unsigned char *)record->data,
754                                              be16_to_cpu(record->len), 0xa0);
755                 if (response < 0) {
756                         err("ezusb_writememory failed for Keyspan PDA "
757                             "firmware (%d %04X %p %d)",
758                             response, be32_to_cpu(record->addr),
759                             record->data, be16_to_cpu(record->len));
760                         break;
761                 }
762                 record = ihex_next_binrec(record);
763         }
764         release_firmware(fw);
765         /* bring device out of reset. Renumeration will occur in a moment
766            and the new device will bind to the real driver */
767         response = ezusb_set_reset(serial, 0);
768
769         /* we want this device to fail to have a driver assigned to it. */
770         return 1;
771 }
772
773 static int keyspan_pda_startup(struct usb_serial *serial)
774 {
775
776         struct keyspan_pda_private *priv;
777
778         /* allocate the private data structures for all ports. Well, for all
779            one ports. */
780
781         priv = kmalloc(sizeof(struct keyspan_pda_private), GFP_KERNEL);
782         if (!priv)
783                 return 1; /* error */
784         usb_set_serial_port_data(serial->port[0], priv);
785         init_waitqueue_head(&serial->port[0]->write_wait);
786         INIT_WORK(&priv->wakeup_work, keyspan_pda_wakeup_write);
787         INIT_WORK(&priv->unthrottle_work, keyspan_pda_request_unthrottle);
788         priv->serial = serial;
789         priv->port = serial->port[0];
790         return 0;
791 }
792
793 static void keyspan_pda_shutdown(struct usb_serial *serial)
794 {
795         dbg("%s", __func__);
796
797         kfree(usb_get_serial_port_data(serial->port[0]));
798 }
799
800 #ifdef KEYSPAN
801 static struct usb_serial_driver keyspan_pda_fake_device = {
802         .driver = {
803                 .owner =        THIS_MODULE,
804                 .name =         "keyspan_pda_pre",
805         },
806         .description =          "Keyspan PDA - (prerenumeration)",
807         .usb_driver =           &keyspan_pda_driver,
808         .id_table =             id_table_fake,
809         .num_ports =            1,
810         .attach =               keyspan_pda_fake_startup,
811 };
812 #endif
813
814 #ifdef XIRCOM
815 static struct usb_serial_driver xircom_pgs_fake_device = {
816         .driver = {
817                 .owner =        THIS_MODULE,
818                 .name =         "xircom_no_firm",
819         },
820         .description =          "Xircom / Entregra PGS - (prerenumeration)",
821         .usb_driver =           &keyspan_pda_driver,
822         .id_table =             id_table_fake_xircom,
823         .num_ports =            1,
824         .attach =               keyspan_pda_fake_startup,
825 };
826 #endif
827
828 static struct usb_serial_driver keyspan_pda_device = {
829         .driver = {
830                 .owner =        THIS_MODULE,
831                 .name =         "keyspan_pda",
832         },
833         .description =          "Keyspan PDA",
834         .usb_driver =           &keyspan_pda_driver,
835         .id_table =             id_table_std,
836         .num_ports =            1,
837         .open =                 keyspan_pda_open,
838         .close =                keyspan_pda_close,
839         .write =                keyspan_pda_write,
840         .write_room =           keyspan_pda_write_room,
841         .write_bulk_callback =  keyspan_pda_write_bulk_callback,
842         .read_int_callback =    keyspan_pda_rx_interrupt,
843         .chars_in_buffer =      keyspan_pda_chars_in_buffer,
844         .throttle =             keyspan_pda_rx_throttle,
845         .unthrottle =           keyspan_pda_rx_unthrottle,
846         .set_termios =          keyspan_pda_set_termios,
847         .break_ctl =            keyspan_pda_break_ctl,
848         .tiocmget =             keyspan_pda_tiocmget,
849         .tiocmset =             keyspan_pda_tiocmset,
850         .attach =               keyspan_pda_startup,
851         .shutdown =             keyspan_pda_shutdown,
852 };
853
854
855 static int __init keyspan_pda_init(void)
856 {
857         int retval;
858         retval = usb_serial_register(&keyspan_pda_device);
859         if (retval)
860                 goto failed_pda_register;
861 #ifdef KEYSPAN
862         retval = usb_serial_register(&keyspan_pda_fake_device);
863         if (retval)
864                 goto failed_pda_fake_register;
865 #endif
866 #ifdef XIRCOM
867         retval = usb_serial_register(&xircom_pgs_fake_device);
868         if (retval)
869                 goto failed_xircom_register;
870 #endif
871         retval = usb_register(&keyspan_pda_driver);
872         if (retval)
873                 goto failed_usb_register;
874         info(DRIVER_DESC " " DRIVER_VERSION);
875         return 0;
876 failed_usb_register:
877 #ifdef XIRCOM
878         usb_serial_deregister(&xircom_pgs_fake_device);
879 failed_xircom_register:
880 #endif /* XIRCOM */
881 #ifdef KEYSPAN
882         usb_serial_deregister(&keyspan_pda_fake_device);
883 #endif
884 #ifdef KEYSPAN
885 failed_pda_fake_register:
886 #endif
887         usb_serial_deregister(&keyspan_pda_device);
888 failed_pda_register:
889         return retval;
890 }
891
892
893 static void __exit keyspan_pda_exit(void)
894 {
895         usb_deregister(&keyspan_pda_driver);
896         usb_serial_deregister(&keyspan_pda_device);
897 #ifdef KEYSPAN
898         usb_serial_deregister(&keyspan_pda_fake_device);
899 #endif
900 #ifdef XIRCOM
901         usb_serial_deregister(&xircom_pgs_fake_device);
902 #endif
903 }
904
905
906 module_init(keyspan_pda_init);
907 module_exit(keyspan_pda_exit);
908
909 MODULE_AUTHOR(DRIVER_AUTHOR);
910 MODULE_DESCRIPTION(DRIVER_DESC);
911 MODULE_LICENSE("GPL");
912
913 module_param(debug, bool, S_IRUGO | S_IWUSR);
914 MODULE_PARM_DESC(debug, "Debug enabled or not");
915