keyspan.c 71.4 KB
Newer Older
L
Linus Torvalds 已提交
1 2
/*
  Keyspan USB to Serial Converter driver
A
Alan Cox 已提交
3

L
Linus Torvalds 已提交
4 5
  (C) Copyright (C) 2000-2001	Hugh Blemings <hugh@blemings.org>
  (C) Copyright (C) 2002	Greg Kroah-Hartman <greg@kroah.com>
A
Alan Cox 已提交
6

L
Linus Torvalds 已提交
7 8 9 10 11 12
  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.

  See http://misc.nu/hugh/keyspan.html for more information.
A
Alan Cox 已提交
13

L
Linus Torvalds 已提交
14 15 16 17 18 19
  Code in this driver inspired by and in a number of places taken
  from Brian Warner's original Keyspan-PDA driver.

  This driver has been put together with the support of Innosys, Inc.
  and Keyspan, Inc the manufacturers of the Keyspan USB-serial products.
  Thanks Guys :)
A
Alan Cox 已提交
20

L
Linus Torvalds 已提交
21 22 23
  Thanks to Paulus for miscellaneous tidy ups, some largish chunks
  of much nicer and/or completely new code and (perhaps most uniquely)
  having the patience to sit down and explain why and where he'd changed
A
Alan Cox 已提交
24 25 26
  stuff.

  Tip 'o the hat to IBM (and previously Linuxcare :) for supporting
L
Linus Torvalds 已提交
27 28 29 30 31 32 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 60 61 62 63 64 65 66 67 68 69 70 71 72
  staff in their work on open source projects.

  Change History

    2003sep04	LPM (Keyspan) add support for new single port product USA19HS.
				Improve setup message handling for all devices.

    Wed Feb 19 22:00:00 PST 2003 (Jeffrey S. Laing <keyspan@jsl.com>)
      Merged the current (1/31/03) Keyspan code with the current (2.4.21-pre4)
      Linux source tree.  The Linux tree lacked support for the 49WLC and
      others.  The Keyspan patches didn't work with the current kernel.

    2003jan30	LPM	add support for the 49WLC and MPR

    Wed Apr 25 12:00:00 PST 2002 (Keyspan)
      Started with Hugh Blemings' code dated Jan 17, 2002.  All adapters
      now supported (including QI and QW).  Modified port open, port
      close, and send setup() logic to fix various data and endpoint
      synchronization bugs and device LED status bugs.  Changed keyspan_
      write_room() to accurately return transmit buffer availability.
      Changed forwardingLength from 1 to 16 for all adapters.

    Fri Oct 12 16:45:00 EST 2001
      Preliminary USA-19QI and USA-28 support (both test OK for me, YMMV)

    Wed Apr 25 12:00:00 PST 2002 (Keyspan)
      Started with Hugh Blemings' code dated Jan 17, 2002.  All adapters
      now supported (including QI and QW).  Modified port open, port
      close, and send setup() logic to fix various data and endpoint
      synchronization bugs and device LED status bugs.  Changed keyspan_
      write_room() to accurately return transmit buffer availability.
      Changed forwardingLength from 1 to 16 for all adapters.

    Fri Oct 12 16:45:00 EST 2001
      Preliminary USA-19QI and USA-28 support (both test OK for me, YMMV)

    Mon Oct  8 14:29:00 EST 2001 hugh
      Fixed bug that prevented mulitport devices operating correctly
      if they weren't the first unit attached.

    Sat Oct  6 12:31:21 EST 2001 hugh
      Added support for USA-28XA and -28XB, misc cleanups, break support
      for usa26 based models thanks to David Gibson.

    Thu May 31 11:56:42 PDT 2001 gkh
      switched from using spinlock to a semaphore
A
Alan Cox 已提交
73

L
Linus Torvalds 已提交
74 75
    (04/08/2001) gb
	Identify version on module load.
A
Alan Cox 已提交
76

L
Linus Torvalds 已提交
77 78
    (11/01/2000) Adam J. Richter
	usb_device_id table support.
A
Alan Cox 已提交
79

L
Linus Torvalds 已提交
80 81 82
    Tue Oct 10 23:15:33 EST 2000 Hugh
      Merged Paul's changes with my USA-49W mods.  Work in progress
      still...
A
Alan Cox 已提交
83

L
Linus Torvalds 已提交
84 85 86
    Wed Jul 19 14:00:42 EST 2000 gkh
      Added module_init and module_exit functions to handle the fact that
      this driver is a loadable module now.
A
Alan Cox 已提交
87

L
Linus Torvalds 已提交
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
    Tue Jul 18 16:14:52 EST 2000 Hugh
      Basic character input/output for USA-19 now mostly works,
      fixed at 9600 baud for the moment.

    Sat Jul  8 11:11:48 EST 2000 Hugh
      First public release - nothing works except the firmware upload.
      Tested on PPC and x86 architectures, seems to behave...
*/


#include <linux/kernel.h>
#include <linux/jiffies.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>
108 109
#include <linux/firmware.h>
#include <linux/ihex.h>
A
Alan Cox 已提交
110
#include <linux/uaccess.h>
L
Linus Torvalds 已提交
111
#include <linux/usb.h>
112
#include <linux/usb/serial.h>
L
Linus Torvalds 已提交
113 114 115 116 117 118 119
#include "keyspan.h"

static int debug;

/*
 * Version Information
 */
120
#define DRIVER_VERSION "v1.1.5"
L
Linus Torvalds 已提交
121 122 123 124 125
#define DRIVER_AUTHOR "Hugh Blemings <hugh@misc.nu"
#define DRIVER_DESC "Keyspan USB to Serial Converter Driver"

#define INSTAT_BUFLEN	32
#define GLOCONT_BUFLEN	64
126
#define INDAT49W_BUFLEN	512
L
Linus Torvalds 已提交
127 128 129 130 131 132 133 134

	/* Per device and per port private data */
struct keyspan_serial_private {
	const struct keyspan_device_details	*device_details;

	struct urb	*instat_urb;
	char		instat_buf[INSTAT_BUFLEN];

A
Alan Cox 已提交
135 136
	/* added to support 49wg, where data from all 4 ports comes in
	   on 1 EP and high-speed supported */
137 138 139
	struct urb	*indat_urb;
	char		indat_buf[INDAT49W_BUFLEN];

L
Linus Torvalds 已提交
140 141 142
	/* XXX this one probably will need a lock */
	struct urb	*glocont_urb;
	char		glocont_buf[GLOCONT_BUFLEN];
A
Alan Cox 已提交
143
	char		ctrl_buf[8];	/* for EP0 control message */
L
Linus Torvalds 已提交
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
};

struct keyspan_port_private {
	/* Keep track of which input & output endpoints to use */
	int		in_flip;
	int		out_flip;

	/* Keep duplicate of device details in each port
	   structure as well - simplifies some of the
	   callback functions etc. */
	const struct keyspan_device_details	*device_details;

	/* Input endpoints and buffer for this port */
	struct urb	*in_urbs[2];
	char		in_buffer[2][64];
	/* Output endpoints and buffer for this port */
	struct urb	*out_urbs[2];
	char		out_buffer[2][64];

	/* Input ack endpoint */
	struct urb	*inack_urb;
	char		inack_buffer[1];

	/* Output control endpoint */
	struct urb	*outcont_urb;
	char		outcont_buffer[64];

	/* Settings for the port */
	int		baud;
	int		old_baud;
	unsigned int	cflag;
	unsigned int	old_cflag;
	enum		{flow_none, flow_cts, flow_xon} flow_control;
	int		rts_state;	/* Handshaking pins (outputs) */
	int		dtr_state;
	int		cts_state;	/* Handshaking pins (inputs) */
	int		dsr_state;
	int		dcd_state;
	int		ri_state;
	int		break_on;

	unsigned long	tx_start_time[2];
	int		resend_cont;	/* need to resend control packet */
};

/* Include Keyspan message headers.  All current Keyspan Adapters
190
   make use of one of five message formats which are referred
A
Alan Cox 已提交
191 192
   to as USA-26, USA-28, USA-49, USA-90, USA-67 by Keyspan and
   within this driver. */
L
Linus Torvalds 已提交
193 194 195 196
#include "keyspan_usa26msg.h"
#include "keyspan_usa28msg.h"
#include "keyspan_usa49msg.h"
#include "keyspan_usa90msg.h"
197
#include "keyspan_usa67msg.h"
A
Alan Cox 已提交
198

L
Linus Torvalds 已提交
199 200

/* Functions used by new usb-serial code. */
A
Alan Cox 已提交
201
static int __init keyspan_init(void)
L
Linus Torvalds 已提交
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
{
	int retval;
	retval = usb_serial_register(&keyspan_pre_device);
	if (retval)
		goto failed_pre_device_register;
	retval = usb_serial_register(&keyspan_1port_device);
	if (retval)
		goto failed_1port_device_register;
	retval = usb_serial_register(&keyspan_2port_device);
	if (retval)
		goto failed_2port_device_register;
	retval = usb_serial_register(&keyspan_4port_device);
	if (retval)
		goto failed_4port_device_register;
	retval = usb_register(&keyspan_driver);
A
Alan Cox 已提交
217
	if (retval)
L
Linus Torvalds 已提交
218 219
		goto failed_usb_register;

220 221
	printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
	       DRIVER_DESC "\n");
L
Linus Torvalds 已提交
222 223 224 225 226 227 228 229 230 231 232 233 234 235

	return 0;
failed_usb_register:
	usb_serial_deregister(&keyspan_4port_device);
failed_4port_device_register:
	usb_serial_deregister(&keyspan_2port_device);
failed_2port_device_register:
	usb_serial_deregister(&keyspan_1port_device);
failed_1port_device_register:
	usb_serial_deregister(&keyspan_pre_device);
failed_pre_device_register:
	return retval;
}

A
Alan Cox 已提交
236
static void __exit keyspan_exit(void)
L
Linus Torvalds 已提交
237
{
A
Alan Cox 已提交
238 239 240 241 242
	usb_deregister(&keyspan_driver);
	usb_serial_deregister(&keyspan_pre_device);
	usb_serial_deregister(&keyspan_1port_device);
	usb_serial_deregister(&keyspan_2port_device);
	usb_serial_deregister(&keyspan_4port_device);
L
Linus Torvalds 已提交
243 244 245 246 247
}

module_init(keyspan_init);
module_exit(keyspan_exit);

A
Alan Cox 已提交
248
static void keyspan_break_ctl(struct tty_struct *tty, int break_state)
L
Linus Torvalds 已提交
249
{
A
Alan Cox 已提交
250
	struct usb_serial_port *port = tty->driver_data;
L
Linus Torvalds 已提交
251 252
	struct keyspan_port_private 	*p_priv;

A
Alan Cox 已提交
253
	dbg("%s", __func__);
L
Linus Torvalds 已提交
254 255 256 257 258 259 260 261 262 263 264 265

	p_priv = usb_get_serial_port_data(port);

	if (break_state == -1)
		p_priv->break_on = 1;
	else
		p_priv->break_on = 0;

	keyspan_send_setup(port, 0);
}


A
Alan Cox 已提交
266
static void keyspan_set_termios(struct tty_struct *tty,
A
Alan Cox 已提交
267
		struct usb_serial_port *port, struct ktermios *old_termios)
L
Linus Torvalds 已提交
268 269 270 271 272 273
{
	int				baud_rate, device_port;
	struct keyspan_port_private 	*p_priv;
	const struct keyspan_device_details	*d_details;
	unsigned int 			cflag;

274
	dbg("%s", __func__);
L
Linus Torvalds 已提交
275 276 277

	p_priv = usb_get_serial_port_data(port);
	d_details = p_priv->device_details;
A
Alan Cox 已提交
278
	cflag = tty->termios->c_cflag;
L
Linus Torvalds 已提交
279 280 281 282
	device_port = port->number - port->serial->minor;

	/* Baud rate calculation takes baud rate as an integer
	   so other rates can be generated if desired. */
A
Alan Cox 已提交
283
	baud_rate = tty_get_baud_rate(tty);
A
Alan Cox 已提交
284
	/* If no match or invalid, don't change */
A
Alan Cox 已提交
285
	if (d_details->calculate_baud_rate(baud_rate, d_details->baudclk,
L
Linus Torvalds 已提交
286 287
				NULL, NULL, NULL, device_port) == KEYSPAN_BAUD_RATE_OK) {
		/* FIXME - more to do here to ensure rate changes cleanly */
A
Alan Cox 已提交
288
		/* FIXME - calcuate exact rate from divisor ? */
L
Linus Torvalds 已提交
289
		p_priv->baud = baud_rate;
A
Alan Cox 已提交
290 291
	} else
		baud_rate = tty_termios_baud_rate(old_termios);
L
Linus Torvalds 已提交
292

A
Alan Cox 已提交
293
	tty_encode_baud_rate(tty, baud_rate, baud_rate);
L
Linus Torvalds 已提交
294 295 296 297
	/* set CTS/RTS handshake etc. */
	p_priv->cflag = cflag;
	p_priv->flow_control = (cflag & CRTSCTS)? flow_cts: flow_none;

A
Alan Cox 已提交
298 299 300
	/* Mark/Space not supported */
	tty->termios->c_cflag &= ~CMSPAR;

L
Linus Torvalds 已提交
301 302 303
	keyspan_send_setup(port, 0);
}

A
Alan Cox 已提交
304
static int keyspan_tiocmget(struct tty_struct *tty, struct file *file)
L
Linus Torvalds 已提交
305
{
A
Alan Cox 已提交
306 307
	struct usb_serial_port *port = tty->driver_data;
	struct keyspan_port_private *p_priv = usb_get_serial_port_data(port);
L
Linus Torvalds 已提交
308
	unsigned int			value;
A
Alan Cox 已提交
309

L
Linus Torvalds 已提交
310 311 312 313 314
	value = ((p_priv->rts_state) ? TIOCM_RTS : 0) |
		((p_priv->dtr_state) ? TIOCM_DTR : 0) |
		((p_priv->cts_state) ? TIOCM_CTS : 0) |
		((p_priv->dsr_state) ? TIOCM_DSR : 0) |
		((p_priv->dcd_state) ? TIOCM_CAR : 0) |
A
Alan Cox 已提交
315
		((p_priv->ri_state) ? TIOCM_RNG : 0);
L
Linus Torvalds 已提交
316 317 318 319

	return value;
}

A
Alan Cox 已提交
320
static int keyspan_tiocmset(struct tty_struct *tty, struct file *file,
L
Linus Torvalds 已提交
321 322
			    unsigned int set, unsigned int clear)
{
A
Alan Cox 已提交
323 324
	struct usb_serial_port *port = tty->driver_data;
	struct keyspan_port_private *p_priv = usb_get_serial_port_data(port);
A
Alan Cox 已提交
325

L
Linus Torvalds 已提交
326 327 328 329 330 331 332 333 334 335 336 337
	if (set & TIOCM_RTS)
		p_priv->rts_state = 1;
	if (set & TIOCM_DTR)
		p_priv->dtr_state = 1;
	if (clear & TIOCM_RTS)
		p_priv->rts_state = 0;
	if (clear & TIOCM_DTR)
		p_priv->dtr_state = 0;
	keyspan_send_setup(port, 0);
	return 0;
}

A
Alan Cox 已提交
338 339 340 341
/* Write function is similar for the four protocols used
   with only a minor change for usa90 (usa19hs) required */
static int keyspan_write(struct tty_struct *tty,
	struct usb_serial_port *port, const unsigned char *buf, int count)
L
Linus Torvalds 已提交
342 343 344 345 346 347
{
	struct keyspan_port_private 	*p_priv;
	const struct keyspan_device_details	*d_details;
	int				flip;
	int 				left, todo;
	struct urb			*this_urb;
A
Alan Cox 已提交
348
	int 				err, maxDataLen, dataOffset;
L
Linus Torvalds 已提交
349 350 351 352 353

	p_priv = usb_get_serial_port_data(port);
	d_details = p_priv->device_details;

	if (d_details->msg_format == msg_usa90) {
A
Alan Cox 已提交
354
		maxDataLen = 64;
L
Linus Torvalds 已提交
355 356 357 358 359
		dataOffset = 0;
	} else {
		maxDataLen = 63;
		dataOffset = 1;
	}
A
Alan Cox 已提交
360

L
Linus Torvalds 已提交
361
	dbg("%s - for port %d (%d chars), flip=%d",
362
	    __func__, port->number, count, p_priv->out_flip);
L
Linus Torvalds 已提交
363 364 365 366 367 368 369

	for (left = count; left > 0; left -= todo) {
		todo = left;
		if (todo > maxDataLen)
			todo = maxDataLen;

		flip = p_priv->out_flip;
A
Alan Cox 已提交
370

L
Linus Torvalds 已提交
371
		/* Check we have a valid urb/endpoint before we use it... */
A
Alan Cox 已提交
372 373
		this_urb = p_priv->out_urbs[flip];
		if (this_urb == NULL) {
L
Linus Torvalds 已提交
374
			/* no bulk out, so return 0 bytes written */
375
			dbg("%s - no output urb :(", __func__);
L
Linus Torvalds 已提交
376 377 378
			return count;
		}

A
Alan Cox 已提交
379 380
		dbg("%s - endpoint %d flip %d",
			__func__, usb_pipeendpoint(this_urb->pipe), flip);
L
Linus Torvalds 已提交
381 382

		if (this_urb->status == -EINPROGRESS) {
A
Alan Cox 已提交
383 384
			if (time_before(jiffies,
					p_priv->tx_start_time[flip] + 10 * HZ))
L
Linus Torvalds 已提交
385 386 387 388 389
				break;
			usb_unlink_urb(this_urb);
			break;
		}

A
Alan Cox 已提交
390 391
		/* First byte in buffer is "last flag" (except for usa19hx)
		   - unused so for now so set to zero */
L
Linus Torvalds 已提交
392 393
		((char *)this_urb->transfer_buffer)[0] = 0;

A
Alan Cox 已提交
394
		memcpy(this_urb->transfer_buffer + dataOffset, buf, todo);
L
Linus Torvalds 已提交
395 396 397 398 399 400
		buf += todo;

		/* send the data out the bulk port */
		this_urb->transfer_buffer_length = todo + dataOffset;

		this_urb->dev = port->serial->dev;
A
Alan Cox 已提交
401 402
		err = usb_submit_urb(this_urb, GFP_ATOMIC);
		if (err != 0)
L
Linus Torvalds 已提交
403 404 405 406 407 408 409 410 411 412 413
			dbg("usb_submit_urb(write bulk) failed (%d)", err);
		p_priv->tx_start_time[flip] = jiffies;

		/* Flip for next time if usa26 or usa28 interface
		   (not used on usa49) */
		p_priv->out_flip = (flip + 1) & d_details->outdat_endp_flip;
	}

	return count - left;
}

414
static void	usa26_indat_callback(struct urb *urb)
L
Linus Torvalds 已提交
415 416 417 418 419 420
{
	int			i, err;
	int			endpoint;
	struct usb_serial_port	*port;
	struct tty_struct	*tty;
	unsigned char 		*data = urb->transfer_buffer;
421
	int status = urb->status;
L
Linus Torvalds 已提交
422

A
Alan Cox 已提交
423
	dbg("%s", __func__);
L
Linus Torvalds 已提交
424 425 426

	endpoint = usb_pipeendpoint(urb->pipe);

427
	if (status) {
L
Linus Torvalds 已提交
428
		dbg("%s - nonzero status: %x on endpoint %d.",
429
		    __func__, status, endpoint);
L
Linus Torvalds 已提交
430 431 432
		return;
	}

433
	port =  urb->context;
A
Alan Cox 已提交
434
	tty = tty_port_tty_get(&port->port);
A
Alan Cox 已提交
435
	if (tty && urb->actual_length) {
L
Linus Torvalds 已提交
436 437
		/* 0x80 bit is error flag */
		if ((data[0] & 0x80) == 0) {
A
Alan Cox 已提交
438 439
			/* no errors on individual bytes, only
			   possible overrun err */
L
Linus Torvalds 已提交
440
			if (data[0] & RXERROR_OVERRUN)
A
Alan Cox 已提交
441 442 443 444
				err = TTY_OVERRUN;
			else
				err = 0;
			for (i = 1; i < urb->actual_length ; ++i)
L
Linus Torvalds 已提交
445 446 447
				tty_insert_flip_char(tty, data[i], err);
		} else {
			/* some bytes had errors, every byte has status */
448
			dbg("%s - RX error!!!!", __func__);
L
Linus Torvalds 已提交
449 450 451 452 453 454 455 456 457 458 459 460 461 462
			for (i = 0; i + 1 < urb->actual_length; i += 2) {
				int stat = data[i], flag = 0;
				if (stat & RXERROR_OVERRUN)
					flag |= TTY_OVERRUN;
				if (stat & RXERROR_FRAMING)
					flag |= TTY_FRAME;
				if (stat & RXERROR_PARITY)
					flag |= TTY_PARITY;
				/* XXX should handle break (0x10) */
				tty_insert_flip_char(tty, data[i+1], flag);
			}
		}
		tty_flip_buffer_push(tty);
	}
A
Alan Cox 已提交
463
	tty_kref_put(tty);
A
Alan Cox 已提交
464 465

	/* Resubmit urb so we continue receiving */
L
Linus Torvalds 已提交
466
	urb->dev = port->serial->dev;
A
Alan Cox 已提交
467 468 469 470 471 472
	if (port->port.count) {
		err = usb_submit_urb(urb, GFP_ATOMIC);
		if (err != 0)
			dbg("%s - resubmit read urb failed. (%d)",
					__func__, err);
	}
L
Linus Torvalds 已提交
473 474 475
	return;
}

A
Alan Cox 已提交
476
/* Outdat handling is common for all devices */
477
static void	usa2x_outdat_callback(struct urb *urb)
L
Linus Torvalds 已提交
478 479 480 481
{
	struct usb_serial_port *port;
	struct keyspan_port_private *p_priv;

482
	port =  urb->context;
L
Linus Torvalds 已提交
483
	p_priv = usb_get_serial_port_data(port);
A
Alan Cox 已提交
484
	dbg("%s - urb %d", __func__, urb == p_priv->out_urbs[1]);
L
Linus Torvalds 已提交
485

A
Alan Cox 已提交
486
	if (port->port.count)
487
		usb_serial_port_softint(port);
L
Linus Torvalds 已提交
488 489
}

490
static void	usa26_inack_callback(struct urb *urb)
L
Linus Torvalds 已提交
491
{
A
Alan Cox 已提交
492 493
	dbg("%s", __func__);

L
Linus Torvalds 已提交
494 495
}

496
static void	usa26_outcont_callback(struct urb *urb)
L
Linus Torvalds 已提交
497 498 499 500
{
	struct usb_serial_port *port;
	struct keyspan_port_private *p_priv;

501
	port =  urb->context;
L
Linus Torvalds 已提交
502 503 504
	p_priv = usb_get_serial_port_data(port);

	if (p_priv->resend_cont) {
A
Alan Cox 已提交
505 506 507
		dbg("%s - sending setup", __func__);
		keyspan_usa26_send_setup(port->serial, port,
						p_priv->resend_cont - 1);
L
Linus Torvalds 已提交
508 509 510
	}
}

511
static void	usa26_instat_callback(struct urb *urb)
L
Linus Torvalds 已提交
512 513 514 515 516 517
{
	unsigned char 				*data = urb->transfer_buffer;
	struct keyspan_usa26_portStatusMessage	*msg;
	struct usb_serial			*serial;
	struct usb_serial_port			*port;
	struct keyspan_port_private	 	*p_priv;
A
Alan Cox 已提交
518
	struct tty_struct			*tty;
L
Linus Torvalds 已提交
519
	int old_dcd_state, err;
520
	int status = urb->status;
L
Linus Torvalds 已提交
521

522
	serial =  urb->context;
L
Linus Torvalds 已提交
523

524
	if (status) {
525
		dbg("%s - nonzero status: %x", __func__, status);
L
Linus Torvalds 已提交
526 527 528
		return;
	}
	if (urb->actual_length != 9) {
529
		dbg("%s - %d byte report??", __func__, urb->actual_length);
L
Linus Torvalds 已提交
530 531 532 533 534 535 536
		goto exit;
	}

	msg = (struct keyspan_usa26_portStatusMessage *)data;

#if 0
	dbg("%s - port status: port %d cts %d dcd %d dsr %d ri %d toff %d txoff %d rxen %d cr %d",
537
	    __func__, msg->port, msg->hskia_cts, msg->gpia_dcd, msg->dsr, msg->ri, msg->_txOff,
L
Linus Torvalds 已提交
538 539 540 541 542 543
	    msg->_txXoff, msg->rxEnabled, msg->controlResponse);
#endif

	/* Now do something useful with the data */


A
Alan Cox 已提交
544
	/* Check port number from message and retrieve private data */
L
Linus Torvalds 已提交
545
	if (msg->port >= serial->num_ports) {
A
Alan Cox 已提交
546
		dbg("%s - Unexpected port number %d", __func__, msg->port);
L
Linus Torvalds 已提交
547 548 549 550
		goto exit;
	}
	port = serial->port[msg->port];
	p_priv = usb_get_serial_port_data(port);
A
Alan Cox 已提交
551

L
Linus Torvalds 已提交
552 553 554 555 556 557 558
	/* Update handshaking pin state information */
	old_dcd_state = p_priv->dcd_state;
	p_priv->cts_state = ((msg->hskia_cts) ? 1 : 0);
	p_priv->dsr_state = ((msg->dsr) ? 1 : 0);
	p_priv->dcd_state = ((msg->gpia_dcd) ? 1 : 0);
	p_priv->ri_state = ((msg->ri) ? 1 : 0);

A
Alan Cox 已提交
559 560 561 562 563
	if (old_dcd_state != p_priv->dcd_state) {
		tty = tty_port_tty_get(&port->port);
		if (tty && !C_CLOCAL(tty))
			tty_hangup(tty);
		tty_kref_put(tty);
L
Linus Torvalds 已提交
564
	}
A
Alan Cox 已提交
565

L
Linus Torvalds 已提交
566 567
	/* Resubmit urb so we continue receiving */
	urb->dev = serial->dev;
A
Alan Cox 已提交
568 569
	err = usb_submit_urb(urb, GFP_ATOMIC);
	if (err != 0)
570
		dbg("%s - resubmit read urb failed. (%d)", __func__, err);
L
Linus Torvalds 已提交
571 572 573
exit: ;
}

574
static void	usa26_glocont_callback(struct urb *urb)
L
Linus Torvalds 已提交
575
{
A
Alan Cox 已提交
576
	dbg("%s", __func__);
L
Linus Torvalds 已提交
577 578 579
}


580
static void usa28_indat_callback(struct urb *urb)
L
Linus Torvalds 已提交
581
{
582
	int                     err;
L
Linus Torvalds 已提交
583 584 585 586
	struct usb_serial_port  *port;
	struct tty_struct       *tty;
	unsigned char           *data;
	struct keyspan_port_private             *p_priv;
587
	int status = urb->status;
L
Linus Torvalds 已提交
588

A
Alan Cox 已提交
589
	dbg("%s", __func__);
L
Linus Torvalds 已提交
590

591
	port =  urb->context;
L
Linus Torvalds 已提交
592 593 594 595 596 597 598
	p_priv = usb_get_serial_port_data(port);
	data = urb->transfer_buffer;

	if (urb != p_priv->in_urbs[p_priv->in_flip])
		return;

	do {
599
		if (status) {
L
Linus Torvalds 已提交
600
			dbg("%s - nonzero status: %x on endpoint %d.",
601
			    __func__, status, usb_pipeendpoint(urb->pipe));
L
Linus Torvalds 已提交
602 603 604
			return;
		}

605
		port =  urb->context;
L
Linus Torvalds 已提交
606 607 608
		p_priv = usb_get_serial_port_data(port);
		data = urb->transfer_buffer;

A
Alan Cox 已提交
609 610
		tty =tty_port_tty_get(&port->port);
		if (tty && urb->actual_length) {
611
			tty_insert_flip_string(tty, data, urb->actual_length);
L
Linus Torvalds 已提交
612 613
			tty_flip_buffer_push(tty);
		}
A
Alan Cox 已提交
614
		tty_kref_put(tty);
L
Linus Torvalds 已提交
615 616 617

		/* Resubmit urb so we continue receiving */
		urb->dev = port->serial->dev;
A
Alan Cox 已提交
618 619 620 621 622 623
		if (port->port.count) {
			err = usb_submit_urb(urb, GFP_ATOMIC);
			if (err != 0)
				dbg("%s - resubmit read urb failed. (%d)",
								__func__, err);
		}
L
Linus Torvalds 已提交
624 625 626 627 628 629
		p_priv->in_flip ^= 1;

		urb = p_priv->in_urbs[p_priv->in_flip];
	} while (urb->status != -EINPROGRESS);
}

630
static void	usa28_inack_callback(struct urb *urb)
L
Linus Torvalds 已提交
631
{
A
Alan Cox 已提交
632
	dbg("%s", __func__);
L
Linus Torvalds 已提交
633 634
}

635
static void	usa28_outcont_callback(struct urb *urb)
L
Linus Torvalds 已提交
636 637 638 639
{
	struct usb_serial_port *port;
	struct keyspan_port_private *p_priv;

640
	port =  urb->context;
L
Linus Torvalds 已提交
641 642 643
	p_priv = usb_get_serial_port_data(port);

	if (p_priv->resend_cont) {
A
Alan Cox 已提交
644 645 646
		dbg("%s - sending setup", __func__);
		keyspan_usa28_send_setup(port->serial, port,
						p_priv->resend_cont - 1);
L
Linus Torvalds 已提交
647 648 649
	}
}

650
static void	usa28_instat_callback(struct urb *urb)
L
Linus Torvalds 已提交
651 652 653 654 655 656 657
{
	int					err;
	unsigned char 				*data = urb->transfer_buffer;
	struct keyspan_usa28_portStatusMessage	*msg;
	struct usb_serial			*serial;
	struct usb_serial_port			*port;
	struct keyspan_port_private	 	*p_priv;
A
Alan Cox 已提交
658
	struct tty_struct			*tty;
L
Linus Torvalds 已提交
659
	int old_dcd_state;
660
	int status = urb->status;
L
Linus Torvalds 已提交
661

662
	serial =  urb->context;
L
Linus Torvalds 已提交
663

664
	if (status) {
665
		dbg("%s - nonzero status: %x", __func__, status);
L
Linus Torvalds 已提交
666 667 668 669
		return;
	}

	if (urb->actual_length != sizeof(struct keyspan_usa28_portStatusMessage)) {
670
		dbg("%s - bad length %d", __func__, urb->actual_length);
L
Linus Torvalds 已提交
671 672 673
		goto exit;
	}

674
	/*dbg("%s %x %x %x %x %x %x %x %x %x %x %x %x", __func__
L
Linus Torvalds 已提交
675 676 677
	    data[0], data[1], data[2], data[3], data[4], data[5],
	    data[6], data[7], data[8], data[9], data[10], data[11]);*/

A
Alan Cox 已提交
678 679
	/* Now do something useful with the data */
	msg = (struct keyspan_usa28_portStatusMessage *)data;
L
Linus Torvalds 已提交
680

A
Alan Cox 已提交
681
	/* Check port number from message and retrieve private data */
L
Linus Torvalds 已提交
682
	if (msg->port >= serial->num_ports) {
A
Alan Cox 已提交
683
		dbg("%s - Unexpected port number %d", __func__, msg->port);
L
Linus Torvalds 已提交
684 685 686 687
		goto exit;
	}
	port = serial->port[msg->port];
	p_priv = usb_get_serial_port_data(port);
A
Alan Cox 已提交
688

L
Linus Torvalds 已提交
689 690 691 692 693 694 695
	/* Update handshaking pin state information */
	old_dcd_state = p_priv->dcd_state;
	p_priv->cts_state = ((msg->cts) ? 1 : 0);
	p_priv->dsr_state = ((msg->dsr) ? 1 : 0);
	p_priv->dcd_state = ((msg->dcd) ? 1 : 0);
	p_priv->ri_state = ((msg->ri) ? 1 : 0);

A
Alan Cox 已提交
696 697 698 699 700
	if( old_dcd_state != p_priv->dcd_state && old_dcd_state) {
		tty = tty_port_tty_get(&port->port);
		if (tty && !C_CLOCAL(tty)) 
			tty_hangup(tty);
		tty_kref_put(tty);
L
Linus Torvalds 已提交
701 702 703 704
	}

		/* Resubmit urb so we continue receiving */
	urb->dev = serial->dev;
A
Alan Cox 已提交
705 706
	err = usb_submit_urb(urb, GFP_ATOMIC);
	if (err != 0)
707
		dbg("%s - resubmit read urb failed. (%d)", __func__, err);
L
Linus Torvalds 已提交
708 709 710
exit: ;
}

711
static void	usa28_glocont_callback(struct urb *urb)
L
Linus Torvalds 已提交
712
{
A
Alan Cox 已提交
713
	dbg("%s", __func__);
L
Linus Torvalds 已提交
714 715 716
}


717
static void	usa49_glocont_callback(struct urb *urb)
L
Linus Torvalds 已提交
718 719 720 721 722 723
{
	struct usb_serial *serial;
	struct usb_serial_port *port;
	struct keyspan_port_private *p_priv;
	int i;

A
Alan Cox 已提交
724
	dbg("%s", __func__);
L
Linus Torvalds 已提交
725

726
	serial =  urb->context;
L
Linus Torvalds 已提交
727 728 729 730 731
	for (i = 0; i < serial->num_ports; ++i) {
		port = serial->port[i];
		p_priv = usb_get_serial_port_data(port);

		if (p_priv->resend_cont) {
A
Alan Cox 已提交
732 733 734
			dbg("%s - sending setup", __func__);
			keyspan_usa49_send_setup(serial, port,
						p_priv->resend_cont - 1);
L
Linus Torvalds 已提交
735 736 737 738 739 740 741
			break;
		}
	}
}

	/* This is actually called glostat in the Keyspan
	   doco */
742
static void	usa49_instat_callback(struct urb *urb)
L
Linus Torvalds 已提交
743 744 745 746 747 748 749 750
{
	int					err;
	unsigned char 				*data = urb->transfer_buffer;
	struct keyspan_usa49_portStatusMessage	*msg;
	struct usb_serial			*serial;
	struct usb_serial_port			*port;
	struct keyspan_port_private	 	*p_priv;
	int old_dcd_state;
751
	int status = urb->status;
L
Linus Torvalds 已提交
752

A
Alan Cox 已提交
753
	dbg("%s", __func__);
L
Linus Torvalds 已提交
754

755
	serial =  urb->context;
L
Linus Torvalds 已提交
756

757
	if (status) {
758
		dbg("%s - nonzero status: %x", __func__, status);
L
Linus Torvalds 已提交
759 760 761
		return;
	}

A
Alan Cox 已提交
762 763
	if (urb->actual_length !=
			sizeof(struct keyspan_usa49_portStatusMessage)) {
764
		dbg("%s - bad length %d", __func__, urb->actual_length);
L
Linus Torvalds 已提交
765 766 767
		goto exit;
	}

768
	/*dbg(" %x %x %x %x %x %x %x %x %x %x %x", __func__,
L
Linus Torvalds 已提交
769 770
	    data[0], data[1], data[2], data[3], data[4], data[5],
	    data[6], data[7], data[8], data[9], data[10]);*/
A
Alan Cox 已提交
771 772

	/* Now do something useful with the data */
L
Linus Torvalds 已提交
773 774
	msg = (struct keyspan_usa49_portStatusMessage *)data;

A
Alan Cox 已提交
775
	/* Check port number from message and retrieve private data */
L
Linus Torvalds 已提交
776
	if (msg->portNumber >= serial->num_ports) {
A
Alan Cox 已提交
777 778
		dbg("%s - Unexpected port number %d",
					__func__, msg->portNumber);
L
Linus Torvalds 已提交
779 780 781 782
		goto exit;
	}
	port = serial->port[msg->portNumber];
	p_priv = usb_get_serial_port_data(port);
A
Alan Cox 已提交
783

L
Linus Torvalds 已提交
784 785 786 787 788 789 790
	/* Update handshaking pin state information */
	old_dcd_state = p_priv->dcd_state;
	p_priv->cts_state = ((msg->cts) ? 1 : 0);
	p_priv->dsr_state = ((msg->dsr) ? 1 : 0);
	p_priv->dcd_state = ((msg->dcd) ? 1 : 0);
	p_priv->ri_state = ((msg->ri) ? 1 : 0);

A
Alan Cox 已提交
791 792 793 794 795
	if (old_dcd_state != p_priv->dcd_state && old_dcd_state) {
		struct tty_struct *tty = tty_port_tty_get(&port->port);
		if (tty && !C_CLOCAL(tty))
			tty_hangup(tty);
		tty_kref_put(tty);
L
Linus Torvalds 已提交
796 797
	}

A
Alan Cox 已提交
798
	/* Resubmit urb so we continue receiving */
L
Linus Torvalds 已提交
799 800
	urb->dev = serial->dev;

A
Alan Cox 已提交
801 802
	err = usb_submit_urb(urb, GFP_ATOMIC);
	if (err != 0)
803
		dbg("%s - resubmit read urb failed. (%d)", __func__, err);
L
Linus Torvalds 已提交
804 805 806
exit:	;
}

807
static void	usa49_inack_callback(struct urb *urb)
L
Linus Torvalds 已提交
808
{
A
Alan Cox 已提交
809
	dbg("%s", __func__);
L
Linus Torvalds 已提交
810 811
}

812
static void	usa49_indat_callback(struct urb *urb)
L
Linus Torvalds 已提交
813 814 815 816 817 818
{
	int			i, err;
	int			endpoint;
	struct usb_serial_port	*port;
	struct tty_struct	*tty;
	unsigned char 		*data = urb->transfer_buffer;
819
	int status = urb->status;
L
Linus Torvalds 已提交
820

A
Alan Cox 已提交
821
	dbg("%s", __func__);
L
Linus Torvalds 已提交
822 823 824

	endpoint = usb_pipeendpoint(urb->pipe);

825
	if (status) {
826
		dbg("%s - nonzero status: %x on endpoint %d.", __func__,
827
		    status, endpoint);
L
Linus Torvalds 已提交
828 829 830
		return;
	}

831
	port =  urb->context;
A
Alan Cox 已提交
832
	tty = tty_port_tty_get(&port->port);
A
Alan Cox 已提交
833
	if (tty && urb->actual_length) {
L
Linus Torvalds 已提交
834 835 836
		/* 0x80 bit is error flag */
		if ((data[0] & 0x80) == 0) {
			/* no error on any byte */
837 838
			tty_insert_flip_string(tty, data + 1,
						urb->actual_length - 1);
L
Linus Torvalds 已提交
839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854
		} else {
			/* some bytes had errors, every byte has status */
			for (i = 0; i + 1 < urb->actual_length; i += 2) {
				int stat = data[i], flag = 0;
				if (stat & RXERROR_OVERRUN)
					flag |= TTY_OVERRUN;
				if (stat & RXERROR_FRAMING)
					flag |= TTY_FRAME;
				if (stat & RXERROR_PARITY)
					flag |= TTY_PARITY;
				/* XXX should handle break (0x10) */
				tty_insert_flip_char(tty, data[i+1], flag);
			}
		}
		tty_flip_buffer_push(tty);
	}
A
Alan Cox 已提交
855
	tty_kref_put(tty);
A
Alan Cox 已提交
856 857

	/* Resubmit urb so we continue receiving */
L
Linus Torvalds 已提交
858
	urb->dev = port->serial->dev;
A
Alan Cox 已提交
859 860 861 862 863 864
	if (port->port.count) {
		err = usb_submit_urb(urb, GFP_ATOMIC);
		if (err != 0)
			dbg("%s - resubmit read urb failed. (%d)",
							__func__, err);
	}
L
Linus Torvalds 已提交
865 866
}

867 868 869 870 871 872 873
static void usa49wg_indat_callback(struct urb *urb)
{
	int			i, len, x, err;
	struct usb_serial	*serial;
	struct usb_serial_port	*port;
	struct tty_struct	*tty;
	unsigned char 		*data = urb->transfer_buffer;
874
	int status = urb->status;
875

A
Alan Cox 已提交
876
	dbg("%s", __func__);
877 878 879

	serial = urb->context;

880
	if (status) {
881
		dbg("%s - nonzero status: %x", __func__, status);
882 883 884 885 886 887 888 889 890 891 892 893
		return;
	}

	/* inbound data is in the form P#, len, status, data */
	i = 0;
	len = 0;

	if (urb->actual_length) {
		while (i < urb->actual_length) {

			/* Check port number from message*/
			if (data[i] >= serial->num_ports) {
A
Alan Cox 已提交
894
				dbg("%s - Unexpected port number %d",
895
					__func__, data[i]);
896 897 898
				return;
			}
			port = serial->port[data[i++]];
A
Alan Cox 已提交
899
			tty = tty_port_tty_get(&port->port);
900 901 902 903 904 905 906
			len = data[i++];

			/* 0x80 bit is error flag */
			if ((data[i] & 0x80) == 0) {
				/* no error on any byte */
				i++;
				for (x = 1; x < len ; ++x)
A
Alan Cox 已提交
907
					if (port->port.count)
908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924
						tty_insert_flip_char(tty,
								data[i++], 0);
					else
						i++;
			} else {
				/*
				 * some bytes had errors, every byte has status
				 */
				for (x = 0; x + 1 < len; x += 2) {
					int stat = data[i], flag = 0;
					if (stat & RXERROR_OVERRUN)
						flag |= TTY_OVERRUN;
					if (stat & RXERROR_FRAMING)
						flag |= TTY_FRAME;
					if (stat & RXERROR_PARITY)
						flag |= TTY_PARITY;
					/* XXX should handle break (0x10) */
A
Alan Cox 已提交
925
					if (port->port.count)
926 927 928 929 930
						tty_insert_flip_char(tty,
							data[i+1], flag);
					i += 2;
				}
			}
A
Alan Cox 已提交
931
			if (port->port.count)
932
				tty_flip_buffer_push(tty);
A
Alan Cox 已提交
933
			tty_kref_put(tty);
934 935 936 937 938 939 940 941
		}
	}

	/* Resubmit urb so we continue receiving */
	urb->dev = serial->dev;

	err = usb_submit_urb(urb, GFP_ATOMIC);
	if (err != 0)
942
		dbg("%s - resubmit read urb failed. (%d)", __func__, err);
943 944
}

L
Linus Torvalds 已提交
945
/* not used, usa-49 doesn't have per-port control endpoints */
946
static void usa49_outcont_callback(struct urb *urb)
L
Linus Torvalds 已提交
947
{
A
Alan Cox 已提交
948
	dbg("%s", __func__);
L
Linus Torvalds 已提交
949 950
}

951
static void usa90_indat_callback(struct urb *urb)
L
Linus Torvalds 已提交
952 953 954 955 956 957 958
{
	int			i, err;
	int			endpoint;
	struct usb_serial_port	*port;
	struct keyspan_port_private	 	*p_priv;
	struct tty_struct	*tty;
	unsigned char 		*data = urb->transfer_buffer;
959
	int status = urb->status;
L
Linus Torvalds 已提交
960

A
Alan Cox 已提交
961
	dbg("%s", __func__);
L
Linus Torvalds 已提交
962 963 964

	endpoint = usb_pipeendpoint(urb->pipe);

965
	if (status) {
L
Linus Torvalds 已提交
966
		dbg("%s - nonzero status: %x on endpoint %d.",
967
		    __func__, status, endpoint);
L
Linus Torvalds 已提交
968 969 970
		return;
	}

971
	port =  urb->context;
L
Linus Torvalds 已提交
972 973 974
	p_priv = usb_get_serial_port_data(port);

	if (urb->actual_length) {
A
Alan Cox 已提交
975
		tty = tty_port_tty_get(&port->port);
L
Linus Torvalds 已提交
976
		/* if current mode is DMA, looks like usa28 format
A
Alan Cox 已提交
977
		   otherwise looks like usa26 data format */
L
Linus Torvalds 已提交
978

979 980 981
		if (p_priv->baud > 57600)
			tty_insert_flip_string(tty, data, urb->actual_length);
		else {
L
Linus Torvalds 已提交
982 983
			/* 0x80 bit is error flag */
			if ((data[0] & 0x80) == 0) {
A
Alan Cox 已提交
984 985
				/* no errors on individual bytes, only
				   possible overrun err*/
L
Linus Torvalds 已提交
986
				if (data[0] & RXERROR_OVERRUN)
A
Alan Cox 已提交
987 988 989 990 991 992 993
					err = TTY_OVERRUN;
				else
					err = 0;
				for (i = 1; i < urb->actual_length ; ++i)
					tty_insert_flip_char(tty, data[i],
									err);
			}  else {
L
Linus Torvalds 已提交
994
			/* some bytes had errors, every byte has status */
995
				dbg("%s - RX error!!!!", __func__);
L
Linus Torvalds 已提交
996 997 998 999 1000 1001 1002 1003 1004
				for (i = 0; i + 1 < urb->actual_length; i += 2) {
					int stat = data[i], flag = 0;
					if (stat & RXERROR_OVERRUN)
						flag |= TTY_OVERRUN;
					if (stat & RXERROR_FRAMING)
						flag |= TTY_FRAME;
					if (stat & RXERROR_PARITY)
						flag |= TTY_PARITY;
					/* XXX should handle break (0x10) */
A
Alan Cox 已提交
1005 1006
					tty_insert_flip_char(tty, data[i+1],
									flag);
L
Linus Torvalds 已提交
1007 1008 1009 1010
				}
			}
		}
		tty_flip_buffer_push(tty);
A
Alan Cox 已提交
1011
		tty_kref_put(tty);
L
Linus Torvalds 已提交
1012
	}
A
Alan Cox 已提交
1013

L
Linus Torvalds 已提交
1014 1015
	/* Resubmit urb so we continue receiving */
	urb->dev = port->serial->dev;
A
Alan Cox 已提交
1016 1017 1018 1019 1020 1021
	if (port->port.count) {
		err = usb_submit_urb(urb, GFP_ATOMIC);
		if (err != 0)
			dbg("%s - resubmit read urb failed. (%d)",
							__func__, err);
	}
L
Linus Torvalds 已提交
1022 1023 1024 1025
	return;
}


1026
static void	usa90_instat_callback(struct urb *urb)
L
Linus Torvalds 已提交
1027 1028 1029 1030 1031 1032
{
	unsigned char 				*data = urb->transfer_buffer;
	struct keyspan_usa90_portStatusMessage	*msg;
	struct usb_serial			*serial;
	struct usb_serial_port			*port;
	struct keyspan_port_private	 	*p_priv;
A
Alan Cox 已提交
1033
	struct tty_struct			*tty;
L
Linus Torvalds 已提交
1034
	int old_dcd_state, err;
1035
	int status = urb->status;
L
Linus Torvalds 已提交
1036

1037
	serial =  urb->context;
L
Linus Torvalds 已提交
1038

1039
	if (status) {
1040
		dbg("%s - nonzero status: %x", __func__, status);
L
Linus Torvalds 已提交
1041 1042 1043
		return;
	}
	if (urb->actual_length < 14) {
1044
		dbg("%s - %d byte report??", __func__, urb->actual_length);
L
Linus Torvalds 已提交
1045 1046 1047 1048 1049 1050 1051 1052 1053
		goto exit;
	}

	msg = (struct keyspan_usa90_portStatusMessage *)data;

	/* Now do something useful with the data */

	port = serial->port[0];
	p_priv = usb_get_serial_port_data(port);
A
Alan Cox 已提交
1054

L
Linus Torvalds 已提交
1055 1056 1057 1058 1059 1060 1061
	/* Update handshaking pin state information */
	old_dcd_state = p_priv->dcd_state;
	p_priv->cts_state = ((msg->cts) ? 1 : 0);
	p_priv->dsr_state = ((msg->dsr) ? 1 : 0);
	p_priv->dcd_state = ((msg->dcd) ? 1 : 0);
	p_priv->ri_state = ((msg->ri) ? 1 : 0);

A
Alan Cox 已提交
1062 1063 1064 1065 1066
	if (old_dcd_state != p_priv->dcd_state && old_dcd_state) {
		tty = tty_port_tty_get(&port->port);
		if (tty && !C_CLOCAL(tty))
			tty_hangup(tty);
		tty_kref_put(tty);
L
Linus Torvalds 已提交
1067
	}
A
Alan Cox 已提交
1068

L
Linus Torvalds 已提交
1069 1070
	/* Resubmit urb so we continue receiving */
	urb->dev = serial->dev;
A
Alan Cox 已提交
1071 1072
	err = usb_submit_urb(urb, GFP_ATOMIC);
	if (err != 0)
1073
		dbg("%s - resubmit read urb failed. (%d)", __func__, err);
L
Linus Torvalds 已提交
1074 1075 1076 1077
exit:
	;
}

1078
static void	usa90_outcont_callback(struct urb *urb)
L
Linus Torvalds 已提交
1079 1080 1081 1082
{
	struct usb_serial_port *port;
	struct keyspan_port_private *p_priv;

1083
	port =  urb->context;
L
Linus Torvalds 已提交
1084 1085 1086
	p_priv = usb_get_serial_port_data(port);

	if (p_priv->resend_cont) {
A
Alan Cox 已提交
1087 1088 1089
		dbg("%s - sending setup", __func__);
		keyspan_usa90_send_setup(port->serial, port,
						p_priv->resend_cont - 1);
L
Linus Torvalds 已提交
1090 1091 1092
	}
}

1093 1094 1095 1096 1097 1098 1099 1100 1101 1102
/* Status messages from the 28xg */
static void	usa67_instat_callback(struct urb *urb)
{
	int					err;
	unsigned char 				*data = urb->transfer_buffer;
	struct keyspan_usa67_portStatusMessage	*msg;
	struct usb_serial			*serial;
	struct usb_serial_port			*port;
	struct keyspan_port_private	 	*p_priv;
	int old_dcd_state;
1103
	int status = urb->status;
1104

A
Alan Cox 已提交
1105
	dbg("%s", __func__);
1106 1107 1108

	serial = urb->context;

1109
	if (status) {
1110
		dbg("%s - nonzero status: %x", __func__, status);
1111 1112 1113
		return;
	}

A
Alan Cox 已提交
1114 1115
	if (urb->actual_length !=
			sizeof(struct keyspan_usa67_portStatusMessage)) {
1116
		dbg("%s - bad length %d", __func__, urb->actual_length);
1117 1118 1119 1120 1121 1122 1123 1124 1125
		return;
	}


	/* Now do something useful with the data */
	msg = (struct keyspan_usa67_portStatusMessage *)data;

	/* Check port number from message and retrieve private data */
	if (msg->port >= serial->num_ports) {
A
Alan Cox 已提交
1126
		dbg("%s - Unexpected port number %d", __func__, msg->port);
1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137
		return;
	}

	port = serial->port[msg->port];
	p_priv = usb_get_serial_port_data(port);

	/* Update handshaking pin state information */
	old_dcd_state = p_priv->dcd_state;
	p_priv->cts_state = ((msg->hskia_cts) ? 1 : 0);
	p_priv->dcd_state = ((msg->gpia_dcd) ? 1 : 0);

A
Alan Cox 已提交
1138 1139 1140 1141 1142
	if (old_dcd_state != p_priv->dcd_state && old_dcd_state) {
		struct tty_struct *tty = tty_port_tty_get(&port->port);
		if (tty && !C_CLOCAL(tty))
			tty_hangup(tty);
		tty_kref_put(tty);
1143 1144 1145 1146 1147 1148
	}

	/* Resubmit urb so we continue receiving */
	urb->dev = serial->dev;
	err = usb_submit_urb(urb, GFP_ATOMIC);
	if (err != 0)
1149
		dbg("%s - resubmit read urb failed. (%d)", __func__, err);
1150 1151 1152 1153 1154 1155 1156 1157 1158
}

static void usa67_glocont_callback(struct urb *urb)
{
	struct usb_serial *serial;
	struct usb_serial_port *port;
	struct keyspan_port_private *p_priv;
	int i;

A
Alan Cox 已提交
1159
	dbg("%s", __func__);
1160 1161 1162 1163 1164 1165 1166

	serial = urb->context;
	for (i = 0; i < serial->num_ports; ++i) {
		port = serial->port[i];
		p_priv = usb_get_serial_port_data(port);

		if (p_priv->resend_cont) {
A
Alan Cox 已提交
1167
			dbg("%s - sending setup", __func__);
1168 1169 1170 1171 1172 1173 1174
			keyspan_usa67_send_setup(serial, port,
						p_priv->resend_cont - 1);
			break;
		}
	}
}

A
Alan Cox 已提交
1175
static int keyspan_write_room(struct tty_struct *tty)
L
Linus Torvalds 已提交
1176
{
A
Alan Cox 已提交
1177
	struct usb_serial_port *port = tty->driver_data;
L
Linus Torvalds 已提交
1178 1179 1180 1181 1182 1183
	struct keyspan_port_private	*p_priv;
	const struct keyspan_device_details	*d_details;
	int				flip;
	int				data_len;
	struct urb			*this_urb;

1184
	dbg("%s", __func__);
L
Linus Torvalds 已提交
1185 1186 1187
	p_priv = usb_get_serial_port_data(port);
	d_details = p_priv->device_details;

1188
	/* FIXME: locking */
L
Linus Torvalds 已提交
1189
	if (d_details->msg_format == msg_usa90)
A
Alan Cox 已提交
1190
		data_len = 64;
L
Linus Torvalds 已提交
1191 1192 1193 1194 1195 1196
	else
		data_len = 63;

	flip = p_priv->out_flip;

	/* Check both endpoints to see if any are available. */
A
Alan Cox 已提交
1197 1198
	this_urb = p_priv->out_urbs[flip];
	if (this_urb != NULL) {
L
Linus Torvalds 已提交
1199
		if (this_urb->status != -EINPROGRESS)
A
Alan Cox 已提交
1200 1201 1202 1203
			return data_len;
		flip = (flip + 1) & d_details->outdat_endp_flip;
		this_urb = p_priv->out_urbs[flip];
		if (this_urb != NULL) {
L
Linus Torvalds 已提交
1204
			if (this_urb->status != -EINPROGRESS)
A
Alan Cox 已提交
1205 1206
				return data_len;
		}
L
Linus Torvalds 已提交
1207
	}
1208
	return 0;
L
Linus Torvalds 已提交
1209 1210 1211
}


A
Alan Cox 已提交
1212 1213
static int keyspan_open(struct tty_struct *tty,
			struct usb_serial_port *port, struct file *filp)
L
Linus Torvalds 已提交
1214
{
1215 1216 1217
	struct keyspan_port_private 	*p_priv;
	struct keyspan_serial_private 	*s_priv;
	struct usb_serial 		*serial = port->serial;
L
Linus Torvalds 已提交
1218 1219
	const struct keyspan_device_details	*d_details;
	int				i, err;
1220
	int				baud_rate, device_port;
L
Linus Torvalds 已提交
1221
	struct urb			*urb;
A
Alan Cox 已提交
1222
	unsigned int			cflag = 0;
L
Linus Torvalds 已提交
1223 1224 1225 1226

	s_priv = usb_get_serial_data(serial);
	p_priv = usb_get_serial_port_data(port);
	d_details = p_priv->device_details;
B
Borislav Petkov 已提交
1227

1228
	dbg("%s - port%d.", __func__, port->number);
L
Linus Torvalds 已提交
1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243

	/* Set some sane defaults */
	p_priv->rts_state = 1;
	p_priv->dtr_state = 1;
	p_priv->baud = 9600;

	/* force baud and lcr to be set on open */
	p_priv->old_baud = 0;
	p_priv->old_cflag = 0;

	p_priv->out_flip = 0;
	p_priv->in_flip = 0;

	/* Reset low level data toggle and start reading from endpoints */
	for (i = 0; i < 2; i++) {
A
Alan Cox 已提交
1244 1245
		urb = p_priv->in_urbs[i];
		if (urb == NULL)
L
Linus Torvalds 已提交
1246 1247 1248
			continue;
		urb->dev = serial->dev;

A
Alan Cox 已提交
1249 1250
		/* make sure endpoint data toggle is synchronized
		   with the device */
L
Linus Torvalds 已提交
1251
		usb_clear_halt(urb->dev, urb->pipe);
A
Alan Cox 已提交
1252 1253 1254 1255
		err = usb_submit_urb(urb, GFP_KERNEL);
		if (err != 0)
			dbg("%s - submit urb %d failed (%d)",
							__func__, i, err);
L
Linus Torvalds 已提交
1256 1257 1258 1259
	}

	/* Reset low level data toggle on out endpoints */
	for (i = 0; i < 2; i++) {
A
Alan Cox 已提交
1260 1261
		urb = p_priv->out_urbs[i];
		if (urb == NULL)
L
Linus Torvalds 已提交
1262 1263
			continue;
		urb->dev = serial->dev;
A
Alan Cox 已提交
1264 1265
		/* usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe),
						usb_pipeout(urb->pipe), 0); */
L
Linus Torvalds 已提交
1266 1267
	}

1268 1269 1270 1271
	/* get the terminal config for the setup message now so we don't
	 * need to send 2 of them */

	device_port = port->number - port->serial->minor;
A
Alan Cox 已提交
1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282
	if (tty) {
		cflag = tty->termios->c_cflag;
		/* Baud rate calculation takes baud rate as an integer
		   so other rates can be generated if desired. */
		baud_rate = tty_get_baud_rate(tty);
		/* If no match or invalid, leave as default */
		if (baud_rate >= 0
		    && d_details->calculate_baud_rate(baud_rate, d_details->baudclk,
					NULL, NULL, NULL, device_port) == KEYSPAN_BAUD_RATE_OK) {
			p_priv->baud = baud_rate;
		}
1283 1284 1285 1286 1287 1288
	}
	/* set CTS/RTS handshake etc. */
	p_priv->cflag = cflag;
	p_priv->flow_control = (cflag & CRTSCTS)? flow_cts: flow_none;

	keyspan_send_setup(port, 1);
A
Alan Cox 已提交
1289 1290
	/* mdelay(100); */
	/* keyspan_set_termios(port, NULL); */
1291

1292
	return 0;
L
Linus Torvalds 已提交
1293 1294 1295 1296
}

static inline void stop_urb(struct urb *urb)
{
1297
	if (urb && urb->status == -EINPROGRESS)
L
Linus Torvalds 已提交
1298 1299 1300
		usb_kill_urb(urb);
}

1301 1302 1303 1304 1305 1306 1307 1308 1309 1310
static void keyspan_dtr_rts(struct usb_serial_port *port, int on)
{
	struct keyspan_port_private *p_priv = usb_get_serial_port_data(port);

	p_priv->rts_state = on;
	p_priv->dtr_state = on;
	keyspan_send_setup(port, 0);
}

static void keyspan_close(struct usb_serial_port *port)
L
Linus Torvalds 已提交
1311 1312 1313 1314 1315 1316
{
	int			i;
	struct usb_serial	*serial = port->serial;
	struct keyspan_serial_private 	*s_priv;
	struct keyspan_port_private 	*p_priv;

1317
	dbg("%s", __func__);
L
Linus Torvalds 已提交
1318 1319
	s_priv = usb_get_serial_data(serial);
	p_priv = usb_get_serial_port_data(port);
A
Alan Cox 已提交
1320

L
Linus Torvalds 已提交
1321 1322
	p_priv->rts_state = 0;
	p_priv->dtr_state = 0;
A
Alan Cox 已提交
1323

L
Linus Torvalds 已提交
1324 1325 1326 1327
	if (serial->dev) {
		keyspan_send_setup(port, 2);
		/* pilot-xfer seems to work best with this delay */
		mdelay(100);
A
Alan Cox 已提交
1328
		/* keyspan_set_termios(port, NULL); */
L
Linus Torvalds 已提交
1329 1330 1331
	}

	/*while (p_priv->outcont_urb->status == -EINPROGRESS) {
1332
		dbg("%s - urb in progress", __func__);
L
Linus Torvalds 已提交
1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348
	}*/

	p_priv->out_flip = 0;
	p_priv->in_flip = 0;

	if (serial->dev) {
		/* Stop reading/writing urbs */
		stop_urb(p_priv->inack_urb);
		/* stop_urb(p_priv->outcont_urb); */
		for (i = 0; i < 2; i++) {
			stop_urb(p_priv->in_urbs[i]);
			stop_urb(p_priv->out_urbs[i]);
		}
	}
}

A
Alan Cox 已提交
1349 1350
/* download the firmware to a pre-renumeration device */
static int keyspan_fake_startup(struct usb_serial *serial)
L
Linus Torvalds 已提交
1351 1352
{
	int 				response;
1353
	const struct ihex_binrec 	*record;
L
Linus Torvalds 已提交
1354
	char				*fw_name;
1355
	const struct firmware		*fw;
L
Linus Torvalds 已提交
1356 1357 1358 1359

	dbg("Keyspan startup version %04x product %04x",
	    le16_to_cpu(serial->dev->descriptor.bcdDevice),
	    le16_to_cpu(serial->dev->descriptor.idProduct));
A
Alan Cox 已提交
1360 1361 1362

	if ((le16_to_cpu(serial->dev->descriptor.bcdDevice) & 0x8000)
								!= 0x8000) {
L
Linus Torvalds 已提交
1363
		dbg("Firmware already loaded.  Quitting.");
A
Alan Cox 已提交
1364
		return 1;
L
Linus Torvalds 已提交
1365 1366 1367 1368 1369
	}

		/* Select firmware image on the basis of idProduct */
	switch (le16_to_cpu(serial->dev->descriptor.idProduct)) {
	case keyspan_usa28_pre_product_id:
1370
		fw_name = "keyspan/usa28.fw";
L
Linus Torvalds 已提交
1371 1372 1373
		break;

	case keyspan_usa28x_pre_product_id:
1374
		fw_name = "keyspan/usa28x.fw";
L
Linus Torvalds 已提交
1375 1376 1377
		break;

	case keyspan_usa28xa_pre_product_id:
1378
		fw_name = "keyspan/usa28xa.fw";
L
Linus Torvalds 已提交
1379 1380 1381
		break;

	case keyspan_usa28xb_pre_product_id:
1382
		fw_name = "keyspan/usa28xb.fw";
L
Linus Torvalds 已提交
1383 1384 1385
		break;

	case keyspan_usa19_pre_product_id:
1386
		fw_name = "keyspan/usa19.fw";
L
Linus Torvalds 已提交
1387
		break;
A
Alan Cox 已提交
1388

L
Linus Torvalds 已提交
1389
	case keyspan_usa19qi_pre_product_id:
1390
		fw_name = "keyspan/usa19qi.fw";
L
Linus Torvalds 已提交
1391
		break;
A
Alan Cox 已提交
1392

L
Linus Torvalds 已提交
1393
	case keyspan_mpr_pre_product_id:
1394
		fw_name = "keyspan/mpr.fw";
L
Linus Torvalds 已提交
1395 1396 1397
		break;

	case keyspan_usa19qw_pre_product_id:
1398
		fw_name = "keyspan/usa19qw.fw";
L
Linus Torvalds 已提交
1399
		break;
A
Alan Cox 已提交
1400

L
Linus Torvalds 已提交
1401
	case keyspan_usa18x_pre_product_id:
1402
		fw_name = "keyspan/usa18x.fw";
L
Linus Torvalds 已提交
1403
		break;
A
Alan Cox 已提交
1404

L
Linus Torvalds 已提交
1405
	case keyspan_usa19w_pre_product_id:
1406
		fw_name = "keyspan/usa19w.fw";
L
Linus Torvalds 已提交
1407
		break;
A
Alan Cox 已提交
1408

L
Linus Torvalds 已提交
1409
	case keyspan_usa49w_pre_product_id:
1410
		fw_name = "keyspan/usa49w.fw";
L
Linus Torvalds 已提交
1411 1412 1413
		break;

	case keyspan_usa49wlc_pre_product_id:
1414
		fw_name = "keyspan/usa49wlc.fw";
L
Linus Torvalds 已提交
1415 1416 1417
		break;

	default:
1418 1419 1420
		dev_err(&serial->dev->dev, "Unknown product ID (%04x)\n",
			le16_to_cpu(serial->dev->descriptor.idProduct));
		return 1;
L
Linus Torvalds 已提交
1421 1422
	}

1423
	if (request_ihex_firmware(&fw, fw_name, &serial->dev->dev)) {
L
Linus Torvalds 已提交
1424 1425 1426 1427 1428 1429 1430 1431 1432
		dev_err(&serial->dev->dev, "Required keyspan firmware image (%s) unavailable.\n", fw_name);
		return(1);
	}

	dbg("Uploading Keyspan %s firmware.", fw_name);

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

1433 1434 1435 1436
	record = (const struct ihex_binrec *)fw->data;

	while (record) {
		response = ezusb_writememory(serial, be32_to_cpu(record->addr),
L
Linus Torvalds 已提交
1437
					     (unsigned char *)record->data,
1438
					     be16_to_cpu(record->len), 0xa0);
L
Linus Torvalds 已提交
1439
		if (response < 0) {
A
Alan Cox 已提交
1440
			dev_err(&serial->dev->dev, "ezusb_writememory failed for Keyspan firmware (%d %04X %p %d)\n",
1441 1442
				response, be32_to_cpu(record->addr),
				record->data, be16_to_cpu(record->len));
L
Linus Torvalds 已提交
1443 1444
			break;
		}
1445
		record = ihex_next_binrec(record);
L
Linus Torvalds 已提交
1446
	}
1447
	release_firmware(fw);
L
Linus Torvalds 已提交
1448 1449 1450 1451 1452
		/* 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 don't want this device to have a driver assigned to it. */
A
Alan Cox 已提交
1453
	return 1;
L
Linus Torvalds 已提交
1454 1455 1456
}

/* Helper functions used by keyspan_setup_urbs */
1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474
static struct usb_endpoint_descriptor const *find_ep(struct usb_serial const *serial,
						     int endpoint)
{
	struct usb_host_interface *iface_desc;
	struct usb_endpoint_descriptor *ep;
	int i;

	iface_desc = serial->interface->cur_altsetting;
	for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
		ep = &iface_desc->endpoint[i].desc;
		if (ep->bEndpointAddress == endpoint)
			return ep;
	}
	dev_warn(&serial->interface->dev, "found no endpoint descriptor for "
		 "endpoint %x\n", endpoint);
	return NULL;
}

A
Alan Cox 已提交
1475
static struct urb *keyspan_setup_urb(struct usb_serial *serial, int endpoint,
L
Linus Torvalds 已提交
1476
				      int dir, void *ctx, char *buf, int len,
1477
				      void (*callback)(struct urb *))
L
Linus Torvalds 已提交
1478 1479
{
	struct urb *urb;
1480 1481
	struct usb_endpoint_descriptor const *ep_desc;
	char const *ep_type_name;
L
Linus Torvalds 已提交
1482 1483 1484 1485

	if (endpoint == -1)
		return NULL;		/* endpoint not needed */

A
Alan Cox 已提交
1486
	dbg("%s - alloc for endpoint %d.", __func__, endpoint);
L
Linus Torvalds 已提交
1487 1488
	urb = usb_alloc_urb(0, GFP_KERNEL);		/* No ISO */
	if (urb == NULL) {
A
Alan Cox 已提交
1489
		dbg("%s - alloc for endpoint %d failed.", __func__, endpoint);
L
Linus Torvalds 已提交
1490 1491 1492
		return NULL;
	}

1493 1494 1495 1496 1497
	if (endpoint == 0) {
		/* control EP filled in when used */
		return urb;
	}

1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516
	ep_desc = find_ep(serial, endpoint);
	if (!ep_desc) {
		/* leak the urb, something's wrong and the callers don't care */
		return urb;
	}
	if (usb_endpoint_xfer_int(ep_desc)) {
		ep_type_name = "INT";
		usb_fill_int_urb(urb, serial->dev,
				 usb_sndintpipe(serial->dev, endpoint) | dir,
				 buf, len, callback, ctx,
				 ep_desc->bInterval);
	} else if (usb_endpoint_xfer_bulk(ep_desc)) {
		ep_type_name = "BULK";
		usb_fill_bulk_urb(urb, serial->dev,
				  usb_sndbulkpipe(serial->dev, endpoint) | dir,
				  buf, len, callback, ctx);
	} else {
		dev_warn(&serial->interface->dev,
			 "unsupported endpoint type %x\n",
1517
			 usb_endpoint_type(ep_desc));
1518 1519 1520
		usb_free_urb(urb);
		return NULL;
	}
L
Linus Torvalds 已提交
1521

1522 1523
	dbg("%s - using urb %p for %s endpoint %x",
	    __func__, urb, ep_type_name, endpoint);
L
Linus Torvalds 已提交
1524 1525 1526 1527
	return urb;
}

static struct callbacks {
1528 1529 1530 1531 1532 1533
	void	(*instat_callback)(struct urb *);
	void	(*glocont_callback)(struct urb *);
	void	(*indat_callback)(struct urb *);
	void	(*outdat_callback)(struct urb *);
	void	(*inack_callback)(struct urb *);
	void	(*outcont_callback)(struct urb *);
L
Linus Torvalds 已提交
1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561
} keyspan_callbacks[] = {
	{
		/* msg_usa26 callbacks */
		.instat_callback =	usa26_instat_callback,
		.glocont_callback =	usa26_glocont_callback,
		.indat_callback =	usa26_indat_callback,
		.outdat_callback =	usa2x_outdat_callback,
		.inack_callback =	usa26_inack_callback,
		.outcont_callback =	usa26_outcont_callback,
	}, {
		/* msg_usa28 callbacks */
		.instat_callback =	usa28_instat_callback,
		.glocont_callback =	usa28_glocont_callback,
		.indat_callback =	usa28_indat_callback,
		.outdat_callback =	usa2x_outdat_callback,
		.inack_callback =	usa28_inack_callback,
		.outcont_callback =	usa28_outcont_callback,
	}, {
		/* msg_usa49 callbacks */
		.instat_callback =	usa49_instat_callback,
		.glocont_callback =	usa49_glocont_callback,
		.indat_callback =	usa49_indat_callback,
		.outdat_callback =	usa2x_outdat_callback,
		.inack_callback =	usa49_inack_callback,
		.outcont_callback =	usa49_outcont_callback,
	}, {
		/* msg_usa90 callbacks */
		.instat_callback =	usa90_instat_callback,
A
Alan Cox 已提交
1562
		.glocont_callback =	usa28_glocont_callback,
L
Linus Torvalds 已提交
1563 1564 1565 1566
		.indat_callback =	usa90_indat_callback,
		.outdat_callback =	usa2x_outdat_callback,
		.inack_callback =	usa28_inack_callback,
		.outcont_callback =	usa90_outcont_callback,
1567 1568 1569 1570 1571 1572 1573 1574
	}, {
		/* msg_usa67 callbacks */
		.instat_callback =	usa67_instat_callback,
		.glocont_callback =	usa67_glocont_callback,
		.indat_callback =	usa26_indat_callback,
		.outdat_callback =	usa2x_outdat_callback,
		.inack_callback =	usa26_inack_callback,
		.outcont_callback =	usa26_outcont_callback,
L
Linus Torvalds 已提交
1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589
	}
};

	/* Generic setup urbs function that uses
	   data in device_details */
static void keyspan_setup_urbs(struct usb_serial *serial)
{
	int				i, j;
	struct keyspan_serial_private 	*s_priv;
	const struct keyspan_device_details	*d_details;
	struct usb_serial_port		*port;
	struct keyspan_port_private	*p_priv;
	struct callbacks		*cback;
	int				endp;

A
Alan Cox 已提交
1590
	dbg("%s", __func__);
L
Linus Torvalds 已提交
1591 1592 1593 1594

	s_priv = usb_get_serial_data(serial);
	d_details = s_priv->device_details;

A
Alan Cox 已提交
1595
	/* Setup values for the various callback routines */
L
Linus Torvalds 已提交
1596 1597
	cback = &keyspan_callbacks[d_details->msg_format];

A
Alan Cox 已提交
1598 1599
	/* Allocate and set up urbs for each one that is in use,
	   starting with instat endpoints */
L
Linus Torvalds 已提交
1600 1601 1602 1603 1604
	s_priv->instat_urb = keyspan_setup_urb
		(serial, d_details->instat_endpoint, USB_DIR_IN,
		 serial, s_priv->instat_buf, INSTAT_BUFLEN,
		 cback->instat_callback);

1605 1606 1607 1608 1609
	s_priv->indat_urb = keyspan_setup_urb
		(serial, d_details->indat_endpoint, USB_DIR_IN,
		 serial, s_priv->indat_buf, INDAT49W_BUFLEN,
		 usa49wg_indat_callback);

L
Linus Torvalds 已提交
1610 1611 1612 1613 1614
	s_priv->glocont_urb = keyspan_setup_urb
		(serial, d_details->glocont_endpoint, USB_DIR_OUT,
		 serial, s_priv->glocont_buf, GLOCONT_BUFLEN,
		 cback->glocont_callback);

A
Alan Cox 已提交
1615 1616
	/* Setup endpoints for each port specific thing */
	for (i = 0; i < d_details->num_ports; i++) {
L
Linus Torvalds 已提交
1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651
		port = serial->port[i];
		p_priv = usb_get_serial_port_data(port);

		/* Do indat endpoints first, once for each flip */
		endp = d_details->indat_endpoints[i];
		for (j = 0; j <= d_details->indat_endp_flip; ++j, ++endp) {
			p_priv->in_urbs[j] = keyspan_setup_urb
				(serial, endp, USB_DIR_IN, port,
				 p_priv->in_buffer[j], 64,
				 cback->indat_callback);
		}
		for (; j < 2; ++j)
			p_priv->in_urbs[j] = NULL;

		/* outdat endpoints also have flip */
		endp = d_details->outdat_endpoints[i];
		for (j = 0; j <= d_details->outdat_endp_flip; ++j, ++endp) {
			p_priv->out_urbs[j] = keyspan_setup_urb
				(serial, endp, USB_DIR_OUT, port,
				 p_priv->out_buffer[j], 64,
				 cback->outdat_callback);
		}
		for (; j < 2; ++j)
			p_priv->out_urbs[j] = NULL;

		/* inack endpoint */
		p_priv->inack_urb = keyspan_setup_urb
			(serial, d_details->inack_endpoints[i], USB_DIR_IN,
			 port, p_priv->inack_buffer, 1, cback->inack_callback);

		/* outcont endpoint */
		p_priv->outcont_urb = keyspan_setup_urb
			(serial, d_details->outcont_endpoints[i], USB_DIR_OUT,
			 port, p_priv->outcont_buffer, 64,
			 cback->outcont_callback);
A
Alan Cox 已提交
1652
	}
L
Linus Torvalds 已提交
1653 1654 1655 1656 1657 1658 1659
}

/* usa19 function doesn't require prescaler */
static int keyspan_usa19_calc_baud(u32 baud_rate, u32 baudclk, u8 *rate_hi,
				   u8 *rate_low, u8 *prescaler, int portnum)
{
	u32 	b16,	/* baud rate times 16 (actual rate used internally) */
A
Alan Cox 已提交
1660
		div,	/* divisor */
L
Linus Torvalds 已提交
1661 1662
		cnt;	/* inverse of divisor (programmed into 8051) */

A
Alan Cox 已提交
1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678
	dbg("%s - %d.", __func__, baud_rate);

	/* prevent divide by zero...  */
	b16 = baud_rate * 16L;
	if (b16 == 0)
		return KEYSPAN_INVALID_BAUD_RATE;
	/* Any "standard" rate over 57k6 is marginal on the USA-19
	   as we run out of divisor resolution. */
	if (baud_rate > 57600)
		return KEYSPAN_INVALID_BAUD_RATE;

	/* calculate the divisor and the counter (its inverse) */
	div = baudclk / b16;
	if (div == 0)
		return KEYSPAN_INVALID_BAUD_RATE;
	else
L
Linus Torvalds 已提交
1679 1680
		cnt = 0 - div;

A
Alan Cox 已提交
1681 1682
	if (div > 0xffff)
		return KEYSPAN_INVALID_BAUD_RATE;
L
Linus Torvalds 已提交
1683

A
Alan Cox 已提交
1684 1685
	/* return the counter values if non-null */
	if (rate_low)
L
Linus Torvalds 已提交
1686
		*rate_low = (u8) (cnt & 0xff);
A
Alan Cox 已提交
1687
	if (rate_hi)
L
Linus Torvalds 已提交
1688
		*rate_hi = (u8) ((cnt >> 8) & 0xff);
A
Alan Cox 已提交
1689 1690 1691 1692
	if (rate_low && rate_hi)
		dbg("%s - %d %02x %02x.",
				__func__, baud_rate, *rate_hi, *rate_low);
	return KEYSPAN_BAUD_RATE_OK;
L
Linus Torvalds 已提交
1693 1694 1695 1696 1697 1698 1699
}

/* usa19hs function doesn't require prescaler */
static int keyspan_usa19hs_calc_baud(u32 baud_rate, u32 baudclk, u8 *rate_hi,
				   u8 *rate_low, u8 *prescaler, int portnum)
{
	u32 	b16,	/* baud rate times 16 (actual rate used internally) */
A
Alan Cox 已提交
1700
			div;	/* divisor */
L
Linus Torvalds 已提交
1701

A
Alan Cox 已提交
1702
	dbg("%s - %d.", __func__, baud_rate);
L
Linus Torvalds 已提交
1703

A
Alan Cox 已提交
1704 1705 1706 1707
	/* prevent divide by zero...  */
	b16 = baud_rate * 16L;
	if (b16 == 0)
		return KEYSPAN_INVALID_BAUD_RATE;
L
Linus Torvalds 已提交
1708

A
Alan Cox 已提交
1709 1710 1711 1712
	/* calculate the divisor */
	div = baudclk / b16;
	if (div == 0)
		return KEYSPAN_INVALID_BAUD_RATE;
L
Linus Torvalds 已提交
1713

A
Alan Cox 已提交
1714 1715
	if (div > 0xffff)
		return KEYSPAN_INVALID_BAUD_RATE;
L
Linus Torvalds 已提交
1716

A
Alan Cox 已提交
1717 1718
	/* return the counter values if non-null */
	if (rate_low)
L
Linus Torvalds 已提交
1719
		*rate_low = (u8) (div & 0xff);
A
Alan Cox 已提交
1720 1721

	if (rate_hi)
L
Linus Torvalds 已提交
1722
		*rate_hi = (u8) ((div >> 8) & 0xff);
A
Alan Cox 已提交
1723 1724 1725 1726 1727 1728

	if (rate_low && rate_hi)
		dbg("%s - %d %02x %02x.",
			__func__, baud_rate, *rate_hi, *rate_low);

	return KEYSPAN_BAUD_RATE_OK;
L
Linus Torvalds 已提交
1729 1730 1731 1732 1733 1734 1735
}

static int keyspan_usa19w_calc_baud(u32 baud_rate, u32 baudclk, u8 *rate_hi,
				    u8 *rate_low, u8 *prescaler, int portnum)
{
	u32 	b16,	/* baud rate times 16 (actual rate used internally) */
		clk,	/* clock with 13/8 prescaler */
A
Alan Cox 已提交
1736
		div,	/* divisor using 13/8 prescaler */
L
Linus Torvalds 已提交
1737 1738 1739 1740 1741 1742
		res,	/* resulting baud rate using 13/8 prescaler */
		diff,	/* error using 13/8 prescaler */
		smallest_diff;
	u8	best_prescaler;
	int	i;

A
Alan Cox 已提交
1743
	dbg("%s - %d.", __func__, baud_rate);
L
Linus Torvalds 已提交
1744

A
Alan Cox 已提交
1745 1746 1747 1748
	/* prevent divide by zero */
	b16 = baud_rate * 16L;
	if (b16 == 0)
		return KEYSPAN_INVALID_BAUD_RATE;
L
Linus Torvalds 已提交
1749

A
Alan Cox 已提交
1750 1751 1752 1753
	/* Calculate prescaler by trying them all and looking
	   for best fit */

	/* start with largest possible difference */
L
Linus Torvalds 已提交
1754 1755 1756 1757 1758
	smallest_diff = 0xffffffff;

		/* 0 is an invalid prescaler, used as a flag */
	best_prescaler = 0;

A
Alan Cox 已提交
1759
	for (i = 8; i <= 0xff; ++i) {
L
Linus Torvalds 已提交
1760
		clk = (baudclk * 8) / (u32) i;
A
Alan Cox 已提交
1761 1762 1763

		div = clk / b16;
		if (div == 0)
L
Linus Torvalds 已提交
1764 1765 1766
			continue;

		res = clk / div;
A
Alan Cox 已提交
1767
		diff = (res > b16) ? (res-b16) : (b16-res);
L
Linus Torvalds 已提交
1768

A
Alan Cox 已提交
1769
		if (diff < smallest_diff) {
L
Linus Torvalds 已提交
1770 1771 1772 1773 1774
			best_prescaler = i;
			smallest_diff = diff;
		}
	}

A
Alan Cox 已提交
1775 1776
	if (best_prescaler == 0)
		return KEYSPAN_INVALID_BAUD_RATE;
L
Linus Torvalds 已提交
1777 1778 1779 1780

	clk = (baudclk * 8) / (u32) best_prescaler;
	div = clk / b16;

A
Alan Cox 已提交
1781 1782
	/* return the divisor and prescaler if non-null */
	if (rate_low)
L
Linus Torvalds 已提交
1783
		*rate_low = (u8) (div & 0xff);
A
Alan Cox 已提交
1784
	if (rate_hi)
L
Linus Torvalds 已提交
1785 1786 1787
		*rate_hi = (u8) ((div >> 8) & 0xff);
	if (prescaler) {
		*prescaler = best_prescaler;
1788
		/*  dbg("%s - %d %d", __func__, *prescaler, div); */
L
Linus Torvalds 已提交
1789
	}
A
Alan Cox 已提交
1790
	return KEYSPAN_BAUD_RATE_OK;
L
Linus Torvalds 已提交
1791 1792 1793 1794 1795 1796 1797
}

	/* USA-28 supports different maximum baud rates on each port */
static int keyspan_usa28_calc_baud(u32 baud_rate, u32 baudclk, u8 *rate_hi,
				    u8 *rate_low, u8 *prescaler, int portnum)
{
	u32 	b16,	/* baud rate times 16 (actual rate used internally) */
A
Alan Cox 已提交
1798
		div,	/* divisor */
L
Linus Torvalds 已提交
1799 1800
		cnt;	/* inverse of divisor (programmed into 8051) */

A
Alan Cox 已提交
1801
	dbg("%s - %d.", __func__, baud_rate);
L
Linus Torvalds 已提交
1802 1803

		/* prevent divide by zero */
A
Alan Cox 已提交
1804 1805 1806 1807 1808 1809 1810 1811 1812
	b16 = baud_rate * 16L;
	if (b16 == 0)
		return KEYSPAN_INVALID_BAUD_RATE;

	/* calculate the divisor and the counter (its inverse) */
	div = KEYSPAN_USA28_BAUDCLK / b16;
	if (div == 0)
		return KEYSPAN_INVALID_BAUD_RATE;
	else
L
Linus Torvalds 已提交
1813 1814
		cnt = 0 - div;

A
Alan Cox 已提交
1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825
	/* check for out of range, based on portnum,
	   and return result */
	if (portnum == 0) {
		if (div > 0xffff)
			return KEYSPAN_INVALID_BAUD_RATE;
	} else {
		if (portnum == 1) {
			if (div > 0xff)
				return KEYSPAN_INVALID_BAUD_RATE;
		} else
			return KEYSPAN_INVALID_BAUD_RATE;
L
Linus Torvalds 已提交
1826 1827 1828 1829
	}

		/* return the counter values if not NULL
		   (port 1 will ignore retHi) */
A
Alan Cox 已提交
1830
	if (rate_low)
L
Linus Torvalds 已提交
1831
		*rate_low = (u8) (cnt & 0xff);
A
Alan Cox 已提交
1832
	if (rate_hi)
L
Linus Torvalds 已提交
1833
		*rate_hi = (u8) ((cnt >> 8) & 0xff);
A
Alan Cox 已提交
1834 1835
	dbg("%s - %d OK.", __func__, baud_rate);
	return KEYSPAN_BAUD_RATE_OK;
L
Linus Torvalds 已提交
1836 1837 1838 1839 1840 1841
}

static int keyspan_usa26_send_setup(struct usb_serial *serial,
				    struct usb_serial_port *port,
				    int reset_port)
{
A
Alan Cox 已提交
1842
	struct keyspan_usa26_portControlMessage	msg;
L
Linus Torvalds 已提交
1843 1844 1845 1846 1847 1848 1849
	struct keyspan_serial_private 		*s_priv;
	struct keyspan_port_private 		*p_priv;
	const struct keyspan_device_details	*d_details;
	int 					outcont_urb;
	struct urb				*this_urb;
	int 					device_port, err;

A
Alan Cox 已提交
1850
	dbg("%s reset=%d", __func__, reset_port);
L
Linus Torvalds 已提交
1851 1852 1853 1854 1855 1856 1857 1858 1859

	s_priv = usb_get_serial_data(serial);
	p_priv = usb_get_serial_port_data(port);
	d_details = s_priv->device_details;
	device_port = port->number - port->serial->minor;

	outcont_urb = d_details->outcont_endpoints[port->number];
	this_urb = p_priv->outcont_urb;

1860
	dbg("%s - endpoint %d", __func__, usb_pipeendpoint(this_urb->pipe));
L
Linus Torvalds 已提交
1861 1862 1863

		/* Make sure we have an urb then send the message */
	if (this_urb == NULL) {
1864
		dbg("%s - oops no urb.", __func__);
L
Linus Torvalds 已提交
1865 1866 1867 1868
		return -1;
	}

	/* Save reset port val for resend.
1869 1870
	   Don't overwrite resend for open/close condition. */
	if ((reset_port + 1) > p_priv->resend_cont)
L
Linus Torvalds 已提交
1871 1872
		p_priv->resend_cont = reset_port + 1;
	if (this_urb->status == -EINPROGRESS) {
A
Alan Cox 已提交
1873
		/*  dbg("%s - already writing", __func__); */
L
Linus Torvalds 已提交
1874
		mdelay(5);
A
Alan Cox 已提交
1875
		return -1;
L
Linus Torvalds 已提交
1876 1877
	}

A
Alan Cox 已提交
1878 1879 1880
	memset(&msg, 0, sizeof(struct keyspan_usa26_portControlMessage));

	/* Only set baud rate if it's changed */
L
Linus Torvalds 已提交
1881 1882 1883 1884 1885
	if (p_priv->old_baud != p_priv->baud) {
		p_priv->old_baud = p_priv->baud;
		msg.setClocking = 0xff;
		if (d_details->calculate_baud_rate
		    (p_priv->baud, d_details->baudclk, &msg.baudHi,
A
Alan Cox 已提交
1886 1887 1888
		     &msg.baudLo, &msg.prescaler, device_port) == KEYSPAN_INVALID_BAUD_RATE) {
			dbg("%s - Invalid baud rate %d requested, using 9600.",
						__func__, p_priv->baud);
L
Linus Torvalds 已提交
1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913
			msg.baudLo = 0;
			msg.baudHi = 125;	/* Values for 9600 baud */
			msg.prescaler = 10;
		}
		msg.setPrescaler = 0xff;
	}

	msg.lcr = (p_priv->cflag & CSTOPB)? STOPBITS_678_2: STOPBITS_5678_1;
	switch (p_priv->cflag & CSIZE) {
	case CS5:
		msg.lcr |= USA_DATABITS_5;
		break;
	case CS6:
		msg.lcr |= USA_DATABITS_6;
		break;
	case CS7:
		msg.lcr |= USA_DATABITS_7;
		break;
	case CS8:
		msg.lcr |= USA_DATABITS_8;
		break;
	}
	if (p_priv->cflag & PARENB) {
		/* note USA_PARITY_NONE == 0 */
		msg.lcr |= (p_priv->cflag & PARODD)?
A
Alan Cox 已提交
1914
			USA_PARITY_ODD : USA_PARITY_EVEN;
L
Linus Torvalds 已提交
1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954
	}
	msg.setLcr = 0xff;

	msg.ctsFlowControl = (p_priv->flow_control == flow_cts);
	msg.xonFlowControl = 0;
	msg.setFlowControl = 0xff;
	msg.forwardingLength = 16;
	msg.xonChar = 17;
	msg.xoffChar = 19;

	/* Opening port */
	if (reset_port == 1) {
		msg._txOn = 1;
		msg._txOff = 0;
		msg.txFlush = 0;
		msg.txBreak = 0;
		msg.rxOn = 1;
		msg.rxOff = 0;
		msg.rxFlush = 1;
		msg.rxForward = 0;
		msg.returnStatus = 0;
		msg.resetDataToggle = 0xff;
	}

	/* Closing port */
	else if (reset_port == 2) {
		msg._txOn = 0;
		msg._txOff = 1;
		msg.txFlush = 0;
		msg.txBreak = 0;
		msg.rxOn = 0;
		msg.rxOff = 1;
		msg.rxFlush = 1;
		msg.rxForward = 0;
		msg.returnStatus = 0;
		msg.resetDataToggle = 0;
	}

	/* Sending intermediate configs */
	else {
A
Alan Cox 已提交
1955
		msg._txOn = (!p_priv->break_on);
L
Linus Torvalds 已提交
1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966
		msg._txOff = 0;
		msg.txFlush = 0;
		msg.txBreak = (p_priv->break_on);
		msg.rxOn = 0;
		msg.rxOff = 0;
		msg.rxFlush = 0;
		msg.rxForward = 0;
		msg.returnStatus = 0;
		msg.resetDataToggle = 0x0;
	}

A
Alan Cox 已提交
1967
	/* Do handshaking outputs */
L
Linus Torvalds 已提交
1968 1969 1970 1971 1972
	msg.setTxTriState_setRts = 0xff;
	msg.txTriState_rts = p_priv->rts_state;

	msg.setHskoa_setDtr = 0xff;
	msg.hskoa_dtr = p_priv->dtr_state;
A
Alan Cox 已提交
1973

L
Linus Torvalds 已提交
1974
	p_priv->resend_cont = 0;
A
Alan Cox 已提交
1975 1976
	memcpy(this_urb->transfer_buffer, &msg, sizeof(msg));

L
Linus Torvalds 已提交
1977 1978 1979 1980
	/* send the data out the device on control endpoint */
	this_urb->transfer_buffer_length = sizeof(msg);

	this_urb->dev = serial->dev;
A
Alan Cox 已提交
1981 1982
	err = usb_submit_urb(this_urb, GFP_ATOMIC);
	if (err != 0)
1983
		dbg("%s - usb_submit_urb(setup) failed (%d)", __func__, err);
L
Linus Torvalds 已提交
1984 1985
#if 0
	else {
1986
		dbg("%s - usb_submit_urb(%d) OK %d bytes (end %d)", __func__
L
Linus Torvalds 已提交
1987 1988 1989 1990 1991
		    outcont_urb, this_urb->transfer_buffer_length,
		    usb_pipeendpoint(this_urb->pipe));
	}
#endif

1992
	return 0;
L
Linus Torvalds 已提交
1993 1994 1995 1996 1997 1998
}

static int keyspan_usa28_send_setup(struct usb_serial *serial,
				    struct usb_serial_port *port,
				    int reset_port)
{
A
Alan Cox 已提交
1999
	struct keyspan_usa28_portControlMessage	msg;
L
Linus Torvalds 已提交
2000 2001 2002 2003 2004 2005
	struct keyspan_serial_private	 	*s_priv;
	struct keyspan_port_private 		*p_priv;
	const struct keyspan_device_details	*d_details;
	struct urb				*this_urb;
	int 					device_port, err;

A
Alan Cox 已提交
2006
	dbg("%s", __func__);
L
Linus Torvalds 已提交
2007 2008 2009 2010 2011 2012 2013

	s_priv = usb_get_serial_data(serial);
	p_priv = usb_get_serial_port_data(port);
	d_details = s_priv->device_details;
	device_port = port->number - port->serial->minor;

	/* only do something if we have a bulk out endpoint */
A
Alan Cox 已提交
2014 2015
	this_urb = p_priv->outcont_urb;
	if (this_urb == NULL) {
2016
		dbg("%s - oops no urb.", __func__);
L
Linus Torvalds 已提交
2017 2018 2019 2020
		return -1;
	}

	/* Save reset port val for resend.
2021 2022
	   Don't overwrite resend for open/close condition. */
	if ((reset_port + 1) > p_priv->resend_cont)
L
Linus Torvalds 已提交
2023 2024
		p_priv->resend_cont = reset_port + 1;
	if (this_urb->status == -EINPROGRESS) {
A
Alan Cox 已提交
2025
		dbg("%s already writing", __func__);
L
Linus Torvalds 已提交
2026
		mdelay(5);
A
Alan Cox 已提交
2027
		return -1;
L
Linus Torvalds 已提交
2028 2029
	}

A
Alan Cox 已提交
2030
	memset(&msg, 0, sizeof(struct keyspan_usa28_portControlMessage));
L
Linus Torvalds 已提交
2031 2032 2033

	msg.setBaudRate = 1;
	if (d_details->calculate_baud_rate(p_priv->baud, d_details->baudclk,
A
Alan Cox 已提交
2034 2035 2036
		&msg.baudHi, &msg.baudLo, NULL, device_port) == KEYSPAN_INVALID_BAUD_RATE) {
		dbg("%s - Invalid baud rate requested %d.",
						__func__, p_priv->baud);
L
Linus Torvalds 已提交
2037 2038 2039 2040 2041 2042 2043 2044 2045 2046
		msg.baudLo = 0xff;
		msg.baudHi = 0xb2;	/* Values for 9600 baud */
	}

	/* If parity is enabled, we must calculate it ourselves. */
	msg.parity = 0;		/* XXX for now */

	msg.ctsFlowControl = (p_priv->flow_control == flow_cts);
	msg.xonFlowControl = 0;

A
Alan Cox 已提交
2047
	/* Do handshaking outputs, DTR is inverted relative to RTS */
L
Linus Torvalds 已提交
2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088
	msg.rts = p_priv->rts_state;
	msg.dtr = p_priv->dtr_state;

	msg.forwardingLength = 16;
	msg.forwardMs = 10;
	msg.breakThreshold = 45;
	msg.xonChar = 17;
	msg.xoffChar = 19;

	/*msg.returnStatus = 1;
	msg.resetDataToggle = 0xff;*/
	/* Opening port */
	if (reset_port == 1) {
		msg._txOn = 1;
		msg._txOff = 0;
		msg.txFlush = 0;
		msg.txForceXoff = 0;
		msg.txBreak = 0;
		msg.rxOn = 1;
		msg.rxOff = 0;
		msg.rxFlush = 1;
		msg.rxForward = 0;
		msg.returnStatus = 0;
		msg.resetDataToggle = 0xff;
	}
	/* Closing port */
	else if (reset_port == 2) {
		msg._txOn = 0;
		msg._txOff = 1;
		msg.txFlush = 0;
		msg.txForceXoff = 0;
		msg.txBreak = 0;
		msg.rxOn = 0;
		msg.rxOff = 1;
		msg.rxFlush = 1;
		msg.rxForward = 0;
		msg.returnStatus = 0;
		msg.resetDataToggle = 0;
	}
	/* Sending intermediate configs */
	else {
A
Alan Cox 已提交
2089
		msg._txOn = (!p_priv->break_on);
L
Linus Torvalds 已提交
2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102
		msg._txOff = 0;
		msg.txFlush = 0;
		msg.txForceXoff = 0;
		msg.txBreak = (p_priv->break_on);
		msg.rxOn = 0;
		msg.rxOff = 0;
		msg.rxFlush = 0;
		msg.rxForward = 0;
		msg.returnStatus = 0;
		msg.resetDataToggle = 0x0;
	}

	p_priv->resend_cont = 0;
A
Alan Cox 已提交
2103
	memcpy(this_urb->transfer_buffer, &msg, sizeof(msg));
L
Linus Torvalds 已提交
2104 2105 2106 2107 2108

	/* send the data out the device on control endpoint */
	this_urb->transfer_buffer_length = sizeof(msg);

	this_urb->dev = serial->dev;
A
Alan Cox 已提交
2109 2110
	err = usb_submit_urb(this_urb, GFP_ATOMIC);
	if (err != 0)
2111
		dbg("%s - usb_submit_urb(setup) failed", __func__);
L
Linus Torvalds 已提交
2112 2113
#if 0
	else {
2114
		dbg("%s - usb_submit_urb(setup) OK %d bytes", __func__,
L
Linus Torvalds 已提交
2115 2116 2117 2118
		    this_urb->transfer_buffer_length);
	}
#endif

2119
	return 0;
L
Linus Torvalds 已提交
2120 2121 2122 2123 2124 2125
}

static int keyspan_usa49_send_setup(struct usb_serial *serial,
				    struct usb_serial_port *port,
				    int reset_port)
{
2126 2127
	struct keyspan_usa49_portControlMessage	msg;
	struct usb_ctrlrequest 			*dr = NULL;
L
Linus Torvalds 已提交
2128 2129 2130 2131 2132 2133
	struct keyspan_serial_private 		*s_priv;
	struct keyspan_port_private 		*p_priv;
	const struct keyspan_device_details	*d_details;
	struct urb				*this_urb;
	int 					err, device_port;

A
Alan Cox 已提交
2134
	dbg("%s", __func__);
L
Linus Torvalds 已提交
2135 2136 2137 2138 2139 2140 2141

	s_priv = usb_get_serial_data(serial);
	p_priv = usb_get_serial_port_data(port);
	d_details = s_priv->device_details;

	this_urb = s_priv->glocont_urb;

2142
	/* Work out which port within the device is being setup */
L
Linus Torvalds 已提交
2143 2144
	device_port = port->number - port->serial->minor;

A
Alan Cox 已提交
2145 2146 2147
	dbg("%s - endpoint %d port %d (%d)",
			__func__, usb_pipeendpoint(this_urb->pipe),
			port->number, device_port);
L
Linus Torvalds 已提交
2148 2149 2150

		/* Make sure we have an urb then send the message */
	if (this_urb == NULL) {
2151
		dbg("%s - oops no urb for port %d.", __func__, port->number);
L
Linus Torvalds 已提交
2152 2153 2154 2155
		return -1;
	}

	/* Save reset port val for resend.
2156 2157
	   Don't overwrite resend for open/close condition. */
	if ((reset_port + 1) > p_priv->resend_cont)
L
Linus Torvalds 已提交
2158
		p_priv->resend_cont = reset_port + 1;
2159

L
Linus Torvalds 已提交
2160
	if (this_urb->status == -EINPROGRESS) {
A
Alan Cox 已提交
2161
		/*  dbg("%s - already writing", __func__); */
L
Linus Torvalds 已提交
2162
		mdelay(5);
A
Alan Cox 已提交
2163
		return -1;
L
Linus Torvalds 已提交
2164 2165
	}

A
Alan Cox 已提交
2166
	memset(&msg, 0, sizeof(struct keyspan_usa49_portControlMessage));
L
Linus Torvalds 已提交
2167 2168 2169

	/*msg.portNumber = port->number;*/
	msg.portNumber = device_port;
A
Alan Cox 已提交
2170 2171

	/* Only set baud rate if it's changed */
L
Linus Torvalds 已提交
2172 2173 2174 2175 2176
	if (p_priv->old_baud != p_priv->baud) {
		p_priv->old_baud = p_priv->baud;
		msg.setClocking = 0xff;
		if (d_details->calculate_baud_rate
		    (p_priv->baud, d_details->baudclk, &msg.baudHi,
A
Alan Cox 已提交
2177 2178 2179
		     &msg.baudLo, &msg.prescaler, device_port) == KEYSPAN_INVALID_BAUD_RATE) {
			dbg("%s - Invalid baud rate %d requested, using 9600.",
						__func__, p_priv->baud);
L
Linus Torvalds 已提交
2180 2181 2182 2183
			msg.baudLo = 0;
			msg.baudHi = 125;	/* Values for 9600 baud */
			msg.prescaler = 10;
		}
A
Alan Cox 已提交
2184
		/* msg.setPrescaler = 0xff; */
L
Linus Torvalds 已提交
2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204
	}

	msg.lcr = (p_priv->cflag & CSTOPB)? STOPBITS_678_2: STOPBITS_5678_1;
	switch (p_priv->cflag & CSIZE) {
	case CS5:
		msg.lcr |= USA_DATABITS_5;
		break;
	case CS6:
		msg.lcr |= USA_DATABITS_6;
		break;
	case CS7:
		msg.lcr |= USA_DATABITS_7;
		break;
	case CS8:
		msg.lcr |= USA_DATABITS_8;
		break;
	}
	if (p_priv->cflag & PARENB) {
		/* note USA_PARITY_NONE == 0 */
		msg.lcr |= (p_priv->cflag & PARODD)?
A
Alan Cox 已提交
2205
			USA_PARITY_ODD : USA_PARITY_EVEN;
L
Linus Torvalds 已提交
2206 2207 2208 2209 2210 2211
	}
	msg.setLcr = 0xff;

	msg.ctsFlowControl = (p_priv->flow_control == flow_cts);
	msg.xonFlowControl = 0;
	msg.setFlowControl = 0xff;
A
Alan Cox 已提交
2212

L
Linus Torvalds 已提交
2213 2214 2215 2216
	msg.forwardingLength = 16;
	msg.xonChar = 17;
	msg.xoffChar = 19;

A
Alan Cox 已提交
2217
	/* Opening port */
L
Linus Torvalds 已提交
2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248
	if (reset_port == 1) {
		msg._txOn = 1;
		msg._txOff = 0;
		msg.txFlush = 0;
		msg.txBreak = 0;
		msg.rxOn = 1;
		msg.rxOff = 0;
		msg.rxFlush = 1;
		msg.rxForward = 0;
		msg.returnStatus = 0;
		msg.resetDataToggle = 0xff;
		msg.enablePort = 1;
		msg.disablePort = 0;
	}
	/* Closing port */
	else if (reset_port == 2) {
		msg._txOn = 0;
		msg._txOff = 1;
		msg.txFlush = 0;
		msg.txBreak = 0;
		msg.rxOn = 0;
		msg.rxOff = 1;
		msg.rxFlush = 1;
		msg.rxForward = 0;
		msg.returnStatus = 0;
		msg.resetDataToggle = 0;
		msg.enablePort = 0;
		msg.disablePort = 1;
	}
	/* Sending intermediate configs */
	else {
A
Alan Cox 已提交
2249
		msg._txOn = (!p_priv->break_on);
L
Linus Torvalds 已提交
2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262
		msg._txOff = 0;
		msg.txFlush = 0;
		msg.txBreak = (p_priv->break_on);
		msg.rxOn = 0;
		msg.rxOff = 0;
		msg.rxFlush = 0;
		msg.rxForward = 0;
		msg.returnStatus = 0;
		msg.resetDataToggle = 0x0;
		msg.enablePort = 0;
		msg.disablePort = 0;
	}

A
Alan Cox 已提交
2263
	/* Do handshaking outputs */
L
Linus Torvalds 已提交
2264 2265 2266 2267 2268
	msg.setRts = 0xff;
	msg.rts = p_priv->rts_state;

	msg.setDtr = 0xff;
	msg.dtr = p_priv->dtr_state;
A
Alan Cox 已提交
2269

L
Linus Torvalds 已提交
2270
	p_priv->resend_cont = 0;
2271

A
Alan Cox 已提交
2272 2273
	/* if the device is a 49wg, we send control message on usb
	   control EP 0 */
2274 2275 2276 2277 2278 2279 2280 2281 2282

	if (d_details->product_id == keyspan_usa49wg_product_id) {
		dr = (void *)(s_priv->ctrl_buf);
		dr->bRequestType = USB_TYPE_VENDOR | USB_DIR_OUT;
		dr->bRequest = 0xB0;	/* 49wg control message */;
		dr->wValue = 0;
		dr->wIndex = 0;
		dr->wLength = cpu_to_le16(sizeof(msg));

A
Alan Cox 已提交
2283
		memcpy(s_priv->glocont_buf, &msg, sizeof(msg));
2284

A
Alan Cox 已提交
2285 2286 2287 2288
		usb_fill_control_urb(this_urb, serial->dev,
				usb_sndctrlpipe(serial->dev, 0),
				(unsigned char *)dr, s_priv->glocont_buf,
				sizeof(msg), usa49_glocont_callback, serial);
2289 2290 2291

	} else {
		memcpy(this_urb->transfer_buffer, &msg, sizeof(msg));
A
Alan Cox 已提交
2292

2293 2294
		/* send the data out the device on control endpoint */
		this_urb->transfer_buffer_length = sizeof(msg);
L
Linus Torvalds 已提交
2295

2296 2297
		this_urb->dev = serial->dev;
	}
A
Alan Cox 已提交
2298 2299
	err = usb_submit_urb(this_urb, GFP_ATOMIC);
	if (err != 0)
2300
		dbg("%s - usb_submit_urb(setup) failed (%d)", __func__, err);
L
Linus Torvalds 已提交
2301 2302
#if 0
	else {
2303
		dbg("%s - usb_submit_urb(%d) OK %d bytes (end %d)", __func__,
2304 2305
			   outcont_urb, this_urb->transfer_buffer_length,
			   usb_pipeendpoint(this_urb->pipe));
L
Linus Torvalds 已提交
2306 2307 2308
	}
#endif

2309
	return 0;
L
Linus Torvalds 已提交
2310 2311 2312 2313 2314 2315
}

static int keyspan_usa90_send_setup(struct usb_serial *serial,
				    struct usb_serial_port *port,
				    int reset_port)
{
A
Alan Cox 已提交
2316
	struct keyspan_usa90_portControlMessage	msg;
L
Linus Torvalds 已提交
2317 2318 2319 2320 2321 2322 2323
	struct keyspan_serial_private 		*s_priv;
	struct keyspan_port_private 		*p_priv;
	const struct keyspan_device_details	*d_details;
	struct urb				*this_urb;
	int 					err;
	u8						prescaler;

A
Alan Cox 已提交
2324
	dbg("%s", __func__);
L
Linus Torvalds 已提交
2325 2326 2327 2328 2329 2330

	s_priv = usb_get_serial_data(serial);
	p_priv = usb_get_serial_port_data(port);
	d_details = s_priv->device_details;

	/* only do something if we have a bulk out endpoint */
A
Alan Cox 已提交
2331 2332
	this_urb = p_priv->outcont_urb;
	if (this_urb == NULL) {
2333
		dbg("%s - oops no urb.", __func__);
L
Linus Torvalds 已提交
2334 2335 2336 2337 2338 2339 2340 2341
		return -1;
	}

	/* Save reset port val for resend.
	   Don't overwrite resend for open/close condition. */
	if ((reset_port + 1) > p_priv->resend_cont)
		p_priv->resend_cont = reset_port + 1;
	if (this_urb->status == -EINPROGRESS) {
A
Alan Cox 已提交
2342
		dbg("%s already writing", __func__);
L
Linus Torvalds 已提交
2343
		mdelay(5);
A
Alan Cox 已提交
2344
		return -1;
L
Linus Torvalds 已提交
2345 2346
	}

A
Alan Cox 已提交
2347
	memset(&msg, 0, sizeof(struct keyspan_usa90_portControlMessage));
L
Linus Torvalds 已提交
2348

A
Alan Cox 已提交
2349
	/* Only set baud rate if it's changed */
L
Linus Torvalds 已提交
2350 2351 2352 2353 2354
	if (p_priv->old_baud != p_priv->baud) {
		p_priv->old_baud = p_priv->baud;
		msg.setClocking = 0x01;
		if (d_details->calculate_baud_rate
		    (p_priv->baud, d_details->baudclk, &msg.baudHi,
A
Alan Cox 已提交
2355 2356 2357
		     &msg.baudLo, &prescaler, 0) == KEYSPAN_INVALID_BAUD_RATE) {
			dbg("%s - Invalid baud rate %d requested, using 9600.",
						__func__, p_priv->baud);
L
Linus Torvalds 已提交
2358
			p_priv->baud = 9600;
A
Alan Cox 已提交
2359
			d_details->calculate_baud_rate(p_priv->baud, d_details->baudclk,
L
Linus Torvalds 已提交
2360 2361 2362 2363 2364 2365 2366
				&msg.baudHi, &msg.baudLo, &prescaler, 0);
		}
		msg.setRxMode = 1;
		msg.setTxMode = 1;
	}

	/* modes must always be correctly specified */
A
Alan Cox 已提交
2367
	if (p_priv->baud > 57600) {
L
Linus Torvalds 已提交
2368 2369
		msg.rxMode = RXMODE_DMA;
		msg.txMode = TXMODE_DMA;
A
Alan Cox 已提交
2370
	} else {
L
Linus Torvalds 已提交
2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392
		msg.rxMode = RXMODE_BYHAND;
		msg.txMode = TXMODE_BYHAND;
	}

	msg.lcr = (p_priv->cflag & CSTOPB)? STOPBITS_678_2: STOPBITS_5678_1;
	switch (p_priv->cflag & CSIZE) {
	case CS5:
		msg.lcr |= USA_DATABITS_5;
		break;
	case CS6:
		msg.lcr |= USA_DATABITS_6;
		break;
	case CS7:
		msg.lcr |= USA_DATABITS_7;
		break;
	case CS8:
		msg.lcr |= USA_DATABITS_8;
		break;
	}
	if (p_priv->cflag & PARENB) {
		/* note USA_PARITY_NONE == 0 */
		msg.lcr |= (p_priv->cflag & PARODD)?
A
Alan Cox 已提交
2393
			USA_PARITY_ODD : USA_PARITY_EVEN;
L
Linus Torvalds 已提交
2394 2395 2396 2397 2398 2399 2400 2401 2402 2403
	}
	if (p_priv->old_cflag != p_priv->cflag) {
		p_priv->old_cflag = p_priv->cflag;
		msg.setLcr = 0x01;
	}

	if (p_priv->flow_control == flow_cts)
		msg.txFlowControl = TXFLOW_CTS;
	msg.setTxFlowControl = 0x01;
	msg.setRxFlowControl = 0x01;
A
Alan Cox 已提交
2404

L
Linus Torvalds 已提交
2405
	msg.rxForwardingLength = 16;
A
Alan Cox 已提交
2406
	msg.rxForwardingTimeout = 16;
L
Linus Torvalds 已提交
2407 2408 2409 2410
	msg.txAckSetting = 0;
	msg.xonChar = 17;
	msg.xoffChar = 19;

A
Alan Cox 已提交
2411
	/* Opening port */
L
Linus Torvalds 已提交
2412 2413 2414 2415 2416 2417
	if (reset_port == 1) {
		msg.portEnabled = 1;
		msg.rxFlush = 1;
		msg.txBreak = (p_priv->break_on);
	}
	/* Closing port */
A
Alan Cox 已提交
2418
	else if (reset_port == 2)
L
Linus Torvalds 已提交
2419 2420 2421
		msg.portEnabled = 0;
	/* Sending intermediate configs */
	else {
A
Alan Cox 已提交
2422
		if (port->port.count)
L
Linus Torvalds 已提交
2423 2424 2425 2426
			msg.portEnabled = 1;
		msg.txBreak = (p_priv->break_on);
	}

A
Alan Cox 已提交
2427
	/* Do handshaking outputs */
L
Linus Torvalds 已提交
2428 2429 2430 2431 2432
	msg.setRts = 0x01;
	msg.rts = p_priv->rts_state;

	msg.setDtr = 0x01;
	msg.dtr = p_priv->dtr_state;
A
Alan Cox 已提交
2433

L
Linus Torvalds 已提交
2434
	p_priv->resend_cont = 0;
A
Alan Cox 已提交
2435 2436
	memcpy(this_urb->transfer_buffer, &msg, sizeof(msg));

L
Linus Torvalds 已提交
2437 2438 2439 2440
	/* send the data out the device on control endpoint */
	this_urb->transfer_buffer_length = sizeof(msg);

	this_urb->dev = serial->dev;
A
Alan Cox 已提交
2441 2442
	err = usb_submit_urb(this_urb, GFP_ATOMIC);
	if (err != 0)
2443
		dbg("%s - usb_submit_urb(setup) failed (%d)", __func__, err);
2444
	return 0;
L
Linus Torvalds 已提交
2445 2446
}

2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457
static int keyspan_usa67_send_setup(struct usb_serial *serial,
				    struct usb_serial_port *port,
				    int reset_port)
{
	struct keyspan_usa67_portControlMessage	msg;
	struct keyspan_serial_private 		*s_priv;
	struct keyspan_port_private 		*p_priv;
	const struct keyspan_device_details	*d_details;
	struct urb				*this_urb;
	int 					err, device_port;

A
Alan Cox 已提交
2458
	dbg("%s", __func__);
2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470

	s_priv = usb_get_serial_data(serial);
	p_priv = usb_get_serial_port_data(port);
	d_details = s_priv->device_details;

	this_urb = s_priv->glocont_urb;

	/* Work out which port within the device is being setup */
	device_port = port->number - port->serial->minor;

	/* Make sure we have an urb then send the message */
	if (this_urb == NULL) {
2471
		dbg("%s - oops no urb for port %d.", __func__,
2472 2473 2474 2475 2476 2477 2478 2479 2480
			port->number);
		return -1;
	}

	/* Save reset port val for resend.
	   Don't overwrite resend for open/close condition. */
	if ((reset_port + 1) > p_priv->resend_cont)
		p_priv->resend_cont = reset_port + 1;
	if (this_urb->status == -EINPROGRESS) {
A
Alan Cox 已提交
2481
		/*  dbg("%s - already writing", __func__); */
2482
		mdelay(5);
A
Alan Cox 已提交
2483
		return -1;
2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495
	}

	memset(&msg, 0, sizeof(struct keyspan_usa67_portControlMessage));

	msg.port = device_port;

	/* Only set baud rate if it's changed */
	if (p_priv->old_baud != p_priv->baud) {
		p_priv->old_baud = p_priv->baud;
		msg.setClocking = 0xff;
		if (d_details->calculate_baud_rate
		    (p_priv->baud, d_details->baudclk, &msg.baudHi,
A
Alan Cox 已提交
2496 2497 2498
		     &msg.baudLo, &msg.prescaler, device_port) == KEYSPAN_INVALID_BAUD_RATE) {
			dbg("%s - Invalid baud rate %d requested, using 9600.",
						__func__, p_priv->baud);
2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523
			msg.baudLo = 0;
			msg.baudHi = 125;	/* Values for 9600 baud */
			msg.prescaler = 10;
		}
		msg.setPrescaler = 0xff;
	}

	msg.lcr = (p_priv->cflag & CSTOPB) ? STOPBITS_678_2 : STOPBITS_5678_1;
	switch (p_priv->cflag & CSIZE) {
	case CS5:
		msg.lcr |= USA_DATABITS_5;
		break;
	case CS6:
		msg.lcr |= USA_DATABITS_6;
		break;
	case CS7:
		msg.lcr |= USA_DATABITS_7;
		break;
	case CS8:
		msg.lcr |= USA_DATABITS_8;
		break;
	}
	if (p_priv->cflag & PARENB) {
		/* note USA_PARITY_NONE == 0 */
		msg.lcr |= (p_priv->cflag & PARODD)?
A
Alan Cox 已提交
2524
					USA_PARITY_ODD : USA_PARITY_EVEN;
2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560
	}
	msg.setLcr = 0xff;

	msg.ctsFlowControl = (p_priv->flow_control == flow_cts);
	msg.xonFlowControl = 0;
	msg.setFlowControl = 0xff;
	msg.forwardingLength = 16;
	msg.xonChar = 17;
	msg.xoffChar = 19;

	if (reset_port == 1) {
		/* Opening port */
		msg._txOn = 1;
		msg._txOff = 0;
		msg.txFlush = 0;
		msg.txBreak = 0;
		msg.rxOn = 1;
		msg.rxOff = 0;
		msg.rxFlush = 1;
		msg.rxForward = 0;
		msg.returnStatus = 0;
		msg.resetDataToggle = 0xff;
	} else if (reset_port == 2) {
		/* Closing port */
		msg._txOn = 0;
		msg._txOff = 1;
		msg.txFlush = 0;
		msg.txBreak = 0;
		msg.rxOn = 0;
		msg.rxOff = 1;
		msg.rxFlush = 1;
		msg.rxForward = 0;
		msg.returnStatus = 0;
		msg.resetDataToggle = 0;
	} else {
		/* Sending intermediate configs */
A
Alan Cox 已提交
2561
		msg._txOn = (!p_priv->break_on);
2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589
		msg._txOff = 0;
		msg.txFlush = 0;
		msg.txBreak = (p_priv->break_on);
		msg.rxOn = 0;
		msg.rxOff = 0;
		msg.rxFlush = 0;
		msg.rxForward = 0;
		msg.returnStatus = 0;
		msg.resetDataToggle = 0x0;
	}

	/* Do handshaking outputs */
	msg.setTxTriState_setRts = 0xff;
	msg.txTriState_rts = p_priv->rts_state;

	msg.setHskoa_setDtr = 0xff;
	msg.hskoa_dtr = p_priv->dtr_state;

	p_priv->resend_cont = 0;

	memcpy(this_urb->transfer_buffer, &msg, sizeof(msg));

	/* send the data out the device on control endpoint */
	this_urb->transfer_buffer_length = sizeof(msg);
	this_urb->dev = serial->dev;

	err = usb_submit_urb(this_urb, GFP_ATOMIC);
	if (err != 0)
2590
		dbg("%s - usb_submit_urb(setup) failed (%d)", __func__,
2591
				err);
2592
	return 0;
2593 2594
}

L
Linus Torvalds 已提交
2595 2596 2597 2598 2599 2600
static void keyspan_send_setup(struct usb_serial_port *port, int reset_port)
{
	struct usb_serial *serial = port->serial;
	struct keyspan_serial_private *s_priv;
	const struct keyspan_device_details *d_details;

A
Alan Cox 已提交
2601
	dbg("%s", __func__);
L
Linus Torvalds 已提交
2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618

	s_priv = usb_get_serial_data(serial);
	d_details = s_priv->device_details;

	switch (d_details->msg_format) {
	case msg_usa26:
		keyspan_usa26_send_setup(serial, port, reset_port);
		break;
	case msg_usa28:
		keyspan_usa28_send_setup(serial, port, reset_port);
		break;
	case msg_usa49:
		keyspan_usa49_send_setup(serial, port, reset_port);
		break;
	case msg_usa90:
		keyspan_usa90_send_setup(serial, port, reset_port);
		break;
2619 2620 2621
	case msg_usa67:
		keyspan_usa67_send_setup(serial, port, reset_port);
		break;
L
Linus Torvalds 已提交
2622 2623 2624 2625 2626 2627
	}
}


/* Gets called by the "real" driver (ie once firmware is loaded
   and renumeration has taken place. */
A
Alan Cox 已提交
2628
static int keyspan_startup(struct usb_serial *serial)
L
Linus Torvalds 已提交
2629 2630 2631 2632 2633 2634 2635
{
	int				i, err;
	struct usb_serial_port		*port;
	struct keyspan_serial_private 	*s_priv;
	struct keyspan_port_private	*p_priv;
	const struct keyspan_device_details	*d_details;

2636
	dbg("%s", __func__);
L
Linus Torvalds 已提交
2637 2638

	for (i = 0; (d_details = keyspan_devices[i]) != NULL; ++i)
A
Alan Cox 已提交
2639 2640
		if (d_details->product_id ==
				le16_to_cpu(serial->dev->descriptor.idProduct))
L
Linus Torvalds 已提交
2641 2642
			break;
	if (d_details == NULL) {
A
Alan Cox 已提交
2643 2644
		dev_err(&serial->dev->dev, "%s - unknown product id %x\n",
		    __func__, le16_to_cpu(serial->dev->descriptor.idProduct));
L
Linus Torvalds 已提交
2645 2646 2647 2648
		return 1;
	}

	/* Setup private data for serial driver */
2649
	s_priv = kzalloc(sizeof(struct keyspan_serial_private), GFP_KERNEL);
L
Linus Torvalds 已提交
2650
	if (!s_priv) {
A
Alan Cox 已提交
2651 2652
		dbg("%s - kmalloc for keyspan_serial_private failed.",
								__func__);
L
Linus Torvalds 已提交
2653 2654 2655 2656 2657 2658 2659 2660 2661
		return -ENOMEM;
	}

	s_priv->device_details = d_details;
	usb_set_serial_data(serial, s_priv);

	/* Now setup per port private data */
	for (i = 0; i < serial->num_ports; i++) {
		port = serial->port[i];
A
Alan Cox 已提交
2662 2663
		p_priv = kzalloc(sizeof(struct keyspan_port_private),
								GFP_KERNEL);
L
Linus Torvalds 已提交
2664
		if (!p_priv) {
2665
			dbg("%s - kmalloc for keyspan_port_private (%d) failed!.", __func__, i);
A
Alan Cox 已提交
2666
			return 1;
L
Linus Torvalds 已提交
2667 2668 2669 2670 2671 2672 2673
		}
		p_priv->device_details = d_details;
		usb_set_serial_port_data(port, p_priv);
	}

	keyspan_setup_urbs(serial);

2674 2675 2676 2677
	if (s_priv->instat_urb != NULL) {
		s_priv->instat_urb->dev = serial->dev;
		err = usb_submit_urb(s_priv->instat_urb, GFP_KERNEL);
		if (err != 0)
2678
			dbg("%s - submit instat urb failed %d", __func__,
2679 2680 2681 2682 2683 2684
				err);
	}
	if (s_priv->indat_urb != NULL) {
		s_priv->indat_urb->dev = serial->dev;
		err = usb_submit_urb(s_priv->indat_urb, GFP_KERNEL);
		if (err != 0)
2685
			dbg("%s - submit indat urb failed %d", __func__,
2686
				err);
L
Linus Torvalds 已提交
2687
	}
A
Alan Cox 已提交
2688

2689
	return 0;
L
Linus Torvalds 已提交
2690 2691
}

2692
static void keyspan_disconnect(struct usb_serial *serial)
L
Linus Torvalds 已提交
2693 2694 2695 2696 2697 2698
{
	int				i, j;
	struct usb_serial_port		*port;
	struct keyspan_serial_private 	*s_priv;
	struct keyspan_port_private	*p_priv;

2699
	dbg("%s", __func__);
L
Linus Torvalds 已提交
2700 2701 2702 2703 2704 2705

	s_priv = usb_get_serial_data(serial);

	/* Stop reading/writing urbs */
	stop_urb(s_priv->instat_urb);
	stop_urb(s_priv->glocont_urb);
2706
	stop_urb(s_priv->indat_urb);
L
Linus Torvalds 已提交
2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718
	for (i = 0; i < serial->num_ports; ++i) {
		port = serial->port[i];
		p_priv = usb_get_serial_port_data(port);
		stop_urb(p_priv->inack_urb);
		stop_urb(p_priv->outcont_urb);
		for (j = 0; j < 2; j++) {
			stop_urb(p_priv->in_urbs[j]);
			stop_urb(p_priv->out_urbs[j]);
		}
	}

	/* Now free them */
M
Mariusz Kozlowski 已提交
2719
	usb_free_urb(s_priv->instat_urb);
2720
	usb_free_urb(s_priv->indat_urb);
M
Mariusz Kozlowski 已提交
2721
	usb_free_urb(s_priv->glocont_urb);
L
Linus Torvalds 已提交
2722 2723 2724
	for (i = 0; i < serial->num_ports; ++i) {
		port = serial->port[i];
		p_priv = usb_get_serial_port_data(port);
M
Mariusz Kozlowski 已提交
2725 2726
		usb_free_urb(p_priv->inack_urb);
		usb_free_urb(p_priv->outcont_urb);
L
Linus Torvalds 已提交
2727
		for (j = 0; j < 2; j++) {
M
Mariusz Kozlowski 已提交
2728 2729
			usb_free_urb(p_priv->in_urbs[j]);
			usb_free_urb(p_priv->out_urbs[j]);
L
Linus Torvalds 已提交
2730 2731
		}
	}
2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742
}

static void keyspan_release(struct usb_serial *serial)
{
	int				i;
	struct usb_serial_port		*port;
	struct keyspan_serial_private 	*s_priv;

	dbg("%s", __func__);

	s_priv = usb_get_serial_data(serial);
L
Linus Torvalds 已提交
2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754

	/*  dbg("Freeing serial->private."); */
	kfree(s_priv);

	/*  dbg("Freeing port->private."); */
	/* Now free per port private data */
	for (i = 0; i < serial->num_ports; i++) {
		port = serial->port[i];
		kfree(usb_get_serial_port_data(port));
	}
}

A
Alan Cox 已提交
2755 2756
MODULE_AUTHOR(DRIVER_AUTHOR);
MODULE_DESCRIPTION(DRIVER_DESC);
L
Linus Torvalds 已提交
2757 2758
MODULE_LICENSE("GPL");

2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771
MODULE_FIRMWARE("keyspan/usa28.fw");
MODULE_FIRMWARE("keyspan/usa28x.fw");
MODULE_FIRMWARE("keyspan/usa28xa.fw");
MODULE_FIRMWARE("keyspan/usa28xb.fw");
MODULE_FIRMWARE("keyspan/usa19.fw");
MODULE_FIRMWARE("keyspan/usa19qi.fw");
MODULE_FIRMWARE("keyspan/mpr.fw");
MODULE_FIRMWARE("keyspan/usa19qw.fw");
MODULE_FIRMWARE("keyspan/usa18x.fw");
MODULE_FIRMWARE("keyspan/usa19w.fw");
MODULE_FIRMWARE("keyspan/usa49w.fw");
MODULE_FIRMWARE("keyspan/usa49wlc.fw");

L
Linus Torvalds 已提交
2772 2773 2774
module_param(debug, bool, S_IRUGO | S_IWUSR);
MODULE_PARM_DESC(debug, "Debug enabled or not");