keyspan_pda.c 18.1 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
 *
 * 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>
8
 * Copyright (C) 2020 Johan Hovold <johan@kernel.org>
L
Linus Torvalds 已提交
9
 *
10
 * See Documentation/usb/usb-serial.rst for more information on using this
A
Alan Cox 已提交
11
 * driver
L
Linus Torvalds 已提交
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
#define DRIVER_AUTHOR "Brian Warner <warner@lothar.com>, Johan Hovold <johan@kernel.org>"
L
Linus Torvalds 已提交
29 30
#define DRIVER_DESC "USB Keyspan PDA Converter driver"

31
#define KEYSPAN_TX_THRESHOLD	128
32

L
Linus Torvalds 已提交
33 34
struct keyspan_pda_private {
	int			tx_room;
35
	struct work_struct	unthrottle_work;
D
David Howells 已提交
36 37
	struct usb_serial	*serial;
	struct usb_serial_port	*port;
L
Linus Torvalds 已提交
38 39
};

40
static int keyspan_pda_write_start(struct usb_serial_port *port);
L
Linus Torvalds 已提交
41 42 43 44 45

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

46
/* For Xircom PGSDB9 and older Entrega version of the same device */
L
Linus Torvalds 已提交
47 48
#define XIRCOM_VENDOR_ID		0x085a
#define XIRCOM_FAKE_ID			0x8027
49
#define XIRCOM_FAKE_ID_2		0x8025 /* "PGMFHUB" serial */
50 51
#define ENTREGA_VENDOR_ID		0x1645
#define ENTREGA_FAKE_ID			0x8093
L
Linus Torvalds 已提交
52

53
static const struct usb_device_id id_table_combined[] = {
L
Linus Torvalds 已提交
54 55
	{ USB_DEVICE(KEYSPAN_VENDOR_ID, KEYSPAN_PDA_FAKE_ID) },
	{ USB_DEVICE(XIRCOM_VENDOR_ID, XIRCOM_FAKE_ID) },
56
	{ USB_DEVICE(XIRCOM_VENDOR_ID, XIRCOM_FAKE_ID_2) },
57
	{ USB_DEVICE(ENTREGA_VENDOR_ID, ENTREGA_FAKE_ID) },
L
Linus Torvalds 已提交
58 59 60
	{ USB_DEVICE(KEYSPAN_VENDOR_ID, KEYSPAN_PDA_ID) },
	{ }						/* Terminating entry */
};
A
Alan Cox 已提交
61
MODULE_DEVICE_TABLE(usb, id_table_combined);
L
Linus Torvalds 已提交
62

63
static const struct usb_device_id id_table_std[] = {
L
Linus Torvalds 已提交
64 65 66 67
	{ USB_DEVICE(KEYSPAN_VENDOR_ID, KEYSPAN_PDA_ID) },
	{ }						/* Terminating entry */
};

68
static const struct usb_device_id id_table_fake[] = {
L
Linus Torvalds 已提交
69
	{ USB_DEVICE(KEYSPAN_VENDOR_ID, KEYSPAN_PDA_FAKE_ID) },
A
Alan Cox 已提交
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
	{ }						/* Terminating entry */
L
Linus Torvalds 已提交
74 75
};

76 77 78
static int keyspan_pda_get_write_room(struct keyspan_pda_private *priv)
{
	struct usb_serial_port *port = priv->port;
79
	struct usb_serial *serial = port->serial;
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
	u8 *room;
	int rc;

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

	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 means "remaining room" */
			     0, /* index */
			     room,
			     1,
			     2000);
	if (rc != 1) {
		if (rc >= 0)
			rc = -EIO;
		dev_dbg(&port->dev, "roomquery failed: %d\n", rc);
		goto out_free;
	}

	dev_dbg(&port->dev, "roomquery says %d\n", *room);
	rc = *room;
out_free:
	kfree(room);

	return rc;
}

D
David Howells 已提交
112
static void keyspan_pda_request_unthrottle(struct work_struct *work)
L
Linus Torvalds 已提交
113
{
D
David Howells 已提交
114 115
	struct keyspan_pda_private *priv =
		container_of(work, struct keyspan_pda_private, unthrottle_work);
116
	struct usb_serial_port *port = priv->port;
117
	struct usb_serial *serial = port->serial;
118
	unsigned long flags;
L
Linus Torvalds 已提交
119 120
	int result;

121 122
	dev_dbg(&port->dev, "%s\n", __func__);

123 124 125 126
	/*
	 * Ask the device to tell us when the tx buffer becomes
	 * sufficiently empty.
	 */
A
Alan Cox 已提交
127
	result = usb_control_msg(serial->dev,
L
Linus Torvalds 已提交
128 129 130 131
				 usb_sndctrlpipe(serial->dev, 0),
				 7, /* request_unthrottle */
				 USB_TYPE_VENDOR | USB_RECIP_INTERFACE
				 | USB_DIR_OUT,
132
				 KEYSPAN_TX_THRESHOLD,
L
Linus Torvalds 已提交
133 134 135 136 137
				 0, /* index */
				 NULL,
				 0,
				 2000);
	if (result < 0)
138 139
		dev_dbg(&serial->dev->dev, "%s - error %d from usb_control_msg\n",
			__func__, result);
140 141 142 143 144 145 146 147 148
	/*
	 * Need to check available space after requesting notification in case
	 * buffer is already empty so that no notification is sent.
	 */
	result = keyspan_pda_get_write_room(priv);
	if (result > KEYSPAN_TX_THRESHOLD) {
		spin_lock_irqsave(&port->lock, flags);
		priv->tx_room = max(priv->tx_room, result);
		spin_unlock_irqrestore(&port->lock, flags);
L
Linus Torvalds 已提交
149

150 151 152
		usb_serial_port_softint(port);
	}
}
L
Linus Torvalds 已提交
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;
L
Linus Torvalds 已提交
157
	unsigned char *data = urb->transfer_buffer;
158
	unsigned int len = urb->actual_length;
159 160
	int retval;
	int status = urb->status;
L
Linus Torvalds 已提交
161
	struct keyspan_pda_private *priv;
162 163
	unsigned long flags;

L
Linus Torvalds 已提交
164 165
	priv = usb_get_serial_port_data(port);

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

181 182 183 184 185
	if (len < 1) {
		dev_warn(&port->dev, "short message received\n");
		goto exit;
	}

A
Alan Cox 已提交
186
	/* see if the message is data or a status interrupt */
L
Linus Torvalds 已提交
187 188
	switch (data[0]) {
	case 0:
189
		 /* rest of message is rx data */
190 191 192 193
		if (len < 2)
			break;
		tty_insert_flip_string(&port->port, data + 1, len - 1);
		tty_flip_buffer_push(&port->port);
L
Linus Torvalds 已提交
194 195 196
		break;
	case 1:
		/* status interrupt */
197
		if (len < 2) {
198 199 200
			dev_warn(&port->dev, "short interrupt message received\n");
			break;
		}
201
		dev_dbg(&port->dev, "rx int, d1=%d\n", data[1]);
L
Linus Torvalds 已提交
202 203 204 205
		switch (data[1]) {
		case 1: /* modemline change */
			break;
		case 2: /* tx unthrottle interrupt */
206 207 208
			spin_lock_irqsave(&port->lock, flags);
			priv->tx_room = max(priv->tx_room, KEYSPAN_TX_THRESHOLD);
			spin_unlock_irqrestore(&port->lock, flags);
209 210 211

			keyspan_pda_write_start(port);

212
			usb_serial_port_softint(port);
L
Linus Torvalds 已提交
213 214 215 216 217 218 219 220 221 222
			break;
		default:
			break;
		}
		break;
	default:
		break;
	}

exit:
A
Alan Cox 已提交
223
	retval = usb_submit_urb(urb, GFP_ATOMIC);
224
	if (retval)
A
Alan Cox 已提交
225
		dev_err(&port->dev,
226
			"%s - usb_submit_urb failed with result %d\n",
A
Alan Cox 已提交
227
			__func__, retval);
L
Linus Torvalds 已提交
228 229
}

A
Alan Cox 已提交
230
static void keyspan_pda_rx_throttle(struct tty_struct *tty)
L
Linus Torvalds 已提交
231
{
A
Alan Cox 已提交
232
	struct usb_serial_port *port = tty->driver_data;
233

234 235 236 237 238 239 240 241
	/*
	 * 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.
	 */
L
Linus Torvalds 已提交
242 243 244
	usb_kill_urb(port->interrupt_in_urb);
}

A
Alan Cox 已提交
245
static void keyspan_pda_rx_unthrottle(struct tty_struct *tty)
L
Linus Torvalds 已提交
246
{
A
Alan Cox 已提交
247
	struct usb_serial_port *port = tty->driver_data;
248

249
	/* just restart the receive interrupt URB */
250
	if (usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL))
251
		dev_dbg(&port->dev, "usb_submit_urb(read urb) failed\n");
L
Linus Torvalds 已提交
252 253
}

A
Alan Cox 已提交
254
static speed_t keyspan_pda_setbaud(struct usb_serial *serial, speed_t baud)
L
Linus Torvalds 已提交
255 256 257 258
{
	int rc;
	int bindex;

A
Alan Cox 已提交
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 285 286 287 288 289 290 291 292
	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 已提交
293 294 295 296
	}

	rc = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
			     0, /* set baud */
A
Alan Cox 已提交
297
			     USB_TYPE_VENDOR
L
Linus Torvalds 已提交
298 299 300 301 302 303 304
			     | USB_RECIP_INTERFACE
			     | USB_DIR_OUT, /* type */
			     bindex, /* value */
			     0, /* index */
			     NULL, /* &data */
			     0, /* size */
			     2000); /* timeout */
305 306
	if (rc < 0)
		return 0;
307

308
	return baud;
L
Linus Torvalds 已提交
309 310
}

A
Alan Cox 已提交
311
static void keyspan_pda_break_ctl(struct tty_struct *tty, int break_state)
L
Linus Torvalds 已提交
312
{
A
Alan Cox 已提交
313
	struct usb_serial_port *port = tty->driver_data;
L
Linus Torvalds 已提交
314 315 316 317 318 319 320 321
	struct usb_serial *serial = port->serial;
	int value;
	int result;

	if (break_state == -1)
		value = 1; /* start break */
	else
		value = 0; /* clear break */
322

L
Linus Torvalds 已提交
323
	result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
A
Alan Cox 已提交
324 325 326
			4, /* set break */
			USB_TYPE_VENDOR | USB_RECIP_INTERFACE | USB_DIR_OUT,
			value, 0, NULL, 0, 2000);
L
Linus Torvalds 已提交
327
	if (result < 0)
328 329
		dev_dbg(&port->dev, "%s - error %d from usb_control_msg\n",
			__func__, result);
L
Linus Torvalds 已提交
330 331
}

A
Alan Cox 已提交
332 333
static void keyspan_pda_set_termios(struct tty_struct *tty,
		struct usb_serial_port *port, struct ktermios *old_termios)
L
Linus Torvalds 已提交
334 335
{
	struct usb_serial *serial = port->serial;
336
	speed_t speed;
L
Linus Torvalds 已提交
337

338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
	/*
	 * 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 已提交
359
	speed = tty_get_baud_rate(tty);
360 361 362
	speed = keyspan_pda_setbaud(serial, speed);

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

375 376 377 378 379
/*
 * 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.
 */
L
Linus Torvalds 已提交
380 381 382 383
static int keyspan_pda_get_modem_info(struct usb_serial *serial,
				      unsigned char *value)
{
	int rc;
384 385 386 387 388 389
	u8 *data;

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

L
Linus Torvalds 已提交
390 391 392
	rc = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
			     3, /* get pins */
			     USB_TYPE_VENDOR|USB_RECIP_INTERFACE|USB_DIR_IN,
393
			     0, 0, data, 1, 2000);
394
	if (rc == 1)
395
		*value = *data;
396 397
	else if (rc >= 0)
		rc = -EIO;
398 399

	kfree(data);
L
Linus Torvalds 已提交
400 401 402 403 404 405 406 407 408 409 410 411 412 413
	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;
}

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

	value = ((status & BIT(7)) ? TIOCM_DTR : 0) |
		((status & BIT(6)) ? TIOCM_CAR : 0) |
		((status & BIT(5)) ? TIOCM_RNG : 0) |
		((status & BIT(4)) ? TIOCM_DSR : 0) |
		((status & BIT(3)) ? TIOCM_CTS : 0) |
		((status & BIT(2)) ? TIOCM_RTS : 0);

L
Linus Torvalds 已提交
433 434 435
	return value;
}

436
static int keyspan_pda_tiocmset(struct tty_struct *tty,
L
Linus Torvalds 已提交
437 438
				unsigned int set, unsigned int clear)
{
A
Alan Cox 已提交
439
	struct usb_serial_port *port = tty->driver_data;
L
Linus Torvalds 已提交
440 441 442 443 444 445 446 447 448
	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)
449
		status |= BIT(2);
L
Linus Torvalds 已提交
450
	if (set & TIOCM_DTR)
451
		status |= BIT(7);
L
Linus Torvalds 已提交
452 453

	if (clear & TIOCM_RTS)
454
		status &= ~BIT(2);
L
Linus Torvalds 已提交
455
	if (clear & TIOCM_DTR)
456
		status &= ~BIT(7);
L
Linus Torvalds 已提交
457 458 459 460
	rc = keyspan_pda_set_modem_info(serial, status);
	return rc;
}

461
static int keyspan_pda_write_start(struct usb_serial_port *port)
L
Linus Torvalds 已提交
462
{
463
	struct keyspan_pda_private *priv = usb_get_serial_port_data(port);
464
	unsigned long flags;
465 466
	struct urb *urb;
	int count;
467 468
	int room;
	int rc;
L
Linus Torvalds 已提交
469

470 471 472 473 474 475 476 477 478
	/*
	 * Guess how much room is left in the device's ring buffer. 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 but also
	 * query how much room is currently available (in case our guess was
	 * too conservative and the buffer is already empty when the
	 * unthrottle work is scheduled).
	 */

479 480 481 482 483
	/*
	 * 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)
	 */
484
	spin_lock_irqsave(&port->lock, flags);
485

486
	room = priv->tx_room;
487 488 489
	count = kfifo_len(&port->write_fifo);

	if (!test_bit(0, &port->write_urbs_free) || count == 0 || room == 0) {
490
		spin_unlock_irqrestore(&port->lock, flags);
491
		return 0;
L
Linus Torvalds 已提交
492
	}
493 494
	__clear_bit(0, &port->write_urbs_free);

495 496
	if (count > room)
		count = room;
497 498 499 500 501 502 503 504
	if (count > port->bulk_out_size)
		count = port->bulk_out_size;

	urb = port->write_urb;
	count = kfifo_out(&port->write_fifo, urb->transfer_buffer, count);
	urb->transfer_buffer_length = count;

	port->tx_bytes += count;
505
	priv->tx_room -= count;
L
Linus Torvalds 已提交
506

507
	spin_unlock_irqrestore(&port->lock, flags);
L
Linus Torvalds 已提交
508

509
	dev_dbg(&port->dev, "%s - count = %d, txroom = %d\n", __func__, count, room);
L
Linus Torvalds 已提交
510

511
	rc = usb_submit_urb(urb, GFP_ATOMIC);
512 513
	if (rc) {
		dev_dbg(&port->dev, "usb_submit_urb(write bulk) failed\n");
L
Linus Torvalds 已提交
514

515
		spin_lock_irqsave(&port->lock, flags);
516
		port->tx_bytes -= count;
517
		priv->tx_room = max(priv->tx_room, room + count);
518
		__set_bit(0, &port->write_urbs_free);
519
		spin_unlock_irqrestore(&port->lock, flags);
520

521
		return rc;
L
Linus Torvalds 已提交
522 523
	}

524
	if (count == room)
L
Linus Torvalds 已提交
525 526
		schedule_work(&priv->unthrottle_work);

527
	return count;
L
Linus Torvalds 已提交
528 529
}

A
Alan Cox 已提交
530
static void keyspan_pda_write_bulk_callback(struct urb *urb)
L
Linus Torvalds 已提交
531
{
532
	struct usb_serial_port *port = urb->context;
533
	unsigned long flags;
L
Linus Torvalds 已提交
534

535 536 537 538 539 540
	spin_lock_irqsave(&port->lock, flags);
	port->tx_bytes -= urb->transfer_buffer_length;
	__set_bit(0, &port->write_urbs_free);
	spin_unlock_irqrestore(&port->lock, flags);

	keyspan_pda_write_start(port);
L
Linus Torvalds 已提交
541

542
	usb_serial_port_softint(port);
L
Linus Torvalds 已提交
543 544
}

545 546
static int keyspan_pda_write(struct tty_struct *tty, struct usb_serial_port *port,
		const unsigned char *buf, int count)
L
Linus Torvalds 已提交
547
{
548
	int rc;
L
Linus Torvalds 已提交
549

550
	dev_dbg(&port->dev, "%s - count = %d\n", __func__, count);
L
Linus Torvalds 已提交
551

552 553
	if (!count)
		return 0;
554

555
	count = kfifo_in_locked(&port->write_fifo, buf, count, &port->lock);
556

557 558 559
	rc = keyspan_pda_write_start(port);
	if (rc)
		return rc;
560

561
	return count;
L
Linus Torvalds 已提交
562 563
}

564 565 566 567
static void keyspan_pda_dtr_rts(struct usb_serial_port *port, int on)
{
	struct usb_serial *serial = port->serial;

568
	if (on)
569
		keyspan_pda_set_modem_info(serial, BIT(7) | BIT(2));
570 571
	else
		keyspan_pda_set_modem_info(serial, 0);
572 573 574
}


A
Alan Cox 已提交
575
static int keyspan_pda_open(struct tty_struct *tty,
576
					struct usb_serial_port *port)
L
Linus Torvalds 已提交
577
{
578 579
	struct keyspan_pda_private *priv = usb_get_serial_port_data(port);
	int rc;
L
Linus Torvalds 已提交
580 581

	/* find out how much room is in the Tx ring */
582 583 584
	rc = keyspan_pda_get_write_room(priv);
	if (rc < 0)
		return rc;
585

586
	spin_lock_irq(&port->lock);
587
	priv->tx_room = rc;
588
	spin_unlock_irq(&port->lock);
L
Linus Torvalds 已提交
589 590 591

	rc = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
	if (rc) {
592
		dev_dbg(&port->dev, "%s - usb_submit_urb(read int) failed\n", __func__);
593
		return rc;
L
Linus Torvalds 已提交
594
	}
595 596

	return 0;
L
Linus Torvalds 已提交
597
}
598

599
static void keyspan_pda_close(struct usb_serial_port *port)
L
Linus Torvalds 已提交
600
{
601 602
	struct keyspan_pda_private *priv = usb_get_serial_port_data(port);

603 604 605 606
	/*
	 * Stop the interrupt URB first as its completion handler may submit
	 * the write URB.
	 */
607
	usb_kill_urb(port->interrupt_in_urb);
608
	usb_kill_urb(port->write_urb);
609 610

	cancel_work_sync(&priv->unthrottle_work);
L
Linus Torvalds 已提交
611

612 613 614 615
	spin_lock_irq(&port->lock);
	kfifo_reset(&port->write_fifo);
	spin_unlock_irq(&port->lock);
}
L
Linus Torvalds 已提交
616 617

/* download the firmware to a "fake" device (pre-renumeration) */
A
Alan Cox 已提交
618
static int keyspan_pda_fake_startup(struct usb_serial *serial)
L
Linus Torvalds 已提交
619
{
620
	unsigned int vid = le16_to_cpu(serial->dev->descriptor.idVendor);
621
	const char *fw_name;
L
Linus Torvalds 已提交
622 623

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

626 627
	switch (vid) {
	case KEYSPAN_VENDOR_ID:
628
		fw_name = "keyspan_pda/keyspan_pda.fw";
629 630 631
		break;
	case XIRCOM_VENDOR_ID:
	case ENTREGA_VENDOR_ID:
632
		fw_name = "keyspan_pda/xircom_pgs.fw";
633 634
		break;
	default:
635 636
		dev_err(&serial->dev->dev, "%s: unknown vendor, aborting.\n",
			__func__);
L
Linus Torvalds 已提交
637 638
		return -ENODEV;
	}
639 640

	if (ezusb_fx1_ihex_firmware_download(serial->dev, fw_name) < 0) {
641 642
		dev_err(&serial->dev->dev, "failed to load firmware \"%s\"\n",
			fw_name);
643 644
		return -ENOENT;
	}
645

646 647 648 649
	/*
	 * After downloading firmware renumeration will occur in a moment and
	 * the new device will bind to the real driver.
	 */
L
Linus Torvalds 已提交
650

651
	/* We want this device to fail to have a driver assigned to it. */
A
Alan Cox 已提交
652
	return 1;
L
Linus Torvalds 已提交
653 654
}

655 656 657
MODULE_FIRMWARE("keyspan_pda/keyspan_pda.fw");
MODULE_FIRMWARE("keyspan_pda/xircom_pgs.fw");

658
static int keyspan_pda_port_probe(struct usb_serial_port *port)
L
Linus Torvalds 已提交
659 660 661 662 663 664
{

	struct keyspan_pda_private *priv;

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

D
David Howells 已提交
667
	INIT_WORK(&priv->unthrottle_work, keyspan_pda_request_unthrottle);
668 669 670 671
	priv->port = port;

	usb_set_serial_port_data(port, priv);

A
Alan Cox 已提交
672
	return 0;
L
Linus Torvalds 已提交
673 674
}

675
static void keyspan_pda_port_remove(struct usb_serial_port *port)
L
Linus Torvalds 已提交
676
{
677 678 679 680
	struct keyspan_pda_private *priv;

	priv = usb_get_serial_port_data(port);
	kfree(priv);
L
Linus Torvalds 已提交
681 682
}

683
static struct usb_serial_driver keyspan_pda_fake_device = {
684 685
	.driver = {
		.owner =	THIS_MODULE,
686
		.name =		"keyspan_pda_pre",
687
	},
688
	.description =		"Keyspan PDA - (prerenumeration)",
L
Linus Torvalds 已提交
689 690 691 692 693
	.id_table =		id_table_fake,
	.num_ports =		1,
	.attach =		keyspan_pda_fake_startup,
};

694
static struct usb_serial_driver keyspan_pda_device = {
695 696
	.driver = {
		.owner =	THIS_MODULE,
697
		.name =		"keyspan_pda",
698
	},
699
	.description =		"Keyspan PDA",
L
Linus Torvalds 已提交
700 701
	.id_table =		id_table_std,
	.num_ports =		1,
702 703
	.num_bulk_out =		1,
	.num_interrupt_in =	1,
704
	.dtr_rts =		keyspan_pda_dtr_rts,
L
Linus Torvalds 已提交
705 706 707
	.open =			keyspan_pda_open,
	.close =		keyspan_pda_close,
	.write =		keyspan_pda_write,
708
	.write_bulk_callback =	keyspan_pda_write_bulk_callback,
L
Linus Torvalds 已提交
709 710 711 712 713 714 715
	.read_int_callback =	keyspan_pda_rx_interrupt,
	.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,
716 717
	.port_probe =		keyspan_pda_port_probe,
	.port_remove =		keyspan_pda_port_remove,
L
Linus Torvalds 已提交
718 719
};

720 721 722 723 724
static struct usb_serial_driver * const serial_drivers[] = {
	&keyspan_pda_device,
	&keyspan_pda_fake_device,
	NULL
};
L
Linus Torvalds 已提交
725

726
module_usb_serial_driver(serial_drivers, id_table_combined);
L
Linus Torvalds 已提交
727

A
Alan Cox 已提交
728 729
MODULE_AUTHOR(DRIVER_AUTHOR);
MODULE_DESCRIPTION(DRIVER_DESC);
L
Linus Torvalds 已提交
730
MODULE_LICENSE("GPL");