io_ti.c 73.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 21 22 23 24 25 26 27 28
/*
 * 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>.
 */

#include <linux/kernel.h>
#include <linux/jiffies.h>
#include <linux/errno.h>
#include <linux/slab.h>
#include <linux/tty.h>
#include <linux/tty_driver.h>
#include <linux/tty_flip.h>
#include <linux/module.h>
#include <linux/spinlock.h>
29
#include <linux/mutex.h>
L
Linus Torvalds 已提交
30
#include <linux/serial.h>
31
#include <linux/swab.h>
32
#include <linux/kfifo.h>
L
Linus Torvalds 已提交
33
#include <linux/ioctl.h>
34
#include <linux/firmware.h>
35
#include <linux/uaccess.h>
L
Linus Torvalds 已提交
36
#include <linux/usb.h>
37
#include <linux/usb/serial.h>
L
Linus Torvalds 已提交
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52

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

#define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com> and David Iacovelli"
#define DRIVER_DESC "Edgeport USB Serial Driver"

#define EPROM_PAGE_SIZE		64


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

53 54 55 56 57 58
/* 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 已提交
59 60 61 62 63 64 65 66 67 68

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


/* Product information read from the Edgeport */
69 70 71
struct product_info {
	int	TiMode;			/* Current TI Mode  */
	__u8	hardware_type;		/* Type of hardware */
L
Linus Torvalds 已提交
72 73
} __attribute__((packed));

74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
/*
 * Edgeport firmware header
 *
 * "build_number" has been set to 0 in all three of the images I have
 * seen, and Digi Tech Support suggests that it is safe to ignore it.
 *
 * "length" is the number of bytes of actual data following the header.
 *
 * "checksum" is the low order byte resulting from adding the values of
 * all the data bytes.
 */
struct edgeport_fw_hdr {
	u8 major_version;
	u8 minor_version;
	__le16 build_number;
	__le16 length;
	u8 checksum;
} __packed;

L
Linus Torvalds 已提交
93 94 95 96 97 98 99
struct edgeport_port {
	__u16 uart_base;
	__u16 dma_address;
	__u8 shadow_msr;
	__u8 shadow_mcr;
	__u8 shadow_lsr;
	__u8 lsr_mask;
100
	__u32 ump_read_timeout;		/* Number of milliseconds the UMP will
L
Linus Torvalds 已提交
101 102 103 104 105
					   wait without data before completing
					   a read short */
	int baud_rate;
	int close_pending;
	int lsr_event;
106

L
Linus Torvalds 已提交
107 108
	struct edgeport_serial	*edge_serial;
	struct usb_serial_port	*port;
109
	__u8 bUartMode;		/* Port type, 0: RS232, etc. */
L
Linus Torvalds 已提交
110 111 112 113 114 115 116
	spinlock_t ep_lock;
	int ep_read_urb_state;
	int ep_write_urb_in_use;
};

struct edgeport_serial {
	struct product_info product_info;
117 118 119
	u8 TI_I2C_Type;			/* Type of I2C in UMP */
	u8 TiReadI2C;			/* Set to TRUE if we have read the
					   I2c in Boot Mode */
120
	struct mutex es_lock;
L
Linus Torvalds 已提交
121 122
	int num_ports_open;
	struct usb_serial *serial;
123
	int fw_version;
L
Linus Torvalds 已提交
124 125 126 127
};


/* Devices that this driver supports */
128
static const struct usb_device_id edgeport_1port_id_table[] = {
L
Linus Torvalds 已提交
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
	{ 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) },
	{ }
};

148
static const struct usb_device_id edgeport_2port_id_table[] = {
L
Linus Torvalds 已提交
149 150 151 152 153 154 155 156 157 158 159 160
	{ 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) },
161
	/* The 4, 8 and 16 port devices show up as multiple 2 port devices */
L
Linus Torvalds 已提交
162
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4S) },
163 164 165 166
	{ 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 已提交
167 168 169 170
	{ }
};

/* Devices that this driver supports */
171
static const struct usb_device_id id_table_combined[] = {
L
Linus Torvalds 已提交
172 173 174 175 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
	{ 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) },
201 202 203 204
	{ 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 已提交
205 206 207
	{ }
};

208
MODULE_DEVICE_TABLE(usb, id_table_combined);
L
Linus Torvalds 已提交
209 210

static int closing_wait = EDGE_CLOSING_WAIT;
211
static bool ignore_cpu_rev;
212
static int default_uart_mode;		/* RS232 */
L
Linus Torvalds 已提交
213

J
Jiri Slaby 已提交
214 215
static void edge_tty_recv(struct usb_serial_port *port, unsigned char *data,
		int length);
L
Linus Torvalds 已提交
216 217 218 219

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

A
Alan Cox 已提交
220 221
static void edge_set_termios(struct tty_struct *tty,
		struct usb_serial_port *port, struct ktermios *old_termios);
222
static void edge_send(struct usb_serial_port *port, struct tty_struct *tty);
L
Linus Torvalds 已提交
223

224 225 226 227
/* 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 已提交
228

229 230 231 232
/* Timeouts in msecs: firmware downloads take longer */
#define TI_VSEND_TIMEOUT_DEFAULT 1000
#define TI_VSEND_TIMEOUT_FW_DOWNLOAD 10000

233 234
static int ti_vread_sync(struct usb_device *dev, __u8 request,
				__u16 value, __u16 index, u8 *data, int size)
L
Linus Torvalds 已提交
235 236 237
{
	int status;

238 239 240
	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 已提交
241 242 243
	if (status < 0)
		return status;
	if (status != size) {
244 245
		dev_dbg(&dev->dev, "%s - wanted to write %d, but only wrote %d\n",
			__func__, size, status);
L
Linus Torvalds 已提交
246 247 248 249 250
		return -ECOMM;
	}
	return 0;
}

251 252
static int ti_vsend_sync(struct usb_device *dev, u8 request, u16 value,
		u16 index, u8 *data, int size, int timeout)
L
Linus Torvalds 已提交
253 254 255
{
	int status;

256 257
	status = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), request,
			(USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT),
258
			value, index, data, size, timeout);
L
Linus Torvalds 已提交
259 260 261
	if (status < 0)
		return status;
	if (status != size) {
262 263
		dev_dbg(&dev->dev, "%s - wanted to write %d, but only wrote %d\n",
			__func__, size, status);
L
Linus Torvalds 已提交
264 265 266 267 268
		return -ECOMM;
	}
	return 0;
}

269
static int send_cmd(struct usb_device *dev, __u8 command,
L
Linus Torvalds 已提交
270 271 272
				__u8 moduleid, __u16 value, u8 *data,
				int size)
{
273 274
	return ti_vsend_sync(dev, command, value, moduleid, data, size,
			TI_VSEND_TIMEOUT_DEFAULT);
L
Linus Torvalds 已提交
275 276 277
}

/* clear tx/rx buffers and fifo in TI UMP */
278
static int purge_port(struct usb_serial_port *port, __u16 mask)
L
Linus Torvalds 已提交
279
{
280
	int port_number = port->port_number;
L
Linus Torvalds 已提交
281

282
	dev_dbg(&port->dev, "%s - port %d, mask %x\n", __func__, port_number, mask);
L
Linus Torvalds 已提交
283

284
	return send_cmd(port->serial->dev,
L
Linus Torvalds 已提交
285 286 287 288 289 290 291 292
					UMPC_PURGE_PORT,
					(__u8)(UMPM_UART1_PORT + port_number),
					mask,
					NULL,
					0);
}

/**
293
 * read_download_mem - Read edgeport memory from TI chip
L
Linus Torvalds 已提交
294 295 296 297 298 299
 * @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
 */
300
static int read_download_mem(struct usb_device *dev, int start_address,
L
Linus Torvalds 已提交
301 302 303 304
				int length, __u8 address_type, __u8 *buffer)
{
	int status = 0;
	__u8 read_length;
305
	u16 be_start_address;
306

307
	dev_dbg(&dev->dev, "%s - @ %x for %d\n", __func__, start_address, length);
L
Linus Torvalds 已提交
308 309 310 311 312 313

	/* Read in blocks of 64 bytes
	 * (TI firmware can't handle more than 64 byte reads)
	 */
	while (length) {
		if (length > 64)
314
			read_length = 64;
L
Linus Torvalds 已提交
315 316 317 318
		else
			read_length = (__u8)length;

		if (read_length > 1) {
319
			dev_dbg(&dev->dev, "%s - @ %x for %d\n", __func__, start_address, read_length);
L
Linus Torvalds 已提交
320
		}
321 322 323 324 325
		/*
		 * NOTE: Must use swab as wIndex is sent in little-endian
		 *       byte order regardless of host byte order.
		 */
		be_start_address = swab16((u16)start_address);
326 327
		status = ti_vread_sync(dev, UMPC_MEMORY_READ,
					(__u16)address_type,
328
					be_start_address,
329
					buffer, read_length);
L
Linus Torvalds 已提交
330 331

		if (status) {
332
			dev_dbg(&dev->dev, "%s - ERROR %x\n", __func__, status);
L
Linus Torvalds 已提交
333 334 335
			return status;
		}

336
		if (read_length > 1)
337
			usb_serial_debug_data(&dev->dev, __func__, read_length, buffer);
L
Linus Torvalds 已提交
338 339 340 341 342 343

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

L
Linus Torvalds 已提交
345 346 347
	return status;
}

348 349
static int read_ram(struct usb_device *dev, int start_address,
						int length, __u8 *buffer)
L
Linus Torvalds 已提交
350
{
351 352
	return read_download_mem(dev, start_address, length,
					DTK_ADDR_SPACE_XDATA, buffer);
L
Linus Torvalds 已提交
353 354 355
}

/* Read edgeport memory to a given block */
356 357
static int read_boot_mem(struct edgeport_serial *serial,
				int start_address, int length, __u8 *buffer)
L
Linus Torvalds 已提交
358 359 360 361
{
	int status = 0;
	int i;

362 363 364 365
	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 已提交
366
		if (status) {
367
			dev_dbg(&serial->serial->dev->dev, "%s - ERROR %x\n", __func__, status);
L
Linus Torvalds 已提交
368 369 370 371
			return status;
		}
	}

372 373
	dev_dbg(&serial->serial->dev->dev, "%s - start_address = %x, length = %d\n",
		__func__, start_address, length);
374
	usb_serial_debug_data(&serial->serial->dev->dev, __func__, length, buffer);
L
Linus Torvalds 已提交
375 376 377 378 379 380 381

	serial->TiReadI2C = 1;

	return status;
}

/* Write given block to TI EPROM memory */
382 383
static int write_boot_mem(struct edgeport_serial *serial,
				int start_address, int length, __u8 *buffer)
L
Linus Torvalds 已提交
384 385 386
{
	int status = 0;
	int i;
387
	u8 *temp;
L
Linus Torvalds 已提交
388 389 390

	/* Must do a read before write */
	if (!serial->TiReadI2C) {
391
		temp = kmalloc(1, GFP_KERNEL);
392
		if (!temp)
393
			return -ENOMEM;
394

395 396
		status = read_boot_mem(serial, 0, 1, temp);
		kfree(temp);
L
Linus Torvalds 已提交
397 398 399 400
		if (status)
			return status;
	}

401
	for (i = 0; i < length; ++i) {
402 403 404
		status = ti_vsend_sync(serial->serial->dev, UMPC_MEMORY_WRITE,
				buffer[i], (u16)(i + start_address), NULL,
				0, TI_VSEND_TIMEOUT_DEFAULT);
L
Linus Torvalds 已提交
405 406 407 408
		if (status)
			return status;
	}

409
	dev_dbg(&serial->serial->dev->dev, "%s - start_sddr = %x, length = %d\n", __func__, start_address, length);
410
	usb_serial_debug_data(&serial->serial->dev->dev, __func__, length, buffer);
L
Linus Torvalds 已提交
411 412 413 414 415 416

	return status;
}


/* Write edgeport I2C memory to TI chip	*/
417 418
static int write_i2c_mem(struct edgeport_serial *serial,
		int start_address, int length, __u8 address_type, __u8 *buffer)
L
Linus Torvalds 已提交
419
{
420
	struct device *dev = &serial->serial->dev->dev;
L
Linus Torvalds 已提交
421 422
	int status = 0;
	int write_length;
423
	u16 be_start_address;
L
Linus Torvalds 已提交
424 425

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

L
Lucas De Marchi 已提交
427
	/* calculate the number of bytes left in the first page */
428 429
	write_length = EPROM_PAGE_SIZE -
				(start_address & (EPROM_PAGE_SIZE - 1));
L
Linus Torvalds 已提交
430 431 432 433

	if (write_length > length)
		write_length = length;

434 435
	dev_dbg(dev, "%s - BytesInFirstPage Addr = %x, length = %d\n",
		__func__, start_address, write_length);
436
	usb_serial_debug_data(dev, __func__, write_length, buffer);
L
Linus Torvalds 已提交
437

438 439 440 441 442 443 444
	/*
	 * Write first page.
	 *
	 * NOTE: Must use swab as wIndex is sent in little-endian byte order
	 *       regardless of host byte order.
	 */
	be_start_address = swab16((u16)start_address);
445 446 447
	status = ti_vsend_sync(serial->serial->dev, UMPC_MEMORY_WRITE,
				(u16)address_type, be_start_address,
				buffer,	write_length, TI_VSEND_TIMEOUT_DEFAULT);
L
Linus Torvalds 已提交
448
	if (status) {
449
		dev_dbg(dev, "%s - ERROR %d\n", __func__, status);
L
Linus Torvalds 已提交
450 451 452 453 454 455 456
		return status;
	}

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

457 458
	/* We should be aligned now -- can write
	   max page size bytes at a time */
L
Linus Torvalds 已提交
459 460 461 462 463 464
	while (length) {
		if (length > EPROM_PAGE_SIZE)
			write_length = EPROM_PAGE_SIZE;
		else
			write_length = length;

465 466
		dev_dbg(dev, "%s - Page Write Addr = %x, length = %d\n",
			__func__, start_address, write_length);
467
		usb_serial_debug_data(dev, __func__, write_length, buffer);
L
Linus Torvalds 已提交
468

469 470 471 472 473 474 475
		/*
		 * Write next page.
		 *
		 * NOTE: Must use swab as wIndex is sent in little-endian byte
		 *       order regardless of host byte order.
		 */
		be_start_address = swab16((u16)start_address);
476
		status = ti_vsend_sync(serial->serial->dev, UMPC_MEMORY_WRITE,
477 478
				(u16)address_type, be_start_address, buffer,
				write_length, TI_VSEND_TIMEOUT_DEFAULT);
L
Linus Torvalds 已提交
479
		if (status) {
480
			dev_err(dev, "%s - ERROR %d\n", __func__, status);
L
Linus Torvalds 已提交
481 482
			return status;
		}
483

L
Linus Torvalds 已提交
484 485 486 487 488 489 490 491
		length		-= write_length;
		start_address	+= write_length;
		buffer		+= write_length;
	}
	return status;
}

/* Examine the UMP DMA registers and LSR
492
 *
L
Linus Torvalds 已提交
493 494 495 496
 * 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.
 */
497
static int tx_active(struct edgeport_port *port)
L
Linus Torvalds 已提交
498 499 500 501 502 503
{
	int status;
	struct out_endpoint_desc_block *oedb;
	__u8 *lsr;
	int bytes_left = 0;

504
	oedb = kmalloc(sizeof(*oedb), GFP_KERNEL);
505
	if (!oedb)
L
Linus Torvalds 已提交
506 507
		return -ENOMEM;

508
	lsr = kmalloc(1, GFP_KERNEL);	/* Sigh, that's right, just one byte,
L
Linus Torvalds 已提交
509 510 511 512 513 514 515
					   as not all platforms can do DMA
					   from stack */
	if (!lsr) {
		kfree(oedb);
		return -ENOMEM;
	}
	/* Read the DMA Count Registers */
516 517
	status = read_ram(port->port->serial->dev, port->dma_address,
						sizeof(*oedb), (void *)oedb);
L
Linus Torvalds 已提交
518 519 520
	if (status)
		goto exit_is_tx_active;

521
	dev_dbg(&port->port->dev, "%s - XByteCount    0x%X\n", __func__, oedb->XByteCount);
L
Linus Torvalds 已提交
522 523

	/* and the LSR */
524 525
	status = read_ram(port->port->serial->dev,
			port->uart_base + UMPMEM_OFFS_UART_LSR, 1, lsr);
L
Linus Torvalds 已提交
526 527 528

	if (status)
		goto exit_is_tx_active;
529
	dev_dbg(&port->port->dev, "%s - LSR = 0x%X\n", __func__, *lsr);
530

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

535
	if ((*lsr & UMP_UART_LSR_TX_MASK) == 0)
L
Linus Torvalds 已提交
536 537 538 539
		bytes_left += 1;

	/* We return Not Active if we get any kind of error */
exit_is_tx_active:
540
	dev_dbg(&port->port->dev, "%s - return %d\n", __func__, bytes_left);
L
Linus Torvalds 已提交
541 542 543 544 545 546

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

547
static int choose_config(struct usb_device *dev)
L
Linus Torvalds 已提交
548
{
549 550 551 552 553 554
	/*
	 * 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 已提交
555

556 557 558 559
	dev_dbg(&dev->dev, "%s - Number of Interfaces = %d\n",
		__func__, dev->config->desc.bNumInterfaces);
	dev_dbg(&dev->dev, "%s - MAX Power            = %d\n",
		__func__, dev->config->desc.bMaxPower * 2);
L
Linus Torvalds 已提交
560 561

	if (dev->config->desc.bNumInterfaces != 1) {
562
		dev_err(&dev->dev, "%s - bNumInterfaces is not 1, ERROR!\n", __func__);
L
Linus Torvalds 已提交
563 564 565 566 567 568
		return -ENODEV;
	}

	return 0;
}

569 570
static int read_rom(struct edgeport_serial *serial,
				int start_address, int length, __u8 *buffer)
L
Linus Torvalds 已提交
571 572 573 574
{
	int status;

	if (serial->product_info.TiMode == TI_MODE_DOWNLOAD) {
575
		status = read_download_mem(serial->serial->dev,
L
Linus Torvalds 已提交
576 577 578 579 580
					       start_address,
					       length,
					       serial->TI_I2C_Type,
					       buffer);
	} else {
581 582
		status = read_boot_mem(serial, start_address, length,
								buffer);
L
Linus Torvalds 已提交
583 584 585 586
	}
	return status;
}

587 588
static int write_rom(struct edgeport_serial *serial, int start_address,
						int length, __u8 *buffer)
L
Linus Torvalds 已提交
589 590
{
	if (serial->product_info.TiMode == TI_MODE_BOOT)
591 592
		return write_boot_mem(serial, start_address, length,
								buffer);
L
Linus Torvalds 已提交
593 594

	if (serial->product_info.TiMode == TI_MODE_DOWNLOAD)
595 596
		return write_i2c_mem(serial, start_address, length,
						serial->TI_I2C_Type, buffer);
L
Linus Torvalds 已提交
597 598 599 600 601 602
	return -EINVAL;
}



/* Read a descriptor header from I2C based on type */
603 604
static int get_descriptor_addr(struct edgeport_serial *serial,
				int desc_type, struct ti_i2c_desc *rom_desc)
L
Linus Torvalds 已提交
605 606 607 608 609 610 611
{
	int start_address;
	int status;

	/* Search for requested descriptor in I2C */
	start_address = 2;
	do {
612
		status = read_rom(serial,
L
Linus Torvalds 已提交
613 614
				   start_address,
				   sizeof(struct ti_i2c_desc),
615
				   (__u8 *)rom_desc);
L
Linus Torvalds 已提交
616 617 618 619 620 621
		if (status)
			return 0;

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

622 623
		start_address = start_address + sizeof(struct ti_i2c_desc) +
						le16_to_cpu(rom_desc->Size);
L
Linus Torvalds 已提交
624 625

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

L
Linus Torvalds 已提交
627 628 629 630
	return 0;
}

/* Validate descriptor checksum */
631
static int valid_csum(struct ti_i2c_desc *rom_desc, __u8 *buffer)
L
Linus Torvalds 已提交
632 633 634 635
{
	__u16 i;
	__u8 cs = 0;

636
	for (i = 0; i < le16_to_cpu(rom_desc->Size); i++)
L
Linus Torvalds 已提交
637
		cs = (__u8)(cs + buffer[i]);
638

L
Linus Torvalds 已提交
639
	if (cs != rom_desc->CheckSum) {
640
		pr_debug("%s - Mismatch %x - %x", __func__, rom_desc->CheckSum, cs);
L
Linus Torvalds 已提交
641 642 643 644 645 646
		return -EINVAL;
	}
	return 0;
}

/* Make sure that the I2C image is good */
647
static int check_i2c_image(struct edgeport_serial *serial)
L
Linus Torvalds 已提交
648 649 650 651 652 653 654 655
{
	struct device *dev = &serial->serial->dev->dev;
	int status = 0;
	struct ti_i2c_desc *rom_desc;
	int start_address = 2;
	__u8 *buffer;
	__u16 ttype;

656
	rom_desc = kmalloc(sizeof(*rom_desc), GFP_KERNEL);
657
	if (!rom_desc)
L
Linus Torvalds 已提交
658
		return -ENOMEM;
659

660
	buffer = kmalloc(TI_MAX_I2C_SIZE, GFP_KERNEL);
L
Linus Torvalds 已提交
661
	if (!buffer) {
662
		kfree(rom_desc);
L
Linus Torvalds 已提交
663 664 665
		return -ENOMEM;
	}

666 667
	/* Read the first byte (Signature0) must be 0x52 or 0x10 */
	status = read_rom(serial, 0, 1, buffer);
L
Linus Torvalds 已提交
668
	if (status)
669
		goto out;
L
Linus Torvalds 已提交
670 671

	if (*buffer != UMP5152 && *buffer != UMP3410) {
672
		dev_err(dev, "%s - invalid buffer signature\n", __func__);
L
Linus Torvalds 已提交
673
		status = -ENODEV;
674
		goto out;
L
Linus Torvalds 已提交
675 676 677
	}

	do {
678 679
		/* Validate the I2C */
		status = read_rom(serial,
L
Linus Torvalds 已提交
680 681 682 683 684 685
				start_address,
				sizeof(struct ti_i2c_desc),
				(__u8 *)rom_desc);
		if (status)
			break;

686
		if ((start_address + sizeof(struct ti_i2c_desc) +
687
			le16_to_cpu(rom_desc->Size)) > TI_MAX_I2C_SIZE) {
L
Linus Torvalds 已提交
688
			status = -ENODEV;
689
			dev_dbg(dev, "%s - structure too big, erroring out.\n", __func__);
L
Linus Torvalds 已提交
690 691 692
			break;
		}

693
		dev_dbg(dev, "%s Type = 0x%x\n", __func__, rom_desc->Type);
L
Linus Torvalds 已提交
694

695
		/* Skip type 2 record */
L
Linus Torvalds 已提交
696
		ttype = rom_desc->Type & 0x0f;
697 698 699 700 701
		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),
702 703
						le16_to_cpu(rom_desc->Size),
						buffer);
L
Linus Torvalds 已提交
704 705 706
			if (status)
				break;

707
			status = valid_csum(rom_desc, buffer);
L
Linus Torvalds 已提交
708 709 710
			if (status)
				break;
		}
711
		start_address = start_address + sizeof(struct ti_i2c_desc) +
712
						le16_to_cpu(rom_desc->Size);
L
Linus Torvalds 已提交
713

714 715
	} while ((rom_desc->Type != I2C_DESC_TYPE_ION) &&
				(start_address < TI_MAX_I2C_SIZE));
L
Linus Torvalds 已提交
716

717 718
	if ((rom_desc->Type != I2C_DESC_TYPE_ION) ||
				(start_address > TI_MAX_I2C_SIZE))
L
Linus Torvalds 已提交
719 720
		status = -ENODEV;

721 722 723
out:
	kfree(buffer);
	kfree(rom_desc);
L
Linus Torvalds 已提交
724 725 726
	return status;
}

727
static int get_manuf_info(struct edgeport_serial *serial, __u8 *buffer)
L
Linus Torvalds 已提交
728 729 730 731 732
{
	int status;
	int start_address;
	struct ti_i2c_desc *rom_desc;
	struct edge_ti_manuf_descriptor *desc;
733
	struct device *dev = &serial->serial->dev->dev;
L
Linus Torvalds 已提交
734

735
	rom_desc = kmalloc(sizeof(*rom_desc), GFP_KERNEL);
736
	if (!rom_desc)
L
Linus Torvalds 已提交
737
		return -ENOMEM;
738

739 740
	start_address = get_descriptor_addr(serial, I2C_DESC_TYPE_ION,
								rom_desc);
L
Linus Torvalds 已提交
741 742

	if (!start_address) {
743
		dev_dbg(dev, "%s - Edge Descriptor not found in I2C\n", __func__);
L
Linus Torvalds 已提交
744 745 746 747
		status = -ENODEV;
		goto exit;
	}

748 749
	/* Read the descriptor data */
	status = read_rom(serial, start_address+sizeof(struct ti_i2c_desc),
750
					le16_to_cpu(rom_desc->Size), buffer);
L
Linus Torvalds 已提交
751 752
	if (status)
		goto exit;
753 754 755

	status = valid_csum(rom_desc, buffer);

L
Linus Torvalds 已提交
756
	desc = (struct edge_ti_manuf_descriptor *)buffer;
757 758 759 760 761 762
	dev_dbg(dev, "%s - IonConfig      0x%x\n", __func__, desc->IonConfig);
	dev_dbg(dev, "%s - Version          %d\n", __func__, desc->Version);
	dev_dbg(dev, "%s - Cpu/Board      0x%x\n", __func__, desc->CpuRev_BoardRev);
	dev_dbg(dev, "%s - NumPorts         %d\n", __func__, desc->NumPorts);
	dev_dbg(dev, "%s - NumVirtualPorts  %d\n", __func__, desc->NumVirtualPorts);
	dev_dbg(dev, "%s - TotalPorts       %d\n", __func__, desc->TotalPorts);
L
Linus Torvalds 已提交
763 764

exit:
765
	kfree(rom_desc);
L
Linus Torvalds 已提交
766 767 768 769
	return status;
}

/* Build firmware header used for firmware update */
770 771
static int build_i2c_fw_hdr(u8 *header, struct device *dev,
		const struct firmware *fw)
L
Linus Torvalds 已提交
772 773 774 775 776 777 778 779
{
	__u8 *buffer;
	int buffer_size;
	int i;
	__u8 cs = 0;
	struct ti_i2c_desc *i2c_header;
	struct ti_i2c_image_header *img_header;
	struct ti_i2c_firmware_rec *firmware_rec;
780
	struct edgeport_fw_hdr *fw_hdr = (struct edgeport_fw_hdr *)fw->data;
L
Linus Torvalds 已提交
781

782 783 784 785 786 787 788 789 790 791 792 793 794 795 796
	/* 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);
797
	if (!buffer)
L
Linus Torvalds 已提交
798
		return -ENOMEM;
799

L
Linus Torvalds 已提交
800
	// Set entire image of 0xffs
801
	memset(buffer, 0xff, buffer_size);
L
Linus Torvalds 已提交
802

803
	/* Copy version number into firmware record */
L
Linus Torvalds 已提交
804 805
	firmware_rec = (struct ti_i2c_firmware_rec *)buffer;

806 807
	firmware_rec->Ver_Major	= fw_hdr->major_version;
	firmware_rec->Ver_Minor	= fw_hdr->minor_version;
L
Linus Torvalds 已提交
808

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

812
	memcpy(buffer + sizeof(struct ti_i2c_firmware_rec),
813
		&fw->data[4 + sizeof(struct ti_i2c_image_header)],
L
Linus Torvalds 已提交
814 815 816 817 818 819
		le16_to_cpu(img_header->Length));

	for (i=0; i < buffer_size; i++) {
		cs = (__u8)(cs + buffer[i]);
	}

820
	kfree(buffer);
L
Linus Torvalds 已提交
821

822
	/* Build new header */
L
Linus Torvalds 已提交
823 824
	i2c_header =  (struct ti_i2c_desc *)header;
	firmware_rec =  (struct ti_i2c_firmware_rec*)i2c_header->Data;
825

L
Linus Torvalds 已提交
826
	i2c_header->Type	= I2C_DESC_TYPE_FIRMWARE_BLANK;
827
	i2c_header->Size	= cpu_to_le16(buffer_size);
L
Linus Torvalds 已提交
828
	i2c_header->CheckSum	= cs;
829 830
	firmware_rec->Ver_Major	= fw_hdr->major_version;
	firmware_rec->Ver_Minor	= fw_hdr->minor_version;
L
Linus Torvalds 已提交
831 832 833 834 835

	return 0;
}

/* Try to figure out what type of I2c we have */
836
static int i2c_type_bootmode(struct edgeport_serial *serial)
L
Linus Torvalds 已提交
837
{
838
	struct device *dev = &serial->serial->dev->dev;
L
Linus Torvalds 已提交
839
	int status;
840 841 842
	u8 *data;

	data = kmalloc(1, GFP_KERNEL);
843
	if (!data)
844
		return -ENOMEM;
845 846 847

	/* Try to read type 2 */
	status = ti_vread_sync(serial->serial->dev, UMPC_MEMORY_READ,
848
				DTK_ADDR_SPACE_I2C_TYPE_II, 0, data, 0x01);
L
Linus Torvalds 已提交
849
	if (status)
850
		dev_dbg(dev, "%s - read 2 status error = %d\n", __func__, status);
L
Linus Torvalds 已提交
851
	else
852
		dev_dbg(dev, "%s - read 2 data = 0x%x\n", __func__, *data);
853
	if ((!status) && (*data == UMP5152 || *data == UMP3410)) {
854
		dev_dbg(dev, "%s - ROM_TYPE_II\n", __func__);
L
Linus Torvalds 已提交
855
		serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
856
		goto out;
L
Linus Torvalds 已提交
857 858
	}

859 860
	/* Try to read type 3 */
	status = ti_vread_sync(serial->serial->dev, UMPC_MEMORY_READ,
861
				DTK_ADDR_SPACE_I2C_TYPE_III, 0,	data, 0x01);
L
Linus Torvalds 已提交
862
	if (status)
863
		dev_dbg(dev, "%s - read 3 status error = %d\n", __func__, status);
L
Linus Torvalds 已提交
864
	else
865
		dev_dbg(dev, "%s - read 2 data = 0x%x\n", __func__, *data);
866
	if ((!status) && (*data == UMP5152 || *data == UMP3410)) {
867
		dev_dbg(dev, "%s - ROM_TYPE_III\n", __func__);
L
Linus Torvalds 已提交
868
		serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_III;
869
		goto out;
L
Linus Torvalds 已提交
870 871
	}

872
	dev_dbg(dev, "%s - Unknown\n", __func__);
L
Linus Torvalds 已提交
873
	serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
874 875 876 877
	status = -ENODEV;
out:
	kfree(data);
	return status;
L
Linus Torvalds 已提交
878 879
}

880 881
static int bulk_xfer(struct usb_serial *serial, void *buffer,
						int length, int *num_sent)
L
Linus Torvalds 已提交
882 883 884
{
	int status;

885 886 887 888
	status = usb_bulk_msg(serial->dev,
			usb_sndbulkpipe(serial->dev,
				serial->port[0]->bulk_out_endpointAddress),
			buffer, length, num_sent, 1000);
L
Linus Torvalds 已提交
889 890 891 892
	return status;
}

/* Download given firmware image to the device (IN BOOT MODE) */
893 894
static int download_code(struct edgeport_serial *serial, __u8 *image,
							int image_length)
L
Linus Torvalds 已提交
895 896 897 898 899 900
{
	int status = 0;
	int pos;
	int transfer;
	int done;

901
	/* Transfer firmware image */
L
Linus Torvalds 已提交
902
	for (pos = 0; pos < image_length; ) {
903
		/* Read the next buffer from file */
L
Linus Torvalds 已提交
904 905 906 907
		transfer = image_length - pos;
		if (transfer > EDGE_FW_BULK_MAX_PACKET_SIZE)
			transfer = EDGE_FW_BULK_MAX_PACKET_SIZE;

908 909 910
		/* Transfer data */
		status = bulk_xfer(serial->serial, &image[pos],
							transfer, &done);
L
Linus Torvalds 已提交
911 912
		if (status)
			break;
913
		/* Advance buffer pointer */
L
Linus Torvalds 已提交
914 915 916 917 918 919
		pos += done;
	}

	return status;
}

920 921
/* FIXME!!! */
static int config_boot_dev(struct usb_device *dev)
L
Linus Torvalds 已提交
922 923 924 925
{
	return 0;
}

926 927 928 929 930
static int ti_cpu_rev(struct edge_ti_manuf_descriptor *desc)
{
	return TI_GET_CPU_REVISION(desc->CpuRev_BoardRev);
}

931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965
static int check_fw_sanity(struct edgeport_serial *serial,
		const struct firmware *fw)
{
	u16 length_total;
	u8 checksum = 0;
	int pos;
	struct device *dev = &serial->serial->interface->dev;
	struct edgeport_fw_hdr *fw_hdr = (struct edgeport_fw_hdr *)fw->data;

	if (fw->size < sizeof(struct edgeport_fw_hdr)) {
		dev_err(dev, "incomplete fw header\n");
		return -EINVAL;
	}

	length_total = le16_to_cpu(fw_hdr->length) +
			sizeof(struct edgeport_fw_hdr);

	if (fw->size != length_total) {
		dev_err(dev, "bad fw size (expected: %u, got: %zu)\n",
				length_total, fw->size);
		return -EINVAL;
	}

	for (pos = sizeof(struct edgeport_fw_hdr); pos < fw->size; ++pos)
		checksum += fw->data[pos];

	if (checksum != fw_hdr->checksum) {
		dev_err(dev, "bad fw checksum (expected: 0x%x, got: 0x%x)\n",
				fw_hdr->checksum, checksum);
		return -EINVAL;
	}

	return 0;
}

L
Linus Torvalds 已提交
966 967
/**
 * DownloadTIFirmware - Download run-time operating firmware to the TI5052
968
 *
L
Linus Torvalds 已提交
969 970 971
 * This routine downloads the main operating code into the TI5052, using the
 * boot code already burned into E2PROM or ROM.
 */
972 973
static int download_fw(struct edgeport_serial *serial,
		const struct firmware *fw)
L
Linus Torvalds 已提交
974 975 976 977 978 979 980 981
{
	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;
982 983
	struct edgeport_fw_hdr *fw_hdr = (struct edgeport_fw_hdr *)fw->data;

984
	if (check_fw_sanity(serial, fw))
985 986 987 988 989
		return -EINVAL;

	/* If on-board version is newer, "fw_version" will be updated below. */
	serial->fw_version = (fw_hdr->major_version << 8) +
			fw_hdr->minor_version;
L
Linus Torvalds 已提交
990 991 992 993 994 995 996 997 998 999

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

1000
	status = choose_config(serial->serial->dev);
L
Linus Torvalds 已提交
1001 1002 1003 1004 1005
	if (status)
		return status;

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

1010 1011 1012 1013 1014
	/*
	 * 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 已提交
1015 1016 1017
	if (interface->bNumEndpoints > 1)
		serial->product_info.TiMode = TI_MODE_DOWNLOAD;
	else
1018
		/* Otherwise we will remain in configuring mode */
L
Linus Torvalds 已提交
1019 1020 1021 1022 1023 1024 1025 1026
		serial->product_info.TiMode = TI_MODE_CONFIGURING;

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

1027
		dev_dbg(dev, "%s - RUNNING IN DOWNLOAD MODE\n", __func__);
L
Linus Torvalds 已提交
1028

1029
		status = check_i2c_image(serial);
L
Linus Torvalds 已提交
1030
		if (status) {
1031
			dev_dbg(dev, "%s - DOWNLOAD MODE -- BAD I2C\n", __func__);
L
Linus Torvalds 已提交
1032 1033
			return status;
		}
1034

L
Linus Torvalds 已提交
1035 1036 1037
		/* Validate Hardware version number
		 * Read Manufacturing Descriptor from TI Based Edgeport
		 */
1038
		ti_manuf_desc = kmalloc(sizeof(*ti_manuf_desc), GFP_KERNEL);
1039
		if (!ti_manuf_desc)
L
Linus Torvalds 已提交
1040
			return -ENOMEM;
1041

1042
		status = get_manuf_info(serial, (__u8 *)ti_manuf_desc);
L
Linus Torvalds 已提交
1043
		if (status) {
1044
			kfree(ti_manuf_desc);
L
Linus Torvalds 已提交
1045 1046 1047
			return status;
		}

1048 1049
		/* Check version number of ION descriptor */
		if (!ignore_cpu_rev && ti_cpu_rev(ti_manuf_desc) < 2) {
1050
			dev_dbg(dev, "%s - Wrong CPU Rev %d (Must be 2)\n",
1051 1052 1053 1054
				__func__, ti_cpu_rev(ti_manuf_desc));
			kfree(ti_manuf_desc);
			return -EINVAL;
  		}
L
Linus Torvalds 已提交
1055

1056
		rom_desc = kmalloc(sizeof(*rom_desc), GFP_KERNEL);
L
Linus Torvalds 已提交
1057
		if (!rom_desc) {
1058
			kfree(ti_manuf_desc);
L
Linus Torvalds 已提交
1059 1060 1061
			return -ENOMEM;
		}

1062 1063 1064 1065
		/* 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 已提交
1066
			struct ti_i2c_firmware_rec *firmware_version;
1067
			u8 *record;
L
Linus Torvalds 已提交
1068

1069
			dev_dbg(dev, "%s - Found Type FIRMWARE (Type 2) record\n", __func__);
L
Linus Torvalds 已提交
1070

1071 1072
			firmware_version = kmalloc(sizeof(*firmware_version),
								GFP_KERNEL);
L
Linus Torvalds 已提交
1073
			if (!firmware_version) {
1074 1075
				kfree(rom_desc);
				kfree(ti_manuf_desc);
L
Linus Torvalds 已提交
1076 1077 1078
				return -ENOMEM;
			}

1079 1080 1081 1082 1083
			/* Validate version number
			 * Read the descriptor data
			 */
			status = read_rom(serial, start_address +
					sizeof(struct ti_i2c_desc),
L
Linus Torvalds 已提交
1084 1085 1086
					sizeof(struct ti_i2c_firmware_rec),
					(__u8 *)firmware_version);
			if (status) {
1087 1088 1089
				kfree(firmware_version);
				kfree(rom_desc);
				kfree(ti_manuf_desc);
L
Linus Torvalds 已提交
1090 1091 1092
				return status;
			}

1093 1094 1095
			/* Check version number of download with current
			   version in I2c */
			download_cur_ver = (firmware_version->Ver_Major << 8) +
L
Linus Torvalds 已提交
1096
					   (firmware_version->Ver_Minor);
1097 1098
			download_new_ver = (fw_hdr->major_version << 8) +
					   (fw_hdr->minor_version);
L
Linus Torvalds 已提交
1099

1100 1101 1102
			dev_dbg(dev, "%s - >> FW Versions Device %d.%d  Driver %d.%d\n",
				__func__, firmware_version->Ver_Major,
				firmware_version->Ver_Minor,
1103
				fw_hdr->major_version, fw_hdr->minor_version);
L
Linus Torvalds 已提交
1104

1105 1106
			/* Check if we have an old version in the I2C and
			   update if necessary */
1107
			if (download_cur_ver < download_new_ver) {
1108 1109 1110 1111
				dev_dbg(dev, "%s - Update I2C dld from %d.%d to %d.%d\n",
					__func__,
					firmware_version->Ver_Major,
					firmware_version->Ver_Minor,
1112 1113
					fw_hdr->major_version,
					fw_hdr->minor_version);
1114

1115 1116 1117 1118 1119 1120 1121
				record = kmalloc(1, GFP_KERNEL);
				if (!record) {
					kfree(firmware_version);
					kfree(rom_desc);
					kfree(ti_manuf_desc);
					return -ENOMEM;
				}
1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133
				/* 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.
				 */
1134
				*record = I2C_DESC_TYPE_FIRMWARE_BLANK;
L
Linus Torvalds 已提交
1135

1136 1137 1138
				/* Change the I2C Firmware record type to
				   0xf2 to trigger an update */
				status = write_rom(serial, start_address,
1139
						sizeof(*record), record);
L
Linus Torvalds 已提交
1140
				if (status) {
1141
					kfree(record);
1142 1143 1144
					kfree(firmware_version);
					kfree(rom_desc);
					kfree(ti_manuf_desc);
L
Linus Torvalds 已提交
1145 1146 1147
					return status;
				}

1148 1149 1150 1151 1152
				/* 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 已提交
1153
							start_address,
1154 1155
							sizeof(*record),
							record);
L
Linus Torvalds 已提交
1156
				if (status) {
1157
					kfree(record);
1158 1159 1160
					kfree(firmware_version);
					kfree(rom_desc);
					kfree(ti_manuf_desc);
L
Linus Torvalds 已提交
1161 1162 1163
					return status;
				}

1164
				if (*record != I2C_DESC_TYPE_FIRMWARE_BLANK) {
1165
					dev_err(dev, "%s - error resetting device\n", __func__);
1166
					kfree(record);
1167 1168 1169
					kfree(firmware_version);
					kfree(rom_desc);
					kfree(ti_manuf_desc);
L
Linus Torvalds 已提交
1170 1171 1172
					return -ENODEV;
				}

1173
				dev_dbg(dev, "%s - HARDWARE RESET\n", __func__);
L
Linus Torvalds 已提交
1174

1175 1176 1177
				/* Reset UMP -- Back to BOOT MODE */
				status = ti_vsend_sync(serial->serial->dev,
						UMPC_HARDWARE_RESET,
1178 1179
						0, 0, NULL, 0,
						TI_VSEND_TIMEOUT_DEFAULT);
L
Linus Torvalds 已提交
1180

1181
				dev_dbg(dev, "%s - HARDWARE RESET return %d\n", __func__, status);
L
Linus Torvalds 已提交
1182 1183

				/* return an error on purpose. */
1184
				kfree(record);
1185 1186 1187
				kfree(firmware_version);
				kfree(rom_desc);
				kfree(ti_manuf_desc);
L
Linus Torvalds 已提交
1188
				return -ENODEV;
1189 1190 1191
			} else {
				/* Same or newer fw version is already loaded */
				serial->fw_version = download_cur_ver;
L
Linus Torvalds 已提交
1192
			}
1193
			kfree(firmware_version);
L
Linus Torvalds 已提交
1194
		}
1195 1196 1197 1198
		/* 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 已提交
1199 1200 1201
			__u8 *header;
			__u8 *vheader;

1202
			header = kmalloc(HEADER_SIZE, GFP_KERNEL);
L
Linus Torvalds 已提交
1203
			if (!header) {
1204 1205
				kfree(rom_desc);
				kfree(ti_manuf_desc);
L
Linus Torvalds 已提交
1206 1207
				return -ENOMEM;
			}
1208 1209

			vheader = kmalloc(HEADER_SIZE, GFP_KERNEL);
L
Linus Torvalds 已提交
1210
			if (!vheader) {
1211 1212 1213
				kfree(header);
				kfree(rom_desc);
				kfree(ti_manuf_desc);
L
Linus Torvalds 已提交
1214 1215
				return -ENOMEM;
			}
1216

1217
			dev_dbg(dev, "%s - Found Type BLANK FIRMWARE (Type F2) record\n", __func__);
1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229

			/*
			 * 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.
			 */
1230
			status = build_i2c_fw_hdr(header, dev, fw);
L
Linus Torvalds 已提交
1231
			if (status) {
1232 1233 1234 1235
				kfree(vheader);
				kfree(header);
				kfree(rom_desc);
				kfree(ti_manuf_desc);
1236
				return -EINVAL;
L
Linus Torvalds 已提交
1237 1238
			}

1239 1240 1241
			/* Update I2C with type 0xf2 record with correct
			   size and checksum */
			status = write_rom(serial,
L
Linus Torvalds 已提交
1242 1243 1244 1245
						start_address,
						HEADER_SIZE,
						header);
			if (status) {
1246 1247 1248 1249
				kfree(vheader);
				kfree(header);
				kfree(rom_desc);
				kfree(ti_manuf_desc);
1250
				return -EINVAL;
L
Linus Torvalds 已提交
1251 1252
			}

1253 1254 1255 1256
			/* 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 已提交
1257 1258

			if (status) {
1259
				dev_dbg(dev, "%s - can't read header back\n", __func__);
1260 1261 1262 1263
				kfree(vheader);
				kfree(header);
				kfree(rom_desc);
				kfree(ti_manuf_desc);
L
Linus Torvalds 已提交
1264 1265 1266
				return status;
			}
			if (memcmp(vheader, header, HEADER_SIZE)) {
1267
				dev_dbg(dev, "%s - write download record failed\n", __func__);
1268 1269 1270 1271
				kfree(vheader);
				kfree(header);
				kfree(rom_desc);
				kfree(ti_manuf_desc);
1272
				return -EINVAL;
L
Linus Torvalds 已提交
1273 1274
			}

1275 1276
			kfree(vheader);
			kfree(header);
L
Linus Torvalds 已提交
1277

1278
			dev_dbg(dev, "%s - Start firmware update\n", __func__);
L
Linus Torvalds 已提交
1279

1280 1281
			/* Tell firmware to copy download image into I2C */
			status = ti_vsend_sync(serial->serial->dev,
1282 1283 1284
					UMPC_COPY_DNLD_TO_I2C,
					0, 0, NULL, 0,
					TI_VSEND_TIMEOUT_FW_DOWNLOAD);
L
Linus Torvalds 已提交
1285

1286
		  	dev_dbg(dev, "%s - Update complete 0x%x\n", __func__, status);
L
Linus Torvalds 已提交
1287
			if (status) {
1288 1289 1290 1291 1292
				dev_err(dev,
					"%s - UMPC_COPY_DNLD_TO_I2C failed\n",
								__func__);
				kfree(rom_desc);
				kfree(ti_manuf_desc);
L
Linus Torvalds 已提交
1293 1294 1295 1296 1297
				return status;
			}
		}

		// The device is running the download code
1298 1299
		kfree(rom_desc);
		kfree(ti_manuf_desc);
L
Linus Torvalds 已提交
1300 1301 1302 1303 1304 1305
		return 0;
	}

	/********************************************************************/
	/* Boot Mode */
	/********************************************************************/
1306
	dev_dbg(dev, "%s - RUNNING IN BOOT MODE\n", __func__);
L
Linus Torvalds 已提交
1307

1308 1309
	/* Configure the TI device so we can use the BULK pipes for download */
	status = config_boot_dev(serial->serial->dev);
L
Linus Torvalds 已提交
1310 1311 1312
	if (status)
		return status;

1313 1314
	if (le16_to_cpu(serial->serial->dev->descriptor.idVendor)
							!= USB_VENDOR_ID_ION) {
1315 1316
		dev_dbg(dev, "%s - VID = 0x%x\n", __func__,
			le16_to_cpu(serial->serial->dev->descriptor.idVendor));
L
Linus Torvalds 已提交
1317
		serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
1318
		goto stayinbootmode;
L
Linus Torvalds 已提交
1319 1320
	}

1321 1322 1323 1324
	/* We have an ION device (I2c Must be programmed)
	   Determine I2C image type */
	if (i2c_type_bootmode(serial))
		goto stayinbootmode;
L
Linus Torvalds 已提交
1325

1326 1327
	/* Check for ION Vendor ID and that the I2C is valid */
	if (!check_i2c_image(serial)) {
L
Linus Torvalds 已提交
1328 1329 1330 1331 1332 1333 1334 1335 1336
		struct ti_i2c_image_header *header;
		int i;
		__u8 cs = 0;
		__u8 *buffer;
		int buffer_size;

		/* Validate Hardware version number
		 * Read Manufacturing Descriptor from TI Based Edgeport
		 */
1337
		ti_manuf_desc = kmalloc(sizeof(*ti_manuf_desc), GFP_KERNEL);
1338
		if (!ti_manuf_desc)
L
Linus Torvalds 已提交
1339
			return -ENOMEM;
1340

1341
		status = get_manuf_info(serial, (__u8 *)ti_manuf_desc);
L
Linus Torvalds 已提交
1342
		if (status) {
1343 1344
			kfree(ti_manuf_desc);
			goto stayinbootmode;
L
Linus Torvalds 已提交
1345 1346
		}

1347 1348
		/* Check for version 2 */
		if (!ignore_cpu_rev && ti_cpu_rev(ti_manuf_desc) < 2) {
1349 1350
			dev_dbg(dev, "%s - Wrong CPU Rev %d (Must be 2)\n",
				__func__, ti_cpu_rev(ti_manuf_desc));
1351 1352
			kfree(ti_manuf_desc);
			goto stayinbootmode;
L
Linus Torvalds 已提交
1353 1354
		}

1355
		kfree(ti_manuf_desc);
L
Linus Torvalds 已提交
1356 1357

		/*
1358 1359 1360 1361 1362 1363 1364 1365 1366
		 * 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 已提交
1367 1368 1369 1370
		 * Do we really have to copy the whole firmware image,
		 * or could we do this in place!
		 */

1371 1372 1373 1374
		/* 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);
1375
		if (!buffer)
L
Linus Torvalds 已提交
1376
			return -ENOMEM;
1377 1378 1379

		/* Initialize the buffer to 0xff (pad the buffer) */
		memset(buffer, 0xff, buffer_size);
1380
		memcpy(buffer, &fw->data[4], fw->size - 4);
L
Linus Torvalds 已提交
1381

1382 1383
		for (i = sizeof(struct ti_i2c_image_header);
				i < buffer_size; i++) {
L
Linus Torvalds 已提交
1384 1385
			cs = (__u8)(cs + buffer[i]);
		}
1386

L
Linus Torvalds 已提交
1387
		header = (struct ti_i2c_image_header *)buffer;
1388 1389 1390 1391

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

1394
		/* Download the operational code  */
1395 1396 1397
		dev_dbg(dev, "%s - Downloading operational code image version %d.%d (TI UMP)\n",
				__func__,
				fw_hdr->major_version, fw_hdr->minor_version);
1398
		status = download_code(serial, buffer, buffer_size);
L
Linus Torvalds 已提交
1399

1400
		kfree(buffer);
L
Linus Torvalds 已提交
1401 1402

		if (status) {
1403
			dev_dbg(dev, "%s - Error downloading operational code image\n", __func__);
L
Linus Torvalds 已提交
1404 1405 1406
			return status;
		}

1407
		/* Device will reboot */
L
Linus Torvalds 已提交
1408 1409
		serial->product_info.TiMode = TI_MODE_TRANSITIONING;

1410
		dev_dbg(dev, "%s - Download successful -- Device rebooting...\n", __func__);
L
Linus Torvalds 已提交
1411 1412 1413 1414 1415

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

1416 1417
stayinbootmode:
	/* Eprom is invalid or blank stay in boot mode */
1418
	dev_dbg(dev, "%s - STAYING IN BOOT MODE\n", __func__);
L
Linus Torvalds 已提交
1419 1420 1421 1422 1423 1424
	serial->product_info.TiMode = TI_MODE_BOOT;

	return 0;
}


1425
static int ti_do_config(struct edgeport_port *port, int feature, int on)
L
Linus Torvalds 已提交
1426
{
1427 1428
	int port_number = port->port->port_number;

1429 1430 1431 1432
	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 已提交
1433 1434 1435
}


1436
static int restore_mcr(struct edgeport_port *port, __u8 mcr)
L
Linus Torvalds 已提交
1437 1438 1439
{
	int status = 0;

1440
	dev_dbg(&port->port->dev, "%s - %x\n", __func__, mcr);
L
Linus Torvalds 已提交
1441

1442
	status = ti_do_config(port, UMPC_SET_CLR_DTR, mcr & MCR_DTR);
L
Linus Torvalds 已提交
1443 1444
	if (status)
		return status;
1445
	status = ti_do_config(port, UMPC_SET_CLR_RTS, mcr & MCR_RTS);
L
Linus Torvalds 已提交
1446 1447
	if (status)
		return status;
1448
	return ti_do_config(port, UMPC_SET_CLR_LOOPBACK, mcr & MCR_LOOPBACK);
L
Linus Torvalds 已提交
1449 1450 1451
}

/* Convert TI LSR to standard UART flags */
1452
static __u8 map_line_status(__u8 ti_lsr)
L
Linus Torvalds 已提交
1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463
{
	__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 */
1464 1465
	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 已提交
1466 1467 1468 1469 1470 1471

#undef MAP_FLAG

	return lsr;
}

1472
static void handle_new_msr(struct edgeport_port *edge_port, __u8 msr)
L
Linus Torvalds 已提交
1473 1474 1475 1476
{
	struct async_icount *icount;
	struct tty_struct *tty;

1477
	dev_dbg(&edge_port->port->dev, "%s - %02x\n", __func__, msr);
L
Linus Torvalds 已提交
1478

1479 1480
	if (msr & (EDGEPORT_MSR_DELTA_CTS | EDGEPORT_MSR_DELTA_DSR |
			EDGEPORT_MSR_DELTA_RI | EDGEPORT_MSR_DELTA_CD)) {
1481
		icount = &edge_port->port->icount;
L
Linus Torvalds 已提交
1482 1483 1484 1485 1486 1487 1488 1489 1490 1491

		/* 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++;
1492
		wake_up_interruptible(&edge_port->port->port.delta_msr_wait);
L
Linus Torvalds 已提交
1493 1494 1495 1496 1497
	}

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

A
Alan Cox 已提交
1498
	tty = tty_port_tty_get(&edge_port->port->port);
L
Linus Torvalds 已提交
1499 1500
	/* handle CTS flow control */
	if (tty && C_CRTSCTS(tty)) {
1501
		if (msr & EDGEPORT_MSR_CTS)
L
Linus Torvalds 已提交
1502 1503
			tty_wakeup(tty);
	}
A
Alan Cox 已提交
1504
	tty_kref_put(tty);
L
Linus Torvalds 已提交
1505 1506
}

1507 1508
static void handle_new_lsr(struct edgeport_port *edge_port, int lsr_data,
							__u8 lsr, __u8 data)
L
Linus Torvalds 已提交
1509 1510
{
	struct async_icount *icount;
1511 1512
	__u8 new_lsr = (__u8)(lsr & (__u8)(LSR_OVER_ERR | LSR_PAR_ERR |
						LSR_FRM_ERR | LSR_BREAK));
L
Linus Torvalds 已提交
1513

1514
	dev_dbg(&edge_port->port->dev, "%s - %02x\n", __func__, new_lsr);
L
Linus Torvalds 已提交
1515 1516 1517

	edge_port->shadow_lsr = lsr;

1518
	if (new_lsr & LSR_BREAK)
L
Linus Torvalds 已提交
1519 1520 1521 1522 1523 1524 1525
		/*
		 * 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 */
J
Jiri Slaby 已提交
1526 1527
	if (lsr_data)
		edge_tty_recv(edge_port->port, &data, 1);
L
Linus Torvalds 已提交
1528 1529

	/* update input line counters */
1530
	icount = &edge_port->port->icount;
L
Linus Torvalds 已提交
1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541
	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++;
}


1542
static void edge_interrupt_callback(struct urb *urb)
L
Linus Torvalds 已提交
1543
{
1544
	struct edgeport_serial *edge_serial = urb->context;
L
Linus Torvalds 已提交
1545 1546
	struct usb_serial_port *port;
	struct edgeport_port *edge_port;
1547
	struct device *dev;
L
Linus Torvalds 已提交
1548 1549 1550 1551
	unsigned char *data = urb->transfer_buffer;
	int length = urb->actual_length;
	int port_number;
	int function;
1552
	int retval;
L
Linus Torvalds 已提交
1553 1554
	__u8 lsr;
	__u8 msr;
1555
	int status = urb->status;
L
Linus Torvalds 已提交
1556

1557
	switch (status) {
L
Linus Torvalds 已提交
1558 1559 1560 1561 1562 1563 1564
	case 0:
		/* success */
		break;
	case -ECONNRESET:
	case -ENOENT:
	case -ESHUTDOWN:
		/* this urb is terminated, clean up */
1565
		dev_dbg(&urb->dev->dev, "%s - urb shutting down with status: %d\n",
1566
		    __func__, status);
L
Linus Torvalds 已提交
1567 1568
		return;
	default:
1569
		dev_err(&urb->dev->dev, "%s - nonzero urb status received: "
1570
			"%d\n", __func__, status);
L
Linus Torvalds 已提交
1571 1572 1573 1574
		goto exit;
	}

	if (!length) {
1575
		dev_dbg(&urb->dev->dev, "%s - no data in urb\n", __func__);
L
Linus Torvalds 已提交
1576 1577
		goto exit;
	}
1578

1579
	dev = &edge_serial->serial->dev->dev;
1580
	usb_serial_debug_data(dev, __func__, length, data);
1581

L
Linus Torvalds 已提交
1582
	if (length != 2) {
1583
		dev_dbg(dev, "%s - expecting packet of size 2, got %d\n", __func__, length);
L
Linus Torvalds 已提交
1584 1585 1586
		goto exit;
	}

1587 1588
	port_number = TIUMP_GET_PORT_FROM_CODE(data[0]);
	function    = TIUMP_GET_FUNC_FROM_CODE(data[0]);
1589 1590
	dev_dbg(dev, "%s - port_number %d, function %d, info 0x%x\n", __func__,
		port_number, function, data[1]);
L
Linus Torvalds 已提交
1591 1592 1593
	port = edge_serial->serial->port[port_number];
	edge_port = usb_get_serial_port_data(port);
	if (!edge_port) {
1594
		dev_dbg(dev, "%s - edge_port not found\n", __func__);
L
Linus Torvalds 已提交
1595 1596 1597 1598
		return;
	}
	switch (function) {
	case TIUMP_INTERRUPT_CODE_LSR:
1599
		lsr = map_line_status(data[1]);
L
Linus Torvalds 已提交
1600
		if (lsr & UMP_UART_LSR_DATA_MASK) {
1601 1602
			/* Save the LSR event for bulk read
			   completion routine */
1603 1604
			dev_dbg(dev, "%s - LSR Event Port %u LSR Status = %02x\n",
				__func__, port_number, lsr);
L
Linus Torvalds 已提交
1605 1606 1607
			edge_port->lsr_event = 1;
			edge_port->lsr_mask = lsr;
		} else {
1608 1609
			dev_dbg(dev, "%s - ===== Port %d LSR Status = %02x ======\n",
				__func__, port_number, lsr);
1610
			handle_new_lsr(edge_port, 0, lsr, 0);
L
Linus Torvalds 已提交
1611 1612 1613
		}
		break;

1614
	case TIUMP_INTERRUPT_CODE_MSR:	/* MSR */
L
Linus Torvalds 已提交
1615 1616
		/* Copy MSR from UMP */
		msr = data[1];
1617 1618
		dev_dbg(dev, "%s - ===== Port %u MSR Status = %02x ======\n",
			__func__, port_number, msr);
1619
		handle_new_msr(edge_port, msr);
L
Linus Torvalds 已提交
1620 1621 1622
		break;

	default:
1623 1624 1625
		dev_err(&urb->dev->dev,
			"%s - Unknown Interrupt code from UMP %x\n",
			__func__, data[1]);
L
Linus Torvalds 已提交
1626
		break;
1627

L
Linus Torvalds 已提交
1628 1629 1630
	}

exit:
1631
	retval = usb_submit_urb(urb, GFP_ATOMIC);
1632
	if (retval)
1633 1634
		dev_err(&urb->dev->dev,
			"%s - usb_submit_urb failed with result %d\n",
1635
			 __func__, retval);
L
Linus Torvalds 已提交
1636 1637
}

1638
static void edge_bulk_in_callback(struct urb *urb)
L
Linus Torvalds 已提交
1639
{
1640
	struct edgeport_port *edge_port = urb->context;
1641
	struct device *dev = &edge_port->port->dev;
L
Linus Torvalds 已提交
1642
	unsigned char *data = urb->transfer_buffer;
1643
	int retval = 0;
L
Linus Torvalds 已提交
1644
	int port_number;
1645
	int status = urb->status;
L
Linus Torvalds 已提交
1646

1647
	switch (status) {
L
Linus Torvalds 已提交
1648 1649 1650 1651 1652 1653 1654
	case 0:
		/* success */
		break;
	case -ECONNRESET:
	case -ENOENT:
	case -ESHUTDOWN:
		/* this urb is terminated, clean up */
1655
		dev_dbg(&urb->dev->dev, "%s - urb shutting down with status: %d\n", __func__, status);
L
Linus Torvalds 已提交
1656 1657
		return;
	default:
1658
		dev_err(&urb->dev->dev, "%s - nonzero read bulk status received: %d\n", __func__, status);
L
Linus Torvalds 已提交
1659 1660
	}

1661
	if (status == -EPIPE)
L
Linus Torvalds 已提交
1662 1663
		goto exit;

1664
	if (status) {
1665
		dev_err(&urb->dev->dev, "%s - stopping read!\n", __func__);
L
Linus Torvalds 已提交
1666 1667 1668
		return;
	}

1669
	port_number = edge_port->port->port_number;
L
Linus Torvalds 已提交
1670 1671 1672

	if (edge_port->lsr_event) {
		edge_port->lsr_event = 0;
1673 1674
		dev_dbg(dev, "%s ===== Port %u LSR Status = %02x, Data = %02x ======\n",
			__func__, port_number, edge_port->lsr_mask, *data);
1675
		handle_new_lsr(edge_port, 1, edge_port->lsr_mask, *data);
L
Linus Torvalds 已提交
1676 1677 1678 1679 1680
		/* Adjust buffer length/pointer */
		--urb->actual_length;
		++data;
	}

J
Jiri Slaby 已提交
1681
	if (urb->actual_length) {
1682
		usb_serial_debug_data(dev, __func__, urb->actual_length, data);
1683
		if (edge_port->close_pending)
1684
			dev_dbg(dev, "%s - close pending, dropping data on the floor\n",
1685 1686
								__func__);
		else
J
Jiri Slaby 已提交
1687
			edge_tty_recv(edge_port->port, data,
J
Jiri Slaby 已提交
1688
					urb->actual_length);
1689
		edge_port->port->icount.rx += urb->actual_length;
L
Linus Torvalds 已提交
1690 1691 1692 1693 1694
	}

exit:
	/* continue read unless stopped */
	spin_lock(&edge_port->ep_lock);
1695
	if (edge_port->ep_read_urb_state == EDGE_READ_URB_RUNNING)
1696
		retval = usb_submit_urb(urb, GFP_ATOMIC);
1697
	else if (edge_port->ep_read_urb_state == EDGE_READ_URB_STOPPING)
L
Linus Torvalds 已提交
1698
		edge_port->ep_read_urb_state = EDGE_READ_URB_STOPPED;
1699

L
Linus Torvalds 已提交
1700
	spin_unlock(&edge_port->ep_lock);
1701
	if (retval)
1702
		dev_err(dev, "%s - usb_submit_urb failed with result %d\n", __func__, retval);
L
Linus Torvalds 已提交
1703 1704
}

J
Jiri Slaby 已提交
1705 1706
static void edge_tty_recv(struct usb_serial_port *port, unsigned char *data,
		int length)
L
Linus Torvalds 已提交
1707
{
1708
	int queued;
L
Linus Torvalds 已提交
1709

J
Jiri Slaby 已提交
1710
	queued = tty_insert_flip_string(&port->port, data, length);
1711
	if (queued < length)
J
Jiri Slaby 已提交
1712
		dev_err(&port->dev, "%s - dropping data, %d bytes lost\n",
1713
			__func__, length - queued);
J
Jiri Slaby 已提交
1714
	tty_flip_buffer_push(&port->port);
L
Linus Torvalds 已提交
1715 1716
}

1717
static void edge_bulk_out_callback(struct urb *urb)
L
Linus Torvalds 已提交
1718
{
1719
	struct usb_serial_port *port = urb->context;
L
Linus Torvalds 已提交
1720
	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1721
	int status = urb->status;
A
Alan Cox 已提交
1722
	struct tty_struct *tty;
L
Linus Torvalds 已提交
1723 1724 1725

	edge_port->ep_write_urb_in_use = 0;

1726
	switch (status) {
L
Linus Torvalds 已提交
1727 1728 1729 1730 1731 1732 1733
	case 0:
		/* success */
		break;
	case -ECONNRESET:
	case -ENOENT:
	case -ESHUTDOWN:
		/* this urb is terminated, clean up */
1734
		dev_dbg(&urb->dev->dev, "%s - urb shutting down with status: %d\n",
1735
		    __func__, status);
L
Linus Torvalds 已提交
1736 1737
		return;
	default:
1738
		dev_err_console(port, "%s - nonzero write bulk status "
1739
			"received: %d\n", __func__, status);
L
Linus Torvalds 已提交
1740 1741 1742
	}

	/* send any buffered data */
A
Alan Cox 已提交
1743
	tty = tty_port_tty_get(&port->port);
1744
	edge_send(port, tty);
A
Alan Cox 已提交
1745
	tty_kref_put(tty);
L
Linus Torvalds 已提交
1746 1747
}

1748
static int edge_open(struct tty_struct *tty, struct usb_serial_port *port)
L
Linus Torvalds 已提交
1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761
{
	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;

	if (edge_port == NULL)
		return -ENODEV;

1762
	port_number = port->port_number;
L
Linus Torvalds 已提交
1763 1764 1765 1766

	dev = port->serial->dev;

	/* turn off loopback */
1767
	status = ti_do_config(edge_port, UMPC_SET_CLR_LOOPBACK, 0);
L
Linus Torvalds 已提交
1768
	if (status) {
1769 1770
		dev_err(&port->dev,
				"%s - cannot send clear loopback command, %d\n",
1771
			__func__, status);
L
Linus Torvalds 已提交
1772 1773
		return status;
	}
1774

L
Linus Torvalds 已提交
1775
	/* set up the port settings */
A
Alan Cox 已提交
1776
	if (tty)
1777
		edge_set_termios(tty, port, &tty->termios);
L
Linus Torvalds 已提交
1778 1779 1780 1781 1782 1783

	/* open up the port */

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

1784 1785
	edge_port->ump_read_timeout =
				max(20, ((transaction_timeout * 3) / 2));
L
Linus Torvalds 已提交
1786

1787 1788 1789
	/* milliseconds to timeout for DMA transfer */
	open_settings = (u8)(UMP_DMA_MODE_CONTINOUS |
			     UMP_PIPE_TRANS_TIMEOUT_ENA |
L
Linus Torvalds 已提交
1790 1791
			     (transaction_timeout << 2));

1792
	dev_dbg(&port->dev, "%s - Sending UMPC_OPEN_PORT\n", __func__);
L
Linus Torvalds 已提交
1793 1794

	/* Tell TI to open and start the port */
1795 1796
	status = send_cmd(dev, UMPC_OPEN_PORT,
		(u8)(UMPM_UART1_PORT + port_number), open_settings, NULL, 0);
L
Linus Torvalds 已提交
1797
	if (status) {
1798 1799
		dev_err(&port->dev, "%s - cannot send open command, %d\n",
							__func__, status);
L
Linus Torvalds 已提交
1800 1801 1802 1803
		return status;
	}

	/* Start the DMA? */
1804 1805
	status = send_cmd(dev, UMPC_START_PORT,
		(u8)(UMPM_UART1_PORT + port_number), 0, NULL, 0);
L
Linus Torvalds 已提交
1806
	if (status) {
1807 1808
		dev_err(&port->dev, "%s - cannot send start DMA command, %d\n",
							__func__, status);
L
Linus Torvalds 已提交
1809 1810 1811 1812
		return status;
	}

	/* Clear TX and RX buffers in UMP */
1813
	status = purge_port(port, UMP_PORT_DIR_OUT | UMP_PORT_DIR_IN);
L
Linus Torvalds 已提交
1814
	if (status) {
1815 1816 1817
		dev_err(&port->dev,
			"%s - cannot send clear buffers command, %d\n",
			__func__, status);
L
Linus Torvalds 已提交
1818 1819 1820 1821
		return status;
	}

	/* Read Initial MSR */
1822 1823 1824
	status = ti_vread_sync(dev, UMPC_READ_MSR, 0,
				(__u16)(UMPM_UART1_PORT + port_number),
				&edge_port->shadow_msr, 1);
L
Linus Torvalds 已提交
1825
	if (status) {
1826 1827
		dev_err(&port->dev, "%s - cannot send read MSR command, %d\n",
							__func__, status);
L
Linus Torvalds 已提交
1828 1829 1830
		return status;
	}

1831
	dev_dbg(&port->dev, "ShadowMSR 0x%X\n", edge_port->shadow_msr);
1832

L
Linus Torvalds 已提交
1833 1834
	/* Set Initial MCR */
	edge_port->shadow_mcr = MCR_RTS | MCR_DTR;
1835
	dev_dbg(&port->dev, "ShadowMCR 0x%X\n", edge_port->shadow_mcr);
L
Linus Torvalds 已提交
1836 1837

	edge_serial = edge_port->edge_serial;
1838
	if (mutex_lock_interruptible(&edge_serial->es_lock))
L
Linus Torvalds 已提交
1839 1840
		return -ERESTARTSYS;
	if (edge_serial->num_ports_open == 0) {
1841
		/* we are the first port to open, post the interrupt urb */
L
Linus Torvalds 已提交
1842 1843
		urb = edge_serial->serial->port[0]->interrupt_in_urb;
		if (!urb) {
1844 1845 1846
			dev_err(&port->dev,
				"%s - no interrupt urb present, exiting\n",
				__func__);
L
Linus Torvalds 已提交
1847
			status = -EINVAL;
1848
			goto release_es_lock;
L
Linus Torvalds 已提交
1849 1850
		}
		urb->context = edge_serial;
1851
		status = usb_submit_urb(urb, GFP_KERNEL);
L
Linus Torvalds 已提交
1852
		if (status) {
1853 1854 1855
			dev_err(&port->dev,
				"%s - usb_submit_urb failed with value %d\n",
					__func__, status);
1856
			goto release_es_lock;
L
Linus Torvalds 已提交
1857 1858 1859 1860 1861 1862 1863
		}
	}

	/*
	 * reset the data toggle on the bulk endpoints to work around bug in
	 * host controllers where things get out of sync some times
	 */
1864 1865
	usb_clear_halt(dev, port->write_urb->pipe);
	usb_clear_halt(dev, port->read_urb->pipe);
L
Linus Torvalds 已提交
1866 1867 1868 1869

	/* start up our bulk read urb */
	urb = port->read_urb;
	if (!urb) {
1870 1871
		dev_err(&port->dev, "%s - no read urb present, exiting\n",
								__func__);
L
Linus Torvalds 已提交
1872 1873 1874 1875 1876
		status = -EINVAL;
		goto unlink_int_urb;
	}
	edge_port->ep_read_urb_state = EDGE_READ_URB_RUNNING;
	urb->context = edge_port;
1877
	status = usb_submit_urb(urb, GFP_KERNEL);
L
Linus Torvalds 已提交
1878
	if (status) {
1879 1880 1881
		dev_err(&port->dev,
			"%s - read bulk usb_submit_urb failed with value %d\n",
				__func__, status);
L
Linus Torvalds 已提交
1882 1883 1884 1885 1886
		goto unlink_int_urb;
	}

	++edge_serial->num_ports_open;

1887
	goto release_es_lock;
L
Linus Torvalds 已提交
1888 1889 1890 1891

unlink_int_urb:
	if (edge_port->edge_serial->num_ports_open == 0)
		usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
1892 1893
release_es_lock:
	mutex_unlock(&edge_serial->es_lock);
L
Linus Torvalds 已提交
1894 1895 1896
	return status;
}

1897
static void edge_close(struct usb_serial_port *port)
L
Linus Torvalds 已提交
1898 1899 1900
{
	struct edgeport_serial *edge_serial;
	struct edgeport_port *edge_port;
1901
	struct usb_serial *serial = port->serial;
1902
	unsigned long flags;
L
Linus Torvalds 已提交
1903 1904 1905 1906
	int port_number;

	edge_serial = usb_get_serial_data(port->serial);
	edge_port = usb_get_serial_port_data(port);
1907
	if (edge_serial == NULL || edge_port == NULL)
L
Linus Torvalds 已提交
1908
		return;
1909 1910

	/* The bulkreadcompletion routine will check
L
Linus Torvalds 已提交
1911 1912 1913 1914 1915 1916
	 * this flag and dump add read data */
	edge_port->close_pending = 1;

	usb_kill_urb(port->read_urb);
	usb_kill_urb(port->write_urb);
	edge_port->ep_write_urb_in_use = 0;
1917
	spin_lock_irqsave(&edge_port->ep_lock, flags);
J
Johan Hovold 已提交
1918
	kfifo_reset_out(&port->write_fifo);
1919
	spin_unlock_irqrestore(&edge_port->ep_lock, flags);
L
Linus Torvalds 已提交
1920

1921
	dev_dbg(&port->dev, "%s - send umpc_close_port\n", __func__);
1922
	port_number = port->port_number;
1923 1924
	send_cmd(serial->dev, UMPC_CLOSE_PORT,
		     (__u8)(UMPM_UART1_PORT + port_number), 0, NULL, 0);
1925

1926
	mutex_lock(&edge_serial->es_lock);
L
Linus Torvalds 已提交
1927 1928 1929 1930 1931 1932
	--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;
	}
1933
	mutex_unlock(&edge_serial->es_lock);
L
Linus Torvalds 已提交
1934 1935 1936
	edge_port->close_pending = 0;
}

A
Alan Cox 已提交
1937 1938
static int edge_write(struct tty_struct *tty, struct usb_serial_port *port,
				const unsigned char *data, int count)
L
Linus Torvalds 已提交
1939 1940 1941 1942
{
	struct edgeport_port *edge_port = usb_get_serial_port_data(port);

	if (count == 0) {
1943
		dev_dbg(&port->dev, "%s - write request of 0 bytes\n", __func__);
L
Linus Torvalds 已提交
1944 1945 1946 1947 1948 1949 1950 1951
		return 0;
	}

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

J
Johan Hovold 已提交
1952
	count = kfifo_in_locked(&port->write_fifo, data, count,
1953
							&edge_port->ep_lock);
1954
	edge_send(port, tty);
L
Linus Torvalds 已提交
1955 1956 1957 1958

	return count;
}

1959
static void edge_send(struct usb_serial_port *port, struct tty_struct *tty)
L
Linus Torvalds 已提交
1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971
{
	int count, result;
	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
	unsigned long flags;

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

J
Johan Hovold 已提交
1972
	count = kfifo_out(&port->write_fifo,
L
Linus Torvalds 已提交
1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984
				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);

1985
	usb_serial_debug_data(&port->dev, __func__, count, port->write_urb->transfer_buffer);
L
Linus Torvalds 已提交
1986 1987

	/* set up our urb */
1988
	port->write_urb->transfer_buffer_length = count;
L
Linus Torvalds 已提交
1989 1990 1991 1992

	/* send the data out the bulk port */
	result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
	if (result) {
1993
		dev_err_console(port,
1994 1995
			"%s - failed submitting write urb, error %d\n",
				__func__, result);
L
Linus Torvalds 已提交
1996
		edge_port->ep_write_urb_in_use = 0;
1997 1998
		/* TODO: reschedule edge_send */
	} else
1999
		edge_port->port->icount.tx += count;
L
Linus Torvalds 已提交
2000 2001 2002

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

A
Alan Cox 已提交
2007
static int edge_write_room(struct tty_struct *tty)
L
Linus Torvalds 已提交
2008
{
A
Alan Cox 已提交
2009
	struct usb_serial_port *port = tty->driver_data;
L
Linus Torvalds 已提交
2010 2011 2012 2013 2014
	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
	int room = 0;
	unsigned long flags;

	if (edge_port == NULL)
2015
		return 0;
L
Linus Torvalds 已提交
2016
	if (edge_port->close_pending == 1)
2017
		return 0;
L
Linus Torvalds 已提交
2018 2019

	spin_lock_irqsave(&edge_port->ep_lock, flags);
J
Johan Hovold 已提交
2020
	room = kfifo_avail(&port->write_fifo);
L
Linus Torvalds 已提交
2021 2022
	spin_unlock_irqrestore(&edge_port->ep_lock, flags);

2023
	dev_dbg(&port->dev, "%s - returns %d\n", __func__, room);
L
Linus Torvalds 已提交
2024 2025 2026
	return room;
}

A
Alan Cox 已提交
2027
static int edge_chars_in_buffer(struct tty_struct *tty)
L
Linus Torvalds 已提交
2028
{
A
Alan Cox 已提交
2029
	struct usb_serial_port *port = tty->driver_data;
L
Linus Torvalds 已提交
2030 2031 2032 2033
	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
	int chars = 0;
	unsigned long flags;
	if (edge_port == NULL)
2034
		return 0;
L
Linus Torvalds 已提交
2035 2036

	spin_lock_irqsave(&edge_port->ep_lock, flags);
J
Johan Hovold 已提交
2037
	chars = kfifo_len(&port->write_fifo);
L
Linus Torvalds 已提交
2038 2039
	spin_unlock_irqrestore(&edge_port->ep_lock, flags);

2040
	dev_dbg(&port->dev, "%s - returns %d\n", __func__, chars);
L
Linus Torvalds 已提交
2041 2042 2043
	return chars;
}

2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055
static bool edge_tx_empty(struct usb_serial_port *port)
{
	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
	int ret;

	ret = tx_active(edge_port);
	if (ret > 0)
		return false;

	return true;
}

A
Alan Cox 已提交
2056
static void edge_throttle(struct tty_struct *tty)
L
Linus Torvalds 已提交
2057
{
A
Alan Cox 已提交
2058
	struct usb_serial_port *port = tty->driver_data;
L
Linus Torvalds 已提交
2059 2060 2061 2062 2063 2064 2065 2066 2067
	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
	int status;

	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 已提交
2068 2069 2070 2071
		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 已提交
2072 2073 2074 2075 2076 2077 2078 2079 2080
	}

	/* 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 已提交
2081
static void edge_unthrottle(struct tty_struct *tty)
L
Linus Torvalds 已提交
2082
{
A
Alan Cox 已提交
2083
	struct usb_serial_port *port = tty->driver_data;
L
Linus Torvalds 已提交
2084 2085 2086 2087 2088 2089 2090 2091 2092
	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
	int status;

	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 已提交
2093 2094 2095 2096
		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 已提交
2097 2098 2099 2100 2101 2102
	}
	/* 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)
2103 2104 2105
			dev_err(&port->dev,
				"%s - read bulk usb_submit_urb failed: %d\n",
							__func__, status);
L
Linus Torvalds 已提交
2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132
	}

}

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;
2133
		status = usb_submit_urb(urb, GFP_ATOMIC);
L
Linus Torvalds 已提交
2134 2135 2136 2137 2138 2139 2140 2141 2142
	}
	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 已提交
2143 2144
static void change_port_settings(struct tty_struct *tty,
		struct edgeport_port *edge_port, struct ktermios *old_termios)
L
Linus Torvalds 已提交
2145
{
2146
	struct device *dev = &edge_port->port->dev;
L
Linus Torvalds 已提交
2147 2148 2149 2150
	struct ump_uart_config *config;
	int baud;
	unsigned cflag;
	int status;
2151
	int port_number = edge_port->port->port_number;
L
Linus Torvalds 已提交
2152

A
Alan Cox 已提交
2153
	config = kmalloc (sizeof (*config), GFP_KERNEL);
L
Linus Torvalds 已提交
2154
	if (!config) {
2155
		tty->termios = *old_termios;
L
Linus Torvalds 已提交
2156 2157 2158
		return;
	}

2159
	cflag = tty->termios.c_cflag;
L
Linus Torvalds 已提交
2160 2161 2162 2163 2164 2165 2166 2167 2168

	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) {
2169 2170
	case CS5:
		    config->bDataBits = UMP_UART_CHAR5BITS;
2171
		    dev_dbg(dev, "%s - data bits = 5\n", __func__);
2172 2173 2174
		    break;
	case CS6:
		    config->bDataBits = UMP_UART_CHAR6BITS;
2175
		    dev_dbg(dev, "%s - data bits = 6\n", __func__);
2176 2177 2178
		    break;
	case CS7:
		    config->bDataBits = UMP_UART_CHAR7BITS;
2179
		    dev_dbg(dev, "%s - data bits = 7\n", __func__);
2180 2181 2182 2183
		    break;
	default:
	case CS8:
		    config->bDataBits = UMP_UART_CHAR8BITS;
2184
		    dev_dbg(dev, "%s - data bits = 8\n", __func__);
L
Linus Torvalds 已提交
2185 2186 2187 2188 2189 2190 2191
			    break;
	}

	if (cflag & PARENB) {
		if (cflag & PARODD) {
			config->wFlags |= UMP_MASK_UART_FLAGS_PARITY;
			config->bParity = UMP_UART_ODDPARITY;
2192
			dev_dbg(dev, "%s - parity = odd\n", __func__);
L
Linus Torvalds 已提交
2193 2194 2195
		} else {
			config->wFlags |= UMP_MASK_UART_FLAGS_PARITY;
			config->bParity = UMP_UART_EVENPARITY;
2196
			dev_dbg(dev, "%s - parity = even\n", __func__);
L
Linus Torvalds 已提交
2197 2198
		}
	} else {
2199
		config->bParity = UMP_UART_NOPARITY;
2200
		dev_dbg(dev, "%s - parity = none\n", __func__);
L
Linus Torvalds 已提交
2201 2202 2203 2204
	}

	if (cflag & CSTOPB) {
		config->bStopBits = UMP_UART_STOPBIT2;
2205
		dev_dbg(dev, "%s - stop bits = 2\n", __func__);
L
Linus Torvalds 已提交
2206 2207
	} else {
		config->bStopBits = UMP_UART_STOPBIT1;
2208
		dev_dbg(dev, "%s - stop bits = 1\n", __func__);
L
Linus Torvalds 已提交
2209 2210 2211 2212 2213 2214
	}

	/* 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;
2215
		dev_dbg(dev, "%s - RTS/CTS is enabled\n", __func__);
L
Linus Torvalds 已提交
2216
	} else {
2217
		dev_dbg(dev, "%s - RTS/CTS is disabled\n", __func__);
L
Linus Torvalds 已提交
2218 2219 2220
		restart_read(edge_port);
	}

2221 2222 2223 2224
	/* 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 已提交
2225

2226 2227 2228
	/* if we are implementing INBOUND XON/XOFF */
	if (I_IXOFF(tty)) {
		config->wFlags |= UMP_MASK_UART_FLAGS_IN_X;
2229 2230
		dev_dbg(dev, "%s - INBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x\n",
			__func__, config->cXon, config->cXoff);
2231
	} else
2232
		dev_dbg(dev, "%s - INBOUND XON/XOFF is disabled\n", __func__);
L
Linus Torvalds 已提交
2233

2234 2235 2236
	/* if we are implementing OUTBOUND XON/XOFF */
	if (I_IXON(tty)) {
		config->wFlags |= UMP_MASK_UART_FLAGS_OUT_X;
2237 2238
		dev_dbg(dev, "%s - OUTBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x\n",
			__func__, config->cXon, config->cXoff);
2239
	} else
2240
		dev_dbg(dev, "%s - OUTBOUND XON/XOFF is disabled\n", __func__);
L
Linus Torvalds 已提交
2241

2242
	tty->termios.c_cflag &= ~CMSPAR;
A
Alan Cox 已提交
2243

L
Linus Torvalds 已提交
2244 2245 2246 2247 2248
	/* Round the baud rate */
	baud = tty_get_baud_rate(tty);
	if (!baud) {
		/* pick a default, any default... */
		baud = 9600;
A
Alan Cox 已提交
2249 2250 2251
	} else
		tty_encode_baud_rate(tty, baud, baud);

L
Linus Torvalds 已提交
2252 2253 2254
	edge_port->baud_rate = baud;
	config->wBaudRate = (__u16)((461550L + baud/2) / baud);

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

2257
	dev_dbg(dev, "%s - baud rate = %d, wBaudRate = %d\n", __func__, baud, config->wBaudRate);
L
Linus Torvalds 已提交
2258

2259 2260 2261 2262 2263 2264 2265 2266
	dev_dbg(dev, "wBaudRate:   %d\n", (int)(461550L / config->wBaudRate));
	dev_dbg(dev, "wFlags:    0x%x\n", config->wFlags);
	dev_dbg(dev, "bDataBits:   %d\n", config->bDataBits);
	dev_dbg(dev, "bParity:     %d\n", config->bParity);
	dev_dbg(dev, "bStopBits:   %d\n", config->bStopBits);
	dev_dbg(dev, "cXon:        %d\n", config->cXon);
	dev_dbg(dev, "cXoff:       %d\n", config->cXoff);
	dev_dbg(dev, "bUartMode:   %d\n", config->bUartMode);
L
Linus Torvalds 已提交
2267 2268

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

2272
	status = send_cmd(edge_port->port->serial->dev, UMPC_SET_CONFIG,
L
Linus Torvalds 已提交
2273
				(__u8)(UMPM_UART1_PORT + port_number),
2274 2275
				0, (__u8 *)config, sizeof(*config));
	if (status)
2276 2277
		dev_dbg(dev, "%s - error %d when trying to write config to device\n",
			__func__, status);
2278
	kfree(config);
L
Linus Torvalds 已提交
2279 2280
}

A
Alan Cox 已提交
2281
static void edge_set_termios(struct tty_struct *tty,
A
Alan Cox 已提交
2282
		struct usb_serial_port *port, struct ktermios *old_termios)
L
Linus Torvalds 已提交
2283 2284
{
	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
A
Alan Cox 已提交
2285 2286
	unsigned int cflag;

2287
	cflag = tty->termios.c_cflag;
L
Linus Torvalds 已提交
2288

2289
	dev_dbg(&port->dev, "%s - clfag %08x iflag %08x\n", __func__,
2290
		tty->termios.c_cflag, tty->termios.c_iflag);
2291 2292
	dev_dbg(&port->dev, "%s - old clfag %08x old iflag %08x\n", __func__,
		old_termios->c_cflag, old_termios->c_iflag);
L
Linus Torvalds 已提交
2293 2294 2295 2296

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

2300
static int edge_tiocmset(struct tty_struct *tty,
2301
					unsigned int set, unsigned int clear)
L
Linus Torvalds 已提交
2302
{
A
Alan Cox 已提交
2303
	struct usb_serial_port *port = tty->driver_data;
L
Linus Torvalds 已提交
2304 2305
	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
	unsigned int mcr;
2306
	unsigned long flags;
L
Linus Torvalds 已提交
2307

2308
	spin_lock_irqsave(&edge_port->ep_lock, flags);
L
Linus Torvalds 已提交
2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324
	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;
2325
	spin_unlock_irqrestore(&edge_port->ep_lock, flags);
L
Linus Torvalds 已提交
2326

2327
	restore_mcr(edge_port, mcr);
L
Linus Torvalds 已提交
2328 2329 2330
	return 0;
}

2331
static int edge_tiocmget(struct tty_struct *tty)
L
Linus Torvalds 已提交
2332
{
A
Alan Cox 已提交
2333
	struct usb_serial_port *port = tty->driver_data;
L
Linus Torvalds 已提交
2334 2335 2336 2337
	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
	unsigned int result = 0;
	unsigned int msr;
	unsigned int mcr;
2338
	unsigned long flags;
L
Linus Torvalds 已提交
2339

2340 2341
	spin_lock_irqsave(&edge_port->ep_lock, flags);

L
Linus Torvalds 已提交
2342 2343 2344 2345 2346 2347 2348 2349 2350 2351
	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 */


2352
	dev_dbg(&port->dev, "%s -- %x\n", __func__, result);
2353
	spin_unlock_irqrestore(&edge_port->ep_lock, flags);
L
Linus Torvalds 已提交
2354 2355 2356 2357

	return result;
}

2358 2359
static int get_serial_info(struct edgeport_port *edge_port,
				struct serial_struct __user *retinfo)
L
Linus Torvalds 已提交
2360 2361
{
	struct serial_struct tmp;
2362
	unsigned cwait;
L
Linus Torvalds 已提交
2363 2364 2365 2366

	if (!retinfo)
		return -EFAULT;

2367 2368
	cwait = edge_port->port->port.closing_wait;
	if (cwait != ASYNC_CLOSING_WAIT_NONE)
J
Johan Hovold 已提交
2369
		cwait = jiffies_to_msecs(cwait) / 10;
2370

L
Linus Torvalds 已提交
2371 2372 2373
	memset(&tmp, 0, sizeof(tmp));

	tmp.type		= PORT_16550A;
2374
	tmp.line		= edge_port->port->minor;
2375
	tmp.port		= edge_port->port->port_number;
L
Linus Torvalds 已提交
2376 2377 2378 2379 2380
	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;
2381
	tmp.closing_wait	= cwait;
L
Linus Torvalds 已提交
2382 2383 2384 2385 2386 2387

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

2388
static int edge_ioctl(struct tty_struct *tty,
2389
					unsigned int cmd, unsigned long arg)
L
Linus Torvalds 已提交
2390
{
A
Alan Cox 已提交
2391
	struct usb_serial_port *port = tty->driver_data;
L
Linus Torvalds 已提交
2392 2393 2394
	struct edgeport_port *edge_port = usb_get_serial_port_data(port);

	switch (cmd) {
2395
	case TIOCGSERIAL:
2396
		dev_dbg(&port->dev, "%s - TIOCGSERIAL\n", __func__);
2397 2398
		return get_serial_info(edge_port,
				(struct serial_struct __user *) arg);
L
Linus Torvalds 已提交
2399 2400 2401 2402
	}
	return -ENOIOCTLCMD;
}

A
Alan Cox 已提交
2403
static void edge_break(struct tty_struct *tty, int break_state)
L
Linus Torvalds 已提交
2404
{
A
Alan Cox 已提交
2405
	struct usb_serial_port *port = tty->driver_data;
L
Linus Torvalds 已提交
2406 2407
	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
	int status;
2408
	int bv = 0;	/* Off */
L
Linus Torvalds 已提交
2409

A
Alan Cox 已提交
2410
	if (break_state == -1)
2411 2412 2413
		bv = 1;	/* On */
	status = ti_do_config(edge_port, UMPC_SET_CLR_BREAK, bv);
	if (status)
2414 2415
		dev_dbg(&port->dev, "%s - error %d sending break set/clear command.\n",
			__func__, status);
L
Linus Torvalds 已提交
2416 2417
}

2418
static int edge_startup(struct usb_serial *serial)
L
Linus Torvalds 已提交
2419 2420 2421
{
	struct edgeport_serial *edge_serial;
	int status;
2422 2423 2424
	const struct firmware *fw;
	const char *fw_name = "edgeport/down3.bin";
	struct device *dev = &serial->interface->dev;
L
Linus Torvalds 已提交
2425 2426

	/* create our private serial structure */
2427
	edge_serial = kzalloc(sizeof(struct edgeport_serial), GFP_KERNEL);
2428
	if (!edge_serial)
L
Linus Torvalds 已提交
2429
		return -ENOMEM;
2430

2431
	mutex_init(&edge_serial->es_lock);
L
Linus Torvalds 已提交
2432 2433 2434
	edge_serial->serial = serial;
	usb_set_serial_data(serial, edge_serial);

2435 2436 2437 2438 2439 2440 2441 2442 2443 2444
	status = request_firmware(&fw, fw_name, dev);
	if (status) {
		dev_err(dev, "Failed to load image \"%s\" err %d\n",
				fw_name, status);
		kfree(edge_serial);
		return status;
	}

	status = download_fw(edge_serial, fw);
	release_firmware(fw);
L
Linus Torvalds 已提交
2445
	if (status) {
2446
		kfree(edge_serial);
L
Linus Torvalds 已提交
2447 2448 2449 2450 2451 2452
		return status;
	}

	return 0;
}

2453
static void edge_disconnect(struct usb_serial *serial)
L
Linus Torvalds 已提交
2454
{
2455 2456 2457 2458
}

static void edge_release(struct usb_serial *serial)
{
2459 2460 2461 2462 2463
	kfree(usb_get_serial_data(serial));
}

static int edge_port_probe(struct usb_serial_port *port)
{
2464
	struct edgeport_port *edge_port;
2465
	int ret;
2466

2467 2468 2469 2470 2471 2472 2473 2474 2475
	edge_port = kzalloc(sizeof(*edge_port), GFP_KERNEL);
	if (!edge_port)
		return -ENOMEM;

	spin_lock_init(&edge_port->ep_lock);
	edge_port->port = port;
	edge_port->edge_serial = usb_get_serial_data(port->serial);
	edge_port->bUartMode = default_uart_mode;

2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495
	switch (port->port_number) {
	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");
		ret = -ENODEV;
		goto err;
	}

	dev_dbg(&port->dev,
		"%s - port_number = %d, uart_base = %04x, dma_address = %04x\n",
		__func__, port->port_number, edge_port->uart_base,
		edge_port->dma_address);

2496 2497
	usb_set_serial_port_data(port, edge_port);

2498
	ret = edge_create_sysfs_attrs(port);
2499 2500
	if (ret)
		goto err;
2501

2502
	port->port.closing_wait = msecs_to_jiffies(closing_wait * 10);
2503
	port->port.drain_delay = 1;
2504

2505
	return 0;
2506 2507 2508 2509
err:
	kfree(edge_port);

	return ret;
L
Linus Torvalds 已提交
2510 2511
}

2512 2513 2514 2515 2516 2517 2518 2519 2520 2521
static int edge_port_remove(struct usb_serial_port *port)
{
	struct edgeport_port *edge_port;

	edge_port = usb_get_serial_port_data(port);
	edge_remove_sysfs_attrs(port);
	kfree(edge_port);

	return 0;
}
L
Linus Torvalds 已提交
2522

2523 2524
/* Sysfs Attributes */

2525
static ssize_t uart_mode_show(struct device *dev,
2526 2527 2528 2529 2530 2531 2532 2533
	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);
}

2534
static ssize_t uart_mode_store(struct device *dev,
2535 2536 2537 2538 2539 2540
	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);

2541
	dev_dbg(dev, "%s: setting uart_mode = %d\n", __func__, v);
2542 2543 2544 2545

	if (v < 256)
		edge_port->bUartMode = v;
	else
2546
		dev_err(dev, "%s - uart_mode %d is invalid\n", __func__, v);
2547 2548 2549

	return count;
}
2550
static DEVICE_ATTR_RW(uart_mode);
2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563

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


2564
static struct usb_serial_driver edgeport_1port_device = {
2565
	.driver = {
2566 2567
		.owner		= THIS_MODULE,
		.name		= "edgeport_ti_1",
2568
	},
2569
	.description		= "Edgeport TI 1 port adapter",
L
Linus Torvalds 已提交
2570 2571 2572 2573 2574 2575 2576
	.id_table		= edgeport_1port_id_table,
	.num_ports		= 1,
	.open			= edge_open,
	.close			= edge_close,
	.throttle		= edge_throttle,
	.unthrottle		= edge_unthrottle,
	.attach			= edge_startup,
2577 2578
	.disconnect		= edge_disconnect,
	.release		= edge_release,
2579 2580
	.port_probe		= edge_port_probe,
	.port_remove		= edge_port_remove,
L
Linus Torvalds 已提交
2581 2582 2583 2584
	.ioctl			= edge_ioctl,
	.set_termios		= edge_set_termios,
	.tiocmget		= edge_tiocmget,
	.tiocmset		= edge_tiocmset,
2585
	.tiocmiwait		= usb_serial_generic_tiocmiwait,
2586
	.get_icount		= usb_serial_generic_get_icount,
L
Linus Torvalds 已提交
2587 2588 2589
	.write			= edge_write,
	.write_room		= edge_write_room,
	.chars_in_buffer	= edge_chars_in_buffer,
2590
	.tx_empty		= edge_tx_empty,
L
Linus Torvalds 已提交
2591 2592 2593 2594 2595 2596
	.break_ctl		= edge_break,
	.read_int_callback	= edge_interrupt_callback,
	.read_bulk_callback	= edge_bulk_in_callback,
	.write_bulk_callback	= edge_bulk_out_callback,
};

2597
static struct usb_serial_driver edgeport_2port_device = {
2598
	.driver = {
2599 2600
		.owner		= THIS_MODULE,
		.name		= "edgeport_ti_2",
2601
	},
2602
	.description		= "Edgeport TI 2 port adapter",
L
Linus Torvalds 已提交
2603 2604 2605 2606 2607 2608 2609
	.id_table		= edgeport_2port_id_table,
	.num_ports		= 2,
	.open			= edge_open,
	.close			= edge_close,
	.throttle		= edge_throttle,
	.unthrottle		= edge_unthrottle,
	.attach			= edge_startup,
2610 2611
	.disconnect		= edge_disconnect,
	.release		= edge_release,
2612 2613
	.port_probe		= edge_port_probe,
	.port_remove		= edge_port_remove,
L
Linus Torvalds 已提交
2614 2615 2616 2617
	.ioctl			= edge_ioctl,
	.set_termios		= edge_set_termios,
	.tiocmget		= edge_tiocmget,
	.tiocmset		= edge_tiocmset,
2618
	.tiocmiwait		= usb_serial_generic_tiocmiwait,
2619
	.get_icount		= usb_serial_generic_get_icount,
L
Linus Torvalds 已提交
2620 2621 2622
	.write			= edge_write,
	.write_room		= edge_write_room,
	.chars_in_buffer	= edge_chars_in_buffer,
2623
	.tx_empty		= edge_tx_empty,
L
Linus Torvalds 已提交
2624 2625 2626 2627 2628 2629
	.break_ctl		= edge_break,
	.read_int_callback	= edge_interrupt_callback,
	.read_bulk_callback	= edge_bulk_in_callback,
	.write_bulk_callback	= edge_bulk_out_callback,
};

2630 2631 2632 2633
static struct usb_serial_driver * const serial_drivers[] = {
	&edgeport_1port_device, &edgeport_2port_device, NULL
};

2634
module_usb_serial_driver(serial_drivers, id_table_combined);
L
Linus Torvalds 已提交
2635 2636 2637 2638

MODULE_AUTHOR(DRIVER_AUTHOR);
MODULE_DESCRIPTION(DRIVER_DESC);
MODULE_LICENSE("GPL");
2639
MODULE_FIRMWARE("edgeport/down3.bin");
L
Linus Torvalds 已提交
2640 2641 2642 2643 2644

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);
2645 2646
MODULE_PARM_DESC(ignore_cpu_rev,
			"Ignore the cpu revision when connecting to a device");
L
Linus Torvalds 已提交
2647

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