xref: /linux/drivers/usb/serial/keyspan_pda.c (revision 5fd54ace4721fc5ce2bb5aef6318fcf17f421460)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * USB Keyspan PDA / Xircom / Entrega Converter driver
4  *
5  * Copyright (C) 1999 - 2001 Greg Kroah-Hartman	<greg@kroah.com>
6  * Copyright (C) 1999, 2000 Brian Warner	<warner@lothar.com>
7  * Copyright (C) 2000 Al Borchers		<borchers@steinerpoint.com>
8  *
9  *	This program is free software; you can redistribute it and/or modify
10  *	it under the terms of the GNU General Public License as published by
11  *	the Free Software Foundation; either version 2 of the License, or
12  *	(at your option) any later version.
13  *
14  * See Documentation/usb/usb-serial.txt for more information on using this
15  * driver
16  */
17 
18 
19 #include <linux/kernel.h>
20 #include <linux/errno.h>
21 #include <linux/slab.h>
22 #include <linux/tty.h>
23 #include <linux/tty_driver.h>
24 #include <linux/tty_flip.h>
25 #include <linux/module.h>
26 #include <linux/spinlock.h>
27 #include <linux/workqueue.h>
28 #include <linux/uaccess.h>
29 #include <linux/usb.h>
30 #include <linux/usb/serial.h>
31 #include <linux/usb/ezusb.h>
32 
33 /* make a simple define to handle if we are compiling keyspan_pda or xircom support */
34 #if IS_ENABLED(CONFIG_USB_SERIAL_KEYSPAN_PDA)
35 	#define KEYSPAN
36 #else
37 	#undef KEYSPAN
38 #endif
39 #if IS_ENABLED(CONFIG_USB_SERIAL_XIRCOM)
40 	#define XIRCOM
41 #else
42 	#undef XIRCOM
43 #endif
44 
45 #define DRIVER_AUTHOR "Brian Warner <warner@lothar.com>"
46 #define DRIVER_DESC "USB Keyspan PDA Converter driver"
47 
48 struct keyspan_pda_private {
49 	int			tx_room;
50 	int			tx_throttled;
51 	struct work_struct			wakeup_work;
52 	struct work_struct			unthrottle_work;
53 	struct usb_serial	*serial;
54 	struct usb_serial_port	*port;
55 };
56 
57 
58 #define KEYSPAN_VENDOR_ID		0x06cd
59 #define KEYSPAN_PDA_FAKE_ID		0x0103
60 #define KEYSPAN_PDA_ID			0x0104 /* no clue */
61 
62 /* For Xircom PGSDB9 and older Entrega version of the same device */
63 #define XIRCOM_VENDOR_ID		0x085a
64 #define XIRCOM_FAKE_ID			0x8027
65 #define XIRCOM_FAKE_ID_2		0x8025 /* "PGMFHUB" serial */
66 #define ENTREGA_VENDOR_ID		0x1645
67 #define ENTREGA_FAKE_ID			0x8093
68 
69 static const struct usb_device_id id_table_combined[] = {
70 #ifdef KEYSPAN
71 	{ USB_DEVICE(KEYSPAN_VENDOR_ID, KEYSPAN_PDA_FAKE_ID) },
72 #endif
73 #ifdef XIRCOM
74 	{ USB_DEVICE(XIRCOM_VENDOR_ID, XIRCOM_FAKE_ID) },
75 	{ USB_DEVICE(XIRCOM_VENDOR_ID, XIRCOM_FAKE_ID_2) },
76 	{ USB_DEVICE(ENTREGA_VENDOR_ID, ENTREGA_FAKE_ID) },
77 #endif
78 	{ USB_DEVICE(KEYSPAN_VENDOR_ID, KEYSPAN_PDA_ID) },
79 	{ }						/* Terminating entry */
80 };
81 
82 MODULE_DEVICE_TABLE(usb, id_table_combined);
83 
84 static const struct usb_device_id id_table_std[] = {
85 	{ USB_DEVICE(KEYSPAN_VENDOR_ID, KEYSPAN_PDA_ID) },
86 	{ }						/* Terminating entry */
87 };
88 
89 #ifdef KEYSPAN
90 static const struct usb_device_id id_table_fake[] = {
91 	{ USB_DEVICE(KEYSPAN_VENDOR_ID, KEYSPAN_PDA_FAKE_ID) },
92 	{ }						/* Terminating entry */
93 };
94 #endif
95 
96 #ifdef XIRCOM
97 static const struct usb_device_id id_table_fake_xircom[] = {
98 	{ USB_DEVICE(XIRCOM_VENDOR_ID, XIRCOM_FAKE_ID) },
99 	{ USB_DEVICE(XIRCOM_VENDOR_ID, XIRCOM_FAKE_ID_2) },
100 	{ USB_DEVICE(ENTREGA_VENDOR_ID, ENTREGA_FAKE_ID) },
101 	{ }
102 };
103 #endif
104 
105 static void keyspan_pda_wakeup_write(struct work_struct *work)
106 {
107 	struct keyspan_pda_private *priv =
108 		container_of(work, struct keyspan_pda_private, wakeup_work);
109 	struct usb_serial_port *port = priv->port;
110 
111 	tty_port_tty_wakeup(&port->port);
112 }
113 
114 static void keyspan_pda_request_unthrottle(struct work_struct *work)
115 {
116 	struct keyspan_pda_private *priv =
117 		container_of(work, struct keyspan_pda_private, unthrottle_work);
118 	struct usb_serial *serial = priv->serial;
119 	int result;
120 
121 	/* ask the device to tell us when the tx buffer becomes
122 	   sufficiently empty */
123 	result = usb_control_msg(serial->dev,
124 				 usb_sndctrlpipe(serial->dev, 0),
125 				 7, /* request_unthrottle */
126 				 USB_TYPE_VENDOR | USB_RECIP_INTERFACE
127 				 | USB_DIR_OUT,
128 				 16, /* value: threshold */
129 				 0, /* index */
130 				 NULL,
131 				 0,
132 				 2000);
133 	if (result < 0)
134 		dev_dbg(&serial->dev->dev, "%s - error %d from usb_control_msg\n",
135 			__func__, result);
136 }
137 
138 
139 static void keyspan_pda_rx_interrupt(struct urb *urb)
140 {
141 	struct usb_serial_port *port = urb->context;
142 	unsigned char *data = urb->transfer_buffer;
143 	unsigned int len = urb->actual_length;
144 	int retval;
145 	int status = urb->status;
146 	struct keyspan_pda_private *priv;
147 	priv = usb_get_serial_port_data(port);
148 
149 	switch (status) {
150 	case 0:
151 		/* success */
152 		break;
153 	case -ECONNRESET:
154 	case -ENOENT:
155 	case -ESHUTDOWN:
156 		/* this urb is terminated, clean up */
157 		dev_dbg(&urb->dev->dev, "%s - urb shutting down with status: %d\n", __func__, status);
158 		return;
159 	default:
160 		dev_dbg(&urb->dev->dev, "%s - nonzero urb status received: %d\n", __func__, status);
161 		goto exit;
162 	}
163 
164 	if (len < 1) {
165 		dev_warn(&port->dev, "short message received\n");
166 		goto exit;
167 	}
168 
169 	/* see if the message is data or a status interrupt */
170 	switch (data[0]) {
171 	case 0:
172 		 /* rest of message is rx data */
173 		if (len < 2)
174 			break;
175 		tty_insert_flip_string(&port->port, data + 1, len - 1);
176 		tty_flip_buffer_push(&port->port);
177 		break;
178 	case 1:
179 		/* status interrupt */
180 		if (len < 3) {
181 			dev_warn(&port->dev, "short interrupt message received\n");
182 			break;
183 		}
184 		dev_dbg(&port->dev, "rx int, d1=%d, d2=%d\n", data[1], data[2]);
185 		switch (data[1]) {
186 		case 1: /* modemline change */
187 			break;
188 		case 2: /* tx unthrottle interrupt */
189 			priv->tx_throttled = 0;
190 			/* queue up a wakeup at scheduler time */
191 			schedule_work(&priv->wakeup_work);
192 			break;
193 		default:
194 			break;
195 		}
196 		break;
197 	default:
198 		break;
199 	}
200 
201 exit:
202 	retval = usb_submit_urb(urb, GFP_ATOMIC);
203 	if (retval)
204 		dev_err(&port->dev,
205 			"%s - usb_submit_urb failed with result %d\n",
206 			__func__, retval);
207 }
208 
209 
210 static void keyspan_pda_rx_throttle(struct tty_struct *tty)
211 {
212 	/* stop receiving characters. We just turn off the URB request, and
213 	   let chars pile up in the device. If we're doing hardware
214 	   flowcontrol, the device will signal the other end when its buffer
215 	   fills up. If we're doing XON/XOFF, this would be a good time to
216 	   send an XOFF, although it might make sense to foist that off
217 	   upon the device too. */
218 	struct usb_serial_port *port = tty->driver_data;
219 
220 	usb_kill_urb(port->interrupt_in_urb);
221 }
222 
223 
224 static void keyspan_pda_rx_unthrottle(struct tty_struct *tty)
225 {
226 	struct usb_serial_port *port = tty->driver_data;
227 	/* just restart the receive interrupt URB */
228 
229 	if (usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL))
230 		dev_dbg(&port->dev, "usb_submit_urb(read urb) failed\n");
231 }
232 
233 
234 static speed_t keyspan_pda_setbaud(struct usb_serial *serial, speed_t baud)
235 {
236 	int rc;
237 	int bindex;
238 
239 	switch (baud) {
240 	case 110:
241 		bindex = 0;
242 		break;
243 	case 300:
244 		bindex = 1;
245 		break;
246 	case 1200:
247 		bindex = 2;
248 		break;
249 	case 2400:
250 		bindex = 3;
251 		break;
252 	case 4800:
253 		bindex = 4;
254 		break;
255 	case 9600:
256 		bindex = 5;
257 		break;
258 	case 19200:
259 		bindex = 6;
260 		break;
261 	case 38400:
262 		bindex = 7;
263 		break;
264 	case 57600:
265 		bindex = 8;
266 		break;
267 	case 115200:
268 		bindex = 9;
269 		break;
270 	default:
271 		bindex = 5;	/* Default to 9600 */
272 		baud = 9600;
273 	}
274 
275 	/* rather than figure out how to sleep while waiting for this
276 	   to complete, I just use the "legacy" API. */
277 	rc = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
278 			     0, /* set baud */
279 			     USB_TYPE_VENDOR
280 			     | USB_RECIP_INTERFACE
281 			     | USB_DIR_OUT, /* type */
282 			     bindex, /* value */
283 			     0, /* index */
284 			     NULL, /* &data */
285 			     0, /* size */
286 			     2000); /* timeout */
287 	if (rc < 0)
288 		return 0;
289 	return baud;
290 }
291 
292 
293 static void keyspan_pda_break_ctl(struct tty_struct *tty, int break_state)
294 {
295 	struct usb_serial_port *port = tty->driver_data;
296 	struct usb_serial *serial = port->serial;
297 	int value;
298 	int result;
299 
300 	if (break_state == -1)
301 		value = 1; /* start break */
302 	else
303 		value = 0; /* clear break */
304 	result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
305 			4, /* set break */
306 			USB_TYPE_VENDOR | USB_RECIP_INTERFACE | USB_DIR_OUT,
307 			value, 0, NULL, 0, 2000);
308 	if (result < 0)
309 		dev_dbg(&port->dev, "%s - error %d from usb_control_msg\n",
310 			__func__, result);
311 	/* there is something funky about this.. the TCSBRK that 'cu' performs
312 	   ought to translate into a break_ctl(-1),break_ctl(0) pair HZ/4
313 	   seconds apart, but it feels like the break sent isn't as long as it
314 	   is on /dev/ttyS0 */
315 }
316 
317 
318 static void keyspan_pda_set_termios(struct tty_struct *tty,
319 		struct usb_serial_port *port, struct ktermios *old_termios)
320 {
321 	struct usb_serial *serial = port->serial;
322 	speed_t speed;
323 
324 	/* cflag specifies lots of stuff: number of stop bits, parity, number
325 	   of data bits, baud. What can the device actually handle?:
326 	   CSTOPB (1 stop bit or 2)
327 	   PARENB (parity)
328 	   CSIZE (5bit .. 8bit)
329 	   There is minimal hw support for parity (a PSW bit seems to hold the
330 	   parity of whatever is in the accumulator). The UART either deals
331 	   with 10 bits (start, 8 data, stop) or 11 bits (start, 8 data,
332 	   1 special, stop). So, with firmware changes, we could do:
333 	   8N1: 10 bit
334 	   8N2: 11 bit, extra bit always (mark?)
335 	   8[EOMS]1: 11 bit, extra bit is parity
336 	   7[EOMS]1: 10 bit, b0/b7 is parity
337 	   7[EOMS]2: 11 bit, b0/b7 is parity, extra bit always (mark?)
338 
339 	   HW flow control is dictated by the tty->termios.c_cflags & CRTSCTS
340 	   bit.
341 
342 	   For now, just do baud. */
343 
344 	speed = tty_get_baud_rate(tty);
345 	speed = keyspan_pda_setbaud(serial, speed);
346 
347 	if (speed == 0) {
348 		dev_dbg(&port->dev, "can't handle requested baud rate\n");
349 		/* It hasn't changed so.. */
350 		speed = tty_termios_baud_rate(old_termios);
351 	}
352 	/* Only speed can change so copy the old h/w parameters
353 	   then encode the new speed */
354 	tty_termios_copy_hw(&tty->termios, old_termios);
355 	tty_encode_baud_rate(tty, speed, speed);
356 }
357 
358 
359 /* modem control pins: DTR and RTS are outputs and can be controlled.
360    DCD, RI, DSR, CTS are inputs and can be read. All outputs can also be
361    read. The byte passed is: DTR(b7) DCD RI DSR CTS RTS(b2) unused unused */
362 
363 static int keyspan_pda_get_modem_info(struct usb_serial *serial,
364 				      unsigned char *value)
365 {
366 	int rc;
367 	u8 *data;
368 
369 	data = kmalloc(1, GFP_KERNEL);
370 	if (!data)
371 		return -ENOMEM;
372 
373 	rc = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
374 			     3, /* get pins */
375 			     USB_TYPE_VENDOR|USB_RECIP_INTERFACE|USB_DIR_IN,
376 			     0, 0, data, 1, 2000);
377 	if (rc >= 0)
378 		*value = *data;
379 
380 	kfree(data);
381 	return rc;
382 }
383 
384 
385 static int keyspan_pda_set_modem_info(struct usb_serial *serial,
386 				      unsigned char value)
387 {
388 	int rc;
389 	rc = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
390 			     3, /* set pins */
391 			     USB_TYPE_VENDOR|USB_RECIP_INTERFACE|USB_DIR_OUT,
392 			     value, 0, NULL, 0, 2000);
393 	return rc;
394 }
395 
396 static int keyspan_pda_tiocmget(struct tty_struct *tty)
397 {
398 	struct usb_serial_port *port = tty->driver_data;
399 	struct usb_serial *serial = port->serial;
400 	int rc;
401 	unsigned char status;
402 	int value;
403 
404 	rc = keyspan_pda_get_modem_info(serial, &status);
405 	if (rc < 0)
406 		return rc;
407 	value =
408 		((status & (1<<7)) ? TIOCM_DTR : 0) |
409 		((status & (1<<6)) ? TIOCM_CAR : 0) |
410 		((status & (1<<5)) ? TIOCM_RNG : 0) |
411 		((status & (1<<4)) ? TIOCM_DSR : 0) |
412 		((status & (1<<3)) ? TIOCM_CTS : 0) |
413 		((status & (1<<2)) ? TIOCM_RTS : 0);
414 	return value;
415 }
416 
417 static int keyspan_pda_tiocmset(struct tty_struct *tty,
418 				unsigned int set, unsigned int clear)
419 {
420 	struct usb_serial_port *port = tty->driver_data;
421 	struct usb_serial *serial = port->serial;
422 	int rc;
423 	unsigned char status;
424 
425 	rc = keyspan_pda_get_modem_info(serial, &status);
426 	if (rc < 0)
427 		return rc;
428 
429 	if (set & TIOCM_RTS)
430 		status |= (1<<2);
431 	if (set & TIOCM_DTR)
432 		status |= (1<<7);
433 
434 	if (clear & TIOCM_RTS)
435 		status &= ~(1<<2);
436 	if (clear & TIOCM_DTR)
437 		status &= ~(1<<7);
438 	rc = keyspan_pda_set_modem_info(serial, status);
439 	return rc;
440 }
441 
442 static int keyspan_pda_write(struct tty_struct *tty,
443 	struct usb_serial_port *port, const unsigned char *buf, int count)
444 {
445 	struct usb_serial *serial = port->serial;
446 	int request_unthrottle = 0;
447 	int rc = 0;
448 	struct keyspan_pda_private *priv;
449 
450 	priv = usb_get_serial_port_data(port);
451 	/* guess how much room is left in the device's ring buffer, and if we
452 	   want to send more than that, check first, updating our notion of
453 	   what is left. If our write will result in no room left, ask the
454 	   device to give us an interrupt when the room available rises above
455 	   a threshold, and hold off all writers (eventually, those using
456 	   select() or poll() too) until we receive that unthrottle interrupt.
457 	   Block if we can't write anything at all, otherwise write as much as
458 	   we can. */
459 	if (count == 0) {
460 		dev_dbg(&port->dev, "write request of 0 bytes\n");
461 		return 0;
462 	}
463 
464 	/* we might block because of:
465 	   the TX urb is in-flight (wait until it completes)
466 	   the device is full (wait until it says there is room)
467 	*/
468 	spin_lock_bh(&port->lock);
469 	if (!test_bit(0, &port->write_urbs_free) || priv->tx_throttled) {
470 		spin_unlock_bh(&port->lock);
471 		return 0;
472 	}
473 	clear_bit(0, &port->write_urbs_free);
474 	spin_unlock_bh(&port->lock);
475 
476 	/* At this point the URB is in our control, nobody else can submit it
477 	   again (the only sudden transition was the one from EINPROGRESS to
478 	   finished).  Also, the tx process is not throttled. So we are
479 	   ready to write. */
480 
481 	count = (count > port->bulk_out_size) ? port->bulk_out_size : count;
482 
483 	/* Check if we might overrun the Tx buffer.   If so, ask the
484 	   device how much room it really has.  This is done only on
485 	   scheduler time, since usb_control_msg() sleeps. */
486 	if (count > priv->tx_room && !in_interrupt()) {
487 		u8 *room;
488 
489 		room = kmalloc(1, GFP_KERNEL);
490 		if (!room) {
491 			rc = -ENOMEM;
492 			goto exit;
493 		}
494 
495 		rc = usb_control_msg(serial->dev,
496 				     usb_rcvctrlpipe(serial->dev, 0),
497 				     6, /* write_room */
498 				     USB_TYPE_VENDOR | USB_RECIP_INTERFACE
499 				     | USB_DIR_IN,
500 				     0, /* value: 0 means "remaining room" */
501 				     0, /* index */
502 				     room,
503 				     1,
504 				     2000);
505 		if (rc > 0) {
506 			dev_dbg(&port->dev, "roomquery says %d\n", *room);
507 			priv->tx_room = *room;
508 		}
509 		kfree(room);
510 		if (rc < 0) {
511 			dev_dbg(&port->dev, "roomquery failed\n");
512 			goto exit;
513 		}
514 		if (rc == 0) {
515 			dev_dbg(&port->dev, "roomquery returned 0 bytes\n");
516 			rc = -EIO; /* device didn't return any data */
517 			goto exit;
518 		}
519 	}
520 	if (count > priv->tx_room) {
521 		/* we're about to completely fill the Tx buffer, so
522 		   we'll be throttled afterwards. */
523 		count = priv->tx_room;
524 		request_unthrottle = 1;
525 	}
526 
527 	if (count) {
528 		/* now transfer data */
529 		memcpy(port->write_urb->transfer_buffer, buf, count);
530 		/* send the data out the bulk port */
531 		port->write_urb->transfer_buffer_length = count;
532 
533 		priv->tx_room -= count;
534 
535 		rc = usb_submit_urb(port->write_urb, GFP_ATOMIC);
536 		if (rc) {
537 			dev_dbg(&port->dev, "usb_submit_urb(write bulk) failed\n");
538 			goto exit;
539 		}
540 	} else {
541 		/* There wasn't any room left, so we are throttled until
542 		   the buffer empties a bit */
543 		request_unthrottle = 1;
544 	}
545 
546 	if (request_unthrottle) {
547 		priv->tx_throttled = 1; /* block writers */
548 		schedule_work(&priv->unthrottle_work);
549 	}
550 
551 	rc = count;
552 exit:
553 	if (rc < 0)
554 		set_bit(0, &port->write_urbs_free);
555 	return rc;
556 }
557 
558 
559 static void keyspan_pda_write_bulk_callback(struct urb *urb)
560 {
561 	struct usb_serial_port *port = urb->context;
562 	struct keyspan_pda_private *priv;
563 
564 	set_bit(0, &port->write_urbs_free);
565 	priv = usb_get_serial_port_data(port);
566 
567 	/* queue up a wakeup at scheduler time */
568 	schedule_work(&priv->wakeup_work);
569 }
570 
571 
572 static int keyspan_pda_write_room(struct tty_struct *tty)
573 {
574 	struct usb_serial_port *port = tty->driver_data;
575 	struct keyspan_pda_private *priv;
576 	priv = usb_get_serial_port_data(port);
577 	/* used by n_tty.c for processing of tabs and such. Giving it our
578 	   conservative guess is probably good enough, but needs testing by
579 	   running a console through the device. */
580 	return priv->tx_room;
581 }
582 
583 
584 static int keyspan_pda_chars_in_buffer(struct tty_struct *tty)
585 {
586 	struct usb_serial_port *port = tty->driver_data;
587 	struct keyspan_pda_private *priv;
588 	unsigned long flags;
589 	int ret = 0;
590 
591 	priv = usb_get_serial_port_data(port);
592 
593 	/* when throttled, return at least WAKEUP_CHARS to tell select() (via
594 	   n_tty.c:normal_poll() ) that we're not writeable. */
595 
596 	spin_lock_irqsave(&port->lock, flags);
597 	if (!test_bit(0, &port->write_urbs_free) || priv->tx_throttled)
598 		ret = 256;
599 	spin_unlock_irqrestore(&port->lock, flags);
600 	return ret;
601 }
602 
603 
604 static void keyspan_pda_dtr_rts(struct usb_serial_port *port, int on)
605 {
606 	struct usb_serial *serial = port->serial;
607 
608 	if (on)
609 		keyspan_pda_set_modem_info(serial, (1 << 7) | (1 << 2));
610 	else
611 		keyspan_pda_set_modem_info(serial, 0);
612 }
613 
614 
615 static int keyspan_pda_open(struct tty_struct *tty,
616 					struct usb_serial_port *port)
617 {
618 	struct usb_serial *serial = port->serial;
619 	u8 *room;
620 	int rc = 0;
621 	struct keyspan_pda_private *priv;
622 
623 	/* find out how much room is in the Tx ring */
624 	room = kmalloc(1, GFP_KERNEL);
625 	if (!room)
626 		return -ENOMEM;
627 
628 	rc = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
629 			     6, /* write_room */
630 			     USB_TYPE_VENDOR | USB_RECIP_INTERFACE
631 			     | USB_DIR_IN,
632 			     0, /* value */
633 			     0, /* index */
634 			     room,
635 			     1,
636 			     2000);
637 	if (rc < 0) {
638 		dev_dbg(&port->dev, "%s - roomquery failed\n", __func__);
639 		goto error;
640 	}
641 	if (rc == 0) {
642 		dev_dbg(&port->dev, "%s - roomquery returned 0 bytes\n", __func__);
643 		rc = -EIO;
644 		goto error;
645 	}
646 	priv = usb_get_serial_port_data(port);
647 	priv->tx_room = *room;
648 	priv->tx_throttled = *room ? 0 : 1;
649 
650 	/*Start reading from the device*/
651 	rc = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
652 	if (rc) {
653 		dev_dbg(&port->dev, "%s - usb_submit_urb(read int) failed\n", __func__);
654 		goto error;
655 	}
656 error:
657 	kfree(room);
658 	return rc;
659 }
660 static void keyspan_pda_close(struct usb_serial_port *port)
661 {
662 	usb_kill_urb(port->write_urb);
663 	usb_kill_urb(port->interrupt_in_urb);
664 }
665 
666 
667 /* download the firmware to a "fake" device (pre-renumeration) */
668 static int keyspan_pda_fake_startup(struct usb_serial *serial)
669 {
670 	int response;
671 	const char *fw_name;
672 
673 	/* download the firmware here ... */
674 	response = ezusb_fx1_set_reset(serial->dev, 1);
675 
676 	if (0) { ; }
677 #ifdef KEYSPAN
678 	else if (le16_to_cpu(serial->dev->descriptor.idVendor) == KEYSPAN_VENDOR_ID)
679 		fw_name = "keyspan_pda/keyspan_pda.fw";
680 #endif
681 #ifdef XIRCOM
682 	else if ((le16_to_cpu(serial->dev->descriptor.idVendor) == XIRCOM_VENDOR_ID) ||
683 		 (le16_to_cpu(serial->dev->descriptor.idVendor) == ENTREGA_VENDOR_ID))
684 		fw_name = "keyspan_pda/xircom_pgs.fw";
685 #endif
686 	else {
687 		dev_err(&serial->dev->dev, "%s: unknown vendor, aborting.\n",
688 			__func__);
689 		return -ENODEV;
690 	}
691 
692 	if (ezusb_fx1_ihex_firmware_download(serial->dev, fw_name) < 0) {
693 		dev_err(&serial->dev->dev, "failed to load firmware \"%s\"\n",
694 			fw_name);
695 		return -ENOENT;
696 	}
697 
698 	/* after downloading firmware Renumeration will occur in a
699 	  moment and the new device will bind to the real driver */
700 
701 	/* we want this device to fail to have a driver assigned to it. */
702 	return 1;
703 }
704 
705 #ifdef KEYSPAN
706 MODULE_FIRMWARE("keyspan_pda/keyspan_pda.fw");
707 #endif
708 #ifdef XIRCOM
709 MODULE_FIRMWARE("keyspan_pda/xircom_pgs.fw");
710 #endif
711 
712 static int keyspan_pda_port_probe(struct usb_serial_port *port)
713 {
714 
715 	struct keyspan_pda_private *priv;
716 
717 	priv = kmalloc(sizeof(struct keyspan_pda_private), GFP_KERNEL);
718 	if (!priv)
719 		return -ENOMEM;
720 
721 	INIT_WORK(&priv->wakeup_work, keyspan_pda_wakeup_write);
722 	INIT_WORK(&priv->unthrottle_work, keyspan_pda_request_unthrottle);
723 	priv->serial = port->serial;
724 	priv->port = port;
725 
726 	usb_set_serial_port_data(port, priv);
727 
728 	return 0;
729 }
730 
731 static int keyspan_pda_port_remove(struct usb_serial_port *port)
732 {
733 	struct keyspan_pda_private *priv;
734 
735 	priv = usb_get_serial_port_data(port);
736 	kfree(priv);
737 
738 	return 0;
739 }
740 
741 #ifdef KEYSPAN
742 static struct usb_serial_driver keyspan_pda_fake_device = {
743 	.driver = {
744 		.owner =	THIS_MODULE,
745 		.name =		"keyspan_pda_pre",
746 	},
747 	.description =		"Keyspan PDA - (prerenumeration)",
748 	.id_table =		id_table_fake,
749 	.num_ports =		1,
750 	.attach =		keyspan_pda_fake_startup,
751 };
752 #endif
753 
754 #ifdef XIRCOM
755 static struct usb_serial_driver xircom_pgs_fake_device = {
756 	.driver = {
757 		.owner =	THIS_MODULE,
758 		.name =		"xircom_no_firm",
759 	},
760 	.description =		"Xircom / Entrega PGS - (prerenumeration)",
761 	.id_table =		id_table_fake_xircom,
762 	.num_ports =		1,
763 	.attach =		keyspan_pda_fake_startup,
764 };
765 #endif
766 
767 static struct usb_serial_driver keyspan_pda_device = {
768 	.driver = {
769 		.owner =	THIS_MODULE,
770 		.name =		"keyspan_pda",
771 	},
772 	.description =		"Keyspan PDA",
773 	.id_table =		id_table_std,
774 	.num_ports =		1,
775 	.num_bulk_out =		1,
776 	.num_interrupt_in =	1,
777 	.dtr_rts =		keyspan_pda_dtr_rts,
778 	.open =			keyspan_pda_open,
779 	.close =		keyspan_pda_close,
780 	.write =		keyspan_pda_write,
781 	.write_room =		keyspan_pda_write_room,
782 	.write_bulk_callback = 	keyspan_pda_write_bulk_callback,
783 	.read_int_callback =	keyspan_pda_rx_interrupt,
784 	.chars_in_buffer =	keyspan_pda_chars_in_buffer,
785 	.throttle =		keyspan_pda_rx_throttle,
786 	.unthrottle =		keyspan_pda_rx_unthrottle,
787 	.set_termios =		keyspan_pda_set_termios,
788 	.break_ctl =		keyspan_pda_break_ctl,
789 	.tiocmget =		keyspan_pda_tiocmget,
790 	.tiocmset =		keyspan_pda_tiocmset,
791 	.port_probe =		keyspan_pda_port_probe,
792 	.port_remove =		keyspan_pda_port_remove,
793 };
794 
795 static struct usb_serial_driver * const serial_drivers[] = {
796 	&keyspan_pda_device,
797 #ifdef KEYSPAN
798 	&keyspan_pda_fake_device,
799 #endif
800 #ifdef XIRCOM
801 	&xircom_pgs_fake_device,
802 #endif
803 	NULL
804 };
805 
806 module_usb_serial_driver(serial_drivers, id_table_combined);
807 
808 MODULE_AUTHOR(DRIVER_AUTHOR);
809 MODULE_DESCRIPTION(DRIVER_DESC);
810 MODULE_LICENSE("GPL");
811