keyspan_pda.c 22.9 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12
/*
 * USB Keyspan PDA / Xircom / Entregra Converter driver
 *
 * 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>
 *
 *	This program is free software; you can redistribute it and/or modify
 *	it under the terms of the GNU General Public License as published by
 *	the Free Software Foundation; either version 2 of the License, or
 *	(at your option) any later version.
 *
A
Alan Cox 已提交
13 14
 * See Documentation/usb/usb-serial.txt for more information on using this
 * driver
L
Linus Torvalds 已提交
15 16 17 18 19 20 21 22 23 24 25 26 27
 */


#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/init.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>
28 29
#include <linux/firmware.h>
#include <linux/ihex.h>
A
Alan Cox 已提交
30
#include <linux/uaccess.h>
L
Linus Torvalds 已提交
31
#include <linux/usb.h>
32
#include <linux/usb/serial.h>
L
Linus Torvalds 已提交
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59

static int debug;

/* make a simple define to handle if we are compiling keyspan_pda or xircom support */
#if defined(CONFIG_USB_SERIAL_KEYSPAN_PDA) || defined(CONFIG_USB_SERIAL_KEYSPAN_PDA_MODULE)
	#define KEYSPAN
#else
	#undef KEYSPAN
#endif
#if defined(CONFIG_USB_SERIAL_XIRCOM) || defined(CONFIG_USB_SERIAL_XIRCOM_MODULE)
	#define XIRCOM
#else
	#undef XIRCOM
#endif

/*
 * Version Information
 */
#define DRIVER_VERSION "v1.1"
#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 已提交
60 61
	struct usb_serial	*serial;
	struct usb_serial_port	*port;
L
Linus Torvalds 已提交
62 63 64 65 66 67 68 69 70 71 72 73 74
};


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

/* For Xircom PGSDB9 and older Entregra version of the same device */
#define XIRCOM_VENDOR_ID		0x085a
#define XIRCOM_FAKE_ID			0x8027
#define ENTREGRA_VENDOR_ID		0x1645
#define ENTREGRA_FAKE_ID		0x8093

75
static const struct usb_device_id id_table_combined[] = {
L
Linus Torvalds 已提交
76 77 78 79 80 81 82 83 84 85 86
#ifdef KEYSPAN
	{ USB_DEVICE(KEYSPAN_VENDOR_ID, KEYSPAN_PDA_FAKE_ID) },
#endif
#ifdef XIRCOM
	{ USB_DEVICE(XIRCOM_VENDOR_ID, XIRCOM_FAKE_ID) },
	{ USB_DEVICE(ENTREGRA_VENDOR_ID, ENTREGRA_FAKE_ID) },
#endif
	{ USB_DEVICE(KEYSPAN_VENDOR_ID, KEYSPAN_PDA_ID) },
	{ }						/* Terminating entry */
};

A
Alan Cox 已提交
87
MODULE_DEVICE_TABLE(usb, id_table_combined);
L
Linus Torvalds 已提交
88 89 90 91 92 93

static struct usb_driver keyspan_pda_driver = {
	.name =		"keyspan_pda",
	.probe =	usb_serial_probe,
	.disconnect =	usb_serial_disconnect,
	.id_table =	id_table_combined,
94
	.no_dynamic_id = 	1,
L
Linus Torvalds 已提交
95 96
};

97
static const struct usb_device_id id_table_std[] = {
L
Linus Torvalds 已提交
98 99 100 101 102
	{ USB_DEVICE(KEYSPAN_VENDOR_ID, KEYSPAN_PDA_ID) },
	{ }						/* Terminating entry */
};

#ifdef KEYSPAN
103
static const struct usb_device_id id_table_fake[] = {
L
Linus Torvalds 已提交
104 105 106 107 108 109
	{ USB_DEVICE(KEYSPAN_VENDOR_ID, KEYSPAN_PDA_FAKE_ID) },
	{ }						/* Terminating entry */
};
#endif

#ifdef XIRCOM
110
static const struct usb_device_id id_table_fake_xircom[] = {
A
Alan Cox 已提交
111 112 113
	{ USB_DEVICE(XIRCOM_VENDOR_ID, XIRCOM_FAKE_ID) },
	{ USB_DEVICE(ENTREGRA_VENDOR_ID, ENTREGRA_FAKE_ID) },
	{ }
L
Linus Torvalds 已提交
114 115 116
};
#endif

D
David Howells 已提交
117
static void keyspan_pda_wakeup_write(struct work_struct *work)
L
Linus Torvalds 已提交
118
{
D
David Howells 已提交
119 120 121
	struct keyspan_pda_private *priv =
		container_of(work, struct keyspan_pda_private, wakeup_work);
	struct usb_serial_port *port = priv->port;
A
Alan Cox 已提交
122
	struct tty_struct *tty = tty_port_tty_get(&port->port);
123 124
	if (tty)
		tty_wakeup(tty);
A
Alan Cox 已提交
125
	tty_kref_put(tty);
L
Linus Torvalds 已提交
126 127
}

D
David Howells 已提交
128
static void keyspan_pda_request_unthrottle(struct work_struct *work)
L
Linus Torvalds 已提交
129
{
D
David Howells 已提交
130 131 132
	struct keyspan_pda_private *priv =
		container_of(work, struct keyspan_pda_private, unthrottle_work);
	struct usb_serial *serial = priv->serial;
L
Linus Torvalds 已提交
133 134 135 136 137
	int result;

	dbg(" request_unthrottle");
	/* ask the device to tell us when the tx buffer becomes
	   sufficiently empty */
A
Alan Cox 已提交
138
	result = usb_control_msg(serial->dev,
L
Linus Torvalds 已提交
139 140 141 142 143 144 145 146 147 148
				 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)
A
Alan Cox 已提交
149
		dbg("%s - error %d from usb_control_msg",
150
		    __func__, result);
L
Linus Torvalds 已提交
151 152 153
}


A
Alan Cox 已提交
154
static void keyspan_pda_rx_interrupt(struct urb *urb)
L
Linus Torvalds 已提交
155
{
156
	struct usb_serial_port *port = urb->context;
157
	struct tty_struct *tty;
L
Linus Torvalds 已提交
158
	unsigned char *data = urb->transfer_buffer;
159 160
	int retval;
	int status = urb->status;
L
Linus Torvalds 已提交
161 162 163
	struct keyspan_pda_private *priv;
	priv = usb_get_serial_port_data(port);

164
	switch (status) {
L
Linus Torvalds 已提交
165 166 167 168 169 170 171
	case 0:
		/* success */
		break;
	case -ECONNRESET:
	case -ENOENT:
	case -ESHUTDOWN:
		/* this urb is terminated, clean up */
172
		dbg("%s - urb shutting down with status: %d",
173
		    __func__, status);
174
		return;
L
Linus Torvalds 已提交
175
	default:
176
		dbg("%s - nonzero urb status received: %d",
177
		    __func__, status);
L
Linus Torvalds 已提交
178 179 180
		goto exit;
	}

A
Alan Cox 已提交
181
	/* see if the message is data or a status interrupt */
L
Linus Torvalds 已提交
182 183
	switch (data[0]) {
	case 0:
184 185 186
		tty = tty_port_tty_get(&port->port);
		 /* rest of message is rx data */
		if (tty && urb->actual_length) {
187 188
			tty_insert_flip_string(tty, data + 1,
						urb->actual_length - 1);
L
Linus Torvalds 已提交
189 190
			tty_flip_buffer_push(tty);
		}
191
		tty_kref_put(tty);
L
Linus Torvalds 已提交
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
		break;
	case 1:
		/* status interrupt */
		dbg(" rx int, d1=%d, d2=%d", data[1], data[2]);
		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 已提交
213
	retval = usb_submit_urb(urb, GFP_ATOMIC);
214
	if (retval)
A
Alan Cox 已提交
215 216 217
		dev_err(&port->dev,
			"%s - usb_submit_urb failed with result %d",
			__func__, retval);
L
Linus Torvalds 已提交
218 219 220
}


A
Alan Cox 已提交
221
static void keyspan_pda_rx_throttle(struct tty_struct *tty)
L
Linus Torvalds 已提交
222 223 224 225 226 227 228
{
	/* 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 已提交
229
	struct usb_serial_port *port = tty->driver_data;
L
Linus Torvalds 已提交
230 231 232 233 234
	dbg("keyspan_pda_rx_throttle port %d", port->number);
	usb_kill_urb(port->interrupt_in_urb);
}


A
Alan Cox 已提交
235
static void keyspan_pda_rx_unthrottle(struct tty_struct *tty)
L
Linus Torvalds 已提交
236
{
A
Alan Cox 已提交
237
	struct usb_serial_port *port = tty->driver_data;
L
Linus Torvalds 已提交
238 239 240
	/* just restart the receive interrupt URB */
	dbg("keyspan_pda_rx_unthrottle port %d", port->number);
	port->interrupt_in_urb->dev = port->serial->dev;
241
	if (usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL))
L
Linus Torvalds 已提交
242 243 244 245
		dbg(" usb_submit_urb(read urb) failed");
}


A
Alan Cox 已提交
246
static speed_t keyspan_pda_setbaud(struct usb_serial *serial, speed_t baud)
L
Linus Torvalds 已提交
247 248 249 250
{
	int rc;
	int bindex;

A
Alan Cox 已提交
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
	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 已提交
285 286 287 288 289 290
	}

	/* 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 已提交
291
			     USB_TYPE_VENDOR
L
Linus Torvalds 已提交
292 293 294 295 296 297 298
			     | USB_RECIP_INTERFACE
			     | USB_DIR_OUT, /* type */
			     bindex, /* value */
			     0, /* index */
			     NULL, /* &data */
			     0, /* size */
			     2000); /* timeout */
299 300 301
	if (rc < 0)
		return 0;
	return baud;
L
Linus Torvalds 已提交
302 303 304
}


A
Alan Cox 已提交
305
static void keyspan_pda_break_ctl(struct tty_struct *tty, int break_state)
L
Linus Torvalds 已提交
306
{
A
Alan Cox 已提交
307
	struct usb_serial_port *port = tty->driver_data;
L
Linus Torvalds 已提交
308 309 310 311 312 313 314 315 316
	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 已提交
317 318 319
			4, /* set break */
			USB_TYPE_VENDOR | USB_RECIP_INTERFACE | USB_DIR_OUT,
			value, 0, NULL, 0, 2000);
L
Linus Torvalds 已提交
320
	if (result < 0)
A
Alan Cox 已提交
321
		dbg("%s - error %d from usb_control_msg",
322
		    __func__, result);
L
Linus Torvalds 已提交
323 324 325 326 327 328 329
	/* 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 已提交
330 331
static void keyspan_pda_set_termios(struct tty_struct *tty,
		struct usb_serial_port *port, struct ktermios *old_termios)
L
Linus Torvalds 已提交
332 333
{
	struct usb_serial *serial = port->serial;
334
	speed_t speed;
L
Linus Torvalds 已提交
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355

	/* 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?)

	   HW flow control is dictated by the tty->termios->c_cflags & CRTSCTS
	   bit.

	   For now, just do baud. */

A
Alan Cox 已提交
356
	speed = tty_get_baud_rate(tty);
357 358 359 360 361 362
	speed = keyspan_pda_setbaud(serial, speed);

	if (speed == 0) {
		dbg("can't handle requested baud rate");
		/* It hasn't changed so.. */
		speed = tty_termios_baud_rate(old_termios);
L
Linus Torvalds 已提交
363
	}
364 365
	/* Only speed can change so copy the old h/w parameters
	   then encode the new speed */
A
Alan Cox 已提交
366 367
	tty_termios_copy_hw(tty->termios, old_termios);
	tty_encode_baud_rate(tty, speed, speed);
L
Linus Torvalds 已提交
368 369 370 371 372 373 374 375 376 377 378
}


/* 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;
379 380 381 382 383 384
	u8 *data;

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

L
Linus Torvalds 已提交
385 386 387
	rc = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
			     3, /* get pins */
			     USB_TYPE_VENDOR|USB_RECIP_INTERFACE|USB_DIR_IN,
388
			     0, 0, data, 1, 2000);
389
	if (rc >= 0)
390 391 392
		*value = *data;

	kfree(data);
L
Linus Torvalds 已提交
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
	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;
}

408
static int keyspan_pda_tiocmget(struct tty_struct *tty)
L
Linus Torvalds 已提交
409
{
A
Alan Cox 已提交
410
	struct usb_serial_port *port = tty->driver_data;
L
Linus Torvalds 已提交
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428
	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;
}

429
static int keyspan_pda_tiocmset(struct tty_struct *tty,
L
Linus Torvalds 已提交
430 431
				unsigned int set, unsigned int clear)
{
A
Alan Cox 已提交
432
	struct usb_serial_port *port = tty->driver_data;
L
Linus Torvalds 已提交
433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453
	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 已提交
454 455
static int keyspan_pda_write(struct tty_struct *tty,
	struct usb_serial_port *port, const unsigned char *buf, int count)
L
Linus Torvalds 已提交
456 457 458 459 460 461 462 463 464 465 466 467 468 469 470
{
	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. */
A
Alan Cox 已提交
471
	dbg("keyspan_pda_write(%d)", count);
L
Linus Torvalds 已提交
472 473
	if (count == 0) {
		dbg(" write request of 0 bytes");
A
Alan Cox 已提交
474
		return 0;
L
Linus Torvalds 已提交
475 476 477 478 479 480
	}

	/* 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)
	*/
481
	spin_lock_bh(&port->lock);
482
	if (port->write_urb_busy || priv->tx_throttled) {
483
		spin_unlock_bh(&port->lock);
484
		return 0;
L
Linus Torvalds 已提交
485
	}
486
	port->write_urb_busy = 1;
487
	spin_unlock_bh(&port->lock);
L
Linus Torvalds 已提交
488 489 490 491 492 493 494 495 496 497 498 499

	/* 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()) {
500 501 502 503 504 505 506 507
		u8 *room;

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

A
Alan Cox 已提交
508
		rc = usb_control_msg(serial->dev,
L
Linus Torvalds 已提交
509 510 511 512 513 514
				     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 */
515
				     room,
L
Linus Torvalds 已提交
516 517
				     1,
				     2000);
518 519 520 521 522
		if (rc > 0) {
			dbg(" roomquery says %d", *room);
			priv->tx_room = *room;
		}
		kfree(room);
L
Linus Torvalds 已提交
523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541
		if (rc < 0) {
			dbg(" roomquery failed");
			goto exit;
		}
		if (rc == 0) {
			dbg(" roomquery returned 0 bytes");
			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 已提交
542
		memcpy(port->write_urb->transfer_buffer, buf, count);
L
Linus Torvalds 已提交
543 544
		/* send the data out the bulk port */
		port->write_urb->transfer_buffer_length = count;
545

L
Linus Torvalds 已提交
546 547 548 549 550 551 552 553
		priv->tx_room -= count;

		port->write_urb->dev = port->serial->dev;
		rc = usb_submit_urb(port->write_urb, GFP_ATOMIC);
		if (rc) {
			dbg(" usb_submit_urb(write bulk) failed");
			goto exit;
		}
A
Alan Cox 已提交
554
	} else {
L
Linus Torvalds 已提交
555 556 557 558 559 560 561 562 563 564 565 566
		/* 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:
567 568
	if (rc < 0)
		port->write_urb_busy = 0;
L
Linus Torvalds 已提交
569 570 571 572
	return rc;
}


A
Alan Cox 已提交
573
static void keyspan_pda_write_bulk_callback(struct urb *urb)
L
Linus Torvalds 已提交
574
{
575
	struct usb_serial_port *port = urb->context;
L
Linus Torvalds 已提交
576 577
	struct keyspan_pda_private *priv;

578
	port->write_urb_busy = 0;
L
Linus Torvalds 已提交
579 580 581 582 583 584 585
	priv = usb_get_serial_port_data(port);

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


A
Alan Cox 已提交
586
static int keyspan_pda_write_room(struct tty_struct *tty)
L
Linus Torvalds 已提交
587
{
A
Alan Cox 已提交
588
	struct usb_serial_port *port = tty->driver_data;
L
Linus Torvalds 已提交
589 590 591 592 593
	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 已提交
594
	return priv->tx_room;
L
Linus Torvalds 已提交
595 596 597
}


A
Alan Cox 已提交
598
static int keyspan_pda_chars_in_buffer(struct tty_struct *tty)
L
Linus Torvalds 已提交
599
{
A
Alan Cox 已提交
600
	struct usb_serial_port *port = tty->driver_data;
L
Linus Torvalds 已提交
601
	struct keyspan_pda_private *priv;
602 603
	unsigned long flags;
	int ret = 0;
604

L
Linus Torvalds 已提交
605
	priv = usb_get_serial_port_data(port);
606

L
Linus Torvalds 已提交
607 608
	/* when throttled, return at least WAKEUP_CHARS to tell select() (via
	   n_tty.c:normal_poll() ) that we're not writeable. */
609 610

	spin_lock_irqsave(&port->lock, flags);
611
	if (port->write_urb_busy || priv->tx_throttled)
612 613 614
		ret = 256;
	spin_unlock_irqrestore(&port->lock, flags);
	return ret;
L
Linus Torvalds 已提交
615 616 617
}


618 619 620 621 622 623 624 625 626 627 628 629 630
static void keyspan_pda_dtr_rts(struct usb_serial_port *port, int on)
{
	struct usb_serial *serial = port->serial;

	if (serial->dev) {
		if (on)
			keyspan_pda_set_modem_info(serial, (1<<7) | (1<< 2));
		else
			keyspan_pda_set_modem_info(serial, 0);
	}
}


A
Alan Cox 已提交
631
static int keyspan_pda_open(struct tty_struct *tty,
632
					struct usb_serial_port *port)
L
Linus Torvalds 已提交
633 634
{
	struct usb_serial *serial = port->serial;
635
	u8 *room;
L
Linus Torvalds 已提交
636 637 638 639
	int rc = 0;
	struct keyspan_pda_private *priv;

	/* find out how much room is in the Tx ring */
640 641 642 643
	room = kmalloc(1, GFP_KERNEL);
	if (!room)
		return -ENOMEM;

L
Linus Torvalds 已提交
644 645 646 647 648 649
	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 */
650
			     room,
L
Linus Torvalds 已提交
651 652 653
			     1,
			     2000);
	if (rc < 0) {
654
		dbg("%s - roomquery failed", __func__);
L
Linus Torvalds 已提交
655 656 657
		goto error;
	}
	if (rc == 0) {
658
		dbg("%s - roomquery returned 0 bytes", __func__);
L
Linus Torvalds 已提交
659 660 661 662
		rc = -EIO;
		goto error;
	}
	priv = usb_get_serial_port_data(port);
663 664
	priv->tx_room = *room;
	priv->tx_throttled = *room ? 0 : 1;
L
Linus Torvalds 已提交
665 666 667 668 669

	/*Start reading from the device*/
	port->interrupt_in_urb->dev = serial->dev;
	rc = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
	if (rc) {
670
		dbg("%s - usb_submit_urb(read int) failed", __func__);
L
Linus Torvalds 已提交
671 672 673
		goto error;
	}
error:
674
	kfree(room);
L
Linus Torvalds 已提交
675 676
	return rc;
}
677
static void keyspan_pda_close(struct usb_serial_port *port)
L
Linus Torvalds 已提交
678 679 680 681 682 683 684 685 686 687 688 689
{
	struct usb_serial *serial = port->serial;

	if (serial->dev) {
		/* shutdown our bulk reads and writes */
		usb_kill_urb(port->write_urb);
		usb_kill_urb(port->interrupt_in_urb);
	}
}


/* download the firmware to a "fake" device (pre-renumeration) */
A
Alan Cox 已提交
690
static int keyspan_pda_fake_startup(struct usb_serial *serial)
L
Linus Torvalds 已提交
691 692
{
	int response;
693 694 695
	const char *fw_name;
	const struct ihex_binrec *record;
	const struct firmware *fw;
L
Linus Torvalds 已提交
696 697 698 699

	/* download the firmware here ... */
	response = ezusb_set_reset(serial, 1);

700
	if (0) { ; }
L
Linus Torvalds 已提交
701
#ifdef KEYSPAN
702 703
	else if (le16_to_cpu(serial->dev->descriptor.idVendor) == KEYSPAN_VENDOR_ID)
		fw_name = "keyspan_pda/keyspan_pda.fw";
L
Linus Torvalds 已提交
704 705
#endif
#ifdef XIRCOM
706 707 708
	else if ((le16_to_cpu(serial->dev->descriptor.idVendor) == XIRCOM_VENDOR_ID) ||
		 (le16_to_cpu(serial->dev->descriptor.idVendor) == ENTREGRA_VENDOR_ID))
		fw_name = "keyspan_pda/xircom_pgs.fw";
L
Linus Torvalds 已提交
709
#endif
710
	else {
711 712
		dev_err(&serial->dev->dev, "%s: unknown vendor, aborting.\n",
			__func__);
L
Linus Torvalds 已提交
713 714
		return -ENODEV;
	}
715
	if (request_ihex_firmware(&fw, fw_name, &serial->dev->dev)) {
716 717
		dev_err(&serial->dev->dev, "failed to load firmware \"%s\"\n",
			fw_name);
718 719 720
		return -ENOENT;
	}
	record = (const struct ihex_binrec *)fw->data;
L
Linus Torvalds 已提交
721

722 723
	while (record) {
		response = ezusb_writememory(serial, be32_to_cpu(record->addr),
L
Linus Torvalds 已提交
724
					     (unsigned char *)record->data,
725
					     be16_to_cpu(record->len), 0xa0);
L
Linus Torvalds 已提交
726
		if (response < 0) {
727 728 729 730
			dev_err(&serial->dev->dev, "ezusb_writememory failed "
				"for Keyspan PDA firmware (%d %04X %p %d)\n",
				response, be32_to_cpu(record->addr),
				record->data, be16_to_cpu(record->len));
L
Linus Torvalds 已提交
731 732
			break;
		}
733
		record = ihex_next_binrec(record);
L
Linus Torvalds 已提交
734
	}
735
	release_firmware(fw);
L
Linus Torvalds 已提交
736 737 738 739 740
	/* bring device out of reset. Renumeration will occur in a moment
	   and the new device will bind to the real driver */
	response = ezusb_set_reset(serial, 0);

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

744 745 746 747 748 749 750
#ifdef KEYSPAN
MODULE_FIRMWARE("keyspan_pda/keyspan_pda.fw");
#endif
#ifdef XIRCOM
MODULE_FIRMWARE("keyspan_pda/xircom_pgs.fw");
#endif

A
Alan Cox 已提交
751
static int keyspan_pda_startup(struct usb_serial *serial)
L
Linus Torvalds 已提交
752 753 754 755 756 757 758 759 760
{

	struct keyspan_pda_private *priv;

	/* allocate the private data structures for all ports. Well, for all
	   one ports. */

	priv = kmalloc(sizeof(struct keyspan_pda_private), GFP_KERNEL);
	if (!priv)
A
Alan Cox 已提交
761
		return 1; /* error */
L
Linus Torvalds 已提交
762 763
	usb_set_serial_port_data(serial->port[0], priv);
	init_waitqueue_head(&serial->port[0]->write_wait);
D
David Howells 已提交
764 765 766 767
	INIT_WORK(&priv->wakeup_work, keyspan_pda_wakeup_write);
	INIT_WORK(&priv->unthrottle_work, keyspan_pda_request_unthrottle);
	priv->serial = serial;
	priv->port = serial->port[0];
A
Alan Cox 已提交
768
	return 0;
L
Linus Torvalds 已提交
769 770
}

771
static void keyspan_pda_release(struct usb_serial *serial)
L
Linus Torvalds 已提交
772
{
773
	dbg("%s", __func__);
A
Alan Cox 已提交
774

L
Linus Torvalds 已提交
775 776 777 778
	kfree(usb_get_serial_port_data(serial->port[0]));
}

#ifdef KEYSPAN
779
static struct usb_serial_driver keyspan_pda_fake_device = {
780 781
	.driver = {
		.owner =	THIS_MODULE,
782
		.name =		"keyspan_pda_pre",
783
	},
784
	.description =		"Keyspan PDA - (prerenumeration)",
785
	.usb_driver = 		&keyspan_pda_driver,
L
Linus Torvalds 已提交
786 787 788 789 790 791 792
	.id_table =		id_table_fake,
	.num_ports =		1,
	.attach =		keyspan_pda_fake_startup,
};
#endif

#ifdef XIRCOM
793
static struct usb_serial_driver xircom_pgs_fake_device = {
794 795
	.driver = {
		.owner =	THIS_MODULE,
796
		.name =		"xircom_no_firm",
797
	},
798
	.description =		"Xircom / Entregra PGS - (prerenumeration)",
799
	.usb_driver = 		&keyspan_pda_driver,
L
Linus Torvalds 已提交
800 801 802 803 804 805
	.id_table =		id_table_fake_xircom,
	.num_ports =		1,
	.attach =		keyspan_pda_fake_startup,
};
#endif

806
static struct usb_serial_driver keyspan_pda_device = {
807 808
	.driver = {
		.owner =	THIS_MODULE,
809
		.name =		"keyspan_pda",
810
	},
811
	.description =		"Keyspan PDA",
812
	.usb_driver = 		&keyspan_pda_driver,
L
Linus Torvalds 已提交
813 814
	.id_table =		id_table_std,
	.num_ports =		1,
815
	.dtr_rts =		keyspan_pda_dtr_rts,
L
Linus Torvalds 已提交
816 817 818 819 820 821 822 823 824 825 826 827 828 829
	.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,
	.attach =		keyspan_pda_startup,
830
	.release =		keyspan_pda_release,
L
Linus Torvalds 已提交
831 832 833
};


A
Alan Cox 已提交
834
static int __init keyspan_pda_init(void)
L
Linus Torvalds 已提交
835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852
{
	int retval;
	retval = usb_serial_register(&keyspan_pda_device);
	if (retval)
		goto failed_pda_register;
#ifdef KEYSPAN
	retval = usb_serial_register(&keyspan_pda_fake_device);
	if (retval)
		goto failed_pda_fake_register;
#endif
#ifdef XIRCOM
	retval = usb_serial_register(&xircom_pgs_fake_device);
	if (retval)
		goto failed_xircom_register;
#endif
	retval = usb_register(&keyspan_pda_driver);
	if (retval)
		goto failed_usb_register;
853 854
	printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
	       DRIVER_DESC "\n");
L
Linus Torvalds 已提交
855
	return 0;
A
Alan Cox 已提交
856
failed_usb_register:
L
Linus Torvalds 已提交
857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872
#ifdef XIRCOM
	usb_serial_deregister(&xircom_pgs_fake_device);
failed_xircom_register:
#endif /* XIRCOM */
#ifdef KEYSPAN
	usb_serial_deregister(&keyspan_pda_fake_device);
#endif
#ifdef KEYSPAN
failed_pda_fake_register:
#endif
	usb_serial_deregister(&keyspan_pda_device);
failed_pda_register:
	return retval;
}


A
Alan Cox 已提交
873
static void __exit keyspan_pda_exit(void)
L
Linus Torvalds 已提交
874
{
A
Alan Cox 已提交
875 876
	usb_deregister(&keyspan_pda_driver);
	usb_serial_deregister(&keyspan_pda_device);
L
Linus Torvalds 已提交
877
#ifdef KEYSPAN
A
Alan Cox 已提交
878
	usb_serial_deregister(&keyspan_pda_fake_device);
L
Linus Torvalds 已提交
879 880
#endif
#ifdef XIRCOM
A
Alan Cox 已提交
881
	usb_serial_deregister(&xircom_pgs_fake_device);
L
Linus Torvalds 已提交
882 883 884 885 886 887 888
#endif
}


module_init(keyspan_pda_init);
module_exit(keyspan_pda_exit);

A
Alan Cox 已提交
889 890
MODULE_AUTHOR(DRIVER_AUTHOR);
MODULE_DESCRIPTION(DRIVER_DESC);
L
Linus Torvalds 已提交
891 892 893 894 895
MODULE_LICENSE("GPL");

module_param(debug, bool, S_IRUGO | S_IWUSR);
MODULE_PARM_DESC(debug, "Debug enabled or not");