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