io_ti.c 76.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 29
/*
 * 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/init.h>
#include <linux/slab.h>
#include <linux/tty.h>
#include <linux/tty_driver.h>
#include <linux/tty_flip.h>
#include <linux/module.h>
#include <linux/spinlock.h>
30
#include <linux/mutex.h>
L
Linus Torvalds 已提交
31
#include <linux/serial.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

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

/*
 * Version Information
 */
46
#define DRIVER_VERSION "v0.7mode043006"
L
Linus Torvalds 已提交
47 48 49 50 51 52 53 54 55 56
#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

57 58 59 60 61 62
/* 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 已提交
63 64 65 66 67 68 69 70 71 72 73 74

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

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

#define EDGE_OUT_BUF_SIZE	1024


/* Product information read from the Edgeport */
75 76 77
struct product_info {
	int	TiMode;			/* Current TI Mode  */
	__u8	hardware_type;		/* Type of hardware */
L
Linus Torvalds 已提交
78 79 80 81 82 83 84 85 86
} __attribute__((packed));

struct edgeport_port {
	__u16 uart_base;
	__u16 dma_address;
	__u8 shadow_msr;
	__u8 shadow_mcr;
	__u8 shadow_lsr;
	__u8 lsr_mask;
87
	__u32 ump_read_timeout;		/* Number of milliseconds the UMP will
L
Linus Torvalds 已提交
88 89 90 91 92 93 94 95 96 97 98
					   wait without data before completing
					   a read short */
	int baud_rate;
	int close_pending;
	int lsr_event;
	struct async_icount	icount;
	wait_queue_head_t	delta_msr_wait;	/* for handling sleeping while
						   waiting for msr change to
						   happen */
	struct edgeport_serial	*edge_serial;
	struct usb_serial_port	*port;
99
	__u8 bUartMode;		/* Port type, 0: RS232, etc. */
L
Linus Torvalds 已提交
100 101 102
	spinlock_t ep_lock;
	int ep_read_urb_state;
	int ep_write_urb_in_use;
103
	struct kfifo write_fifo;
L
Linus Torvalds 已提交
104 105 106 107
};

struct edgeport_serial {
	struct product_info product_info;
108 109 110
	u8 TI_I2C_Type;			/* Type of I2C in UMP */
	u8 TiReadI2C;			/* Set to TRUE if we have read the
					   I2c in Boot Mode */
111
	struct mutex es_lock;
L
Linus Torvalds 已提交
112 113 114 115 116 117
	int num_ports_open;
	struct usb_serial *serial;
};


/* Devices that this driver supports */
118
static const struct usb_device_id edgeport_1port_id_table[] = {
L
Linus Torvalds 已提交
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
	{ 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) },
	{ }
};

138
static const struct usb_device_id edgeport_2port_id_table[] = {
L
Linus Torvalds 已提交
139 140 141 142 143 144 145 146 147 148 149 150
	{ 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) },
151
	/* The 4, 8 and 16 port devices show up as multiple 2 port devices */
L
Linus Torvalds 已提交
152
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4S) },
153 154 155 156
	{ 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 已提交
157 158 159 160
	{ }
};

/* Devices that this driver supports */
161
static const struct usb_device_id id_table_combined[] = {
L
Linus Torvalds 已提交
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
	{ 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) },
191 192 193 194
	{ 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 已提交
195 196 197
	{ }
};

198
MODULE_DEVICE_TABLE(usb, id_table_combined);
L
Linus Torvalds 已提交
199

200 201 202
static unsigned char OperationalMajorVersion;
static unsigned char OperationalMinorVersion;
static unsigned short OperationalBuildNumber;
L
Linus Torvalds 已提交
203 204

static int closing_wait = EDGE_CLOSING_WAIT;
205
static bool ignore_cpu_rev;
206
static int default_uart_mode;		/* RS232 */
L
Linus Torvalds 已提交
207

208 209
static void edge_tty_recv(struct device *dev, struct tty_struct *tty,
			  unsigned char *data, int length);
L
Linus Torvalds 已提交
210 211 212 213

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

A
Alan Cox 已提交
214 215 216
static void edge_set_termios(struct tty_struct *tty,
		struct usb_serial_port *port, struct ktermios *old_termios);
static void edge_send(struct tty_struct *tty);
L
Linus Torvalds 已提交
217

218 219 220 221
/* 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 已提交
222

223 224
static int ti_vread_sync(struct usb_device *dev, __u8 request,
				__u16 value, __u16 index, u8 *data, int size)
L
Linus Torvalds 已提交
225 226 227
{
	int status;

228 229 230
	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 已提交
231 232 233
	if (status < 0)
		return status;
	if (status != size) {
234 235
		dev_dbg(&dev->dev, "%s - wanted to write %d, but only wrote %d\n",
			__func__, size, status);
L
Linus Torvalds 已提交
236 237 238 239 240
		return -ECOMM;
	}
	return 0;
}

241 242
static int ti_vsend_sync(struct usb_device *dev, __u8 request,
				__u16 value, __u16 index, u8 *data, int size)
L
Linus Torvalds 已提交
243 244 245
{
	int status;

246 247 248
	status = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), request,
			(USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT),
			value, index, data, size, 1000);
L
Linus Torvalds 已提交
249 250 251
	if (status < 0)
		return status;
	if (status != size) {
252 253
		dev_dbg(&dev->dev, "%s - wanted to write %d, but only wrote %d\n",
			__func__, size, status);
L
Linus Torvalds 已提交
254 255 256 257 258
		return -ECOMM;
	}
	return 0;
}

259
static int send_cmd(struct usb_device *dev, __u8 command,
L
Linus Torvalds 已提交
260 261 262
				__u8 moduleid, __u16 value, u8 *data,
				int size)
{
263
	return ti_vsend_sync(dev, command, value, moduleid, data, size);
L
Linus Torvalds 已提交
264 265 266
}

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

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

273
	return send_cmd(port->serial->dev,
L
Linus Torvalds 已提交
274 275 276 277 278 279 280 281
					UMPC_PURGE_PORT,
					(__u8)(UMPM_UART1_PORT + port_number),
					mask,
					NULL,
					0);
}

/**
282
 * read_download_mem - Read edgeport memory from TI chip
L
Linus Torvalds 已提交
283 284 285 286 287 288
 * @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
 */
289
static int read_download_mem(struct usb_device *dev, int start_address,
L
Linus Torvalds 已提交
290 291 292 293 294
				int length, __u8 address_type, __u8 *buffer)
{
	int status = 0;
	__u8 read_length;
	__be16 be_start_address;
295

296
	dev_dbg(&dev->dev, "%s - @ %x for %d\n", __func__, start_address, length);
L
Linus Torvalds 已提交
297 298 299 300 301 302

	/* Read in blocks of 64 bytes
	 * (TI firmware can't handle more than 64 byte reads)
	 */
	while (length) {
		if (length > 64)
303
			read_length = 64;
L
Linus Torvalds 已提交
304 305 306 307
		else
			read_length = (__u8)length;

		if (read_length > 1) {
308
			dev_dbg(&dev->dev, "%s - @ %x for %d\n", __func__, start_address, read_length);
L
Linus Torvalds 已提交
309
		}
310 311 312 313 314
		be_start_address = cpu_to_be16(start_address);
		status = ti_vread_sync(dev, UMPC_MEMORY_READ,
					(__u16)address_type,
					(__force __u16)be_start_address,
					buffer, read_length);
L
Linus Torvalds 已提交
315 316

		if (status) {
317
			dev_dbg(&dev->dev, "%s - ERROR %x\n", __func__, status);
L
Linus Torvalds 已提交
318 319 320
			return status;
		}

321
		if (read_length > 1)
322
			usb_serial_debug_data(&dev->dev, __func__, read_length, buffer);
L
Linus Torvalds 已提交
323 324 325 326 327 328

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

L
Linus Torvalds 已提交
330 331 332
	return status;
}

333 334
static int read_ram(struct usb_device *dev, int start_address,
						int length, __u8 *buffer)
L
Linus Torvalds 已提交
335
{
336 337
	return read_download_mem(dev, start_address, length,
					DTK_ADDR_SPACE_XDATA, buffer);
L
Linus Torvalds 已提交
338 339 340
}

/* Read edgeport memory to a given block */
341 342
static int read_boot_mem(struct edgeport_serial *serial,
				int start_address, int length, __u8 *buffer)
L
Linus Torvalds 已提交
343 344 345 346
{
	int status = 0;
	int i;

347 348 349 350
	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 已提交
351
		if (status) {
352
			dev_dbg(&serial->serial->dev->dev, "%s - ERROR %x\n", __func__, status);
L
Linus Torvalds 已提交
353 354 355 356
			return status;
		}
	}

357 358
	dev_dbg(&serial->serial->dev->dev, "%s - start_address = %x, length = %d\n",
		__func__, start_address, length);
359
	usb_serial_debug_data(&serial->serial->dev->dev, __func__, length, buffer);
L
Linus Torvalds 已提交
360 361 362 363 364 365 366

	serial->TiReadI2C = 1;

	return status;
}

/* Write given block to TI EPROM memory */
367 368
static int write_boot_mem(struct edgeport_serial *serial,
				int start_address, int length, __u8 *buffer)
L
Linus Torvalds 已提交
369 370 371
{
	int status = 0;
	int i;
372
	u8 *temp;
L
Linus Torvalds 已提交
373 374 375

	/* Must do a read before write */
	if (!serial->TiReadI2C) {
376 377 378 379 380 381 382 383
		temp = kmalloc(1, GFP_KERNEL);
		if (!temp) {
			dev_err(&serial->serial->dev->dev,
					"%s - out of memory\n", __func__);
			return -ENOMEM;
		}
		status = read_boot_mem(serial, 0, 1, temp);
		kfree(temp);
L
Linus Torvalds 已提交
384 385 386 387
		if (status)
			return status;
	}

388 389 390 391
	for (i = 0; i < length; ++i) {
		status = ti_vsend_sync(serial->serial->dev,
				UMPC_MEMORY_WRITE, buffer[i],
				(__u16)(i + start_address), NULL, 0);
L
Linus Torvalds 已提交
392 393 394 395
		if (status)
			return status;
	}

396
	dev_dbg(&serial->serial->dev->dev, "%s - start_sddr = %x, length = %d\n", __func__, start_address, length);
397
	usb_serial_debug_data(&serial->serial->dev->dev, __func__, length, buffer);
L
Linus Torvalds 已提交
398 399 400 401 402 403

	return status;
}


/* Write edgeport I2C memory to TI chip	*/
404 405
static int write_i2c_mem(struct edgeport_serial *serial,
		int start_address, int length, __u8 address_type, __u8 *buffer)
L
Linus Torvalds 已提交
406
{
407
	struct device *dev = &serial->serial->dev->dev;
L
Linus Torvalds 已提交
408 409 410 411 412
	int status = 0;
	int write_length;
	__be16 be_start_address;

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

L
Lucas De Marchi 已提交
414
	/* calculate the number of bytes left in the first page */
415 416
	write_length = EPROM_PAGE_SIZE -
				(start_address & (EPROM_PAGE_SIZE - 1));
L
Linus Torvalds 已提交
417 418 419 420

	if (write_length > length)
		write_length = length;

421 422
	dev_dbg(dev, "%s - BytesInFirstPage Addr = %x, length = %d\n",
		__func__, start_address, write_length);
423
	usb_serial_debug_data(dev, __func__, write_length, buffer);
L
Linus Torvalds 已提交
424 425

	/* Write first page */
426 427 428 429 430
	be_start_address = cpu_to_be16(start_address);
	status = ti_vsend_sync(serial->serial->dev,
				UMPC_MEMORY_WRITE, (__u16)address_type,
				(__force __u16)be_start_address,
				buffer,	write_length);
L
Linus Torvalds 已提交
431
	if (status) {
432
		dev_dbg(dev, "%s - ERROR %d\n", __func__, status);
L
Linus Torvalds 已提交
433 434 435 436 437 438 439
		return status;
	}

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

440 441
	/* We should be aligned now -- can write
	   max page size bytes at a time */
L
Linus Torvalds 已提交
442 443 444 445 446 447
	while (length) {
		if (length > EPROM_PAGE_SIZE)
			write_length = EPROM_PAGE_SIZE;
		else
			write_length = length;

448 449
		dev_dbg(dev, "%s - Page Write Addr = %x, length = %d\n",
			__func__, start_address, write_length);
450
		usb_serial_debug_data(dev, __func__, write_length, buffer);
L
Linus Torvalds 已提交
451 452

		/* Write next page */
453 454 455 456 457
		be_start_address = cpu_to_be16(start_address);
		status = ti_vsend_sync(serial->serial->dev, UMPC_MEMORY_WRITE,
				(__u16)address_type,
				(__force __u16)be_start_address,
				buffer, write_length);
L
Linus Torvalds 已提交
458
		if (status) {
459
			dev_err(dev, "%s - ERROR %d\n", __func__, status);
L
Linus Torvalds 已提交
460 461
			return status;
		}
462

L
Linus Torvalds 已提交
463 464 465 466 467 468 469 470
		length		-= write_length;
		start_address	+= write_length;
		buffer		+= write_length;
	}
	return status;
}

/* Examine the UMP DMA registers and LSR
471
 *
L
Linus Torvalds 已提交
472 473 474 475
 * 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.
 */
476
static int tx_active(struct edgeport_port *port)
L
Linus Torvalds 已提交
477 478 479 480 481 482
{
	int status;
	struct out_endpoint_desc_block *oedb;
	__u8 *lsr;
	int bytes_left = 0;

483
	oedb = kmalloc(sizeof(*oedb), GFP_KERNEL);
L
Linus Torvalds 已提交
484
	if (!oedb) {
485
		dev_err(&port->port->dev, "%s - out of memory\n", __func__);
L
Linus Torvalds 已提交
486 487 488
		return -ENOMEM;
	}

489
	lsr = kmalloc(1, GFP_KERNEL);	/* Sigh, that's right, just one byte,
L
Linus Torvalds 已提交
490 491 492 493 494 495 496
					   as not all platforms can do DMA
					   from stack */
	if (!lsr) {
		kfree(oedb);
		return -ENOMEM;
	}
	/* Read the DMA Count Registers */
497 498
	status = read_ram(port->port->serial->dev, port->dma_address,
						sizeof(*oedb), (void *)oedb);
L
Linus Torvalds 已提交
499 500 501
	if (status)
		goto exit_is_tx_active;

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

	/* and the LSR */
505 506
	status = read_ram(port->port->serial->dev,
			port->uart_base + UMPMEM_OFFS_UART_LSR, 1, lsr);
L
Linus Torvalds 已提交
507 508 509

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

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

516
	if ((*lsr & UMP_UART_LSR_TX_MASK) == 0)
L
Linus Torvalds 已提交
517 518 519 520
		bytes_left += 1;

	/* We return Not Active if we get any kind of error */
exit_is_tx_active:
521
	dev_dbg(&port->port->dev, "%s - return %d\n", __func__, bytes_left);
L
Linus Torvalds 已提交
522 523 524 525 526 527

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

528 529
static void chase_port(struct edgeport_port *port, unsigned long timeout,
								int flush)
L
Linus Torvalds 已提交
530 531
{
	int baud_rate;
A
Alan Cox 已提交
532
	struct tty_struct *tty = tty_port_tty_get(&port->port->port);
533
	struct usb_serial *serial = port->port->serial;
L
Linus Torvalds 已提交
534 535 536 537
	wait_queue_t wait;
	unsigned long flags;

	if (!timeout)
538
		timeout = (HZ * EDGE_CLOSING_WAIT)/100;
L
Linus Torvalds 已提交
539 540 541 542 543 544 545

	/* wait for data to drain from the buffer */
	spin_lock_irqsave(&port->ep_lock, flags);
	init_waitqueue_entry(&wait, current);
	add_wait_queue(&tty->write_wait, &wait);
	for (;;) {
		set_current_state(TASK_INTERRUPTIBLE);
546
		if (kfifo_len(&port->write_fifo) == 0
L
Linus Torvalds 已提交
547
		|| timeout == 0 || signal_pending(current)
548
		|| serial->disconnected)
549
			/* disconnect */
L
Linus Torvalds 已提交
550 551 552 553 554 555 556 557
			break;
		spin_unlock_irqrestore(&port->ep_lock, flags);
		timeout = schedule_timeout(timeout);
		spin_lock_irqsave(&port->ep_lock, flags);
	}
	set_current_state(TASK_RUNNING);
	remove_wait_queue(&tty->write_wait, &wait);
	if (flush)
558
		kfifo_reset_out(&port->write_fifo);
L
Linus Torvalds 已提交
559
	spin_unlock_irqrestore(&port->ep_lock, flags);
A
Alan Cox 已提交
560
	tty_kref_put(tty);
L
Linus Torvalds 已提交
561 562 563 564

	/* wait for data to drain from the device */
	timeout += jiffies;
	while ((long)(jiffies - timeout) < 0 && !signal_pending(current)
565
						&& !serial->disconnected) {
566 567
		/* not disconnected */
		if (!tx_active(port))
L
Linus Torvalds 已提交
568 569 570 571 572
			break;
		msleep(10);
	}

	/* disconnected */
573
	if (serial->disconnected)
L
Linus Torvalds 已提交
574 575 576
		return;

	/* wait one more character time, based on baud rate */
577 578 579
	/* (tx_active doesn't seem to wait for the last byte) */
	baud_rate = port->baud_rate;
	if (baud_rate == 0)
L
Linus Torvalds 已提交
580
		baud_rate = 50;
J
Julia Lawall 已提交
581
	msleep(max(1, DIV_ROUND_UP(10000, baud_rate)));
L
Linus Torvalds 已提交
582 583
}

584
static int choose_config(struct usb_device *dev)
L
Linus Torvalds 已提交
585
{
586 587 588 589 590 591
	/*
	 * 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 已提交
592

593 594 595 596
	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 已提交
597 598

	if (dev->config->desc.bNumInterfaces != 1) {
599
		dev_err(&dev->dev, "%s - bNumInterfaces is not 1, ERROR!\n", __func__);
L
Linus Torvalds 已提交
600 601 602 603 604 605
		return -ENODEV;
	}

	return 0;
}

606 607
static int read_rom(struct edgeport_serial *serial,
				int start_address, int length, __u8 *buffer)
L
Linus Torvalds 已提交
608 609 610 611
{
	int status;

	if (serial->product_info.TiMode == TI_MODE_DOWNLOAD) {
612
		status = read_download_mem(serial->serial->dev,
L
Linus Torvalds 已提交
613 614 615 616 617
					       start_address,
					       length,
					       serial->TI_I2C_Type,
					       buffer);
	} else {
618 619
		status = read_boot_mem(serial, start_address, length,
								buffer);
L
Linus Torvalds 已提交
620 621 622 623
	}
	return status;
}

624 625
static int write_rom(struct edgeport_serial *serial, int start_address,
						int length, __u8 *buffer)
L
Linus Torvalds 已提交
626 627
{
	if (serial->product_info.TiMode == TI_MODE_BOOT)
628 629
		return write_boot_mem(serial, start_address, length,
								buffer);
L
Linus Torvalds 已提交
630 631

	if (serial->product_info.TiMode == TI_MODE_DOWNLOAD)
632 633
		return write_i2c_mem(serial, start_address, length,
						serial->TI_I2C_Type, buffer);
L
Linus Torvalds 已提交
634 635 636 637 638 639
	return -EINVAL;
}



/* Read a descriptor header from I2C based on type */
640 641
static int get_descriptor_addr(struct edgeport_serial *serial,
				int desc_type, struct ti_i2c_desc *rom_desc)
L
Linus Torvalds 已提交
642 643 644 645 646 647 648
{
	int start_address;
	int status;

	/* Search for requested descriptor in I2C */
	start_address = 2;
	do {
649
		status = read_rom(serial,
L
Linus Torvalds 已提交
650 651
				   start_address,
				   sizeof(struct ti_i2c_desc),
652
				   (__u8 *)rom_desc);
L
Linus Torvalds 已提交
653 654 655 656 657 658
		if (status)
			return 0;

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

659 660
		start_address = start_address + sizeof(struct ti_i2c_desc)
							+ rom_desc->Size;
L
Linus Torvalds 已提交
661 662

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

L
Linus Torvalds 已提交
664 665 666 667
	return 0;
}

/* Validate descriptor checksum */
668
static int valid_csum(struct ti_i2c_desc *rom_desc, __u8 *buffer)
L
Linus Torvalds 已提交
669 670 671 672
{
	__u16 i;
	__u8 cs = 0;

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

L
Linus Torvalds 已提交
676
	if (cs != rom_desc->CheckSum) {
677
		pr_debug("%s - Mismatch %x - %x", __func__, rom_desc->CheckSum, cs);
L
Linus Torvalds 已提交
678 679 680 681 682 683
		return -EINVAL;
	}
	return 0;
}

/* Make sure that the I2C image is good */
684
static int check_i2c_image(struct edgeport_serial *serial)
L
Linus Torvalds 已提交
685 686 687 688 689 690 691 692
{
	struct device *dev = &serial->serial->dev->dev;
	int status = 0;
	struct ti_i2c_desc *rom_desc;
	int start_address = 2;
	__u8 *buffer;
	__u16 ttype;

693
	rom_desc = kmalloc(sizeof(*rom_desc), GFP_KERNEL);
L
Linus Torvalds 已提交
694
	if (!rom_desc) {
695
		dev_err(dev, "%s - out of memory\n", __func__);
L
Linus Torvalds 已提交
696 697
		return -ENOMEM;
	}
698
	buffer = kmalloc(TI_MAX_I2C_SIZE, GFP_KERNEL);
L
Linus Torvalds 已提交
699
	if (!buffer) {
700 701 702
		dev_err(dev, "%s - out of memory when allocating buffer\n",
								__func__);
		kfree(rom_desc);
L
Linus Torvalds 已提交
703 704 705
		return -ENOMEM;
	}

706 707
	/* Read the first byte (Signature0) must be 0x52 or 0x10 */
	status = read_rom(serial, 0, 1, buffer);
L
Linus Torvalds 已提交
708
	if (status)
709
		goto out;
L
Linus Torvalds 已提交
710 711

	if (*buffer != UMP5152 && *buffer != UMP3410) {
712
		dev_err(dev, "%s - invalid buffer signature\n", __func__);
L
Linus Torvalds 已提交
713
		status = -ENODEV;
714
		goto out;
L
Linus Torvalds 已提交
715 716 717
	}

	do {
718 719
		/* Validate the I2C */
		status = read_rom(serial,
L
Linus Torvalds 已提交
720 721 722 723 724 725
				start_address,
				sizeof(struct ti_i2c_desc),
				(__u8 *)rom_desc);
		if (status)
			break;

726 727
		if ((start_address + sizeof(struct ti_i2c_desc) +
					rom_desc->Size) > TI_MAX_I2C_SIZE) {
L
Linus Torvalds 已提交
728
			status = -ENODEV;
729
			dev_dbg(dev, "%s - structure too big, erroring out.\n", __func__);
L
Linus Torvalds 已提交
730 731 732
			break;
		}

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

735
		/* Skip type 2 record */
L
Linus Torvalds 已提交
736
		ttype = rom_desc->Type & 0x0f;
737 738 739 740 741 742
		if (ttype != I2C_DESC_TYPE_FIRMWARE_BASIC
			&& ttype != I2C_DESC_TYPE_FIRMWARE_AUTO) {
			/* Read the descriptor data */
			status = read_rom(serial, start_address +
						sizeof(struct ti_i2c_desc),
						rom_desc->Size, buffer);
L
Linus Torvalds 已提交
743 744 745
			if (status)
				break;

746
			status = valid_csum(rom_desc, buffer);
L
Linus Torvalds 已提交
747 748 749
			if (status)
				break;
		}
750 751
		start_address = start_address + sizeof(struct ti_i2c_desc) +
								rom_desc->Size;
L
Linus Torvalds 已提交
752

753 754
	} while ((rom_desc->Type != I2C_DESC_TYPE_ION) &&
				(start_address < TI_MAX_I2C_SIZE));
L
Linus Torvalds 已提交
755

756 757
	if ((rom_desc->Type != I2C_DESC_TYPE_ION) ||
				(start_address > TI_MAX_I2C_SIZE))
L
Linus Torvalds 已提交
758 759
		status = -ENODEV;

760 761 762
out:
	kfree(buffer);
	kfree(rom_desc);
L
Linus Torvalds 已提交
763 764 765
	return status;
}

766
static int get_manuf_info(struct edgeport_serial *serial, __u8 *buffer)
L
Linus Torvalds 已提交
767 768 769 770 771
{
	int status;
	int start_address;
	struct ti_i2c_desc *rom_desc;
	struct edge_ti_manuf_descriptor *desc;
772
	struct device *dev = &serial->serial->dev->dev;
L
Linus Torvalds 已提交
773

774
	rom_desc = kmalloc(sizeof(*rom_desc), GFP_KERNEL);
L
Linus Torvalds 已提交
775
	if (!rom_desc) {
776
		dev_err(dev, "%s - out of memory\n", __func__);
L
Linus Torvalds 已提交
777 778
		return -ENOMEM;
	}
779 780
	start_address = get_descriptor_addr(serial, I2C_DESC_TYPE_ION,
								rom_desc);
L
Linus Torvalds 已提交
781 782

	if (!start_address) {
783
		dev_dbg(dev, "%s - Edge Descriptor not found in I2C\n", __func__);
L
Linus Torvalds 已提交
784 785 786 787
		status = -ENODEV;
		goto exit;
	}

788 789 790
	/* Read the descriptor data */
	status = read_rom(serial, start_address+sizeof(struct ti_i2c_desc),
						rom_desc->Size, buffer);
L
Linus Torvalds 已提交
791 792
	if (status)
		goto exit;
793 794 795

	status = valid_csum(rom_desc, buffer);

L
Linus Torvalds 已提交
796
	desc = (struct edge_ti_manuf_descriptor *)buffer;
797 798 799 800 801 802
	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 已提交
803 804

exit:
805
	kfree(rom_desc);
L
Linus Torvalds 已提交
806 807 808 809
	return status;
}

/* Build firmware header used for firmware update */
810
static int build_i2c_fw_hdr(__u8 *header, struct device *dev)
L
Linus Torvalds 已提交
811 812 813 814
{
	__u8 *buffer;
	int buffer_size;
	int i;
815
	int err;
L
Linus Torvalds 已提交
816 817 818 819
	__u8 cs = 0;
	struct ti_i2c_desc *i2c_header;
	struct ti_i2c_image_header *img_header;
	struct ti_i2c_firmware_rec *firmware_rec;
820 821
	const struct firmware *fw;
	const char *fw_name = "edgeport/down3.bin";
L
Linus Torvalds 已提交
822

823 824 825 826 827 828 829 830 831 832 833 834 835 836 837
	/* In order to update the I2C firmware we must change the type 2 record
	 * to type 0xF2.  This will force the UMP to come up in Boot Mode.
	 * Then while in boot mode, the driver will download the latest
	 * firmware (padded to 15.5k) into the UMP ram.  And finally when the
	 * device comes back up in download mode the driver will cause the new
	 * firmware to be copied from the UMP Ram to I2C and the firmware will
	 * update the record type from 0xf2 to 0x02.
	 */

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

	buffer = kmalloc(buffer_size, GFP_KERNEL);
L
Linus Torvalds 已提交
838
	if (!buffer) {
839
		dev_err(dev, "%s - out of memory\n", __func__);
L
Linus Torvalds 已提交
840 841
		return -ENOMEM;
	}
842

L
Linus Torvalds 已提交
843
	// Set entire image of 0xffs
844
	memset(buffer, 0xff, buffer_size);
L
Linus Torvalds 已提交
845

846 847
	err = request_firmware(&fw, fw_name, dev);
	if (err) {
848 849
		dev_err(dev, "Failed to load image \"%s\" err %d\n",
			fw_name, err);
850 851 852 853 854 855 856 857 858
		kfree(buffer);
		return err;
	}

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

859
	/* Copy version number into firmware record */
L
Linus Torvalds 已提交
860 861
	firmware_rec = (struct ti_i2c_firmware_rec *)buffer;

862 863
	firmware_rec->Ver_Major	= OperationalMajorVersion;
	firmware_rec->Ver_Minor	= OperationalMinorVersion;
L
Linus Torvalds 已提交
864

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

868
	memcpy(buffer + sizeof(struct ti_i2c_firmware_rec),
869
		&fw->data[4 + sizeof(struct ti_i2c_image_header)],
L
Linus Torvalds 已提交
870 871
		le16_to_cpu(img_header->Length));

872 873
	release_firmware(fw);

L
Linus Torvalds 已提交
874 875 876 877
	for (i=0; i < buffer_size; i++) {
		cs = (__u8)(cs + buffer[i]);
	}

878
	kfree(buffer);
L
Linus Torvalds 已提交
879

880
	/* Build new header */
L
Linus Torvalds 已提交
881 882
	i2c_header =  (struct ti_i2c_desc *)header;
	firmware_rec =  (struct ti_i2c_firmware_rec*)i2c_header->Data;
883

L
Linus Torvalds 已提交
884 885 886
	i2c_header->Type	= I2C_DESC_TYPE_FIRMWARE_BLANK;
	i2c_header->Size	= (__u16)buffer_size;
	i2c_header->CheckSum	= cs;
887 888
	firmware_rec->Ver_Major	= OperationalMajorVersion;
	firmware_rec->Ver_Minor	= OperationalMinorVersion;
L
Linus Torvalds 已提交
889 890 891 892 893

	return 0;
}

/* Try to figure out what type of I2c we have */
894
static int i2c_type_bootmode(struct edgeport_serial *serial)
L
Linus Torvalds 已提交
895
{
896
	struct device *dev = &serial->serial->dev->dev;
L
Linus Torvalds 已提交
897
	int status;
898 899 900 901
	u8 *data;

	data = kmalloc(1, GFP_KERNEL);
	if (!data) {
902
		dev_err(dev, "%s - out of memory\n", __func__);
903 904
		return -ENOMEM;
	}
905 906 907

	/* Try to read type 2 */
	status = ti_vread_sync(serial->serial->dev, UMPC_MEMORY_READ,
908
				DTK_ADDR_SPACE_I2C_TYPE_II, 0, data, 0x01);
L
Linus Torvalds 已提交
909
	if (status)
910
		dev_dbg(dev, "%s - read 2 status error = %d\n", __func__, status);
L
Linus Torvalds 已提交
911
	else
912
		dev_dbg(dev, "%s - read 2 data = 0x%x\n", __func__, *data);
913
	if ((!status) && (*data == UMP5152 || *data == UMP3410)) {
914
		dev_dbg(dev, "%s - ROM_TYPE_II\n", __func__);
L
Linus Torvalds 已提交
915
		serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
916
		goto out;
L
Linus Torvalds 已提交
917 918
	}

919 920
	/* Try to read type 3 */
	status = ti_vread_sync(serial->serial->dev, UMPC_MEMORY_READ,
921
				DTK_ADDR_SPACE_I2C_TYPE_III, 0,	data, 0x01);
L
Linus Torvalds 已提交
922
	if (status)
923
		dev_dbg(dev, "%s - read 3 status error = %d\n", __func__, status);
L
Linus Torvalds 已提交
924
	else
925
		dev_dbg(dev, "%s - read 2 data = 0x%x\n", __func__, *data);
926
	if ((!status) && (*data == UMP5152 || *data == UMP3410)) {
927
		dev_dbg(dev, "%s - ROM_TYPE_III\n", __func__);
L
Linus Torvalds 已提交
928
		serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_III;
929
		goto out;
L
Linus Torvalds 已提交
930 931
	}

932
	dev_dbg(dev, "%s - Unknown\n", __func__);
L
Linus Torvalds 已提交
933
	serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
934 935 936 937
	status = -ENODEV;
out:
	kfree(data);
	return status;
L
Linus Torvalds 已提交
938 939
}

940 941
static int bulk_xfer(struct usb_serial *serial, void *buffer,
						int length, int *num_sent)
L
Linus Torvalds 已提交
942 943 944
{
	int status;

945 946 947 948
	status = usb_bulk_msg(serial->dev,
			usb_sndbulkpipe(serial->dev,
				serial->port[0]->bulk_out_endpointAddress),
			buffer, length, num_sent, 1000);
L
Linus Torvalds 已提交
949 950 951 952
	return status;
}

/* Download given firmware image to the device (IN BOOT MODE) */
953 954
static int download_code(struct edgeport_serial *serial, __u8 *image,
							int image_length)
L
Linus Torvalds 已提交
955 956 957 958 959 960
{
	int status = 0;
	int pos;
	int transfer;
	int done;

961
	/* Transfer firmware image */
L
Linus Torvalds 已提交
962
	for (pos = 0; pos < image_length; ) {
963
		/* Read the next buffer from file */
L
Linus Torvalds 已提交
964 965 966 967
		transfer = image_length - pos;
		if (transfer > EDGE_FW_BULK_MAX_PACKET_SIZE)
			transfer = EDGE_FW_BULK_MAX_PACKET_SIZE;

968 969 970
		/* Transfer data */
		status = bulk_xfer(serial->serial, &image[pos],
							transfer, &done);
L
Linus Torvalds 已提交
971 972
		if (status)
			break;
973
		/* Advance buffer pointer */
L
Linus Torvalds 已提交
974 975 976 977 978 979
		pos += done;
	}

	return status;
}

980 981
/* FIXME!!! */
static int config_boot_dev(struct usb_device *dev)
L
Linus Torvalds 已提交
982 983 984 985
{
	return 0;
}

986 987 988 989 990
static int ti_cpu_rev(struct edge_ti_manuf_descriptor *desc)
{
	return TI_GET_CPU_REVISION(desc->CpuRev_BoardRev);
}

L
Linus Torvalds 已提交
991 992
/**
 * DownloadTIFirmware - Download run-time operating firmware to the TI5052
993
 *
L
Linus Torvalds 已提交
994 995 996
 * This routine downloads the main operating code into the TI5052, using the
 * boot code already burned into E2PROM or ROM.
 */
997
static int download_fw(struct edgeport_serial *serial)
L
Linus Torvalds 已提交
998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015
{
	struct device *dev = &serial->serial->dev->dev;
	int status = 0;
	int start_address;
	struct edge_ti_manuf_descriptor *ti_manuf_desc;
	struct usb_interface_descriptor *interface;
	int download_cur_ver;
	int download_new_ver;

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

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

1016
	status = choose_config(serial->serial->dev);
L
Linus Torvalds 已提交
1017 1018 1019 1020 1021
	if (status)
		return status;

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

1026 1027 1028 1029 1030
	/*
	 * 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 已提交
1031 1032 1033
	if (interface->bNumEndpoints > 1)
		serial->product_info.TiMode = TI_MODE_DOWNLOAD;
	else
1034
		/* Otherwise we will remain in configuring mode */
L
Linus Torvalds 已提交
1035 1036 1037 1038 1039 1040 1041 1042
		serial->product_info.TiMode = TI_MODE_CONFIGURING;

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

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

1045
		status = check_i2c_image(serial);
L
Linus Torvalds 已提交
1046
		if (status) {
1047
			dev_dbg(dev, "%s - DOWNLOAD MODE -- BAD I2C\n", __func__);
L
Linus Torvalds 已提交
1048 1049
			return status;
		}
1050

L
Linus Torvalds 已提交
1051 1052 1053
		/* Validate Hardware version number
		 * Read Manufacturing Descriptor from TI Based Edgeport
		 */
1054
		ti_manuf_desc = kmalloc(sizeof(*ti_manuf_desc), GFP_KERNEL);
L
Linus Torvalds 已提交
1055
		if (!ti_manuf_desc) {
1056
			dev_err(dev, "%s - out of memory.\n", __func__);
L
Linus Torvalds 已提交
1057 1058
			return -ENOMEM;
		}
1059
		status = get_manuf_info(serial, (__u8 *)ti_manuf_desc);
L
Linus Torvalds 已提交
1060
		if (status) {
1061
			kfree(ti_manuf_desc);
L
Linus Torvalds 已提交
1062 1063 1064
			return status;
		}

1065 1066
		/* Check version number of ION descriptor */
		if (!ignore_cpu_rev && ti_cpu_rev(ti_manuf_desc) < 2) {
1067
			dev_dbg(dev, "%s - Wrong CPU Rev %d (Must be 2)\n",
1068 1069 1070 1071
				__func__, ti_cpu_rev(ti_manuf_desc));
			kfree(ti_manuf_desc);
			return -EINVAL;
  		}
L
Linus Torvalds 已提交
1072

1073
		rom_desc = kmalloc(sizeof(*rom_desc), GFP_KERNEL);
L
Linus Torvalds 已提交
1074
		if (!rom_desc) {
1075 1076
			dev_err(dev, "%s - out of memory.\n", __func__);
			kfree(ti_manuf_desc);
L
Linus Torvalds 已提交
1077 1078 1079
			return -ENOMEM;
		}

1080 1081 1082 1083
		/* 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 已提交
1084
			struct ti_i2c_firmware_rec *firmware_version;
1085
			u8 *record;
L
Linus Torvalds 已提交
1086

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

1089 1090
			firmware_version = kmalloc(sizeof(*firmware_version),
								GFP_KERNEL);
L
Linus Torvalds 已提交
1091
			if (!firmware_version) {
1092 1093 1094
				dev_err(dev, "%s - out of memory.\n", __func__);
				kfree(rom_desc);
				kfree(ti_manuf_desc);
L
Linus Torvalds 已提交
1095 1096 1097
				return -ENOMEM;
			}

1098 1099 1100 1101 1102
			/* Validate version number
			 * Read the descriptor data
			 */
			status = read_rom(serial, start_address +
					sizeof(struct ti_i2c_desc),
L
Linus Torvalds 已提交
1103 1104 1105
					sizeof(struct ti_i2c_firmware_rec),
					(__u8 *)firmware_version);
			if (status) {
1106 1107 1108
				kfree(firmware_version);
				kfree(rom_desc);
				kfree(ti_manuf_desc);
L
Linus Torvalds 已提交
1109 1110 1111
				return status;
			}

1112 1113 1114
			/* Check version number of download with current
			   version in I2c */
			download_cur_ver = (firmware_version->Ver_Major << 8) +
L
Linus Torvalds 已提交
1115
					   (firmware_version->Ver_Minor);
1116 1117
			download_new_ver = (OperationalMajorVersion << 8) +
					   (OperationalMinorVersion);
L
Linus Torvalds 已提交
1118

1119 1120 1121 1122 1123
			dev_dbg(dev, "%s - >> FW Versions Device %d.%d  Driver %d.%d\n",
				__func__, firmware_version->Ver_Major,
				firmware_version->Ver_Minor,
				OperationalMajorVersion,
				OperationalMinorVersion);
L
Linus Torvalds 已提交
1124

1125 1126
			/* Check if we have an old version in the I2C and
			   update if necessary */
1127
			if (download_cur_ver < download_new_ver) {
1128 1129 1130 1131 1132 1133
				dev_dbg(dev, "%s - Update I2C dld from %d.%d to %d.%d\n",
					__func__,
					firmware_version->Ver_Major,
					firmware_version->Ver_Minor,
					OperationalMajorVersion,
					OperationalMinorVersion);
1134

1135 1136 1137 1138 1139 1140 1141 1142 1143
				record = kmalloc(1, GFP_KERNEL);
				if (!record) {
					dev_err(dev, "%s - out of memory.\n",
							__func__);
					kfree(firmware_version);
					kfree(rom_desc);
					kfree(ti_manuf_desc);
					return -ENOMEM;
				}
1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155
				/* 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.
				 */
1156
				*record = I2C_DESC_TYPE_FIRMWARE_BLANK;
L
Linus Torvalds 已提交
1157

1158 1159 1160
				/* Change the I2C Firmware record type to
				   0xf2 to trigger an update */
				status = write_rom(serial, start_address,
1161
						sizeof(*record), record);
L
Linus Torvalds 已提交
1162
				if (status) {
1163
					kfree(record);
1164 1165 1166
					kfree(firmware_version);
					kfree(rom_desc);
					kfree(ti_manuf_desc);
L
Linus Torvalds 已提交
1167 1168 1169
					return status;
				}

1170 1171 1172 1173 1174
				/* 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 已提交
1175
							start_address,
1176 1177
							sizeof(*record),
							record);
L
Linus Torvalds 已提交
1178
				if (status) {
1179
					kfree(record);
1180 1181 1182
					kfree(firmware_version);
					kfree(rom_desc);
					kfree(ti_manuf_desc);
L
Linus Torvalds 已提交
1183 1184 1185
					return status;
				}

1186
				if (*record != I2C_DESC_TYPE_FIRMWARE_BLANK) {
1187
					dev_err(dev, "%s - error resetting device\n", __func__);
1188
					kfree(record);
1189 1190 1191
					kfree(firmware_version);
					kfree(rom_desc);
					kfree(ti_manuf_desc);
L
Linus Torvalds 已提交
1192 1193 1194
					return -ENODEV;
				}

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

1197 1198 1199 1200
				/* Reset UMP -- Back to BOOT MODE */
				status = ti_vsend_sync(serial->serial->dev,
						UMPC_HARDWARE_RESET,
						0, 0, NULL, 0);
L
Linus Torvalds 已提交
1201

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

				/* return an error on purpose. */
1205
				kfree(record);
1206 1207 1208
				kfree(firmware_version);
				kfree(rom_desc);
				kfree(ti_manuf_desc);
L
Linus Torvalds 已提交
1209 1210
				return -ENODEV;
			}
1211
			kfree(firmware_version);
L
Linus Torvalds 已提交
1212
		}
1213 1214 1215 1216
		/* 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 已提交
1217 1218 1219
			__u8 *header;
			__u8 *vheader;

1220
			header = kmalloc(HEADER_SIZE, GFP_KERNEL);
L
Linus Torvalds 已提交
1221
			if (!header) {
1222 1223 1224
				dev_err(dev, "%s - out of memory.\n", __func__);
				kfree(rom_desc);
				kfree(ti_manuf_desc);
L
Linus Torvalds 已提交
1225 1226
				return -ENOMEM;
			}
1227 1228

			vheader = kmalloc(HEADER_SIZE, GFP_KERNEL);
L
Linus Torvalds 已提交
1229
			if (!vheader) {
1230 1231 1232 1233
				dev_err(dev, "%s - out of memory.\n", __func__);
				kfree(header);
				kfree(rom_desc);
				kfree(ti_manuf_desc);
L
Linus Torvalds 已提交
1234 1235
				return -ENOMEM;
			}
1236

1237
			dev_dbg(dev, "%s - Found Type BLANK FIRMWARE (Type F2) record\n", __func__);
1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250

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

1259 1260 1261
			/* Update I2C with type 0xf2 record with correct
			   size and checksum */
			status = write_rom(serial,
L
Linus Torvalds 已提交
1262 1263 1264 1265
						start_address,
						HEADER_SIZE,
						header);
			if (status) {
1266 1267 1268 1269
				kfree(vheader);
				kfree(header);
				kfree(rom_desc);
				kfree(ti_manuf_desc);
1270
				return -EINVAL;
L
Linus Torvalds 已提交
1271 1272
			}

1273 1274 1275 1276
			/* 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 已提交
1277 1278

			if (status) {
1279
				dev_dbg(dev, "%s - can't read header back\n", __func__);
1280 1281 1282 1283
				kfree(vheader);
				kfree(header);
				kfree(rom_desc);
				kfree(ti_manuf_desc);
L
Linus Torvalds 已提交
1284 1285 1286
				return status;
			}
			if (memcmp(vheader, header, HEADER_SIZE)) {
1287
				dev_dbg(dev, "%s - write download record failed\n", __func__);
1288 1289 1290 1291
				kfree(vheader);
				kfree(header);
				kfree(rom_desc);
				kfree(ti_manuf_desc);
1292
				return -EINVAL;
L
Linus Torvalds 已提交
1293 1294
			}

1295 1296
			kfree(vheader);
			kfree(header);
L
Linus Torvalds 已提交
1297

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

1300 1301 1302
			/* Tell firmware to copy download image into I2C */
			status = ti_vsend_sync(serial->serial->dev,
					UMPC_COPY_DNLD_TO_I2C, 0, 0, NULL, 0);
L
Linus Torvalds 已提交
1303

1304
		  	dev_dbg(dev, "%s - Update complete 0x%x\n", __func__, status);
L
Linus Torvalds 已提交
1305
			if (status) {
1306 1307 1308 1309 1310
				dev_err(dev,
					"%s - UMPC_COPY_DNLD_TO_I2C failed\n",
								__func__);
				kfree(rom_desc);
				kfree(ti_manuf_desc);
L
Linus Torvalds 已提交
1311 1312 1313 1314 1315
				return status;
			}
		}

		// The device is running the download code
1316 1317
		kfree(rom_desc);
		kfree(ti_manuf_desc);
L
Linus Torvalds 已提交
1318 1319 1320 1321 1322 1323
		return 0;
	}

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

1326 1327
	/* Configure the TI device so we can use the BULK pipes for download */
	status = config_boot_dev(serial->serial->dev);
L
Linus Torvalds 已提交
1328 1329 1330
	if (status)
		return status;

1331 1332
	if (le16_to_cpu(serial->serial->dev->descriptor.idVendor)
							!= USB_VENDOR_ID_ION) {
1333 1334
		dev_dbg(dev, "%s - VID = 0x%x\n", __func__,
			le16_to_cpu(serial->serial->dev->descriptor.idVendor));
L
Linus Torvalds 已提交
1335
		serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
1336
		goto stayinbootmode;
L
Linus Torvalds 已提交
1337 1338
	}

1339 1340 1341 1342
	/* We have an ION device (I2c Must be programmed)
	   Determine I2C image type */
	if (i2c_type_bootmode(serial))
		goto stayinbootmode;
L
Linus Torvalds 已提交
1343

1344 1345
	/* Check for ION Vendor ID and that the I2C is valid */
	if (!check_i2c_image(serial)) {
L
Linus Torvalds 已提交
1346 1347 1348 1349 1350
		struct ti_i2c_image_header *header;
		int i;
		__u8 cs = 0;
		__u8 *buffer;
		int buffer_size;
1351 1352 1353
		int err;
		const struct firmware *fw;
		const char *fw_name = "edgeport/down3.bin";
L
Linus Torvalds 已提交
1354 1355 1356 1357

		/* Validate Hardware version number
		 * Read Manufacturing Descriptor from TI Based Edgeport
		 */
1358
		ti_manuf_desc = kmalloc(sizeof(*ti_manuf_desc), GFP_KERNEL);
L
Linus Torvalds 已提交
1359
		if (!ti_manuf_desc) {
1360
			dev_err(dev, "%s - out of memory.\n", __func__);
L
Linus Torvalds 已提交
1361 1362
			return -ENOMEM;
		}
1363
		status = get_manuf_info(serial, (__u8 *)ti_manuf_desc);
L
Linus Torvalds 已提交
1364
		if (status) {
1365 1366
			kfree(ti_manuf_desc);
			goto stayinbootmode;
L
Linus Torvalds 已提交
1367 1368
		}

1369 1370
		/* Check for version 2 */
		if (!ignore_cpu_rev && ti_cpu_rev(ti_manuf_desc) < 2) {
1371 1372
			dev_dbg(dev, "%s - Wrong CPU Rev %d (Must be 2)\n",
				__func__, ti_cpu_rev(ti_manuf_desc));
1373 1374
			kfree(ti_manuf_desc);
			goto stayinbootmode;
L
Linus Torvalds 已提交
1375 1376
		}

1377
		kfree(ti_manuf_desc);
L
Linus Torvalds 已提交
1378 1379

		/*
1380 1381 1382 1383 1384 1385 1386 1387 1388
		 * 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 已提交
1389 1390 1391 1392
		 * Do we really have to copy the whole firmware image,
		 * or could we do this in place!
		 */

1393 1394 1395 1396
		/* Allocate a 15.5k buffer + 3 byte header */
		buffer_size = (((1024 * 16) - 512) +
					sizeof(struct ti_i2c_image_header));
		buffer = kmalloc(buffer_size, GFP_KERNEL);
L
Linus Torvalds 已提交
1397
		if (!buffer) {
1398
			dev_err(dev, "%s - out of memory\n", __func__);
L
Linus Torvalds 已提交
1399 1400
			return -ENOMEM;
		}
1401 1402 1403

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

1405 1406
		err = request_firmware(&fw, fw_name, dev);
		if (err) {
1407 1408
			dev_err(dev, "Failed to load image \"%s\" err %d\n",
				fw_name, err);
1409 1410 1411 1412 1413
			kfree(buffer);
			return err;
		}
		memcpy(buffer, &fw->data[4], fw->size - 4);
		release_firmware(fw);
L
Linus Torvalds 已提交
1414

1415 1416
		for (i = sizeof(struct ti_i2c_image_header);
				i < buffer_size; i++) {
L
Linus Torvalds 已提交
1417 1418
			cs = (__u8)(cs + buffer[i]);
		}
1419

L
Linus Torvalds 已提交
1420
		header = (struct ti_i2c_image_header *)buffer;
1421 1422 1423 1424

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

1427
		/* Download the operational code  */
1428
		dev_dbg(dev, "%s - Downloading operational code image (TI UMP)\n", __func__);
1429
		status = download_code(serial, buffer, buffer_size);
L
Linus Torvalds 已提交
1430

1431
		kfree(buffer);
L
Linus Torvalds 已提交
1432 1433

		if (status) {
1434
			dev_dbg(dev, "%s - Error downloading operational code image\n", __func__);
L
Linus Torvalds 已提交
1435 1436 1437
			return status;
		}

1438
		/* Device will reboot */
L
Linus Torvalds 已提交
1439 1440
		serial->product_info.TiMode = TI_MODE_TRANSITIONING;

1441
		dev_dbg(dev, "%s - Download successful -- Device rebooting...\n", __func__);
L
Linus Torvalds 已提交
1442 1443 1444 1445 1446

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

1447 1448
stayinbootmode:
	/* Eprom is invalid or blank stay in boot mode */
1449
	dev_dbg(dev, "%s - STAYING IN BOOT MODE\n", __func__);
L
Linus Torvalds 已提交
1450 1451 1452 1453 1454 1455
	serial->product_info.TiMode = TI_MODE_BOOT;

	return 0;
}


1456
static int ti_do_config(struct edgeport_port *port, int feature, int on)
L
Linus Torvalds 已提交
1457 1458
{
	int port_number = port->port->number - port->port->serial->minor;
1459 1460 1461 1462
	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 已提交
1463 1464 1465
}


1466
static int restore_mcr(struct edgeport_port *port, __u8 mcr)
L
Linus Torvalds 已提交
1467 1468 1469
{
	int status = 0;

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

1472
	status = ti_do_config(port, UMPC_SET_CLR_DTR, mcr & MCR_DTR);
L
Linus Torvalds 已提交
1473 1474
	if (status)
		return status;
1475
	status = ti_do_config(port, UMPC_SET_CLR_RTS, mcr & MCR_RTS);
L
Linus Torvalds 已提交
1476 1477
	if (status)
		return status;
1478
	return ti_do_config(port, UMPC_SET_CLR_LOOPBACK, mcr & MCR_LOOPBACK);
L
Linus Torvalds 已提交
1479 1480 1481
}

/* Convert TI LSR to standard UART flags */
1482
static __u8 map_line_status(__u8 ti_lsr)
L
Linus Torvalds 已提交
1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493
{
	__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 */
1494 1495
	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 已提交
1496 1497 1498 1499 1500 1501

#undef MAP_FLAG

	return lsr;
}

1502
static void handle_new_msr(struct edgeport_port *edge_port, __u8 msr)
L
Linus Torvalds 已提交
1503 1504 1505 1506
{
	struct async_icount *icount;
	struct tty_struct *tty;

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

1509 1510
	if (msr & (EDGEPORT_MSR_DELTA_CTS | EDGEPORT_MSR_DELTA_DSR |
			EDGEPORT_MSR_DELTA_RI | EDGEPORT_MSR_DELTA_CD)) {
L
Linus Torvalds 已提交
1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521
		icount = &edge_port->icount;

		/* update input line counters */
		if (msr & EDGEPORT_MSR_DELTA_CTS)
			icount->cts++;
		if (msr & EDGEPORT_MSR_DELTA_DSR)
			icount->dsr++;
		if (msr & EDGEPORT_MSR_DELTA_CD)
			icount->dcd++;
		if (msr & EDGEPORT_MSR_DELTA_RI)
			icount->rng++;
1522
		wake_up_interruptible(&edge_port->delta_msr_wait);
L
Linus Torvalds 已提交
1523 1524 1525 1526 1527
	}

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

A
Alan Cox 已提交
1528
	tty = tty_port_tty_get(&edge_port->port->port);
L
Linus Torvalds 已提交
1529 1530 1531 1532 1533 1534 1535 1536 1537
	/* handle CTS flow control */
	if (tty && C_CRTSCTS(tty)) {
		if (msr & EDGEPORT_MSR_CTS) {
			tty->hw_stopped = 0;
			tty_wakeup(tty);
		} else {
			tty->hw_stopped = 1;
		}
	}
A
Alan Cox 已提交
1538
	tty_kref_put(tty);
L
Linus Torvalds 已提交
1539 1540
}

1541 1542
static void handle_new_lsr(struct edgeport_port *edge_port, int lsr_data,
							__u8 lsr, __u8 data)
L
Linus Torvalds 已提交
1543 1544
{
	struct async_icount *icount;
1545 1546
	__u8 new_lsr = (__u8)(lsr & (__u8)(LSR_OVER_ERR | LSR_PAR_ERR |
						LSR_FRM_ERR | LSR_BREAK));
A
Alan Cox 已提交
1547
	struct tty_struct *tty;
L
Linus Torvalds 已提交
1548

1549
	dev_dbg(&edge_port->port->dev, "%s - %02x\n", __func__, new_lsr);
L
Linus Torvalds 已提交
1550 1551 1552

	edge_port->shadow_lsr = lsr;

1553
	if (new_lsr & LSR_BREAK)
L
Linus Torvalds 已提交
1554 1555 1556 1557 1558 1559 1560
		/*
		 * Parity and Framing errors only count if they
		 * occur exclusive of a break being received.
		 */
		new_lsr &= (__u8)(LSR_OVER_ERR | LSR_BREAK);

	/* Place LSR data byte into Rx buffer */
A
Alan Cox 已提交
1561 1562 1563 1564 1565 1566 1567
	if (lsr_data) {
		tty = tty_port_tty_get(&edge_port->port->port);
		if (tty) {
			edge_tty_recv(&edge_port->port->dev, tty, &data, 1);
			tty_kref_put(tty);
		}
	}
L
Linus Torvalds 已提交
1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581

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


1582
static void edge_interrupt_callback(struct urb *urb)
L
Linus Torvalds 已提交
1583
{
1584
	struct edgeport_serial *edge_serial = urb->context;
L
Linus Torvalds 已提交
1585 1586
	struct usb_serial_port *port;
	struct edgeport_port *edge_port;
1587
	struct device *dev;
L
Linus Torvalds 已提交
1588 1589 1590 1591
	unsigned char *data = urb->transfer_buffer;
	int length = urb->actual_length;
	int port_number;
	int function;
1592
	int retval;
L
Linus Torvalds 已提交
1593 1594
	__u8 lsr;
	__u8 msr;
1595
	int status = urb->status;
L
Linus Torvalds 已提交
1596

1597
	switch (status) {
L
Linus Torvalds 已提交
1598 1599 1600 1601 1602 1603 1604
	case 0:
		/* success */
		break;
	case -ECONNRESET:
	case -ENOENT:
	case -ESHUTDOWN:
		/* this urb is terminated, clean up */
1605
		dev_dbg(&urb->dev->dev, "%s - urb shutting down with status: %d\n",
1606
		    __func__, status);
L
Linus Torvalds 已提交
1607 1608
		return;
	default:
1609
		dev_err(&urb->dev->dev, "%s - nonzero urb status received: "
1610
			"%d\n", __func__, status);
L
Linus Torvalds 已提交
1611 1612 1613 1614
		goto exit;
	}

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

1619
	dev = &edge_serial->serial->dev->dev;
1620
	usb_serial_debug_data(dev, __func__, length, data);
1621

L
Linus Torvalds 已提交
1622
	if (length != 2) {
1623
		dev_dbg(dev, "%s - expecting packet of size 2, got %d\n", __func__, length);
L
Linus Torvalds 已提交
1624 1625 1626
		goto exit;
	}

1627 1628
	port_number = TIUMP_GET_PORT_FROM_CODE(data[0]);
	function    = TIUMP_GET_FUNC_FROM_CODE(data[0]);
1629 1630
	dev_dbg(dev, "%s - port_number %d, function %d, info 0x%x\n", __func__,
		port_number, function, data[1]);
L
Linus Torvalds 已提交
1631 1632 1633
	port = edge_serial->serial->port[port_number];
	edge_port = usb_get_serial_port_data(port);
	if (!edge_port) {
1634
		dev_dbg(dev, "%s - edge_port not found\n", __func__);
L
Linus Torvalds 已提交
1635 1636 1637 1638
		return;
	}
	switch (function) {
	case TIUMP_INTERRUPT_CODE_LSR:
1639
		lsr = map_line_status(data[1]);
L
Linus Torvalds 已提交
1640
		if (lsr & UMP_UART_LSR_DATA_MASK) {
1641 1642
			/* Save the LSR event for bulk read
			   completion routine */
1643 1644
			dev_dbg(dev, "%s - LSR Event Port %u LSR Status = %02x\n",
				__func__, port_number, lsr);
L
Linus Torvalds 已提交
1645 1646 1647
			edge_port->lsr_event = 1;
			edge_port->lsr_mask = lsr;
		} else {
1648 1649
			dev_dbg(dev, "%s - ===== Port %d LSR Status = %02x ======\n",
				__func__, port_number, lsr);
1650
			handle_new_lsr(edge_port, 0, lsr, 0);
L
Linus Torvalds 已提交
1651 1652 1653
		}
		break;

1654
	case TIUMP_INTERRUPT_CODE_MSR:	/* MSR */
L
Linus Torvalds 已提交
1655 1656
		/* Copy MSR from UMP */
		msr = data[1];
1657 1658
		dev_dbg(dev, "%s - ===== Port %u MSR Status = %02x ======\n",
			__func__, port_number, msr);
1659
		handle_new_msr(edge_port, msr);
L
Linus Torvalds 已提交
1660 1661 1662
		break;

	default:
1663 1664 1665
		dev_err(&urb->dev->dev,
			"%s - Unknown Interrupt code from UMP %x\n",
			__func__, data[1]);
L
Linus Torvalds 已提交
1666
		break;
1667

L
Linus Torvalds 已提交
1668 1669 1670
	}

exit:
1671
	retval = usb_submit_urb(urb, GFP_ATOMIC);
1672
	if (retval)
1673 1674
		dev_err(&urb->dev->dev,
			"%s - usb_submit_urb failed with result %d\n",
1675
			 __func__, retval);
L
Linus Torvalds 已提交
1676 1677
}

1678
static void edge_bulk_in_callback(struct urb *urb)
L
Linus Torvalds 已提交
1679
{
1680
	struct edgeport_port *edge_port = urb->context;
1681
	struct device *dev = &edge_port->port->dev;
L
Linus Torvalds 已提交
1682 1683
	unsigned char *data = urb->transfer_buffer;
	struct tty_struct *tty;
1684
	int retval = 0;
L
Linus Torvalds 已提交
1685
	int port_number;
1686
	int status = urb->status;
L
Linus Torvalds 已提交
1687

1688
	switch (status) {
L
Linus Torvalds 已提交
1689 1690 1691 1692 1693 1694 1695
	case 0:
		/* success */
		break;
	case -ECONNRESET:
	case -ENOENT:
	case -ESHUTDOWN:
		/* this urb is terminated, clean up */
1696
		dev_dbg(&urb->dev->dev, "%s - urb shutting down with status: %d\n", __func__, status);
L
Linus Torvalds 已提交
1697 1698
		return;
	default:
1699
		dev_err(&urb->dev->dev, "%s - nonzero read bulk status received: %d\n", __func__, status);
L
Linus Torvalds 已提交
1700 1701
	}

1702
	if (status == -EPIPE)
L
Linus Torvalds 已提交
1703 1704
		goto exit;

1705
	if (status) {
1706
		dev_err(&urb->dev->dev, "%s - stopping read!\n", __func__);
L
Linus Torvalds 已提交
1707 1708 1709 1710 1711 1712 1713
		return;
	}

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

	if (edge_port->lsr_event) {
		edge_port->lsr_event = 0;
1714 1715
		dev_dbg(dev, "%s ===== Port %u LSR Status = %02x, Data = %02x ======\n",
			__func__, port_number, edge_port->lsr_mask, *data);
1716
		handle_new_lsr(edge_port, 1, edge_port->lsr_mask, *data);
L
Linus Torvalds 已提交
1717 1718 1719 1720 1721
		/* Adjust buffer length/pointer */
		--urb->actual_length;
		++data;
	}

A
Alan Cox 已提交
1722
	tty = tty_port_tty_get(&edge_port->port->port);
L
Linus Torvalds 已提交
1723
	if (tty && urb->actual_length) {
1724
		usb_serial_debug_data(dev, __func__, urb->actual_length, data);
1725
		if (edge_port->close_pending)
1726
			dev_dbg(dev, "%s - close pending, dropping data on the floor\n",
1727 1728
								__func__);
		else
1729
			edge_tty_recv(dev, tty, data, urb->actual_length);
L
Linus Torvalds 已提交
1730 1731
		edge_port->icount.rx += urb->actual_length;
	}
A
Alan Cox 已提交
1732
	tty_kref_put(tty);
L
Linus Torvalds 已提交
1733 1734 1735 1736

exit:
	/* continue read unless stopped */
	spin_lock(&edge_port->ep_lock);
1737
	if (edge_port->ep_read_urb_state == EDGE_READ_URB_RUNNING)
1738
		retval = usb_submit_urb(urb, GFP_ATOMIC);
1739
	else if (edge_port->ep_read_urb_state == EDGE_READ_URB_STOPPING)
L
Linus Torvalds 已提交
1740
		edge_port->ep_read_urb_state = EDGE_READ_URB_STOPPED;
1741

L
Linus Torvalds 已提交
1742
	spin_unlock(&edge_port->ep_lock);
1743
	if (retval)
1744
		dev_err(dev, "%s - usb_submit_urb failed with result %d\n", __func__, retval);
L
Linus Torvalds 已提交
1745 1746
}

1747 1748
static void edge_tty_recv(struct device *dev, struct tty_struct *tty,
					unsigned char *data, int length)
L
Linus Torvalds 已提交
1749
{
1750
	int queued;
L
Linus Torvalds 已提交
1751

1752 1753 1754 1755
	queued = tty_insert_flip_string(tty, data, length);
	if (queued < length)
		dev_err(dev, "%s - dropping data, %d bytes lost\n",
			__func__, length - queued);
L
Linus Torvalds 已提交
1756 1757 1758
	tty_flip_buffer_push(tty);
}

1759
static void edge_bulk_out_callback(struct urb *urb)
L
Linus Torvalds 已提交
1760
{
1761
	struct usb_serial_port *port = urb->context;
L
Linus Torvalds 已提交
1762
	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1763
	int status = urb->status;
A
Alan Cox 已提交
1764
	struct tty_struct *tty;
L
Linus Torvalds 已提交
1765 1766 1767

	edge_port->ep_write_urb_in_use = 0;

1768
	switch (status) {
L
Linus Torvalds 已提交
1769 1770 1771 1772 1773 1774 1775
	case 0:
		/* success */
		break;
	case -ECONNRESET:
	case -ENOENT:
	case -ESHUTDOWN:
		/* this urb is terminated, clean up */
1776
		dev_dbg(&urb->dev->dev, "%s - urb shutting down with status: %d\n",
1777
		    __func__, status);
L
Linus Torvalds 已提交
1778 1779
		return;
	default:
1780
		dev_err_console(port, "%s - nonzero write bulk status "
1781
			"received: %d\n", __func__, status);
L
Linus Torvalds 已提交
1782 1783 1784
	}

	/* send any buffered data */
A
Alan Cox 已提交
1785 1786 1787
	tty = tty_port_tty_get(&port->port);
	edge_send(tty);
	tty_kref_put(tty);
L
Linus Torvalds 已提交
1788 1789
}

1790
static int edge_open(struct tty_struct *tty, struct usb_serial_port *port)
L
Linus Torvalds 已提交
1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805
{
	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;

	port_number = port->number - port->serial->minor;
	switch (port_number) {
1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816
	case 0:
		edge_port->uart_base = UMPMEM_BASE_UART1;
		edge_port->dma_address = UMPD_OEDB1_ADDRESS;
		break;
	case 1:
		edge_port->uart_base = UMPMEM_BASE_UART2;
		edge_port->dma_address = UMPD_OEDB2_ADDRESS;
		break;
	default:
		dev_err(&port->dev, "Unknown port number!!!\n");
		return -ENODEV;
L
Linus Torvalds 已提交
1817 1818
	}

1819 1820
	dev_dbg(&port->dev, "%s - port_number = %d, uart_base = %04x, dma_address = %04x\n",
		__func__, port_number, edge_port->uart_base, edge_port->dma_address);
L
Linus Torvalds 已提交
1821 1822 1823

	dev = port->serial->dev;

1824 1825
	memset(&(edge_port->icount), 0x00, sizeof(edge_port->icount));
	init_waitqueue_head(&edge_port->delta_msr_wait);
L
Linus Torvalds 已提交
1826 1827

	/* turn off loopback */
1828
	status = ti_do_config(edge_port, UMPC_SET_CLR_LOOPBACK, 0);
L
Linus Torvalds 已提交
1829
	if (status) {
1830 1831
		dev_err(&port->dev,
				"%s - cannot send clear loopback command, %d\n",
1832
			__func__, status);
L
Linus Torvalds 已提交
1833 1834
		return status;
	}
1835

L
Linus Torvalds 已提交
1836
	/* set up the port settings */
A
Alan Cox 已提交
1837
	if (tty)
1838
		edge_set_termios(tty, port, &tty->termios);
L
Linus Torvalds 已提交
1839 1840 1841 1842 1843 1844

	/* open up the port */

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

1845 1846
	edge_port->ump_read_timeout =
				max(20, ((transaction_timeout * 3) / 2));
L
Linus Torvalds 已提交
1847

1848 1849 1850
	/* milliseconds to timeout for DMA transfer */
	open_settings = (u8)(UMP_DMA_MODE_CONTINOUS |
			     UMP_PIPE_TRANS_TIMEOUT_ENA |
L
Linus Torvalds 已提交
1851 1852
			     (transaction_timeout << 2));

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

	/* Tell TI to open and start the port */
1856 1857
	status = send_cmd(dev, UMPC_OPEN_PORT,
		(u8)(UMPM_UART1_PORT + port_number), open_settings, NULL, 0);
L
Linus Torvalds 已提交
1858
	if (status) {
1859 1860
		dev_err(&port->dev, "%s - cannot send open command, %d\n",
							__func__, status);
L
Linus Torvalds 已提交
1861 1862 1863 1864
		return status;
	}

	/* Start the DMA? */
1865 1866
	status = send_cmd(dev, UMPC_START_PORT,
		(u8)(UMPM_UART1_PORT + port_number), 0, NULL, 0);
L
Linus Torvalds 已提交
1867
	if (status) {
1868 1869
		dev_err(&port->dev, "%s - cannot send start DMA command, %d\n",
							__func__, status);
L
Linus Torvalds 已提交
1870 1871 1872 1873
		return status;
	}

	/* Clear TX and RX buffers in UMP */
1874
	status = purge_port(port, UMP_PORT_DIR_OUT | UMP_PORT_DIR_IN);
L
Linus Torvalds 已提交
1875
	if (status) {
1876 1877 1878
		dev_err(&port->dev,
			"%s - cannot send clear buffers command, %d\n",
			__func__, status);
L
Linus Torvalds 已提交
1879 1880 1881 1882
		return status;
	}

	/* Read Initial MSR */
1883 1884 1885
	status = ti_vread_sync(dev, UMPC_READ_MSR, 0,
				(__u16)(UMPM_UART1_PORT + port_number),
				&edge_port->shadow_msr, 1);
L
Linus Torvalds 已提交
1886
	if (status) {
1887 1888
		dev_err(&port->dev, "%s - cannot send read MSR command, %d\n",
							__func__, status);
L
Linus Torvalds 已提交
1889 1890 1891
		return status;
	}

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

L
Linus Torvalds 已提交
1894 1895
	/* Set Initial MCR */
	edge_port->shadow_mcr = MCR_RTS | MCR_DTR;
1896
	dev_dbg(&port->dev, "ShadowMCR 0x%X\n", edge_port->shadow_mcr);
L
Linus Torvalds 已提交
1897 1898

	edge_serial = edge_port->edge_serial;
1899
	if (mutex_lock_interruptible(&edge_serial->es_lock))
L
Linus Torvalds 已提交
1900 1901
		return -ERESTARTSYS;
	if (edge_serial->num_ports_open == 0) {
1902
		/* we are the first port to open, post the interrupt urb */
L
Linus Torvalds 已提交
1903 1904
		urb = edge_serial->serial->port[0]->interrupt_in_urb;
		if (!urb) {
1905 1906 1907
			dev_err(&port->dev,
				"%s - no interrupt urb present, exiting\n",
				__func__);
L
Linus Torvalds 已提交
1908
			status = -EINVAL;
1909
			goto release_es_lock;
L
Linus Torvalds 已提交
1910 1911
		}
		urb->context = edge_serial;
1912
		status = usb_submit_urb(urb, GFP_KERNEL);
L
Linus Torvalds 已提交
1913
		if (status) {
1914 1915 1916
			dev_err(&port->dev,
				"%s - usb_submit_urb failed with value %d\n",
					__func__, status);
1917
			goto release_es_lock;
L
Linus Torvalds 已提交
1918 1919 1920 1921 1922 1923 1924
		}
	}

	/*
	 * reset the data toggle on the bulk endpoints to work around bug in
	 * host controllers where things get out of sync some times
	 */
1925 1926
	usb_clear_halt(dev, port->write_urb->pipe);
	usb_clear_halt(dev, port->read_urb->pipe);
L
Linus Torvalds 已提交
1927 1928 1929 1930

	/* start up our bulk read urb */
	urb = port->read_urb;
	if (!urb) {
1931 1932
		dev_err(&port->dev, "%s - no read urb present, exiting\n",
								__func__);
L
Linus Torvalds 已提交
1933 1934 1935 1936 1937
		status = -EINVAL;
		goto unlink_int_urb;
	}
	edge_port->ep_read_urb_state = EDGE_READ_URB_RUNNING;
	urb->context = edge_port;
1938
	status = usb_submit_urb(urb, GFP_KERNEL);
L
Linus Torvalds 已提交
1939
	if (status) {
1940 1941 1942
		dev_err(&port->dev,
			"%s - read bulk usb_submit_urb failed with value %d\n",
				__func__, status);
L
Linus Torvalds 已提交
1943 1944 1945 1946 1947
		goto unlink_int_urb;
	}

	++edge_serial->num_ports_open;

1948
	goto release_es_lock;
L
Linus Torvalds 已提交
1949 1950 1951 1952

unlink_int_urb:
	if (edge_port->edge_serial->num_ports_open == 0)
		usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
1953 1954
release_es_lock:
	mutex_unlock(&edge_serial->es_lock);
L
Linus Torvalds 已提交
1955 1956 1957
	return status;
}

1958
static void edge_close(struct usb_serial_port *port)
L
Linus Torvalds 已提交
1959 1960 1961
{
	struct edgeport_serial *edge_serial;
	struct edgeport_port *edge_port;
1962
	struct usb_serial *serial = port->serial;
L
Linus Torvalds 已提交
1963 1964 1965 1966
	int port_number;

	edge_serial = usb_get_serial_data(port->serial);
	edge_port = usb_get_serial_port_data(port);
1967
	if (edge_serial == NULL || edge_port == NULL)
L
Linus Torvalds 已提交
1968
		return;
1969 1970

	/* The bulkreadcompletion routine will check
L
Linus Torvalds 已提交
1971 1972 1973 1974
	 * this flag and dump add read data */
	edge_port->close_pending = 1;

	/* chase the port close and flush */
1975
	chase_port(edge_port, (HZ * closing_wait) / 100, 1);
L
Linus Torvalds 已提交
1976 1977 1978 1979 1980 1981 1982

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

	/* assuming we can still talk to the device,
	 * send a close port command to it */
1983
	dev_dbg(&port->dev, "%s - send umpc_close_port\n", __func__);
L
Linus Torvalds 已提交
1984
	port_number = port->number - port->serial->minor;
1985 1986 1987 1988

	mutex_lock(&serial->disc_mutex);
	if (!serial->disconnected) {
		send_cmd(serial->dev,
L
Linus Torvalds 已提交
1989 1990 1991 1992 1993
				     UMPC_CLOSE_PORT,
				     (__u8)(UMPM_UART1_PORT + port_number),
				     0,
				     NULL,
				     0);
1994 1995 1996
	}
	mutex_unlock(&serial->disc_mutex);

1997
	mutex_lock(&edge_serial->es_lock);
L
Linus Torvalds 已提交
1998 1999 2000 2001 2002 2003
	--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;
	}
2004
	mutex_unlock(&edge_serial->es_lock);
L
Linus Torvalds 已提交
2005 2006 2007
	edge_port->close_pending = 0;
}

A
Alan Cox 已提交
2008 2009
static int edge_write(struct tty_struct *tty, struct usb_serial_port *port,
				const unsigned char *data, int count)
L
Linus Torvalds 已提交
2010 2011 2012 2013
{
	struct edgeport_port *edge_port = usb_get_serial_port_data(port);

	if (count == 0) {
2014
		dev_dbg(&port->dev, "%s - write request of 0 bytes\n", __func__);
L
Linus Torvalds 已提交
2015 2016 2017 2018 2019 2020 2021 2022
		return 0;
	}

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

2023 2024
	count = kfifo_in_locked(&edge_port->write_fifo, data, count,
							&edge_port->ep_lock);
A
Alan Cox 已提交
2025
	edge_send(tty);
L
Linus Torvalds 已提交
2026 2027 2028 2029

	return count;
}

A
Alan Cox 已提交
2030
static void edge_send(struct tty_struct *tty)
L
Linus Torvalds 已提交
2031
{
A
Alan Cox 已提交
2032
	struct usb_serial_port *port = tty->driver_data;
L
Linus Torvalds 已提交
2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043
	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;
	}

2044
	count = kfifo_out(&edge_port->write_fifo,
L
Linus Torvalds 已提交
2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056
				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);

2057
	usb_serial_debug_data(&port->dev, __func__, count, port->write_urb->transfer_buffer);
L
Linus Torvalds 已提交
2058 2059

	/* set up our urb */
2060
	port->write_urb->transfer_buffer_length = count;
L
Linus Torvalds 已提交
2061 2062 2063 2064

	/* send the data out the bulk port */
	result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
	if (result) {
2065
		dev_err_console(port,
2066 2067
			"%s - failed submitting write urb, error %d\n",
				__func__, result);
L
Linus Torvalds 已提交
2068
		edge_port->ep_write_urb_in_use = 0;
2069 2070
		/* TODO: reschedule edge_send */
	} else
L
Linus Torvalds 已提交
2071 2072 2073 2074
		edge_port->icount.tx += count;

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

A
Alan Cox 已提交
2079
static int edge_write_room(struct tty_struct *tty)
L
Linus Torvalds 已提交
2080
{
A
Alan Cox 已提交
2081
	struct usb_serial_port *port = tty->driver_data;
L
Linus Torvalds 已提交
2082 2083 2084 2085 2086
	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
	int room = 0;
	unsigned long flags;

	if (edge_port == NULL)
2087
		return 0;
L
Linus Torvalds 已提交
2088
	if (edge_port->close_pending == 1)
2089
		return 0;
L
Linus Torvalds 已提交
2090 2091

	spin_lock_irqsave(&edge_port->ep_lock, flags);
2092
	room = kfifo_avail(&edge_port->write_fifo);
L
Linus Torvalds 已提交
2093 2094
	spin_unlock_irqrestore(&edge_port->ep_lock, flags);

2095
	dev_dbg(&port->dev, "%s - returns %d\n", __func__, room);
L
Linus Torvalds 已提交
2096 2097 2098
	return room;
}

A
Alan Cox 已提交
2099
static int edge_chars_in_buffer(struct tty_struct *tty)
L
Linus Torvalds 已提交
2100
{
A
Alan Cox 已提交
2101
	struct usb_serial_port *port = tty->driver_data;
L
Linus Torvalds 已提交
2102 2103 2104 2105 2106
	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
	int chars = 0;
	unsigned long flags;

	if (edge_port == NULL)
2107
		return 0;
L
Linus Torvalds 已提交
2108
	if (edge_port->close_pending == 1)
2109
		return 0;
L
Linus Torvalds 已提交
2110 2111

	spin_lock_irqsave(&edge_port->ep_lock, flags);
2112
	chars = kfifo_len(&edge_port->write_fifo);
L
Linus Torvalds 已提交
2113 2114
	spin_unlock_irqrestore(&edge_port->ep_lock, flags);

2115
	dev_dbg(&port->dev, "%s - returns %d\n", __func__, chars);
L
Linus Torvalds 已提交
2116 2117 2118
	return chars;
}

A
Alan Cox 已提交
2119
static void edge_throttle(struct tty_struct *tty)
L
Linus Torvalds 已提交
2120
{
A
Alan Cox 已提交
2121
	struct usb_serial_port *port = tty->driver_data;
L
Linus Torvalds 已提交
2122 2123 2124 2125 2126 2127 2128 2129 2130
	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 已提交
2131 2132 2133 2134
		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 已提交
2135 2136 2137 2138 2139 2140 2141 2142 2143
	}

	/* 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 已提交
2144
static void edge_unthrottle(struct tty_struct *tty)
L
Linus Torvalds 已提交
2145
{
A
Alan Cox 已提交
2146
	struct usb_serial_port *port = tty->driver_data;
L
Linus Torvalds 已提交
2147 2148 2149 2150 2151 2152 2153 2154 2155
	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 已提交
2156 2157 2158 2159
		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 已提交
2160 2161 2162 2163 2164 2165
	}
	/* 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)
2166 2167 2168
			dev_err(&port->dev,
				"%s - read bulk usb_submit_urb failed: %d\n",
							__func__, status);
L
Linus Torvalds 已提交
2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195
	}

}

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;
2196
		status = usb_submit_urb(urb, GFP_ATOMIC);
L
Linus Torvalds 已提交
2197 2198 2199 2200 2201 2202 2203 2204 2205
	}
	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 已提交
2206 2207
static void change_port_settings(struct tty_struct *tty,
		struct edgeport_port *edge_port, struct ktermios *old_termios)
L
Linus Torvalds 已提交
2208
{
2209
	struct device *dev = &edge_port->port->dev;
L
Linus Torvalds 已提交
2210 2211 2212 2213
	struct ump_uart_config *config;
	int baud;
	unsigned cflag;
	int status;
2214 2215
	int port_number = edge_port->port->number -
					edge_port->port->serial->minor;
L
Linus Torvalds 已提交
2216

2217
	dev_dbg(dev, "%s - port %d\n", __func__, edge_port->port->number);
L
Linus Torvalds 已提交
2218

A
Alan Cox 已提交
2219
	config = kmalloc (sizeof (*config), GFP_KERNEL);
L
Linus Torvalds 已提交
2220
	if (!config) {
2221
		tty->termios = *old_termios;
2222
		dev_err(dev, "%s - out of memory\n", __func__);
L
Linus Torvalds 已提交
2223 2224 2225
		return;
	}

2226
	cflag = tty->termios.c_cflag;
L
Linus Torvalds 已提交
2227 2228 2229 2230 2231 2232 2233 2234 2235

	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) {
2236 2237
	case CS5:
		    config->bDataBits = UMP_UART_CHAR5BITS;
2238
		    dev_dbg(dev, "%s - data bits = 5\n", __func__);
2239 2240 2241
		    break;
	case CS6:
		    config->bDataBits = UMP_UART_CHAR6BITS;
2242
		    dev_dbg(dev, "%s - data bits = 6\n", __func__);
2243 2244 2245
		    break;
	case CS7:
		    config->bDataBits = UMP_UART_CHAR7BITS;
2246
		    dev_dbg(dev, "%s - data bits = 7\n", __func__);
2247 2248 2249 2250
		    break;
	default:
	case CS8:
		    config->bDataBits = UMP_UART_CHAR8BITS;
2251
		    dev_dbg(dev, "%s - data bits = 8\n", __func__);
L
Linus Torvalds 已提交
2252 2253 2254 2255 2256 2257 2258
			    break;
	}

	if (cflag & PARENB) {
		if (cflag & PARODD) {
			config->wFlags |= UMP_MASK_UART_FLAGS_PARITY;
			config->bParity = UMP_UART_ODDPARITY;
2259
			dev_dbg(dev, "%s - parity = odd\n", __func__);
L
Linus Torvalds 已提交
2260 2261 2262
		} else {
			config->wFlags |= UMP_MASK_UART_FLAGS_PARITY;
			config->bParity = UMP_UART_EVENPARITY;
2263
			dev_dbg(dev, "%s - parity = even\n", __func__);
L
Linus Torvalds 已提交
2264 2265
		}
	} else {
2266
		config->bParity = UMP_UART_NOPARITY;
2267
		dev_dbg(dev, "%s - parity = none\n", __func__);
L
Linus Torvalds 已提交
2268 2269 2270 2271
	}

	if (cflag & CSTOPB) {
		config->bStopBits = UMP_UART_STOPBIT2;
2272
		dev_dbg(dev, "%s - stop bits = 2\n", __func__);
L
Linus Torvalds 已提交
2273 2274
	} else {
		config->bStopBits = UMP_UART_STOPBIT1;
2275
		dev_dbg(dev, "%s - stop bits = 1\n", __func__);
L
Linus Torvalds 已提交
2276 2277 2278 2279 2280 2281
	}

	/* 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;
2282
		dev_dbg(dev, "%s - RTS/CTS is enabled\n", __func__);
L
Linus Torvalds 已提交
2283
	} else {
2284
		dev_dbg(dev, "%s - RTS/CTS is disabled\n", __func__);
L
Linus Torvalds 已提交
2285 2286 2287 2288
		tty->hw_stopped = 0;
		restart_read(edge_port);
	}

2289 2290 2291 2292
	/* 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 已提交
2293

2294 2295 2296
	/* if we are implementing INBOUND XON/XOFF */
	if (I_IXOFF(tty)) {
		config->wFlags |= UMP_MASK_UART_FLAGS_IN_X;
2297 2298
		dev_dbg(dev, "%s - INBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x\n",
			__func__, config->cXon, config->cXoff);
2299
	} else
2300
		dev_dbg(dev, "%s - INBOUND XON/XOFF is disabled\n", __func__);
L
Linus Torvalds 已提交
2301

2302 2303 2304
	/* if we are implementing OUTBOUND XON/XOFF */
	if (I_IXON(tty)) {
		config->wFlags |= UMP_MASK_UART_FLAGS_OUT_X;
2305 2306
		dev_dbg(dev, "%s - OUTBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x\n",
			__func__, config->cXon, config->cXoff);
2307
	} else
2308
		dev_dbg(dev, "%s - OUTBOUND XON/XOFF is disabled\n", __func__);
L
Linus Torvalds 已提交
2309

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

L
Linus Torvalds 已提交
2312 2313 2314 2315 2316
	/* Round the baud rate */
	baud = tty_get_baud_rate(tty);
	if (!baud) {
		/* pick a default, any default... */
		baud = 9600;
A
Alan Cox 已提交
2317 2318 2319
	} else
		tty_encode_baud_rate(tty, baud, baud);

L
Linus Torvalds 已提交
2320 2321 2322
	edge_port->baud_rate = baud;
	config->wBaudRate = (__u16)((461550L + baud/2) / baud);

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

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

2327 2328 2329 2330 2331 2332 2333 2334
	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 已提交
2335 2336

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

2340
	status = send_cmd(edge_port->port->serial->dev, UMPC_SET_CONFIG,
L
Linus Torvalds 已提交
2341
				(__u8)(UMPM_UART1_PORT + port_number),
2342 2343
				0, (__u8 *)config, sizeof(*config));
	if (status)
2344 2345
		dev_dbg(dev, "%s - error %d when trying to write config to device\n",
			__func__, status);
2346
	kfree(config);
L
Linus Torvalds 已提交
2347 2348
}

A
Alan Cox 已提交
2349
static void edge_set_termios(struct tty_struct *tty,
A
Alan Cox 已提交
2350
		struct usb_serial_port *port, struct ktermios *old_termios)
L
Linus Torvalds 已提交
2351 2352
{
	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
A
Alan Cox 已提交
2353 2354
	unsigned int cflag;

2355
	cflag = tty->termios.c_cflag;
L
Linus Torvalds 已提交
2356

2357
	dev_dbg(&port->dev, "%s - clfag %08x iflag %08x\n", __func__,
2358
		tty->termios.c_cflag, tty->termios.c_iflag);
2359 2360 2361
	dev_dbg(&port->dev, "%s - old clfag %08x old iflag %08x\n", __func__,
		old_termios->c_cflag, old_termios->c_iflag);
	dev_dbg(&port->dev, "%s - port %d\n", __func__, port->number);
L
Linus Torvalds 已提交
2362 2363 2364 2365

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

2369
static int edge_tiocmset(struct tty_struct *tty,
2370
					unsigned int set, unsigned int clear)
L
Linus Torvalds 已提交
2371
{
A
Alan Cox 已提交
2372
	struct usb_serial_port *port = tty->driver_data;
L
Linus Torvalds 已提交
2373 2374
	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
	unsigned int mcr;
2375
	unsigned long flags;
L
Linus Torvalds 已提交
2376

2377
	spin_lock_irqsave(&edge_port->ep_lock, flags);
L
Linus Torvalds 已提交
2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393
	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;
2394
	spin_unlock_irqrestore(&edge_port->ep_lock, flags);
L
Linus Torvalds 已提交
2395

2396
	restore_mcr(edge_port, mcr);
L
Linus Torvalds 已提交
2397 2398 2399
	return 0;
}

2400
static int edge_tiocmget(struct tty_struct *tty)
L
Linus Torvalds 已提交
2401
{
A
Alan Cox 已提交
2402
	struct usb_serial_port *port = tty->driver_data;
L
Linus Torvalds 已提交
2403 2404 2405 2406
	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
	unsigned int result = 0;
	unsigned int msr;
	unsigned int mcr;
2407
	unsigned long flags;
L
Linus Torvalds 已提交
2408

2409 2410
	spin_lock_irqsave(&edge_port->ep_lock, flags);

L
Linus Torvalds 已提交
2411 2412 2413 2414 2415 2416 2417 2418 2419 2420
	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 */


2421
	dev_dbg(&port->dev, "%s -- %x\n", __func__, result);
2422
	spin_unlock_irqrestore(&edge_port->ep_lock, flags);
L
Linus Torvalds 已提交
2423 2424 2425 2426

	return result;
}

2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447
static int edge_get_icount(struct tty_struct *tty,
				struct serial_icounter_struct *icount)
{
	struct usb_serial_port *port = tty->driver_data;
	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
	struct async_icount *ic = &edge_port->icount;

	icount->cts = ic->cts;
	icount->dsr = ic->dsr;
	icount->rng = ic->rng;
	icount->dcd = ic->dcd;
	icount->tx = ic->tx;
        icount->rx = ic->rx;
        icount->frame = ic->frame;
        icount->parity = ic->parity;
        icount->overrun = ic->overrun;
        icount->brk = ic->brk;
        icount->buf_overrun = ic->buf_overrun;
	return 0;
}

2448 2449
static int get_serial_info(struct edgeport_port *edge_port,
				struct serial_struct __user *retinfo)
L
Linus Torvalds 已提交
2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472
{
	struct serial_struct tmp;

	if (!retinfo)
		return -EFAULT;

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

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

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

2473
static int edge_ioctl(struct tty_struct *tty,
2474
					unsigned int cmd, unsigned long arg)
L
Linus Torvalds 已提交
2475
{
A
Alan Cox 已提交
2476
	struct usb_serial_port *port = tty->driver_data;
L
Linus Torvalds 已提交
2477 2478 2479 2480
	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
	struct async_icount cnow;
	struct async_icount cprev;

2481
	dev_dbg(&port->dev, "%s - port %d, cmd = 0x%x\n", __func__, port->number, cmd);
L
Linus Torvalds 已提交
2482 2483

	switch (cmd) {
2484
	case TIOCGSERIAL:
2485
		dev_dbg(&port->dev, "%s - TIOCGSERIAL\n", __func__);
2486 2487 2488
		return get_serial_info(edge_port,
				(struct serial_struct __user *) arg);
	case TIOCMIWAIT:
2489
		dev_dbg(&port->dev, "%s - TIOCMIWAIT\n", __func__);
2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504
		cprev = edge_port->icount;
		while (1) {
			interruptible_sleep_on(&edge_port->delta_msr_wait);
			/* see if a signal did it */
			if (signal_pending(current))
				return -ERESTARTSYS;
			cnow = edge_port->icount;
			if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
			    cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
				return -EIO; /* no change => error */
			if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
			    ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
			    ((arg & TIOCM_CD)  && (cnow.dcd != cprev.dcd)) ||
			    ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
				return 0;
L
Linus Torvalds 已提交
2505
			}
2506 2507 2508 2509
			cprev = cnow;
		}
		/* not reached */
		break;
L
Linus Torvalds 已提交
2510 2511 2512 2513
	}
	return -ENOIOCTLCMD;
}

A
Alan Cox 已提交
2514
static void edge_break(struct tty_struct *tty, int break_state)
L
Linus Torvalds 已提交
2515
{
A
Alan Cox 已提交
2516
	struct usb_serial_port *port = tty->driver_data;
L
Linus Torvalds 已提交
2517 2518
	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
	int status;
2519
	int bv = 0;	/* Off */
L
Linus Torvalds 已提交
2520 2521

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

A
Alan Cox 已提交
2524
	if (break_state == -1)
2525 2526 2527
		bv = 1;	/* On */
	status = ti_do_config(edge_port, UMPC_SET_CLR_BREAK, bv);
	if (status)
2528 2529
		dev_dbg(&port->dev, "%s - error %d sending break set/clear command.\n",
			__func__, status);
L
Linus Torvalds 已提交
2530 2531
}

2532
static int edge_startup(struct usb_serial *serial)
L
Linus Torvalds 已提交
2533 2534 2535 2536 2537 2538 2539 2540 2541 2542
{
	struct edgeport_serial *edge_serial;
	struct edgeport_port *edge_port;
	struct usb_device *dev;
	int status;
	int i;

	dev = serial->dev;

	/* create our private serial structure */
2543
	edge_serial = kzalloc(sizeof(struct edgeport_serial), GFP_KERNEL);
L
Linus Torvalds 已提交
2544
	if (edge_serial == NULL) {
2545
		dev_err(&serial->dev->dev, "%s - Out of memory\n", __func__);
L
Linus Torvalds 已提交
2546 2547
		return -ENOMEM;
	}
2548
	mutex_init(&edge_serial->es_lock);
L
Linus Torvalds 已提交
2549 2550 2551
	edge_serial->serial = serial;
	usb_set_serial_data(serial, edge_serial);

2552
	status = download_fw(edge_serial);
L
Linus Torvalds 已提交
2553
	if (status) {
2554
		kfree(edge_serial);
L
Linus Torvalds 已提交
2555 2556 2557 2558 2559
		return status;
	}

	/* set up our port private structures */
	for (i = 0; i < serial->num_ports; ++i) {
2560
		edge_port = kzalloc(sizeof(struct edgeport_port), GFP_KERNEL);
L
Linus Torvalds 已提交
2561
		if (edge_port == NULL) {
2562 2563
			dev_err(&serial->dev->dev, "%s - Out of memory\n",
								__func__);
L
Linus Torvalds 已提交
2564 2565 2566
			goto cleanup;
		}
		spin_lock_init(&edge_port->ep_lock);
2567 2568
		if (kfifo_alloc(&edge_port->write_fifo, EDGE_OUT_BUF_SIZE,
								GFP_KERNEL)) {
2569 2570
			dev_err(&serial->dev->dev, "%s - Out of memory\n",
								__func__);
L
Linus Torvalds 已提交
2571 2572 2573 2574 2575 2576
			kfree(edge_port);
			goto cleanup;
		}
		edge_port->port = serial->port[i];
		edge_port->edge_serial = edge_serial;
		usb_set_serial_port_data(serial->port[i], edge_port);
2577
		edge_port->bUartMode = default_uart_mode;
L
Linus Torvalds 已提交
2578
	}
2579

L
Linus Torvalds 已提交
2580 2581 2582
	return 0;

cleanup:
2583
	for (--i; i >= 0; --i) {
L
Linus Torvalds 已提交
2584
		edge_port = usb_get_serial_port_data(serial->port[i]);
2585
		kfifo_free(&edge_port->write_fifo);
L
Linus Torvalds 已提交
2586 2587 2588
		kfree(edge_port);
		usb_set_serial_port_data(serial->port[i], NULL);
	}
2589
	kfree(edge_serial);
L
Linus Torvalds 已提交
2590 2591 2592 2593
	usb_set_serial_data(serial, NULL);
	return -ENOMEM;
}

2594
static void edge_disconnect(struct usb_serial *serial)
L
Linus Torvalds 已提交
2595
{
2596 2597 2598 2599 2600 2601 2602 2603 2604
}

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

	for (i = 0; i < serial->num_ports; ++i) {
		edge_port = usb_get_serial_port_data(serial->port[i]);
2605
		kfifo_free(&edge_port->write_fifo);
2606
		kfree(edge_port);
L
Linus Torvalds 已提交
2607
	}
2608
	kfree(usb_get_serial_data(serial));
L
Linus Torvalds 已提交
2609 2610 2611
}


2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629
/* Sysfs Attributes */

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

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

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

2630
	dev_dbg(dev, "%s: setting uart_mode = %d\n", __func__, v);
2631 2632 2633 2634

	if (v < 256)
		edge_port->bUartMode = v;
	else
2635
		dev_err(dev, "%s - uart_mode %d is invalid\n", __func__, v);
2636 2637 2638 2639

	return count;
}

2640 2641
static DEVICE_ATTR(uart_mode, S_IWUSR | S_IRUGO, show_uart_mode,
							store_uart_mode);
2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654

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


2655
static struct usb_serial_driver edgeport_1port_device = {
2656
	.driver = {
2657 2658
		.owner		= THIS_MODULE,
		.name		= "edgeport_ti_1",
2659
	},
2660
	.description		= "Edgeport TI 1 port adapter",
L
Linus Torvalds 已提交
2661 2662 2663 2664 2665 2666 2667
	.id_table		= edgeport_1port_id_table,
	.num_ports		= 1,
	.open			= edge_open,
	.close			= edge_close,
	.throttle		= edge_throttle,
	.unthrottle		= edge_unthrottle,
	.attach			= edge_startup,
2668 2669
	.disconnect		= edge_disconnect,
	.release		= edge_release,
2670
	.port_probe		= edge_create_sysfs_attrs,
2671
	.port_remove		= edge_remove_sysfs_attrs,
L
Linus Torvalds 已提交
2672 2673 2674 2675
	.ioctl			= edge_ioctl,
	.set_termios		= edge_set_termios,
	.tiocmget		= edge_tiocmget,
	.tiocmset		= edge_tiocmset,
2676
	.get_icount		= edge_get_icount,
L
Linus Torvalds 已提交
2677 2678 2679 2680 2681 2682 2683 2684 2685
	.write			= edge_write,
	.write_room		= edge_write_room,
	.chars_in_buffer	= edge_chars_in_buffer,
	.break_ctl		= edge_break,
	.read_int_callback	= edge_interrupt_callback,
	.read_bulk_callback	= edge_bulk_in_callback,
	.write_bulk_callback	= edge_bulk_out_callback,
};

2686
static struct usb_serial_driver edgeport_2port_device = {
2687
	.driver = {
2688 2689
		.owner		= THIS_MODULE,
		.name		= "edgeport_ti_2",
2690
	},
2691
	.description		= "Edgeport TI 2 port adapter",
L
Linus Torvalds 已提交
2692 2693 2694 2695 2696 2697 2698
	.id_table		= edgeport_2port_id_table,
	.num_ports		= 2,
	.open			= edge_open,
	.close			= edge_close,
	.throttle		= edge_throttle,
	.unthrottle		= edge_unthrottle,
	.attach			= edge_startup,
2699 2700
	.disconnect		= edge_disconnect,
	.release		= edge_release,
2701
	.port_probe		= edge_create_sysfs_attrs,
2702
	.port_remove		= edge_remove_sysfs_attrs,
L
Linus Torvalds 已提交
2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715
	.ioctl			= edge_ioctl,
	.set_termios		= edge_set_termios,
	.tiocmget		= edge_tiocmget,
	.tiocmset		= edge_tiocmset,
	.write			= edge_write,
	.write_room		= edge_write_room,
	.chars_in_buffer	= edge_chars_in_buffer,
	.break_ctl		= edge_break,
	.read_int_callback	= edge_interrupt_callback,
	.read_bulk_callback	= edge_bulk_in_callback,
	.write_bulk_callback	= edge_bulk_out_callback,
};

2716 2717 2718 2719
static struct usb_serial_driver * const serial_drivers[] = {
	&edgeport_1port_device, &edgeport_2port_device, NULL
};

2720
module_usb_serial_driver(serial_drivers, id_table_combined);
L
Linus Torvalds 已提交
2721 2722 2723 2724

MODULE_AUTHOR(DRIVER_AUTHOR);
MODULE_DESCRIPTION(DRIVER_DESC);
MODULE_LICENSE("GPL");
2725
MODULE_FIRMWARE("edgeport/down3.bin");
L
Linus Torvalds 已提交
2726 2727 2728 2729 2730

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

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