io_ti.c 80.0 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * Edgeport USB Serial Converter driver
 *
 * Copyright (C) 2000-2002 Inside Out Networks, All rights reserved.
 * Copyright (C) 2001-2002 Greg Kroah-Hartman <greg@kroah.com>
 *
 *	This program is free software; you can redistribute it and/or modify
 *	it under the terms of the GNU General Public License as published by
 *	the Free Software Foundation; either version 2 of the License, or
 *	(at your option) any later version.
 *
 * Supports the following devices:
 *	EP/1 EP/2 EP/4 EP/21 EP/22 EP/221 EP/42 EP/421 WATCHPORT
 *
 * For questions or problems with this driver, contact Inside Out
 * Networks technical support, or Peter Berger <pberger@brimson.com>,
 * or Al Borchers <alborchers@steinerpoint.com>.
 *
 * Version history:
 *
21 22
 *	July 11, 2002 	Removed 4 port device structure since all TI UMP
 *			chips have only 2 ports
L
Linus Torvalds 已提交
23 24 25 26 27 28 29 30 31 32 33 34 35 36
 *			David Iacovelli (davidi@ionetworks.com)
 *
 */

#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>
37
#include <linux/mutex.h>
L
Linus Torvalds 已提交
38 39
#include <linux/serial.h>
#include <linux/ioctl.h>
40
#include <linux/firmware.h>
41
#include <linux/uaccess.h>
L
Linus Torvalds 已提交
42
#include <linux/usb.h>
43
#include <linux/usb/serial.h>
L
Linus Torvalds 已提交
44 45 46 47 48 49 50 51

#include "io_16654.h"
#include "io_usbvend.h"
#include "io_ti.h"

/*
 * Version Information
 */
52
#define DRIVER_VERSION "v0.7mode043006"
L
Linus Torvalds 已提交
53 54 55 56 57 58 59
#define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com> and David Iacovelli"
#define DRIVER_DESC "Edgeport USB Serial Driver"

#define EPROM_PAGE_SIZE		64


struct edgeport_uart_buf_desc {
60
	__u32 count;		/* Number of bytes currently in buffer */
L
Linus Torvalds 已提交
61 62 63 64 65 66
};

/* different hardware types */
#define HARDWARE_TYPE_930	0
#define HARDWARE_TYPE_TIUMP	1

67 68 69 70 71 72
/* IOCTL_PRIVATE_TI_GET_MODE Definitions */
#define	TI_MODE_CONFIGURING	0   /* Device has not entered start device */
#define	TI_MODE_BOOT		1   /* Staying in boot mode		   */
#define TI_MODE_DOWNLOAD	2   /* Made it to download mode		   */
#define TI_MODE_TRANSITIONING	3   /* Currently in boot mode but
				       transitioning to download mode	   */
L
Linus Torvalds 已提交
73 74 75 76 77 78 79 80 81 82 83 84

/* read urb state */
#define EDGE_READ_URB_RUNNING	0
#define EDGE_READ_URB_STOPPING	1
#define EDGE_READ_URB_STOPPED	2

#define EDGE_CLOSING_WAIT	4000	/* in .01 sec */

#define EDGE_OUT_BUF_SIZE	1024


/* Product information read from the Edgeport */
85 86 87
struct product_info {
	int	TiMode;			/* Current TI Mode  */
	__u8	hardware_type;		/* Type of hardware */
L
Linus Torvalds 已提交
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
} __attribute__((packed));

/* circular buffer */
struct edge_buf {
	unsigned int	buf_size;
	char		*buf_buf;
	char		*buf_get;
	char		*buf_put;
};

struct edgeport_port {
	__u16 uart_base;
	__u16 dma_address;
	__u8 shadow_msr;
	__u8 shadow_mcr;
	__u8 shadow_lsr;
	__u8 lsr_mask;
105
	__u32 ump_read_timeout;		/* Number of milliseconds the UMP will
L
Linus Torvalds 已提交
106 107 108 109 110 111 112 113 114 115 116 117
					   wait without data before completing
					   a read short */
	int baud_rate;
	int close_pending;
	int lsr_event;
	struct edgeport_uart_buf_desc tx;
	struct async_icount	icount;
	wait_queue_head_t	delta_msr_wait;	/* for handling sleeping while
						   waiting for msr change to
						   happen */
	struct edgeport_serial	*edge_serial;
	struct usb_serial_port	*port;
118
	__u8 bUartMode;		/* Port type, 0: RS232, etc. */
L
Linus Torvalds 已提交
119 120 121 122 123 124 125 126
	spinlock_t ep_lock;
	int ep_read_urb_state;
	int ep_write_urb_in_use;
	struct edge_buf *ep_out_buf;
};

struct edgeport_serial {
	struct product_info product_info;
127 128 129
	u8 TI_I2C_Type;			/* Type of I2C in UMP */
	u8 TiReadI2C;			/* Set to TRUE if we have read the
					   I2c in Boot Mode */
130
	struct mutex es_lock;
L
Linus Torvalds 已提交
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
	int num_ports_open;
	struct usb_serial *serial;
};


/* Devices that this driver supports */
static struct usb_device_id edgeport_1port_id_table [] = {
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_1) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1I) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROXIMITY) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOTION) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOISTURE) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_TEMPERATURE) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_HUMIDITY) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_POWER) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_LIGHT) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_RADIATION) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_DISTANCE) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_ACCELERATION) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROX_DIST) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_HP4CD) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_PCI) },
	{ }
};

static struct usb_device_id edgeport_2port_id_table [] = {
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2C) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2I) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_421) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_42) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4I) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22I) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_221C) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22C) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21C) },
170
	/* The 4, 8 and 16 port devices show up as multiple 2 port devices */
L
Linus Torvalds 已提交
171
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4S) },
172 173 174 175
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_8) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_8S) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_416) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_416B) },
L
Linus Torvalds 已提交
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
	{ }
};

/* Devices that this driver supports */
static struct usb_device_id id_table_combined [] = {
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_1) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1I) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROXIMITY) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOTION) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOISTURE) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_TEMPERATURE) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_HUMIDITY) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_POWER) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_LIGHT) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_RADIATION) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_DISTANCE) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_ACCELERATION) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROX_DIST) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_HP4CD) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_PCI) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2C) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2I) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_421) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_42) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4I) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22I) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_221C) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22C) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21C) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4S) },
210 211 212 213
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_8) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_8S) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_416) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_416B) },
L
Linus Torvalds 已提交
214 215 216
	{ }
};

217
MODULE_DEVICE_TABLE(usb, id_table_combined);
L
Linus Torvalds 已提交
218 219 220 221 222 223

static struct usb_driver io_driver = {
	.name =		"io_ti",
	.probe =	usb_serial_probe,
	.disconnect =	usb_serial_disconnect,
	.id_table =	id_table_combined,
224
	.no_dynamic_id = 	1,
L
Linus Torvalds 已提交
225 226 227
};


228 229 230
static unsigned char OperationalMajorVersion;
static unsigned char OperationalMinorVersion;
static unsigned short OperationalBuildNumber;
L
Linus Torvalds 已提交
231 232 233 234

static int debug;

static int closing_wait = EDGE_CLOSING_WAIT;
235 236
static int ignore_cpu_rev;
static int default_uart_mode;		/* RS232 */
L
Linus Torvalds 已提交
237

238 239
static void edge_tty_recv(struct device *dev, struct tty_struct *tty,
			  unsigned char *data, int length);
L
Linus Torvalds 已提交
240 241 242 243

static void stop_read(struct edgeport_port *edge_port);
static int restart_read(struct edgeport_port *edge_port);

A
Alan Cox 已提交
244 245 246
static void edge_set_termios(struct tty_struct *tty,
		struct usb_serial_port *port, struct ktermios *old_termios);
static void edge_send(struct tty_struct *tty);
L
Linus Torvalds 已提交
247

248 249 250 251
/* sysfs attributes */
static int edge_create_sysfs_attrs(struct usb_serial_port *port);
static int edge_remove_sysfs_attrs(struct usb_serial_port *port);

L
Linus Torvalds 已提交
252 253 254 255 256 257 258 259 260 261 262 263
/* circular buffer */
static struct edge_buf *edge_buf_alloc(unsigned int size);
static void edge_buf_free(struct edge_buf *eb);
static void edge_buf_clear(struct edge_buf *eb);
static unsigned int edge_buf_data_avail(struct edge_buf *eb);
static unsigned int edge_buf_space_avail(struct edge_buf *eb);
static unsigned int edge_buf_put(struct edge_buf *eb, const char *buf,
	unsigned int count);
static unsigned int edge_buf_get(struct edge_buf *eb, char *buf,
	unsigned int count);


264 265
static int ti_vread_sync(struct usb_device *dev, __u8 request,
				__u16 value, __u16 index, u8 *data, int size)
L
Linus Torvalds 已提交
266 267 268
{
	int status;

269 270 271
	status = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), request,
			(USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN),
			value, index, data, size, 1000);
L
Linus Torvalds 已提交
272 273 274
	if (status < 0)
		return status;
	if (status != size) {
275 276
		dbg("%s - wanted to write %d, but only wrote %d",
					     __func__, size, status);
L
Linus Torvalds 已提交
277 278 279 280 281
		return -ECOMM;
	}
	return 0;
}

282 283
static int ti_vsend_sync(struct usb_device *dev, __u8 request,
				__u16 value, __u16 index, u8 *data, int size)
L
Linus Torvalds 已提交
284 285 286
{
	int status;

287 288 289
	status = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), request,
			(USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT),
			value, index, data, size, 1000);
L
Linus Torvalds 已提交
290 291 292
	if (status < 0)
		return status;
	if (status != size) {
293
		dbg("%s - wanted to write %d, but only wrote %d",
294
		     __func__, size, status);
L
Linus Torvalds 已提交
295 296 297 298 299
		return -ECOMM;
	}
	return 0;
}

300
static int send_cmd(struct usb_device *dev, __u8 command,
L
Linus Torvalds 已提交
301 302 303
				__u8 moduleid, __u16 value, u8 *data,
				int size)
{
304
	return ti_vsend_sync(dev, command, value, moduleid, data, size);
L
Linus Torvalds 已提交
305 306 307
}

/* clear tx/rx buffers and fifo in TI UMP */
308
static int purge_port(struct usb_serial_port *port, __u16 mask)
L
Linus Torvalds 已提交
309 310 311
{
	int port_number = port->number - port->serial->minor;

312
	dbg("%s - port %d, mask %x", __func__, port_number, mask);
L
Linus Torvalds 已提交
313

314
	return send_cmd(port->serial->dev,
L
Linus Torvalds 已提交
315 316 317 318 319 320 321 322
					UMPC_PURGE_PORT,
					(__u8)(UMPM_UART1_PORT + port_number),
					mask,
					NULL,
					0);
}

/**
323
 * read_download_mem - Read edgeport memory from TI chip
L
Linus Torvalds 已提交
324 325 326 327 328 329
 * @dev: usb device pointer
 * @start_address: Device CPU address at which to read
 * @length: Length of above data
 * @address_type: Can read both XDATA and I2C
 * @buffer: pointer to input data buffer
 */
330
static int read_download_mem(struct usb_device *dev, int start_address,
L
Linus Torvalds 已提交
331 332 333 334 335
				int length, __u8 address_type, __u8 *buffer)
{
	int status = 0;
	__u8 read_length;
	__be16 be_start_address;
336 337

	dbg("%s - @ %x for %d", __func__, start_address, length);
L
Linus Torvalds 已提交
338 339 340 341 342 343

	/* Read in blocks of 64 bytes
	 * (TI firmware can't handle more than 64 byte reads)
	 */
	while (length) {
		if (length > 64)
344
			read_length = 64;
L
Linus Torvalds 已提交
345 346 347 348
		else
			read_length = (__u8)length;

		if (read_length > 1) {
349
			dbg("%s - @ %x for %d", __func__,
L
Linus Torvalds 已提交
350 351
			     start_address, read_length);
		}
352 353 354 355 356
		be_start_address = cpu_to_be16(start_address);
		status = ti_vread_sync(dev, UMPC_MEMORY_READ,
					(__u16)address_type,
					(__force __u16)be_start_address,
					buffer, read_length);
L
Linus Torvalds 已提交
357 358

		if (status) {
359
			dbg("%s - ERROR %x", __func__, status);
L
Linus Torvalds 已提交
360 361 362
			return status;
		}

363
		if (read_length > 1)
364
			usb_serial_debug_data(debug, &dev->dev, __func__,
L
Linus Torvalds 已提交
365 366 367 368 369 370 371
					      read_length, buffer);

		/* Update pointers/length */
		start_address += read_length;
		buffer += read_length;
		length -= read_length;
	}
372

L
Linus Torvalds 已提交
373 374 375
	return status;
}

376 377
static int read_ram(struct usb_device *dev, int start_address,
						int length, __u8 *buffer)
L
Linus Torvalds 已提交
378
{
379 380
	return read_download_mem(dev, start_address, length,
					DTK_ADDR_SPACE_XDATA, buffer);
L
Linus Torvalds 已提交
381 382 383
}

/* Read edgeport memory to a given block */
384 385
static int read_boot_mem(struct edgeport_serial *serial,
				int start_address, int length, __u8 *buffer)
L
Linus Torvalds 已提交
386 387 388 389
{
	int status = 0;
	int i;

390 391 392 393
	for (i = 0; i < length; i++) {
		status = ti_vread_sync(serial->serial->dev,
				UMPC_MEMORY_READ, serial->TI_I2C_Type,
				(__u16)(start_address+i), &buffer[i], 0x01);
L
Linus Torvalds 已提交
394
		if (status) {
395
			dbg("%s - ERROR %x", __func__, status);
L
Linus Torvalds 已提交
396 397 398 399
			return status;
		}
	}

400 401 402 403
	dbg("%s - start_address = %x, length = %d",
					__func__, start_address, length);
	usb_serial_debug_data(debug, &serial->serial->dev->dev,
					__func__, length, buffer);
L
Linus Torvalds 已提交
404 405 406 407 408 409 410

	serial->TiReadI2C = 1;

	return status;
}

/* Write given block to TI EPROM memory */
411 412
static int write_boot_mem(struct edgeport_serial *serial,
				int start_address, int length, __u8 *buffer)
L
Linus Torvalds 已提交
413 414 415 416 417 418 419
{
	int status = 0;
	int i;
	__u8 temp;

	/* Must do a read before write */
	if (!serial->TiReadI2C) {
420
		status = read_boot_mem(serial, 0, 1, &temp);
L
Linus Torvalds 已提交
421 422 423 424
		if (status)
			return status;
	}

425 426 427 428
	for (i = 0; i < length; ++i) {
		status = ti_vsend_sync(serial->serial->dev,
				UMPC_MEMORY_WRITE, buffer[i],
				(__u16)(i + start_address), NULL, 0);
L
Linus Torvalds 已提交
429 430 431 432
		if (status)
			return status;
	}

433 434 435 436
	dbg("%s - start_sddr = %x, length = %d",
					__func__, start_address, length);
	usb_serial_debug_data(debug, &serial->serial->dev->dev,
					__func__, length, buffer);
L
Linus Torvalds 已提交
437 438 439 440 441 442

	return status;
}


/* Write edgeport I2C memory to TI chip	*/
443 444
static int write_i2c_mem(struct edgeport_serial *serial,
		int start_address, int length, __u8 address_type, __u8 *buffer)
L
Linus Torvalds 已提交
445 446 447 448 449 450
{
	int status = 0;
	int write_length;
	__be16 be_start_address;

	/* We can only send a maximum of 1 aligned byte page at a time */
451

L
Linus Torvalds 已提交
452
	/* calulate the number of bytes left in the first page */
453 454
	write_length = EPROM_PAGE_SIZE -
				(start_address & (EPROM_PAGE_SIZE - 1));
L
Linus Torvalds 已提交
455 456 457 458

	if (write_length > length)
		write_length = length;

459 460 461 462
	dbg("%s - BytesInFirstPage Addr = %x, length = %d",
					__func__, start_address, write_length);
	usb_serial_debug_data(debug, &serial->serial->dev->dev,
						__func__, write_length, buffer);
L
Linus Torvalds 已提交
463 464

	/* Write first page */
465 466 467 468 469
	be_start_address = cpu_to_be16(start_address);
	status = ti_vsend_sync(serial->serial->dev,
				UMPC_MEMORY_WRITE, (__u16)address_type,
				(__force __u16)be_start_address,
				buffer,	write_length);
L
Linus Torvalds 已提交
470
	if (status) {
471
		dbg("%s - ERROR %d", __func__, status);
L
Linus Torvalds 已提交
472 473 474 475 476 477 478
		return status;
	}

	length		-= write_length;
	start_address	+= write_length;
	buffer		+= write_length;

479 480
	/* We should be aligned now -- can write
	   max page size bytes at a time */
L
Linus Torvalds 已提交
481 482 483 484 485 486
	while (length) {
		if (length > EPROM_PAGE_SIZE)
			write_length = EPROM_PAGE_SIZE;
		else
			write_length = length;

487 488 489 490
		dbg("%s - Page Write Addr = %x, length = %d",
					__func__, start_address, write_length);
		usb_serial_debug_data(debug, &serial->serial->dev->dev,
					__func__, write_length, buffer);
L
Linus Torvalds 已提交
491 492

		/* Write next page */
493 494 495 496 497
		be_start_address = cpu_to_be16(start_address);
		status = ti_vsend_sync(serial->serial->dev, UMPC_MEMORY_WRITE,
				(__u16)address_type,
				(__force __u16)be_start_address,
				buffer, write_length);
L
Linus Torvalds 已提交
498
		if (status) {
499 500
			dev_err(&serial->serial->dev->dev, "%s - ERROR %d\n",
					__func__, status);
L
Linus Torvalds 已提交
501 502
			return status;
		}
503

L
Linus Torvalds 已提交
504 505 506 507 508 509 510 511
		length		-= write_length;
		start_address	+= write_length;
		buffer		+= write_length;
	}
	return status;
}

/* Examine the UMP DMA registers and LSR
512
 *
L
Linus Torvalds 已提交
513 514 515 516
 * Check the MSBit of the X and Y DMA byte count registers.
 * A zero in this bit indicates that the TX DMA buffers are empty
 * then check the TX Empty bit in the UART.
 */
517
static int tx_active(struct edgeport_port *port)
L
Linus Torvalds 已提交
518 519 520 521 522 523
{
	int status;
	struct out_endpoint_desc_block *oedb;
	__u8 *lsr;
	int bytes_left = 0;

524
	oedb = kmalloc(sizeof(*oedb), GFP_KERNEL);
L
Linus Torvalds 已提交
525
	if (!oedb) {
526
		dev_err(&port->port->dev, "%s - out of memory\n", __func__);
L
Linus Torvalds 已提交
527 528 529
		return -ENOMEM;
	}

530
	lsr = kmalloc(1, GFP_KERNEL);	/* Sigh, that's right, just one byte,
L
Linus Torvalds 已提交
531 532 533 534 535 536 537
					   as not all platforms can do DMA
					   from stack */
	if (!lsr) {
		kfree(oedb);
		return -ENOMEM;
	}
	/* Read the DMA Count Registers */
538 539
	status = read_ram(port->port->serial->dev, port->dma_address,
						sizeof(*oedb), (void *)oedb);
L
Linus Torvalds 已提交
540 541 542
	if (status)
		goto exit_is_tx_active;

543
	dbg("%s - XByteCount    0x%X", __func__, oedb->XByteCount);
L
Linus Torvalds 已提交
544 545

	/* and the LSR */
546 547
	status = read_ram(port->port->serial->dev,
			port->uart_base + UMPMEM_OFFS_UART_LSR, 1, lsr);
L
Linus Torvalds 已提交
548 549 550

	if (status)
		goto exit_is_tx_active;
551 552
	dbg("%s - LSR = 0x%X", __func__, *lsr);

L
Linus Torvalds 已提交
553
	/* If either buffer has data or we are transmitting then return TRUE */
554
	if ((oedb->XByteCount & 0x80) != 0)
L
Linus Torvalds 已提交
555 556
		bytes_left += 64;

557
	if ((*lsr & UMP_UART_LSR_TX_MASK) == 0)
L
Linus Torvalds 已提交
558 559 560 561
		bytes_left += 1;

	/* We return Not Active if we get any kind of error */
exit_is_tx_active:
562
	dbg("%s - return %d", __func__, bytes_left);
L
Linus Torvalds 已提交
563 564 565 566 567 568

	kfree(lsr);
	kfree(oedb);
	return bytes_left;
}

569 570
static void chase_port(struct edgeport_port *port, unsigned long timeout,
								int flush)
L
Linus Torvalds 已提交
571 572
{
	int baud_rate;
A
Alan Cox 已提交
573
	struct tty_struct *tty = tty_port_tty_get(&port->port->port);
L
Linus Torvalds 已提交
574 575 576 577
	wait_queue_t wait;
	unsigned long flags;

	if (!timeout)
578
		timeout = (HZ * EDGE_CLOSING_WAIT)/100;
L
Linus Torvalds 已提交
579 580 581 582 583 584 585 586 587

	/* wait for data to drain from the buffer */
	spin_lock_irqsave(&port->ep_lock, flags);
	init_waitqueue_entry(&wait, current);
	add_wait_queue(&tty->write_wait, &wait);
	for (;;) {
		set_current_state(TASK_INTERRUPTIBLE);
		if (edge_buf_data_avail(port->ep_out_buf) == 0
		|| timeout == 0 || signal_pending(current)
588 589
		|| !usb_get_intfdata(port->port->serial->interface))
			/* disconnect */
L
Linus Torvalds 已提交
590 591 592 593 594 595 596 597 598 599
			break;
		spin_unlock_irqrestore(&port->ep_lock, flags);
		timeout = schedule_timeout(timeout);
		spin_lock_irqsave(&port->ep_lock, flags);
	}
	set_current_state(TASK_RUNNING);
	remove_wait_queue(&tty->write_wait, &wait);
	if (flush)
		edge_buf_clear(port->ep_out_buf);
	spin_unlock_irqrestore(&port->ep_lock, flags);
A
Alan Cox 已提交
600
	tty_kref_put(tty);
L
Linus Torvalds 已提交
601 602 603 604

	/* wait for data to drain from the device */
	timeout += jiffies;
	while ((long)(jiffies - timeout) < 0 && !signal_pending(current)
605 606 607
	&& usb_get_intfdata(port->port->serial->interface)) {
		/* not disconnected */
		if (!tx_active(port))
L
Linus Torvalds 已提交
608 609 610 611 612 613 614 615 616
			break;
		msleep(10);
	}

	/* disconnected */
	if (!usb_get_intfdata(port->port->serial->interface))
		return;

	/* wait one more character time, based on baud rate */
617 618 619
	/* (tx_active doesn't seem to wait for the last byte) */
	baud_rate = port->baud_rate;
	if (baud_rate == 0)
L
Linus Torvalds 已提交
620
		baud_rate = 50;
J
Julia Lawall 已提交
621
	msleep(max(1, DIV_ROUND_UP(10000, baud_rate)));
L
Linus Torvalds 已提交
622 623
}

624
static int choose_config(struct usb_device *dev)
L
Linus Torvalds 已提交
625
{
626 627 628 629 630 631
	/*
	 * There may be multiple configurations on this device, in which case
	 * we would need to read and parse all of them to find out which one
	 * we want. However, we just support one config at this point,
	 * configuration # 1, which is Config Descriptor 0.
	 */
L
Linus Torvalds 已提交
632

633 634 635 636
	dbg("%s - Number of Interfaces = %d",
				__func__, dev->config->desc.bNumInterfaces);
	dbg("%s - MAX Power            = %d",
				__func__, dev->config->desc.bMaxPower * 2);
L
Linus Torvalds 已提交
637 638

	if (dev->config->desc.bNumInterfaces != 1) {
639 640
		dev_err(&dev->dev, "%s - bNumInterfaces is not 1, ERROR!\n",
								__func__);
L
Linus Torvalds 已提交
641 642 643 644 645 646
		return -ENODEV;
	}

	return 0;
}

647 648
static int read_rom(struct edgeport_serial *serial,
				int start_address, int length, __u8 *buffer)
L
Linus Torvalds 已提交
649 650 651 652
{
	int status;

	if (serial->product_info.TiMode == TI_MODE_DOWNLOAD) {
653
		status = read_download_mem(serial->serial->dev,
L
Linus Torvalds 已提交
654 655 656 657 658
					       start_address,
					       length,
					       serial->TI_I2C_Type,
					       buffer);
	} else {
659 660
		status = read_boot_mem(serial, start_address, length,
								buffer);
L
Linus Torvalds 已提交
661 662 663 664
	}
	return status;
}

665 666
static int write_rom(struct edgeport_serial *serial, int start_address,
						int length, __u8 *buffer)
L
Linus Torvalds 已提交
667 668
{
	if (serial->product_info.TiMode == TI_MODE_BOOT)
669 670
		return write_boot_mem(serial, start_address, length,
								buffer);
L
Linus Torvalds 已提交
671 672

	if (serial->product_info.TiMode == TI_MODE_DOWNLOAD)
673 674
		return write_i2c_mem(serial, start_address, length,
						serial->TI_I2C_Type, buffer);
L
Linus Torvalds 已提交
675 676 677 678 679 680
	return -EINVAL;
}



/* Read a descriptor header from I2C based on type */
681 682
static int get_descriptor_addr(struct edgeport_serial *serial,
				int desc_type, struct ti_i2c_desc *rom_desc)
L
Linus Torvalds 已提交
683 684 685 686 687 688 689
{
	int start_address;
	int status;

	/* Search for requested descriptor in I2C */
	start_address = 2;
	do {
690
		status = read_rom(serial,
L
Linus Torvalds 已提交
691 692
				   start_address,
				   sizeof(struct ti_i2c_desc),
693
				   (__u8 *)rom_desc);
L
Linus Torvalds 已提交
694 695 696 697 698 699
		if (status)
			return 0;

		if (rom_desc->Type == desc_type)
			return start_address;

700 701
		start_address = start_address + sizeof(struct ti_i2c_desc)
							+ rom_desc->Size;
L
Linus Torvalds 已提交
702 703

	} while ((start_address < TI_MAX_I2C_SIZE) && rom_desc->Type);
704

L
Linus Torvalds 已提交
705 706 707 708
	return 0;
}

/* Validate descriptor checksum */
709
static int valid_csum(struct ti_i2c_desc *rom_desc, __u8 *buffer)
L
Linus Torvalds 已提交
710 711 712 713
{
	__u16 i;
	__u8 cs = 0;

714
	for (i = 0; i < rom_desc->Size; i++)
L
Linus Torvalds 已提交
715
		cs = (__u8)(cs + buffer[i]);
716

L
Linus Torvalds 已提交
717
	if (cs != rom_desc->CheckSum) {
718
		dbg("%s - Mismatch %x - %x", __func__, rom_desc->CheckSum, cs);
L
Linus Torvalds 已提交
719 720 721 722 723 724
		return -EINVAL;
	}
	return 0;
}

/* Make sure that the I2C image is good */
725
static int check_i2c_image(struct edgeport_serial *serial)
L
Linus Torvalds 已提交
726 727 728 729 730 731 732 733
{
	struct device *dev = &serial->serial->dev->dev;
	int status = 0;
	struct ti_i2c_desc *rom_desc;
	int start_address = 2;
	__u8 *buffer;
	__u16 ttype;

734
	rom_desc = kmalloc(sizeof(*rom_desc), GFP_KERNEL);
L
Linus Torvalds 已提交
735
	if (!rom_desc) {
736
		dev_err(dev, "%s - out of memory\n", __func__);
L
Linus Torvalds 已提交
737 738
		return -ENOMEM;
	}
739
	buffer = kmalloc(TI_MAX_I2C_SIZE, GFP_KERNEL);
L
Linus Torvalds 已提交
740
	if (!buffer) {
741 742 743
		dev_err(dev, "%s - out of memory when allocating buffer\n",
								__func__);
		kfree(rom_desc);
L
Linus Torvalds 已提交
744 745 746
		return -ENOMEM;
	}

747 748
	/* Read the first byte (Signature0) must be 0x52 or 0x10 */
	status = read_rom(serial, 0, 1, buffer);
L
Linus Torvalds 已提交
749
	if (status)
750
		goto out;
L
Linus Torvalds 已提交
751 752

	if (*buffer != UMP5152 && *buffer != UMP3410) {
753
		dev_err(dev, "%s - invalid buffer signature\n", __func__);
L
Linus Torvalds 已提交
754
		status = -ENODEV;
755
		goto out;
L
Linus Torvalds 已提交
756 757 758
	}

	do {
759 760
		/* Validate the I2C */
		status = read_rom(serial,
L
Linus Torvalds 已提交
761 762 763 764 765 766
				start_address,
				sizeof(struct ti_i2c_desc),
				(__u8 *)rom_desc);
		if (status)
			break;

767 768
		if ((start_address + sizeof(struct ti_i2c_desc) +
					rom_desc->Size) > TI_MAX_I2C_SIZE) {
L
Linus Torvalds 已提交
769
			status = -ENODEV;
770
			dbg("%s - structure too big, erroring out.", __func__);
L
Linus Torvalds 已提交
771 772 773
			break;
		}

774
		dbg("%s Type = 0x%x", __func__, rom_desc->Type);
L
Linus Torvalds 已提交
775

776
		/* Skip type 2 record */
L
Linus Torvalds 已提交
777
		ttype = rom_desc->Type & 0x0f;
778 779 780 781 782 783
		if (ttype != I2C_DESC_TYPE_FIRMWARE_BASIC
			&& ttype != I2C_DESC_TYPE_FIRMWARE_AUTO) {
			/* Read the descriptor data */
			status = read_rom(serial, start_address +
						sizeof(struct ti_i2c_desc),
						rom_desc->Size, buffer);
L
Linus Torvalds 已提交
784 785 786
			if (status)
				break;

787
			status = valid_csum(rom_desc, buffer);
L
Linus Torvalds 已提交
788 789 790
			if (status)
				break;
		}
791 792
		start_address = start_address + sizeof(struct ti_i2c_desc) +
								rom_desc->Size;
L
Linus Torvalds 已提交
793

794 795
	} while ((rom_desc->Type != I2C_DESC_TYPE_ION) &&
				(start_address < TI_MAX_I2C_SIZE));
L
Linus Torvalds 已提交
796

797 798
	if ((rom_desc->Type != I2C_DESC_TYPE_ION) ||
				(start_address > TI_MAX_I2C_SIZE))
L
Linus Torvalds 已提交
799 800
		status = -ENODEV;

801 802 803
out:
	kfree(buffer);
	kfree(rom_desc);
L
Linus Torvalds 已提交
804 805 806
	return status;
}

807
static int get_manuf_info(struct edgeport_serial *serial, __u8 *buffer)
L
Linus Torvalds 已提交
808 809 810 811 812 813
{
	int status;
	int start_address;
	struct ti_i2c_desc *rom_desc;
	struct edge_ti_manuf_descriptor *desc;

814
	rom_desc = kmalloc(sizeof(*rom_desc), GFP_KERNEL);
L
Linus Torvalds 已提交
815
	if (!rom_desc) {
816 817
		dev_err(&serial->serial->dev->dev, "%s - out of memory\n",
								__func__);
L
Linus Torvalds 已提交
818 819
		return -ENOMEM;
	}
820 821
	start_address = get_descriptor_addr(serial, I2C_DESC_TYPE_ION,
								rom_desc);
L
Linus Torvalds 已提交
822 823

	if (!start_address) {
824
		dbg("%s - Edge Descriptor not found in I2C", __func__);
L
Linus Torvalds 已提交
825 826 827 828
		status = -ENODEV;
		goto exit;
	}

829 830 831
	/* Read the descriptor data */
	status = read_rom(serial, start_address+sizeof(struct ti_i2c_desc),
						rom_desc->Size, buffer);
L
Linus Torvalds 已提交
832 833
	if (status)
		goto exit;
834 835 836

	status = valid_csum(rom_desc, buffer);

L
Linus Torvalds 已提交
837
	desc = (struct edge_ti_manuf_descriptor *)buffer;
838 839 840 841 842 843
	dbg("%s - IonConfig      0x%x", __func__, desc->IonConfig);
	dbg("%s - Version          %d", __func__, desc->Version);
	dbg("%s - Cpu/Board      0x%x", __func__, desc->CpuRev_BoardRev);
	dbg("%s - NumPorts         %d", __func__, desc->NumPorts);
	dbg("%s - NumVirtualPorts  %d", __func__, desc->NumVirtualPorts);
	dbg("%s - TotalPorts       %d", __func__, desc->TotalPorts);
L
Linus Torvalds 已提交
844 845

exit:
846
	kfree(rom_desc);
L
Linus Torvalds 已提交
847 848 849 850
	return status;
}

/* Build firmware header used for firmware update */
851
static int build_i2c_fw_hdr(__u8 *header, struct device *dev)
L
Linus Torvalds 已提交
852 853 854 855
{
	__u8 *buffer;
	int buffer_size;
	int i;
856
	int err;
L
Linus Torvalds 已提交
857 858 859 860
	__u8 cs = 0;
	struct ti_i2c_desc *i2c_header;
	struct ti_i2c_image_header *img_header;
	struct ti_i2c_firmware_rec *firmware_rec;
861 862
	const struct firmware *fw;
	const char *fw_name = "edgeport/down3.bin";
L
Linus Torvalds 已提交
863

864 865 866 867 868 869 870 871 872 873 874 875 876 877 878
	/* In order to update the I2C firmware we must change the type 2 record
	 * to type 0xF2.  This will force the UMP to come up in Boot Mode.
	 * Then while in boot mode, the driver will download the latest
	 * firmware (padded to 15.5k) into the UMP ram.  And finally when the
	 * device comes back up in download mode the driver will cause the new
	 * firmware to be copied from the UMP Ram to I2C and the firmware will
	 * update the record type from 0xf2 to 0x02.
	 */

	/* Allocate a 15.5k buffer + 2 bytes for version number
	 * (Firmware Record) */
	buffer_size = (((1024 * 16) - 512 ) +
			sizeof(struct ti_i2c_firmware_rec));

	buffer = kmalloc(buffer_size, GFP_KERNEL);
L
Linus Torvalds 已提交
879
	if (!buffer) {
880
		dev_err(dev, "%s - out of memory\n", __func__);
L
Linus Torvalds 已提交
881 882
		return -ENOMEM;
	}
883

L
Linus Torvalds 已提交
884
	// Set entire image of 0xffs
885
	memset(buffer, 0xff, buffer_size);
L
Linus Torvalds 已提交
886

887 888 889 890 891 892 893 894 895 896 897 898 899
	err = request_firmware(&fw, fw_name, dev);
	if (err) {
		printk(KERN_ERR "Failed to load image \"%s\" err %d\n",
		       fw_name, err);
		kfree(buffer);
		return err;
	}

	/* Save Download Version Number */
	OperationalMajorVersion = fw->data[0];
	OperationalMinorVersion = fw->data[1];
	OperationalBuildNumber = fw->data[2] | (fw->data[3] << 8);

900
	/* Copy version number into firmware record */
L
Linus Torvalds 已提交
901 902
	firmware_rec = (struct ti_i2c_firmware_rec *)buffer;

903 904
	firmware_rec->Ver_Major	= OperationalMajorVersion;
	firmware_rec->Ver_Minor	= OperationalMinorVersion;
L
Linus Torvalds 已提交
905

906
	/* Pointer to fw_down memory image */
907
	img_header = (struct ti_i2c_image_header *)&fw->data[4];
L
Linus Torvalds 已提交
908

909
	memcpy(buffer + sizeof(struct ti_i2c_firmware_rec),
910
		&fw->data[4 + sizeof(struct ti_i2c_image_header)],
L
Linus Torvalds 已提交
911 912
		le16_to_cpu(img_header->Length));

913 914
	release_firmware(fw);

L
Linus Torvalds 已提交
915 916 917 918
	for (i=0; i < buffer_size; i++) {
		cs = (__u8)(cs + buffer[i]);
	}

919
	kfree(buffer);
L
Linus Torvalds 已提交
920

921
	/* Build new header */
L
Linus Torvalds 已提交
922 923
	i2c_header =  (struct ti_i2c_desc *)header;
	firmware_rec =  (struct ti_i2c_firmware_rec*)i2c_header->Data;
924

L
Linus Torvalds 已提交
925 926 927
	i2c_header->Type	= I2C_DESC_TYPE_FIRMWARE_BLANK;
	i2c_header->Size	= (__u16)buffer_size;
	i2c_header->CheckSum	= cs;
928 929
	firmware_rec->Ver_Major	= OperationalMajorVersion;
	firmware_rec->Ver_Minor	= OperationalMinorVersion;
L
Linus Torvalds 已提交
930 931 932 933 934

	return 0;
}

/* Try to figure out what type of I2c we have */
935
static int i2c_type_bootmode(struct edgeport_serial *serial)
L
Linus Torvalds 已提交
936 937 938
{
	int status;
	__u8 data;
939 940 941 942

	/* Try to read type 2 */
	status = ti_vread_sync(serial->serial->dev, UMPC_MEMORY_READ,
				DTK_ADDR_SPACE_I2C_TYPE_II, 0, &data, 0x01);
L
Linus Torvalds 已提交
943
	if (status)
944
		dbg("%s - read 2 status error = %d", __func__, status);
L
Linus Torvalds 已提交
945
	else
946
		dbg("%s - read 2 data = 0x%x", __func__, data);
L
Linus Torvalds 已提交
947
	if ((!status) && (data == UMP5152 || data == UMP3410)) {
948
		dbg("%s - ROM_TYPE_II", __func__);
L
Linus Torvalds 已提交
949 950 951 952
		serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
		return 0;
	}

953 954 955
	/* Try to read type 3 */
	status = ti_vread_sync(serial->serial->dev, UMPC_MEMORY_READ,
				DTK_ADDR_SPACE_I2C_TYPE_III, 0,	&data, 0x01);
L
Linus Torvalds 已提交
956
	if (status)
957
		dbg("%s - read 3 status error = %d", __func__, status);
L
Linus Torvalds 已提交
958
	else
959
		dbg("%s - read 2 data = 0x%x", __func__, data);
L
Linus Torvalds 已提交
960
	if ((!status) && (data == UMP5152 || data == UMP3410)) {
961
		dbg("%s - ROM_TYPE_III", __func__);
L
Linus Torvalds 已提交
962 963 964 965
		serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_III;
		return 0;
	}

966
	dbg("%s - Unknown", __func__);
L
Linus Torvalds 已提交
967 968 969 970
	serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
	return -ENODEV;
}

971 972
static int bulk_xfer(struct usb_serial *serial, void *buffer,
						int length, int *num_sent)
L
Linus Torvalds 已提交
973 974 975
{
	int status;

976 977 978 979
	status = usb_bulk_msg(serial->dev,
			usb_sndbulkpipe(serial->dev,
				serial->port[0]->bulk_out_endpointAddress),
			buffer, length, num_sent, 1000);
L
Linus Torvalds 已提交
980 981 982 983
	return status;
}

/* Download given firmware image to the device (IN BOOT MODE) */
984 985
static int download_code(struct edgeport_serial *serial, __u8 *image,
							int image_length)
L
Linus Torvalds 已提交
986 987 988 989 990 991
{
	int status = 0;
	int pos;
	int transfer;
	int done;

992
	/* Transfer firmware image */
L
Linus Torvalds 已提交
993
	for (pos = 0; pos < image_length; ) {
994
		/* Read the next buffer from file */
L
Linus Torvalds 已提交
995 996 997 998
		transfer = image_length - pos;
		if (transfer > EDGE_FW_BULK_MAX_PACKET_SIZE)
			transfer = EDGE_FW_BULK_MAX_PACKET_SIZE;

999 1000 1001
		/* Transfer data */
		status = bulk_xfer(serial->serial, &image[pos],
							transfer, &done);
L
Linus Torvalds 已提交
1002 1003
		if (status)
			break;
1004
		/* Advance buffer pointer */
L
Linus Torvalds 已提交
1005 1006 1007 1008 1009 1010
		pos += done;
	}

	return status;
}

1011 1012
/* FIXME!!! */
static int config_boot_dev(struct usb_device *dev)
L
Linus Torvalds 已提交
1013 1014 1015 1016
{
	return 0;
}

1017 1018 1019 1020 1021
static int ti_cpu_rev(struct edge_ti_manuf_descriptor *desc)
{
	return TI_GET_CPU_REVISION(desc->CpuRev_BoardRev);
}

L
Linus Torvalds 已提交
1022 1023
/**
 * DownloadTIFirmware - Download run-time operating firmware to the TI5052
1024
 *
L
Linus Torvalds 已提交
1025 1026 1027
 * This routine downloads the main operating code into the TI5052, using the
 * boot code already burned into E2PROM or ROM.
 */
1028
static int download_fw(struct edgeport_serial *serial)
L
Linus Torvalds 已提交
1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046
{
	struct device *dev = &serial->serial->dev->dev;
	int status = 0;
	int start_address;
	struct edge_ti_manuf_descriptor *ti_manuf_desc;
	struct usb_interface_descriptor *interface;
	int download_cur_ver;
	int download_new_ver;

	/* This routine is entered by both the BOOT mode and the Download mode
	 * We can determine which code is running by the reading the config
	 * descriptor and if we have only one bulk pipe it is in boot mode
	 */
	serial->product_info.hardware_type = HARDWARE_TYPE_TIUMP;

	/* Default to type 2 i2c */
	serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;

1047
	status = choose_config(serial->serial->dev);
L
Linus Torvalds 已提交
1048 1049 1050 1051 1052
	if (status)
		return status;

	interface = &serial->serial->interface->cur_altsetting->desc;
	if (!interface) {
1053
		dev_err(dev, "%s - no interface set, error!\n", __func__);
L
Linus Torvalds 已提交
1054 1055 1056
		return -ENODEV;
	}

1057 1058 1059 1060 1061
	/*
	 * Setup initial mode -- the default mode 0 is TI_MODE_CONFIGURING
	 * if we have more than one endpoint we are definitely in download
	 * mode
	 */
L
Linus Torvalds 已提交
1062 1063 1064
	if (interface->bNumEndpoints > 1)
		serial->product_info.TiMode = TI_MODE_DOWNLOAD;
	else
1065
		/* Otherwise we will remain in configuring mode */
L
Linus Torvalds 已提交
1066 1067 1068 1069 1070 1071 1072 1073
		serial->product_info.TiMode = TI_MODE_CONFIGURING;

	/********************************************************************/
	/* Download Mode */
	/********************************************************************/
	if (serial->product_info.TiMode == TI_MODE_DOWNLOAD) {
		struct ti_i2c_desc *rom_desc;

1074
		dbg("%s - RUNNING IN DOWNLOAD MODE", __func__);
L
Linus Torvalds 已提交
1075

1076
		status = check_i2c_image(serial);
L
Linus Torvalds 已提交
1077
		if (status) {
1078
			dbg("%s - DOWNLOAD MODE -- BAD I2C", __func__);
L
Linus Torvalds 已提交
1079 1080
			return status;
		}
1081

L
Linus Torvalds 已提交
1082 1083 1084
		/* Validate Hardware version number
		 * Read Manufacturing Descriptor from TI Based Edgeport
		 */
1085
		ti_manuf_desc = kmalloc(sizeof(*ti_manuf_desc), GFP_KERNEL);
L
Linus Torvalds 已提交
1086
		if (!ti_manuf_desc) {
1087
			dev_err(dev, "%s - out of memory.\n", __func__);
L
Linus Torvalds 已提交
1088 1089
			return -ENOMEM;
		}
1090
		status = get_manuf_info(serial, (__u8 *)ti_manuf_desc);
L
Linus Torvalds 已提交
1091
		if (status) {
1092
			kfree(ti_manuf_desc);
L
Linus Torvalds 已提交
1093 1094 1095
			return status;
		}

1096 1097 1098 1099 1100 1101 1102
		/* Check version number of ION descriptor */
		if (!ignore_cpu_rev && ti_cpu_rev(ti_manuf_desc) < 2) {
			dbg("%s - Wrong CPU Rev %d (Must be 2)",
				__func__, ti_cpu_rev(ti_manuf_desc));
			kfree(ti_manuf_desc);
			return -EINVAL;
  		}
L
Linus Torvalds 已提交
1103

1104
		rom_desc = kmalloc(sizeof(*rom_desc), GFP_KERNEL);
L
Linus Torvalds 已提交
1105
		if (!rom_desc) {
1106 1107
			dev_err(dev, "%s - out of memory.\n", __func__);
			kfree(ti_manuf_desc);
L
Linus Torvalds 已提交
1108 1109 1110
			return -ENOMEM;
		}

1111 1112 1113 1114
		/* Search for type 2 record (firmware record) */
		start_address = get_descriptor_addr(serial,
				I2C_DESC_TYPE_FIRMWARE_BASIC, rom_desc);
		if (start_address != 0) {
L
Linus Torvalds 已提交
1115 1116 1117
			struct ti_i2c_firmware_rec *firmware_version;
			__u8 record;

1118 1119
			dbg("%s - Found Type FIRMWARE (Type 2) record",
								__func__);
L
Linus Torvalds 已提交
1120

1121 1122
			firmware_version = kmalloc(sizeof(*firmware_version),
								GFP_KERNEL);
L
Linus Torvalds 已提交
1123
			if (!firmware_version) {
1124 1125 1126
				dev_err(dev, "%s - out of memory.\n", __func__);
				kfree(rom_desc);
				kfree(ti_manuf_desc);
L
Linus Torvalds 已提交
1127 1128 1129
				return -ENOMEM;
			}

1130 1131 1132 1133 1134
			/* Validate version number
			 * Read the descriptor data
			 */
			status = read_rom(serial, start_address +
					sizeof(struct ti_i2c_desc),
L
Linus Torvalds 已提交
1135 1136 1137
					sizeof(struct ti_i2c_firmware_rec),
					(__u8 *)firmware_version);
			if (status) {
1138 1139 1140
				kfree(firmware_version);
				kfree(rom_desc);
				kfree(ti_manuf_desc);
L
Linus Torvalds 已提交
1141 1142 1143
				return status;
			}

1144 1145 1146
			/* Check version number of download with current
			   version in I2c */
			download_cur_ver = (firmware_version->Ver_Major << 8) +
L
Linus Torvalds 已提交
1147
					   (firmware_version->Ver_Minor);
1148 1149
			download_new_ver = (OperationalMajorVersion << 8) +
					   (OperationalMinorVersion);
L
Linus Torvalds 已提交
1150

1151 1152 1153 1154 1155 1156
			dbg("%s - >> FW Versions Device %d.%d  Driver %d.%d",
			    __func__,
			    firmware_version->Ver_Major,
			    firmware_version->Ver_Minor,
			    OperationalMajorVersion,
			    OperationalMinorVersion);
L
Linus Torvalds 已提交
1157

1158 1159
			/* Check if we have an old version in the I2C and
			   update if necessary */
L
Linus Torvalds 已提交
1160
			if (download_cur_ver != download_new_ver) {
1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179
				dbg("%s - Update I2C dld from %d.%d to %d.%d",
				    __func__,
				    firmware_version->Ver_Major,
				    firmware_version->Ver_Minor,
				    OperationalMajorVersion,
				    OperationalMinorVersion);

				/* In order to update the I2C firmware we must
				 * change the type 2 record to type 0xF2. This
				 * will force the UMP to come up in Boot Mode.
				 * Then while in boot mode, the driver will
				 * download the latest firmware (padded to
				 * 15.5k) into the UMP ram. Finally when the
				 * device comes back up in download mode the
				 * driver will cause the new firmware to be
				 * copied from the UMP Ram to I2C and the
				 * firmware will update the record type from
				 * 0xf2 to 0x02.
				 */
L
Linus Torvalds 已提交
1180 1181
				record = I2C_DESC_TYPE_FIRMWARE_BLANK;

1182 1183 1184 1185
				/* Change the I2C Firmware record type to
				   0xf2 to trigger an update */
				status = write_rom(serial, start_address,
						sizeof(record),	&record);
L
Linus Torvalds 已提交
1186
				if (status) {
1187 1188 1189
					kfree(firmware_version);
					kfree(rom_desc);
					kfree(ti_manuf_desc);
L
Linus Torvalds 已提交
1190 1191 1192
					return status;
				}

1193 1194 1195 1196 1197
				/* verify the write -- must do this in order
				 * for write to complete before we do the
				 * hardware reset
				 */
				status = read_rom(serial,
L
Linus Torvalds 已提交
1198 1199 1200 1201
							start_address,
							sizeof(record),
							&record);
				if (status) {
1202 1203 1204
					kfree(firmware_version);
					kfree(rom_desc);
					kfree(ti_manuf_desc);
L
Linus Torvalds 已提交
1205 1206 1207 1208
					return status;
				}

				if (record != I2C_DESC_TYPE_FIRMWARE_BLANK) {
1209 1210 1211 1212 1213 1214
					dev_err(dev,
						"%s - error resetting device\n",
						__func__);
					kfree(firmware_version);
					kfree(rom_desc);
					kfree(ti_manuf_desc);
L
Linus Torvalds 已提交
1215 1216 1217
					return -ENODEV;
				}

1218
				dbg("%s - HARDWARE RESET", __func__);
L
Linus Torvalds 已提交
1219

1220 1221 1222 1223
				/* Reset UMP -- Back to BOOT MODE */
				status = ti_vsend_sync(serial->serial->dev,
						UMPC_HARDWARE_RESET,
						0, 0, NULL, 0);
L
Linus Torvalds 已提交
1224

1225 1226
				dbg("%s - HARDWARE RESET return %d",
						__func__, status);
L
Linus Torvalds 已提交
1227 1228

				/* return an error on purpose. */
1229 1230 1231
				kfree(firmware_version);
				kfree(rom_desc);
				kfree(ti_manuf_desc);
L
Linus Torvalds 已提交
1232 1233
				return -ENODEV;
			}
1234
			kfree(firmware_version);
L
Linus Torvalds 已提交
1235
		}
1236 1237 1238 1239
		/* Search for type 0xF2 record (firmware blank record) */
		else if ((start_address = get_descriptor_addr(serial, I2C_DESC_TYPE_FIRMWARE_BLANK, rom_desc)) != 0) {
#define HEADER_SIZE	(sizeof(struct ti_i2c_desc) + \
					sizeof(struct ti_i2c_firmware_rec))
L
Linus Torvalds 已提交
1240 1241 1242
			__u8 *header;
			__u8 *vheader;

1243
			header = kmalloc(HEADER_SIZE, GFP_KERNEL);
L
Linus Torvalds 已提交
1244
			if (!header) {
1245 1246 1247
				dev_err(dev, "%s - out of memory.\n", __func__);
				kfree(rom_desc);
				kfree(ti_manuf_desc);
L
Linus Torvalds 已提交
1248 1249
				return -ENOMEM;
			}
1250 1251

			vheader = kmalloc(HEADER_SIZE, GFP_KERNEL);
L
Linus Torvalds 已提交
1252
			if (!vheader) {
1253 1254 1255 1256
				dev_err(dev, "%s - out of memory.\n", __func__);
				kfree(header);
				kfree(rom_desc);
				kfree(ti_manuf_desc);
L
Linus Torvalds 已提交
1257 1258
				return -ENOMEM;
			}
1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274

			dbg("%s - Found Type BLANK FIRMWARE (Type F2) record",
								__func__);

			/*
			 * In order to update the I2C firmware we must change
			 * the type 2 record to type 0xF2. This will force the
			 * UMP to come up in Boot Mode.  Then while in boot
			 * mode, the driver will download the latest firmware
			 * (padded to 15.5k) into the UMP ram. Finally when the
			 * device comes back up in download mode the driver
			 * will cause the new firmware to be copied from the
			 * UMP Ram to I2C and the firmware will update the
			 * record type from 0xf2 to 0x02.
			 */
			status = build_i2c_fw_hdr(header, dev);
L
Linus Torvalds 已提交
1275
			if (status) {
1276 1277 1278 1279
				kfree(vheader);
				kfree(header);
				kfree(rom_desc);
				kfree(ti_manuf_desc);
L
Linus Torvalds 已提交
1280 1281 1282
				return status;
			}

1283 1284 1285
			/* Update I2C with type 0xf2 record with correct
			   size and checksum */
			status = write_rom(serial,
L
Linus Torvalds 已提交
1286 1287 1288 1289
						start_address,
						HEADER_SIZE,
						header);
			if (status) {
1290 1291 1292 1293
				kfree(vheader);
				kfree(header);
				kfree(rom_desc);
				kfree(ti_manuf_desc);
L
Linus Torvalds 已提交
1294 1295 1296
				return status;
			}

1297 1298 1299 1300
			/* verify the write -- must do this in order for
			   write to complete before we do the hardware reset */
			status = read_rom(serial, start_address,
							HEADER_SIZE, vheader);
L
Linus Torvalds 已提交
1301 1302

			if (status) {
1303 1304 1305 1306 1307
				dbg("%s - can't read header back", __func__);
				kfree(vheader);
				kfree(header);
				kfree(rom_desc);
				kfree(ti_manuf_desc);
L
Linus Torvalds 已提交
1308 1309 1310
				return status;
			}
			if (memcmp(vheader, header, HEADER_SIZE)) {
1311 1312 1313 1314 1315 1316
				dbg("%s - write download record failed",
					__func__);
				kfree(vheader);
				kfree(header);
				kfree(rom_desc);
				kfree(ti_manuf_desc);
L
Linus Torvalds 已提交
1317 1318 1319
				return status;
			}

1320 1321
			kfree(vheader);
			kfree(header);
L
Linus Torvalds 已提交
1322

1323
			dbg("%s - Start firmware update", __func__);
L
Linus Torvalds 已提交
1324

1325 1326 1327
			/* Tell firmware to copy download image into I2C */
			status = ti_vsend_sync(serial->serial->dev,
					UMPC_COPY_DNLD_TO_I2C, 0, 0, NULL, 0);
L
Linus Torvalds 已提交
1328

1329
		  	dbg("%s - Update complete 0x%x", __func__, status);
L
Linus Torvalds 已提交
1330
			if (status) {
1331 1332 1333 1334 1335
				dev_err(dev,
					"%s - UMPC_COPY_DNLD_TO_I2C failed\n",
								__func__);
				kfree(rom_desc);
				kfree(ti_manuf_desc);
L
Linus Torvalds 已提交
1336 1337 1338 1339 1340
				return status;
			}
		}

		// The device is running the download code
1341 1342
		kfree(rom_desc);
		kfree(ti_manuf_desc);
L
Linus Torvalds 已提交
1343 1344 1345 1346 1347 1348
		return 0;
	}

	/********************************************************************/
	/* Boot Mode */
	/********************************************************************/
1349
	dbg("%s - RUNNING IN BOOT MODE", __func__);
L
Linus Torvalds 已提交
1350

1351 1352
	/* Configure the TI device so we can use the BULK pipes for download */
	status = config_boot_dev(serial->serial->dev);
L
Linus Torvalds 已提交
1353 1354 1355
	if (status)
		return status;

1356 1357 1358
	if (le16_to_cpu(serial->serial->dev->descriptor.idVendor)
							!= USB_VENDOR_ID_ION) {
		dbg("%s - VID = 0x%x", __func__,
L
Linus Torvalds 已提交
1359 1360
		     le16_to_cpu(serial->serial->dev->descriptor.idVendor));
		serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
1361
		goto stayinbootmode;
L
Linus Torvalds 已提交
1362 1363
	}

1364 1365 1366 1367
	/* We have an ION device (I2c Must be programmed)
	   Determine I2C image type */
	if (i2c_type_bootmode(serial))
		goto stayinbootmode;
L
Linus Torvalds 已提交
1368

1369 1370
	/* Check for ION Vendor ID and that the I2C is valid */
	if (!check_i2c_image(serial)) {
L
Linus Torvalds 已提交
1371 1372 1373 1374 1375
		struct ti_i2c_image_header *header;
		int i;
		__u8 cs = 0;
		__u8 *buffer;
		int buffer_size;
1376 1377 1378
		int err;
		const struct firmware *fw;
		const char *fw_name = "edgeport/down3.bin";
L
Linus Torvalds 已提交
1379 1380 1381 1382

		/* Validate Hardware version number
		 * Read Manufacturing Descriptor from TI Based Edgeport
		 */
1383
		ti_manuf_desc = kmalloc(sizeof(*ti_manuf_desc), GFP_KERNEL);
L
Linus Torvalds 已提交
1384
		if (!ti_manuf_desc) {
1385
			dev_err(dev, "%s - out of memory.\n", __func__);
L
Linus Torvalds 已提交
1386 1387
			return -ENOMEM;
		}
1388
		status = get_manuf_info(serial, (__u8 *)ti_manuf_desc);
L
Linus Torvalds 已提交
1389
		if (status) {
1390 1391
			kfree(ti_manuf_desc);
			goto stayinbootmode;
L
Linus Torvalds 已提交
1392 1393
		}

1394 1395 1396 1397 1398 1399
		/* Check for version 2 */
		if (!ignore_cpu_rev && ti_cpu_rev(ti_manuf_desc) < 2) {
			dbg("%s - Wrong CPU Rev %d (Must be 2)",
					__func__, ti_cpu_rev(ti_manuf_desc));
			kfree(ti_manuf_desc);
			goto stayinbootmode;
L
Linus Torvalds 已提交
1400 1401
		}

1402
		kfree(ti_manuf_desc);
L
Linus Torvalds 已提交
1403 1404

		/*
1405 1406 1407 1408 1409 1410 1411 1412 1413
		 * In order to update the I2C firmware we must change the type
		 * 2 record to type 0xF2. This will force the UMP to come up
		 * in Boot Mode.  Then while in boot mode, the driver will
		 * download the latest firmware (padded to 15.5k) into the
		 * UMP ram. Finally when the device comes back up in download
		 * mode the driver will cause the new firmware to be copied
		 * from the UMP Ram to I2C and the firmware will update the
		 * record type from 0xf2 to 0x02.
		 *
L
Linus Torvalds 已提交
1414 1415 1416 1417
		 * Do we really have to copy the whole firmware image,
		 * or could we do this in place!
		 */

1418 1419 1420 1421
		/* Allocate a 15.5k buffer + 3 byte header */
		buffer_size = (((1024 * 16) - 512) +
					sizeof(struct ti_i2c_image_header));
		buffer = kmalloc(buffer_size, GFP_KERNEL);
L
Linus Torvalds 已提交
1422
		if (!buffer) {
1423
			dev_err(dev, "%s - out of memory\n", __func__);
L
Linus Torvalds 已提交
1424 1425
			return -ENOMEM;
		}
1426 1427 1428

		/* Initialize the buffer to 0xff (pad the buffer) */
		memset(buffer, 0xff, buffer_size);
L
Linus Torvalds 已提交
1429

1430 1431 1432 1433 1434 1435 1436 1437 1438
		err = request_firmware(&fw, fw_name, dev);
		if (err) {
			printk(KERN_ERR "Failed to load image \"%s\" err %d\n",
			       fw_name, err);
			kfree(buffer);
			return err;
		}
		memcpy(buffer, &fw->data[4], fw->size - 4);
		release_firmware(fw);
L
Linus Torvalds 已提交
1439

1440 1441
		for (i = sizeof(struct ti_i2c_image_header);
				i < buffer_size; i++) {
L
Linus Torvalds 已提交
1442 1443
			cs = (__u8)(cs + buffer[i]);
		}
1444

L
Linus Torvalds 已提交
1445
		header = (struct ti_i2c_image_header *)buffer;
1446 1447 1448 1449

		/* update length and checksum after padding */
		header->Length 	 = cpu_to_le16((__u16)(buffer_size -
					sizeof(struct ti_i2c_image_header)));
L
Linus Torvalds 已提交
1450 1451
		header->CheckSum = cs;

1452 1453 1454 1455
		/* Download the operational code  */
		dbg("%s - Downloading operational code image (TI UMP)",
								__func__);
		status = download_code(serial, buffer, buffer_size);
L
Linus Torvalds 已提交
1456

1457
		kfree(buffer);
L
Linus Torvalds 已提交
1458 1459

		if (status) {
1460 1461
			dbg("%s - Error downloading operational code image",
								__func__);
L
Linus Torvalds 已提交
1462 1463 1464
			return status;
		}

1465
		/* Device will reboot */
L
Linus Torvalds 已提交
1466 1467
		serial->product_info.TiMode = TI_MODE_TRANSITIONING;

1468 1469
		dbg("%s - Download successful -- Device rebooting...",
								__func__);
L
Linus Torvalds 已提交
1470 1471 1472 1473 1474

		/* return an error on purpose */
		return -ENODEV;
	}

1475 1476
stayinbootmode:
	/* Eprom is invalid or blank stay in boot mode */
1477
	dbg("%s - STAYING IN BOOT MODE", __func__);
L
Linus Torvalds 已提交
1478 1479 1480 1481 1482 1483
	serial->product_info.TiMode = TI_MODE_BOOT;

	return 0;
}


1484
static int ti_do_config(struct edgeport_port *port, int feature, int on)
L
Linus Torvalds 已提交
1485 1486
{
	int port_number = port->port->number - port->port->serial->minor;
1487 1488 1489 1490
	on = !!on;	/* 1 or 0 not bitmask */
	return send_cmd(port->port->serial->dev,
			feature, (__u8)(UMPM_UART1_PORT + port_number),
			on, NULL, 0);
L
Linus Torvalds 已提交
1491 1492 1493
}


1494
static int restore_mcr(struct edgeport_port *port, __u8 mcr)
L
Linus Torvalds 已提交
1495 1496 1497
{
	int status = 0;

1498
	dbg("%s - %x", __func__, mcr);
L
Linus Torvalds 已提交
1499

1500
	status = ti_do_config(port, UMPC_SET_CLR_DTR, mcr & MCR_DTR);
L
Linus Torvalds 已提交
1501 1502
	if (status)
		return status;
1503
	status = ti_do_config(port, UMPC_SET_CLR_RTS, mcr & MCR_RTS);
L
Linus Torvalds 已提交
1504 1505
	if (status)
		return status;
1506
	return ti_do_config(port, UMPC_SET_CLR_LOOPBACK, mcr & MCR_LOOPBACK);
L
Linus Torvalds 已提交
1507 1508 1509
}

/* Convert TI LSR to standard UART flags */
1510
static __u8 map_line_status(__u8 ti_lsr)
L
Linus Torvalds 已提交
1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521
{
	__u8 lsr = 0;

#define MAP_FLAG(flagUmp, flagUart)    \
	if (ti_lsr & flagUmp) \
		lsr |= flagUart;

	MAP_FLAG(UMP_UART_LSR_OV_MASK, LSR_OVER_ERR)	/* overrun */
	MAP_FLAG(UMP_UART_LSR_PE_MASK, LSR_PAR_ERR)	/* parity error */
	MAP_FLAG(UMP_UART_LSR_FE_MASK, LSR_FRM_ERR)	/* framing error */
	MAP_FLAG(UMP_UART_LSR_BR_MASK, LSR_BREAK)	/* break detected */
1522 1523
	MAP_FLAG(UMP_UART_LSR_RX_MASK, LSR_RX_AVAIL)	/* rx data available */
	MAP_FLAG(UMP_UART_LSR_TX_MASK, LSR_TX_EMPTY)	/* tx hold reg empty */
L
Linus Torvalds 已提交
1524 1525 1526 1527 1528 1529

#undef MAP_FLAG

	return lsr;
}

1530
static void handle_new_msr(struct edgeport_port *edge_port, __u8 msr)
L
Linus Torvalds 已提交
1531 1532 1533 1534
{
	struct async_icount *icount;
	struct tty_struct *tty;

1535
	dbg("%s - %02x", __func__, msr);
L
Linus Torvalds 已提交
1536

1537 1538
	if (msr & (EDGEPORT_MSR_DELTA_CTS | EDGEPORT_MSR_DELTA_DSR |
			EDGEPORT_MSR_DELTA_RI | EDGEPORT_MSR_DELTA_CD)) {
L
Linus Torvalds 已提交
1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549
		icount = &edge_port->icount;

		/* update input line counters */
		if (msr & EDGEPORT_MSR_DELTA_CTS)
			icount->cts++;
		if (msr & EDGEPORT_MSR_DELTA_DSR)
			icount->dsr++;
		if (msr & EDGEPORT_MSR_DELTA_CD)
			icount->dcd++;
		if (msr & EDGEPORT_MSR_DELTA_RI)
			icount->rng++;
1550
		wake_up_interruptible(&edge_port->delta_msr_wait);
L
Linus Torvalds 已提交
1551 1552 1553 1554 1555
	}

	/* Save the new modem status */
	edge_port->shadow_msr = msr & 0xf0;

A
Alan Cox 已提交
1556
	tty = tty_port_tty_get(&edge_port->port->port);
L
Linus Torvalds 已提交
1557 1558 1559 1560 1561 1562 1563 1564 1565
	/* handle CTS flow control */
	if (tty && C_CRTSCTS(tty)) {
		if (msr & EDGEPORT_MSR_CTS) {
			tty->hw_stopped = 0;
			tty_wakeup(tty);
		} else {
			tty->hw_stopped = 1;
		}
	}
A
Alan Cox 已提交
1566
	tty_kref_put(tty);
L
Linus Torvalds 已提交
1567 1568 1569 1570

	return;
}

1571 1572
static void handle_new_lsr(struct edgeport_port *edge_port, int lsr_data,
							__u8 lsr, __u8 data)
L
Linus Torvalds 已提交
1573 1574
{
	struct async_icount *icount;
1575 1576
	__u8 new_lsr = (__u8)(lsr & (__u8)(LSR_OVER_ERR | LSR_PAR_ERR |
						LSR_FRM_ERR | LSR_BREAK));
A
Alan Cox 已提交
1577
	struct tty_struct *tty;
L
Linus Torvalds 已提交
1578

1579
	dbg("%s - %02x", __func__, new_lsr);
L
Linus Torvalds 已提交
1580 1581 1582

	edge_port->shadow_lsr = lsr;

1583
	if (new_lsr & LSR_BREAK)
L
Linus Torvalds 已提交
1584 1585 1586 1587 1588 1589 1590
		/*
		 * Parity and Framing errors only count if they
		 * occur exclusive of a break being received.
		 */
		new_lsr &= (__u8)(LSR_OVER_ERR | LSR_BREAK);

	/* Place LSR data byte into Rx buffer */
A
Alan Cox 已提交
1591 1592 1593 1594 1595 1596 1597
	if (lsr_data) {
		tty = tty_port_tty_get(&edge_port->port->port);
		if (tty) {
			edge_tty_recv(&edge_port->port->dev, tty, &data, 1);
			tty_kref_put(tty);
		}
	}
L
Linus Torvalds 已提交
1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611

	/* update input line counters */
	icount = &edge_port->icount;
	if (new_lsr & LSR_BREAK)
		icount->brk++;
	if (new_lsr & LSR_OVER_ERR)
		icount->overrun++;
	if (new_lsr & LSR_PAR_ERR)
		icount->parity++;
	if (new_lsr & LSR_FRM_ERR)
		icount->frame++;
}


1612
static void edge_interrupt_callback(struct urb *urb)
L
Linus Torvalds 已提交
1613
{
1614
	struct edgeport_serial *edge_serial = urb->context;
L
Linus Torvalds 已提交
1615 1616 1617 1618 1619 1620
	struct usb_serial_port *port;
	struct edgeport_port *edge_port;
	unsigned char *data = urb->transfer_buffer;
	int length = urb->actual_length;
	int port_number;
	int function;
1621
	int retval;
L
Linus Torvalds 已提交
1622 1623
	__u8 lsr;
	__u8 msr;
1624
	int status = urb->status;
L
Linus Torvalds 已提交
1625

1626
	dbg("%s", __func__);
L
Linus Torvalds 已提交
1627

1628
	switch (status) {
L
Linus Torvalds 已提交
1629 1630 1631 1632 1633 1634 1635
	case 0:
		/* success */
		break;
	case -ECONNRESET:
	case -ENOENT:
	case -ESHUTDOWN:
		/* this urb is terminated, clean up */
1636
		dbg("%s - urb shutting down with status: %d",
1637
		    __func__, status);
L
Linus Torvalds 已提交
1638 1639
		return;
	default:
1640
		dev_err(&urb->dev->dev, "%s - nonzero urb status received: "
1641
			"%d\n", __func__, status);
L
Linus Torvalds 已提交
1642 1643 1644 1645
		goto exit;
	}

	if (!length) {
1646
		dbg("%s - no data in urb", __func__);
L
Linus Torvalds 已提交
1647 1648
		goto exit;
	}
1649 1650 1651 1652

	usb_serial_debug_data(debug, &edge_serial->serial->dev->dev,
						__func__, length, data);

L
Linus Torvalds 已提交
1653
	if (length != 2) {
1654 1655
		dbg("%s - expecting packet of size 2, got %d",
							__func__, length);
L
Linus Torvalds 已提交
1656 1657 1658
		goto exit;
	}

1659 1660 1661
	port_number = TIUMP_GET_PORT_FROM_CODE(data[0]);
	function    = TIUMP_GET_FUNC_FROM_CODE(data[0]);
	dbg("%s - port_number %d, function %d, info 0x%x",
1662
	     __func__, port_number, function, data[1]);
L
Linus Torvalds 已提交
1663 1664 1665
	port = edge_serial->serial->port[port_number];
	edge_port = usb_get_serial_port_data(port);
	if (!edge_port) {
1666
		dbg("%s - edge_port not found", __func__);
L
Linus Torvalds 已提交
1667 1668 1669 1670
		return;
	}
	switch (function) {
	case TIUMP_INTERRUPT_CODE_LSR:
1671
		lsr = map_line_status(data[1]);
L
Linus Torvalds 已提交
1672
		if (lsr & UMP_UART_LSR_DATA_MASK) {
1673 1674 1675
			/* Save the LSR event for bulk read
			   completion routine */
			dbg("%s - LSR Event Port %u LSR Status = %02x",
1676
			     __func__, port_number, lsr);
L
Linus Torvalds 已提交
1677 1678 1679
			edge_port->lsr_event = 1;
			edge_port->lsr_mask = lsr;
		} else {
1680
			dbg("%s - ===== Port %d LSR Status = %02x ======",
1681
			     __func__, port_number, lsr);
1682
			handle_new_lsr(edge_port, 0, lsr, 0);
L
Linus Torvalds 已提交
1683 1684 1685
		}
		break;

1686
	case TIUMP_INTERRUPT_CODE_MSR:	/* MSR */
L
Linus Torvalds 已提交
1687 1688
		/* Copy MSR from UMP */
		msr = data[1];
1689
		dbg("%s - ===== Port %u MSR Status = %02x ======\n",
1690
		     __func__, port_number, msr);
1691
		handle_new_msr(edge_port, msr);
L
Linus Torvalds 已提交
1692 1693 1694
		break;

	default:
1695 1696 1697
		dev_err(&urb->dev->dev,
			"%s - Unknown Interrupt code from UMP %x\n",
			__func__, data[1]);
L
Linus Torvalds 已提交
1698
		break;
1699

L
Linus Torvalds 已提交
1700 1701 1702
	}

exit:
1703
	retval = usb_submit_urb(urb, GFP_ATOMIC);
1704
	if (retval)
1705 1706
		dev_err(&urb->dev->dev,
			"%s - usb_submit_urb failed with result %d\n",
1707
			 __func__, retval);
L
Linus Torvalds 已提交
1708 1709
}

1710
static void edge_bulk_in_callback(struct urb *urb)
L
Linus Torvalds 已提交
1711
{
1712
	struct edgeport_port *edge_port = urb->context;
L
Linus Torvalds 已提交
1713 1714
	unsigned char *data = urb->transfer_buffer;
	struct tty_struct *tty;
1715
	int retval = 0;
L
Linus Torvalds 已提交
1716
	int port_number;
1717
	int status = urb->status;
L
Linus Torvalds 已提交
1718

1719
	dbg("%s", __func__);
L
Linus Torvalds 已提交
1720

1721
	switch (status) {
L
Linus Torvalds 已提交
1722 1723 1724 1725 1726 1727 1728
	case 0:
		/* success */
		break;
	case -ECONNRESET:
	case -ENOENT:
	case -ESHUTDOWN:
		/* this urb is terminated, clean up */
1729
		dbg("%s - urb shutting down with status: %d",
1730
		    __func__, status);
L
Linus Torvalds 已提交
1731 1732
		return;
	default:
1733 1734 1735
		dev_err(&urb->dev->dev,
			"%s - nonzero read bulk status received: %d\n",
			     __func__, status);
L
Linus Torvalds 已提交
1736 1737
	}

1738
	if (status == -EPIPE)
L
Linus Torvalds 已提交
1739 1740
		goto exit;

1741
	if (status) {
1742
		dev_err(&urb->dev->dev, "%s - stopping read!\n", __func__);
L
Linus Torvalds 已提交
1743 1744 1745 1746 1747 1748 1749
		return;
	}

	port_number = edge_port->port->number - edge_port->port->serial->minor;

	if (edge_port->lsr_event) {
		edge_port->lsr_event = 0;
1750
		dbg("%s ===== Port %u LSR Status = %02x, Data = %02x ======",
1751
		     __func__, port_number, edge_port->lsr_mask, *data);
1752
		handle_new_lsr(edge_port, 1, edge_port->lsr_mask, *data);
L
Linus Torvalds 已提交
1753 1754 1755 1756 1757
		/* Adjust buffer length/pointer */
		--urb->actual_length;
		++data;
	}

A
Alan Cox 已提交
1758
	tty = tty_port_tty_get(&edge_port->port->port);
L
Linus Torvalds 已提交
1759
	if (tty && urb->actual_length) {
1760 1761 1762 1763 1764 1765 1766 1767
		usb_serial_debug_data(debug, &edge_port->port->dev,
					__func__, urb->actual_length, data);
		if (edge_port->close_pending)
			dbg("%s - close pending, dropping data on the floor",
								__func__);
		else
			edge_tty_recv(&edge_port->port->dev, tty, data,
							urb->actual_length);
L
Linus Torvalds 已提交
1768 1769
		edge_port->icount.rx += urb->actual_length;
	}
A
Alan Cox 已提交
1770
	tty_kref_put(tty);
L
Linus Torvalds 已提交
1771 1772 1773 1774 1775 1776

exit:
	/* continue read unless stopped */
	spin_lock(&edge_port->ep_lock);
	if (edge_port->ep_read_urb_state == EDGE_READ_URB_RUNNING) {
		urb->dev = edge_port->port->serial->dev;
1777
		retval = usb_submit_urb(urb, GFP_ATOMIC);
L
Linus Torvalds 已提交
1778 1779 1780 1781
	} else if (edge_port->ep_read_urb_state == EDGE_READ_URB_STOPPING) {
		edge_port->ep_read_urb_state = EDGE_READ_URB_STOPPED;
	}
	spin_unlock(&edge_port->ep_lock);
1782
	if (retval)
1783 1784
		dev_err(&urb->dev->dev,
			"%s - usb_submit_urb failed with result %d\n",
1785
			 __func__, retval);
L
Linus Torvalds 已提交
1786 1787
}

1788 1789
static void edge_tty_recv(struct device *dev, struct tty_struct *tty,
					unsigned char *data, int length)
L
Linus Torvalds 已提交
1790
{
1791
	int queued;
L
Linus Torvalds 已提交
1792

1793 1794 1795 1796 1797
	tty_buffer_request_room(tty, length);
	queued = tty_insert_flip_string(tty, data, length);
	if (queued < length)
		dev_err(dev, "%s - dropping data, %d bytes lost\n",
			__func__, length - queued);
L
Linus Torvalds 已提交
1798 1799 1800
	tty_flip_buffer_push(tty);
}

1801
static void edge_bulk_out_callback(struct urb *urb)
L
Linus Torvalds 已提交
1802
{
1803
	struct usb_serial_port *port = urb->context;
L
Linus Torvalds 已提交
1804
	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1805
	int status = urb->status;
A
Alan Cox 已提交
1806
	struct tty_struct *tty;
L
Linus Torvalds 已提交
1807

1808
	dbg("%s - port %d", __func__, port->number);
L
Linus Torvalds 已提交
1809 1810 1811

	edge_port->ep_write_urb_in_use = 0;

1812
	switch (status) {
L
Linus Torvalds 已提交
1813 1814 1815 1816 1817 1818 1819
	case 0:
		/* success */
		break;
	case -ECONNRESET:
	case -ENOENT:
	case -ESHUTDOWN:
		/* this urb is terminated, clean up */
1820
		dbg("%s - urb shutting down with status: %d",
1821
		    __func__, status);
L
Linus Torvalds 已提交
1822 1823
		return;
	default:
1824
		dev_err(&urb->dev->dev, "%s - nonzero write bulk status "
1825
			"received: %d\n", __func__, status);
L
Linus Torvalds 已提交
1826 1827 1828
	}

	/* send any buffered data */
A
Alan Cox 已提交
1829 1830 1831
	tty = tty_port_tty_get(&port->port);
	edge_send(tty);
	tty_kref_put(tty);
L
Linus Torvalds 已提交
1832 1833
}

1834
static int edge_open(struct tty_struct *tty, struct usb_serial_port *port)
L
Linus Torvalds 已提交
1835 1836 1837 1838 1839 1840 1841 1842 1843 1844
{
	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
	struct edgeport_serial *edge_serial;
	struct usb_device *dev;
	struct urb *urb;
	int port_number;
	int status;
	u16 open_settings;
	u8 transaction_timeout;

1845
	dbg("%s - port %d", __func__, port->number);
L
Linus Torvalds 已提交
1846 1847 1848 1849 1850 1851

	if (edge_port == NULL)
		return -ENODEV;

	port_number = port->number - port->serial->minor;
	switch (port_number) {
1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862
	case 0:
		edge_port->uart_base = UMPMEM_BASE_UART1;
		edge_port->dma_address = UMPD_OEDB1_ADDRESS;
		break;
	case 1:
		edge_port->uart_base = UMPMEM_BASE_UART2;
		edge_port->dma_address = UMPD_OEDB2_ADDRESS;
		break;
	default:
		dev_err(&port->dev, "Unknown port number!!!\n");
		return -ENODEV;
L
Linus Torvalds 已提交
1863 1864
	}

1865 1866 1867
	dbg("%s - port_number = %d, uart_base = %04x, dma_address = %04x",
				__func__, port_number, edge_port->uart_base,
				edge_port->dma_address);
L
Linus Torvalds 已提交
1868 1869 1870

	dev = port->serial->dev;

1871 1872
	memset(&(edge_port->icount), 0x00, sizeof(edge_port->icount));
	init_waitqueue_head(&edge_port->delta_msr_wait);
L
Linus Torvalds 已提交
1873 1874

	/* turn off loopback */
1875
	status = ti_do_config(edge_port, UMPC_SET_CLR_LOOPBACK, 0);
L
Linus Torvalds 已提交
1876
	if (status) {
1877 1878
		dev_err(&port->dev,
				"%s - cannot send clear loopback command, %d\n",
1879
			__func__, status);
L
Linus Torvalds 已提交
1880 1881
		return status;
	}
1882

L
Linus Torvalds 已提交
1883
	/* set up the port settings */
A
Alan Cox 已提交
1884
	if (tty)
A
Alan Cox 已提交
1885
		edge_set_termios(tty, port, tty->termios);
L
Linus Torvalds 已提交
1886 1887 1888 1889 1890 1891

	/* open up the port */

	/* milliseconds to timeout for DMA transfer */
	transaction_timeout = 2;

1892 1893
	edge_port->ump_read_timeout =
				max(20, ((transaction_timeout * 3) / 2));
L
Linus Torvalds 已提交
1894

1895 1896 1897
	/* milliseconds to timeout for DMA transfer */
	open_settings = (u8)(UMP_DMA_MODE_CONTINOUS |
			     UMP_PIPE_TRANS_TIMEOUT_ENA |
L
Linus Torvalds 已提交
1898 1899
			     (transaction_timeout << 2));

1900
	dbg("%s - Sending UMPC_OPEN_PORT", __func__);
L
Linus Torvalds 已提交
1901 1902

	/* Tell TI to open and start the port */
1903 1904
	status = send_cmd(dev, UMPC_OPEN_PORT,
		(u8)(UMPM_UART1_PORT + port_number), open_settings, NULL, 0);
L
Linus Torvalds 已提交
1905
	if (status) {
1906 1907
		dev_err(&port->dev, "%s - cannot send open command, %d\n",
							__func__, status);
L
Linus Torvalds 已提交
1908 1909 1910 1911
		return status;
	}

	/* Start the DMA? */
1912 1913
	status = send_cmd(dev, UMPC_START_PORT,
		(u8)(UMPM_UART1_PORT + port_number), 0, NULL, 0);
L
Linus Torvalds 已提交
1914
	if (status) {
1915 1916
		dev_err(&port->dev, "%s - cannot send start DMA command, %d\n",
							__func__, status);
L
Linus Torvalds 已提交
1917 1918 1919 1920
		return status;
	}

	/* Clear TX and RX buffers in UMP */
1921
	status = purge_port(port, UMP_PORT_DIR_OUT | UMP_PORT_DIR_IN);
L
Linus Torvalds 已提交
1922
	if (status) {
1923 1924 1925
		dev_err(&port->dev,
			"%s - cannot send clear buffers command, %d\n",
			__func__, status);
L
Linus Torvalds 已提交
1926 1927 1928 1929
		return status;
	}

	/* Read Initial MSR */
1930 1931 1932
	status = ti_vread_sync(dev, UMPC_READ_MSR, 0,
				(__u16)(UMPM_UART1_PORT + port_number),
				&edge_port->shadow_msr, 1);
L
Linus Torvalds 已提交
1933
	if (status) {
1934 1935
		dev_err(&port->dev, "%s - cannot send read MSR command, %d\n",
							__func__, status);
L
Linus Torvalds 已提交
1936 1937 1938
		return status;
	}

1939 1940
	dbg("ShadowMSR 0x%X", edge_port->shadow_msr);

L
Linus Torvalds 已提交
1941 1942
	/* Set Initial MCR */
	edge_port->shadow_mcr = MCR_RTS | MCR_DTR;
1943
	dbg("ShadowMCR 0x%X", edge_port->shadow_mcr);
L
Linus Torvalds 已提交
1944 1945

	edge_serial = edge_port->edge_serial;
1946
	if (mutex_lock_interruptible(&edge_serial->es_lock))
L
Linus Torvalds 已提交
1947 1948
		return -ERESTARTSYS;
	if (edge_serial->num_ports_open == 0) {
1949
		/* we are the first port to open, post the interrupt urb */
L
Linus Torvalds 已提交
1950 1951
		urb = edge_serial->serial->port[0]->interrupt_in_urb;
		if (!urb) {
1952 1953 1954
			dev_err(&port->dev,
				"%s - no interrupt urb present, exiting\n",
				__func__);
L
Linus Torvalds 已提交
1955
			status = -EINVAL;
1956
			goto release_es_lock;
L
Linus Torvalds 已提交
1957 1958 1959 1960
		}
		urb->complete = edge_interrupt_callback;
		urb->context = edge_serial;
		urb->dev = dev;
1961
		status = usb_submit_urb(urb, GFP_KERNEL);
L
Linus Torvalds 已提交
1962
		if (status) {
1963 1964 1965
			dev_err(&port->dev,
				"%s - usb_submit_urb failed with value %d\n",
					__func__, status);
1966
			goto release_es_lock;
L
Linus Torvalds 已提交
1967 1968 1969 1970 1971 1972 1973
		}
	}

	/*
	 * reset the data toggle on the bulk endpoints to work around bug in
	 * host controllers where things get out of sync some times
	 */
1974 1975
	usb_clear_halt(dev, port->write_urb->pipe);
	usb_clear_halt(dev, port->read_urb->pipe);
L
Linus Torvalds 已提交
1976 1977 1978 1979

	/* start up our bulk read urb */
	urb = port->read_urb;
	if (!urb) {
1980 1981
		dev_err(&port->dev, "%s - no read urb present, exiting\n",
								__func__);
L
Linus Torvalds 已提交
1982 1983 1984 1985 1986 1987 1988
		status = -EINVAL;
		goto unlink_int_urb;
	}
	edge_port->ep_read_urb_state = EDGE_READ_URB_RUNNING;
	urb->complete = edge_bulk_in_callback;
	urb->context = edge_port;
	urb->dev = dev;
1989
	status = usb_submit_urb(urb, GFP_KERNEL);
L
Linus Torvalds 已提交
1990
	if (status) {
1991 1992 1993
		dev_err(&port->dev,
			"%s - read bulk usb_submit_urb failed with value %d\n",
				__func__, status);
L
Linus Torvalds 已提交
1994 1995 1996 1997 1998
		goto unlink_int_urb;
	}

	++edge_serial->num_ports_open;

1999
	dbg("%s - exited", __func__);
L
Linus Torvalds 已提交
2000

2001
	goto release_es_lock;
L
Linus Torvalds 已提交
2002 2003 2004 2005

unlink_int_urb:
	if (edge_port->edge_serial->num_ports_open == 0)
		usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
2006 2007
release_es_lock:
	mutex_unlock(&edge_serial->es_lock);
L
Linus Torvalds 已提交
2008 2009 2010
	return status;
}

2011
static void edge_close(struct usb_serial_port *port)
L
Linus Torvalds 已提交
2012 2013 2014 2015 2016 2017
{
	struct edgeport_serial *edge_serial;
	struct edgeport_port *edge_port;
	int port_number;
	int status;

2018
	dbg("%s - port %d", __func__, port->number);
2019

L
Linus Torvalds 已提交
2020 2021
	edge_serial = usb_get_serial_data(port->serial);
	edge_port = usb_get_serial_port_data(port);
2022
	if (edge_serial == NULL || edge_port == NULL)
L
Linus Torvalds 已提交
2023
		return;
2024 2025

	/* The bulkreadcompletion routine will check
L
Linus Torvalds 已提交
2026 2027 2028 2029
	 * this flag and dump add read data */
	edge_port->close_pending = 1;

	/* chase the port close and flush */
2030
	chase_port(edge_port, (HZ * closing_wait) / 100, 1);
L
Linus Torvalds 已提交
2031 2032 2033 2034 2035 2036 2037

	usb_kill_urb(port->read_urb);
	usb_kill_urb(port->write_urb);
	edge_port->ep_write_urb_in_use = 0;

	/* assuming we can still talk to the device,
	 * send a close port command to it */
2038
	dbg("%s - send umpc_close_port", __func__);
L
Linus Torvalds 已提交
2039
	port_number = port->number - port->serial->minor;
2040
	status = send_cmd(port->serial->dev,
L
Linus Torvalds 已提交
2041 2042 2043 2044 2045
				     UMPC_CLOSE_PORT,
				     (__u8)(UMPM_UART1_PORT + port_number),
				     0,
				     NULL,
				     0);
2046
	mutex_lock(&edge_serial->es_lock);
L
Linus Torvalds 已提交
2047 2048 2049 2050 2051 2052
	--edge_port->edge_serial->num_ports_open;
	if (edge_port->edge_serial->num_ports_open <= 0) {
		/* last port is now closed, let's shut down our interrupt urb */
		usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
		edge_port->edge_serial->num_ports_open = 0;
	}
2053
	mutex_unlock(&edge_serial->es_lock);
L
Linus Torvalds 已提交
2054 2055
	edge_port->close_pending = 0;

2056
	dbg("%s - exited", __func__);
L
Linus Torvalds 已提交
2057 2058
}

A
Alan Cox 已提交
2059 2060
static int edge_write(struct tty_struct *tty, struct usb_serial_port *port,
				const unsigned char *data, int count)
L
Linus Torvalds 已提交
2061 2062 2063 2064
{
	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
	unsigned long flags;

2065
	dbg("%s - port %d", __func__, port->number);
L
Linus Torvalds 已提交
2066 2067

	if (count == 0) {
2068
		dbg("%s - write request of 0 bytes", __func__);
L
Linus Torvalds 已提交
2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080
		return 0;
	}

	if (edge_port == NULL)
		return -ENODEV;
	if (edge_port->close_pending == 1)
		return -ENODEV;

	spin_lock_irqsave(&edge_port->ep_lock, flags);
	count = edge_buf_put(edge_port->ep_out_buf, data, count);
	spin_unlock_irqrestore(&edge_port->ep_lock, flags);

A
Alan Cox 已提交
2081
	edge_send(tty);
L
Linus Torvalds 已提交
2082 2083 2084 2085

	return count;
}

A
Alan Cox 已提交
2086
static void edge_send(struct tty_struct *tty)
L
Linus Torvalds 已提交
2087
{
A
Alan Cox 已提交
2088
	struct usb_serial_port *port = tty->driver_data;
L
Linus Torvalds 已提交
2089 2090 2091 2092 2093
	int count, result;
	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
	unsigned long flags;


2094
	dbg("%s - port %d", __func__, port->number);
L
Linus Torvalds 已提交
2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115

	spin_lock_irqsave(&edge_port->ep_lock, flags);

	if (edge_port->ep_write_urb_in_use) {
		spin_unlock_irqrestore(&edge_port->ep_lock, flags);
		return;
	}

	count = edge_buf_get(edge_port->ep_out_buf,
				port->write_urb->transfer_buffer,
				port->bulk_out_size);

	if (count == 0) {
		spin_unlock_irqrestore(&edge_port->ep_lock, flags);
		return;
	}

	edge_port->ep_write_urb_in_use = 1;

	spin_unlock_irqrestore(&edge_port->ep_lock, flags);

2116 2117
	usb_serial_debug_data(debug, &port->dev, __func__, count,
				port->write_urb->transfer_buffer);
L
Linus Torvalds 已提交
2118 2119

	/* set up our urb */
2120 2121
	usb_fill_bulk_urb(port->write_urb, port->serial->dev,
			   usb_sndbulkpipe(port->serial->dev,
L
Linus Torvalds 已提交
2122 2123 2124 2125 2126 2127 2128 2129
					    port->bulk_out_endpointAddress),
			   port->write_urb->transfer_buffer, count,
			   edge_bulk_out_callback,
			   port);

	/* send the data out the bulk port */
	result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
	if (result) {
2130 2131 2132
		dev_err(&port->dev,
			"%s - failed submitting write urb, error %d\n",
				__func__, result);
L
Linus Torvalds 已提交
2133
		edge_port->ep_write_urb_in_use = 0;
2134 2135
		/* TODO: reschedule edge_send */
	} else
L
Linus Torvalds 已提交
2136 2137 2138 2139
		edge_port->icount.tx += count;

	/* wakeup any process waiting for writes to complete */
	/* there is now more room in the buffer for new writes */
2140
	if (tty)
L
Linus Torvalds 已提交
2141 2142 2143
		tty_wakeup(tty);
}

A
Alan Cox 已提交
2144
static int edge_write_room(struct tty_struct *tty)
L
Linus Torvalds 已提交
2145
{
A
Alan Cox 已提交
2146
	struct usb_serial_port *port = tty->driver_data;
L
Linus Torvalds 已提交
2147 2148 2149 2150
	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
	int room = 0;
	unsigned long flags;

2151
	dbg("%s - port %d", __func__, port->number);
L
Linus Torvalds 已提交
2152 2153

	if (edge_port == NULL)
2154
		return 0;
L
Linus Torvalds 已提交
2155
	if (edge_port->close_pending == 1)
2156
		return 0;
L
Linus Torvalds 已提交
2157 2158 2159 2160 2161

	spin_lock_irqsave(&edge_port->ep_lock, flags);
	room = edge_buf_space_avail(edge_port->ep_out_buf);
	spin_unlock_irqrestore(&edge_port->ep_lock, flags);

2162
	dbg("%s - returns %d", __func__, room);
L
Linus Torvalds 已提交
2163 2164 2165
	return room;
}

A
Alan Cox 已提交
2166
static int edge_chars_in_buffer(struct tty_struct *tty)
L
Linus Torvalds 已提交
2167
{
A
Alan Cox 已提交
2168
	struct usb_serial_port *port = tty->driver_data;
L
Linus Torvalds 已提交
2169 2170 2171 2172
	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
	int chars = 0;
	unsigned long flags;

2173
	dbg("%s - port %d", __func__, port->number);
L
Linus Torvalds 已提交
2174 2175

	if (edge_port == NULL)
2176
		return 0;
L
Linus Torvalds 已提交
2177
	if (edge_port->close_pending == 1)
2178
		return 0;
L
Linus Torvalds 已提交
2179 2180 2181 2182 2183

	spin_lock_irqsave(&edge_port->ep_lock, flags);
	chars = edge_buf_data_avail(edge_port->ep_out_buf);
	spin_unlock_irqrestore(&edge_port->ep_lock, flags);

2184
	dbg("%s - returns %d", __func__, chars);
L
Linus Torvalds 已提交
2185 2186 2187
	return chars;
}

A
Alan Cox 已提交
2188
static void edge_throttle(struct tty_struct *tty)
L
Linus Torvalds 已提交
2189
{
A
Alan Cox 已提交
2190
	struct usb_serial_port *port = tty->driver_data;
L
Linus Torvalds 已提交
2191 2192 2193
	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
	int status;

2194
	dbg("%s - port %d", __func__, port->number);
L
Linus Torvalds 已提交
2195 2196 2197 2198 2199 2200 2201

	if (edge_port == NULL)
		return;

	/* if we are implementing XON/XOFF, send the stop character */
	if (I_IXOFF(tty)) {
		unsigned char stop_char = STOP_CHAR(tty);
A
Alan Cox 已提交
2202 2203 2204 2205
		status = edge_write(tty, port, &stop_char, 1);
		if (status <= 0) {
			dev_err(&port->dev, "%s - failed to write stop character, %d\n", __func__, status);
		}
L
Linus Torvalds 已提交
2206 2207 2208 2209 2210 2211 2212 2213 2214
	}

	/* if we are implementing RTS/CTS, stop reads */
	/* and the Edgeport will clear the RTS line */
	if (C_CRTSCTS(tty))
		stop_read(edge_port);

}

A
Alan Cox 已提交
2215
static void edge_unthrottle(struct tty_struct *tty)
L
Linus Torvalds 已提交
2216
{
A
Alan Cox 已提交
2217
	struct usb_serial_port *port = tty->driver_data;
L
Linus Torvalds 已提交
2218 2219 2220
	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
	int status;

2221
	dbg("%s - port %d", __func__, port->number);
L
Linus Torvalds 已提交
2222 2223 2224 2225 2226 2227 2228

	if (edge_port == NULL)
		return;

	/* if we are implementing XON/XOFF, send the start character */
	if (I_IXOFF(tty)) {
		unsigned char start_char = START_CHAR(tty);
A
Alan Cox 已提交
2229 2230 2231 2232
		status = edge_write(tty, port, &start_char, 1);
		if (status <= 0) {
			dev_err(&port->dev, "%s - failed to write start character, %d\n", __func__, status);
		}
L
Linus Torvalds 已提交
2233 2234 2235 2236 2237 2238
	}
	/* if we are implementing RTS/CTS, restart reads */
	/* are the Edgeport will assert the RTS line */
	if (C_CRTSCTS(tty)) {
		status = restart_read(edge_port);
		if (status)
2239 2240 2241
			dev_err(&port->dev,
				"%s - read bulk usb_submit_urb failed: %d\n",
							__func__, status);
L
Linus Torvalds 已提交
2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271
	}

}

static void stop_read(struct edgeport_port *edge_port)
{
	unsigned long flags;

	spin_lock_irqsave(&edge_port->ep_lock, flags);

	if (edge_port->ep_read_urb_state == EDGE_READ_URB_RUNNING)
		edge_port->ep_read_urb_state = EDGE_READ_URB_STOPPING;
	edge_port->shadow_mcr &= ~MCR_RTS;

	spin_unlock_irqrestore(&edge_port->ep_lock, flags);
}

static int restart_read(struct edgeport_port *edge_port)
{
	struct urb *urb;
	int status = 0;
	unsigned long flags;

	spin_lock_irqsave(&edge_port->ep_lock, flags);

	if (edge_port->ep_read_urb_state == EDGE_READ_URB_STOPPED) {
		urb = edge_port->port->read_urb;
		urb->complete = edge_bulk_in_callback;
		urb->context = edge_port;
		urb->dev = edge_port->port->serial->dev;
2272
		status = usb_submit_urb(urb, GFP_ATOMIC);
L
Linus Torvalds 已提交
2273 2274 2275 2276 2277 2278 2279 2280 2281
	}
	edge_port->ep_read_urb_state = EDGE_READ_URB_RUNNING;
	edge_port->shadow_mcr |= MCR_RTS;

	spin_unlock_irqrestore(&edge_port->ep_lock, flags);

	return status;
}

A
Alan Cox 已提交
2282 2283
static void change_port_settings(struct tty_struct *tty,
		struct edgeport_port *edge_port, struct ktermios *old_termios)
L
Linus Torvalds 已提交
2284 2285 2286 2287 2288
{
	struct ump_uart_config *config;
	int baud;
	unsigned cflag;
	int status;
2289 2290
	int port_number = edge_port->port->number -
					edge_port->port->serial->minor;
L
Linus Torvalds 已提交
2291

2292
	dbg("%s - port %d", __func__, edge_port->port->number);
L
Linus Torvalds 已提交
2293

A
Alan Cox 已提交
2294
	config = kmalloc (sizeof (*config), GFP_KERNEL);
L
Linus Torvalds 已提交
2295
	if (!config) {
2296 2297 2298
		*tty->termios = *old_termios;
		dev_err(&edge_port->port->dev, "%s - out of memory\n",
								__func__);
L
Linus Torvalds 已提交
2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311
		return;
	}

	cflag = tty->termios->c_cflag;

	config->wFlags = 0;

	/* These flags must be set */
	config->wFlags |= UMP_MASK_UART_FLAGS_RECEIVE_MS_INT;
	config->wFlags |= UMP_MASK_UART_FLAGS_AUTO_START_ON_ERR;
	config->bUartMode = (__u8)(edge_port->bUartMode);

	switch (cflag & CSIZE) {
2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327
	case CS5:
		    config->bDataBits = UMP_UART_CHAR5BITS;
		    dbg("%s - data bits = 5", __func__);
		    break;
	case CS6:
		    config->bDataBits = UMP_UART_CHAR6BITS;
		    dbg("%s - data bits = 6", __func__);
		    break;
	case CS7:
		    config->bDataBits = UMP_UART_CHAR7BITS;
		    dbg("%s - data bits = 7", __func__);
		    break;
	default:
	case CS8:
		    config->bDataBits = UMP_UART_CHAR8BITS;
		    dbg("%s - data bits = 8", __func__);
L
Linus Torvalds 已提交
2328 2329 2330 2331 2332 2333 2334
			    break;
	}

	if (cflag & PARENB) {
		if (cflag & PARODD) {
			config->wFlags |= UMP_MASK_UART_FLAGS_PARITY;
			config->bParity = UMP_UART_ODDPARITY;
2335
			dbg("%s - parity = odd", __func__);
L
Linus Torvalds 已提交
2336 2337 2338
		} else {
			config->wFlags |= UMP_MASK_UART_FLAGS_PARITY;
			config->bParity = UMP_UART_EVENPARITY;
2339
			dbg("%s - parity = even", __func__);
L
Linus Torvalds 已提交
2340 2341
		}
	} else {
2342
		config->bParity = UMP_UART_NOPARITY;
2343
		dbg("%s - parity = none", __func__);
L
Linus Torvalds 已提交
2344 2345 2346 2347
	}

	if (cflag & CSTOPB) {
		config->bStopBits = UMP_UART_STOPBIT2;
2348
		dbg("%s - stop bits = 2", __func__);
L
Linus Torvalds 已提交
2349 2350
	} else {
		config->bStopBits = UMP_UART_STOPBIT1;
2351
		dbg("%s - stop bits = 1", __func__);
L
Linus Torvalds 已提交
2352 2353 2354 2355 2356 2357
	}

	/* figure out the flow control settings */
	if (cflag & CRTSCTS) {
		config->wFlags |= UMP_MASK_UART_FLAGS_OUT_X_CTS_FLOW;
		config->wFlags |= UMP_MASK_UART_FLAGS_RTS_FLOW;
2358
		dbg("%s - RTS/CTS is enabled", __func__);
L
Linus Torvalds 已提交
2359
	} else {
2360
		dbg("%s - RTS/CTS is disabled", __func__);
L
Linus Torvalds 已提交
2361 2362 2363 2364
		tty->hw_stopped = 0;
		restart_read(edge_port);
	}

2365 2366 2367 2368
	/* if we are implementing XON/XOFF, set the start and stop
	   character in the device */
	config->cXon  = START_CHAR(tty);
	config->cXoff = STOP_CHAR(tty);
L
Linus Torvalds 已提交
2369

2370 2371 2372 2373 2374 2375 2376
	/* if we are implementing INBOUND XON/XOFF */
	if (I_IXOFF(tty)) {
		config->wFlags |= UMP_MASK_UART_FLAGS_IN_X;
		dbg("%s - INBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x",
		     __func__, config->cXon, config->cXoff);
	} else
		dbg("%s - INBOUND XON/XOFF is disabled", __func__);
L
Linus Torvalds 已提交
2377

2378 2379 2380 2381 2382 2383 2384
	/* if we are implementing OUTBOUND XON/XOFF */
	if (I_IXON(tty)) {
		config->wFlags |= UMP_MASK_UART_FLAGS_OUT_X;
		dbg("%s - OUTBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x",
		     __func__, config->cXon, config->cXoff);
	} else
		dbg("%s - OUTBOUND XON/XOFF is disabled", __func__);
L
Linus Torvalds 已提交
2385

A
Alan Cox 已提交
2386 2387
	tty->termios->c_cflag &= ~CMSPAR;

L
Linus Torvalds 已提交
2388 2389 2390 2391 2392
	/* Round the baud rate */
	baud = tty_get_baud_rate(tty);
	if (!baud) {
		/* pick a default, any default... */
		baud = 9600;
A
Alan Cox 已提交
2393 2394 2395
	} else
		tty_encode_baud_rate(tty, baud, baud);

L
Linus Torvalds 已提交
2396 2397 2398
	edge_port->baud_rate = baud;
	config->wBaudRate = (__u16)((461550L + baud/2) / baud);

A
Alan Cox 已提交
2399 2400
	/* FIXME: Recompute actual baud from divisor here */

2401 2402
	dbg("%s - baud rate = %d, wBaudRate = %d", __func__, baud,
							config->wBaudRate);
L
Linus Torvalds 已提交
2403

2404 2405 2406 2407 2408 2409 2410 2411
	dbg("wBaudRate:   %d", (int)(461550L / config->wBaudRate));
	dbg("wFlags:    0x%x", config->wFlags);
	dbg("bDataBits:   %d", config->bDataBits);
	dbg("bParity:     %d", config->bParity);
	dbg("bStopBits:   %d", config->bStopBits);
	dbg("cXon:        %d", config->cXon);
	dbg("cXoff:       %d", config->cXoff);
	dbg("bUartMode:   %d", config->bUartMode);
L
Linus Torvalds 已提交
2412 2413

	/* move the word values into big endian mode */
2414 2415
	cpu_to_be16s(&config->wFlags);
	cpu_to_be16s(&config->wBaudRate);
L
Linus Torvalds 已提交
2416

2417
	status = send_cmd(edge_port->port->serial->dev, UMPC_SET_CONFIG,
L
Linus Torvalds 已提交
2418
				(__u8)(UMPM_UART1_PORT + port_number),
2419 2420 2421
				0, (__u8 *)config, sizeof(*config));
	if (status)
		dbg("%s - error %d when trying to write config to device",
2422
		     __func__, status);
2423
	kfree(config);
L
Linus Torvalds 已提交
2424 2425 2426
	return;
}

A
Alan Cox 已提交
2427
static void edge_set_termios(struct tty_struct *tty,
A
Alan Cox 已提交
2428
		struct usb_serial_port *port, struct ktermios *old_termios)
L
Linus Torvalds 已提交
2429 2430
{
	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
A
Alan Cox 已提交
2431 2432 2433
	unsigned int cflag;

	cflag = tty->termios->c_cflag;
L
Linus Torvalds 已提交
2434

2435
	dbg("%s - clfag %08x iflag %08x", __func__,
L
Linus Torvalds 已提交
2436
	    tty->termios->c_cflag, tty->termios->c_iflag);
2437
	dbg("%s - old clfag %08x old iflag %08x", __func__,
A
Alan Cox 已提交
2438
	    old_termios->c_cflag, old_termios->c_iflag);
2439
	dbg("%s - port %d", __func__, port->number);
L
Linus Torvalds 已提交
2440 2441 2442 2443

	if (edge_port == NULL)
		return;
	/* change the port settings to the new ones specified */
A
Alan Cox 已提交
2444 2445
	change_port_settings(tty, edge_port, old_termios);
	return;
L
Linus Torvalds 已提交
2446 2447
}

A
Alan Cox 已提交
2448
static int edge_tiocmset(struct tty_struct *tty, struct file *file,
2449
					unsigned int set, unsigned int clear)
L
Linus Torvalds 已提交
2450
{
A
Alan Cox 已提交
2451
	struct usb_serial_port *port = tty->driver_data;
L
Linus Torvalds 已提交
2452 2453
	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
	unsigned int mcr;
2454
	unsigned long flags;
L
Linus Torvalds 已提交
2455

2456
	dbg("%s - port %d", __func__, port->number);
L
Linus Torvalds 已提交
2457

2458
	spin_lock_irqsave(&edge_port->ep_lock, flags);
L
Linus Torvalds 已提交
2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474
	mcr = edge_port->shadow_mcr;
	if (set & TIOCM_RTS)
		mcr |= MCR_RTS;
	if (set & TIOCM_DTR)
		mcr |= MCR_DTR;
	if (set & TIOCM_LOOP)
		mcr |= MCR_LOOPBACK;

	if (clear & TIOCM_RTS)
		mcr &= ~MCR_RTS;
	if (clear & TIOCM_DTR)
		mcr &= ~MCR_DTR;
	if (clear & TIOCM_LOOP)
		mcr &= ~MCR_LOOPBACK;

	edge_port->shadow_mcr = mcr;
2475
	spin_unlock_irqrestore(&edge_port->ep_lock, flags);
L
Linus Torvalds 已提交
2476

2477
	restore_mcr(edge_port, mcr);
L
Linus Torvalds 已提交
2478 2479 2480
	return 0;
}

A
Alan Cox 已提交
2481
static int edge_tiocmget(struct tty_struct *tty, struct file *file)
L
Linus Torvalds 已提交
2482
{
A
Alan Cox 已提交
2483
	struct usb_serial_port *port = tty->driver_data;
L
Linus Torvalds 已提交
2484 2485 2486 2487
	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
	unsigned int result = 0;
	unsigned int msr;
	unsigned int mcr;
2488
	unsigned long flags;
L
Linus Torvalds 已提交
2489

2490
	dbg("%s - port %d", __func__, port->number);
L
Linus Torvalds 已提交
2491

2492 2493
	spin_lock_irqsave(&edge_port->ep_lock, flags);

L
Linus Torvalds 已提交
2494 2495 2496 2497 2498 2499 2500 2501 2502 2503
	msr = edge_port->shadow_msr;
	mcr = edge_port->shadow_mcr;
	result = ((mcr & MCR_DTR)	? TIOCM_DTR: 0)	  /* 0x002 */
		  | ((mcr & MCR_RTS)	? TIOCM_RTS: 0)   /* 0x004 */
		  | ((msr & EDGEPORT_MSR_CTS)	? TIOCM_CTS: 0)   /* 0x020 */
		  | ((msr & EDGEPORT_MSR_CD)	? TIOCM_CAR: 0)   /* 0x040 */
		  | ((msr & EDGEPORT_MSR_RI)	? TIOCM_RI:  0)   /* 0x080 */
		  | ((msr & EDGEPORT_MSR_DSR)	? TIOCM_DSR: 0);  /* 0x100 */


2504
	dbg("%s -- %x", __func__, result);
2505
	spin_unlock_irqrestore(&edge_port->ep_lock, flags);
L
Linus Torvalds 已提交
2506 2507 2508 2509

	return result;
}

2510 2511
static int get_serial_info(struct edgeport_port *edge_port,
				struct serial_struct __user *retinfo)
L
Linus Torvalds 已提交
2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534
{
	struct serial_struct tmp;

	if (!retinfo)
		return -EFAULT;

	memset(&tmp, 0, sizeof(tmp));

	tmp.type		= PORT_16550A;
	tmp.line		= edge_port->port->serial->minor;
	tmp.port		= edge_port->port->number;
	tmp.irq			= 0;
	tmp.flags		= ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
	tmp.xmit_fifo_size	= edge_port->port->bulk_out_size;
	tmp.baud_base		= 9600;
	tmp.close_delay		= 5*HZ;
	tmp.closing_wait	= closing_wait;

	if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
		return -EFAULT;
	return 0;
}

A
Alan Cox 已提交
2535
static int edge_ioctl(struct tty_struct *tty, struct file *file,
2536
					unsigned int cmd, unsigned long arg)
L
Linus Torvalds 已提交
2537
{
A
Alan Cox 已提交
2538
	struct usb_serial_port *port = tty->driver_data;
L
Linus Torvalds 已提交
2539 2540 2541 2542
	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
	struct async_icount cnow;
	struct async_icount cprev;

2543
	dbg("%s - port %d, cmd = 0x%x", __func__, port->number, cmd);
L
Linus Torvalds 已提交
2544 2545

	switch (cmd) {
2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566
	case TIOCGSERIAL:
		dbg("%s - (%d) TIOCGSERIAL", __func__, port->number);
		return get_serial_info(edge_port,
				(struct serial_struct __user *) arg);
	case TIOCMIWAIT:
		dbg("%s - (%d) TIOCMIWAIT", __func__, port->number);
		cprev = edge_port->icount;
		while (1) {
			interruptible_sleep_on(&edge_port->delta_msr_wait);
			/* see if a signal did it */
			if (signal_pending(current))
				return -ERESTARTSYS;
			cnow = edge_port->icount;
			if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
			    cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
				return -EIO; /* no change => error */
			if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
			    ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
			    ((arg & TIOCM_CD)  && (cnow.dcd != cprev.dcd)) ||
			    ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
				return 0;
L
Linus Torvalds 已提交
2567
			}
2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578
			cprev = cnow;
		}
		/* not reached */
		break;
	case TIOCGICOUNT:
		dbg("%s - (%d) TIOCGICOUNT RX=%d, TX=%d", __func__,
		     port->number, edge_port->icount.rx, edge_port->icount.tx);
		if (copy_to_user((void __user *)arg, &edge_port->icount,
				sizeof(edge_port->icount)))
			return -EFAULT;
		return 0;
L
Linus Torvalds 已提交
2579 2580 2581 2582
	}
	return -ENOIOCTLCMD;
}

A
Alan Cox 已提交
2583
static void edge_break(struct tty_struct *tty, int break_state)
L
Linus Torvalds 已提交
2584
{
A
Alan Cox 已提交
2585
	struct usb_serial_port *port = tty->driver_data;
L
Linus Torvalds 已提交
2586 2587
	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
	int status;
2588
	int bv = 0;	/* Off */
L
Linus Torvalds 已提交
2589

A
Alan Cox 已提交
2590
	dbg("%s - state = %d", __func__, break_state);
L
Linus Torvalds 已提交
2591 2592

	/* chase the port close */
2593
	chase_port(edge_port, 0, 0);
L
Linus Torvalds 已提交
2594

A
Alan Cox 已提交
2595
	if (break_state == -1)
2596 2597 2598 2599
		bv = 1;	/* On */
	status = ti_do_config(edge_port, UMPC_SET_CLR_BREAK, bv);
	if (status)
		dbg("%s - error %d sending break set/clear command.",
2600
		     __func__, status);
L
Linus Torvalds 已提交
2601 2602
}

2603
static int edge_startup(struct usb_serial *serial)
L
Linus Torvalds 已提交
2604 2605 2606 2607 2608 2609 2610 2611 2612 2613
{
	struct edgeport_serial *edge_serial;
	struct edgeport_port *edge_port;
	struct usb_device *dev;
	int status;
	int i;

	dev = serial->dev;

	/* create our private serial structure */
2614
	edge_serial = kzalloc(sizeof(struct edgeport_serial), GFP_KERNEL);
L
Linus Torvalds 已提交
2615
	if (edge_serial == NULL) {
2616
		dev_err(&serial->dev->dev, "%s - Out of memory\n", __func__);
L
Linus Torvalds 已提交
2617 2618
		return -ENOMEM;
	}
2619
	mutex_init(&edge_serial->es_lock);
L
Linus Torvalds 已提交
2620 2621 2622
	edge_serial->serial = serial;
	usb_set_serial_data(serial, edge_serial);

2623
	status = download_fw(edge_serial);
L
Linus Torvalds 已提交
2624
	if (status) {
2625
		kfree(edge_serial);
L
Linus Torvalds 已提交
2626 2627 2628 2629 2630
		return status;
	}

	/* set up our port private structures */
	for (i = 0; i < serial->num_ports; ++i) {
2631
		edge_port = kzalloc(sizeof(struct edgeport_port), GFP_KERNEL);
L
Linus Torvalds 已提交
2632
		if (edge_port == NULL) {
2633 2634
			dev_err(&serial->dev->dev, "%s - Out of memory\n",
								__func__);
L
Linus Torvalds 已提交
2635 2636 2637 2638 2639
			goto cleanup;
		}
		spin_lock_init(&edge_port->ep_lock);
		edge_port->ep_out_buf = edge_buf_alloc(EDGE_OUT_BUF_SIZE);
		if (edge_port->ep_out_buf == NULL) {
2640 2641
			dev_err(&serial->dev->dev, "%s - Out of memory\n",
								__func__);
L
Linus Torvalds 已提交
2642 2643 2644 2645 2646 2647
			kfree(edge_port);
			goto cleanup;
		}
		edge_port->port = serial->port[i];
		edge_port->edge_serial = edge_serial;
		usb_set_serial_port_data(serial->port[i], edge_port);
2648
		edge_port->bUartMode = default_uart_mode;
L
Linus Torvalds 已提交
2649
	}
2650

L
Linus Torvalds 已提交
2651 2652 2653
	return 0;

cleanup:
2654
	for (--i; i >= 0; --i) {
L
Linus Torvalds 已提交
2655 2656 2657 2658 2659
		edge_port = usb_get_serial_port_data(serial->port[i]);
		edge_buf_free(edge_port->ep_out_buf);
		kfree(edge_port);
		usb_set_serial_port_data(serial->port[i], NULL);
	}
2660
	kfree(edge_serial);
L
Linus Torvalds 已提交
2661 2662 2663 2664
	usb_set_serial_data(serial, NULL);
	return -ENOMEM;
}

2665
static void edge_disconnect(struct usb_serial *serial)
L
Linus Torvalds 已提交
2666 2667 2668 2669
{
	int i;
	struct edgeport_port *edge_port;

2670
	dbg("%s", __func__);
L
Linus Torvalds 已提交
2671

2672
	for (i = 0; i < serial->num_ports; ++i) {
L
Linus Torvalds 已提交
2673
		edge_port = usb_get_serial_port_data(serial->port[i]);
2674
		edge_remove_sysfs_attrs(edge_port->port);
2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686
	}
}

static void edge_release(struct usb_serial *serial)
{
	int i;
	struct edgeport_port *edge_port;

	dbg("%s", __func__);

	for (i = 0; i < serial->num_ports; ++i) {
		edge_port = usb_get_serial_port_data(serial->port[i]);
2687 2688
		edge_buf_free(edge_port->ep_out_buf);
		kfree(edge_port);
L
Linus Torvalds 已提交
2689
	}
2690
	kfree(usb_get_serial_data(serial));
L
Linus Torvalds 已提交
2691 2692 2693
}


2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711
/* Sysfs Attributes */

static ssize_t show_uart_mode(struct device *dev,
	struct device_attribute *attr, char *buf)
{
	struct usb_serial_port *port = to_usb_serial_port(dev);
	struct edgeport_port *edge_port = usb_get_serial_port_data(port);

	return sprintf(buf, "%d\n", edge_port->bUartMode);
}

static ssize_t store_uart_mode(struct device *dev,
	struct device_attribute *attr, const char *valbuf, size_t count)
{
	struct usb_serial_port *port = to_usb_serial_port(dev);
	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
	unsigned int v = simple_strtoul(valbuf, NULL, 0);

2712
	dbg("%s: setting uart_mode = %d", __func__, v);
2713 2714 2715 2716

	if (v < 256)
		edge_port->bUartMode = v;
	else
2717
		dev_err(dev, "%s - uart_mode %d is invalid\n", __func__, v);
2718 2719 2720 2721

	return count;
}

2722 2723
static DEVICE_ATTR(uart_mode, S_IWUSR | S_IRUGO, show_uart_mode,
							store_uart_mode);
2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736

static int edge_create_sysfs_attrs(struct usb_serial_port *port)
{
	return device_create_file(&port->dev, &dev_attr_uart_mode);
}

static int edge_remove_sysfs_attrs(struct usb_serial_port *port)
{
	device_remove_file(&port->dev, &dev_attr_uart_mode);
	return 0;
}


L
Linus Torvalds 已提交
2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752
/* Circular Buffer */

/*
 * edge_buf_alloc
 *
 * Allocate a circular buffer and all associated memory.
 */

static struct edge_buf *edge_buf_alloc(unsigned int size)
{
	struct edge_buf *eb;


	if (size == 0)
		return NULL;

2753
	eb = kmalloc(sizeof(struct edge_buf), GFP_KERNEL);
L
Linus Torvalds 已提交
2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775
	if (eb == NULL)
		return NULL;

	eb->buf_buf = kmalloc(size, GFP_KERNEL);
	if (eb->buf_buf == NULL) {
		kfree(eb);
		return NULL;
	}

	eb->buf_size = size;
	eb->buf_get = eb->buf_put = eb->buf_buf;

	return eb;
}


/*
 * edge_buf_free
 *
 * Free the buffer and all associated memory.
 */

A
Adrian Bunk 已提交
2776
static void edge_buf_free(struct edge_buf *eb)
L
Linus Torvalds 已提交
2777
{
2778 2779
	if (eb) {
		kfree(eb->buf_buf);
L
Linus Torvalds 已提交
2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792
		kfree(eb);
	}
}


/*
 * edge_buf_clear
 *
 * Clear out all data in the circular buffer.
 */

static void edge_buf_clear(struct edge_buf *eb)
{
2793 2794 2795
	if (eb != NULL)
		eb->buf_get = eb->buf_put;
	/* equivalent to a get of all data available */
L
Linus Torvalds 已提交
2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807
}


/*
 * edge_buf_data_avail
 *
 * Return the number of bytes of data available in the circular
 * buffer.
 */

static unsigned int edge_buf_data_avail(struct edge_buf *eb)
{
2808
	if (eb == NULL)
L
Linus Torvalds 已提交
2809
		return 0;
2810
	return ((eb->buf_size + eb->buf_put - eb->buf_get) % eb->buf_size);
L
Linus Torvalds 已提交
2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822
}


/*
 * edge_buf_space_avail
 *
 * Return the number of bytes of space available in the circular
 * buffer.
 */

static unsigned int edge_buf_space_avail(struct edge_buf *eb)
{
2823
	if (eb == NULL)
L
Linus Torvalds 已提交
2824
		return 0;
2825
	return ((eb->buf_size + eb->buf_get - eb->buf_put - 1) % eb->buf_size);
L
Linus Torvalds 已提交
2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912
}


/*
 * edge_buf_put
 *
 * Copy data data from a user buffer and put it into the circular buffer.
 * Restrict to the amount of space available.
 *
 * Return the number of bytes copied.
 */

static unsigned int edge_buf_put(struct edge_buf *eb, const char *buf,
	unsigned int count)
{
	unsigned int len;


	if (eb == NULL)
		return 0;

	len  = edge_buf_space_avail(eb);
	if (count > len)
		count = len;

	if (count == 0)
		return 0;

	len = eb->buf_buf + eb->buf_size - eb->buf_put;
	if (count > len) {
		memcpy(eb->buf_put, buf, len);
		memcpy(eb->buf_buf, buf+len, count - len);
		eb->buf_put = eb->buf_buf + count - len;
	} else {
		memcpy(eb->buf_put, buf, count);
		if (count < len)
			eb->buf_put += count;
		else /* count == len */
			eb->buf_put = eb->buf_buf;
	}

	return count;
}


/*
 * edge_buf_get
 *
 * Get data from the circular buffer and copy to the given buffer.
 * Restrict to the amount of data available.
 *
 * Return the number of bytes copied.
 */

static unsigned int edge_buf_get(struct edge_buf *eb, char *buf,
	unsigned int count)
{
	unsigned int len;


	if (eb == NULL)
		return 0;

	len = edge_buf_data_avail(eb);
	if (count > len)
		count = len;

	if (count == 0)
		return 0;

	len = eb->buf_buf + eb->buf_size - eb->buf_get;
	if (count > len) {
		memcpy(buf, eb->buf_get, len);
		memcpy(buf+len, eb->buf_buf, count - len);
		eb->buf_get = eb->buf_buf + count - len;
	} else {
		memcpy(buf, eb->buf_get, count);
		if (count < len)
			eb->buf_get += count;
		else /* count == len */
			eb->buf_get = eb->buf_buf;
	}

	return count;
}


2913
static struct usb_serial_driver edgeport_1port_device = {
2914
	.driver = {
2915 2916
		.owner		= THIS_MODULE,
		.name		= "edgeport_ti_1",
2917
	},
2918
	.description		= "Edgeport TI 1 port adapter",
2919
	.usb_driver		= &io_driver,
L
Linus Torvalds 已提交
2920 2921 2922 2923 2924 2925 2926
	.id_table		= edgeport_1port_id_table,
	.num_ports		= 1,
	.open			= edge_open,
	.close			= edge_close,
	.throttle		= edge_throttle,
	.unthrottle		= edge_unthrottle,
	.attach			= edge_startup,
2927 2928
	.disconnect		= edge_disconnect,
	.release		= edge_release,
2929
	.port_probe		= edge_create_sysfs_attrs,
L
Linus Torvalds 已提交
2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942
	.ioctl			= edge_ioctl,
	.set_termios		= edge_set_termios,
	.tiocmget		= edge_tiocmget,
	.tiocmset		= edge_tiocmset,
	.write			= edge_write,
	.write_room		= edge_write_room,
	.chars_in_buffer	= edge_chars_in_buffer,
	.break_ctl		= edge_break,
	.read_int_callback	= edge_interrupt_callback,
	.read_bulk_callback	= edge_bulk_in_callback,
	.write_bulk_callback	= edge_bulk_out_callback,
};

2943
static struct usb_serial_driver edgeport_2port_device = {
2944
	.driver = {
2945 2946
		.owner		= THIS_MODULE,
		.name		= "edgeport_ti_2",
2947
	},
2948
	.description		= "Edgeport TI 2 port adapter",
2949
	.usb_driver		= &io_driver,
L
Linus Torvalds 已提交
2950 2951 2952 2953 2954 2955 2956
	.id_table		= edgeport_2port_id_table,
	.num_ports		= 2,
	.open			= edge_open,
	.close			= edge_close,
	.throttle		= edge_throttle,
	.unthrottle		= edge_unthrottle,
	.attach			= edge_startup,
2957 2958
	.disconnect		= edge_disconnect,
	.release		= edge_release,
2959
	.port_probe		= edge_create_sysfs_attrs,
L
Linus Torvalds 已提交
2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983
	.ioctl			= edge_ioctl,
	.set_termios		= edge_set_termios,
	.tiocmget		= edge_tiocmget,
	.tiocmset		= edge_tiocmset,
	.write			= edge_write,
	.write_room		= edge_write_room,
	.chars_in_buffer	= edge_chars_in_buffer,
	.break_ctl		= edge_break,
	.read_int_callback	= edge_interrupt_callback,
	.read_bulk_callback	= edge_bulk_in_callback,
	.write_bulk_callback	= edge_bulk_out_callback,
};


static int __init edgeport_init(void)
{
	int retval;
	retval = usb_serial_register(&edgeport_1port_device);
	if (retval)
		goto failed_1port_device_register;
	retval = usb_serial_register(&edgeport_2port_device);
	if (retval)
		goto failed_2port_device_register;
	retval = usb_register(&io_driver);
2984
	if (retval)
L
Linus Torvalds 已提交
2985
		goto failed_usb_register;
2986 2987
	printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
	       DRIVER_DESC "\n");
L
Linus Torvalds 已提交
2988 2989 2990 2991 2992 2993 2994 2995 2996
	return 0;
failed_usb_register:
	usb_serial_deregister(&edgeport_2port_device);
failed_2port_device_register:
	usb_serial_deregister(&edgeport_1port_device);
failed_1port_device_register:
	return retval;
}

2997
static void __exit edgeport_exit(void)
L
Linus Torvalds 已提交
2998
{
2999 3000 3001
	usb_deregister(&io_driver);
	usb_serial_deregister(&edgeport_1port_device);
	usb_serial_deregister(&edgeport_2port_device);
L
Linus Torvalds 已提交
3002 3003 3004 3005 3006 3007 3008 3009 3010
}

module_init(edgeport_init);
module_exit(edgeport_exit);

/* Module information */
MODULE_AUTHOR(DRIVER_AUTHOR);
MODULE_DESCRIPTION(DRIVER_DESC);
MODULE_LICENSE("GPL");
3011
MODULE_FIRMWARE("edgeport/down3.bin");
L
Linus Torvalds 已提交
3012 3013 3014 3015 3016 3017 3018 3019

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

module_param(closing_wait, int, S_IRUGO | S_IWUSR);
MODULE_PARM_DESC(closing_wait, "Maximum wait for data to drain, in .01 secs");

module_param(ignore_cpu_rev, bool, S_IRUGO | S_IWUSR);
3020 3021
MODULE_PARM_DESC(ignore_cpu_rev,
			"Ignore the cpu revision when connecting to a device");
L
Linus Torvalds 已提交
3022

3023 3024
module_param(default_uart_mode, int, S_IRUGO | S_IWUSR);
MODULE_PARM_DESC(default_uart_mode, "Default uart_mode, 0=RS232, ...");