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

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

L
Linus Torvalds 已提交
7 8 9 10 11
  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.
A
Alan Cox 已提交
13

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

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

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

  Tip 'o the hat to IBM (and previously Linuxcare :) for supporting
L
Linus Torvalds 已提交
27 28 29 30 31 32 33 34 35 36 37 38 39 40
  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>
A
Alan Cox 已提交
43
#include <linux/uaccess.h>
L
Linus Torvalds 已提交
44
#include <linux/usb.h>
45
#include <linux/usb/serial.h>
46
#include <linux/usb/ezusb.h>
L
Linus Torvalds 已提交
47 48 49 50 51
#include "keyspan.h"

/*
 * Version Information
 */
52
#define DRIVER_VERSION "v1.1.5"
L
Linus Torvalds 已提交
53 54 55 56 57
#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
58
#define INDAT49W_BUFLEN	512
L
Linus Torvalds 已提交
59 60 61 62 63 64 65 66

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

	struct urb	*instat_urb;
	char		instat_buf[INSTAT_BUFLEN];

A
Alan Cox 已提交
67 68
	/* added to support 49wg, where data from all 4 ports comes in
	   on 1 EP and high-speed supported */
69 70 71
	struct urb	*indat_urb;
	char		indat_buf[INDAT49W_BUFLEN];

L
Linus Torvalds 已提交
72 73 74
	/* XXX this one probably will need a lock */
	struct urb	*glocont_urb;
	char		glocont_buf[GLOCONT_BUFLEN];
A
Alan Cox 已提交
75
	char		ctrl_buf[8];	/* for EP0 control message */
L
Linus Torvalds 已提交
76 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
};

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

L
Linus Torvalds 已提交
131

132
module_usb_serial_driver(serial_drivers, keyspan_ids_combined);
L
Linus Torvalds 已提交
133

A
Alan Cox 已提交
134
static void keyspan_break_ctl(struct tty_struct *tty, int break_state)
L
Linus Torvalds 已提交
135
{
A
Alan Cox 已提交
136
	struct usb_serial_port *port = tty->driver_data;
L
Linus Torvalds 已提交
137 138 139 140 141 142 143 144 145 146 147 148 149
	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);
}


A
Alan Cox 已提交
150
static void keyspan_set_termios(struct tty_struct *tty,
A
Alan Cox 已提交
151
		struct usb_serial_port *port, struct ktermios *old_termios)
L
Linus Torvalds 已提交
152 153 154 155 156 157 158 159
{
	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;
A
Alan Cox 已提交
160
	cflag = tty->termios->c_cflag;
L
Linus Torvalds 已提交
161 162 163 164
	device_port = port->number - port->serial->minor;

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

A
Alan Cox 已提交
175
	tty_encode_baud_rate(tty, baud_rate, baud_rate);
L
Linus Torvalds 已提交
176 177
	/* set CTS/RTS handshake etc. */
	p_priv->cflag = cflag;
178
	p_priv->flow_control = (cflag & CRTSCTS) ? flow_cts : flow_none;
L
Linus Torvalds 已提交
179

A
Alan Cox 已提交
180 181 182
	/* Mark/Space not supported */
	tty->termios->c_cflag &= ~CMSPAR;

L
Linus Torvalds 已提交
183 184 185
	keyspan_send_setup(port, 0);
}

186
static int keyspan_tiocmget(struct tty_struct *tty)
L
Linus Torvalds 已提交
187
{
A
Alan Cox 已提交
188 189
	struct usb_serial_port *port = tty->driver_data;
	struct keyspan_port_private *p_priv = usb_get_serial_port_data(port);
L
Linus Torvalds 已提交
190
	unsigned int			value;
A
Alan Cox 已提交
191

L
Linus Torvalds 已提交
192 193 194 195 196
	value = ((p_priv->rts_state) ? TIOCM_RTS : 0) |
		((p_priv->dtr_state) ? TIOCM_DTR : 0) |
		((p_priv->cts_state) ? TIOCM_CTS : 0) |
		((p_priv->dsr_state) ? TIOCM_DSR : 0) |
		((p_priv->dcd_state) ? TIOCM_CAR : 0) |
A
Alan Cox 已提交
197
		((p_priv->ri_state) ? TIOCM_RNG : 0);
L
Linus Torvalds 已提交
198 199 200 201

	return value;
}

202
static int keyspan_tiocmset(struct tty_struct *tty,
L
Linus Torvalds 已提交
203 204
			    unsigned int set, unsigned int clear)
{
A
Alan Cox 已提交
205 206
	struct usb_serial_port *port = tty->driver_data;
	struct keyspan_port_private *p_priv = usb_get_serial_port_data(port);
A
Alan Cox 已提交
207

L
Linus Torvalds 已提交
208 209 210 211 212 213 214 215 216 217 218 219
	if (set & TIOCM_RTS)
		p_priv->rts_state = 1;
	if (set & TIOCM_DTR)
		p_priv->dtr_state = 1;
	if (clear & TIOCM_RTS)
		p_priv->rts_state = 0;
	if (clear & TIOCM_DTR)
		p_priv->dtr_state = 0;
	keyspan_send_setup(port, 0);
	return 0;
}

A
Alan Cox 已提交
220 221 222 223
/* Write function is similar for the four protocols used
   with only a minor change for usa90 (usa19hs) required */
static int keyspan_write(struct tty_struct *tty,
	struct usb_serial_port *port, const unsigned char *buf, int count)
L
Linus Torvalds 已提交
224 225 226 227 228 229
{
	struct keyspan_port_private 	*p_priv;
	const struct keyspan_device_details	*d_details;
	int				flip;
	int 				left, todo;
	struct urb			*this_urb;
A
Alan Cox 已提交
230
	int 				err, maxDataLen, dataOffset;
L
Linus Torvalds 已提交
231 232 233 234 235

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

	if (d_details->msg_format == msg_usa90) {
A
Alan Cox 已提交
236
		maxDataLen = 64;
L
Linus Torvalds 已提交
237 238 239 240 241
		dataOffset = 0;
	} else {
		maxDataLen = 63;
		dataOffset = 1;
	}
A
Alan Cox 已提交
242

243 244
	dev_dbg(&port->dev, "%s - for port %d (%d chars), flip=%d\n",
		__func__, port->number, count, p_priv->out_flip);
L
Linus Torvalds 已提交
245 246 247 248 249 250 251

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

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

L
Linus Torvalds 已提交
253
		/* Check we have a valid urb/endpoint before we use it... */
A
Alan Cox 已提交
254 255
		this_urb = p_priv->out_urbs[flip];
		if (this_urb == NULL) {
L
Linus Torvalds 已提交
256
			/* no bulk out, so return 0 bytes written */
257
			dev_dbg(&port->dev, "%s - no output urb :(\n", __func__);
L
Linus Torvalds 已提交
258 259 260
			return count;
		}

261
		dev_dbg(&port->dev, "%s - endpoint %d flip %d\n",
A
Alan Cox 已提交
262
			__func__, usb_pipeendpoint(this_urb->pipe), flip);
L
Linus Torvalds 已提交
263 264

		if (this_urb->status == -EINPROGRESS) {
A
Alan Cox 已提交
265 266
			if (time_before(jiffies,
					p_priv->tx_start_time[flip] + 10 * HZ))
L
Linus Torvalds 已提交
267 268 269 270 271
				break;
			usb_unlink_urb(this_urb);
			break;
		}

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

A
Alan Cox 已提交
276
		memcpy(this_urb->transfer_buffer + dataOffset, buf, todo);
L
Linus Torvalds 已提交
277 278 279 280 281
		buf += todo;

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

A
Alan Cox 已提交
282 283
		err = usb_submit_urb(this_urb, GFP_ATOMIC);
		if (err != 0)
284
			dev_dbg(&port->dev, "usb_submit_urb(write bulk) failed (%d)\n", err);
L
Linus Torvalds 已提交
285 286 287 288 289 290 291 292 293 294
		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;
}

295
static void	usa26_indat_callback(struct urb *urb)
L
Linus Torvalds 已提交
296 297 298 299 300 301
{
	int			i, err;
	int			endpoint;
	struct usb_serial_port	*port;
	struct tty_struct	*tty;
	unsigned char 		*data = urb->transfer_buffer;
302
	int status = urb->status;
L
Linus Torvalds 已提交
303 304 305

	endpoint = usb_pipeendpoint(urb->pipe);

306
	if (status) {
307 308
		dev_dbg(&urb->dev->dev,"%s - nonzero status: %x on endpoint %d.\n",
			__func__, status, endpoint);
L
Linus Torvalds 已提交
309 310 311
		return;
	}

312
	port =  urb->context;
A
Alan Cox 已提交
313
	tty = tty_port_tty_get(&port->port);
A
Alan Cox 已提交
314
	if (tty && urb->actual_length) {
L
Linus Torvalds 已提交
315 316
		/* 0x80 bit is error flag */
		if ((data[0] & 0x80) == 0) {
A
Alan Cox 已提交
317 318
			/* no errors on individual bytes, only
			   possible overrun err */
L
Linus Torvalds 已提交
319
			if (data[0] & RXERROR_OVERRUN)
A
Alan Cox 已提交
320 321 322 323
				err = TTY_OVERRUN;
			else
				err = 0;
			for (i = 1; i < urb->actual_length ; ++i)
L
Linus Torvalds 已提交
324 325 326
				tty_insert_flip_char(tty, data[i], err);
		} else {
			/* some bytes had errors, every byte has status */
327
			dev_dbg(&port->dev, "%s - RX error!!!!\n", __func__);
L
Linus Torvalds 已提交
328 329 330 331 332 333 334 335 336 337 338 339 340 341
			for (i = 0; i + 1 < urb->actual_length; i += 2) {
				int stat = data[i], flag = 0;
				if (stat & RXERROR_OVERRUN)
					flag |= TTY_OVERRUN;
				if (stat & RXERROR_FRAMING)
					flag |= TTY_FRAME;
				if (stat & RXERROR_PARITY)
					flag |= TTY_PARITY;
				/* XXX should handle break (0x10) */
				tty_insert_flip_char(tty, data[i+1], flag);
			}
		}
		tty_flip_buffer_push(tty);
	}
A
Alan Cox 已提交
342
	tty_kref_put(tty);
A
Alan Cox 已提交
343 344

	/* Resubmit urb so we continue receiving */
345 346
	err = usb_submit_urb(urb, GFP_ATOMIC);
	if (err != 0)
347
		dev_dbg(&port->dev, "%s - resubmit read urb failed. (%d)\n", __func__, err);
L
Linus Torvalds 已提交
348 349
}

A
Alan Cox 已提交
350
/* Outdat handling is common for all devices */
351
static void	usa2x_outdat_callback(struct urb *urb)
L
Linus Torvalds 已提交
352 353 354 355
{
	struct usb_serial_port *port;
	struct keyspan_port_private *p_priv;

356
	port =  urb->context;
L
Linus Torvalds 已提交
357
	p_priv = usb_get_serial_port_data(port);
358
	dev_dbg(&port->dev, "%s - urb %d\n", __func__, urb == p_priv->out_urbs[1]);
L
Linus Torvalds 已提交
359

360
	usb_serial_port_softint(port);
L
Linus Torvalds 已提交
361 362
}

363
static void	usa26_inack_callback(struct urb *urb)
L
Linus Torvalds 已提交
364 365 366
{
}

367
static void	usa26_outcont_callback(struct urb *urb)
L
Linus Torvalds 已提交
368 369 370 371
{
	struct usb_serial_port *port;
	struct keyspan_port_private *p_priv;

372
	port =  urb->context;
L
Linus Torvalds 已提交
373 374 375
	p_priv = usb_get_serial_port_data(port);

	if (p_priv->resend_cont) {
376
		dev_dbg(&port->dev, "%s - sending setup\n", __func__);
A
Alan Cox 已提交
377 378
		keyspan_usa26_send_setup(port->serial, port,
						p_priv->resend_cont - 1);
L
Linus Torvalds 已提交
379 380 381
	}
}

382
static void	usa26_instat_callback(struct urb *urb)
L
Linus Torvalds 已提交
383 384 385 386 387 388
{
	unsigned char 				*data = urb->transfer_buffer;
	struct keyspan_usa26_portStatusMessage	*msg;
	struct usb_serial			*serial;
	struct usb_serial_port			*port;
	struct keyspan_port_private	 	*p_priv;
A
Alan Cox 已提交
389
	struct tty_struct			*tty;
L
Linus Torvalds 已提交
390
	int old_dcd_state, err;
391
	int status = urb->status;
L
Linus Torvalds 已提交
392

393
	serial =  urb->context;
L
Linus Torvalds 已提交
394

395
	if (status) {
396
		dev_dbg(&urb->dev->dev, "%s - nonzero status: %x\n", __func__, status);
L
Linus Torvalds 已提交
397 398 399
		return;
	}
	if (urb->actual_length != 9) {
400
		dev_dbg(&urb->dev->dev, "%s - %d byte report??\n", __func__, urb->actual_length);
L
Linus Torvalds 已提交
401 402 403 404 405 406
		goto exit;
	}

	msg = (struct keyspan_usa26_portStatusMessage *)data;

#if 0
407 408 409 410 411
	dev_dbg(&urb->dev->dev,
		"%s - port status: port %d cts %d dcd %d dsr %d ri %d toff %d txoff %d rxen %d cr %d",
		__func__, msg->port, msg->hskia_cts, msg->gpia_dcd, msg->dsr,
		msg->ri, msg->_txOff, msg->_txXoff, msg->rxEnabled,
		msg->controlResponse);
L
Linus Torvalds 已提交
412 413 414 415 416
#endif

	/* Now do something useful with the data */


A
Alan Cox 已提交
417
	/* Check port number from message and retrieve private data */
L
Linus Torvalds 已提交
418
	if (msg->port >= serial->num_ports) {
419
		dev_dbg(&urb->dev->dev, "%s - Unexpected port number %d\n", __func__, msg->port);
L
Linus Torvalds 已提交
420 421 422 423
		goto exit;
	}
	port = serial->port[msg->port];
	p_priv = usb_get_serial_port_data(port);
A
Alan Cox 已提交
424

L
Linus Torvalds 已提交
425 426 427 428 429 430 431
	/* Update handshaking pin state information */
	old_dcd_state = p_priv->dcd_state;
	p_priv->cts_state = ((msg->hskia_cts) ? 1 : 0);
	p_priv->dsr_state = ((msg->dsr) ? 1 : 0);
	p_priv->dcd_state = ((msg->gpia_dcd) ? 1 : 0);
	p_priv->ri_state = ((msg->ri) ? 1 : 0);

A
Alan Cox 已提交
432 433 434 435 436
	if (old_dcd_state != p_priv->dcd_state) {
		tty = tty_port_tty_get(&port->port);
		if (tty && !C_CLOCAL(tty))
			tty_hangup(tty);
		tty_kref_put(tty);
L
Linus Torvalds 已提交
437
	}
A
Alan Cox 已提交
438

L
Linus Torvalds 已提交
439
	/* Resubmit urb so we continue receiving */
A
Alan Cox 已提交
440 441
	err = usb_submit_urb(urb, GFP_ATOMIC);
	if (err != 0)
442
		dev_dbg(&port->dev, "%s - resubmit read urb failed. (%d)\n", __func__, err);
L
Linus Torvalds 已提交
443 444 445
exit: ;
}

446
static void	usa26_glocont_callback(struct urb *urb)
L
Linus Torvalds 已提交
447 448 449 450
{
}


451
static void usa28_indat_callback(struct urb *urb)
L
Linus Torvalds 已提交
452
{
453
	int                     err;
L
Linus Torvalds 已提交
454 455 456 457
	struct usb_serial_port  *port;
	struct tty_struct       *tty;
	unsigned char           *data;
	struct keyspan_port_private             *p_priv;
458
	int status = urb->status;
L
Linus Torvalds 已提交
459

460
	port =  urb->context;
L
Linus Torvalds 已提交
461 462 463 464 465 466 467
	p_priv = usb_get_serial_port_data(port);
	data = urb->transfer_buffer;

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

	do {
468
		if (status) {
469 470
			dev_dbg(&urb->dev->dev, "%s - nonzero status: %x on endpoint %d.\n",
				__func__, status, usb_pipeendpoint(urb->pipe));
L
Linus Torvalds 已提交
471 472 473
			return;
		}

474
		port =  urb->context;
L
Linus Torvalds 已提交
475 476 477
		p_priv = usb_get_serial_port_data(port);
		data = urb->transfer_buffer;

478
		tty = tty_port_tty_get(&port->port);
A
Alan Cox 已提交
479
		if (tty && urb->actual_length) {
480
			tty_insert_flip_string(tty, data, urb->actual_length);
L
Linus Torvalds 已提交
481 482
			tty_flip_buffer_push(tty);
		}
A
Alan Cox 已提交
483
		tty_kref_put(tty);
L
Linus Torvalds 已提交
484 485

		/* Resubmit urb so we continue receiving */
486 487
		err = usb_submit_urb(urb, GFP_ATOMIC);
		if (err != 0)
488
			dev_dbg(&port->dev, "%s - resubmit read urb failed. (%d)\n",
489
							__func__, err);
L
Linus Torvalds 已提交
490 491 492 493 494 495
		p_priv->in_flip ^= 1;

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

496
static void	usa28_inack_callback(struct urb *urb)
L
Linus Torvalds 已提交
497 498 499
{
}

500
static void	usa28_outcont_callback(struct urb *urb)
L
Linus Torvalds 已提交
501 502 503 504
{
	struct usb_serial_port *port;
	struct keyspan_port_private *p_priv;

505
	port =  urb->context;
L
Linus Torvalds 已提交
506 507 508
	p_priv = usb_get_serial_port_data(port);

	if (p_priv->resend_cont) {
509
		dev_dbg(&port->dev, "%s - sending setup\n", __func__);
A
Alan Cox 已提交
510 511
		keyspan_usa28_send_setup(port->serial, port,
						p_priv->resend_cont - 1);
L
Linus Torvalds 已提交
512 513 514
	}
}

515
static void	usa28_instat_callback(struct urb *urb)
L
Linus Torvalds 已提交
516 517 518 519 520 521 522
{
	int					err;
	unsigned char 				*data = urb->transfer_buffer;
	struct keyspan_usa28_portStatusMessage	*msg;
	struct usb_serial			*serial;
	struct usb_serial_port			*port;
	struct keyspan_port_private	 	*p_priv;
A
Alan Cox 已提交
523
	struct tty_struct			*tty;
L
Linus Torvalds 已提交
524
	int old_dcd_state;
525
	int status = urb->status;
L
Linus Torvalds 已提交
526

527
	serial =  urb->context;
L
Linus Torvalds 已提交
528

529
	if (status) {
530
		dev_dbg(&urb->dev->dev, "%s - nonzero status: %x\n", __func__, status);
L
Linus Torvalds 已提交
531 532 533 534
		return;
	}

	if (urb->actual_length != sizeof(struct keyspan_usa28_portStatusMessage)) {
535
		dev_dbg(&urb->dev->dev, "%s - bad length %d\n", __func__, urb->actual_length);
L
Linus Torvalds 已提交
536 537 538
		goto exit;
	}

539 540 541 542 543 544
	/*
	dev_dbg(&urb->dev->dev,
	  	"%s %x %x %x %x %x %x %x %x %x %x %x %x", __func__,
		data[0], data[1], data[2], data[3], data[4], data[5],
		data[6], data[7], data[8], data[9], data[10], data[11]);
	*/
L
Linus Torvalds 已提交
545

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

A
Alan Cox 已提交
549
	/* Check port number from message and retrieve private data */
L
Linus Torvalds 已提交
550
	if (msg->port >= serial->num_ports) {
551
		dev_dbg(&urb->dev->dev, "%s - Unexpected port number %d\n", __func__, msg->port);
L
Linus Torvalds 已提交
552 553 554 555
		goto exit;
	}
	port = serial->port[msg->port];
	p_priv = usb_get_serial_port_data(port);
A
Alan Cox 已提交
556

L
Linus Torvalds 已提交
557 558 559 560 561 562 563
	/* 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);

564
	if (old_dcd_state != p_priv->dcd_state && old_dcd_state) {
A
Alan Cox 已提交
565
		tty = tty_port_tty_get(&port->port);
566
		if (tty && !C_CLOCAL(tty))
A
Alan Cox 已提交
567 568
			tty_hangup(tty);
		tty_kref_put(tty);
L
Linus Torvalds 已提交
569 570 571
	}

		/* Resubmit urb so we continue receiving */
A
Alan Cox 已提交
572 573
	err = usb_submit_urb(urb, GFP_ATOMIC);
	if (err != 0)
574
		dev_dbg(&port->dev, "%s - resubmit read urb failed. (%d)\n", __func__, err);
L
Linus Torvalds 已提交
575 576 577
exit: ;
}

578
static void	usa28_glocont_callback(struct urb *urb)
L
Linus Torvalds 已提交
579 580 581 582
{
}


583
static void	usa49_glocont_callback(struct urb *urb)
L
Linus Torvalds 已提交
584 585 586 587 588 589
{
	struct usb_serial *serial;
	struct usb_serial_port *port;
	struct keyspan_port_private *p_priv;
	int i;

590
	serial =  urb->context;
L
Linus Torvalds 已提交
591 592 593 594 595
	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) {
596
			dev_dbg(&port->dev, "%s - sending setup\n", __func__);
A
Alan Cox 已提交
597 598
			keyspan_usa49_send_setup(serial, port,
						p_priv->resend_cont - 1);
L
Linus Torvalds 已提交
599 600 601 602 603 604 605
			break;
		}
	}
}

	/* This is actually called glostat in the Keyspan
	   doco */
606
static void	usa49_instat_callback(struct urb *urb)
L
Linus Torvalds 已提交
607 608 609 610 611 612 613 614
{
	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;
615
	int status = urb->status;
L
Linus Torvalds 已提交
616

617
	serial =  urb->context;
L
Linus Torvalds 已提交
618

619
	if (status) {
620
		dev_dbg(&urb->dev->dev, "%s - nonzero status: %x\n", __func__, status);
L
Linus Torvalds 已提交
621 622 623
		return;
	}

A
Alan Cox 已提交
624 625
	if (urb->actual_length !=
			sizeof(struct keyspan_usa49_portStatusMessage)) {
626
		dev_dbg(&urb->dev->dev, "%s - bad length %d\n", __func__, urb->actual_length);
L
Linus Torvalds 已提交
627 628 629
		goto exit;
	}

630 631 632 633 634
	/*
	dev_dbg(&urb->dev->dev, "%s: %x %x %x %x %x %x %x %x %x %x %x",
		__func__, data[0], data[1], data[2], data[3], data[4],
		data[5], data[6], data[7], data[8], data[9], data[10]);
	*/
A
Alan Cox 已提交
635 636

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

A
Alan Cox 已提交
639
	/* Check port number from message and retrieve private data */
L
Linus Torvalds 已提交
640
	if (msg->portNumber >= serial->num_ports) {
641 642
		dev_dbg(&urb->dev->dev, "%s - Unexpected port number %d\n",
			__func__, msg->portNumber);
L
Linus Torvalds 已提交
643 644 645 646
		goto exit;
	}
	port = serial->port[msg->portNumber];
	p_priv = usb_get_serial_port_data(port);
A
Alan Cox 已提交
647

L
Linus Torvalds 已提交
648 649 650 651 652 653 654
	/* Update handshaking pin state information */
	old_dcd_state = p_priv->dcd_state;
	p_priv->cts_state = ((msg->cts) ? 1 : 0);
	p_priv->dsr_state = ((msg->dsr) ? 1 : 0);
	p_priv->dcd_state = ((msg->dcd) ? 1 : 0);
	p_priv->ri_state = ((msg->ri) ? 1 : 0);

A
Alan Cox 已提交
655 656 657 658 659
	if (old_dcd_state != p_priv->dcd_state && old_dcd_state) {
		struct tty_struct *tty = tty_port_tty_get(&port->port);
		if (tty && !C_CLOCAL(tty))
			tty_hangup(tty);
		tty_kref_put(tty);
L
Linus Torvalds 已提交
660 661
	}

A
Alan Cox 已提交
662 663 664
	/* Resubmit urb so we continue receiving */
	err = usb_submit_urb(urb, GFP_ATOMIC);
	if (err != 0)
665
		dev_dbg(&port->dev, "%s - resubmit read urb failed. (%d)\n", __func__, err);
L
Linus Torvalds 已提交
666 667 668
exit:	;
}

669
static void	usa49_inack_callback(struct urb *urb)
L
Linus Torvalds 已提交
670 671 672
{
}

673
static void	usa49_indat_callback(struct urb *urb)
L
Linus Torvalds 已提交
674 675 676 677 678 679
{
	int			i, err;
	int			endpoint;
	struct usb_serial_port	*port;
	struct tty_struct	*tty;
	unsigned char 		*data = urb->transfer_buffer;
680
	int status = urb->status;
L
Linus Torvalds 已提交
681 682 683

	endpoint = usb_pipeendpoint(urb->pipe);

684
	if (status) {
685 686
		dev_dbg(&urb->dev->dev, "%s - nonzero status: %x on endpoint %d.\n",
			__func__, status, endpoint);
L
Linus Torvalds 已提交
687 688 689
		return;
	}

690
	port =  urb->context;
A
Alan Cox 已提交
691
	tty = tty_port_tty_get(&port->port);
A
Alan Cox 已提交
692
	if (tty && urb->actual_length) {
L
Linus Torvalds 已提交
693 694 695
		/* 0x80 bit is error flag */
		if ((data[0] & 0x80) == 0) {
			/* no error on any byte */
696 697
			tty_insert_flip_string(tty, data + 1,
						urb->actual_length - 1);
L
Linus Torvalds 已提交
698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713
		} else {
			/* some bytes had errors, every byte has status */
			for (i = 0; i + 1 < urb->actual_length; i += 2) {
				int stat = data[i], flag = 0;
				if (stat & RXERROR_OVERRUN)
					flag |= TTY_OVERRUN;
				if (stat & RXERROR_FRAMING)
					flag |= TTY_FRAME;
				if (stat & RXERROR_PARITY)
					flag |= TTY_PARITY;
				/* XXX should handle break (0x10) */
				tty_insert_flip_char(tty, data[i+1], flag);
			}
		}
		tty_flip_buffer_push(tty);
	}
A
Alan Cox 已提交
714
	tty_kref_put(tty);
A
Alan Cox 已提交
715 716

	/* Resubmit urb so we continue receiving */
717 718
	err = usb_submit_urb(urb, GFP_ATOMIC);
	if (err != 0)
719
		dev_dbg(&port->dev, "%s - resubmit read urb failed. (%d)\n", __func__, err);
L
Linus Torvalds 已提交
720 721
}

722 723 724 725 726 727 728
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;
729
	int status = urb->status;
730 731 732

	serial = urb->context;

733
	if (status) {
734
		dev_dbg(&urb->dev->dev, "%s - nonzero status: %x\n", __func__, status);
735 736 737 738 739 740 741 742 743 744 745 746
		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) {
747
				dev_dbg(&urb->dev->dev, "%s - Unexpected port number %d\n",
748
					__func__, data[i]);
749 750 751
				return;
			}
			port = serial->port[data[i++]];
A
Alan Cox 已提交
752
			tty = tty_port_tty_get(&port->port);
753 754 755 756 757 758 759
			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)
760
					tty_insert_flip_char(tty, data[i++], 0);
761 762 763 764 765 766 767 768 769 770 771 772 773
			} 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) */
774
					tty_insert_flip_char(tty,
775 776 777 778
							data[i+1], flag);
					i += 2;
				}
			}
779
			tty_flip_buffer_push(tty);
A
Alan Cox 已提交
780
			tty_kref_put(tty);
781 782 783 784 785 786
		}
	}

	/* Resubmit urb so we continue receiving */
	err = usb_submit_urb(urb, GFP_ATOMIC);
	if (err != 0)
787
		dev_dbg(&urb->dev->dev, "%s - resubmit read urb failed. (%d)\n", __func__, err);
788 789
}

L
Linus Torvalds 已提交
790
/* not used, usa-49 doesn't have per-port control endpoints */
791
static void usa49_outcont_callback(struct urb *urb)
L
Linus Torvalds 已提交
792 793 794
{
}

795
static void usa90_indat_callback(struct urb *urb)
L
Linus Torvalds 已提交
796 797 798 799 800 801 802
{
	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;
803
	int status = urb->status;
L
Linus Torvalds 已提交
804 805 806

	endpoint = usb_pipeendpoint(urb->pipe);

807
	if (status) {
808
		dev_dbg(&urb->dev->dev, "%s - nonzero status: %x on endpoint %d.\n",
809
		    __func__, status, endpoint);
L
Linus Torvalds 已提交
810 811 812
		return;
	}

813
	port =  urb->context;
L
Linus Torvalds 已提交
814 815 816
	p_priv = usb_get_serial_port_data(port);

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

821 822 823
		if (p_priv->baud > 57600)
			tty_insert_flip_string(tty, data, urb->actual_length);
		else {
L
Linus Torvalds 已提交
824 825
			/* 0x80 bit is error flag */
			if ((data[0] & 0x80) == 0) {
A
Alan Cox 已提交
826 827
				/* no errors on individual bytes, only
				   possible overrun err*/
L
Linus Torvalds 已提交
828
				if (data[0] & RXERROR_OVERRUN)
A
Alan Cox 已提交
829 830 831 832 833 834 835
					err = TTY_OVERRUN;
				else
					err = 0;
				for (i = 1; i < urb->actual_length ; ++i)
					tty_insert_flip_char(tty, data[i],
									err);
			}  else {
L
Linus Torvalds 已提交
836
			/* some bytes had errors, every byte has status */
837
				dev_dbg(&port->dev, "%s - RX error!!!!\n", __func__);
L
Linus Torvalds 已提交
838 839 840 841 842 843 844 845 846
				for (i = 0; i + 1 < urb->actual_length; i += 2) {
					int stat = data[i], flag = 0;
					if (stat & RXERROR_OVERRUN)
						flag |= TTY_OVERRUN;
					if (stat & RXERROR_FRAMING)
						flag |= TTY_FRAME;
					if (stat & RXERROR_PARITY)
						flag |= TTY_PARITY;
					/* XXX should handle break (0x10) */
A
Alan Cox 已提交
847 848
					tty_insert_flip_char(tty, data[i+1],
									flag);
L
Linus Torvalds 已提交
849 850 851 852
				}
			}
		}
		tty_flip_buffer_push(tty);
A
Alan Cox 已提交
853
		tty_kref_put(tty);
L
Linus Torvalds 已提交
854
	}
A
Alan Cox 已提交
855

L
Linus Torvalds 已提交
856
	/* Resubmit urb so we continue receiving */
857 858
	err = usb_submit_urb(urb, GFP_ATOMIC);
	if (err != 0)
859
		dev_dbg(&port->dev, "%s - resubmit read urb failed. (%d)\n", __func__, err);
L
Linus Torvalds 已提交
860 861 862
}


863
static void	usa90_instat_callback(struct urb *urb)
L
Linus Torvalds 已提交
864 865 866 867 868 869
{
	unsigned char 				*data = urb->transfer_buffer;
	struct keyspan_usa90_portStatusMessage	*msg;
	struct usb_serial			*serial;
	struct usb_serial_port			*port;
	struct keyspan_port_private	 	*p_priv;
A
Alan Cox 已提交
870
	struct tty_struct			*tty;
L
Linus Torvalds 已提交
871
	int old_dcd_state, err;
872
	int status = urb->status;
L
Linus Torvalds 已提交
873

874
	serial =  urb->context;
L
Linus Torvalds 已提交
875

876
	if (status) {
877
		dev_dbg(&urb->dev->dev, "%s - nonzero status: %x\n", __func__, status);
L
Linus Torvalds 已提交
878 879 880
		return;
	}
	if (urb->actual_length < 14) {
881
		dev_dbg(&urb->dev->dev, "%s - %d byte report??\n", __func__, urb->actual_length);
L
Linus Torvalds 已提交
882 883 884 885 886 887 888 889 890
		goto exit;
	}

	msg = (struct keyspan_usa90_portStatusMessage *)data;

	/* Now do something useful with the data */

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

L
Linus Torvalds 已提交
892 893 894 895 896 897 898
	/* Update handshaking pin state information */
	old_dcd_state = p_priv->dcd_state;
	p_priv->cts_state = ((msg->cts) ? 1 : 0);
	p_priv->dsr_state = ((msg->dsr) ? 1 : 0);
	p_priv->dcd_state = ((msg->dcd) ? 1 : 0);
	p_priv->ri_state = ((msg->ri) ? 1 : 0);

A
Alan Cox 已提交
899 900 901 902 903
	if (old_dcd_state != p_priv->dcd_state && old_dcd_state) {
		tty = tty_port_tty_get(&port->port);
		if (tty && !C_CLOCAL(tty))
			tty_hangup(tty);
		tty_kref_put(tty);
L
Linus Torvalds 已提交
904
	}
A
Alan Cox 已提交
905

L
Linus Torvalds 已提交
906
	/* Resubmit urb so we continue receiving */
A
Alan Cox 已提交
907 908
	err = usb_submit_urb(urb, GFP_ATOMIC);
	if (err != 0)
909
		dev_dbg(&port->dev, "%s - resubmit read urb failed. (%d)\n", __func__, err);
L
Linus Torvalds 已提交
910 911 912 913
exit:
	;
}

914
static void	usa90_outcont_callback(struct urb *urb)
L
Linus Torvalds 已提交
915 916 917 918
{
	struct usb_serial_port *port;
	struct keyspan_port_private *p_priv;

919
	port =  urb->context;
L
Linus Torvalds 已提交
920 921 922
	p_priv = usb_get_serial_port_data(port);

	if (p_priv->resend_cont) {
923
		dev_dbg(&urb->dev->dev, "%s - sending setup\n", __func__);
A
Alan Cox 已提交
924 925
		keyspan_usa90_send_setup(port->serial, port,
						p_priv->resend_cont - 1);
L
Linus Torvalds 已提交
926 927 928
	}
}

929 930 931 932 933 934 935 936 937 938
/* 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;
939
	int status = urb->status;
940 941 942

	serial = urb->context;

943
	if (status) {
944
		dev_dbg(&urb->dev->dev, "%s - nonzero status: %x\n", __func__, status);
945 946 947
		return;
	}

A
Alan Cox 已提交
948 949
	if (urb->actual_length !=
			sizeof(struct keyspan_usa67_portStatusMessage)) {
950
		dev_dbg(&urb->dev->dev, "%s - bad length %d\n", __func__, urb->actual_length);
951 952 953 954 955 956 957 958 959
		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) {
960
		dev_dbg(&urb->dev->dev, "%s - Unexpected port number %d\n", __func__, msg->port);
961 962 963 964 965 966 967 968 969 970 971
		return;
	}

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

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

A
Alan Cox 已提交
972 973 974 975 976
	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);
977 978 979 980 981
	}

	/* Resubmit urb so we continue receiving */
	err = usb_submit_urb(urb, GFP_ATOMIC);
	if (err != 0)
982
		dev_dbg(&port->dev, "%s - resubmit read urb failed. (%d)\n", __func__, err);
983 984 985 986 987 988 989 990 991 992 993 994 995 996 997
}

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) {
998
			dev_dbg(&port->dev, "%s - sending setup\n", __func__);
999 1000 1001 1002 1003 1004 1005
			keyspan_usa67_send_setup(serial, port,
						p_priv->resend_cont - 1);
			break;
		}
	}
}

A
Alan Cox 已提交
1006
static int keyspan_write_room(struct tty_struct *tty)
L
Linus Torvalds 已提交
1007
{
A
Alan Cox 已提交
1008
	struct usb_serial_port *port = tty->driver_data;
L
Linus Torvalds 已提交
1009 1010 1011 1012 1013 1014 1015 1016 1017
	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;

1018
	/* FIXME: locking */
L
Linus Torvalds 已提交
1019
	if (d_details->msg_format == msg_usa90)
A
Alan Cox 已提交
1020
		data_len = 64;
L
Linus Torvalds 已提交
1021 1022 1023 1024 1025 1026
	else
		data_len = 63;

	flip = p_priv->out_flip;

	/* Check both endpoints to see if any are available. */
A
Alan Cox 已提交
1027 1028
	this_urb = p_priv->out_urbs[flip];
	if (this_urb != NULL) {
L
Linus Torvalds 已提交
1029
		if (this_urb->status != -EINPROGRESS)
A
Alan Cox 已提交
1030 1031 1032 1033
			return data_len;
		flip = (flip + 1) & d_details->outdat_endp_flip;
		this_urb = p_priv->out_urbs[flip];
		if (this_urb != NULL) {
L
Linus Torvalds 已提交
1034
			if (this_urb->status != -EINPROGRESS)
A
Alan Cox 已提交
1035 1036
				return data_len;
		}
L
Linus Torvalds 已提交
1037
	}
1038
	return 0;
L
Linus Torvalds 已提交
1039 1040 1041
}


1042
static int keyspan_open(struct tty_struct *tty, struct usb_serial_port *port)
L
Linus Torvalds 已提交
1043
{
1044
	struct keyspan_port_private 	*p_priv;
L
Linus Torvalds 已提交
1045 1046
	const struct keyspan_device_details	*d_details;
	int				i, err;
1047
	int				baud_rate, device_port;
L
Linus Torvalds 已提交
1048
	struct urb			*urb;
A
Alan Cox 已提交
1049
	unsigned int			cflag = 0;
L
Linus Torvalds 已提交
1050 1051 1052

	p_priv = usb_get_serial_port_data(port);
	d_details = p_priv->device_details;
B
Borislav Petkov 已提交
1053

L
Linus Torvalds 已提交
1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067
	/* Set some sane defaults */
	p_priv->rts_state = 1;
	p_priv->dtr_state = 1;
	p_priv->baud = 9600;

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

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

	/* Reset low level data toggle and start reading from endpoints */
	for (i = 0; i < 2; i++) {
A
Alan Cox 已提交
1068 1069
		urb = p_priv->in_urbs[i];
		if (urb == NULL)
L
Linus Torvalds 已提交
1070 1071
			continue;

A
Alan Cox 已提交
1072 1073
		/* make sure endpoint data toggle is synchronized
		   with the device */
L
Linus Torvalds 已提交
1074
		usb_clear_halt(urb->dev, urb->pipe);
A
Alan Cox 已提交
1075 1076
		err = usb_submit_urb(urb, GFP_KERNEL);
		if (err != 0)
1077
			dev_dbg(&port->dev, "%s - submit urb %d failed (%d)\n", __func__, i, err);
L
Linus Torvalds 已提交
1078 1079 1080 1081
	}

	/* Reset low level data toggle on out endpoints */
	for (i = 0; i < 2; i++) {
A
Alan Cox 已提交
1082 1083
		urb = p_priv->out_urbs[i];
		if (urb == NULL)
L
Linus Torvalds 已提交
1084
			continue;
A
Alan Cox 已提交
1085 1086
		/* usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe),
						usb_pipeout(urb->pipe), 0); */
L
Linus Torvalds 已提交
1087 1088
	}

1089 1090 1091 1092
	/* get the terminal config for the setup message now so we don't
	 * need to send 2 of them */

	device_port = port->number - port->serial->minor;
A
Alan Cox 已提交
1093 1094 1095 1096 1097 1098 1099
	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
1100
		    && d_details->calculate_baud_rate(port, baud_rate, d_details->baudclk,
A
Alan Cox 已提交
1101 1102 1103
					NULL, NULL, NULL, device_port) == KEYSPAN_BAUD_RATE_OK) {
			p_priv->baud = baud_rate;
		}
1104 1105 1106
	}
	/* set CTS/RTS handshake etc. */
	p_priv->cflag = cflag;
1107
	p_priv->flow_control = (cflag & CRTSCTS) ? flow_cts : flow_none;
1108 1109

	keyspan_send_setup(port, 1);
A
Alan Cox 已提交
1110 1111
	/* mdelay(100); */
	/* keyspan_set_termios(port, NULL); */
1112

1113
	return 0;
L
Linus Torvalds 已提交
1114 1115 1116 1117
}

static inline void stop_urb(struct urb *urb)
{
1118
	if (urb && urb->status == -EINPROGRESS)
L
Linus Torvalds 已提交
1119 1120 1121
		usb_kill_urb(urb);
}

1122 1123 1124 1125 1126 1127 1128 1129 1130 1131
static void keyspan_dtr_rts(struct usb_serial_port *port, int on)
{
	struct keyspan_port_private *p_priv = usb_get_serial_port_data(port);

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

static void keyspan_close(struct usb_serial_port *port)
L
Linus Torvalds 已提交
1132 1133 1134 1135 1136 1137
{
	int			i;
	struct usb_serial	*serial = port->serial;
	struct keyspan_port_private 	*p_priv;

	p_priv = usb_get_serial_port_data(port);
A
Alan Cox 已提交
1138

L
Linus Torvalds 已提交
1139 1140
	p_priv->rts_state = 0;
	p_priv->dtr_state = 0;
A
Alan Cox 已提交
1141

L
Linus Torvalds 已提交
1142 1143 1144 1145
	if (serial->dev) {
		keyspan_send_setup(port, 2);
		/* pilot-xfer seems to work best with this delay */
		mdelay(100);
A
Alan Cox 已提交
1146
		/* keyspan_set_termios(port, NULL); */
L
Linus Torvalds 已提交
1147 1148 1149
	}

	/*while (p_priv->outcont_urb->status == -EINPROGRESS) {
1150
		dev_dbg(&port->dev, "%s - urb in progress\n", __func__);
L
Linus Torvalds 已提交
1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166
	}*/

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

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

A
Alan Cox 已提交
1167 1168
/* download the firmware to a pre-renumeration device */
static int keyspan_fake_startup(struct usb_serial *serial)
L
Linus Torvalds 已提交
1169 1170
{
	int 				response;
1171
	const struct ihex_binrec 	*record;
L
Linus Torvalds 已提交
1172
	char				*fw_name;
1173
	const struct firmware		*fw;
L
Linus Torvalds 已提交
1174

1175 1176 1177
	dev_dbg(&serial->dev->dev, "Keyspan startup version %04x product %04x\n",
		le16_to_cpu(serial->dev->descriptor.bcdDevice),
		le16_to_cpu(serial->dev->descriptor.idProduct));
A
Alan Cox 已提交
1178 1179 1180

	if ((le16_to_cpu(serial->dev->descriptor.bcdDevice) & 0x8000)
								!= 0x8000) {
1181
		dev_dbg(&serial->dev->dev, "Firmware already loaded.  Quitting.\n");
A
Alan Cox 已提交
1182
		return 1;
L
Linus Torvalds 已提交
1183 1184 1185 1186 1187
	}

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

	case keyspan_usa28x_pre_product_id:
1192
		fw_name = "keyspan/usa28x.fw";
L
Linus Torvalds 已提交
1193 1194 1195
		break;

	case keyspan_usa28xa_pre_product_id:
1196
		fw_name = "keyspan/usa28xa.fw";
L
Linus Torvalds 已提交
1197 1198 1199
		break;

	case keyspan_usa28xb_pre_product_id:
1200
		fw_name = "keyspan/usa28xb.fw";
L
Linus Torvalds 已提交
1201 1202 1203
		break;

	case keyspan_usa19_pre_product_id:
1204
		fw_name = "keyspan/usa19.fw";
L
Linus Torvalds 已提交
1205
		break;
A
Alan Cox 已提交
1206

L
Linus Torvalds 已提交
1207
	case keyspan_usa19qi_pre_product_id:
1208
		fw_name = "keyspan/usa19qi.fw";
L
Linus Torvalds 已提交
1209
		break;
A
Alan Cox 已提交
1210

L
Linus Torvalds 已提交
1211
	case keyspan_mpr_pre_product_id:
1212
		fw_name = "keyspan/mpr.fw";
L
Linus Torvalds 已提交
1213 1214 1215
		break;

	case keyspan_usa19qw_pre_product_id:
1216
		fw_name = "keyspan/usa19qw.fw";
L
Linus Torvalds 已提交
1217
		break;
A
Alan Cox 已提交
1218

L
Linus Torvalds 已提交
1219
	case keyspan_usa18x_pre_product_id:
1220
		fw_name = "keyspan/usa18x.fw";
L
Linus Torvalds 已提交
1221
		break;
A
Alan Cox 已提交
1222

L
Linus Torvalds 已提交
1223
	case keyspan_usa19w_pre_product_id:
1224
		fw_name = "keyspan/usa19w.fw";
L
Linus Torvalds 已提交
1225
		break;
A
Alan Cox 已提交
1226

L
Linus Torvalds 已提交
1227
	case keyspan_usa49w_pre_product_id:
1228
		fw_name = "keyspan/usa49w.fw";
L
Linus Torvalds 已提交
1229 1230 1231
		break;

	case keyspan_usa49wlc_pre_product_id:
1232
		fw_name = "keyspan/usa49wlc.fw";
L
Linus Torvalds 已提交
1233 1234 1235
		break;

	default:
1236 1237 1238
		dev_err(&serial->dev->dev, "Unknown product ID (%04x)\n",
			le16_to_cpu(serial->dev->descriptor.idProduct));
		return 1;
L
Linus Torvalds 已提交
1239 1240
	}

1241
	if (request_ihex_firmware(&fw, fw_name, &serial->dev->dev)) {
L
Linus Torvalds 已提交
1242
		dev_err(&serial->dev->dev, "Required keyspan firmware image (%s) unavailable.\n", fw_name);
1243
		return 1;
L
Linus Torvalds 已提交
1244 1245
	}

1246
	dev_dbg(&serial->dev->dev, "Uploading Keyspan %s firmware.\n", fw_name);
L
Linus Torvalds 已提交
1247 1248

		/* download the firmware image */
1249
	response = ezusb_fx1_set_reset(serial->dev, 1);
L
Linus Torvalds 已提交
1250

1251 1252 1253
	record = (const struct ihex_binrec *)fw->data;

	while (record) {
1254
		response = ezusb_writememory(serial->dev, be32_to_cpu(record->addr),
L
Linus Torvalds 已提交
1255
					     (unsigned char *)record->data,
1256
					     be16_to_cpu(record->len), 0xa0);
L
Linus Torvalds 已提交
1257
		if (response < 0) {
A
Alan Cox 已提交
1258
			dev_err(&serial->dev->dev, "ezusb_writememory failed for Keyspan firmware (%d %04X %p %d)\n",
1259 1260
				response, be32_to_cpu(record->addr),
				record->data, be16_to_cpu(record->len));
L
Linus Torvalds 已提交
1261 1262
			break;
		}
1263
		record = ihex_next_binrec(record);
L
Linus Torvalds 已提交
1264
	}
1265
	release_firmware(fw);
L
Linus Torvalds 已提交
1266 1267
		/* bring device out of reset. Renumeration will occur in a
		   moment and the new device will bind to the real driver */
1268
	response = ezusb_fx1_set_reset(serial->dev, 0);
L
Linus Torvalds 已提交
1269 1270

	/* we don't want this device to have a driver assigned to it. */
A
Alan Cox 已提交
1271
	return 1;
L
Linus Torvalds 已提交
1272 1273 1274
}

/* Helper functions used by keyspan_setup_urbs */
1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292
static struct usb_endpoint_descriptor const *find_ep(struct usb_serial const *serial,
						     int endpoint)
{
	struct usb_host_interface *iface_desc;
	struct usb_endpoint_descriptor *ep;
	int i;

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

A
Alan Cox 已提交
1293
static struct urb *keyspan_setup_urb(struct usb_serial *serial, int endpoint,
L
Linus Torvalds 已提交
1294
				      int dir, void *ctx, char *buf, int len,
1295
				      void (*callback)(struct urb *))
L
Linus Torvalds 已提交
1296 1297
{
	struct urb *urb;
1298 1299
	struct usb_endpoint_descriptor const *ep_desc;
	char const *ep_type_name;
L
Linus Torvalds 已提交
1300 1301 1302 1303

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

1304
	dev_dbg(&serial->interface->dev, "%s - alloc for endpoint %d.\n", __func__, endpoint);
L
Linus Torvalds 已提交
1305 1306
	urb = usb_alloc_urb(0, GFP_KERNEL);		/* No ISO */
	if (urb == NULL) {
1307
		dev_dbg(&serial->interface->dev, "%s - alloc for endpoint %d failed.\n", __func__, endpoint);
L
Linus Torvalds 已提交
1308 1309 1310
		return NULL;
	}

1311 1312 1313 1314 1315
	if (endpoint == 0) {
		/* control EP filled in when used */
		return urb;
	}

1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334
	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",
1335
			 usb_endpoint_type(ep_desc));
1336 1337 1338
		usb_free_urb(urb);
		return NULL;
	}
L
Linus Torvalds 已提交
1339

1340
	dev_dbg(&serial->interface->dev, "%s - using urb %p for %s endpoint %x\n",
1341
	    __func__, urb, ep_type_name, endpoint);
L
Linus Torvalds 已提交
1342 1343 1344 1345
	return urb;
}

static struct callbacks {
1346 1347 1348 1349 1350 1351
	void	(*instat_callback)(struct urb *);
	void	(*glocont_callback)(struct urb *);
	void	(*indat_callback)(struct urb *);
	void	(*outdat_callback)(struct urb *);
	void	(*inack_callback)(struct urb *);
	void	(*outcont_callback)(struct urb *);
L
Linus Torvalds 已提交
1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379
} keyspan_callbacks[] = {
	{
		/* msg_usa26 callbacks */
		.instat_callback =	usa26_instat_callback,
		.glocont_callback =	usa26_glocont_callback,
		.indat_callback =	usa26_indat_callback,
		.outdat_callback =	usa2x_outdat_callback,
		.inack_callback =	usa26_inack_callback,
		.outcont_callback =	usa26_outcont_callback,
	}, {
		/* msg_usa28 callbacks */
		.instat_callback =	usa28_instat_callback,
		.glocont_callback =	usa28_glocont_callback,
		.indat_callback =	usa28_indat_callback,
		.outdat_callback =	usa2x_outdat_callback,
		.inack_callback =	usa28_inack_callback,
		.outcont_callback =	usa28_outcont_callback,
	}, {
		/* msg_usa49 callbacks */
		.instat_callback =	usa49_instat_callback,
		.glocont_callback =	usa49_glocont_callback,
		.indat_callback =	usa49_indat_callback,
		.outdat_callback =	usa2x_outdat_callback,
		.inack_callback =	usa49_inack_callback,
		.outcont_callback =	usa49_outcont_callback,
	}, {
		/* msg_usa90 callbacks */
		.instat_callback =	usa90_instat_callback,
A
Alan Cox 已提交
1380
		.glocont_callback =	usa28_glocont_callback,
L
Linus Torvalds 已提交
1381 1382 1383 1384
		.indat_callback =	usa90_indat_callback,
		.outdat_callback =	usa2x_outdat_callback,
		.inack_callback =	usa28_inack_callback,
		.outcont_callback =	usa90_outcont_callback,
1385 1386 1387 1388 1389 1390 1391 1392
	}, {
		/* msg_usa67 callbacks */
		.instat_callback =	usa67_instat_callback,
		.glocont_callback =	usa67_glocont_callback,
		.indat_callback =	usa26_indat_callback,
		.outdat_callback =	usa2x_outdat_callback,
		.inack_callback =	usa26_inack_callback,
		.outcont_callback =	usa26_outcont_callback,
L
Linus Torvalds 已提交
1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410
	}
};

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

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

A
Alan Cox 已提交
1414 1415
	/* Allocate and set up urbs for each one that is in use,
	   starting with instat endpoints */
L
Linus Torvalds 已提交
1416 1417 1418 1419 1420
	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);

1421 1422 1423 1424 1425
	s_priv->indat_urb = keyspan_setup_urb
		(serial, d_details->indat_endpoint, USB_DIR_IN,
		 serial, s_priv->indat_buf, INDAT49W_BUFLEN,
		 usa49wg_indat_callback);

L
Linus Torvalds 已提交
1426 1427 1428 1429 1430
	s_priv->glocont_urb = keyspan_setup_urb
		(serial, d_details->glocont_endpoint, USB_DIR_OUT,
		 serial, s_priv->glocont_buf, GLOCONT_BUFLEN,
		 cback->glocont_callback);

A
Alan Cox 已提交
1431 1432
	/* Setup endpoints for each port specific thing */
	for (i = 0; i < d_details->num_ports; i++) {
L
Linus Torvalds 已提交
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 1463 1464 1465 1466 1467
		port = serial->port[i];
		p_priv = usb_get_serial_port_data(port);

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

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

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

		/* outcont endpoint */
		p_priv->outcont_urb = keyspan_setup_urb
			(serial, d_details->outcont_endpoints[i], USB_DIR_OUT,
			 port, p_priv->outcont_buffer, 64,
			 cback->outcont_callback);
A
Alan Cox 已提交
1468
	}
L
Linus Torvalds 已提交
1469 1470 1471
}

/* usa19 function doesn't require prescaler */
1472 1473
static int keyspan_usa19_calc_baud(struct usb_serial_port *port,
				   u32 baud_rate, u32 baudclk, u8 *rate_hi,
L
Linus Torvalds 已提交
1474 1475 1476
				   u8 *rate_low, u8 *prescaler, int portnum)
{
	u32 	b16,	/* baud rate times 16 (actual rate used internally) */
A
Alan Cox 已提交
1477
		div,	/* divisor */
L
Linus Torvalds 已提交
1478 1479
		cnt;	/* inverse of divisor (programmed into 8051) */

1480
	dev_dbg(&port->dev, "%s - %d.\n", __func__, baud_rate);
A
Alan Cox 已提交
1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495

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

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

A
Alan Cox 已提交
1498 1499
	if (div > 0xffff)
		return KEYSPAN_INVALID_BAUD_RATE;
L
Linus Torvalds 已提交
1500

A
Alan Cox 已提交
1501 1502
	/* return the counter values if non-null */
	if (rate_low)
L
Linus Torvalds 已提交
1503
		*rate_low = (u8) (cnt & 0xff);
A
Alan Cox 已提交
1504
	if (rate_hi)
L
Linus Torvalds 已提交
1505
		*rate_hi = (u8) ((cnt >> 8) & 0xff);
A
Alan Cox 已提交
1506
	if (rate_low && rate_hi)
1507
		dev_dbg(&port->dev, "%s - %d %02x %02x.\n",
A
Alan Cox 已提交
1508 1509
				__func__, baud_rate, *rate_hi, *rate_low);
	return KEYSPAN_BAUD_RATE_OK;
L
Linus Torvalds 已提交
1510 1511 1512
}

/* usa19hs function doesn't require prescaler */
1513 1514 1515
static int keyspan_usa19hs_calc_baud(struct usb_serial_port *port,
				     u32 baud_rate, u32 baudclk, u8 *rate_hi,
				     u8 *rate_low, u8 *prescaler, int portnum)
L
Linus Torvalds 已提交
1516 1517
{
	u32 	b16,	/* baud rate times 16 (actual rate used internally) */
A
Alan Cox 已提交
1518
			div;	/* divisor */
L
Linus Torvalds 已提交
1519

1520
	dev_dbg(&port->dev, "%s - %d.\n", __func__, baud_rate);
L
Linus Torvalds 已提交
1521

A
Alan Cox 已提交
1522 1523 1524 1525
	/* prevent divide by zero...  */
	b16 = baud_rate * 16L;
	if (b16 == 0)
		return KEYSPAN_INVALID_BAUD_RATE;
L
Linus Torvalds 已提交
1526

A
Alan Cox 已提交
1527 1528 1529 1530
	/* calculate the divisor */
	div = baudclk / b16;
	if (div == 0)
		return KEYSPAN_INVALID_BAUD_RATE;
L
Linus Torvalds 已提交
1531

A
Alan Cox 已提交
1532 1533
	if (div > 0xffff)
		return KEYSPAN_INVALID_BAUD_RATE;
L
Linus Torvalds 已提交
1534

A
Alan Cox 已提交
1535 1536
	/* return the counter values if non-null */
	if (rate_low)
L
Linus Torvalds 已提交
1537
		*rate_low = (u8) (div & 0xff);
A
Alan Cox 已提交
1538 1539

	if (rate_hi)
L
Linus Torvalds 已提交
1540
		*rate_hi = (u8) ((div >> 8) & 0xff);
A
Alan Cox 已提交
1541 1542

	if (rate_low && rate_hi)
1543
		dev_dbg(&port->dev, "%s - %d %02x %02x.\n",
A
Alan Cox 已提交
1544 1545 1546
			__func__, baud_rate, *rate_hi, *rate_low);

	return KEYSPAN_BAUD_RATE_OK;
L
Linus Torvalds 已提交
1547 1548
}

1549 1550
static int keyspan_usa19w_calc_baud(struct usb_serial_port *port,
				    u32 baud_rate, u32 baudclk, u8 *rate_hi,
L
Linus Torvalds 已提交
1551 1552 1553 1554
				    u8 *rate_low, u8 *prescaler, int portnum)
{
	u32 	b16,	/* baud rate times 16 (actual rate used internally) */
		clk,	/* clock with 13/8 prescaler */
A
Alan Cox 已提交
1555
		div,	/* divisor using 13/8 prescaler */
L
Linus Torvalds 已提交
1556 1557 1558 1559 1560 1561
		res,	/* resulting baud rate using 13/8 prescaler */
		diff,	/* error using 13/8 prescaler */
		smallest_diff;
	u8	best_prescaler;
	int	i;

1562
	dev_dbg(&port->dev, "%s - %d.\n", __func__, baud_rate);
L
Linus Torvalds 已提交
1563

A
Alan Cox 已提交
1564 1565 1566 1567
	/* prevent divide by zero */
	b16 = baud_rate * 16L;
	if (b16 == 0)
		return KEYSPAN_INVALID_BAUD_RATE;
L
Linus Torvalds 已提交
1568

A
Alan Cox 已提交
1569 1570 1571 1572
	/* Calculate prescaler by trying them all and looking
	   for best fit */

	/* start with largest possible difference */
L
Linus Torvalds 已提交
1573 1574 1575 1576 1577
	smallest_diff = 0xffffffff;

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

A
Alan Cox 已提交
1578
	for (i = 8; i <= 0xff; ++i) {
L
Linus Torvalds 已提交
1579
		clk = (baudclk * 8) / (u32) i;
A
Alan Cox 已提交
1580 1581 1582

		div = clk / b16;
		if (div == 0)
L
Linus Torvalds 已提交
1583 1584 1585
			continue;

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

A
Alan Cox 已提交
1588
		if (diff < smallest_diff) {
L
Linus Torvalds 已提交
1589 1590 1591 1592 1593
			best_prescaler = i;
			smallest_diff = diff;
		}
	}

A
Alan Cox 已提交
1594 1595
	if (best_prescaler == 0)
		return KEYSPAN_INVALID_BAUD_RATE;
L
Linus Torvalds 已提交
1596 1597 1598 1599

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

A
Alan Cox 已提交
1600 1601
	/* return the divisor and prescaler if non-null */
	if (rate_low)
L
Linus Torvalds 已提交
1602
		*rate_low = (u8) (div & 0xff);
A
Alan Cox 已提交
1603
	if (rate_hi)
L
Linus Torvalds 已提交
1604 1605 1606
		*rate_hi = (u8) ((div >> 8) & 0xff);
	if (prescaler) {
		*prescaler = best_prescaler;
1607
		/*  dev_dbg(&port->dev, "%s - %d %d\n", __func__, *prescaler, div); */
L
Linus Torvalds 已提交
1608
	}
A
Alan Cox 已提交
1609
	return KEYSPAN_BAUD_RATE_OK;
L
Linus Torvalds 已提交
1610 1611 1612
}

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

1621
	dev_dbg(&port->dev, "%s - %d.\n", __func__, baud_rate);
L
Linus Torvalds 已提交
1622 1623

		/* prevent divide by zero */
A
Alan Cox 已提交
1624 1625 1626 1627 1628 1629 1630 1631 1632
	b16 = baud_rate * 16L;
	if (b16 == 0)
		return KEYSPAN_INVALID_BAUD_RATE;

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

A
Alan Cox 已提交
1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645
	/* check for out of range, based on portnum,
	   and return result */
	if (portnum == 0) {
		if (div > 0xffff)
			return KEYSPAN_INVALID_BAUD_RATE;
	} else {
		if (portnum == 1) {
			if (div > 0xff)
				return KEYSPAN_INVALID_BAUD_RATE;
		} else
			return KEYSPAN_INVALID_BAUD_RATE;
L
Linus Torvalds 已提交
1646 1647 1648 1649
	}

		/* return the counter values if not NULL
		   (port 1 will ignore retHi) */
A
Alan Cox 已提交
1650
	if (rate_low)
L
Linus Torvalds 已提交
1651
		*rate_low = (u8) (cnt & 0xff);
A
Alan Cox 已提交
1652
	if (rate_hi)
L
Linus Torvalds 已提交
1653
		*rate_hi = (u8) ((cnt >> 8) & 0xff);
1654
	dev_dbg(&port->dev, "%s - %d OK.\n", __func__, baud_rate);
A
Alan Cox 已提交
1655
	return KEYSPAN_BAUD_RATE_OK;
L
Linus Torvalds 已提交
1656 1657 1658 1659 1660 1661
}

static int keyspan_usa26_send_setup(struct usb_serial *serial,
				    struct usb_serial_port *port,
				    int reset_port)
{
A
Alan Cox 已提交
1662
	struct keyspan_usa26_portControlMessage	msg;
L
Linus Torvalds 已提交
1663 1664 1665 1666 1667 1668 1669
	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;

1670
	dev_dbg(&port->dev, "%s reset=%d\n", __func__, reset_port);
L
Linus Torvalds 已提交
1671 1672 1673 1674 1675 1676 1677 1678 1679

	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;

1680
	dev_dbg(&port->dev, "%s - endpoint %d\n", __func__, usb_pipeendpoint(this_urb->pipe));
L
Linus Torvalds 已提交
1681 1682 1683

		/* Make sure we have an urb then send the message */
	if (this_urb == NULL) {
1684
		dev_dbg(&port->dev, "%s - oops no urb.\n", __func__);
L
Linus Torvalds 已提交
1685 1686 1687 1688
		return -1;
	}

	/* Save reset port val for resend.
1689 1690
	   Don't overwrite resend for open/close condition. */
	if ((reset_port + 1) > p_priv->resend_cont)
L
Linus Torvalds 已提交
1691 1692
		p_priv->resend_cont = reset_port + 1;
	if (this_urb->status == -EINPROGRESS) {
1693
		/*  dev_dbg(&port->dev, "%s - already writing\n", __func__); */
L
Linus Torvalds 已提交
1694
		mdelay(5);
A
Alan Cox 已提交
1695
		return -1;
L
Linus Torvalds 已提交
1696 1697
	}

A
Alan Cox 已提交
1698 1699 1700
	memset(&msg, 0, sizeof(struct keyspan_usa26_portControlMessage));

	/* Only set baud rate if it's changed */
L
Linus Torvalds 已提交
1701 1702 1703
	if (p_priv->old_baud != p_priv->baud) {
		p_priv->old_baud = p_priv->baud;
		msg.setClocking = 0xff;
1704 1705 1706 1707 1708
		if (d_details->calculate_baud_rate(port, p_priv->baud, d_details->baudclk,
						   &msg.baudHi, &msg.baudLo, &msg.prescaler,
						   device_port) == KEYSPAN_INVALID_BAUD_RATE) {
			dev_dbg(&port->dev, "%s - Invalid baud rate %d requested, using 9600.\n",
				__func__, p_priv->baud);
L
Linus Torvalds 已提交
1709 1710 1711 1712 1713 1714 1715
			msg.baudLo = 0;
			msg.baudHi = 125;	/* Values for 9600 baud */
			msg.prescaler = 10;
		}
		msg.setPrescaler = 0xff;
	}

1716
	msg.lcr = (p_priv->cflag & CSTOPB) ? STOPBITS_678_2 : STOPBITS_5678_1;
L
Linus Torvalds 已提交
1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732
	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 */
1733
		msg.lcr |= (p_priv->cflag & PARODD) ?
A
Alan Cox 已提交
1734
			USA_PARITY_ODD : USA_PARITY_EVEN;
L
Linus Torvalds 已提交
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 1766 1767 1768 1769 1770 1771 1772 1773 1774
	}
	msg.setLcr = 0xff;

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

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

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

	/* Sending intermediate configs */
	else {
A
Alan Cox 已提交
1775
		msg._txOn = (!p_priv->break_on);
L
Linus Torvalds 已提交
1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786
		msg._txOff = 0;
		msg.txFlush = 0;
		msg.txBreak = (p_priv->break_on);
		msg.rxOn = 0;
		msg.rxOff = 0;
		msg.rxFlush = 0;
		msg.rxForward = 0;
		msg.returnStatus = 0;
		msg.resetDataToggle = 0x0;
	}

A
Alan Cox 已提交
1787
	/* Do handshaking outputs */
L
Linus Torvalds 已提交
1788 1789 1790 1791 1792
	msg.setTxTriState_setRts = 0xff;
	msg.txTriState_rts = p_priv->rts_state;

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

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

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

A
Alan Cox 已提交
1800 1801
	err = usb_submit_urb(this_urb, GFP_ATOMIC);
	if (err != 0)
1802
		dev_dbg(&port->dev, "%s - usb_submit_urb(setup) failed (%d)\n", __func__, err);
L
Linus Torvalds 已提交
1803 1804
#if 0
	else {
1805 1806 1807
		dev_dbg(&port->dev, "%s - usb_submit_urb(%d) OK %d bytes (end %d)\n", __func__
			outcont_urb, this_urb->transfer_buffer_length,
			usb_pipeendpoint(this_urb->pipe));
L
Linus Torvalds 已提交
1808 1809 1810
	}
#endif

1811
	return 0;
L
Linus Torvalds 已提交
1812 1813 1814 1815 1816 1817
}

static int keyspan_usa28_send_setup(struct usb_serial *serial,
				    struct usb_serial_port *port,
				    int reset_port)
{
A
Alan Cox 已提交
1818
	struct keyspan_usa28_portControlMessage	msg;
L
Linus Torvalds 已提交
1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830
	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 */
A
Alan Cox 已提交
1831 1832
	this_urb = p_priv->outcont_urb;
	if (this_urb == NULL) {
1833
		dev_dbg(&port->dev, "%s - oops no urb.\n", __func__);
L
Linus Torvalds 已提交
1834 1835 1836 1837
		return -1;
	}

	/* Save reset port val for resend.
1838 1839
	   Don't overwrite resend for open/close condition. */
	if ((reset_port + 1) > p_priv->resend_cont)
L
Linus Torvalds 已提交
1840 1841
		p_priv->resend_cont = reset_port + 1;
	if (this_urb->status == -EINPROGRESS) {
1842
		dev_dbg(&port->dev, "%s already writing\n", __func__);
L
Linus Torvalds 已提交
1843
		mdelay(5);
A
Alan Cox 已提交
1844
		return -1;
L
Linus Torvalds 已提交
1845 1846
	}

A
Alan Cox 已提交
1847
	memset(&msg, 0, sizeof(struct keyspan_usa28_portControlMessage));
L
Linus Torvalds 已提交
1848 1849

	msg.setBaudRate = 1;
1850 1851 1852 1853
	if (d_details->calculate_baud_rate(port, p_priv->baud, d_details->baudclk,
					   &msg.baudHi, &msg.baudLo, NULL,
					   device_port) == KEYSPAN_INVALID_BAUD_RATE) {
		dev_dbg(&port->dev, "%s - Invalid baud rate requested %d.\n",
A
Alan Cox 已提交
1854
						__func__, p_priv->baud);
L
Linus Torvalds 已提交
1855 1856 1857 1858 1859 1860 1861 1862 1863 1864
		msg.baudLo = 0xff;
		msg.baudHi = 0xb2;	/* Values for 9600 baud */
	}

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

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

A
Alan Cox 已提交
1865
	/* Do handshaking outputs, DTR is inverted relative to RTS */
L
Linus Torvalds 已提交
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 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906
	msg.rts = p_priv->rts_state;
	msg.dtr = p_priv->dtr_state;

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

	/*msg.returnStatus = 1;
	msg.resetDataToggle = 0xff;*/
	/* Opening port */
	if (reset_port == 1) {
		msg._txOn = 1;
		msg._txOff = 0;
		msg.txFlush = 0;
		msg.txForceXoff = 0;
		msg.txBreak = 0;
		msg.rxOn = 1;
		msg.rxOff = 0;
		msg.rxFlush = 1;
		msg.rxForward = 0;
		msg.returnStatus = 0;
		msg.resetDataToggle = 0xff;
	}
	/* Closing port */
	else if (reset_port == 2) {
		msg._txOn = 0;
		msg._txOff = 1;
		msg.txFlush = 0;
		msg.txForceXoff = 0;
		msg.txBreak = 0;
		msg.rxOn = 0;
		msg.rxOff = 1;
		msg.rxFlush = 1;
		msg.rxForward = 0;
		msg.returnStatus = 0;
		msg.resetDataToggle = 0;
	}
	/* Sending intermediate configs */
	else {
A
Alan Cox 已提交
1907
		msg._txOn = (!p_priv->break_on);
L
Linus Torvalds 已提交
1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920
		msg._txOff = 0;
		msg.txFlush = 0;
		msg.txForceXoff = 0;
		msg.txBreak = (p_priv->break_on);
		msg.rxOn = 0;
		msg.rxOff = 0;
		msg.rxFlush = 0;
		msg.rxForward = 0;
		msg.returnStatus = 0;
		msg.resetDataToggle = 0x0;
	}

	p_priv->resend_cont = 0;
A
Alan Cox 已提交
1921
	memcpy(this_urb->transfer_buffer, &msg, sizeof(msg));
L
Linus Torvalds 已提交
1922 1923 1924 1925

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

A
Alan Cox 已提交
1926 1927
	err = usb_submit_urb(this_urb, GFP_ATOMIC);
	if (err != 0)
1928
		dev_dbg(&port->dev, "%s - usb_submit_urb(setup) failed\n", __func__);
L
Linus Torvalds 已提交
1929 1930
#if 0
	else {
1931
		dev_dbg(&port->dev, "%s - usb_submit_urb(setup) OK %d bytes\n", __func__,
L
Linus Torvalds 已提交
1932 1933 1934 1935
		    this_urb->transfer_buffer_length);
	}
#endif

1936
	return 0;
L
Linus Torvalds 已提交
1937 1938 1939 1940 1941 1942
}

static int keyspan_usa49_send_setup(struct usb_serial *serial,
				    struct usb_serial_port *port,
				    int reset_port)
{
1943 1944
	struct keyspan_usa49_portControlMessage	msg;
	struct usb_ctrlrequest 			*dr = NULL;
L
Linus Torvalds 已提交
1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956
	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;

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

1960
	/* Make sure we have an urb then send the message */
L
Linus Torvalds 已提交
1961
	if (this_urb == NULL) {
1962
		dev_dbg(&port->dev, "%s - oops no urb for port %d.\n", __func__, port->number);
L
Linus Torvalds 已提交
1963 1964 1965
		return -1;
	}

1966 1967 1968
	dev_dbg(&port->dev, "%s - endpoint %d port %d (%d)\n",
		__func__, usb_pipeendpoint(this_urb->pipe),
		port->number, device_port);
1969

L
Linus Torvalds 已提交
1970
	/* Save reset port val for resend.
1971 1972
	   Don't overwrite resend for open/close condition. */
	if ((reset_port + 1) > p_priv->resend_cont)
L
Linus Torvalds 已提交
1973
		p_priv->resend_cont = reset_port + 1;
1974

L
Linus Torvalds 已提交
1975
	if (this_urb->status == -EINPROGRESS) {
1976
		/*  dev_dbg(&port->dev, "%s - already writing\n", __func__); */
L
Linus Torvalds 已提交
1977
		mdelay(5);
A
Alan Cox 已提交
1978
		return -1;
L
Linus Torvalds 已提交
1979 1980
	}

A
Alan Cox 已提交
1981
	memset(&msg, 0, sizeof(struct keyspan_usa49_portControlMessage));
L
Linus Torvalds 已提交
1982 1983 1984

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

	/* Only set baud rate if it's changed */
L
Linus Torvalds 已提交
1987 1988 1989
	if (p_priv->old_baud != p_priv->baud) {
		p_priv->old_baud = p_priv->baud;
		msg.setClocking = 0xff;
1990 1991 1992 1993 1994
		if (d_details->calculate_baud_rate(port, p_priv->baud, d_details->baudclk,
						   &msg.baudHi, &msg.baudLo, &msg.prescaler,
						   device_port) == KEYSPAN_INVALID_BAUD_RATE) {
			dev_dbg(&port->dev, "%s - Invalid baud rate %d requested, using 9600.\n",
				__func__, p_priv->baud);
L
Linus Torvalds 已提交
1995 1996 1997 1998
			msg.baudLo = 0;
			msg.baudHi = 125;	/* Values for 9600 baud */
			msg.prescaler = 10;
		}
A
Alan Cox 已提交
1999
		/* msg.setPrescaler = 0xff; */
L
Linus Torvalds 已提交
2000 2001
	}

2002
	msg.lcr = (p_priv->cflag & CSTOPB) ? STOPBITS_678_2 : STOPBITS_5678_1;
L
Linus Torvalds 已提交
2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018
	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 */
2019
		msg.lcr |= (p_priv->cflag & PARODD) ?
A
Alan Cox 已提交
2020
			USA_PARITY_ODD : USA_PARITY_EVEN;
L
Linus Torvalds 已提交
2021 2022 2023 2024 2025 2026
	}
	msg.setLcr = 0xff;

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

L
Linus Torvalds 已提交
2028 2029 2030 2031
	msg.forwardingLength = 16;
	msg.xonChar = 17;
	msg.xoffChar = 19;

A
Alan Cox 已提交
2032
	/* Opening port */
L
Linus Torvalds 已提交
2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063
	if (reset_port == 1) {
		msg._txOn = 1;
		msg._txOff = 0;
		msg.txFlush = 0;
		msg.txBreak = 0;
		msg.rxOn = 1;
		msg.rxOff = 0;
		msg.rxFlush = 1;
		msg.rxForward = 0;
		msg.returnStatus = 0;
		msg.resetDataToggle = 0xff;
		msg.enablePort = 1;
		msg.disablePort = 0;
	}
	/* Closing port */
	else if (reset_port == 2) {
		msg._txOn = 0;
		msg._txOff = 1;
		msg.txFlush = 0;
		msg.txBreak = 0;
		msg.rxOn = 0;
		msg.rxOff = 1;
		msg.rxFlush = 1;
		msg.rxForward = 0;
		msg.returnStatus = 0;
		msg.resetDataToggle = 0;
		msg.enablePort = 0;
		msg.disablePort = 1;
	}
	/* Sending intermediate configs */
	else {
A
Alan Cox 已提交
2064
		msg._txOn = (!p_priv->break_on);
L
Linus Torvalds 已提交
2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077
		msg._txOff = 0;
		msg.txFlush = 0;
		msg.txBreak = (p_priv->break_on);
		msg.rxOn = 0;
		msg.rxOff = 0;
		msg.rxFlush = 0;
		msg.rxForward = 0;
		msg.returnStatus = 0;
		msg.resetDataToggle = 0x0;
		msg.enablePort = 0;
		msg.disablePort = 0;
	}

A
Alan Cox 已提交
2078
	/* Do handshaking outputs */
L
Linus Torvalds 已提交
2079 2080 2081 2082 2083
	msg.setRts = 0xff;
	msg.rts = p_priv->rts_state;

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

L
Linus Torvalds 已提交
2085
	p_priv->resend_cont = 0;
2086

A
Alan Cox 已提交
2087 2088
	/* if the device is a 49wg, we send control message on usb
	   control EP 0 */
2089 2090 2091 2092 2093 2094 2095 2096 2097

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

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

A
Alan Cox 已提交
2100 2101 2102 2103
		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);
2104 2105 2106

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

2108 2109 2110
		/* send the data out the device on control endpoint */
		this_urb->transfer_buffer_length = sizeof(msg);
	}
A
Alan Cox 已提交
2111 2112
	err = usb_submit_urb(this_urb, GFP_ATOMIC);
	if (err != 0)
2113
		dev_dbg(&port->dev, "%s - usb_submit_urb(setup) failed (%d)\n", __func__, err);
L
Linus Torvalds 已提交
2114 2115
#if 0
	else {
2116 2117 2118
		dev_dbg(&port->dev, "%s - usb_submit_urb(%d) OK %d bytes (end %d)\n", __func__,
			outcont_urb, this_urb->transfer_buffer_length,
			usb_pipeendpoint(this_urb->pipe));
L
Linus Torvalds 已提交
2119 2120 2121
	}
#endif

2122
	return 0;
L
Linus Torvalds 已提交
2123 2124 2125 2126 2127 2128
}

static int keyspan_usa90_send_setup(struct usb_serial *serial,
				    struct usb_serial_port *port,
				    int reset_port)
{
A
Alan Cox 已提交
2129
	struct keyspan_usa90_portControlMessage	msg;
L
Linus Torvalds 已提交
2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141
	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 */
A
Alan Cox 已提交
2142 2143
	this_urb = p_priv->outcont_urb;
	if (this_urb == NULL) {
2144
		dev_dbg(&port->dev, "%s - oops no urb.\n", __func__);
L
Linus Torvalds 已提交
2145 2146 2147 2148 2149 2150 2151 2152
		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) {
2153
		dev_dbg(&port->dev, "%s already writing\n", __func__);
L
Linus Torvalds 已提交
2154
		mdelay(5);
A
Alan Cox 已提交
2155
		return -1;
L
Linus Torvalds 已提交
2156 2157
	}

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

A
Alan Cox 已提交
2160
	/* Only set baud rate if it's changed */
L
Linus Torvalds 已提交
2161 2162 2163
	if (p_priv->old_baud != p_priv->baud) {
		p_priv->old_baud = p_priv->baud;
		msg.setClocking = 0x01;
2164 2165 2166 2167
		if (d_details->calculate_baud_rate(port, p_priv->baud, d_details->baudclk,
						   &msg.baudHi, &msg.baudLo, &prescaler, 0) == KEYSPAN_INVALID_BAUD_RATE) {
			dev_dbg(&port->dev, "%s - Invalid baud rate %d requested, using 9600.\n",
				__func__, p_priv->baud);
L
Linus Torvalds 已提交
2168
			p_priv->baud = 9600;
2169
			d_details->calculate_baud_rate(port, p_priv->baud, d_details->baudclk,
L
Linus Torvalds 已提交
2170 2171 2172 2173 2174 2175 2176
				&msg.baudHi, &msg.baudLo, &prescaler, 0);
		}
		msg.setRxMode = 1;
		msg.setTxMode = 1;
	}

	/* modes must always be correctly specified */
A
Alan Cox 已提交
2177
	if (p_priv->baud > 57600) {
L
Linus Torvalds 已提交
2178 2179
		msg.rxMode = RXMODE_DMA;
		msg.txMode = TXMODE_DMA;
A
Alan Cox 已提交
2180
	} else {
L
Linus Torvalds 已提交
2181 2182 2183 2184
		msg.rxMode = RXMODE_BYHAND;
		msg.txMode = TXMODE_BYHAND;
	}

2185
	msg.lcr = (p_priv->cflag & CSTOPB) ? STOPBITS_678_2 : STOPBITS_5678_1;
L
Linus Torvalds 已提交
2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201
	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 */
2202
		msg.lcr |= (p_priv->cflag & PARODD) ?
A
Alan Cox 已提交
2203
			USA_PARITY_ODD : USA_PARITY_EVEN;
L
Linus Torvalds 已提交
2204 2205 2206 2207 2208 2209 2210 2211 2212 2213
	}
	if (p_priv->old_cflag != p_priv->cflag) {
		p_priv->old_cflag = p_priv->cflag;
		msg.setLcr = 0x01;
	}

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

L
Linus Torvalds 已提交
2215
	msg.rxForwardingLength = 16;
A
Alan Cox 已提交
2216
	msg.rxForwardingTimeout = 16;
L
Linus Torvalds 已提交
2217 2218 2219 2220
	msg.txAckSetting = 0;
	msg.xonChar = 17;
	msg.xoffChar = 19;

A
Alan Cox 已提交
2221
	/* Opening port */
L
Linus Torvalds 已提交
2222 2223 2224 2225 2226 2227
	if (reset_port == 1) {
		msg.portEnabled = 1;
		msg.rxFlush = 1;
		msg.txBreak = (p_priv->break_on);
	}
	/* Closing port */
A
Alan Cox 已提交
2228
	else if (reset_port == 2)
L
Linus Torvalds 已提交
2229 2230 2231
		msg.portEnabled = 0;
	/* Sending intermediate configs */
	else {
2232
		msg.portEnabled = 1;
L
Linus Torvalds 已提交
2233 2234 2235
		msg.txBreak = (p_priv->break_on);
	}

A
Alan Cox 已提交
2236
	/* Do handshaking outputs */
L
Linus Torvalds 已提交
2237 2238 2239 2240 2241
	msg.setRts = 0x01;
	msg.rts = p_priv->rts_state;

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

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

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

A
Alan Cox 已提交
2249 2250
	err = usb_submit_urb(this_urb, GFP_ATOMIC);
	if (err != 0)
2251
		dev_dbg(&port->dev, "%s - usb_submit_urb(setup) failed (%d)\n", __func__, err);
2252
	return 0;
L
Linus Torvalds 已提交
2253 2254
}

2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276
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) {
2277
		dev_dbg(&port->dev, "%s - oops no urb for port %d.\n", __func__,
2278 2279 2280 2281 2282 2283 2284 2285 2286
			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) {
2287
		/*  dev_dbg(&port->dev, "%s - already writing\n", __func__); */
2288
		mdelay(5);
A
Alan Cox 已提交
2289
		return -1;
2290 2291 2292 2293 2294 2295 2296 2297 2298 2299
	}

	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;
2300 2301 2302 2303 2304
		if (d_details->calculate_baud_rate(port, p_priv->baud, d_details->baudclk,
						   &msg.baudHi, &msg.baudLo, &msg.prescaler,
						   device_port) == KEYSPAN_INVALID_BAUD_RATE) {
			dev_dbg(&port->dev, "%s - Invalid baud rate %d requested, using 9600.\n",
				__func__, p_priv->baud);
2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328
			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 */
2329
		msg.lcr |= (p_priv->cflag & PARODD) ?
A
Alan Cox 已提交
2330
					USA_PARITY_ODD : USA_PARITY_EVEN;
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 2358 2359 2360 2361 2362 2363 2364 2365 2366
	}
	msg.setLcr = 0xff;

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

	if (reset_port == 1) {
		/* Opening port */
		msg._txOn = 1;
		msg._txOff = 0;
		msg.txFlush = 0;
		msg.txBreak = 0;
		msg.rxOn = 1;
		msg.rxOff = 0;
		msg.rxFlush = 1;
		msg.rxForward = 0;
		msg.returnStatus = 0;
		msg.resetDataToggle = 0xff;
	} else if (reset_port == 2) {
		/* Closing port */
		msg._txOn = 0;
		msg._txOff = 1;
		msg.txFlush = 0;
		msg.txBreak = 0;
		msg.rxOn = 0;
		msg.rxOff = 1;
		msg.rxFlush = 1;
		msg.rxForward = 0;
		msg.returnStatus = 0;
		msg.resetDataToggle = 0;
	} else {
		/* Sending intermediate configs */
A
Alan Cox 已提交
2367
		msg._txOn = (!p_priv->break_on);
2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394
		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)
2395
		dev_dbg(&port->dev, "%s - usb_submit_urb(setup) failed (%d)\n", __func__, err);
2396
	return 0;
2397 2398
}

L
Linus Torvalds 已提交
2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420
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;
2421 2422 2423
	case msg_usa67:
		keyspan_usa67_send_setup(serial, port, reset_port);
		break;
L
Linus Torvalds 已提交
2424 2425 2426 2427 2428 2429
	}
}


/* Gets called by the "real" driver (ie once firmware is loaded
   and renumeration has taken place. */
A
Alan Cox 已提交
2430
static int keyspan_startup(struct usb_serial *serial)
L
Linus Torvalds 已提交
2431 2432 2433 2434 2435 2436 2437 2438
{
	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)
A
Alan Cox 已提交
2439 2440
		if (d_details->product_id ==
				le16_to_cpu(serial->dev->descriptor.idProduct))
L
Linus Torvalds 已提交
2441 2442
			break;
	if (d_details == NULL) {
A
Alan Cox 已提交
2443 2444
		dev_err(&serial->dev->dev, "%s - unknown product id %x\n",
		    __func__, le16_to_cpu(serial->dev->descriptor.idProduct));
L
Linus Torvalds 已提交
2445 2446 2447 2448
		return 1;
	}

	/* Setup private data for serial driver */
2449
	s_priv = kzalloc(sizeof(struct keyspan_serial_private), GFP_KERNEL);
L
Linus Torvalds 已提交
2450
	if (!s_priv) {
2451
		dev_dbg(&serial->dev->dev, "%s - kmalloc for keyspan_serial_private failed.\n", __func__);
L
Linus Torvalds 已提交
2452 2453 2454 2455 2456 2457 2458 2459 2460
		return -ENOMEM;
	}

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

	/* Now setup per port private data */
	for (i = 0; i < serial->num_ports; i++) {
		port = serial->port[i];
A
Alan Cox 已提交
2461 2462
		p_priv = kzalloc(sizeof(struct keyspan_port_private),
								GFP_KERNEL);
L
Linus Torvalds 已提交
2463
		if (!p_priv) {
2464
			dev_dbg(&port->dev, "%s - kmalloc for keyspan_port_private (%d) failed!.\n", __func__, i);
A
Alan Cox 已提交
2465
			return 1;
L
Linus Torvalds 已提交
2466 2467 2468 2469 2470 2471 2472
		}
		p_priv->device_details = d_details;
		usb_set_serial_port_data(port, p_priv);
	}

	keyspan_setup_urbs(serial);

2473 2474 2475
	if (s_priv->instat_urb != NULL) {
		err = usb_submit_urb(s_priv->instat_urb, GFP_KERNEL);
		if (err != 0)
2476
			dev_dbg(&serial->dev->dev, "%s - submit instat urb failed %d\n", __func__, err);
2477 2478 2479 2480
	}
	if (s_priv->indat_urb != NULL) {
		err = usb_submit_urb(s_priv->indat_urb, GFP_KERNEL);
		if (err != 0)
2481
			dev_dbg(&serial->dev->dev, "%s - submit indat urb failed %d\n", __func__, err);
L
Linus Torvalds 已提交
2482
	}
A
Alan Cox 已提交
2483

2484
	return 0;
L
Linus Torvalds 已提交
2485 2486
}

2487
static void keyspan_disconnect(struct usb_serial *serial)
L
Linus Torvalds 已提交
2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498
{
	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);
2499
	stop_urb(s_priv->indat_urb);
L
Linus Torvalds 已提交
2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511
	for (i = 0; i < serial->num_ports; ++i) {
		port = serial->port[i];
		p_priv = usb_get_serial_port_data(port);
		stop_urb(p_priv->inack_urb);
		stop_urb(p_priv->outcont_urb);
		for (j = 0; j < 2; j++) {
			stop_urb(p_priv->in_urbs[j]);
			stop_urb(p_priv->out_urbs[j]);
		}
	}

	/* Now free them */
M
Mariusz Kozlowski 已提交
2512
	usb_free_urb(s_priv->instat_urb);
2513
	usb_free_urb(s_priv->indat_urb);
M
Mariusz Kozlowski 已提交
2514
	usb_free_urb(s_priv->glocont_urb);
L
Linus Torvalds 已提交
2515 2516 2517
	for (i = 0; i < serial->num_ports; ++i) {
		port = serial->port[i];
		p_priv = usb_get_serial_port_data(port);
M
Mariusz Kozlowski 已提交
2518 2519
		usb_free_urb(p_priv->inack_urb);
		usb_free_urb(p_priv->outcont_urb);
L
Linus Torvalds 已提交
2520
		for (j = 0; j < 2; j++) {
M
Mariusz Kozlowski 已提交
2521 2522
			usb_free_urb(p_priv->in_urbs[j]);
			usb_free_urb(p_priv->out_urbs[j]);
L
Linus Torvalds 已提交
2523 2524
		}
	}
2525 2526 2527 2528 2529 2530 2531 2532 2533
}

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);
L
Linus Torvalds 已提交
2534 2535 2536 2537 2538 2539 2540 2541 2542 2543

	kfree(s_priv);

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

A
Alan Cox 已提交
2544 2545
MODULE_AUTHOR(DRIVER_AUTHOR);
MODULE_DESCRIPTION(DRIVER_DESC);
L
Linus Torvalds 已提交
2546 2547
MODULE_LICENSE("GPL");

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