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