keyspan.c 65.7 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1 2
/*
  Keyspan USB to Serial Converter driver
Alan Cox's avatar
Alan Cox committed
3

Linus Torvalds's avatar
Linus Torvalds committed
4 5
  (C) Copyright (C) 2000-2001	Hugh Blemings <hugh@blemings.org>
  (C) Copyright (C) 2002	Greg Kroah-Hartman <greg@kroah.com>
Alan Cox's avatar
Alan Cox committed
6

Linus Torvalds's avatar
Linus Torvalds committed
7 8 9 10 11
  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.

12
  See http://blemings.org/hugh/keyspan.html for more information.
Alan Cox's avatar
Alan Cox committed
13

Linus Torvalds's avatar
Linus Torvalds committed
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 :)
Alan Cox's avatar
Alan Cox committed
20

Linus Torvalds's avatar
Linus Torvalds committed
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
Alan Cox's avatar
Alan Cox committed
24 25 26
  stuff.

  Tip 'o the hat to IBM (and previously Linuxcare :) for supporting
Linus Torvalds's avatar
Linus Torvalds committed
27 28 29 30 31 32 33 34 35 36 37 38 39 40
  staff in their work on open source projects.
*/


#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>
41 42
#include <linux/firmware.h>
#include <linux/ihex.h>
Alan Cox's avatar
Alan Cox committed
43
#include <linux/uaccess.h>
Linus Torvalds's avatar
Linus Torvalds committed
44
#include <linux/usb.h>
45
#include <linux/usb/serial.h>
Linus Torvalds's avatar
Linus Torvalds committed
46 47
#include "keyspan.h"

48
static bool debug;
Linus Torvalds's avatar
Linus Torvalds committed
49 50 51 52

/*
 * Version Information
 */
53
#define DRIVER_VERSION "v1.1.5"
Linus Torvalds's avatar
Linus Torvalds committed
54 55 56 57 58
#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
59
#define INDAT49W_BUFLEN	512
Linus Torvalds's avatar
Linus Torvalds committed
60 61 62 63 64 65 66 67

	/* 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];

Alan Cox's avatar
Alan Cox committed
68 69
	/* added to support 49wg, where data from all 4 ports comes in
	   on 1 EP and high-speed supported */
70 71 72
	struct urb	*indat_urb;
	char		indat_buf[INDAT49W_BUFLEN];

Linus Torvalds's avatar
Linus Torvalds committed
73 74 75
	/* XXX this one probably will need a lock */
	struct urb	*glocont_urb;
	char		glocont_buf[GLOCONT_BUFLEN];
Alan Cox's avatar
Alan Cox committed
76
	char		ctrl_buf[8];	/* for EP0 control message */
Linus Torvalds's avatar
Linus Torvalds committed
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
};

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
123
   make use of one of five message formats which are referred
Alan Cox's avatar
Alan Cox committed
124 125
   to as USA-26, USA-28, USA-49, USA-90, USA-67 by Keyspan and
   within this driver. */
Linus Torvalds's avatar
Linus Torvalds committed
126 127 128 129
#include "keyspan_usa26msg.h"
#include "keyspan_usa28msg.h"
#include "keyspan_usa49msg.h"
#include "keyspan_usa90msg.h"
130
#include "keyspan_usa67msg.h"
Alan Cox's avatar
Alan Cox committed
131

Linus Torvalds's avatar
Linus Torvalds committed
132

133
module_usb_serial_driver(serial_drivers, keyspan_ids_combined);
Linus Torvalds's avatar
Linus Torvalds committed
134

Alan Cox's avatar
Alan Cox committed
135
static void keyspan_break_ctl(struct tty_struct *tty, int break_state)
Linus Torvalds's avatar
Linus Torvalds committed
136
{
Alan Cox's avatar
Alan Cox committed
137
	struct usb_serial_port *port = tty->driver_data;
Linus Torvalds's avatar
Linus Torvalds committed
138 139 140 141 142 143 144 145 146 147 148 149 150
	struct keyspan_port_private 	*p_priv;

	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);
}


Alan Cox's avatar
Alan Cox committed
151
static void keyspan_set_termios(struct tty_struct *tty,
Alan Cox's avatar
Alan Cox committed
152
		struct usb_serial_port *port, struct ktermios *old_termios)
Linus Torvalds's avatar
Linus Torvalds committed
153 154 155 156 157 158 159 160
{
	int				baud_rate, device_port;
	struct keyspan_port_private 	*p_priv;
	const struct keyspan_device_details	*d_details;
	unsigned int 			cflag;

	p_priv = usb_get_serial_port_data(port);
	d_details = p_priv->device_details;
Alan Cox's avatar
Alan Cox committed
161
	cflag = tty->termios->c_cflag;
Linus Torvalds's avatar
Linus Torvalds committed
162 163 164 165
	device_port = port->number - port->serial->minor;

	/* Baud rate calculation takes baud rate as an integer
	   so other rates can be generated if desired. */
Alan Cox's avatar
Alan Cox committed
166
	baud_rate = tty_get_baud_rate(tty);
Alan Cox's avatar
Alan Cox committed
167
	/* If no match or invalid, don't change */
Alan Cox's avatar
Alan Cox committed
168
	if (d_details->calculate_baud_rate(baud_rate, d_details->baudclk,
Linus Torvalds's avatar
Linus Torvalds committed
169 170
				NULL, NULL, NULL, device_port) == KEYSPAN_BAUD_RATE_OK) {
		/* FIXME - more to do here to ensure rate changes cleanly */
Alan Cox's avatar
Alan Cox committed
171
		/* FIXME - calcuate exact rate from divisor ? */
Linus Torvalds's avatar
Linus Torvalds committed
172
		p_priv->baud = baud_rate;
Alan Cox's avatar
Alan Cox committed
173 174
	} else
		baud_rate = tty_termios_baud_rate(old_termios);
Linus Torvalds's avatar
Linus Torvalds committed
175

Alan Cox's avatar
Alan Cox committed
176
	tty_encode_baud_rate(tty, baud_rate, baud_rate);
Linus Torvalds's avatar
Linus Torvalds committed
177 178
	/* set CTS/RTS handshake etc. */
	p_priv->cflag = cflag;
179
	p_priv->flow_control = (cflag & CRTSCTS) ? flow_cts : flow_none;
Linus Torvalds's avatar
Linus Torvalds committed
180

Alan Cox's avatar
Alan Cox committed
181 182 183
	/* Mark/Space not supported */
	tty->termios->c_cflag &= ~CMSPAR;

Linus Torvalds's avatar
Linus Torvalds committed
184 185 186
	keyspan_send_setup(port, 0);
}

187
static int keyspan_tiocmget(struct tty_struct *tty)
Linus Torvalds's avatar
Linus Torvalds committed
188
{
Alan Cox's avatar
Alan Cox committed
189 190
	struct usb_serial_port *port = tty->driver_data;
	struct keyspan_port_private *p_priv = usb_get_serial_port_data(port);
Linus Torvalds's avatar
Linus Torvalds committed
191
	unsigned int			value;
Alan Cox's avatar
Alan Cox committed
192

Linus Torvalds's avatar
Linus Torvalds committed
193 194 195 196 197
	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) |
Alan Cox's avatar
Alan Cox committed
198
		((p_priv->ri_state) ? TIOCM_RNG : 0);
Linus Torvalds's avatar
Linus Torvalds committed
199 200 201 202

	return value;
}

203
static int keyspan_tiocmset(struct tty_struct *tty,
Linus Torvalds's avatar
Linus Torvalds committed
204 205
			    unsigned int set, unsigned int clear)
{
Alan Cox's avatar
Alan Cox committed
206 207
	struct usb_serial_port *port = tty->driver_data;
	struct keyspan_port_private *p_priv = usb_get_serial_port_data(port);
Alan Cox's avatar
Alan Cox committed
208

Linus Torvalds's avatar
Linus Torvalds committed
209 210 211 212 213 214 215 216 217 218 219 220
	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;
}

Alan Cox's avatar
Alan Cox committed
221 222 223 224
/* 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)
Linus Torvalds's avatar
Linus Torvalds committed
225 226 227 228 229 230
{
	struct keyspan_port_private 	*p_priv;
	const struct keyspan_device_details	*d_details;
	int				flip;
	int 				left, todo;
	struct urb			*this_urb;
Alan Cox's avatar
Alan Cox committed
231
	int 				err, maxDataLen, dataOffset;
Linus Torvalds's avatar
Linus Torvalds committed
232 233 234 235 236

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

	if (d_details->msg_format == msg_usa90) {
Alan Cox's avatar
Alan Cox committed
237
		maxDataLen = 64;
Linus Torvalds's avatar
Linus Torvalds committed
238 239 240 241 242
		dataOffset = 0;
	} else {
		maxDataLen = 63;
		dataOffset = 1;
	}
Alan Cox's avatar
Alan Cox committed
243

Linus Torvalds's avatar
Linus Torvalds committed
244
	dbg("%s - for port %d (%d chars), flip=%d",
245
	    __func__, port->number, count, p_priv->out_flip);
Linus Torvalds's avatar
Linus Torvalds committed
246 247 248 249 250 251 252

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

		flip = p_priv->out_flip;
Alan Cox's avatar
Alan Cox committed
253

Linus Torvalds's avatar
Linus Torvalds committed
254
		/* Check we have a valid urb/endpoint before we use it... */
Alan Cox's avatar
Alan Cox committed
255 256
		this_urb = p_priv->out_urbs[flip];
		if (this_urb == NULL) {
Linus Torvalds's avatar
Linus Torvalds committed
257
			/* no bulk out, so return 0 bytes written */
258
			dbg("%s - no output urb :(", __func__);
Linus Torvalds's avatar
Linus Torvalds committed
259 260 261
			return count;
		}

Alan Cox's avatar
Alan Cox committed
262 263
		dbg("%s - endpoint %d flip %d",
			__func__, usb_pipeendpoint(this_urb->pipe), flip);
Linus Torvalds's avatar
Linus Torvalds committed
264 265

		if (this_urb->status == -EINPROGRESS) {
Alan Cox's avatar
Alan Cox committed
266 267
			if (time_before(jiffies,
					p_priv->tx_start_time[flip] + 10 * HZ))
Linus Torvalds's avatar
Linus Torvalds committed
268 269 270 271 272
				break;
			usb_unlink_urb(this_urb);
			break;
		}

Alan Cox's avatar
Alan Cox committed
273 274
		/* First byte in buffer is "last flag" (except for usa19hx)
		   - unused so for now so set to zero */
Linus Torvalds's avatar
Linus Torvalds committed
275 276
		((char *)this_urb->transfer_buffer)[0] = 0;

Alan Cox's avatar
Alan Cox committed
277
		memcpy(this_urb->transfer_buffer + dataOffset, buf, todo);
Linus Torvalds's avatar
Linus Torvalds committed
278 279 280 281 282
		buf += todo;

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

Alan Cox's avatar
Alan Cox committed
283 284
		err = usb_submit_urb(this_urb, GFP_ATOMIC);
		if (err != 0)
Linus Torvalds's avatar
Linus Torvalds committed
285 286 287 288 289 290 291 292 293 294 295
			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;
}

296
static void	usa26_indat_callback(struct urb *urb)
Linus Torvalds's avatar
Linus Torvalds committed
297 298 299 300 301 302
{
	int			i, err;
	int			endpoint;
	struct usb_serial_port	*port;
	struct tty_struct	*tty;
	unsigned char 		*data = urb->transfer_buffer;
303
	int status = urb->status;
Linus Torvalds's avatar
Linus Torvalds committed
304 305 306

	endpoint = usb_pipeendpoint(urb->pipe);

307
	if (status) {
Linus Torvalds's avatar
Linus Torvalds committed
308
		dbg("%s - nonzero status: %x on endpoint %d.",
309
		    __func__, status, endpoint);
Linus Torvalds's avatar
Linus Torvalds committed
310 311 312
		return;
	}

313
	port =  urb->context;
Alan Cox's avatar
Alan Cox committed
314
	tty = tty_port_tty_get(&port->port);
Alan Cox's avatar
Alan Cox committed
315
	if (tty && urb->actual_length) {
Linus Torvalds's avatar
Linus Torvalds committed
316 317
		/* 0x80 bit is error flag */
		if ((data[0] & 0x80) == 0) {
Alan Cox's avatar
Alan Cox committed
318 319
			/* no errors on individual bytes, only
			   possible overrun err */
Linus Torvalds's avatar
Linus Torvalds committed
320
			if (data[0] & RXERROR_OVERRUN)
Alan Cox's avatar
Alan Cox committed
321 322 323 324
				err = TTY_OVERRUN;
			else
				err = 0;
			for (i = 1; i < urb->actual_length ; ++i)
Linus Torvalds's avatar
Linus Torvalds committed
325 326 327
				tty_insert_flip_char(tty, data[i], err);
		} else {
			/* some bytes had errors, every byte has status */
328
			dbg("%s - RX error!!!!", __func__);
Linus Torvalds's avatar
Linus Torvalds committed
329 330 331 332 333 334 335 336 337 338 339 340 341 342
			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);
	}
Alan Cox's avatar
Alan Cox committed
343
	tty_kref_put(tty);
Alan Cox's avatar
Alan Cox committed
344 345

	/* Resubmit urb so we continue receiving */
346 347 348
	err = usb_submit_urb(urb, GFP_ATOMIC);
	if (err != 0)
		dbg("%s - resubmit read urb failed. (%d)", __func__, err);
Linus Torvalds's avatar
Linus Torvalds committed
349 350
}

Alan Cox's avatar
Alan Cox committed
351
/* Outdat handling is common for all devices */
352
static void	usa2x_outdat_callback(struct urb *urb)
Linus Torvalds's avatar
Linus Torvalds committed
353 354 355 356
{
	struct usb_serial_port *port;
	struct keyspan_port_private *p_priv;

357
	port =  urb->context;
Linus Torvalds's avatar
Linus Torvalds committed
358
	p_priv = usb_get_serial_port_data(port);
Alan Cox's avatar
Alan Cox committed
359
	dbg("%s - urb %d", __func__, urb == p_priv->out_urbs[1]);
Linus Torvalds's avatar
Linus Torvalds committed
360

361
	usb_serial_port_softint(port);
Linus Torvalds's avatar
Linus Torvalds committed
362 363
}

364
static void	usa26_inack_callback(struct urb *urb)
Linus Torvalds's avatar
Linus Torvalds committed
365 366 367
{
}

368
static void	usa26_outcont_callback(struct urb *urb)
Linus Torvalds's avatar
Linus Torvalds committed
369 370 371 372
{
	struct usb_serial_port *port;
	struct keyspan_port_private *p_priv;

373
	port =  urb->context;
Linus Torvalds's avatar
Linus Torvalds committed
374 375 376
	p_priv = usb_get_serial_port_data(port);

	if (p_priv->resend_cont) {
Alan Cox's avatar
Alan Cox committed
377 378 379
		dbg("%s - sending setup", __func__);
		keyspan_usa26_send_setup(port->serial, port,
						p_priv->resend_cont - 1);
Linus Torvalds's avatar
Linus Torvalds committed
380 381 382
	}
}

383
static void	usa26_instat_callback(struct urb *urb)
Linus Torvalds's avatar
Linus Torvalds committed
384 385 386 387 388 389
{
	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;
Alan Cox's avatar
Alan Cox committed
390
	struct tty_struct			*tty;
Linus Torvalds's avatar
Linus Torvalds committed
391
	int old_dcd_state, err;
392
	int status = urb->status;
Linus Torvalds's avatar
Linus Torvalds committed
393

394
	serial =  urb->context;
Linus Torvalds's avatar
Linus Torvalds committed
395

396
	if (status) {
397
		dbg("%s - nonzero status: %x", __func__, status);
Linus Torvalds's avatar
Linus Torvalds committed
398 399 400
		return;
	}
	if (urb->actual_length != 9) {
401
		dbg("%s - %d byte report??", __func__, urb->actual_length);
Linus Torvalds's avatar
Linus Torvalds committed
402 403 404 405 406 407 408
		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",
409
	    __func__, msg->port, msg->hskia_cts, msg->gpia_dcd, msg->dsr, msg->ri, msg->_txOff,
Linus Torvalds's avatar
Linus Torvalds committed
410 411 412 413 414 415
	    msg->_txXoff, msg->rxEnabled, msg->controlResponse);
#endif

	/* Now do something useful with the data */


Alan Cox's avatar
Alan Cox committed
416
	/* Check port number from message and retrieve private data */
Linus Torvalds's avatar
Linus Torvalds committed
417
	if (msg->port >= serial->num_ports) {
Alan Cox's avatar
Alan Cox committed
418
		dbg("%s - Unexpected port number %d", __func__, msg->port);
Linus Torvalds's avatar
Linus Torvalds committed
419 420 421 422
		goto exit;
	}
	port = serial->port[msg->port];
	p_priv = usb_get_serial_port_data(port);
Alan Cox's avatar
Alan Cox committed
423

Linus Torvalds's avatar
Linus Torvalds committed
424 425 426 427 428 429 430
	/* 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);

Alan Cox's avatar
Alan Cox committed
431 432 433 434 435
	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);
Linus Torvalds's avatar
Linus Torvalds committed
436
	}
Alan Cox's avatar
Alan Cox committed
437

Linus Torvalds's avatar
Linus Torvalds committed
438
	/* Resubmit urb so we continue receiving */
Alan Cox's avatar
Alan Cox committed
439 440
	err = usb_submit_urb(urb, GFP_ATOMIC);
	if (err != 0)
441
		dbg("%s - resubmit read urb failed. (%d)", __func__, err);
Linus Torvalds's avatar
Linus Torvalds committed
442 443 444
exit: ;
}

445
static void	usa26_glocont_callback(struct urb *urb)
Linus Torvalds's avatar
Linus Torvalds committed
446 447 448 449
{
}


450
static void usa28_indat_callback(struct urb *urb)
Linus Torvalds's avatar
Linus Torvalds committed
451
{
452
	int                     err;
Linus Torvalds's avatar
Linus Torvalds committed
453 454 455 456
	struct usb_serial_port  *port;
	struct tty_struct       *tty;
	unsigned char           *data;
	struct keyspan_port_private             *p_priv;
457
	int status = urb->status;
Linus Torvalds's avatar
Linus Torvalds committed
458

459
	port =  urb->context;
Linus Torvalds's avatar
Linus Torvalds committed
460 461 462 463 464 465 466
	p_priv = usb_get_serial_port_data(port);
	data = urb->transfer_buffer;

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

	do {
467
		if (status) {
Linus Torvalds's avatar
Linus Torvalds committed
468
			dbg("%s - nonzero status: %x on endpoint %d.",
469
			    __func__, status, usb_pipeendpoint(urb->pipe));
Linus Torvalds's avatar
Linus Torvalds committed
470 471 472
			return;
		}

473
		port =  urb->context;
Linus Torvalds's avatar
Linus Torvalds committed
474 475 476
		p_priv = usb_get_serial_port_data(port);
		data = urb->transfer_buffer;

477
		tty = tty_port_tty_get(&port->port);
Alan Cox's avatar
Alan Cox committed
478
		if (tty && urb->actual_length) {
479
			tty_insert_flip_string(tty, data, urb->actual_length);
Linus Torvalds's avatar
Linus Torvalds committed
480 481
			tty_flip_buffer_push(tty);
		}
Alan Cox's avatar
Alan Cox committed
482
		tty_kref_put(tty);
Linus Torvalds's avatar
Linus Torvalds committed
483 484

		/* Resubmit urb so we continue receiving */
485 486 487 488
		err = usb_submit_urb(urb, GFP_ATOMIC);
		if (err != 0)
			dbg("%s - resubmit read urb failed. (%d)",
							__func__, err);
Linus Torvalds's avatar
Linus Torvalds committed
489 490 491 492 493 494
		p_priv->in_flip ^= 1;

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

495
static void	usa28_inack_callback(struct urb *urb)
Linus Torvalds's avatar
Linus Torvalds committed
496 497 498
{
}

499
static void	usa28_outcont_callback(struct urb *urb)
Linus Torvalds's avatar
Linus Torvalds committed
500 501 502 503
{
	struct usb_serial_port *port;
	struct keyspan_port_private *p_priv;

504
	port =  urb->context;
Linus Torvalds's avatar
Linus Torvalds committed
505 506 507
	p_priv = usb_get_serial_port_data(port);

	if (p_priv->resend_cont) {
Alan Cox's avatar
Alan Cox committed
508 509 510
		dbg("%s - sending setup", __func__);
		keyspan_usa28_send_setup(port->serial, port,
						p_priv->resend_cont - 1);
Linus Torvalds's avatar
Linus Torvalds committed
511 512 513
	}
}

514
static void	usa28_instat_callback(struct urb *urb)
Linus Torvalds's avatar
Linus Torvalds committed
515 516 517 518 519 520 521
{
	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;
Alan Cox's avatar
Alan Cox committed
522
	struct tty_struct			*tty;
Linus Torvalds's avatar
Linus Torvalds committed
523
	int old_dcd_state;
524
	int status = urb->status;
Linus Torvalds's avatar
Linus Torvalds committed
525

526
	serial =  urb->context;
Linus Torvalds's avatar
Linus Torvalds committed
527

528
	if (status) {
529
		dbg("%s - nonzero status: %x", __func__, status);
Linus Torvalds's avatar
Linus Torvalds committed
530 531 532 533
		return;
	}

	if (urb->actual_length != sizeof(struct keyspan_usa28_portStatusMessage)) {
534
		dbg("%s - bad length %d", __func__, urb->actual_length);
Linus Torvalds's avatar
Linus Torvalds committed
535 536 537
		goto exit;
	}

538
	/*dbg("%s %x %x %x %x %x %x %x %x %x %x %x %x", __func__
Linus Torvalds's avatar
Linus Torvalds committed
539 540 541
	    data[0], data[1], data[2], data[3], data[4], data[5],
	    data[6], data[7], data[8], data[9], data[10], data[11]);*/

Alan Cox's avatar
Alan Cox committed
542 543
	/* Now do something useful with the data */
	msg = (struct keyspan_usa28_portStatusMessage *)data;
Linus Torvalds's avatar
Linus Torvalds committed
544

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

Linus Torvalds's avatar
Linus Torvalds committed
553 554 555 556 557 558 559
	/* 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);

Alan Cox's avatar
Alan Cox committed
560 561 562 563 564
	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);
Linus Torvalds's avatar
Linus Torvalds committed
565 566 567
	}

		/* Resubmit urb so we continue receiving */
Alan Cox's avatar
Alan Cox committed
568 569
	err = usb_submit_urb(urb, GFP_ATOMIC);
	if (err != 0)
570
		dbg("%s - resubmit read urb failed. (%d)", __func__, err);
Linus Torvalds's avatar
Linus Torvalds committed
571 572 573
exit: ;
}

574
static void	usa28_glocont_callback(struct urb *urb)
Linus Torvalds's avatar
Linus Torvalds committed
575 576 577 578
{
}


579
static void	usa49_glocont_callback(struct urb *urb)
Linus Torvalds's avatar
Linus Torvalds committed
580 581 582 583 584 585
{
	struct usb_serial *serial;
	struct usb_serial_port *port;
	struct keyspan_port_private *p_priv;
	int i;

586
	serial =  urb->context;
Linus Torvalds's avatar
Linus Torvalds committed
587 588 589 590 591
	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) {
Alan Cox's avatar
Alan Cox committed
592 593 594
			dbg("%s - sending setup", __func__);
			keyspan_usa49_send_setup(serial, port,
						p_priv->resend_cont - 1);
Linus Torvalds's avatar
Linus Torvalds committed
595 596 597 598 599 600 601
			break;
		}
	}
}

	/* This is actually called glostat in the Keyspan
	   doco */
602
static void	usa49_instat_callback(struct urb *urb)
Linus Torvalds's avatar
Linus Torvalds committed
603 604 605 606 607 608 609 610
{
	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;
611
	int status = urb->status;
Linus Torvalds's avatar
Linus Torvalds committed
612

613
	serial =  urb->context;
Linus Torvalds's avatar
Linus Torvalds committed
614

615
	if (status) {
616
		dbg("%s - nonzero status: %x", __func__, status);
Linus Torvalds's avatar
Linus Torvalds committed
617 618 619
		return;
	}

Alan Cox's avatar
Alan Cox committed
620 621
	if (urb->actual_length !=
			sizeof(struct keyspan_usa49_portStatusMessage)) {
622
		dbg("%s - bad length %d", __func__, urb->actual_length);
Linus Torvalds's avatar
Linus Torvalds committed
623 624 625
		goto exit;
	}

626
	/*dbg(" %x %x %x %x %x %x %x %x %x %x %x", __func__,
Linus Torvalds's avatar
Linus Torvalds committed
627 628
	    data[0], data[1], data[2], data[3], data[4], data[5],
	    data[6], data[7], data[8], data[9], data[10]);*/
Alan Cox's avatar
Alan Cox committed
629 630

	/* Now do something useful with the data */
Linus Torvalds's avatar
Linus Torvalds committed
631 632
	msg = (struct keyspan_usa49_portStatusMessage *)data;

Alan Cox's avatar
Alan Cox committed
633
	/* Check port number from message and retrieve private data */
Linus Torvalds's avatar
Linus Torvalds committed
634
	if (msg->portNumber >= serial->num_ports) {
Alan Cox's avatar
Alan Cox committed
635 636
		dbg("%s - Unexpected port number %d",
					__func__, msg->portNumber);
Linus Torvalds's avatar
Linus Torvalds committed
637 638 639 640
		goto exit;
	}
	port = serial->port[msg->portNumber];
	p_priv = usb_get_serial_port_data(port);
Alan Cox's avatar
Alan Cox committed
641

Linus Torvalds's avatar
Linus Torvalds committed
642 643 644 645 646 647 648
	/* 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);

Alan Cox's avatar
Alan Cox committed
649 650 651 652 653
	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);
Linus Torvalds's avatar
Linus Torvalds committed
654 655
	}

Alan Cox's avatar
Alan Cox committed
656 657 658
	/* Resubmit urb so we continue receiving */
	err = usb_submit_urb(urb, GFP_ATOMIC);
	if (err != 0)
659
		dbg("%s - resubmit read urb failed. (%d)", __func__, err);
Linus Torvalds's avatar
Linus Torvalds committed
660 661 662
exit:	;
}

663
static void	usa49_inack_callback(struct urb *urb)
Linus Torvalds's avatar
Linus Torvalds committed
664 665 666
{
}

667
static void	usa49_indat_callback(struct urb *urb)
Linus Torvalds's avatar
Linus Torvalds committed
668 669 670 671 672 673
{
	int			i, err;
	int			endpoint;
	struct usb_serial_port	*port;
	struct tty_struct	*tty;
	unsigned char 		*data = urb->transfer_buffer;
674
	int status = urb->status;
Linus Torvalds's avatar
Linus Torvalds committed
675 676 677

	endpoint = usb_pipeendpoint(urb->pipe);

678
	if (status) {
679
		dbg("%s - nonzero status: %x on endpoint %d.", __func__,
680
		    status, endpoint);
Linus Torvalds's avatar
Linus Torvalds committed
681 682 683
		return;
	}

684
	port =  urb->context;
Alan Cox's avatar
Alan Cox committed
685
	tty = tty_port_tty_get(&port->port);
Alan Cox's avatar
Alan Cox committed
686
	if (tty && urb->actual_length) {
Linus Torvalds's avatar
Linus Torvalds committed
687 688 689
		/* 0x80 bit is error flag */
		if ((data[0] & 0x80) == 0) {
			/* no error on any byte */
690 691
			tty_insert_flip_string(tty, data + 1,
						urb->actual_length - 1);
Linus Torvalds's avatar
Linus Torvalds committed
692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707
		} 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);
	}
Alan Cox's avatar
Alan Cox committed
708
	tty_kref_put(tty);
Alan Cox's avatar
Alan Cox committed
709 710

	/* Resubmit urb so we continue receiving */
711 712 713
	err = usb_submit_urb(urb, GFP_ATOMIC);
	if (err != 0)
		dbg("%s - resubmit read urb failed. (%d)", __func__, err);
Linus Torvalds's avatar
Linus Torvalds committed
714 715
}

716 717 718 719 720 721 722
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;
723
	int status = urb->status;
724 725 726

	serial = urb->context;

727
	if (status) {
728
		dbg("%s - nonzero status: %x", __func__, status);
729 730 731 732 733 734 735 736 737 738 739 740
		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) {
Alan Cox's avatar
Alan Cox committed
741
				dbg("%s - Unexpected port number %d",
742
					__func__, data[i]);
743 744 745
				return;
			}
			port = serial->port[data[i++]];
Alan Cox's avatar
Alan Cox committed
746
			tty = tty_port_tty_get(&port->port);
747 748 749 750 751 752 753
			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)
754
					tty_insert_flip_char(tty, data[i++], 0);
755 756 757 758 759 760 761 762 763 764 765 766 767
			} 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) */
768
					tty_insert_flip_char(tty,
769 770 771 772
							data[i+1], flag);
					i += 2;
				}
			}
773
			tty_flip_buffer_push(tty);
Alan Cox's avatar
Alan Cox committed
774
			tty_kref_put(tty);
775 776 777 778 779 780
		}
	}

	/* Resubmit urb so we continue receiving */
	err = usb_submit_urb(urb, GFP_ATOMIC);
	if (err != 0)
781
		dbg("%s - resubmit read urb failed. (%d)", __func__, err);
782 783
}

Linus Torvalds's avatar
Linus Torvalds committed
784
/* not used, usa-49 doesn't have per-port control endpoints */
785
static void usa49_outcont_callback(struct urb *urb)
Linus Torvalds's avatar
Linus Torvalds committed
786 787 788
{
}

789
static void usa90_indat_callback(struct urb *urb)
Linus Torvalds's avatar
Linus Torvalds committed
790 791 792 793 794 795 796
{
	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;
797
	int status = urb->status;
Linus Torvalds's avatar
Linus Torvalds committed
798 799 800

	endpoint = usb_pipeendpoint(urb->pipe);

801
	if (status) {
Linus Torvalds's avatar
Linus Torvalds committed
802
		dbg("%s - nonzero status: %x on endpoint %d.",
803
		    __func__, status, endpoint);
Linus Torvalds's avatar
Linus Torvalds committed
804 805 806
		return;
	}

807
	port =  urb->context;
Linus Torvalds's avatar
Linus Torvalds committed
808 809 810
	p_priv = usb_get_serial_port_data(port);

	if (urb->actual_length) {
Alan Cox's avatar
Alan Cox committed
811
		tty = tty_port_tty_get(&port->port);
Linus Torvalds's avatar
Linus Torvalds committed
812
		/* if current mode is DMA, looks like usa28 format
Alan Cox's avatar
Alan Cox committed
813
		   otherwise looks like usa26 data format */
Linus Torvalds's avatar
Linus Torvalds committed
814

815 816 817
		if (p_priv->baud > 57600)
			tty_insert_flip_string(tty, data, urb->actual_length);
		else {
Linus Torvalds's avatar
Linus Torvalds committed
818 819
			/* 0x80 bit is error flag */
			if ((data[0] & 0x80) == 0) {
Alan Cox's avatar
Alan Cox committed
820 821
				/* no errors on individual bytes, only
				   possible overrun err*/
Linus Torvalds's avatar
Linus Torvalds committed
822
				if (data[0] & RXERROR_OVERRUN)
Alan Cox's avatar
Alan Cox committed
823 824 825 826 827 828 829
					err = TTY_OVERRUN;
				else
					err = 0;
				for (i = 1; i < urb->actual_length ; ++i)
					tty_insert_flip_char(tty, data[i],
									err);
			}  else {
Linus Torvalds's avatar
Linus Torvalds committed
830
			/* some bytes had errors, every byte has status */
831
				dbg("%s - RX error!!!!", __func__);
Linus Torvalds's avatar
Linus Torvalds committed
832 833 834 835 836 837 838 839 840
				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) */
Alan Cox's avatar
Alan Cox committed
841 842
					tty_insert_flip_char(tty, data[i+1],
									flag);
Linus Torvalds's avatar
Linus Torvalds committed
843 844 845 846
				}
			}
		}
		tty_flip_buffer_push(tty);
Alan Cox's avatar
Alan Cox committed
847
		tty_kref_put(tty);
Linus Torvalds's avatar
Linus Torvalds committed
848
	}
Alan Cox's avatar
Alan Cox committed
849

Linus Torvalds's avatar
Linus Torvalds committed
850
	/* Resubmit urb so we continue receiving */
851 852 853
	err = usb_submit_urb(urb, GFP_ATOMIC);
	if (err != 0)
		dbg("%s - resubmit read urb failed. (%d)", __func__, err);
Linus Torvalds's avatar
Linus Torvalds committed
854 855 856
}


857
static void	usa90_instat_callback(struct urb *urb)
Linus Torvalds's avatar
Linus Torvalds committed
858 859 860 861 862 863
{
	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;
Alan Cox's avatar
Alan Cox committed
864
	struct tty_struct			*tty;
Linus Torvalds's avatar
Linus Torvalds committed
865
	int old_dcd_state, err;
866
	int status = urb->status;
Linus Torvalds's avatar
Linus Torvalds committed
867

868
	serial =  urb->context;
Linus Torvalds's avatar
Linus Torvalds committed
869

870
	if (status) {
871
		dbg("%s - nonzero status: %x", __func__, status);
Linus Torvalds's avatar
Linus Torvalds committed
872 873 874
		return;
	}
	if (urb->actual_length < 14) {
875
		dbg("%s - %d byte report??", __func__, urb->actual_length);
Linus Torvalds's avatar
Linus Torvalds committed
876 877 878 879 880 881 882 883 884
		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);
Alan Cox's avatar
Alan Cox committed
885

Linus Torvalds's avatar
Linus Torvalds committed
886 887 888 889 890 891 892
	/* 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);

Alan Cox's avatar
Alan Cox committed
893 894 895 896 897
	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);
Linus Torvalds's avatar
Linus Torvalds committed
898
	}
Alan Cox's avatar
Alan Cox committed
899

Linus Torvalds's avatar
Linus Torvalds committed
900
	/* Resubmit urb so we continue receiving */
Alan Cox's avatar
Alan Cox committed
901 902
	err = usb_submit_urb(urb, GFP_ATOMIC);
	if (err != 0)
903
		dbg("%s - resubmit read urb failed. (%d)", __func__, err);
Linus Torvalds's avatar
Linus Torvalds committed
904 905 906 907
exit:
	;
}

908
static void	usa90_outcont_callback(struct urb *urb)
Linus Torvalds's avatar
Linus Torvalds committed
909 910 911 912
{
	struct usb_serial_port *port;
	struct keyspan_port_private *p_priv;

913
	port =  urb->context;
Linus Torvalds's avatar
Linus Torvalds committed
914 915 916
	p_priv = usb_get_serial_port_data(port);

	if (p_priv->resend_cont) {
Alan Cox's avatar
Alan Cox committed
917 918 919
		dbg("%s - sending setup", __func__);
		keyspan_usa90_send_setup(port->serial, port,
						p_priv->resend_cont - 1);
Linus Torvalds's avatar
Linus Torvalds committed
920 921 922
	}
}

923 924 925 926 927 928 929 930 931 932
/* 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;
933
	int status = urb->status;
934 935 936

	serial = urb->context;

937
	if (status) {
938
		dbg("%s - nonzero status: %x", __func__, status);
939 940 941
		return;
	}

Alan Cox's avatar
Alan Cox committed
942 943
	if (urb->actual_length !=
			sizeof(struct keyspan_usa67_portStatusMessage)) {
944
		dbg("%s - bad length %d", __func__, urb->actual_length);
945 946 947 948 949 950 951 952 953
		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) {
Alan Cox's avatar
Alan Cox committed
954
		dbg("%s - Unexpected port number %d", __func__, msg->port);
955 956 957 958 959 960 961 962 963 964 965
		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);

Alan Cox's avatar
Alan Cox committed
966 967 968 969 970
	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);
971 972 973 974 975
	}

	/* Resubmit urb so we continue receiving */
	err = usb_submit_urb(urb, GFP_ATOMIC);
	if (err != 0)
976
		dbg("%s - resubmit read urb failed. (%d)", __func__, err);
977 978 979 980 981 982 983 984 985 986 987 988 989 990 991
}

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;

	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) {
Alan Cox's avatar
Alan Cox committed
992
			dbg("%s - sending setup", __func__);
993 994 995 996 997 998 999
			keyspan_usa67_send_setup(serial, port,
						p_priv->resend_cont - 1);
			break;
		}
	}
}

Alan Cox's avatar
Alan Cox committed
1000
static int keyspan_write_room(struct tty_struct *tty)
Linus Torvalds's avatar
Linus Torvalds committed
1001
{
Alan Cox's avatar
Alan Cox committed
1002
	struct usb_serial_port *port = tty->driver_data;
Linus Torvalds's avatar
Linus Torvalds committed
1003 1004 1005 1006 1007 1008 1009 1010 1011
	struct keyspan_port_private	*p_priv;
	const struct keyspan_device_details	*d_details;
	int				flip;
	int				data_len;
	struct urb			*this_urb;

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

1012
	/* FIXME: locking */
Linus Torvalds's avatar
Linus Torvalds committed
1013
	if (d_details->msg_format == msg_usa90)
Alan Cox's avatar
Alan Cox committed
1014
		data_len = 64;
Linus Torvalds's avatar
Linus Torvalds committed
1015 1016 1017 1018 1019 1020
	else
		data_len = 63;

	flip = p_priv->out_flip;

	/* Check both endpoints to see if any are available. */
Alan Cox's avatar
Alan Cox committed
1021 1022
	this_urb = p_priv->out_urbs[flip];
	if (this_urb != NULL) {
Linus Torvalds's avatar
Linus Torvalds committed
1023
		if (this_urb->status != -EINPROGRESS)
Alan Cox's avatar
Alan Cox committed
1024 1025 1026 1027
			return data_len;
		flip = (flip + 1) & d_details->outdat_endp_flip;
		this_urb = p_priv->out_urbs[flip];
		if (this_urb != NULL) {
Linus Torvalds's avatar
Linus Torvalds committed
1028
			if (this_urb->status != -EINPROGRESS)
Alan Cox's avatar
Alan Cox committed
1029 1030
				return data_len;
		}
Linus Torvalds's avatar
Linus Torvalds committed
1031
	}
1032
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
1033 1034 1035
}


1036
static int keyspan_open(struct tty_struct *tty, struct usb_serial_port *port)
Linus Torvalds's avatar
Linus Torvalds committed
1037
{
1038
	struct keyspan_port_private 	*p_priv;
Linus Torvalds's avatar
Linus Torvalds committed
1039 1040
	const struct keyspan_device_details	*d_details;
	int				i, err;
1041
	int				baud_rate, device_port;
Linus Torvalds's avatar
Linus Torvalds committed
1042
	struct urb			*urb;
Alan Cox's avatar
Alan Cox committed
1043
	unsigned int			cflag = 0;
Linus Torvalds's avatar
Linus Torvalds committed
1044 1045 1046

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

Linus Torvalds's avatar
Linus Torvalds committed
1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061
	/* 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++) {
Alan Cox's avatar
Alan Cox committed
1062 1063
		urb = p_priv->in_urbs[i];
		if (urb == NULL)
Linus Torvalds's avatar
Linus Torvalds committed
1064 1065
			continue;

Alan Cox's avatar
Alan Cox committed
1066 1067
		/* make sure endpoint data toggle is synchronized
		   with the device */
Linus Torvalds's avatar
Linus Torvalds committed
1068
		usb_clear_halt(urb->dev, urb->pipe);
Alan Cox's avatar
Alan Cox committed
1069 1070 1071 1072
		err = usb_submit_urb(urb, GFP_KERNEL);
		if (err != 0)
			dbg("%s - submit urb %d failed (%d)",
							__func__, i, err);
Linus Torvalds's avatar
Linus Torvalds committed
1073 1074 1075 1076
	}

	/* Reset low level data toggle on out endpoints */
	for (i = 0; i < 2; i++) {
Alan Cox's avatar
Alan Cox committed
1077 1078
		urb = p_priv->out_urbs[i];
		if (urb == NULL)
Linus Torvalds's avatar
Linus Torvalds committed
1079
			continue;
Alan Cox's avatar
Alan Cox committed
1080 1081
		/* usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe),
						usb_pipeout(urb->pipe), 0); */
Linus Torvalds's avatar
Linus Torvalds committed
1082 1083
	}

1084 1085 1086 1087
	/* 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;
Alan Cox's avatar
Alan Cox committed
1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098
	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;
		}
1099 1100 1101
	}
	/* set CTS/RTS handshake etc. */
	p_priv->cflag = cflag;
1102
	p_priv->flow_control = (cflag & CRTSCTS) ? flow_cts : flow_none;
1103 1104

	keyspan_send_setup(port, 1);
Alan Cox's avatar
Alan Cox committed
1105 1106
	/* mdelay(100); */
	/* keyspan_set_termios(port, NULL); */
1107

1108
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
1109 1110 1111 1112
}

static inline void stop_urb(struct urb *urb)
{
1113
	if (urb && urb->status == -EINPROGRESS)
Linus Torvalds's avatar
Linus Torvalds committed
1114 1115 1116
		usb_kill_urb(urb);
}

1117 1118 1119 1120 1121 1122 1123 1124 1125 1126
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)
Linus Torvalds's avatar
Linus Torvalds committed
1127 1128 1129 1130 1131 1132
{
	int			i;
	struct usb_serial	*serial = port->serial;
	struct keyspan_port_private 	*p_priv;

	p_priv = usb_get_serial_port_data(port);
Alan Cox's avatar
Alan Cox committed
1133

Linus Torvalds's avatar
Linus Torvalds committed
1134 1135
	p_priv->rts_state = 0;
	p_priv->dtr_state = 0;
Alan Cox's avatar
Alan Cox committed
1136

Linus Torvalds's avatar
Linus Torvalds committed
1137 1138 1139 1140
	if (serial->dev) {
		keyspan_send_setup(port, 2);
		/* pilot-xfer seems to work best with this delay */
		mdelay(100);
Alan Cox's avatar
Alan Cox committed
1141
		/* keyspan_set_termios(port, NULL); */
Linus Torvalds's avatar
Linus Torvalds committed
1142 1143 1144
	}

	/*while (p_priv->outcont_urb->status == -EINPROGRESS) {
1145
		dbg("%s - urb in progress", __func__);
Linus Torvalds's avatar
Linus Torvalds committed
1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161
	}*/

	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]);
		}
	}
}

Alan Cox's avatar
Alan Cox committed
1162 1163
/* download the firmware to a pre-renumeration device */
static int keyspan_fake_startup(struct usb_serial *serial)
Linus Torvalds's avatar
Linus Torvalds committed
1164 1165
{
	int 				response;
1166
	const struct ihex_binrec 	*record;
Linus Torvalds's avatar
Linus Torvalds committed
1167
	char				*fw_name;
1168
	const struct firmware		*fw;
Linus Torvalds's avatar
Linus Torvalds committed
1169 1170 1171 1172

	dbg("Keyspan startup version %04x product %04x",
	    le16_to_cpu(serial->dev->descriptor.bcdDevice),
	    le16_to_cpu(serial->dev->descriptor.idProduct));
Alan Cox's avatar
Alan Cox committed
1173 1174 1175

	if ((le16_to_cpu(serial->dev->descriptor.bcdDevice) & 0x8000)
								!= 0x8000) {
Linus Torvalds's avatar
Linus Torvalds committed
1176
		dbg("Firmware already loaded.  Quitting.");
Alan Cox's avatar
Alan Cox committed
1177
		return 1;
Linus Torvalds's avatar
Linus Torvalds committed
1178 1179 1180 1181 1182
	}

		/* Select firmware image on the basis of idProduct */
	switch (le16_to_cpu(serial->dev->descriptor.idProduct)) {
	case keyspan_usa28_pre_product_id:
1183
		fw_name = "keyspan/usa28.fw";
Linus Torvalds's avatar
Linus Torvalds committed
1184 1185 1186
		break;

	case keyspan_usa28x_pre_product_id:
1187
		fw_name = "keyspan/usa28x.fw";
Linus Torvalds's avatar
Linus Torvalds committed
1188 1189 1190
		break;

	case keyspan_usa28xa_pre_product_id:
1191
		fw_name = "keyspan/usa28xa.fw";
Linus Torvalds's avatar
Linus Torvalds committed
1192 1193 1194
		break;

	case keyspan_usa28xb_pre_product_id:
1195
		fw_name = "keyspan/usa28xb.fw";
Linus Torvalds's avatar
Linus Torvalds committed
1196 1197 1198
		break;

	case keyspan_usa19_pre_product_id:
1199
		fw_name = "keyspan/usa19.fw";
Linus Torvalds's avatar
Linus Torvalds committed
1200
		break;
Alan Cox's avatar
Alan Cox committed
1201

Linus Torvalds's avatar
Linus Torvalds committed
1202
	case keyspan_usa19qi_pre_product_id:
1203
		fw_name = "keyspan/usa19qi.fw";
Linus Torvalds's avatar
Linus Torvalds committed
1204
		break;
Alan Cox's avatar
Alan Cox committed
1205

Linus Torvalds's avatar
Linus Torvalds committed
1206
	case keyspan_mpr_pre_product_id:
1207
		fw_name = "keyspan/mpr.fw";
Linus Torvalds's avatar
Linus Torvalds committed
1208 1209 1210
		break;

	case keyspan_usa19qw_pre_product_id:
1211
		fw_name = "keyspan/usa19qw.fw";
Linus Torvalds's avatar
Linus Torvalds committed
1212
		break;
Alan Cox's avatar
Alan Cox committed
1213

Linus Torvalds's avatar
Linus Torvalds committed
1214
	case keyspan_usa18x_pre_product_id:
1215
		fw_name = "keyspan/usa18x.fw";
Linus Torvalds's avatar
Linus Torvalds committed
1216
		break;
Alan Cox's avatar
Alan Cox committed
1217

Linus Torvalds's avatar
Linus Torvalds committed
1218
	case keyspan_usa19w_pre_product_id:
1219
		fw_name = "keyspan/usa19w.fw";
Linus Torvalds's avatar
Linus Torvalds committed
1220
		break;
Alan Cox's avatar
Alan Cox committed
1221

Linus Torvalds's avatar
Linus Torvalds committed
1222
	case keyspan_usa49w_pre_product_id:
1223
		fw_name = "keyspan/usa49w.fw";
Linus Torvalds's avatar
Linus Torvalds committed
1224 1225 1226
		break;

	case keyspan_usa49wlc_pre_product_id:
1227
		fw_name = "keyspan/usa49wlc.fw";
Linus Torvalds's avatar
Linus Torvalds committed
1228 1229 1230
		break;

	default:
1231 1232 1233
		dev_err(&serial->dev->dev, "Unknown product ID (%04x)\n",
			le16_to_cpu(serial->dev->descriptor.idProduct));
		return 1;
Linus Torvalds's avatar
Linus Torvalds committed
1234 1235
	}

1236
	if (request_ihex_firmware(&fw, fw_name, &serial->dev->dev)) {
Linus Torvalds's avatar
Linus Torvalds committed
1237 1238 1239 1240 1241 1242 1243 1244 1245
		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);

1246 1247 1248 1249
	record = (const struct ihex_binrec *)fw->data;

	while (record) {
		response = ezusb_writememory(serial, be32_to_cpu(record->addr),
Linus Torvalds's avatar
Linus Torvalds committed
1250
					     (unsigned char *)record->data,
1251
					     be16_to_cpu(record->len), 0xa0);
Linus Torvalds's avatar
Linus Torvalds committed
1252
		if (response < 0) {
Alan Cox's avatar
Alan Cox committed
1253
			dev_err(&serial->dev->dev, "ezusb_writememory failed for Keyspan firmware (%d %04X %p %d)\n",
1254 1255
				response, be32_to_cpu(record->addr),
				record->data, be16_to_cpu(record->len));
Linus Torvalds's avatar
Linus Torvalds committed
1256 1257
			break;
		}
1258
		record = ihex_next_binrec(record);
Linus Torvalds's avatar
Linus Torvalds committed
1259
	}
1260
	release_firmware(fw);
Linus Torvalds's avatar
Linus Torvalds committed
1261 1262 1263 1264 1265
		/* 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. */
Alan Cox's avatar
Alan Cox committed
1266
	return 1;
Linus Torvalds's avatar
Linus Torvalds committed
1267 1268 1269
}

/* Helper functions used by keyspan_setup_urbs */
1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287
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;
}

Alan Cox's avatar
Alan Cox committed
1288
static struct urb *keyspan_setup_urb(struct usb_serial *serial, int endpoint,
Linus Torvalds's avatar
Linus Torvalds committed
1289
				      int dir, void *ctx, char *buf, int len,
1290
				      void (*callback)(struct urb *))
Linus Torvalds's avatar
Linus Torvalds committed
1291 1292
{
	struct urb *urb;
1293 1294
	struct usb_endpoint_descriptor const *ep_desc;
	char const *ep_type_name;
Linus Torvalds's avatar
Linus Torvalds committed
1295 1296 1297 1298

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

Alan Cox's avatar
Alan Cox committed
1299
	dbg("%s - alloc for endpoint %d.", __func__, endpoint);
Linus Torvalds's avatar
Linus Torvalds committed
1300 1301
	urb = usb_alloc_urb(0, GFP_KERNEL);		/* No ISO */
	if (urb == NULL) {
Alan Cox's avatar
Alan Cox committed
1302
		dbg("%s - alloc for endpoint %d failed.", __func__, endpoint);
Linus Torvalds's avatar
Linus Torvalds committed
1303 1304 1305
		return NULL;
	}

1306 1307 1308 1309 1310
	if (endpoint == 0) {
		/* control EP filled in when used */
		return urb;
	}

1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329
	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",
1330
			 usb_endpoint_type(ep_desc));
1331 1332 1333
		usb_free_urb(urb);
		return NULL;
	}
Linus Torvalds's avatar
Linus Torvalds committed
1334

1335 1336
	dbg("%s - using urb %p for %s endpoint %x",
	    __func__, urb, ep_type_name, endpoint);
Linus Torvalds's avatar
Linus Torvalds committed
1337 1338 1339 1340
	return urb;
}

static struct callbacks {
1341 1342 1343 1344 1345 1346
	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 *);
Linus Torvalds's avatar
Linus Torvalds committed
1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374
} 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,
Alan Cox's avatar
Alan Cox committed
1375
		.glocont_callback =	usa28_glocont_callback,
Linus Torvalds's avatar
Linus Torvalds committed
1376 1377 1378 1379
		.indat_callback =	usa90_indat_callback,
		.outdat_callback =	usa2x_outdat_callback,
		.inack_callback =	usa28_inack_callback,
		.outcont_callback =	usa90_outcont_callback,
1380 1381 1382 1383 1384 1385 1386 1387
	}, {
		/* 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,
Linus Torvalds's avatar
Linus Torvalds committed
1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405
	}
};

	/* 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;

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

Alan Cox's avatar
Alan Cox committed
1406
	/* Setup values for the various callback routines */
Linus Torvalds's avatar
Linus Torvalds committed
1407 1408
	cback = &keyspan_callbacks[d_details->msg_format];

Alan Cox's avatar
Alan Cox committed
1409 1410
	/* Allocate and set up urbs for each one that is in use,
	   starting with instat endpoints */
Linus Torvalds's avatar
Linus Torvalds committed
1411 1412 1413 1414 1415
	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);

1416 1417 1418 1419 1420
	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);

Linus Torvalds's avatar
Linus Torvalds committed
1421 1422 1423 1424 1425
	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);

Alan Cox's avatar
Alan Cox committed
1426 1427
	/* Setup endpoints for each port specific thing */
	for (i = 0; i < d_details->num_ports; i++) {
Linus Torvalds's avatar
Linus Torvalds committed
1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462
		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);
Alan Cox's avatar
Alan Cox committed
1463
	}
Linus Torvalds's avatar
Linus Torvalds committed
1464 1465 1466 1467 1468 1469 1470
}

/* 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) */
Alan Cox's avatar
Alan Cox committed
1471
		div,	/* divisor */
Linus Torvalds's avatar
Linus Torvalds committed
1472 1473
		cnt;	/* inverse of divisor (programmed into 8051) */

Alan Cox's avatar
Alan Cox committed
1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489
	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
Linus Torvalds's avatar
Linus Torvalds committed
1490 1491
		cnt = 0 - div;

Alan Cox's avatar
Alan Cox committed
1492 1493
	if (div > 0xffff)
		return KEYSPAN_INVALID_BAUD_RATE;
Linus Torvalds's avatar
Linus Torvalds committed
1494

Alan Cox's avatar
Alan Cox committed
1495 1496
	/* return the counter values if non-null */
	if (rate_low)
Linus Torvalds's avatar
Linus Torvalds committed
1497
		*rate_low = (u8) (cnt & 0xff);
Alan Cox's avatar
Alan Cox committed
1498
	if (rate_hi)
Linus Torvalds's avatar
Linus Torvalds committed
1499
		*rate_hi = (u8) ((cnt >> 8) & 0xff);
Alan Cox's avatar
Alan Cox committed
1500 1501 1502 1503
	if (rate_low && rate_hi)
		dbg("%s - %d %02x %02x.",
				__func__, baud_rate, *rate_hi, *rate_low);
	return KEYSPAN_BAUD_RATE_OK;
Linus Torvalds's avatar
Linus Torvalds committed
1504 1505 1506 1507 1508 1509 1510
}

/* 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) */
Alan Cox's avatar
Alan Cox committed
1511
			div;	/* divisor */
Linus Torvalds's avatar
Linus Torvalds committed
1512

Alan Cox's avatar
Alan Cox committed
1513
	dbg("%s - %d.", __func__, baud_rate);
Linus Torvalds's avatar
Linus Torvalds committed
1514

Alan Cox's avatar
Alan Cox committed
1515 1516 1517 1518
	/* prevent divide by zero...  */
	b16 = baud_rate * 16L;
	if (b16 == 0)
		return KEYSPAN_INVALID_BAUD_RATE;
Linus Torvalds's avatar
Linus Torvalds committed
1519

Alan Cox's avatar
Alan Cox committed
1520 1521 1522 1523
	/* calculate the divisor */
	div = baudclk / b16;
	if (div == 0)
		return KEYSPAN_INVALID_BAUD_RATE;
Linus Torvalds's avatar
Linus Torvalds committed
1524

Alan Cox's avatar
Alan Cox committed
1525 1526
	if (div > 0xffff)
		return KEYSPAN_INVALID_BAUD_RATE;
Linus Torvalds's avatar
Linus Torvalds committed
1527

Alan Cox's avatar
Alan Cox committed
1528 1529
	/* return the counter values if non-null */
	if (rate_low)
Linus Torvalds's avatar
Linus Torvalds committed
1530
		*rate_low = (u8) (div & 0xff);
Alan Cox's avatar
Alan Cox committed
1531 1532

	if (rate_hi)
Linus Torvalds's avatar
Linus Torvalds committed
1533
		*rate_hi = (u8) ((div >> 8) & 0xff);
Alan Cox's avatar
Alan Cox committed
1534 1535 1536 1537 1538 1539

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

	return KEYSPAN_BAUD_RATE_OK;
Linus Torvalds's avatar
Linus Torvalds committed
1540 1541 1542 1543 1544 1545 1546
}

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 */
Alan Cox's avatar
Alan Cox committed
1547
		div,	/* divisor using 13/8 prescaler */
Linus Torvalds's avatar
Linus Torvalds committed
1548 1549 1550 1551 1552 1553
		res,	/* resulting baud rate using 13/8 prescaler */
		diff,	/* error using 13/8 prescaler */
		smallest_diff;
	u8	best_prescaler;
	int	i;

Alan Cox's avatar
Alan Cox committed
1554
	dbg("%s - %d.", __func__, baud_rate);
Linus Torvalds's avatar
Linus Torvalds committed
1555

Alan Cox's avatar
Alan Cox committed
1556 1557 1558 1559
	/* prevent divide by zero */
	b16 = baud_rate * 16L;
	if (b16 == 0)
		return KEYSPAN_INVALID_BAUD_RATE;
Linus Torvalds's avatar
Linus Torvalds committed
1560

Alan Cox's avatar
Alan Cox committed
1561 1562 1563 1564
	/* Calculate prescaler by trying them all and looking
	   for best fit */

	/* start with largest possible difference */
Linus Torvalds's avatar
Linus Torvalds committed
1565 1566 1567 1568 1569
	smallest_diff = 0xffffffff;

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

Alan Cox's avatar
Alan Cox committed
1570
	for (i = 8; i <= 0xff; ++i) {
Linus Torvalds's avatar
Linus Torvalds committed
1571
		clk = (baudclk * 8) / (u32) i;
Alan Cox's avatar
Alan Cox committed
1572 1573 1574

		div = clk / b16;
		if (div == 0)
Linus Torvalds's avatar
Linus Torvalds committed
1575 1576 1577
			continue;

		res = clk / div;
Alan Cox's avatar
Alan Cox committed
1578
		diff = (res > b16) ? (res-b16) : (b16-res);
Linus Torvalds's avatar
Linus Torvalds committed
1579

Alan Cox's avatar
Alan Cox committed
1580
		if (diff < smallest_diff) {
Linus Torvalds's avatar
Linus Torvalds committed
1581 1582 1583 1584 1585
			best_prescaler = i;
			smallest_diff = diff;
		}
	}

Alan Cox's avatar
Alan Cox committed
1586 1587
	if (best_prescaler == 0)
		return KEYSPAN_INVALID_BAUD_RATE;
Linus Torvalds's avatar
Linus Torvalds committed
1588 1589 1590 1591

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

Alan Cox's avatar
Alan Cox committed
1592 1593
	/* return the divisor and prescaler if non-null */
	if (rate_low)
Linus Torvalds's avatar
Linus Torvalds committed
1594
		*rate_low = (u8) (div & 0xff);
Alan Cox's avatar
Alan Cox committed
1595
	if (rate_hi)
Linus Torvalds's avatar
Linus Torvalds committed
1596 1597 1598
		*rate_hi = (u8) ((div >> 8) & 0xff);
	if (prescaler) {
		*prescaler = best_prescaler;
1599
		/*  dbg("%s - %d %d", __func__, *prescaler, div); */
Linus Torvalds's avatar
Linus Torvalds committed
1600
	}
Alan Cox's avatar
Alan Cox committed
1601
	return KEYSPAN_BAUD_RATE_OK;
Linus Torvalds's avatar
Linus Torvalds committed
1602 1603 1604 1605 1606 1607 1608
}

	/* 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) */
Alan Cox's avatar
Alan Cox committed
1609
		div,	/* divisor */
Linus Torvalds's avatar
Linus Torvalds committed
1610 1611
		cnt;	/* inverse of divisor (programmed into 8051) */

Alan Cox's avatar
Alan Cox committed
1612
	dbg("%s - %d.", __func__, baud_rate);
Linus Torvalds's avatar
Linus Torvalds committed
1613 1614

		/* prevent divide by zero */
Alan Cox's avatar
Alan Cox committed
1615 1616 1617 1618 1619 1620 1621 1622 1623
	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
Linus Torvalds's avatar
Linus Torvalds committed
1624 1625
		cnt = 0 - div;

Alan Cox's avatar
Alan Cox committed
1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636
	/* 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;
Linus Torvalds's avatar
Linus Torvalds committed
1637 1638 1639 1640
	}

		/* return the counter values if not NULL
		   (port 1 will ignore retHi) */
Alan Cox's avatar
Alan Cox committed
1641
	if (rate_low)
Linus Torvalds's avatar
Linus Torvalds committed
1642
		*rate_low = (u8) (cnt & 0xff);
Alan Cox's avatar
Alan Cox committed
1643
	if (rate_hi)
Linus Torvalds's avatar
Linus Torvalds committed
1644
		*rate_hi = (u8) ((cnt >> 8) & 0xff);
Alan Cox's avatar
Alan Cox committed
1645 1646
	dbg("%s - %d OK.", __func__, baud_rate);
	return KEYSPAN_BAUD_RATE_OK;
Linus Torvalds's avatar
Linus Torvalds committed
1647 1648 1649 1650 1651 1652
}

static int keyspan_usa26_send_setup(struct usb_serial *serial,
				    struct usb_serial_port *port,
				    int reset_port)
{
Alan Cox's avatar
Alan Cox committed
1653
	struct keyspan_usa26_portControlMessage	msg;
Linus Torvalds's avatar
Linus Torvalds committed
1654 1655 1656 1657 1658 1659 1660
	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;

Alan Cox's avatar
Alan Cox committed
1661
	dbg("%s reset=%d", __func__, reset_port);
Linus Torvalds's avatar
Linus Torvalds committed
1662 1663 1664 1665 1666 1667 1668 1669 1670

	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;

1671
	dbg("%s - endpoint %d", __func__, usb_pipeendpoint(this_urb->pipe));
Linus Torvalds's avatar
Linus Torvalds committed
1672 1673 1674

		/* Make sure we have an urb then send the message */
	if (this_urb == NULL) {
1675
		dbg("%s - oops no urb.", __func__);
Linus Torvalds's avatar
Linus Torvalds committed
1676 1677 1678 1679
		return -1;
	}

	/* Save reset port val for resend.
1680 1681
	   Don't overwrite resend for open/close condition. */
	if ((reset_port + 1) > p_priv->resend_cont)
Linus Torvalds's avatar
Linus Torvalds committed
1682 1683
		p_priv->resend_cont = reset_port + 1;
	if (this_urb->status == -EINPROGRESS) {
Alan Cox's avatar
Alan Cox committed
1684
		/*  dbg("%s - already writing", __func__); */
Linus Torvalds's avatar
Linus Torvalds committed
1685
		mdelay(5);
Alan Cox's avatar
Alan Cox committed
1686
		return -1;
Linus Torvalds's avatar
Linus Torvalds committed
1687 1688
	}

Alan Cox's avatar
Alan Cox committed
1689 1690 1691
	memset(&msg, 0, sizeof(struct keyspan_usa26_portControlMessage));

	/* Only set baud rate if it's changed */
Linus Torvalds's avatar
Linus Torvalds committed
1692 1693 1694 1695 1696
	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,
Alan Cox's avatar
Alan Cox committed
1697 1698 1699
		     &msg.baudLo, &msg.prescaler, device_port) == KEYSPAN_INVALID_BAUD_RATE) {
			dbg("%s - Invalid baud rate %d requested, using 9600.",
						__func__, p_priv->baud);
Linus Torvalds's avatar
Linus Torvalds committed
1700 1701 1702 1703 1704 1705 1706
			msg.baudLo = 0;
			msg.baudHi = 125;	/* Values for 9600 baud */
			msg.prescaler = 10;
		}
		msg.setPrescaler = 0xff;
	}

1707
	msg.lcr = (p_priv->cflag & CSTOPB) ? STOPBITS_678_2 : STOPBITS_5678_1;
Linus Torvalds's avatar
Linus Torvalds committed
1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723
	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 */
1724
		msg.lcr |= (p_priv->cflag & PARODD) ?
Alan Cox's avatar
Alan Cox committed
1725
			USA_PARITY_ODD : USA_PARITY_EVEN;
Linus Torvalds's avatar
Linus Torvalds committed
1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765
	}
	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 {
Alan Cox's avatar
Alan Cox committed
1766
		msg._txOn = (!p_priv->break_on);
Linus Torvalds's avatar
Linus Torvalds committed
1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777
		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;
	}

Alan Cox's avatar
Alan Cox committed
1778
	/* Do handshaking outputs */
Linus Torvalds's avatar
Linus Torvalds committed
1779 1780 1781 1782 1783
	msg.setTxTriState_setRts = 0xff;
	msg.txTriState_rts = p_priv->rts_state;

	msg.setHskoa_setDtr = 0xff;
	msg.hskoa_dtr = p_priv->dtr_state;
Alan Cox's avatar
Alan Cox committed
1784

Linus Torvalds's avatar
Linus Torvalds committed
1785
	p_priv->resend_cont = 0;
Alan Cox's avatar
Alan Cox committed
1786 1787
	memcpy(this_urb->transfer_buffer, &msg, sizeof(msg));

Linus Torvalds's avatar
Linus Torvalds committed
1788 1789 1790
	/* send the data out the device on control endpoint */
	this_urb->transfer_buffer_length = sizeof(msg);

Alan Cox's avatar
Alan Cox committed
1791 1792
	err = usb_submit_urb(this_urb, GFP_ATOMIC);
	if (err != 0)
1793
		dbg("%s - usb_submit_urb(setup) failed (%d)", __func__, err);
Linus Torvalds's avatar
Linus Torvalds committed
1794 1795
#if 0
	else {
1796
		dbg("%s - usb_submit_urb(%d) OK %d bytes (end %d)", __func__
Linus Torvalds's avatar
Linus Torvalds committed
1797 1798 1799 1800 1801
		    outcont_urb, this_urb->transfer_buffer_length,
		    usb_pipeendpoint(this_urb->pipe));
	}
#endif

1802
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
1803 1804 1805 1806 1807 1808
}

static int keyspan_usa28_send_setup(struct usb_serial *serial,
				    struct usb_serial_port *port,
				    int reset_port)
{
Alan Cox's avatar
Alan Cox committed
1809
	struct keyspan_usa28_portControlMessage	msg;
Linus Torvalds's avatar
Linus Torvalds committed
1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821
	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;

	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 */
Alan Cox's avatar
Alan Cox committed
1822 1823
	this_urb = p_priv->outcont_urb;
	if (this_urb == NULL) {
1824
		dbg("%s - oops no urb.", __func__);
Linus Torvalds's avatar
Linus Torvalds committed
1825 1826 1827 1828
		return -1;
	}

	/* Save reset port val for resend.
1829 1830
	   Don't overwrite resend for open/close condition. */
	if ((reset_port + 1) > p_priv->resend_cont)
Linus Torvalds's avatar
Linus Torvalds committed
1831 1832
		p_priv->resend_cont = reset_port + 1;
	if (this_urb->status == -EINPROGRESS) {
Alan Cox's avatar
Alan Cox committed
1833
		dbg("%s already writing", __func__);
Linus Torvalds's avatar
Linus Torvalds committed
1834
		mdelay(5);
Alan Cox's avatar
Alan Cox committed
1835
		return -1;
Linus Torvalds's avatar
Linus Torvalds committed
1836 1837
	}

Alan Cox's avatar
Alan Cox committed
1838
	memset(&msg, 0, sizeof(struct keyspan_usa28_portControlMessage));
Linus Torvalds's avatar
Linus Torvalds committed
1839 1840 1841

	msg.setBaudRate = 1;
	if (d_details->calculate_baud_rate(p_priv->baud, d_details->baudclk,
Alan Cox's avatar
Alan Cox committed
1842 1843 1844
		&msg.baudHi, &msg.baudLo, NULL, device_port) == KEYSPAN_INVALID_BAUD_RATE) {
		dbg("%s - Invalid baud rate requested %d.",
						__func__, p_priv->baud);
Linus Torvalds's avatar
Linus Torvalds committed
1845 1846 1847 1848 1849 1850 1851 1852 1853 1854
		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;

Alan Cox's avatar
Alan Cox committed
1855
	/* Do handshaking outputs, DTR is inverted relative to RTS */
Linus Torvalds's avatar
Linus Torvalds committed
1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896
	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 {
Alan Cox's avatar
Alan Cox committed
1897
		msg._txOn = (!p_priv->break_on);
Linus Torvalds's avatar
Linus Torvalds committed
1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910
		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;
Alan Cox's avatar
Alan Cox committed
1911
	memcpy(this_urb->transfer_buffer, &msg, sizeof(msg));
Linus Torvalds's avatar
Linus Torvalds committed
1912 1913 1914 1915

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

Alan Cox's avatar
Alan Cox committed
1916 1917
	err = usb_submit_urb(this_urb, GFP_ATOMIC);
	if (err != 0)
1918
		dbg("%s - usb_submit_urb(setup) failed", __func__);
Linus Torvalds's avatar
Linus Torvalds committed
1919 1920
#if 0
	else {
1921
		dbg("%s - usb_submit_urb(setup) OK %d bytes", __func__,
Linus Torvalds's avatar
Linus Torvalds committed
1922 1923 1924 1925
		    this_urb->transfer_buffer_length);
	}
#endif

1926
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
1927 1928 1929 1930 1931 1932
}

static int keyspan_usa49_send_setup(struct usb_serial *serial,
				    struct usb_serial_port *port,
				    int reset_port)
{
1933 1934
	struct keyspan_usa49_portControlMessage	msg;
	struct usb_ctrlrequest 			*dr = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946
	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;

	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;

1947
	/* Work out which port within the device is being setup */
Linus Torvalds's avatar
Linus Torvalds committed
1948 1949
	device_port = port->number - port->serial->minor;

1950
	/* Make sure we have an urb then send the message */
Linus Torvalds's avatar
Linus Torvalds committed
1951
	if (this_urb == NULL) {
1952
		dbg("%s - oops no urb for port %d.", __func__, port->number);
Linus Torvalds's avatar
Linus Torvalds committed
1953 1954 1955
		return -1;
	}

1956 1957 1958 1959
	dbg("%s - endpoint %d port %d (%d)",
			__func__, usb_pipeendpoint(this_urb->pipe),
			port->number, device_port);

Linus Torvalds's avatar
Linus Torvalds committed
1960
	/* Save reset port val for resend.
1961 1962
	   Don't overwrite resend for open/close condition. */
	if ((reset_port + 1) > p_priv->resend_cont)
Linus Torvalds's avatar
Linus Torvalds committed
1963
		p_priv->resend_cont = reset_port + 1;
1964

Linus Torvalds's avatar
Linus Torvalds committed
1965
	if (this_urb->status == -EINPROGRESS) {
Alan Cox's avatar
Alan Cox committed
1966
		/*  dbg("%s - already writing", __func__); */
Linus Torvalds's avatar
Linus Torvalds committed
1967
		mdelay(5);
Alan Cox's avatar
Alan Cox committed
1968
		return -1;
Linus Torvalds's avatar
Linus Torvalds committed
1969 1970
	}

Alan Cox's avatar
Alan Cox committed
1971
	memset(&msg, 0, sizeof(struct keyspan_usa49_portControlMessage));
Linus Torvalds's avatar
Linus Torvalds committed
1972 1973 1974

	/*msg.portNumber = port->number;*/
	msg.portNumber = device_port;
Alan Cox's avatar
Alan Cox committed
1975 1976

	/* Only set baud rate if it's changed */
Linus Torvalds's avatar
Linus Torvalds committed
1977 1978 1979 1980 1981
	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,
Alan Cox's avatar
Alan Cox committed
1982 1983 1984
		     &msg.baudLo, &msg.prescaler, device_port) == KEYSPAN_INVALID_BAUD_RATE) {
			dbg("%s - Invalid baud rate %d requested, using 9600.",
						__func__, p_priv->baud);
Linus Torvalds's avatar
Linus Torvalds committed
1985 1986 1987 1988
			msg.baudLo = 0;
			msg.baudHi = 125;	/* Values for 9600 baud */
			msg.prescaler = 10;
		}
Alan Cox's avatar
Alan Cox committed
1989
		/* msg.setPrescaler = 0xff; */
Linus Torvalds's avatar
Linus Torvalds committed
1990 1991
	}

1992
	msg.lcr = (p_priv->cflag & CSTOPB) ? STOPBITS_678_2 : STOPBITS_5678_1;
Linus Torvalds's avatar
Linus Torvalds committed
1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008
	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 */
2009
		msg.lcr |= (p_priv->cflag & PARODD) ?
Alan Cox's avatar
Alan Cox committed
2010
			USA_PARITY_ODD : USA_PARITY_EVEN;
Linus Torvalds's avatar
Linus Torvalds committed
2011 2012 2013 2014 2015 2016
	}
	msg.setLcr = 0xff;

	msg.ctsFlowControl = (p_priv->flow_control == flow_cts);
	msg.xonFlowControl = 0;
	msg.setFlowControl = 0xff;
Alan Cox's avatar
Alan Cox committed
2017

Linus Torvalds's avatar
Linus Torvalds committed
2018 2019 2020 2021
	msg.forwardingLength = 16;
	msg.xonChar = 17;
	msg.xoffChar = 19;

Alan Cox's avatar
Alan Cox committed
2022
	/* Opening port */
Linus Torvalds's avatar
Linus Torvalds committed
2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053
	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 {
Alan Cox's avatar
Alan Cox committed
2054
		msg._txOn = (!p_priv->break_on);
Linus Torvalds's avatar
Linus Torvalds committed
2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067
		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;
	}

Alan Cox's avatar
Alan Cox committed
2068
	/* Do handshaking outputs */
Linus Torvalds's avatar
Linus Torvalds committed
2069 2070 2071 2072 2073
	msg.setRts = 0xff;
	msg.rts = p_priv->rts_state;

	msg.setDtr = 0xff;
	msg.dtr = p_priv->dtr_state;
Alan Cox's avatar
Alan Cox committed
2074

Linus Torvalds's avatar
Linus Torvalds committed
2075
	p_priv->resend_cont = 0;
2076

Alan Cox's avatar
Alan Cox committed
2077 2078
	/* if the device is a 49wg, we send control message on usb
	   control EP 0 */
2079 2080 2081 2082 2083 2084 2085 2086 2087

	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));

Alan Cox's avatar
Alan Cox committed
2088
		memcpy(s_priv->glocont_buf, &msg, sizeof(msg));
2089

Alan Cox's avatar
Alan Cox committed
2090 2091 2092 2093
		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);
2094 2095 2096

	} else {
		memcpy(this_urb->transfer_buffer, &msg, sizeof(msg));
Alan Cox's avatar
Alan Cox committed
2097

2098 2099 2100
		/* send the data out the device on control endpoint */
		this_urb->transfer_buffer_length = sizeof(msg);
	}
Alan Cox's avatar
Alan Cox committed
2101 2102
	err = usb_submit_urb(this_urb, GFP_ATOMIC);
	if (err != 0)
2103
		dbg("%s - usb_submit_urb(setup) failed (%d)", __func__, err);
Linus Torvalds's avatar
Linus Torvalds committed
2104 2105
#if 0
	else {
2106
		dbg("%s - usb_submit_urb(%d) OK %d bytes (end %d)", __func__,
2107 2108
			   outcont_urb, this_urb->transfer_buffer_length,
			   usb_pipeendpoint(this_urb->pipe));
Linus Torvalds's avatar
Linus Torvalds committed
2109 2110 2111
	}
#endif

2112
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
2113 2114 2115 2116 2117 2118
}

static int keyspan_usa90_send_setup(struct usb_serial *serial,
				    struct usb_serial_port *port,
				    int reset_port)
{
Alan Cox's avatar
Alan Cox committed
2119
	struct keyspan_usa90_portControlMessage	msg;
Linus Torvalds's avatar
Linus Torvalds committed
2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131
	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;

	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 */
Alan Cox's avatar
Alan Cox committed
2132 2133
	this_urb = p_priv->outcont_urb;
	if (this_urb == NULL) {
2134
		dbg("%s - oops no urb.", __func__);
Linus Torvalds's avatar
Linus Torvalds committed
2135 2136 2137 2138 2139 2140 2141 2142
		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) {
Alan Cox's avatar
Alan Cox committed
2143
		dbg("%s already writing", __func__);
Linus Torvalds's avatar
Linus Torvalds committed
2144
		mdelay(5);
Alan Cox's avatar
Alan Cox committed
2145
		return -1;
Linus Torvalds's avatar
Linus Torvalds committed
2146 2147
	}

Alan Cox's avatar
Alan Cox committed
2148
	memset(&msg, 0, sizeof(struct keyspan_usa90_portControlMessage));
Linus Torvalds's avatar
Linus Torvalds committed
2149

Alan Cox's avatar
Alan Cox committed
2150
	/* Only set baud rate if it's changed */
Linus Torvalds's avatar
Linus Torvalds committed
2151 2152 2153 2154 2155
	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,
Alan Cox's avatar
Alan Cox committed
2156 2157 2158
		     &msg.baudLo, &prescaler, 0) == KEYSPAN_INVALID_BAUD_RATE) {
			dbg("%s - Invalid baud rate %d requested, using 9600.",
						__func__, p_priv->baud);
Linus Torvalds's avatar
Linus Torvalds committed
2159
			p_priv->baud = 9600;
Alan Cox's avatar
Alan Cox committed
2160
			d_details->calculate_baud_rate(p_priv->baud, d_details->baudclk,
Linus Torvalds's avatar
Linus Torvalds committed
2161 2162 2163 2164 2165 2166 2167
				&msg.baudHi, &msg.baudLo, &prescaler, 0);
		}
		msg.setRxMode = 1;
		msg.setTxMode = 1;
	}

	/* modes must always be correctly specified */
Alan Cox's avatar
Alan Cox committed
2168
	if (p_priv->baud > 57600) {
Linus Torvalds's avatar
Linus Torvalds committed
2169 2170
		msg.rxMode = RXMODE_DMA;
		msg.txMode = TXMODE_DMA;
Alan Cox's avatar
Alan Cox committed
2171
	} else {
Linus Torvalds's avatar
Linus Torvalds committed
2172 2173 2174 2175
		msg.rxMode = RXMODE_BYHAND;
		msg.txMode = TXMODE_BYHAND;
	}

2176
	msg.lcr = (p_priv->cflag & CSTOPB) ? STOPBITS_678_2 : STOPBITS_5678_1;
Linus Torvalds's avatar
Linus Torvalds committed
2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192
	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 */
2193
		msg.lcr |= (p_priv->cflag & PARODD) ?
Alan Cox's avatar
Alan Cox committed
2194
			USA_PARITY_ODD : USA_PARITY_EVEN;
Linus Torvalds's avatar
Linus Torvalds committed
2195 2196 2197 2198 2199 2200 2201 2202 2203 2204
	}
	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;
Alan Cox's avatar
Alan Cox committed
2205

Linus Torvalds's avatar
Linus Torvalds committed
2206
	msg.rxForwardingLength = 16;
Alan Cox's avatar
Alan Cox committed
2207
	msg.rxForwardingTimeout = 16;
Linus Torvalds's avatar
Linus Torvalds committed
2208 2209 2210 2211
	msg.txAckSetting = 0;
	msg.xonChar = 17;
	msg.xoffChar = 19;

Alan Cox's avatar
Alan Cox committed
2212
	/* Opening port */
Linus Torvalds's avatar
Linus Torvalds committed
2213 2214 2215 2216 2217 2218
	if (reset_port == 1) {
		msg.portEnabled = 1;
		msg.rxFlush = 1;
		msg.txBreak = (p_priv->break_on);
	}
	/* Closing port */
Alan Cox's avatar
Alan Cox committed
2219
	else if (reset_port == 2)
Linus Torvalds's avatar
Linus Torvalds committed
2220 2221 2222
		msg.portEnabled = 0;
	/* Sending intermediate configs */
	else {
2223
		msg.portEnabled = 1;
Linus Torvalds's avatar
Linus Torvalds committed
2224 2225 2226
		msg.txBreak = (p_priv->break_on);
	}

Alan Cox's avatar
Alan Cox committed
2227
	/* Do handshaking outputs */
Linus Torvalds's avatar
Linus Torvalds committed
2228 2229 2230 2231 2232
	msg.setRts = 0x01;
	msg.rts = p_priv->rts_state;

	msg.setDtr = 0x01;
	msg.dtr = p_priv->dtr_state;
Alan Cox's avatar
Alan Cox committed
2233

Linus Torvalds's avatar
Linus Torvalds committed
2234
	p_priv->resend_cont = 0;
Alan Cox's avatar
Alan Cox committed
2235 2236
	memcpy(this_urb->transfer_buffer, &msg, sizeof(msg));

Linus Torvalds's avatar
Linus Torvalds committed
2237 2238 2239
	/* send the data out the device on control endpoint */
	this_urb->transfer_buffer_length = sizeof(msg);

Alan Cox's avatar
Alan Cox committed
2240 2241
	err = usb_submit_urb(this_urb, GFP_ATOMIC);
	if (err != 0)
2242
		dbg("%s - usb_submit_urb(setup) failed (%d)", __func__, err);
2243
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
2244 2245
}

2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267
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;

	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) {
2268
		dbg("%s - oops no urb for port %d.", __func__,
2269 2270 2271 2272 2273 2274 2275 2276 2277
			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) {
Alan Cox's avatar
Alan Cox committed
2278
		/*  dbg("%s - already writing", __func__); */
2279
		mdelay(5);
Alan Cox's avatar
Alan Cox committed
2280
		return -1;
2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292
	}

	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,
Alan Cox's avatar
Alan Cox committed
2293 2294 2295
		     &msg.baudLo, &msg.prescaler, device_port) == KEYSPAN_INVALID_BAUD_RATE) {
			dbg("%s - Invalid baud rate %d requested, using 9600.",
						__func__, p_priv->baud);
2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319
			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 */
2320
		msg.lcr |= (p_priv->cflag & PARODD) ?
Alan Cox's avatar
Alan Cox committed
2321
					USA_PARITY_ODD : USA_PARITY_EVEN;
2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357
	}
	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 */
Alan Cox's avatar
Alan Cox committed
2358
		msg._txOn = (!p_priv->break_on);
2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385
		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);

	err = usb_submit_urb(this_urb, GFP_ATOMIC);
	if (err != 0)
2386
		dbg("%s - usb_submit_urb(setup) failed (%d)", __func__,
2387
				err);
2388
	return 0;
2389 2390
}

Linus Torvalds's avatar
Linus Torvalds committed
2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412
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;

	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;
2413 2414 2415
	case msg_usa67:
		keyspan_usa67_send_setup(serial, port, reset_port);
		break;
Linus Torvalds's avatar
Linus Torvalds committed
2416 2417 2418 2419 2420 2421
	}
}


/* Gets called by the "real" driver (ie once firmware is loaded
   and renumeration has taken place. */
Alan Cox's avatar
Alan Cox committed
2422
static int keyspan_startup(struct usb_serial *serial)
Linus Torvalds's avatar
Linus Torvalds committed
2423 2424 2425 2426 2427 2428 2429 2430
{
	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;

	for (i = 0; (d_details = keyspan_devices[i]) != NULL; ++i)
Alan Cox's avatar
Alan Cox committed
2431 2432
		if (d_details->product_id ==
				le16_to_cpu(serial->dev->descriptor.idProduct))
Linus Torvalds's avatar
Linus Torvalds committed
2433 2434
			break;
	if (d_details == NULL) {
Alan Cox's avatar
Alan Cox committed
2435 2436
		dev_err(&serial->dev->dev, "%s - unknown product id %x\n",
		    __func__, le16_to_cpu(serial->dev->descriptor.idProduct));
Linus Torvalds's avatar
Linus Torvalds committed
2437 2438 2439 2440
		return 1;
	}

	/* Setup private data for serial driver */
2441
	s_priv = kzalloc(sizeof(struct keyspan_serial_private), GFP_KERNEL);
Linus Torvalds's avatar
Linus Torvalds committed
2442
	if (!s_priv) {
Alan Cox's avatar
Alan Cox committed
2443 2444
		dbg("%s - kmalloc for keyspan_serial_private failed.",
								__func__);
Linus Torvalds's avatar
Linus Torvalds committed
2445 2446 2447 2448 2449 2450 2451 2452 2453
		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];
Alan Cox's avatar
Alan Cox committed
2454 2455
		p_priv = kzalloc(sizeof(struct keyspan_port_private),
								GFP_KERNEL);
Linus Torvalds's avatar
Linus Torvalds committed
2456
		if (!p_priv) {
2457
			dbg("%s - kmalloc for keyspan_port_private (%d) failed!.", __func__, i);
Alan Cox's avatar
Alan Cox committed
2458
			return 1;
Linus Torvalds's avatar
Linus Torvalds committed
2459 2460 2461 2462 2463 2464 2465
		}
		p_priv->device_details = d_details;
		usb_set_serial_port_data(port, p_priv);
	}

	keyspan_setup_urbs(serial);

2466 2467 2468
	if (s_priv->instat_urb != NULL) {
		err = usb_submit_urb(s_priv->instat_urb, GFP_KERNEL);
		if (err != 0)
2469
			dbg("%s - submit instat urb failed %d", __func__,
2470 2471 2472 2473 2474
				err);
	}
	if (s_priv->indat_urb != NULL) {
		err = usb_submit_urb(s_priv->indat_urb, GFP_KERNEL);
		if (err != 0)
2475
			dbg("%s - submit indat urb failed %d", __func__,
2476
				err);
Linus Torvalds's avatar
Linus Torvalds committed
2477
	}
Alan Cox's avatar
Alan Cox committed
2478

2479
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
2480 2481
}

2482
static void keyspan_disconnect(struct usb_serial *serial)
Linus Torvalds's avatar
Linus Torvalds committed
2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493
{
	int				i, j;
	struct usb_serial_port		*port;
	struct keyspan_serial_private 	*s_priv;
	struct keyspan_port_private	*p_priv;

	s_priv = usb_get_serial_data(serial);

	/* Stop reading/writing urbs */
	stop_urb(s_priv->instat_urb);
	stop_urb(s_priv->glocont_urb);
2494
	stop_urb(s_priv->indat_urb);
Linus Torvalds's avatar
Linus Torvalds committed
2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506
	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 */
2507
	usb_free_urb(s_priv->instat_urb);
2508
	usb_free_urb(s_priv->indat_urb);
2509
	usb_free_urb(s_priv->glocont_urb);
Linus Torvalds's avatar
Linus Torvalds committed
2510 2511 2512
	for (i = 0; i < serial->num_ports; ++i) {
		port = serial->port[i];
		p_priv = usb_get_serial_port_data(port);
2513 2514
		usb_free_urb(p_priv->inack_urb);
		usb_free_urb(p_priv->outcont_urb);
Linus Torvalds's avatar
Linus Torvalds committed
2515
		for (j = 0; j < 2; j++) {
2516 2517
			usb_free_urb(p_priv->in_urbs[j]);
			usb_free_urb(p_priv->out_urbs[j]);
Linus Torvalds's avatar
Linus Torvalds committed
2518 2519
		}
	}
2520 2521 2522 2523 2524 2525 2526 2527 2528
}

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

	s_priv = usb_get_serial_data(serial);
Linus Torvalds's avatar
Linus Torvalds committed
2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540

	/*  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));
	}
}

Alan Cox's avatar
Alan Cox committed
2541 2542
MODULE_AUTHOR(DRIVER_AUTHOR);
MODULE_DESCRIPTION(DRIVER_DESC);
Linus Torvalds's avatar
Linus Torvalds committed
2543 2544
MODULE_LICENSE("GPL");

2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557
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");

Linus Torvalds's avatar
Linus Torvalds committed
2558 2559 2560
module_param(debug, bool, S_IRUGO | S_IWUSR);
MODULE_PARM_DESC(debug, "Debug enabled or not");