keyspan_pda.c 20.5 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0+
L
Linus Torvalds 已提交
2
/*
3
 * USB Keyspan PDA / Xircom / Entrega Converter driver
L
Linus Torvalds 已提交
4 5 6 7 8
 *
 * Copyright (C) 1999 - 2001 Greg Kroah-Hartman	<greg@kroah.com>
 * Copyright (C) 1999, 2000 Brian Warner	<warner@lothar.com>
 * Copyright (C) 2000 Al Borchers		<borchers@steinerpoint.com>
 *
9
 * See Documentation/usb/usb-serial.rst for more information on using this
A
Alan Cox 已提交
10
 * driver
L
Linus Torvalds 已提交
11 12 13 14 15 16 17 18 19 20 21 22
 */


#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/slab.h>
#include <linux/tty.h>
#include <linux/tty_driver.h>
#include <linux/tty_flip.h>
#include <linux/module.h>
#include <linux/spinlock.h>
#include <linux/workqueue.h>
A
Alan Cox 已提交
23
#include <linux/uaccess.h>
L
Linus Torvalds 已提交
24
#include <linux/usb.h>
25
#include <linux/usb/serial.h>
26
#include <linux/usb/ezusb.h>
L
Linus Torvalds 已提交
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)
L
Linus Torvalds 已提交
30 31 32 33
	#define KEYSPAN
#else
	#undef KEYSPAN
#endif
34
#if IS_ENABLED(CONFIG_USB_SERIAL_XIRCOM)
L
Linus Torvalds 已提交
35 36 37 38 39 40 41 42 43 44 45 46 47
	#define XIRCOM
#else
	#undef XIRCOM
#endif

#define DRIVER_AUTHOR "Brian Warner <warner@lothar.com>"
#define DRIVER_DESC "USB Keyspan PDA Converter driver"

struct keyspan_pda_private {
	int			tx_room;
	int			tx_throttled;
	struct work_struct			wakeup_work;
	struct work_struct			unthrottle_work;
D
David Howells 已提交
48 49
	struct usb_serial	*serial;
	struct usb_serial_port	*port;
L
Linus Torvalds 已提交
50 51 52 53 54 55 56
};


#define KEYSPAN_VENDOR_ID		0x06cd
#define KEYSPAN_PDA_FAKE_ID		0x0103
#define KEYSPAN_PDA_ID			0x0104 /* no clue */

57
/* For Xircom PGSDB9 and older Entrega version of the same device */
L
Linus Torvalds 已提交
58 59
#define XIRCOM_VENDOR_ID		0x085a
#define XIRCOM_FAKE_ID			0x8027
60
#define XIRCOM_FAKE_ID_2		0x8025 /* "PGMFHUB" serial */
61 62
#define ENTREGA_VENDOR_ID		0x1645
#define ENTREGA_FAKE_ID			0x8093
L
Linus Torvalds 已提交
63

64
static const struct usb_device_id id_table_combined[] = {
L
Linus Torvalds 已提交
65 66 67 68 69
#ifdef KEYSPAN
	{ USB_DEVICE(KEYSPAN_VENDOR_ID, KEYSPAN_PDA_FAKE_ID) },
#endif
#ifdef XIRCOM
	{ USB_DEVICE(XIRCOM_VENDOR_ID, XIRCOM_FAKE_ID) },
70
	{ USB_DEVICE(XIRCOM_VENDOR_ID, XIRCOM_FAKE_ID_2) },
71
	{ USB_DEVICE(ENTREGA_VENDOR_ID, ENTREGA_FAKE_ID) },
L
Linus Torvalds 已提交
72 73 74 75 76
#endif
	{ USB_DEVICE(KEYSPAN_VENDOR_ID, KEYSPAN_PDA_ID) },
	{ }						/* Terminating entry */
};

A
Alan Cox 已提交
77
MODULE_DEVICE_TABLE(usb, id_table_combined);
L
Linus Torvalds 已提交
78

79
static const struct usb_device_id id_table_std[] = {
L
Linus Torvalds 已提交
80 81 82 83 84
	{ USB_DEVICE(KEYSPAN_VENDOR_ID, KEYSPAN_PDA_ID) },
	{ }						/* Terminating entry */
};

#ifdef KEYSPAN
85
static const struct usb_device_id id_table_fake[] = {
L
Linus Torvalds 已提交
86 87 88 89 90 91
	{ USB_DEVICE(KEYSPAN_VENDOR_ID, KEYSPAN_PDA_FAKE_ID) },
	{ }						/* Terminating entry */
};
#endif

#ifdef XIRCOM
92
static const struct usb_device_id id_table_fake_xircom[] = {
A
Alan Cox 已提交
93
	{ USB_DEVICE(XIRCOM_VENDOR_ID, XIRCOM_FAKE_ID) },
94
	{ USB_DEVICE(XIRCOM_VENDOR_ID, XIRCOM_FAKE_ID_2) },
95
	{ USB_DEVICE(ENTREGA_VENDOR_ID, ENTREGA_FAKE_ID) },
A
Alan Cox 已提交
96
	{ }
L
Linus Torvalds 已提交
97 98 99
};
#endif

D
David Howells 已提交
100
static void keyspan_pda_wakeup_write(struct work_struct *work)
L
Linus Torvalds 已提交
101
{
D
David Howells 已提交
102 103 104
	struct keyspan_pda_private *priv =
		container_of(work, struct keyspan_pda_private, wakeup_work);
	struct usb_serial_port *port = priv->port;
J
Jiri Slaby 已提交
105 106

	tty_port_tty_wakeup(&port->port);
L
Linus Torvalds 已提交
107 108
}

D
David Howells 已提交
109
static void keyspan_pda_request_unthrottle(struct work_struct *work)
L
Linus Torvalds 已提交
110
{
D
David Howells 已提交
111 112 113
	struct keyspan_pda_private *priv =
		container_of(work, struct keyspan_pda_private, unthrottle_work);
	struct usb_serial *serial = priv->serial;
L
Linus Torvalds 已提交
114 115 116 117
	int result;

	/* ask the device to tell us when the tx buffer becomes
	   sufficiently empty */
A
Alan Cox 已提交
118
	result = usb_control_msg(serial->dev,
L
Linus Torvalds 已提交
119 120 121 122 123 124 125 126 127 128
				 usb_sndctrlpipe(serial->dev, 0),
				 7, /* request_unthrottle */
				 USB_TYPE_VENDOR | USB_RECIP_INTERFACE
				 | USB_DIR_OUT,
				 16, /* value: threshold */
				 0, /* index */
				 NULL,
				 0,
				 2000);
	if (result < 0)
129 130
		dev_dbg(&serial->dev->dev, "%s - error %d from usb_control_msg\n",
			__func__, result);
L
Linus Torvalds 已提交
131 132 133
}


A
Alan Cox 已提交
134
static void keyspan_pda_rx_interrupt(struct urb *urb)
L
Linus Torvalds 已提交
135
{
136
	struct usb_serial_port *port = urb->context;
L
Linus Torvalds 已提交
137
	unsigned char *data = urb->transfer_buffer;
138
	unsigned int len = urb->actual_length;
139 140
	int retval;
	int status = urb->status;
L
Linus Torvalds 已提交
141 142 143
	struct keyspan_pda_private *priv;
	priv = usb_get_serial_port_data(port);

144
	switch (status) {
L
Linus Torvalds 已提交
145 146 147 148 149 150 151
	case 0:
		/* success */
		break;
	case -ECONNRESET:
	case -ENOENT:
	case -ESHUTDOWN:
		/* this urb is terminated, clean up */
152
		dev_dbg(&urb->dev->dev, "%s - urb shutting down with status: %d\n", __func__, status);
153
		return;
L
Linus Torvalds 已提交
154
	default:
155
		dev_dbg(&urb->dev->dev, "%s - nonzero urb status received: %d\n", __func__, status);
L
Linus Torvalds 已提交
156 157 158
		goto exit;
	}

159 160 161 162 163
	if (len < 1) {
		dev_warn(&port->dev, "short message received\n");
		goto exit;
	}

A
Alan Cox 已提交
164
	/* see if the message is data or a status interrupt */
L
Linus Torvalds 已提交
165 166
	switch (data[0]) {
	case 0:
167
		 /* rest of message is rx data */
168 169 170 171
		if (len < 2)
			break;
		tty_insert_flip_string(&port->port, data + 1, len - 1);
		tty_flip_buffer_push(&port->port);
L
Linus Torvalds 已提交
172 173 174
		break;
	case 1:
		/* status interrupt */
175
		if (len < 2) {
176 177 178
			dev_warn(&port->dev, "short interrupt message received\n");
			break;
		}
179
		dev_dbg(&port->dev, "rx int, d1=%d\n", data[1]);
L
Linus Torvalds 已提交
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
		switch (data[1]) {
		case 1: /* modemline change */
			break;
		case 2: /* tx unthrottle interrupt */
			priv->tx_throttled = 0;
			/* queue up a wakeup at scheduler time */
			schedule_work(&priv->wakeup_work);
			break;
		default:
			break;
		}
		break;
	default:
		break;
	}

exit:
A
Alan Cox 已提交
197
	retval = usb_submit_urb(urb, GFP_ATOMIC);
198
	if (retval)
A
Alan Cox 已提交
199
		dev_err(&port->dev,
200
			"%s - usb_submit_urb failed with result %d\n",
A
Alan Cox 已提交
201
			__func__, retval);
L
Linus Torvalds 已提交
202 203 204
}


A
Alan Cox 已提交
205
static void keyspan_pda_rx_throttle(struct tty_struct *tty)
L
Linus Torvalds 已提交
206 207 208 209 210 211 212
{
	/* stop receiving characters. We just turn off the URB request, and
	   let chars pile up in the device. If we're doing hardware
	   flowcontrol, the device will signal the other end when its buffer
	   fills up. If we're doing XON/XOFF, this would be a good time to
	   send an XOFF, although it might make sense to foist that off
	   upon the device too. */
A
Alan Cox 已提交
213
	struct usb_serial_port *port = tty->driver_data;
214

L
Linus Torvalds 已提交
215 216 217 218
	usb_kill_urb(port->interrupt_in_urb);
}


A
Alan Cox 已提交
219
static void keyspan_pda_rx_unthrottle(struct tty_struct *tty)
L
Linus Torvalds 已提交
220
{
A
Alan Cox 已提交
221
	struct usb_serial_port *port = tty->driver_data;
L
Linus Torvalds 已提交
222
	/* just restart the receive interrupt URB */
223

224
	if (usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL))
225
		dev_dbg(&port->dev, "usb_submit_urb(read urb) failed\n");
L
Linus Torvalds 已提交
226 227 228
}


A
Alan Cox 已提交
229
static speed_t keyspan_pda_setbaud(struct usb_serial *serial, speed_t baud)
L
Linus Torvalds 已提交
230 231 232 233
{
	int rc;
	int bindex;

A
Alan Cox 已提交
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
	switch (baud) {
	case 110:
		bindex = 0;
		break;
	case 300:
		bindex = 1;
		break;
	case 1200:
		bindex = 2;
		break;
	case 2400:
		bindex = 3;
		break;
	case 4800:
		bindex = 4;
		break;
	case 9600:
		bindex = 5;
		break;
	case 19200:
		bindex = 6;
		break;
	case 38400:
		bindex = 7;
		break;
	case 57600:
		bindex = 8;
		break;
	case 115200:
		bindex = 9;
		break;
	default:
		bindex = 5;	/* Default to 9600 */
		baud = 9600;
L
Linus Torvalds 已提交
268 269 270 271 272 273
	}

	/* rather than figure out how to sleep while waiting for this
	   to complete, I just use the "legacy" API. */
	rc = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
			     0, /* set baud */
A
Alan Cox 已提交
274
			     USB_TYPE_VENDOR
L
Linus Torvalds 已提交
275 276 277 278 279 280 281
			     | USB_RECIP_INTERFACE
			     | USB_DIR_OUT, /* type */
			     bindex, /* value */
			     0, /* index */
			     NULL, /* &data */
			     0, /* size */
			     2000); /* timeout */
282 283 284
	if (rc < 0)
		return 0;
	return baud;
L
Linus Torvalds 已提交
285 286 287
}


A
Alan Cox 已提交
288
static void keyspan_pda_break_ctl(struct tty_struct *tty, int break_state)
L
Linus Torvalds 已提交
289
{
A
Alan Cox 已提交
290
	struct usb_serial_port *port = tty->driver_data;
L
Linus Torvalds 已提交
291 292 293 294 295 296 297 298 299
	struct usb_serial *serial = port->serial;
	int value;
	int result;

	if (break_state == -1)
		value = 1; /* start break */
	else
		value = 0; /* clear break */
	result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
A
Alan Cox 已提交
300 301 302
			4, /* set break */
			USB_TYPE_VENDOR | USB_RECIP_INTERFACE | USB_DIR_OUT,
			value, 0, NULL, 0, 2000);
L
Linus Torvalds 已提交
303
	if (result < 0)
304 305
		dev_dbg(&port->dev, "%s - error %d from usb_control_msg\n",
			__func__, result);
L
Linus Torvalds 已提交
306 307 308 309 310 311 312
	/* there is something funky about this.. the TCSBRK that 'cu' performs
	   ought to translate into a break_ctl(-1),break_ctl(0) pair HZ/4
	   seconds apart, but it feels like the break sent isn't as long as it
	   is on /dev/ttyS0 */
}


A
Alan Cox 已提交
313 314
static void keyspan_pda_set_termios(struct tty_struct *tty,
		struct usb_serial_port *port, struct ktermios *old_termios)
L
Linus Torvalds 已提交
315 316
{
	struct usb_serial *serial = port->serial;
317
	speed_t speed;
L
Linus Torvalds 已提交
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333

	/* cflag specifies lots of stuff: number of stop bits, parity, number
	   of data bits, baud. What can the device actually handle?:
	   CSTOPB (1 stop bit or 2)
	   PARENB (parity)
	   CSIZE (5bit .. 8bit)
	   There is minimal hw support for parity (a PSW bit seems to hold the
	   parity of whatever is in the accumulator). The UART either deals
	   with 10 bits (start, 8 data, stop) or 11 bits (start, 8 data,
	   1 special, stop). So, with firmware changes, we could do:
	   8N1: 10 bit
	   8N2: 11 bit, extra bit always (mark?)
	   8[EOMS]1: 11 bit, extra bit is parity
	   7[EOMS]1: 10 bit, b0/b7 is parity
	   7[EOMS]2: 11 bit, b0/b7 is parity, extra bit always (mark?)

334
	   HW flow control is dictated by the tty->termios.c_cflags & CRTSCTS
L
Linus Torvalds 已提交
335 336 337 338
	   bit.

	   For now, just do baud. */

A
Alan Cox 已提交
339
	speed = tty_get_baud_rate(tty);
340 341 342
	speed = keyspan_pda_setbaud(serial, speed);

	if (speed == 0) {
343
		dev_dbg(&port->dev, "can't handle requested baud rate\n");
344 345
		/* It hasn't changed so.. */
		speed = tty_termios_baud_rate(old_termios);
L
Linus Torvalds 已提交
346
	}
347 348
	/* Only speed can change so copy the old h/w parameters
	   then encode the new speed */
349
	tty_termios_copy_hw(&tty->termios, old_termios);
A
Alan Cox 已提交
350
	tty_encode_baud_rate(tty, speed, speed);
L
Linus Torvalds 已提交
351 352 353 354 355 356 357 358 359 360 361
}


/* modem control pins: DTR and RTS are outputs and can be controlled.
   DCD, RI, DSR, CTS are inputs and can be read. All outputs can also be
   read. The byte passed is: DTR(b7) DCD RI DSR CTS RTS(b2) unused unused */

static int keyspan_pda_get_modem_info(struct usb_serial *serial,
				      unsigned char *value)
{
	int rc;
362 363 364 365 366 367
	u8 *data;

	data = kmalloc(1, GFP_KERNEL);
	if (!data)
		return -ENOMEM;

L
Linus Torvalds 已提交
368 369 370
	rc = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
			     3, /* get pins */
			     USB_TYPE_VENDOR|USB_RECIP_INTERFACE|USB_DIR_IN,
371
			     0, 0, data, 1, 2000);
372
	if (rc == 1)
373
		*value = *data;
374 375
	else if (rc >= 0)
		rc = -EIO;
376 377

	kfree(data);
L
Linus Torvalds 已提交
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
	return rc;
}


static int keyspan_pda_set_modem_info(struct usb_serial *serial,
				      unsigned char value)
{
	int rc;
	rc = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
			     3, /* set pins */
			     USB_TYPE_VENDOR|USB_RECIP_INTERFACE|USB_DIR_OUT,
			     value, 0, NULL, 0, 2000);
	return rc;
}

393
static int keyspan_pda_tiocmget(struct tty_struct *tty)
L
Linus Torvalds 已提交
394
{
A
Alan Cox 已提交
395
	struct usb_serial_port *port = tty->driver_data;
L
Linus Torvalds 已提交
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
	struct usb_serial *serial = port->serial;
	int rc;
	unsigned char status;
	int value;

	rc = keyspan_pda_get_modem_info(serial, &status);
	if (rc < 0)
		return rc;
	value =
		((status & (1<<7)) ? TIOCM_DTR : 0) |
		((status & (1<<6)) ? TIOCM_CAR : 0) |
		((status & (1<<5)) ? TIOCM_RNG : 0) |
		((status & (1<<4)) ? TIOCM_DSR : 0) |
		((status & (1<<3)) ? TIOCM_CTS : 0) |
		((status & (1<<2)) ? TIOCM_RTS : 0);
	return value;
}

414
static int keyspan_pda_tiocmset(struct tty_struct *tty,
L
Linus Torvalds 已提交
415 416
				unsigned int set, unsigned int clear)
{
A
Alan Cox 已提交
417
	struct usb_serial_port *port = tty->driver_data;
L
Linus Torvalds 已提交
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438
	struct usb_serial *serial = port->serial;
	int rc;
	unsigned char status;

	rc = keyspan_pda_get_modem_info(serial, &status);
	if (rc < 0)
		return rc;

	if (set & TIOCM_RTS)
		status |= (1<<2);
	if (set & TIOCM_DTR)
		status |= (1<<7);

	if (clear & TIOCM_RTS)
		status &= ~(1<<2);
	if (clear & TIOCM_DTR)
		status &= ~(1<<7);
	rc = keyspan_pda_set_modem_info(serial, status);
	return rc;
}

A
Alan Cox 已提交
439 440
static int keyspan_pda_write(struct tty_struct *tty,
	struct usb_serial_port *port, const unsigned char *buf, int count)
L
Linus Torvalds 已提交
441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456
{
	struct usb_serial *serial = port->serial;
	int request_unthrottle = 0;
	int rc = 0;
	struct keyspan_pda_private *priv;

	priv = usb_get_serial_port_data(port);
	/* guess how much room is left in the device's ring buffer, and if we
	   want to send more than that, check first, updating our notion of
	   what is left. If our write will result in no room left, ask the
	   device to give us an interrupt when the room available rises above
	   a threshold, and hold off all writers (eventually, those using
	   select() or poll() too) until we receive that unthrottle interrupt.
	   Block if we can't write anything at all, otherwise write as much as
	   we can. */
	if (count == 0) {
457
		dev_dbg(&port->dev, "write request of 0 bytes\n");
A
Alan Cox 已提交
458
		return 0;
L
Linus Torvalds 已提交
459 460 461 462 463 464
	}

	/* we might block because of:
	   the TX urb is in-flight (wait until it completes)
	   the device is full (wait until it says there is room)
	*/
465
	spin_lock_bh(&port->lock);
466
	if (!test_bit(0, &port->write_urbs_free) || priv->tx_throttled) {
467
		spin_unlock_bh(&port->lock);
468
		return 0;
L
Linus Torvalds 已提交
469
	}
470
	clear_bit(0, &port->write_urbs_free);
471
	spin_unlock_bh(&port->lock);
L
Linus Torvalds 已提交
472 473 474 475 476 477 478 479 480 481 482 483

	/* At this point the URB is in our control, nobody else can submit it
	   again (the only sudden transition was the one from EINPROGRESS to
	   finished).  Also, the tx process is not throttled. So we are
	   ready to write. */

	count = (count > port->bulk_out_size) ? port->bulk_out_size : count;

	/* Check if we might overrun the Tx buffer.   If so, ask the
	   device how much room it really has.  This is done only on
	   scheduler time, since usb_control_msg() sleeps. */
	if (count > priv->tx_room && !in_interrupt()) {
484 485 486 487 488 489 490 491
		u8 *room;

		room = kmalloc(1, GFP_KERNEL);
		if (!room) {
			rc = -ENOMEM;
			goto exit;
		}

A
Alan Cox 已提交
492
		rc = usb_control_msg(serial->dev,
L
Linus Torvalds 已提交
493 494 495 496 497 498
				     usb_rcvctrlpipe(serial->dev, 0),
				     6, /* write_room */
				     USB_TYPE_VENDOR | USB_RECIP_INTERFACE
				     | USB_DIR_IN,
				     0, /* value: 0 means "remaining room" */
				     0, /* index */
499
				     room,
L
Linus Torvalds 已提交
500 501
				     1,
				     2000);
502
		if (rc > 0) {
503
			dev_dbg(&port->dev, "roomquery says %d\n", *room);
504 505 506
			priv->tx_room = *room;
		}
		kfree(room);
L
Linus Torvalds 已提交
507
		if (rc < 0) {
508
			dev_dbg(&port->dev, "roomquery failed\n");
L
Linus Torvalds 已提交
509 510 511
			goto exit;
		}
		if (rc == 0) {
512
			dev_dbg(&port->dev, "roomquery returned 0 bytes\n");
L
Linus Torvalds 已提交
513 514 515 516 517 518 519 520 521 522 523 524 525
			rc = -EIO; /* device didn't return any data */
			goto exit;
		}
	}
	if (count > priv->tx_room) {
		/* we're about to completely fill the Tx buffer, so
		   we'll be throttled afterwards. */
		count = priv->tx_room;
		request_unthrottle = 1;
	}

	if (count) {
		/* now transfer data */
A
Alan Cox 已提交
526
		memcpy(port->write_urb->transfer_buffer, buf, count);
L
Linus Torvalds 已提交
527 528
		/* send the data out the bulk port */
		port->write_urb->transfer_buffer_length = count;
529

L
Linus Torvalds 已提交
530 531 532 533
		priv->tx_room -= count;

		rc = usb_submit_urb(port->write_urb, GFP_ATOMIC);
		if (rc) {
534
			dev_dbg(&port->dev, "usb_submit_urb(write bulk) failed\n");
L
Linus Torvalds 已提交
535 536
			goto exit;
		}
A
Alan Cox 已提交
537
	} else {
L
Linus Torvalds 已提交
538 539 540 541 542 543 544 545 546 547 548 549
		/* There wasn't any room left, so we are throttled until
		   the buffer empties a bit */
		request_unthrottle = 1;
	}

	if (request_unthrottle) {
		priv->tx_throttled = 1; /* block writers */
		schedule_work(&priv->unthrottle_work);
	}

	rc = count;
exit:
550
	if (rc < 0)
551
		set_bit(0, &port->write_urbs_free);
L
Linus Torvalds 已提交
552 553 554 555
	return rc;
}


A
Alan Cox 已提交
556
static void keyspan_pda_write_bulk_callback(struct urb *urb)
L
Linus Torvalds 已提交
557
{
558
	struct usb_serial_port *port = urb->context;
L
Linus Torvalds 已提交
559 560
	struct keyspan_pda_private *priv;

561
	set_bit(0, &port->write_urbs_free);
L
Linus Torvalds 已提交
562 563 564 565 566 567 568
	priv = usb_get_serial_port_data(port);

	/* queue up a wakeup at scheduler time */
	schedule_work(&priv->wakeup_work);
}


A
Alan Cox 已提交
569
static int keyspan_pda_write_room(struct tty_struct *tty)
L
Linus Torvalds 已提交
570
{
A
Alan Cox 已提交
571
	struct usb_serial_port *port = tty->driver_data;
L
Linus Torvalds 已提交
572 573 574 575 576
	struct keyspan_pda_private *priv;
	priv = usb_get_serial_port_data(port);
	/* used by n_tty.c for processing of tabs and such. Giving it our
	   conservative guess is probably good enough, but needs testing by
	   running a console through the device. */
A
Alan Cox 已提交
577
	return priv->tx_room;
L
Linus Torvalds 已提交
578 579 580
}


A
Alan Cox 已提交
581
static int keyspan_pda_chars_in_buffer(struct tty_struct *tty)
L
Linus Torvalds 已提交
582
{
A
Alan Cox 已提交
583
	struct usb_serial_port *port = tty->driver_data;
L
Linus Torvalds 已提交
584
	struct keyspan_pda_private *priv;
585 586
	unsigned long flags;
	int ret = 0;
587

L
Linus Torvalds 已提交
588
	priv = usb_get_serial_port_data(port);
589

L
Linus Torvalds 已提交
590 591
	/* when throttled, return at least WAKEUP_CHARS to tell select() (via
	   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 596 597
		ret = 256;
	spin_unlock_irqrestore(&port->lock, flags);
	return ret;
L
Linus Torvalds 已提交
598 599 600
}


601 602 603 604
static void keyspan_pda_dtr_rts(struct usb_serial_port *port, int on)
{
	struct usb_serial *serial = port->serial;

605 606 607 608
	if (on)
		keyspan_pda_set_modem_info(serial, (1 << 7) | (1 << 2));
	else
		keyspan_pda_set_modem_info(serial, 0);
609 610 611
}


A
Alan Cox 已提交
612
static int keyspan_pda_open(struct tty_struct *tty,
613
					struct usb_serial_port *port)
L
Linus Torvalds 已提交
614 615
{
	struct usb_serial *serial = port->serial;
616
	u8 *room;
L
Linus Torvalds 已提交
617 618 619 620
	int rc = 0;
	struct keyspan_pda_private *priv;

	/* find out how much room is in the Tx ring */
621 622 623 624
	room = kmalloc(1, GFP_KERNEL);
	if (!room)
		return -ENOMEM;

L
Linus Torvalds 已提交
625 626 627 628 629 630
	rc = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
			     6, /* write_room */
			     USB_TYPE_VENDOR | USB_RECIP_INTERFACE
			     | USB_DIR_IN,
			     0, /* value */
			     0, /* index */
631
			     room,
L
Linus Torvalds 已提交
632 633 634
			     1,
			     2000);
	if (rc < 0) {
635
		dev_dbg(&port->dev, "%s - roomquery failed\n", __func__);
L
Linus Torvalds 已提交
636 637 638
		goto error;
	}
	if (rc == 0) {
639
		dev_dbg(&port->dev, "%s - roomquery returned 0 bytes\n", __func__);
L
Linus Torvalds 已提交
640 641 642 643
		rc = -EIO;
		goto error;
	}
	priv = usb_get_serial_port_data(port);
644 645
	priv->tx_room = *room;
	priv->tx_throttled = *room ? 0 : 1;
L
Linus Torvalds 已提交
646 647 648 649

	/*Start reading from the device*/
	rc = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
	if (rc) {
650
		dev_dbg(&port->dev, "%s - usb_submit_urb(read int) failed\n", __func__);
L
Linus Torvalds 已提交
651 652 653
		goto error;
	}
error:
654
	kfree(room);
L
Linus Torvalds 已提交
655 656
	return rc;
}
657
static void keyspan_pda_close(struct usb_serial_port *port)
L
Linus Torvalds 已提交
658
{
659 660
	usb_kill_urb(port->write_urb);
	usb_kill_urb(port->interrupt_in_urb);
L
Linus Torvalds 已提交
661 662 663 664
}


/* download the firmware to a "fake" device (pre-renumeration) */
A
Alan Cox 已提交
665
static int keyspan_pda_fake_startup(struct usb_serial *serial)
L
Linus Torvalds 已提交
666
{
667
	const char *fw_name;
L
Linus Torvalds 已提交
668 669

	/* download the firmware here ... */
670
	ezusb_fx1_set_reset(serial->dev, 1);
L
Linus Torvalds 已提交
671

672
	if (0) { ; }
L
Linus Torvalds 已提交
673
#ifdef KEYSPAN
674 675
	else if (le16_to_cpu(serial->dev->descriptor.idVendor) == KEYSPAN_VENDOR_ID)
		fw_name = "keyspan_pda/keyspan_pda.fw";
L
Linus Torvalds 已提交
676 677
#endif
#ifdef XIRCOM
678
	else if ((le16_to_cpu(serial->dev->descriptor.idVendor) == XIRCOM_VENDOR_ID) ||
679
		 (le16_to_cpu(serial->dev->descriptor.idVendor) == ENTREGA_VENDOR_ID))
680
		fw_name = "keyspan_pda/xircom_pgs.fw";
L
Linus Torvalds 已提交
681
#endif
682
	else {
683 684
		dev_err(&serial->dev->dev, "%s: unknown vendor, aborting.\n",
			__func__);
L
Linus Torvalds 已提交
685 686
		return -ENODEV;
	}
687 688

	if (ezusb_fx1_ihex_firmware_download(serial->dev, fw_name) < 0) {
689 690
		dev_err(&serial->dev->dev, "failed to load firmware \"%s\"\n",
			fw_name);
691 692
		return -ENOENT;
	}
693 694 695

	/* after downloading firmware Renumeration will occur in a
	  moment and the new device will bind to the real driver */
L
Linus Torvalds 已提交
696 697

	/* we want this device to fail to have a driver assigned to it. */
A
Alan Cox 已提交
698
	return 1;
L
Linus Torvalds 已提交
699 700
}

701 702 703 704 705 706 707
#ifdef KEYSPAN
MODULE_FIRMWARE("keyspan_pda/keyspan_pda.fw");
#endif
#ifdef XIRCOM
MODULE_FIRMWARE("keyspan_pda/xircom_pgs.fw");
#endif

708
static int keyspan_pda_port_probe(struct usb_serial_port *port)
L
Linus Torvalds 已提交
709 710 711 712 713 714
{

	struct keyspan_pda_private *priv;

	priv = kmalloc(sizeof(struct keyspan_pda_private), GFP_KERNEL);
	if (!priv)
715 716
		return -ENOMEM;

D
David Howells 已提交
717 718
	INIT_WORK(&priv->wakeup_work, keyspan_pda_wakeup_write);
	INIT_WORK(&priv->unthrottle_work, keyspan_pda_request_unthrottle);
719 720 721 722 723
	priv->serial = port->serial;
	priv->port = port;

	usb_set_serial_port_data(port, priv);

A
Alan Cox 已提交
724
	return 0;
L
Linus Torvalds 已提交
725 726
}

727
static int keyspan_pda_port_remove(struct usb_serial_port *port)
L
Linus Torvalds 已提交
728
{
729 730 731 732 733 734
	struct keyspan_pda_private *priv;

	priv = usb_get_serial_port_data(port);
	kfree(priv);

	return 0;
L
Linus Torvalds 已提交
735 736 737
}

#ifdef KEYSPAN
738
static struct usb_serial_driver keyspan_pda_fake_device = {
739 740
	.driver = {
		.owner =	THIS_MODULE,
741
		.name =		"keyspan_pda_pre",
742
	},
743
	.description =		"Keyspan PDA - (prerenumeration)",
L
Linus Torvalds 已提交
744 745 746 747 748 749 750
	.id_table =		id_table_fake,
	.num_ports =		1,
	.attach =		keyspan_pda_fake_startup,
};
#endif

#ifdef XIRCOM
751
static struct usb_serial_driver xircom_pgs_fake_device = {
752 753
	.driver = {
		.owner =	THIS_MODULE,
754
		.name =		"xircom_no_firm",
755
	},
756
	.description =		"Xircom / Entrega PGS - (prerenumeration)",
L
Linus Torvalds 已提交
757 758 759 760 761 762
	.id_table =		id_table_fake_xircom,
	.num_ports =		1,
	.attach =		keyspan_pda_fake_startup,
};
#endif

763
static struct usb_serial_driver keyspan_pda_device = {
764 765
	.driver = {
		.owner =	THIS_MODULE,
766
		.name =		"keyspan_pda",
767
	},
768
	.description =		"Keyspan PDA",
L
Linus Torvalds 已提交
769 770
	.id_table =		id_table_std,
	.num_ports =		1,
771 772
	.num_bulk_out =		1,
	.num_interrupt_in =	1,
773
	.dtr_rts =		keyspan_pda_dtr_rts,
L
Linus Torvalds 已提交
774 775 776 777 778 779 780 781 782 783 784 785 786
	.open =			keyspan_pda_open,
	.close =		keyspan_pda_close,
	.write =		keyspan_pda_write,
	.write_room =		keyspan_pda_write_room,
	.write_bulk_callback = 	keyspan_pda_write_bulk_callback,
	.read_int_callback =	keyspan_pda_rx_interrupt,
	.chars_in_buffer =	keyspan_pda_chars_in_buffer,
	.throttle =		keyspan_pda_rx_throttle,
	.unthrottle =		keyspan_pda_rx_unthrottle,
	.set_termios =		keyspan_pda_set_termios,
	.break_ctl =		keyspan_pda_break_ctl,
	.tiocmget =		keyspan_pda_tiocmget,
	.tiocmset =		keyspan_pda_tiocmset,
787 788
	.port_probe =		keyspan_pda_port_probe,
	.port_remove =		keyspan_pda_port_remove,
L
Linus Torvalds 已提交
789 790
};

791 792
static struct usb_serial_driver * const serial_drivers[] = {
	&keyspan_pda_device,
L
Linus Torvalds 已提交
793
#ifdef KEYSPAN
794
	&keyspan_pda_fake_device,
L
Linus Torvalds 已提交
795 796
#endif
#ifdef XIRCOM
797
	&xircom_pgs_fake_device,
L
Linus Torvalds 已提交
798
#endif
799 800
	NULL
};
L
Linus Torvalds 已提交
801

802
module_usb_serial_driver(serial_drivers, id_table_combined);
L
Linus Torvalds 已提交
803

A
Alan Cox 已提交
804 805
MODULE_AUTHOR(DRIVER_AUTHOR);
MODULE_DESCRIPTION(DRIVER_DESC);
L
Linus Torvalds 已提交
806
MODULE_LICENSE("GPL");