1 /*
2  * Edgeport USB Serial Converter driver
3  *
4  * Copyright (C) 2000-2002 Inside Out Networks, All rights reserved.
5  * Copyright (C) 2001-2002 Greg Kroah-Hartman <greg@kroah.com>
6  *
7  *	This program is free software; you can redistribute it and/or modify
8  *	it under the terms of the GNU General Public License as published by
9  *	the Free Software Foundation; either version 2 of the License, or
10  *	(at your option) any later version.
11  *
12  * Supports the following devices:
13  *	EP/1 EP/2 EP/4
14  *
15  * Version history:
16  *
17  *	July 11, 2002 	Removed 4 port device structure since all TI UMP
18  *			chips have only 2 ports
19  *			David Iacovelli (davidi@ionetworks.com)
20  *
21  */
22 
23 #include <linux/config.h>
24 #include <linux/kernel.h>
25 #include <linux/errno.h>
26 #include <linux/init.h>
27 #include <linux/slab.h>
28 #include <linux/tty.h>
29 #include <linux/tty_driver.h>
30 #include <linux/tty_flip.h>
31 #include <linux/module.h>
32 #include <linux/spinlock.h>
33 #include <linux/serial.h>
34 #include <linux/ioctl.h>
35 #include <asm/uaccess.h>
36 #include <linux/usb.h>
37 
38 #ifdef CONFIG_USB_SERIAL_DEBUG
39 	static int debug = 1;
40 #else
41 	static int debug;
42 #endif
43 
44 #include "usb-serial.h"
45 
46 #include "io_16654.h"
47 #include "io_usbvend.h"
48 #include "io_ti.h"
49 
50 /*
51  * Version Information
52  */
53 #define DRIVER_VERSION "v0.2"
54 #define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com> and David Iacovelli"
55 #define DRIVER_DESC "Edgeport USB Serial Driver"
56 
57 
58 /* firmware image code */
59 #define IMAGE_VERSION_NAME	PagableOperationalCodeImageVersion
60 #define IMAGE_ARRAY_NAME	PagableOperationalCodeImage
61 #define IMAGE_SIZE		PagableOperationalCodeSize
62 #include "io_fw_down3.h"	/* Define array OperationalCodeImage[] */
63 
64 #define EPROM_PAGE_SIZE		64
65 
66 
67 struct edgeport_uart_buf_desc {
68 	__u32 count;		// Number of bytes currently in buffer
69 };
70 
71 /* different hardware types */
72 #define HARDWARE_TYPE_930	0
73 #define HARDWARE_TYPE_TIUMP	1
74 
75 // IOCTL_PRIVATE_TI_GET_MODE Definitions
76 #define	TI_MODE_CONFIGURING	0   // Device has not entered start device
77 #define	TI_MODE_BOOT		1   // Staying in boot mode
78 #define TI_MODE_DOWNLOAD	2   // Made it to download mode
79 #define TI_MODE_TRANSITIONING	3   // Currently in boot mode but transitioning to download mode
80 
81 
82 /* Product information read from the Edgeport */
83 struct product_info
84 {
85 	int	TiMode;			// Current TI Mode
86 	__u8	hardware_type;		// Type of hardware
87 } __attribute__((packed));
88 
89 
90 struct edgeport_port {
91 	__u16 uart_base;
92 	__u16 dma_address;
93 	__u8 shadow_msr;
94 	__u8 shadow_mcr;
95 	__u8 shadow_lsr;
96 	__u8 lsr_mask;
97 	__u32 ump_read_timeout;		/* Number of miliseconds the UMP will
98 					   wait without data before completing
99 					   a read short */
100 	int baud_rate;
101 	int close_pending;
102 	int lsr_event;
103 	struct edgeport_uart_buf_desc tx;
104 	struct async_icount	icount;
105 	wait_queue_head_t	delta_msr_wait;	/* for handling sleeping while
106 						   waiting for msr change to
107 						   happen */
108 	struct edgeport_serial	*edge_serial;
109 	struct usb_serial_port	*port;
110 };
111 
112 struct edgeport_serial {
113 	struct product_info product_info;
114 	u8 TI_I2C_Type;			// Type of I2C in UMP
115 	u8 TiReadI2C;			// Set to TRUE if we have read the I2c in Boot Mode
116 	int num_ports_open;
117 	struct usb_serial *serial;
118 };
119 
120 
121 /* Devices that this driver supports */
122 static struct usb_device_id edgeport_1port_id_table [] = {
123 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_1) },
124 	{ }
125 };
126 
127 static struct usb_device_id edgeport_2port_id_table [] = {
128 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2) },
129 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2C) },
130 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2I) },
131 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_421) },
132 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_421_BOOT) },
133 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_421_DOWN) },
134 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21) },
135 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21_BOOT) },
136 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21_DOWN) },
137 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_42) },
138 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4) },
139 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4I) },
140 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22) },
141 	{ }
142 };
143 
144 /* Devices that this driver supports */
145 static __devinitdata struct usb_device_id id_table_combined [] = {
146 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_1) },
147 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2) },
148 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2C) },
149 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2I) },
150 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_421) },
151 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_421_BOOT) },
152 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_421_DOWN) },
153 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21) },
154 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21_BOOT) },
155 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21_DOWN) },
156 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_42) },
157 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4) },
158 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4I) },
159 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22) },
160 	{ }
161 };
162 
163 MODULE_DEVICE_TABLE (usb, id_table_combined);
164 
165 
166 static struct EDGE_FIRMWARE_VERSION_INFO OperationalCodeImageVersion;
167 
168 static int TIStayInBootMode = 0;
169 static int ignore_cpu_rev = 0;
170 
171 
172 
173 static void edge_set_termios (struct usb_serial_port *port, struct termios *old_termios);
174 
TIReadVendorRequestSync(struct usb_device * dev,__u8 request,__u16 value,__u16 index,u8 * data,int size)175 static int TIReadVendorRequestSync (struct usb_device *dev,
176 				__u8		request,
177 				__u16		value,
178 				__u16		index,
179 				u8 		*data,
180 				int		size)
181 {
182 	int status;
183 
184 	status = usb_control_msg (dev,
185 				usb_rcvctrlpipe(dev, 0),
186 				request,
187 				(USB_TYPE_VENDOR |
188 				 USB_RECIP_DEVICE |
189 				 USB_DIR_IN),
190 				value,
191 				index,
192 				data,
193 				size,
194 				HZ);
195 	if (status < 0)
196 		return status;
197 	if (status != size) {
198 		dbg ("%s - wanted to write %d, but only wrote %d",
199 		     __FUNCTION__, size, status);
200 		return -ECOMM;
201 	}
202 	return 0;
203 }
204 
TISendVendorRequestSync(struct usb_device * dev,__u8 request,__u16 value,__u16 index,u8 * data,int size)205 static int TISendVendorRequestSync (struct usb_device *dev,
206 				__u8		request,
207 				__u16		value,
208 				__u16		index,
209 				u8 		*data,
210 				int		size)
211 {
212 	int status;
213 
214 	status = usb_control_msg (dev,
215 				usb_sndctrlpipe(dev, 0),
216 				request,
217 				(USB_TYPE_VENDOR |
218 				 USB_RECIP_DEVICE |
219 				 USB_DIR_OUT),
220 				value,
221 				index,
222 				data,
223 				size,
224 				HZ);
225 
226 	if (status < 0)
227 		return status;
228 	if (status != size) {
229 		dbg ("%s - wanted to write %d, but only wrote %d",
230 		     __FUNCTION__, size, status);
231 		return -ECOMM;
232 	}
233 	return 0;
234 }
235 
TIWriteCommandSync(struct usb_device * dev,__u8 command,__u8 moduleid,__u16 value,u8 * data,int size)236 static int TIWriteCommandSync (struct usb_device *dev, __u8 command,
237 				__u8 moduleid, __u16 value, u8 *data,
238 				int size)
239 {
240 	return TISendVendorRequestSync (dev,
241 					  command,	  		// Request
242 					  value,			// wValue
243 					  moduleid,			// wIndex
244 					  data,				// TransferBuffer
245 					  size);			// TransferBufferLength
246 
247 }
248 
249 
250 /* clear tx/rx buffers and fifo in TI UMP */
TIPurgeDataSync(struct usb_serial_port * port,__u16 mask)251 static int TIPurgeDataSync (struct usb_serial_port *port, __u16 mask)
252 {
253 	int port_number = port->number - port->serial->minor;
254 
255 	dbg ("%s - port %d, mask %x", __FUNCTION__, port_number, mask);
256 
257 	return TIWriteCommandSync (port->serial->dev,
258 					UMPC_PURGE_PORT,
259 					(__u8)(UMPM_UART1_PORT + port_number),
260 					mask,
261 					NULL,
262 					0);
263 }
264 
265 /**
266  * TIReadDownloadMemory - Read edgeport memory from TI chip
267  * @dev: usb device pointer
268  * @address: Device CPU address at which to read
269  * @length: Length of above data
270  * @address_type: Can read both XDATA and I2C
271  * @buffer: pointer to input data buffer
272  */
TIReadDownloadMemory(struct usb_device * dev,int start_address,int length,__u8 address_type,__u8 * buffer)273 int TIReadDownloadMemory (struct usb_device *dev, int start_address, int length,
274 			  __u8 address_type, __u8 *buffer)
275 {
276 	int status = 0;
277 	__u8 read_length;
278 	__u16 be_start_address;
279 
280 	dbg ("%s - @ %x for %d", __FUNCTION__, start_address, length);
281 
282 	/* Read in blocks of 64 bytes
283 	 * (TI firmware can't handle more than 64 byte reads)
284 	 */
285 	while (length) {
286 		if (length > 64)
287 			read_length= 64;
288 		else
289 			read_length = (__u8)length;
290 
291 		if (read_length > 1) {
292 			dbg ("%s - @ %x for %d", __FUNCTION__,
293 			     start_address, read_length);
294 		}
295 		be_start_address = cpu_to_be16 (start_address);
296 		status = TIReadVendorRequestSync (dev,
297 						  UMPC_MEMORY_READ,	// Request
298 						  (__u16)address_type,	// wValue (Address type)
299 						  be_start_address,	// wIndex (Address to read)
300 						  buffer,		// TransferBuffer
301 						  read_length);	// TransferBufferLength
302 
303 		if (status) {
304 			dbg ("%s - ERROR %x", __FUNCTION__, status);
305 			return status;
306 		}
307 
308 		if (read_length > 1) {
309 			usb_serial_debug_data (__FILE__, __FUNCTION__,
310 					       read_length, buffer);
311 		}
312 
313 		/* Update pointers/length */
314 		start_address += read_length;
315 		buffer += read_length;
316 		length -= read_length;
317 	}
318 
319 	return status;
320 }
321 
TIReadRam(struct usb_device * dev,int start_address,int length,__u8 * buffer)322 int TIReadRam (struct usb_device *dev, int start_address, int length, __u8 *buffer)
323 {
324 	return TIReadDownloadMemory (dev,
325 				     start_address,
326 				     length,
327 				     DTK_ADDR_SPACE_XDATA,
328 				     buffer);
329 }
330 
331 /* Read edgeport memory to a given block */
TIReadBootMemory(struct edgeport_serial * serial,int start_address,int length,__u8 * buffer)332 static int TIReadBootMemory (struct edgeport_serial *serial, int start_address, int length, __u8 * buffer)
333 {
334 	int status = 0;
335 	int i;
336 
337 	for (i=0; i< length; i++) {
338 		status = TIReadVendorRequestSync (serial->serial->dev,
339 					UMPC_MEMORY_READ,		// Request
340 					serial->TI_I2C_Type,		// wValue (Address type)
341 					(__u16)(start_address+i),	// wIndex
342 					&buffer[i],			// TransferBuffer
343 					0x01);				// TransferBufferLength
344 		if (status) {
345 			dbg ("%s - ERROR %x", __FUNCTION__, status);
346 			return status;
347 		}
348 	}
349 
350 	dbg ("%s - start_address = %x, length = %d", __FUNCTION__, start_address, length);
351 	usb_serial_debug_data (__FILE__, __FUNCTION__, length, buffer);
352 
353 	serial->TiReadI2C = 1;
354 
355 	return status;
356 }
357 
358 /* Write given block to TI EPROM memory */
TIWriteBootMemory(struct edgeport_serial * serial,int start_address,int length,__u8 * buffer)359 static int TIWriteBootMemory (struct edgeport_serial *serial, int start_address, int length, __u8 *buffer)
360 {
361 	int status = 0;
362 	int i;
363 	__u8 temp;
364 
365 	/* Must do a read before write */
366 	if (!serial->TiReadI2C) {
367 		status = TIReadBootMemory(serial, 0, 1, &temp);
368 		if (status)
369 			return status;
370 	}
371 
372 	for (i=0; i < length; ++i) {
373 		status = TISendVendorRequestSync (serial->serial->dev,
374 						UMPC_MEMORY_WRITE,		// Request
375 						buffer[i],			// wValue
376 						(__u16)(i+start_address),	// wIndex
377 						NULL,				// TransferBuffer
378 						0);				// TransferBufferLength
379 		if (status)
380 			return status;
381 	}
382 
383   	dbg ("%s - start_sddr = %x, length = %d", __FUNCTION__, start_address, length);
384 	usb_serial_debug_data (__FILE__, __FUNCTION__, length, buffer);
385 
386 	return status;
387 }
388 
389 
390 /* Write edgeport I2C memory to TI chip	*/
TIWriteDownloadI2C(struct edgeport_serial * serial,int start_address,int length,__u8 address_type,__u8 * buffer)391 static int TIWriteDownloadI2C (struct edgeport_serial *serial, int start_address, int length, __u8 address_type, __u8 *buffer)
392 {
393 	int status = 0;
394 	int write_length;
395 	__u16 be_start_address;
396 
397 	/* We can only send a maximum of 1 aligned byte page at a time */
398 
399 	/* calulate the number of bytes left in the first page */
400 	write_length = EPROM_PAGE_SIZE - (start_address & (EPROM_PAGE_SIZE - 1));
401 
402 	if (write_length > length)
403 		write_length = length;
404 
405 	dbg ("%s - BytesInFirstPage Addr = %x, length = %d", __FUNCTION__, start_address, write_length);
406 	usb_serial_debug_data (__FILE__, __FUNCTION__, write_length, buffer);
407 
408 	/* Write first page */
409 	be_start_address = cpu_to_be16 (start_address);
410 	status = TISendVendorRequestSync (serial->serial->dev,
411 					UMPC_MEMORY_WRITE,	// Request
412 					(__u16)address_type,	// wValue
413 					be_start_address,	// wIndex
414 					buffer,			// TransferBuffer
415 					write_length);
416 	if (status) {
417 		dbg ("%s - ERROR %d", __FUNCTION__, status);
418 		return status;
419 	}
420 
421 	length		-= write_length;
422 	start_address	+= write_length;
423 	buffer		+= write_length;
424 
425 	/* We should be aligned now -- can write max page size bytes at a time */
426 	while (length) {
427 		if (length > EPROM_PAGE_SIZE)
428 			write_length = EPROM_PAGE_SIZE;
429 		else
430 			write_length = length;
431 
432 		dbg ("%s - Page Write Addr = %x, length = %d", __FUNCTION__, start_address, write_length);
433 		usb_serial_debug_data (__FILE__, __FUNCTION__, write_length, buffer);
434 
435 		/* Write next page */
436 		be_start_address = cpu_to_be16 (start_address);
437 		status = TISendVendorRequestSync (serial->serial->dev,
438 						UMPC_MEMORY_WRITE,	// Request
439 						(__u16)address_type,	// wValue
440 						be_start_address,	// wIndex
441 						buffer,	  		// TransferBuffer
442 						write_length);		// TransferBufferLength
443 		if (status) {
444 			dbg ("%s - ERROR %d", __FUNCTION__, status);
445 			return status;
446 		}
447 
448 		length		-= write_length;
449 		start_address	+= write_length;
450 		buffer		+= write_length;
451 	}
452 	return status;
453 }
454 
455 /* Examine the UMP DMA registers and LSR
456  *
457  * Check the MSBit of the X and Y DMA byte count registers.
458  * A zero in this bit indicates that the TX DMA buffers are empty
459  * then check the TX Empty bit in the UART.
460  */
TIIsTxActive(struct edgeport_port * port)461 static int TIIsTxActive (struct edgeport_port *port)
462 {
463 	int status;
464 	struct out_endpoint_desc_block *oedb;
465 	__u8 *lsr;
466 	int bytes_left = 0;
467 
468 	oedb = kmalloc (sizeof (* oedb), GFP_KERNEL);
469 	if (!oedb) {
470 		err ("%s - out of memory", __FUNCTION__);
471 		return -ENOMEM;
472 	}
473 
474 	lsr = kmalloc (1, GFP_KERNEL);	/* Sigh, that's right, just one byte,
475 					   as not all platforms can do DMA
476 					   from stack */
477 	if (!lsr) {
478 		kfree(oedb);
479 		return -ENOMEM;
480 	}
481 	/* Read the DMA Count Registers */
482 	status = TIReadRam (port->port->serial->dev,
483 			    port->dma_address,
484 			    sizeof( *oedb),
485 			    (void *)oedb);
486 
487 	if (status)
488 		goto exit_is_tx_active;
489 
490 	dbg ("%s - XByteCount    0x%X", __FUNCTION__, oedb->XByteCount);
491 
492 	/* and the LSR */
493 	status = TIReadRam (port->port->serial->dev,
494 			    port->uart_base + UMPMEM_OFFS_UART_LSR,
495 			    1,
496 			    lsr);
497 
498 	if (status)
499 		goto exit_is_tx_active;
500 	dbg ("%s - LSR = 0x%X", __FUNCTION__, *lsr);
501 
502 	/* If either buffer has data or we are transmitting then return TRUE */
503 	if ((oedb->XByteCount & 0x80 ) != 0 )
504 		bytes_left += 64;
505 
506 	if ((*lsr & UMP_UART_LSR_TX_MASK ) == 0 )
507 		bytes_left += 1;
508 
509 	/* We return Not Active if we get any kind of error */
510 exit_is_tx_active:
511 	dbg ("%s - return %d", __FUNCTION__, bytes_left );
512 
513 	kfree(lsr);
514 	kfree(oedb);
515 	return bytes_left;
516 }
517 
TIChasePort(struct edgeport_port * port)518 static void TIChasePort(struct edgeport_port *port)
519 {
520 	int loops;
521 	int last_count;
522 	int write_size;
523 
524 restart_tx_loop:
525 	// Base the LoopTime on the baud rate
526 	if (port->baud_rate == 0)
527 		port->baud_rate = 1200;
528 
529 	write_size = port->tx.count;
530 	loops = max(100, (100*write_size)/(port->baud_rate/10));
531 	dbg ("%s - write_size %d, baud %d loop = %d", __FUNCTION__,
532 	     write_size, port->baud_rate, loops);
533 
534 	while (1) {
535 		// Save Last count
536 		last_count = port->tx.count;
537 
538 		dbg ("%s - Tx Buffer Size = %d loops = %d", __FUNCTION__,
539 		     last_count, loops);
540 
541 		/* Is the Edgeport Buffer empty? */
542 		if (port->tx.count == 0)
543 			break;
544 
545 		/* Block the thread for 10ms */
546 		wait_ms (10);
547 
548 		if (last_count == port->tx.count) {
549 			/* No activity.. count down. */
550 			--loops;
551 			if (loops == 0) {
552 				dbg ("%s - Wait for TxEmpty - TIMEOUT",
553 				     __FUNCTION__);
554 				return;
555 			}
556 		} else {
557 			/* Reset timeout value back to a minimum of 1 second */
558 			dbg ("%s - Wait for TxEmpty  Reset Count", __FUNCTION__);
559 			goto restart_tx_loop;
560 		}
561 	}
562 
563 	dbg ("%s - Local Tx Buffer Empty -- Waiting for TI UMP to EMPTY X/Y and FIFO",
564 	     __FUNCTION__);
565 
566 	write_size = TIIsTxActive (port);
567 	loops = max(50, (100*write_size)/(port->baud_rate/10));
568 	dbg ("%s - write_size %d, baud %d loop = %d", __FUNCTION__,
569 	     write_size, port->baud_rate, loops);
570 
571 	while (1) {
572 		/* This function takes 4 ms; */
573 		if (!TIIsTxActive (port)) {
574 			/* Delay a few char times */
575 			wait_ms (50);
576 			dbg ("%s - Empty", __FUNCTION__);
577 			return;
578 		}
579 
580 		--loops;
581 		if (loops == 0) {
582 			dbg ("%s - TIMEOUT", __FUNCTION__);
583 			return;
584 		}
585 	}
586 }
587 
TIChooseConfiguration(struct usb_device * dev)588 static int TIChooseConfiguration (struct usb_device *dev)
589 {
590 	// There may be multiple configurations on this device, in which case
591 	// we would need to read and parse all of them to find out which one
592 	// we want. However, we just support one config at this point,
593 	// configuration # 1, which is Config Descriptor 0.
594 
595 	dbg ("%s - Number of Interfaces = %d", __FUNCTION__, dev->config->bNumInterfaces);
596 	dbg ("%s - MAX Power            = %d", __FUNCTION__, dev->config->MaxPower*2);
597 
598 	if (dev->config->bNumInterfaces != 1) {
599 		err ("%s - bNumInterfaces is not 1, ERROR!", __FUNCTION__);
600 		return -ENODEV;
601 	}
602 
603 	return 0;
604 }
605 
TIReadRom(struct edgeport_serial * serial,int start_address,int length,__u8 * buffer)606 int TIReadRom (struct edgeport_serial *serial, int start_address, int length, __u8 *buffer)
607 {
608 	int status;
609 
610 	if (serial->product_info.TiMode == TI_MODE_DOWNLOAD) {
611 		status = TIReadDownloadMemory (serial->serial->dev,
612 					       start_address,
613 					       length,
614 					       serial->TI_I2C_Type,
615 					       buffer);
616 	} else {
617 		status = TIReadBootMemory (serial,
618 					   start_address,
619 					   length,
620 					   buffer);
621 	}
622 
623 	return status;
624 }
625 
TIWriteRom(struct edgeport_serial * serial,int start_address,int length,__u8 * buffer)626 int TIWriteRom (struct edgeport_serial *serial, int start_address, int length, __u8 *buffer)
627 {
628 	if (serial->product_info.TiMode == TI_MODE_BOOT)
629 		return TIWriteBootMemory (serial,
630 					  start_address,
631 					  length,
632 					  buffer);
633 
634 	if (serial->product_info.TiMode == TI_MODE_DOWNLOAD)
635 		return TIWriteDownloadI2C (serial,
636 					   start_address,
637 					   length,
638 					   serial->TI_I2C_Type,
639 					   buffer);
640 
641 	return -EINVAL;
642 }
643 
644 
645 
646 /* Read a descriptor header from I2C based on type */
TIGetDescriptorAddress(struct edgeport_serial * serial,int desc_type,struct ti_i2c_desc * rom_desc)647 static int TIGetDescriptorAddress (struct edgeport_serial *serial, int desc_type, struct ti_i2c_desc *rom_desc)
648 {
649 	int start_address;
650 	int status;
651 
652 	/* Search for requested descriptor in I2C */
653 	start_address = 2;
654 	do {
655 		status = TIReadRom (serial,
656 				   start_address,
657 				   sizeof(struct ti_i2c_desc),
658 				   (__u8 *)rom_desc );
659 		if (status)
660 			return 0;
661 
662 		if (rom_desc->Type == desc_type)
663 			return start_address;
664 
665 		start_address = start_address + sizeof(struct ti_i2c_desc) +  rom_desc->Size;
666 
667 	} while ((start_address < TI_MAX_I2C_SIZE) && rom_desc->Type);
668 
669 	return 0;
670 }
671 
672 /* Validate descriptor checksum */
ValidChecksum(struct ti_i2c_desc * rom_desc,__u8 * buffer)673 static int ValidChecksum(struct ti_i2c_desc *rom_desc, __u8 *buffer)
674 {
675 	__u16 i;
676 	__u8 cs = 0;
677 
678 	for (i=0; i < rom_desc->Size; i++) {
679 		cs = (__u8)(cs + buffer[i]);
680 	}
681 	if (cs != rom_desc->CheckSum) {
682 		dbg ("%s - Mismatch %x - %x", __FUNCTION__, rom_desc->CheckSum, cs);
683 		return -EINVAL;
684 	}
685 	return 0;
686 }
687 
688 /* Make sure that the I2C image is good */
TiValidateI2cImage(struct edgeport_serial * serial)689 static int TiValidateI2cImage (struct edgeport_serial *serial)
690 {
691 	int status = 0;
692 	struct ti_i2c_desc *rom_desc;
693 	int start_address = 2;
694 	__u8 *buffer;
695 
696 	rom_desc = kmalloc (sizeof (*rom_desc), GFP_KERNEL);
697 	if (!rom_desc) {
698 		err ("%s - out of memory", __FUNCTION__);
699 		return -ENOMEM;
700 	}
701 	buffer = kmalloc (TI_MAX_I2C_SIZE, GFP_KERNEL);
702 	if (!buffer) {
703 		err ("%s - out of memory when allocating buffer", __FUNCTION__);
704 		kfree (rom_desc);
705 		return -ENOMEM;
706 	}
707 
708 	// Read the first byte (Signature0) must be 0x52
709 	status = TIReadRom (serial, 0, 1, buffer);
710 	if (status)
711 		goto ExitTiValidateI2cImage;
712 
713 	if (*buffer != 0x52) {
714 		err ("%s - invalid buffer signature", __FUNCTION__);
715 		status = -ENODEV;
716 		goto ExitTiValidateI2cImage;
717 	}
718 
719 	do {
720 		// Validate the I2C
721 		status = TIReadRom (serial,
722 				start_address,
723 				sizeof(struct ti_i2c_desc),
724 				(__u8 *)rom_desc);
725 		if (status)
726 			break;
727 
728 		if ((start_address + sizeof(struct ti_i2c_desc) + rom_desc->Size) > TI_MAX_I2C_SIZE) {
729 			status = -ENODEV;
730 			dbg ("%s - structure too big, erroring out.", __FUNCTION__);
731 			break;
732 		}
733 
734 		dbg ("%s Type = 0x%x", __FUNCTION__, rom_desc->Type);
735 
736 		// Skip type 2 record
737 		if ((rom_desc->Type & 0x0f) != I2C_DESC_TYPE_FIRMWARE_BASIC) {
738 			// Read the descriptor data
739 			status = TIReadRom(serial,
740 						start_address+sizeof(struct ti_i2c_desc),
741 						rom_desc->Size,
742 						buffer);
743 			if (status)
744 				break;
745 
746 			status = ValidChecksum(rom_desc, buffer);
747 			if (status)
748 				break;
749 		}
750 		start_address = start_address + sizeof(struct ti_i2c_desc) + rom_desc->Size;
751 
752 	} while ((rom_desc->Type != I2C_DESC_TYPE_ION) && (start_address < TI_MAX_I2C_SIZE));
753 
754 	if ((rom_desc->Type != I2C_DESC_TYPE_ION) || (start_address > TI_MAX_I2C_SIZE))
755 		status = -ENODEV;
756 
757 ExitTiValidateI2cImage:
758 	kfree (buffer);
759 	kfree (rom_desc);
760 	return status;
761 }
762 
TIReadManufDescriptor(struct edgeport_serial * serial,__u8 * buffer)763 static int TIReadManufDescriptor (struct edgeport_serial *serial, __u8 *buffer)
764 {
765 	int status;
766 	int start_address;
767 	struct ti_i2c_desc *rom_desc;
768 	struct edge_ti_manuf_descriptor *desc;
769 
770 	rom_desc = kmalloc (sizeof (*rom_desc), GFP_KERNEL);
771 	if (!rom_desc) {
772 		err ("%s - out of memory", __FUNCTION__);
773 		return -ENOMEM;
774 	}
775 	start_address = TIGetDescriptorAddress (serial, I2C_DESC_TYPE_ION, rom_desc);
776 
777 	if (!start_address) {
778 		dbg ("%s - Edge Descriptor not found in I2C", __FUNCTION__);
779 		status = -ENODEV;
780 		goto exit;
781 	}
782 
783 	// Read the descriptor data
784 	status = TIReadRom (serial,
785 				start_address+sizeof(struct ti_i2c_desc),
786 				rom_desc->Size,
787 				buffer);
788 	if (status)
789 		goto exit;
790 
791 	status = ValidChecksum(rom_desc, buffer);
792 
793 	desc = (struct edge_ti_manuf_descriptor *)buffer;
794 	dbg ( "%s - IonConfig      0x%x", __FUNCTION__, desc->IonConfig 	);
795 	dbg ( "%s - Version          %d", __FUNCTION__, desc->Version	  	);
796 	dbg ( "%s - Cpu/Board      0x%x", __FUNCTION__, desc->CpuRev_BoardRev	);
797 	dbg ( "%s - NumPorts         %d", __FUNCTION__, desc->NumPorts  	);
798 	dbg ( "%s - NumVirtualPorts  %d", __FUNCTION__, desc->NumVirtualPorts	);
799 	dbg ( "%s - TotalPorts       %d", __FUNCTION__, desc->TotalPorts  	);
800 
801 exit:
802 	kfree (rom_desc);
803 	return status;
804 }
805 
806 /* Build firmware header used for firmware update */
BuildI2CFirmwareHeader(__u8 * header)807 static int BuildI2CFirmwareHeader (__u8 *header)
808 {
809 	__u8 *buffer;
810 	int buffer_size;
811 	int i;
812 	__u8 cs = 0;
813 	struct ti_i2c_desc *i2c_header;
814 	struct ti_i2c_image_header *img_header;
815 	struct ti_i2c_firmware_rec *firmware_rec;
816 
817 	// In order to update the I2C firmware we must change the type 2 record to type 0xF2.
818 	// This will force the UMP to come up in Boot Mode.  Then while in boot mode, the driver
819 	// will download the latest firmware (padded to 15.5k) into the UMP ram.
820 	// And finally when the device comes back up in download mode the driver will cause
821 	// the new firmware to be copied from the UMP Ram to I2C and the firmware will update
822 	// the record type from 0xf2 to 0x02.
823 
824 	// Allocate a 15.5k buffer + 2 bytes for version number (Firmware Record)
825 	buffer_size = (((1024 * 16) - 512 )+ sizeof(struct ti_i2c_firmware_rec));
826 
827 	buffer = kmalloc (buffer_size, GFP_KERNEL);
828 	if (!buffer) {
829 		err ("%s - out of memory", __FUNCTION__);
830 		return -ENOMEM;
831 	}
832 
833 	// Set entire image of 0xffs
834 	memset (buffer, 0xff, buffer_size);
835 
836 	// Copy version number into firmware record
837 	firmware_rec = (struct ti_i2c_firmware_rec *)buffer;
838 
839 	firmware_rec->Ver_Major	= OperationalCodeImageVersion.MajorVersion;
840 	firmware_rec->Ver_Minor	= OperationalCodeImageVersion.MinorVersion;
841 
842 	// Pointer to fw_down memory image
843 	img_header = (struct ti_i2c_image_header *)&PagableOperationalCodeImage[0];
844 
845 	memcpy (buffer + sizeof(struct ti_i2c_firmware_rec),
846 		&PagableOperationalCodeImage[sizeof(struct ti_i2c_image_header)],
847 		img_header->Length);
848 
849 	for (i=0; i < buffer_size; i++) {
850 		cs = (__u8)(cs + buffer[i]);
851 	}
852 
853 	kfree (buffer);
854 
855 	// Build new header
856 	i2c_header =  (struct ti_i2c_desc *)header;
857 	firmware_rec =  (struct ti_i2c_firmware_rec*)i2c_header->Data;
858 
859 	i2c_header->Type	= I2C_DESC_TYPE_FIRMWARE_BLANK;
860 	i2c_header->Size	= (__u16)buffer_size;
861 	i2c_header->CheckSum	= cs;
862 	firmware_rec->Ver_Major	= OperationalCodeImageVersion.MajorVersion;
863 	firmware_rec->Ver_Minor	= OperationalCodeImageVersion.MinorVersion;
864 
865 	return 0;
866 }
867 
868 /* Try to figure out what type of I2c we have */
TIGetI2cTypeInBootMode(struct edgeport_serial * serial)869 static int TIGetI2cTypeInBootMode (struct edgeport_serial *serial)
870 {
871 	int status;
872 	__u8 data;
873 
874 	// Try to read type 2
875 	status = TIReadVendorRequestSync (serial->serial->dev,
876 					UMPC_MEMORY_READ,		// Request
877 					DTK_ADDR_SPACE_I2C_TYPE_II,	// wValue (Address type)
878 					0,		 		// wIndex
879 					&data,				// TransferBuffer
880 					0x01);				// TransferBufferLength
881 	if (status)
882 		dbg ("%s - read 2 status error = %d", __FUNCTION__, status);
883 	else
884 		dbg ("%s - read 2 data = 0x%x", __FUNCTION__, data);
885 	if ((!status) && data == 0x52) {
886 		dbg ("%s - ROM_TYPE_II", __FUNCTION__);
887 		serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
888 		return 0;
889 	}
890 
891 	// Try to read type 3
892 	status = TIReadVendorRequestSync (serial->serial->dev,
893 					UMPC_MEMORY_READ,		// Request
894 					DTK_ADDR_SPACE_I2C_TYPE_III,	// wValue (Address type)
895 					0,				// wIndex
896 					&data,				// TransferBuffer
897 					0x01);				// TransferBufferLength
898 	if (status)
899 		dbg ("%s - read 3 status error = %d", __FUNCTION__, status);
900 	else
901 		dbg ("%s - read 2 data = 0x%x", __FUNCTION__, data);
902 	if ((!status) && data == 0x52) {
903 		dbg ("%s - ROM_TYPE_III", __FUNCTION__);
904 		serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_III;
905 		return 0;
906 	}
907 
908 	dbg ("%s - Unknown", __FUNCTION__);
909 	serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
910 	return -ENODEV;
911 }
912 
TISendBulkTransferSync(struct usb_serial * serial,void * buffer,int length,int * num_sent)913 static int TISendBulkTransferSync (struct usb_serial *serial, void *buffer, int length, int *num_sent)
914 {
915 	int status;
916 
917 	status = usb_bulk_msg (serial->dev,
918 				usb_sndbulkpipe(serial->dev,
919 						serial->port[0].bulk_out_endpointAddress),
920 				buffer,
921 				length,
922 				num_sent,
923 				HZ);
924 	return status;
925 }
926 
927 /* Download given firmware image to the device (IN BOOT MODE) */
TIDownloadCodeImage(struct edgeport_serial * serial,__u8 * image,int image_length)928 static int TIDownloadCodeImage (struct edgeport_serial *serial, __u8 *image, int image_length)
929 {
930 	int status = 0;
931 	int pos;
932 	int transfer;
933 	int done;
934 
935 	// Transfer firmware image
936 	for (pos = 0; pos < image_length; ) {
937 		// Read the next buffer from file
938 		transfer = image_length - pos;
939 		if (transfer > EDGE_FW_BULK_MAX_PACKET_SIZE)
940 			transfer = EDGE_FW_BULK_MAX_PACKET_SIZE;
941 
942 		// Transfer data
943 		status = TISendBulkTransferSync (serial->serial, &image[pos], transfer, &done);
944 		if (status)
945 			break;
946 		// Advance buffer pointer
947 		pos += done;
948 	}
949 
950 	return status;
951 }
952 
953 // FIXME!!!
TIConfigureBootDevice(struct usb_device * dev)954 static int TIConfigureBootDevice (struct usb_device *dev)
955 {
956 	return 0;
957 }
958 
959 /**
960  * DownloadTIFirmware - Download run-time operating firmware to the TI5052
961  *
962  * This routine downloads the main operating code into the TI5052, using the
963  * boot code already burned into E2PROM or ROM.
964  */
TIDownloadFirmware(struct edgeport_serial * serial)965 static int TIDownloadFirmware (struct edgeport_serial *serial)
966 {
967 	int status = 0;
968 	int start_address;
969 	struct edge_ti_manuf_descriptor *ti_manuf_desc;
970 	struct usb_interface_descriptor *interface;
971 	int download_cur_ver;
972 	int download_new_ver;
973 
974 	/* This routine is entered by both the BOOT mode and the Download mode
975 	 * We can determine which code is running by the reading the config
976 	 * descriptor and if we have only one bulk pipe it is in boot mode
977 	 */
978 	serial->product_info.hardware_type = HARDWARE_TYPE_TIUMP;
979 
980 	/* Default to type 2 i2c */
981 	serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
982 
983 	status = TIChooseConfiguration (serial->serial->dev);
984 	if (status)
985 		return status;
986 
987 	interface = serial->serial->dev->config->interface->altsetting;
988 	if (!interface) {
989 		err ("%s - no interface set, error!", __FUNCTION__);
990 		return -ENODEV;
991 	}
992 
993 	// Setup initial mode -- the default mode 0 is TI_MODE_CONFIGURING
994 	// if we have more than one endpoint we are definitely in download mode
995 	if (interface->bNumEndpoints > 1)
996 		serial->product_info.TiMode = TI_MODE_DOWNLOAD;
997 	else
998 		// Otherwise we will remain in configuring mode
999 		serial->product_info.TiMode = TI_MODE_CONFIGURING;
1000 
1001 	// Save Download Version Number
1002 	OperationalCodeImageVersion.MajorVersion = PagableOperationalCodeImageVersion.MajorVersion;
1003 	OperationalCodeImageVersion.MinorVersion = PagableOperationalCodeImageVersion.MinorVersion;
1004 	OperationalCodeImageVersion.BuildNumber	 = PagableOperationalCodeImageVersion.BuildNumber;
1005 
1006 	/********************************************************************/
1007 	/* Download Mode */
1008 	/********************************************************************/
1009 	if (serial->product_info.TiMode == TI_MODE_DOWNLOAD) {
1010 		struct ti_i2c_desc *rom_desc;
1011 
1012 		dbg ("%s - <<<<<<<<<<<<<<<RUNNING IN DOWNLOAD MODE>>>>>>>>>>", __FUNCTION__);
1013 
1014 		status = TiValidateI2cImage (serial);
1015 		if (status) {
1016 			dbg ("%s - <<<<<<<<<<<<<<<DOWNLOAD MODE -- BAD I2C >>>>>>>>>>",
1017 			     __FUNCTION__);
1018 			return status;
1019 		}
1020 
1021 		/* Validate Hardware version number
1022 		 * Read Manufacturing Descriptor from TI Based Edgeport
1023 		 */
1024 		ti_manuf_desc = kmalloc (sizeof (*ti_manuf_desc), GFP_KERNEL);
1025 		if (!ti_manuf_desc) {
1026 			err ("%s - out of memory.", __FUNCTION__);
1027 			return -ENOMEM;
1028 		}
1029 		status = TIReadManufDescriptor (serial, (__u8 *)ti_manuf_desc);
1030 		if (status) {
1031 			kfree (ti_manuf_desc);
1032 			return status;
1033 		}
1034 
1035 		// Check version number of ION descriptor
1036 		if (!ignore_cpu_rev && TI_GET_CPU_REVISION(ti_manuf_desc->CpuRev_BoardRev) < 2) {
1037 			dbg ( "%s - Wrong CPU Rev %d (Must be 2)", __FUNCTION__,
1038 			     TI_GET_CPU_REVISION(ti_manuf_desc->CpuRev_BoardRev));
1039 			kfree (ti_manuf_desc);
1040 		   	return -EINVAL;
1041 		}
1042 
1043 		rom_desc = kmalloc (sizeof (*rom_desc), GFP_KERNEL);
1044 		if (!rom_desc) {
1045 			err ("%s - out of memory.", __FUNCTION__);
1046 			kfree (ti_manuf_desc);
1047 			return -ENOMEM;
1048 		}
1049 
1050 		// Search for type 2 record (firmware record)
1051 		if ((start_address = TIGetDescriptorAddress (serial, I2C_DESC_TYPE_FIRMWARE_BASIC, rom_desc)) != 0) {
1052 			struct ti_i2c_firmware_rec *firmware_version;
1053 			__u8 record;
1054 
1055 			dbg ("%s - Found Type FIRMWARE (Type 2) record", __FUNCTION__);
1056 
1057 			firmware_version = kmalloc (sizeof (*firmware_version), GFP_KERNEL);
1058 			if (!firmware_version) {
1059 				err ("%s - out of memory.", __FUNCTION__);
1060 				kfree (rom_desc);
1061 				kfree (ti_manuf_desc);
1062 				return -ENOMEM;
1063 			}
1064 
1065 			// Validate version number
1066 			// Read the descriptor data
1067 			status = TIReadRom (serial,
1068 					start_address+sizeof(struct ti_i2c_desc),
1069 					sizeof(struct ti_i2c_firmware_rec),
1070 					(__u8 *)firmware_version);
1071 			if (status) {
1072 				kfree (firmware_version);
1073 				kfree (rom_desc);
1074 				kfree (ti_manuf_desc);
1075 				return status;
1076 			}
1077 
1078 			// Check version number of download with current version in I2c
1079 			download_cur_ver = (firmware_version->Ver_Major << 8) +
1080 					   (firmware_version->Ver_Minor);
1081 			download_new_ver = (OperationalCodeImageVersion.MajorVersion << 8) +
1082 					   (OperationalCodeImageVersion.MinorVersion);
1083 
1084 			dbg ("%s - >>>Firmware Versions Device %d.%d  Driver %d.%d",
1085 			     __FUNCTION__,
1086 			     firmware_version->Ver_Major,
1087 			     firmware_version->Ver_Minor,
1088 			     OperationalCodeImageVersion.MajorVersion,
1089 			     OperationalCodeImageVersion.MinorVersion);
1090 
1091 			// Check if we have an old version in the I2C and update if necessary
1092 			if (download_cur_ver != download_new_ver) {
1093 				dbg ("%s - Update I2C Download from %d.%d to %d.%d",
1094 				     __FUNCTION__,
1095 				     firmware_version->Ver_Major,
1096 				     firmware_version->Ver_Minor,
1097 				     OperationalCodeImageVersion.MajorVersion,
1098 				     OperationalCodeImageVersion.MinorVersion);
1099 
1100 				// In order to update the I2C firmware we must change the type 2 record to type 0xF2.
1101 				// This will force the UMP to come up in Boot Mode.  Then while in boot mode, the driver
1102 				// will download the latest firmware (padded to 15.5k) into the UMP ram.
1103 				// And finally when the device comes back up in download mode the driver will cause
1104 				// the new firmware to be copied from the UMP Ram to I2C and the firmware will update
1105 				// the record type from 0xf2 to 0x02.
1106 
1107 				record = I2C_DESC_TYPE_FIRMWARE_BLANK;
1108 
1109 				// Change the I2C Firmware record type to 0xf2 to trigger an update
1110 				status = TIWriteRom (serial,
1111 							start_address,
1112 							sizeof(record),
1113 							&record);
1114 				if (status) {
1115 					kfree (firmware_version);
1116 					kfree (rom_desc);
1117 					kfree (ti_manuf_desc);
1118 					return status;
1119 				}
1120 
1121 				// verify the write -- must do this in order for write to
1122 				// complete before we do the hardware reset
1123 				status = TIReadRom (serial,
1124 							start_address,
1125 							sizeof(record),
1126 							&record);
1127 
1128 				if (status) {
1129 					kfree (firmware_version);
1130 					kfree (rom_desc);
1131 					kfree (ti_manuf_desc);
1132 					return status;
1133 				}
1134 
1135 				if (record != I2C_DESC_TYPE_FIRMWARE_BLANK) {
1136 					err ("%s - error resetting device", __FUNCTION__);
1137 					kfree (firmware_version);
1138 					kfree (rom_desc);
1139 					kfree (ti_manuf_desc);
1140 					return -ENODEV;
1141 				}
1142 
1143 				dbg ("%s - HARDWARE RESET", __FUNCTION__);
1144 
1145 				// Reset UMP -- Back to BOOT MODE
1146 				status = TISendVendorRequestSync (serial->serial->dev,
1147 								UMPC_HARDWARE_RESET,	// Request
1148 								0,			// wValue
1149 								0,			// wIndex
1150 								NULL,			// TransferBuffer
1151 								0);			// TransferBufferLength
1152 
1153 				dbg ( "%s - HARDWARE RESET return %d", __FUNCTION__, status);
1154 
1155 				/* return an error on purpose. */
1156 				kfree (firmware_version);
1157 				kfree (rom_desc);
1158 				kfree (ti_manuf_desc);
1159 				return -ENODEV;
1160 			}
1161 			kfree (firmware_version);
1162 		}
1163 		// Search for type 0xF2 record (firmware blank record)
1164 		else if ((start_address = TIGetDescriptorAddress (serial, I2C_DESC_TYPE_FIRMWARE_BLANK, rom_desc)) != 0) {
1165 			#define HEADER_SIZE	(sizeof(struct ti_i2c_desc) + sizeof(struct ti_i2c_firmware_rec))
1166 			__u8 *header;
1167 			__u8 *vheader;
1168 
1169 			header  = kmalloc (HEADER_SIZE, GFP_KERNEL);
1170 			if (!header) {
1171 				err ("%s - out of memory.", __FUNCTION__);
1172 				kfree (rom_desc);
1173 				kfree (ti_manuf_desc);
1174 				return -ENOMEM;
1175 			}
1176 
1177 			vheader = kmalloc (HEADER_SIZE, GFP_KERNEL);
1178 			if (!vheader) {
1179 				err ("%s - out of memory.", __FUNCTION__);
1180 				kfree (header);
1181 				kfree (rom_desc);
1182 				kfree (ti_manuf_desc);
1183 				return -ENOMEM;
1184 			}
1185 
1186 			dbg ("%s - Found Type BLANK FIRMWARE (Type F2) record", __FUNCTION__);
1187 
1188 			// In order to update the I2C firmware we must change the type 2 record to type 0xF2.
1189 			// This will force the UMP to come up in Boot Mode.  Then while in boot mode, the driver
1190 			// will download the latest firmware (padded to 15.5k) into the UMP ram.
1191 			// And finally when the device comes back up in download mode the driver will cause
1192 			// the new firmware to be copied from the UMP Ram to I2C and the firmware will update
1193 			// the record type from 0xf2 to 0x02.
1194 			status = BuildI2CFirmwareHeader(header);
1195 			if (status) {
1196 				kfree (vheader);
1197 				kfree (header);
1198 				kfree (rom_desc);
1199 				kfree (ti_manuf_desc);
1200 				return status;
1201 			}
1202 
1203 			// Update I2C with type 0xf2 record with correct size and checksum
1204 			status = TIWriteRom (serial,
1205 						start_address,
1206 						HEADER_SIZE,
1207 						header);
1208 			if (status) {
1209 				kfree (vheader);
1210 				kfree (header);
1211 				kfree (rom_desc);
1212 				kfree (ti_manuf_desc);
1213 				return status;
1214 			}
1215 
1216 			// verify the write -- must do this in order for write to
1217 			// complete before we do the hardware reset
1218 			status = TIReadRom (serial,
1219 						start_address,
1220 						HEADER_SIZE,
1221 						vheader);
1222 
1223 			if (status) {
1224 				dbg ("%s - can't read header back", __FUNCTION__);
1225 				kfree (vheader);
1226 				kfree (header);
1227 				kfree (rom_desc);
1228 				kfree (ti_manuf_desc);
1229 				return status;
1230 			}
1231 			if (memcmp(vheader, header, HEADER_SIZE)) {
1232 				dbg ("%s - write download record failed", __FUNCTION__);
1233 				kfree (vheader);
1234 				kfree (header);
1235 				kfree (rom_desc);
1236 				kfree (ti_manuf_desc);
1237 				return status;
1238 			}
1239 
1240 			kfree (vheader);
1241 			kfree (header);
1242 
1243 			dbg ("%s - Start firmware update", __FUNCTION__);
1244 
1245 			// Tell firmware to copy download image into I2C
1246 			status = TISendVendorRequestSync (serial->serial->dev,
1247 						UMPC_COPY_DNLD_TO_I2C,	// Request
1248 						0,			// wValue
1249 						0,			// wIndex
1250 						NULL,			// TransferBuffer
1251 						0);			// TransferBufferLength
1252 
1253 		  	dbg ("%s - Update complete 0x%x", __FUNCTION__, status);
1254 			if (status) {
1255 				dbg ("%s - UMPC_COPY_DNLD_TO_I2C failed", __FUNCTION__);
1256 				kfree (rom_desc);
1257 				kfree (ti_manuf_desc);
1258 				return status;
1259 			}
1260 		}
1261 
1262 		// The device is running the download code
1263 		kfree (rom_desc);
1264 		kfree (ti_manuf_desc);
1265 		return 0;
1266 	}
1267 
1268 	/********************************************************************/
1269 	/* Boot Mode */
1270 	/********************************************************************/
1271 	dbg ("%s - <<<<<<<<<<<<<<<RUNNING IN BOOT MODE>>>>>>>>>>>>>>>",
1272 	     __FUNCTION__);
1273 
1274 	// Configure the TI device so we can use the BULK pipes for download
1275 	status = TIConfigureBootDevice (serial->serial->dev);
1276 	if (status)
1277 		return status;
1278 
1279 	if (serial->serial->dev->descriptor.idVendor != USB_VENDOR_ID_ION) {
1280 		dbg ("%s - VID = 0x%x", __FUNCTION__,
1281 		     serial->serial->dev->descriptor.idVendor);
1282 		serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
1283 		goto StayInBootMode;
1284 	}
1285 
1286 	// We have an ION device (I2c Must be programmed)
1287 	// Determine I2C image type
1288 	if (TIGetI2cTypeInBootMode(serial)) {
1289 		goto StayInBootMode;
1290 	}
1291 
1292 	// Registry variable set?
1293 	if (TIStayInBootMode) {
1294 		dbg ("%s - TIStayInBootMode", __FUNCTION__);
1295 		goto StayInBootMode;
1296 	}
1297 
1298 	// Check for ION Vendor ID and that the I2C is valid
1299 	if (!TiValidateI2cImage(serial)) {
1300 		struct ti_i2c_image_header *header;
1301 		int i;
1302 		__u8 cs = 0;
1303 		__u8 *buffer;
1304 		int buffer_size;
1305 
1306 		/* Validate Hardware version number
1307 		 * Read Manufacturing Descriptor from TI Based Edgeport
1308 		 */
1309 		ti_manuf_desc = kmalloc (sizeof (*ti_manuf_desc), GFP_KERNEL);
1310 		if (!ti_manuf_desc) {
1311 			err ("%s - out of memory.", __FUNCTION__);
1312 			return -ENOMEM;
1313 		}
1314 		status = TIReadManufDescriptor (serial, (__u8 *)ti_manuf_desc);
1315 		if (status) {
1316 			kfree (ti_manuf_desc);
1317 			goto StayInBootMode;
1318 		}
1319 
1320 		// Check for version 2
1321 		if (!ignore_cpu_rev && TI_GET_CPU_REVISION(ti_manuf_desc->CpuRev_BoardRev) < 2) {
1322 			dbg ("%s - Wrong CPU Rev %d (Must be 2)", __FUNCTION__,
1323 			     TI_GET_CPU_REVISION(ti_manuf_desc->CpuRev_BoardRev));
1324 			kfree (ti_manuf_desc);
1325 			goto StayInBootMode;
1326 		}
1327 
1328 		kfree (ti_manuf_desc);
1329 
1330 		// In order to update the I2C firmware we must change the type 2 record to type 0xF2.
1331 		// This will force the UMP to come up in Boot Mode.  Then while in boot mode, the driver
1332 		// will download the latest firmware (padded to 15.5k) into the UMP ram.
1333 		// And finally when the device comes back up in download mode the driver will cause
1334 		// the new firmware to be copied from the UMP Ram to I2C and the firmware will update
1335 		// the record type from 0xf2 to 0x02.
1336 
1337 		/*
1338 		 * Do we really have to copy the whole firmware image,
1339 		 * or could we do this in place!
1340 		 */
1341 
1342 		// Allocate a 15.5k buffer + 3 byte header
1343 		buffer_size = (((1024 * 16) - 512) + sizeof(struct ti_i2c_image_header));
1344 		buffer = kmalloc (buffer_size, GFP_KERNEL);
1345 		if (!buffer) {
1346 			err ("%s - out of memory", __FUNCTION__);
1347 			return -ENOMEM;
1348 		}
1349 
1350 		// Initialize the buffer to 0xff (pad the buffer)
1351 		memset (buffer, 0xff, buffer_size);
1352 
1353 		memcpy (buffer, &PagableOperationalCodeImage[0], PagableOperationalCodeSize);
1354 
1355 		for(i = sizeof(struct ti_i2c_image_header); i < buffer_size; i++) {
1356 			cs = (__u8)(cs + buffer[i]);
1357 		}
1358 
1359 		header = (struct ti_i2c_image_header *)buffer;
1360 
1361 		// update length and checksum after padding
1362 		header->Length 	 = (__u16)(buffer_size - sizeof(struct ti_i2c_image_header));
1363 		header->CheckSum = cs;
1364 
1365 		// Download the operational code
1366 		dbg ("%s - Downloading operational code image (TI UMP)", __FUNCTION__);
1367 		status = TIDownloadCodeImage (serial, buffer, buffer_size);
1368 
1369 		kfree (buffer);
1370 
1371 		if (status) {
1372 	  		dbg ("%s - Error downloading operational code image", __FUNCTION__);
1373 			return status;
1374 		}
1375 
1376 		// Device will reboot
1377 		serial->product_info.TiMode = TI_MODE_TRANSITIONING;
1378 
1379   		dbg ("%s - Download successful -- Device rebooting...", __FUNCTION__);
1380 
1381 		/* return an error on purpose */
1382 		return -ENODEV;
1383 	}
1384 
1385 StayInBootMode:
1386 	// Eprom is invalid or blank stay in boot mode
1387 	dbg ("%s - <<<<<<<<<<<<<<<STAYING IN BOOT MODE>>>>>>>>>>>>", __FUNCTION__);
1388 	serial->product_info.TiMode = TI_MODE_BOOT;
1389 
1390 	return 0;
1391 }
1392 
1393 
TISetDtr(struct edgeport_port * port)1394 static int TISetDtr (struct edgeport_port *port)
1395 {
1396 	int port_number = port->port->number - port->port->serial->minor;
1397 
1398 	dbg ("%s", __FUNCTION__);
1399 	port->shadow_mcr |= MCR_DTR;
1400 
1401 	return TIWriteCommandSync (port->port->serial->dev,
1402 				UMPC_SET_CLR_DTR,
1403 				(__u8)(UMPM_UART1_PORT + port_number),
1404 				1,	/* set */
1405 				NULL,
1406 				0);
1407 }
1408 
TIClearDtr(struct edgeport_port * port)1409 static int TIClearDtr (struct edgeport_port *port)
1410 {
1411 	int port_number = port->port->number - port->port->serial->minor;
1412 
1413 	dbg ("%s", __FUNCTION__);
1414 	port->shadow_mcr &= ~MCR_DTR;
1415 
1416 	return TIWriteCommandSync (port->port->serial->dev,
1417 				UMPC_SET_CLR_DTR,
1418 				(__u8)(UMPM_UART1_PORT + port_number),
1419 				0,	/* clear */
1420 				NULL,
1421 				0);
1422 }
1423 
TISetRts(struct edgeport_port * port)1424 static int TISetRts (struct edgeport_port *port)
1425 {
1426 	int port_number = port->port->number - port->port->serial->minor;
1427 
1428 	dbg ("%s", __FUNCTION__);
1429 	port->shadow_mcr |= MCR_RTS;
1430 
1431 	return TIWriteCommandSync (port->port->serial->dev,
1432 				UMPC_SET_CLR_RTS,
1433 				(__u8)(UMPM_UART1_PORT + port_number),
1434 				1,	/* set */
1435 				NULL,
1436 				0);
1437 }
1438 
TIClearRts(struct edgeport_port * port)1439 static int TIClearRts (struct edgeport_port *port)
1440 {
1441 	int port_number = port->port->number - port->port->serial->minor;
1442 
1443 	dbg ("%s", __FUNCTION__);
1444 	port->shadow_mcr &= ~MCR_RTS;
1445 
1446 	return TIWriteCommandSync (port->port->serial->dev,
1447 				UMPC_SET_CLR_RTS,
1448 				(__u8)(UMPM_UART1_PORT + port_number),
1449 				0,	/* clear */
1450 				NULL,
1451 				0);
1452 }
1453 
TISetLoopBack(struct edgeport_port * port)1454 static int TISetLoopBack (struct edgeport_port *port)
1455 {
1456 	int port_number = port->port->number - port->port->serial->minor;
1457 
1458 	dbg ("%s", __FUNCTION__);
1459 
1460 	return TIWriteCommandSync (port->port->serial->dev,
1461 				UMPC_SET_CLR_LOOPBACK,
1462 				(__u8)(UMPM_UART1_PORT + port_number),
1463 				1,	/* set */
1464 				NULL,
1465 				0);
1466 }
1467 
TIClearLoopBack(struct edgeport_port * port)1468 static int TIClearLoopBack (struct edgeport_port *port)
1469 {
1470 	int port_number = port->port->number - port->port->serial->minor;
1471 
1472 	dbg ("%s", __FUNCTION__);
1473 
1474 	return TIWriteCommandSync (port->port->serial->dev,
1475 				UMPC_SET_CLR_LOOPBACK,
1476 				(__u8)(UMPM_UART1_PORT + port_number),
1477 				0,	/* clear */
1478 				NULL,
1479 				0);
1480 }
1481 
TISetBreak(struct edgeport_port * port)1482 static int TISetBreak (struct edgeport_port *port)
1483 {
1484 	int port_number = port->port->number - port->port->serial->minor;
1485 
1486 	dbg ("%s", __FUNCTION__);
1487 
1488 	return TIWriteCommandSync (port->port->serial->dev,
1489 				UMPC_SET_CLR_BREAK,
1490 				(__u8)(UMPM_UART1_PORT + port_number),
1491 				1,	/* set */
1492 				NULL,
1493 				0);
1494 }
1495 
TIClearBreak(struct edgeport_port * port)1496 static int TIClearBreak (struct edgeport_port *port)
1497 {
1498 	int port_number = port->port->number - port->port->serial->minor;
1499 
1500 	dbg ("%s", __FUNCTION__);
1501 
1502 	return TIWriteCommandSync (port->port->serial->dev,
1503 				UMPC_SET_CLR_BREAK,
1504 				(__u8)(UMPM_UART1_PORT + port_number),
1505 				0,	/* clear */
1506 				NULL,
1507 				0);
1508 }
1509 
TIRestoreMCR(struct edgeport_port * port,__u8 mcr)1510 static int TIRestoreMCR (struct edgeport_port *port, __u8 mcr)
1511 {
1512 	int status = 0;
1513 
1514 	dbg ("%s - %x", __FUNCTION__, mcr);
1515 
1516 	if (mcr & MCR_DTR)
1517 		status = TISetDtr (port);
1518 	else
1519 		status = TIClearDtr (port);
1520 
1521 	if (status)
1522 		return status;
1523 
1524 	if (mcr & MCR_RTS)
1525 		status = TISetRts (port);
1526 	else
1527 		status = TIClearRts (port);
1528 
1529 	if (status)
1530 		return status;
1531 
1532 	if (mcr & MCR_LOOPBACK)
1533 		status = TISetLoopBack (port);
1534 	else
1535 		status = TIClearLoopBack (port);
1536 
1537 	return status;
1538 }
1539 
1540 
1541 
1542 /* Convert TI LSR to standard UART flags */
MapLineStatus(__u8 ti_lsr)1543 static __u8 MapLineStatus (__u8 ti_lsr)
1544 {
1545 	__u8 lsr = 0;
1546 
1547 #define MAP_FLAG(flagUmp, flagUart)    \
1548 	if (ti_lsr & flagUmp) lsr |= flagUart;
1549 
1550 	MAP_FLAG(UMP_UART_LSR_OV_MASK, LSR_OVER_ERR)	/* overrun */
1551 	MAP_FLAG(UMP_UART_LSR_PE_MASK, LSR_PAR_ERR)	/* parity error */
1552 	MAP_FLAG(UMP_UART_LSR_FE_MASK, LSR_FRM_ERR)	/* framing error */
1553 	MAP_FLAG(UMP_UART_LSR_BR_MASK, LSR_BREAK)	/* break detected */
1554 	MAP_FLAG(UMP_UART_LSR_RX_MASK, LSR_RX_AVAIL)	/* receive data available */
1555 	MAP_FLAG(UMP_UART_LSR_TX_MASK, LSR_TX_EMPTY)	/* transmit holding register empty */
1556 
1557 #undef MAP_FLAG
1558 
1559 	return lsr;
1560 }
1561 
handle_new_msr(struct edgeport_port * edge_port,__u8 msr)1562 static void handle_new_msr (struct edgeport_port *edge_port, __u8 msr)
1563 {
1564 	struct async_icount *icount;
1565 
1566 	dbg ("%s - %02x", __FUNCTION__, msr);
1567 
1568 	if (msr & (EDGEPORT_MSR_DELTA_CTS | EDGEPORT_MSR_DELTA_DSR | EDGEPORT_MSR_DELTA_RI | EDGEPORT_MSR_DELTA_CD)) {
1569 		icount = &edge_port->icount;
1570 
1571 		/* update input line counters */
1572 		if (msr & EDGEPORT_MSR_DELTA_CTS)
1573 			icount->cts++;
1574 		if (msr & EDGEPORT_MSR_DELTA_DSR)
1575 			icount->dsr++;
1576 		if (msr & EDGEPORT_MSR_DELTA_CD)
1577 			icount->dcd++;
1578 		if (msr & EDGEPORT_MSR_DELTA_RI)
1579 			icount->rng++;
1580 		wake_up_interruptible (&edge_port->delta_msr_wait);
1581 	}
1582 
1583 	/* Save the new modem status */
1584 	edge_port->shadow_msr = msr & 0xf0;
1585 
1586 	return;
1587 }
1588 
handle_new_lsr(struct edgeport_port * edge_port,int lsr_data,__u8 lsr,__u8 data)1589 static void handle_new_lsr (struct edgeport_port *edge_port, int lsr_data, __u8 lsr, __u8 data)
1590 {
1591 	struct async_icount *icount;
1592 	__u8 new_lsr = (__u8)(lsr & (__u8)(LSR_OVER_ERR | LSR_PAR_ERR | LSR_FRM_ERR | LSR_BREAK));
1593 
1594 	dbg ("%s - %02x", __FUNCTION__, new_lsr);
1595 
1596 	edge_port->shadow_lsr = lsr;
1597 
1598 	if (new_lsr & LSR_BREAK) {
1599 		/*
1600 		 * Parity and Framing errors only count if they
1601 		 * occur exclusive of a break being received.
1602 		 */
1603 		new_lsr &= (__u8)(LSR_OVER_ERR | LSR_BREAK);
1604 	}
1605 
1606 	/* Place LSR data byte into Rx buffer */
1607 	if (lsr_data && edge_port->port->tty) {
1608 		tty_insert_flip_char(edge_port->port->tty, data, 0);
1609 		tty_flip_buffer_push(edge_port->port->tty);
1610 	}
1611 
1612 	/* update input line counters */
1613 	icount = &edge_port->icount;
1614 	if (new_lsr & LSR_BREAK)
1615 		icount->brk++;
1616 	if (new_lsr & LSR_OVER_ERR)
1617 		icount->overrun++;
1618 	if (new_lsr & LSR_PAR_ERR)
1619 		icount->parity++;
1620 	if (new_lsr & LSR_FRM_ERR)
1621 		icount->frame++;
1622 }
1623 
1624 
edge_interrupt_callback(struct urb * urb)1625 static void edge_interrupt_callback (struct urb *urb)
1626 {
1627 	struct edgeport_serial	*edge_serial = (struct edgeport_serial *)urb->context;
1628 	struct usb_serial_port *port;
1629 	struct edgeport_port *edge_port;
1630 	unsigned char *data = urb->transfer_buffer;
1631 	int length = urb->actual_length;
1632 	int port_number;
1633 	int function;
1634 	__u8 lsr;
1635 	__u8 msr;
1636 
1637 	dbg("%s", __FUNCTION__);
1638 
1639 	if (serial_paranoia_check (edge_serial->serial, __FUNCTION__)) {
1640 		return;
1641 	}
1642 
1643 	if (urb->status) {
1644 		dbg("%s - nonzero control read status received: %d", __FUNCTION__, urb->status);
1645 		return;
1646 	}
1647 
1648 	if (!length) {
1649 		dbg ("%s - no data in urb", __FUNCTION__);
1650 		return;
1651 	}
1652 
1653 	usb_serial_debug_data (__FILE__, __FUNCTION__, length, data);
1654 
1655 	if (length != 2) {
1656 		dbg ("%s - expecting packet of size 2, got %d", __FUNCTION__, length);
1657 		return;
1658 	}
1659 
1660 	port_number = TIUMP_GET_PORT_FROM_CODE (data[0]);
1661 	function    = TIUMP_GET_FUNC_FROM_CODE (data[0]);
1662 	dbg ("%s - port_number %d, function %d, info 0x%x",
1663 	     __FUNCTION__, port_number, function, data[1]);
1664 	port = &edge_serial->serial->port[port_number];
1665 	if (port_paranoia_check (port, __FUNCTION__)) {
1666 		dbg ("%s - change found for port that is not present",
1667 		     __FUNCTION__);
1668 		return;
1669 	}
1670 	edge_port = port->private;
1671 	if (!edge_port) {
1672 		dbg ("%s - edge_port not found", __FUNCTION__);
1673 		return;
1674 	}
1675 	switch (function) {
1676 	case TIUMP_INTERRUPT_CODE_LSR:
1677 		lsr = MapLineStatus(data[1]);
1678 		if (lsr & UMP_UART_LSR_DATA_MASK) {
1679 			/* Save the LSR event for bulk read completion routine */
1680 			dbg ("%s - LSR Event Port %u LSR Status = %02x",
1681 			     __FUNCTION__, port_number, lsr);
1682 			edge_port->lsr_event = 1;
1683 			edge_port->lsr_mask = lsr;
1684 		} else {
1685 			dbg ("%s - ===== Port %d LSR Status = %02x ======",
1686 			     __FUNCTION__, port_number, lsr);
1687 			handle_new_lsr (edge_port, 0, lsr, 0);
1688 		}
1689 		break;
1690 
1691 	case TIUMP_INTERRUPT_CODE_MSR:	// MSR
1692 		/* Copy MSR from UMP */
1693 		msr = data[1];
1694 		dbg ("%s - ===== Port %u MSR Status = %02x ======\n",
1695 		     __FUNCTION__, port_number, msr);
1696 		handle_new_msr (edge_port, msr);
1697 		break;
1698 
1699 	default:
1700 		err ("%s - Unknown Interrupt code from UMP %x\n",
1701 		     __FUNCTION__, data[1]);
1702 		break;
1703 
1704 	}
1705 }
1706 
edge_bulk_in_callback(struct urb * urb)1707 static void edge_bulk_in_callback (struct urb *urb)
1708 {
1709 	struct edgeport_port *edge_port = (struct edgeport_port *)urb->context;
1710 	unsigned char *data = urb->transfer_buffer;
1711 	struct tty_struct *tty;
1712 	int status;
1713 	int i;
1714 	int port_number;
1715 
1716 	dbg("%s", __FUNCTION__);
1717 
1718 	if (port_paranoia_check (edge_port->port, __FUNCTION__))
1719 		return;
1720 
1721 	if (urb->status) {
1722 		dbg ("%s - nonzero read bulk status received: %d",
1723 		     __FUNCTION__, urb->status);
1724 
1725 		if (urb->status == -EPIPE) {
1726 			/* clear any problem that might have happened on this pipe */
1727 			usb_clear_halt (edge_port->port->serial->dev, urb->pipe);
1728 			goto exit;
1729 		}
1730 		return;
1731 	}
1732 
1733 	port_number = edge_port->port->number - edge_port->port->serial->minor;
1734 
1735 	if (edge_port->lsr_event) {
1736 		edge_port->lsr_event = 0;
1737 		dbg ("%s ===== Port %u LSR Status = %02x, Data = %02x ======",
1738 		     __FUNCTION__, port_number, edge_port->lsr_mask, *data);
1739 		handle_new_lsr (edge_port, 1, edge_port->lsr_mask, *data);
1740 		/* Adjust buffer length/pointer */
1741 		--urb->actual_length;
1742 		++data;
1743 	}
1744 
1745 	tty = edge_port->port->tty;
1746 	if (tty && urb->actual_length) {
1747 		usb_serial_debug_data (__FILE__, __FUNCTION__, urb->actual_length, data);
1748 
1749 		if (edge_port->close_pending) {
1750 			dbg ("%s - close is pending, dropping data on the floor.", __FUNCTION__);
1751 		} else {
1752 			for (i = 0; i < urb->actual_length ; ++i) {
1753 				/* if we insert more than TTY_FLIPBUF_SIZE characters,
1754 				 * we drop them. */
1755 				if (tty->flip.count >= TTY_FLIPBUF_SIZE) {
1756 					tty_flip_buffer_push(tty);
1757 				}
1758 				/* this doesn't actually push the data through unless
1759 				 * tty->low_latency is set */
1760 				tty_insert_flip_char(tty, data[i], 0);
1761 			}
1762 			tty_flip_buffer_push(tty);
1763 		}
1764 		edge_port->icount.rx += urb->actual_length;
1765 	}
1766 
1767 exit:
1768 	/* continue always trying to read */
1769 	urb->dev = edge_port->port->serial->dev;
1770 	status = usb_submit_urb (urb);
1771 	if (status)
1772 		err ("%s - usb_submit_urb failed with result %d",
1773 		     __FUNCTION__, status);
1774 }
1775 
edge_bulk_out_callback(struct urb * urb)1776 static void edge_bulk_out_callback (struct urb *urb)
1777 {
1778 	struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
1779 	struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);
1780 	struct tty_struct *tty;
1781 
1782 	dbg ("%s - port %d", __FUNCTION__, port->number);
1783 
1784 	if (!serial) {
1785 		dbg ("%s - bad serial pointer, exiting", __FUNCTION__);
1786 		return;
1787 	}
1788 
1789 	if (urb->status) {
1790 		dbg ("%s - nonzero write bulk status received: %d",
1791 		     __FUNCTION__, urb->status);
1792 
1793 		if (urb->status == -EPIPE) {
1794 			/* clear any problem that might have happened on this pipe */
1795 			usb_clear_halt (serial->dev, urb->pipe);
1796 		}
1797 		return;
1798 	}
1799 
1800 	tty = port->tty;
1801 	if (tty) {
1802 		/* let the tty driver wakeup if it has a special write_wakeup function */
1803 		tty_wakeup(tty);
1804 	}
1805 }
1806 
edge_open(struct usb_serial_port * port,struct file * filp)1807 static int edge_open (struct usb_serial_port *port, struct file * filp)
1808 {
1809 	struct edgeport_port *edge_port = (struct edgeport_port *)port->private;
1810 	struct edgeport_serial *edge_serial;
1811 	struct usb_device *dev;
1812 	struct urb *urb;
1813 	int port_number;
1814 	int status;
1815 	u16 open_settings;
1816 	u8 transaction_timeout;
1817 
1818 	if (port_paranoia_check (port, __FUNCTION__))
1819 		return -ENODEV;
1820 
1821 	dbg("%s - port %d", __FUNCTION__, port->number);
1822 
1823 	if (edge_port == NULL)
1824 		return -ENODEV;
1825 
1826 	/* force low_latency on so that our tty_push actually forces the data through,
1827 	   otherwise it is scheduled, and with high data rates (like with OHCI) data
1828 	   can get lost. */
1829 	if (port->tty)
1830 		port->tty->low_latency = 1;
1831 
1832 	port_number = port->number - port->serial->minor;
1833 	switch (port_number) {
1834 		case 0:
1835 			edge_port->uart_base = UMPMEM_BASE_UART1;
1836 			edge_port->dma_address = UMPD_OEDB1_ADDRESS;
1837 			break;
1838 		case 1:
1839 			edge_port->uart_base = UMPMEM_BASE_UART2;
1840 			edge_port->dma_address = UMPD_OEDB2_ADDRESS;
1841 			break;
1842 		default:
1843 			err ("Unknown port number!!!");
1844 			return -ENODEV;
1845 	}
1846 
1847 	dbg ("%s - port_number = %d, uart_base = %04x, dma_address = %04x",
1848 	     __FUNCTION__, port_number, edge_port->uart_base, edge_port->dma_address);
1849 
1850 	dev = port->serial->dev;
1851 
1852 	memset (&(edge_port->icount), 0x00, sizeof(edge_port->icount));
1853 	init_waitqueue_head (&edge_port->delta_msr_wait);
1854 
1855 	/* turn off loopback */
1856 	status = TIClearLoopBack (edge_port);
1857 	if (status)
1858 		return status;
1859 
1860 	/* set up the port settings */
1861 	edge_set_termios (port, NULL);
1862 
1863 	/* open up the port */
1864 
1865 	/* milliseconds to timeout for DMA transfer */
1866 	transaction_timeout = 2;
1867 
1868 	edge_port->ump_read_timeout = max (20, ((transaction_timeout * 3) / 2) );
1869 
1870 	// milliseconds to timeout for DMA transfer
1871 	open_settings = (u8)(UMP_DMA_MODE_CONTINOUS |
1872 			     UMP_PIPE_TRANS_TIMEOUT_ENA |
1873 			     (transaction_timeout << 2));
1874 
1875 	dbg ("%s - Sending UMPC_OPEN_PORT", __FUNCTION__);
1876 
1877 	/* Tell TI to open and start the port */
1878 	status = TIWriteCommandSync (dev,
1879 					UMPC_OPEN_PORT,
1880 					(u8)(UMPM_UART1_PORT + port_number),
1881 					open_settings,
1882 					NULL,
1883 					0);
1884 	if (status)
1885 		return status;
1886 
1887 	/* Start the DMA? */
1888 	status = TIWriteCommandSync (dev,
1889 					UMPC_START_PORT,
1890 					(u8)(UMPM_UART1_PORT + port_number),
1891 					0,
1892 					NULL,
1893 					0);
1894 	if (status)
1895 		return status;
1896 
1897 	/* Clear TX and RX buffers in UMP */
1898 	status = TIPurgeDataSync (port, UMP_PORT_DIR_OUT | UMP_PORT_DIR_IN);
1899 	if (status)
1900 		return status;
1901 
1902 	/* Read Initial MSR */
1903 	status = TIReadVendorRequestSync (dev,
1904 					UMPC_READ_MSR,	// Request
1905 					0,		// wValue
1906 					(__u16)(UMPM_UART1_PORT + port_number),	// wIndex (Address)
1907 					&edge_port->shadow_msr,			// TransferBuffer
1908 					1);					// TransferBufferLength
1909 	if (status)
1910 		return status;
1911 
1912 	dbg ("ShadowMSR 0x%X", edge_port->shadow_msr);
1913 
1914 	edge_serial = edge_port->edge_serial;
1915 	if (edge_serial->num_ports_open == 0) {
1916 		dbg ("%s - setting up bulk in urb", __FUNCTION__);
1917 		/* we are the first port to be opened, let's post the interrupt urb */
1918 		urb = edge_serial->serial->port[0].interrupt_in_urb;
1919 		if (!urb) {
1920 			err ("%s - no interrupt urb present, exiting", __FUNCTION__);
1921 			return -EINVAL;
1922 		}
1923 		urb->complete = edge_interrupt_callback;
1924 		urb->context = edge_serial;
1925 		urb->dev = dev;
1926 		status = usb_submit_urb (urb);
1927 		if (status) {
1928 			err ("%s - usb_submit_urb failed with value %d", __FUNCTION__, status);
1929 			return status;
1930 		}
1931 	}
1932 
1933 	/*
1934 	 * reset the data toggle on the bulk endpoints to work around bug in
1935 	 * host controllers where things get out of sync some times
1936 	 */
1937 	usb_clear_halt (dev, port->write_urb->pipe);
1938 	usb_clear_halt (dev, port->read_urb->pipe);
1939 
1940 	/* start up our bulk read urb */
1941 	urb = port->read_urb;
1942 	if (!urb) {
1943 		err ("%s - no read urb present, exiting", __FUNCTION__);
1944 		return -EINVAL;
1945 	}
1946 	urb->complete = edge_bulk_in_callback;
1947 	urb->context = edge_port;
1948 	urb->dev = dev;
1949 	status = usb_submit_urb (urb);
1950 	if (status) {
1951 		err ("%s - read bulk usb_submit_urb failed with value %d", __FUNCTION__, status);
1952 		return status;
1953 	}
1954 
1955 	++edge_serial->num_ports_open;
1956 
1957 	dbg("%s - exited", __FUNCTION__);
1958 
1959 	return 0;
1960 }
1961 
edge_close(struct usb_serial_port * port,struct file * filp)1962 static void edge_close (struct usb_serial_port *port, struct file * filp)
1963 {
1964 	struct usb_serial *serial;
1965 	struct edgeport_serial *edge_serial;
1966 	struct edgeport_port *edge_port;
1967 	int port_number;
1968 	int status;
1969 
1970 	if (port_paranoia_check (port, __FUNCTION__))
1971 		return;
1972 
1973 	dbg("%s - port %d", __FUNCTION__, port->number);
1974 
1975 	serial = get_usb_serial (port, __FUNCTION__);
1976 	if (!serial)
1977 		return;
1978 
1979 	edge_serial = (struct edgeport_serial *)serial->private;
1980 	edge_port = (struct edgeport_port *)port->private;
1981 	if ((edge_serial == NULL) || (edge_port == NULL))
1982 		return;
1983 
1984 	if (serial->dev) {
1985 		/* The bulkreadcompletion routine will check
1986 		 * this flag and dump add read data */
1987 		edge_port->close_pending = 1;
1988 
1989 		/* chase the port close */
1990 		TIChasePort (edge_port);
1991 
1992 		usb_unlink_urb (port->read_urb);
1993 
1994 		/* assuming we can still talk to the device,
1995 		 * send a close port command to it */
1996 		dbg("%s - send umpc_close_port", __FUNCTION__);
1997 		port_number = port->number - port->serial->minor;
1998 		status = TIWriteCommandSync (port->serial->dev,
1999 					     UMPC_CLOSE_PORT,
2000 					     (__u8)(UMPM_UART1_PORT + port_number),
2001 					     0,
2002 					     NULL,
2003 					     0);
2004 		--edge_port->edge_serial->num_ports_open;
2005 		if (edge_port->edge_serial->num_ports_open <= 0) {
2006 			/* last port is now closed, let's shut down our interrupt urb */
2007 			usb_unlink_urb (serial->port[0].interrupt_in_urb);
2008 			edge_port->edge_serial->num_ports_open = 0;
2009 		}
2010 	edge_port->close_pending = 0;
2011 	}
2012 
2013 	dbg("%s - exited", __FUNCTION__);
2014 }
2015 
edge_write(struct usb_serial_port * port,int from_user,const unsigned char * data,int count)2016 static int edge_write (struct usb_serial_port *port, int from_user, const unsigned char *data, int count)
2017 {
2018 	struct usb_serial *serial = port->serial;
2019 	struct edgeport_port *edge_port = port->private;
2020 	int result;
2021 
2022 	dbg("%s - port %d", __FUNCTION__, port->number);
2023 
2024 	if (count == 0) {
2025 		dbg("%s - write request of 0 bytes", __FUNCTION__);
2026 		return 0;
2027 	}
2028 
2029 	if (edge_port == NULL)
2030 		return -ENODEV;
2031 	if (edge_port->close_pending == 1)
2032 		return -ENODEV;
2033 
2034 	if (port->write_urb->status == -EINPROGRESS) {
2035 		dbg ("%s - already writing", __FUNCTION__);
2036 		return 0;
2037 	}
2038 
2039 	count = min (count, port->bulk_out_size);
2040 
2041 	if (from_user) {
2042 		if (copy_from_user(port->write_urb->transfer_buffer, data, count))
2043 			return -EFAULT;
2044 	} else {
2045 		memcpy (port->write_urb->transfer_buffer, data, count);
2046 	}
2047 
2048 	usb_serial_debug_data (__FILE__, __FUNCTION__, count, port->write_urb->transfer_buffer);
2049 
2050 	/* set up our urb */
2051 	usb_fill_bulk_urb (port->write_urb, serial->dev,
2052 			   usb_sndbulkpipe (serial->dev,
2053 					    port->bulk_out_endpointAddress),
2054 			   port->write_urb->transfer_buffer, count,
2055 			   edge_bulk_out_callback,
2056 			   port);
2057 
2058 	/* send the data out the bulk port */
2059 	result = usb_submit_urb(port->write_urb);
2060 	if (result)
2061 		err("%s - failed submitting write urb, error %d", __FUNCTION__, result);
2062 	else
2063 		result = count;
2064 
2065 	if (result > 0)
2066 		edge_port->icount.tx += count;
2067 
2068 	return result;
2069 }
2070 
edge_write_room(struct usb_serial_port * port)2071 static int edge_write_room (struct usb_serial_port *port)
2072 {
2073 	struct edgeport_port *edge_port = (struct edgeport_port *)(port->private);
2074 	int room = 0;
2075 
2076 	dbg("%s", __FUNCTION__);
2077 
2078 	if (edge_port == NULL)
2079 		return -ENODEV;
2080 	if (edge_port->close_pending == 1)
2081 		return -ENODEV;
2082 
2083 	dbg("%s - port %d", __FUNCTION__, port->number);
2084 
2085 	if (port->write_urb->status != -EINPROGRESS)
2086 		room = port->bulk_out_size;
2087 
2088 	dbg("%s - returns %d", __FUNCTION__, room);
2089 	return room;
2090 }
2091 
edge_chars_in_buffer(struct usb_serial_port * port)2092 static int edge_chars_in_buffer (struct usb_serial_port *port)
2093 {
2094 	struct edgeport_port *edge_port = (struct edgeport_port *)(port->private);
2095 	int chars = 0;
2096 
2097 	dbg("%s", __FUNCTION__);
2098 
2099 	if (edge_port == NULL)
2100 		return -ENODEV;
2101 	if (edge_port->close_pending == 1)
2102 		return -ENODEV;
2103 
2104 	dbg("%s - port %d", __FUNCTION__, port->number);
2105 
2106 	if (port->write_urb->status == -EINPROGRESS)
2107 		chars = port->write_urb->transfer_buffer_length;
2108 
2109 	dbg ("%s - returns %d", __FUNCTION__, chars);
2110 	return chars;
2111 }
2112 
edge_throttle(struct usb_serial_port * port)2113 static void edge_throttle (struct usb_serial_port *port)
2114 {
2115 	struct edgeport_port *edge_port = (struct edgeport_port *)(port->private);
2116 	struct tty_struct *tty;
2117 	int status;
2118 
2119 	dbg("%s - port %d", __FUNCTION__, port->number);
2120 
2121 	if (edge_port == NULL)
2122 		return;
2123 
2124 	tty = port->tty;
2125 	if (!tty) {
2126 		dbg ("%s - no tty available", __FUNCTION__);
2127 		return;
2128 	}
2129 	/* if we are implementing XON/XOFF, send the stop character */
2130 	if (I_IXOFF(tty)) {
2131 		unsigned char stop_char = STOP_CHAR(tty);
2132 		status = edge_write (port, 0, &stop_char, 1);
2133 		if (status <= 0) {
2134 			return;
2135 		}
2136 	}
2137 
2138 	/* if we are implementing RTS/CTS, toggle that line */
2139 	if (tty->termios->c_cflag & CRTSCTS) {
2140 		status = TIClearRts (edge_port);
2141 	}
2142 
2143 	usb_unlink_urb (port->read_urb);
2144 }
2145 
edge_unthrottle(struct usb_serial_port * port)2146 static void edge_unthrottle (struct usb_serial_port *port)
2147 {
2148 	struct edgeport_port *edge_port = (struct edgeport_port *)(port->private);
2149 	struct tty_struct *tty;
2150 	int status;
2151 
2152 	dbg("%s - port %d", __FUNCTION__, port->number);
2153 
2154 	if (edge_port == NULL)
2155 		return;
2156 
2157 	tty = port->tty;
2158 	if (!tty) {
2159 		dbg ("%s - no tty available", __FUNCTION__);
2160 		return;
2161 	}
2162 
2163 	/* if we are implementing XON/XOFF, send the start character */
2164 	if (I_IXOFF(tty)) {
2165 		unsigned char start_char = START_CHAR(tty);
2166 		status = edge_write (port, 0, &start_char, 1);
2167 		if (status <= 0) {
2168 			return;
2169 		}
2170 	}
2171 
2172 	/* if we are implementing RTS/CTS, toggle that line */
2173 	if (tty->termios->c_cflag & CRTSCTS) {
2174 		status = TISetRts (edge_port);
2175 	}
2176 
2177 	port->read_urb->dev = port->serial->dev;
2178 	status = usb_submit_urb (port->read_urb);
2179 	if (status) {
2180 		err ("%s - usb_submit_urb failed with value %d", __FUNCTION__, status);
2181 	}
2182 }
2183 
2184 
change_port_settings(struct edgeport_port * edge_port,struct termios * old_termios)2185 static void change_port_settings (struct edgeport_port *edge_port, struct termios *old_termios)
2186 {
2187 	struct ump_uart_config *config;
2188 	struct tty_struct *tty;
2189 	int baud;
2190 	int round;
2191 	unsigned cflag;
2192 	int status;
2193 	int port_number = edge_port->port->number - edge_port->port->serial->minor;
2194 
2195 	dbg("%s - port %d", __FUNCTION__, edge_port->port->number);
2196 
2197 	tty = edge_port->port->tty;
2198 	if ((!tty) ||
2199 	    (!tty->termios)) {
2200 		dbg("%s - no tty structures", __FUNCTION__);
2201 		return;
2202 	}
2203 
2204 	config = kmalloc (sizeof (*config), GFP_KERNEL);
2205 	if (!config) {
2206 		err ("%s - out of memory", __FUNCTION__);
2207 		return;
2208 	}
2209 
2210 	cflag = tty->termios->c_cflag;
2211 
2212 	config->wFlags = 0;
2213 
2214 	/* These flags must be set */
2215 	config->wFlags |= UMP_MASK_UART_FLAGS_RECEIVE_MS_INT;
2216 	config->wFlags |= UMP_MASK_UART_FLAGS_AUTO_START_ON_ERR;
2217 	config->bUartMode = 0;
2218 
2219 	switch (cflag & CSIZE) {
2220 		case CS5:
2221 			    config->bDataBits = UMP_UART_CHAR5BITS;
2222 			    dbg ("%s - data bits = 5", __FUNCTION__);
2223 			    break;
2224 		case CS6:
2225 			    config->bDataBits = UMP_UART_CHAR6BITS;
2226 			    dbg ("%s - data bits = 6", __FUNCTION__);
2227 			    break;
2228 		case CS7:
2229 			    config->bDataBits = UMP_UART_CHAR7BITS;
2230 			    dbg ("%s - data bits = 7", __FUNCTION__);
2231 			    break;
2232 		default:
2233 		case CS8:
2234 			    config->bDataBits = UMP_UART_CHAR8BITS;
2235 			    dbg ("%s - data bits = 8", __FUNCTION__);
2236 			    break;
2237 	}
2238 
2239 	if (cflag & PARENB) {
2240 		if (cflag & PARODD) {
2241 			config->wFlags |= UMP_MASK_UART_FLAGS_PARITY;
2242 			config->bParity = UMP_UART_ODDPARITY;
2243 			dbg("%s - parity = odd", __FUNCTION__);
2244 		} else {
2245 			config->wFlags |= UMP_MASK_UART_FLAGS_PARITY;
2246 			config->bParity = UMP_UART_EVENPARITY;
2247 			dbg("%s - parity = even", __FUNCTION__);
2248 		}
2249 	} else {
2250 		config->bParity = UMP_UART_NOPARITY;
2251 		dbg("%s - parity = none", __FUNCTION__);
2252 	}
2253 
2254 	if (cflag & CSTOPB) {
2255 		config->bStopBits = UMP_UART_STOPBIT2;
2256 		dbg("%s - stop bits = 2", __FUNCTION__);
2257 	} else {
2258 		config->bStopBits = UMP_UART_STOPBIT1;
2259 		dbg("%s - stop bits = 1", __FUNCTION__);
2260 	}
2261 
2262 	/* figure out the flow control settings */
2263 	if (cflag & CRTSCTS) {
2264 		config->wFlags |= UMP_MASK_UART_FLAGS_OUT_X_CTS_FLOW;
2265 		config->wFlags |= UMP_MASK_UART_FLAGS_RTS_FLOW;
2266 		dbg("%s - RTS/CTS is enabled", __FUNCTION__);
2267 	} else {
2268 		dbg("%s - RTS/CTS is disabled", __FUNCTION__);
2269 	}
2270 
2271 	/* if we are implementing XON/XOFF, set the start and stop character in the device */
2272 	if (I_IXOFF(tty) || I_IXON(tty)) {
2273 		config->cXon  = START_CHAR(tty);
2274 		config->cXoff = STOP_CHAR(tty);
2275 
2276 		/* if we are implementing INBOUND XON/XOFF */
2277 		if (I_IXOFF(tty)) {
2278 			config->wFlags |= UMP_MASK_UART_FLAGS_IN_X;
2279 			dbg ("%s - INBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x",
2280 			     __FUNCTION__, config->cXon, config->cXoff);
2281 		} else {
2282 			dbg ("%s - INBOUND XON/XOFF is disabled", __FUNCTION__);
2283 		}
2284 
2285 		/* if we are implementing OUTBOUND XON/XOFF */
2286 		if (I_IXON(tty)) {
2287 			config->wFlags |= UMP_MASK_UART_FLAGS_OUT_X;
2288 			dbg ("%s - OUTBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x",
2289 			     __FUNCTION__, config->cXon, config->cXoff);
2290 		} else {
2291 			dbg ("%s - OUTBOUND XON/XOFF is disabled", __FUNCTION__);
2292 		}
2293 	}
2294 
2295 	/* Round the baud rate */
2296 	baud = tty_get_baud_rate(tty);
2297 	if (!baud) {
2298 		/* pick a default, any default... */
2299 		baud = 9600;
2300 	}
2301 	config->wBaudRate = (__u16)(461550L / baud);
2302 	round = 4615500L / baud;
2303 	if ((round - (config->wBaudRate * 10)) >= 5)
2304 		config->wBaudRate++;
2305 
2306 	dbg ("%s - baud rate = %d, wBaudRate = %d", __FUNCTION__, baud, config->wBaudRate);
2307 
2308 	dbg ("wBaudRate:   %d", (int)(461550L / config->wBaudRate));
2309 	dbg ("wFlags:    0x%x", config->wFlags);
2310 	dbg ("bDataBits:   %d", config->bDataBits);
2311 	dbg ("bParity:     %d", config->bParity);
2312 	dbg ("bStopBits:   %d", config->bStopBits);
2313 	dbg ("cXon:        %d", config->cXon);
2314 	dbg ("cXoff:       %d", config->cXoff);
2315 	dbg ("bUartMode:   %d", config->bUartMode);
2316 
2317 	/* move the word values into big endian mode */
2318 	cpu_to_be16s (&config->wFlags);
2319 	cpu_to_be16s (&config->wBaudRate);
2320 
2321 	status = TIWriteCommandSync (edge_port->port->serial->dev,
2322 				UMPC_SET_CONFIG,
2323 				(__u8)(UMPM_UART1_PORT + port_number),
2324 				0,
2325 				(__u8 *)config,
2326 				sizeof(*config));
2327 	if (status) {
2328 		dbg ("%s - error %d when trying to write config to device",
2329 		     __FUNCTION__, status);
2330 	}
2331 
2332 	kfree (config);
2333 
2334 	return;
2335 }
2336 
edge_set_termios(struct usb_serial_port * port,struct termios * old_termios)2337 static void edge_set_termios (struct usb_serial_port *port, struct termios *old_termios)
2338 {
2339 	struct edgeport_port *edge_port = (struct edgeport_port *)(port->private);
2340 	struct tty_struct *tty = port->tty;
2341 	unsigned int cflag;
2342 
2343 	if (!port->tty || !port->tty->termios) {
2344 		dbg ("%s - no tty or termios", __FUNCTION__);
2345 		return;
2346 	}
2347 
2348 	cflag = tty->termios->c_cflag;
2349 	/* check that they really want us to change something */
2350 	if (old_termios) {
2351 		if ((cflag == old_termios->c_cflag) &&
2352 		    (RELEVANT_IFLAG(tty->termios->c_iflag) == RELEVANT_IFLAG(old_termios->c_iflag))) {
2353 			dbg ("%s - nothing to change", __FUNCTION__);
2354 			return;
2355 		}
2356 	}
2357 
2358 	dbg("%s - clfag %08x iflag %08x", __FUNCTION__,
2359 	    tty->termios->c_cflag,
2360 	    RELEVANT_IFLAG(tty->termios->c_iflag));
2361 	if (old_termios) {
2362 		dbg("%s - old clfag %08x old iflag %08x", __FUNCTION__,
2363 		    old_termios->c_cflag,
2364 		    RELEVANT_IFLAG(old_termios->c_iflag));
2365 	}
2366 
2367 	dbg("%s - port %d", __FUNCTION__, port->number);
2368 
2369 	if (edge_port == NULL)
2370 		return;
2371 
2372 	/* change the port settings to the new ones specified */
2373 	change_port_settings (edge_port, old_termios);
2374 
2375 	return;
2376 }
2377 
set_modem_info(struct edgeport_port * edge_port,unsigned int cmd,unsigned int * value)2378 static int set_modem_info (struct edgeport_port *edge_port, unsigned int cmd, unsigned int *value)
2379 {
2380 	unsigned int mcr = edge_port->shadow_mcr;
2381 	unsigned int arg;
2382 
2383 	if (copy_from_user(&arg, value, sizeof(int)))
2384 		return -EFAULT;
2385 
2386 	switch (cmd) {
2387 		case TIOCMBIS:
2388 			if (arg & TIOCM_RTS)
2389 				mcr |= MCR_RTS;
2390 			if (arg & TIOCM_DTR)
2391 				mcr |= MCR_RTS;
2392 			if (arg & TIOCM_LOOP)
2393 				mcr |= MCR_LOOPBACK;
2394 			break;
2395 
2396 		case TIOCMBIC:
2397 			if (arg & TIOCM_RTS)
2398 				mcr &= ~MCR_RTS;
2399 			if (arg & TIOCM_DTR)
2400 				mcr &= ~MCR_RTS;
2401 			if (arg & TIOCM_LOOP)
2402 				mcr &= ~MCR_LOOPBACK;
2403 			break;
2404 
2405 		case TIOCMSET:
2406 			/* turn off the RTS and DTR and LOOPBACK
2407 			 * and then only turn on what was asked to */
2408 			mcr &=  ~(MCR_RTS | MCR_DTR | MCR_LOOPBACK);
2409 			mcr |= ((arg & TIOCM_RTS) ? MCR_RTS : 0);
2410 			mcr |= ((arg & TIOCM_DTR) ? MCR_DTR : 0);
2411 			mcr |= ((arg & TIOCM_LOOP) ? MCR_LOOPBACK : 0);
2412 			break;
2413 	}
2414 
2415 	edge_port->shadow_mcr = mcr;
2416 
2417 	TIRestoreMCR (edge_port, mcr);
2418 
2419 	return 0;
2420 }
2421 
get_modem_info(struct edgeport_port * edge_port,unsigned int * value)2422 static int get_modem_info (struct edgeport_port *edge_port, unsigned int *value)
2423 {
2424 	unsigned int result = 0;
2425 	unsigned int msr = edge_port->shadow_msr;
2426 	unsigned int mcr = edge_port->shadow_mcr;
2427 
2428 	result = ((mcr & MCR_DTR)	? TIOCM_DTR: 0)	  /* 0x002 */
2429 		  | ((mcr & MCR_RTS)	? TIOCM_RTS: 0)   /* 0x004 */
2430 		  | ((msr & EDGEPORT_MSR_CTS)	? TIOCM_CTS: 0)   /* 0x020 */
2431 		  | ((msr & EDGEPORT_MSR_CD)	? TIOCM_CAR: 0)   /* 0x040 */
2432 		  | ((msr & EDGEPORT_MSR_RI)	? TIOCM_RI:  0)   /* 0x080 */
2433 		  | ((msr & EDGEPORT_MSR_DSR)	? TIOCM_DSR: 0);  /* 0x100 */
2434 
2435 
2436 	dbg("%s -- %x", __FUNCTION__, result);
2437 
2438 	if (copy_to_user(value, &result, sizeof(int)))
2439 		return -EFAULT;
2440 	return 0;
2441 }
2442 
get_serial_info(struct edgeport_port * edge_port,struct serial_struct * retinfo)2443 static int get_serial_info (struct edgeport_port *edge_port, struct serial_struct * retinfo)
2444 {
2445 	struct serial_struct tmp;
2446 
2447 	if (!retinfo)
2448 		return -EFAULT;
2449 
2450 	memset(&tmp, 0, sizeof(tmp));
2451 
2452 	tmp.type		= PORT_16550A;
2453 	tmp.line		= edge_port->port->serial->minor;
2454 	tmp.port		= edge_port->port->number;
2455 	tmp.irq			= 0;
2456 	tmp.flags		= ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
2457 	tmp.xmit_fifo_size	= edge_port->port->bulk_out_size;
2458 	tmp.baud_base		= 9600;
2459 	tmp.close_delay		= 5*HZ;
2460 	tmp.closing_wait	= 30*HZ;
2461 //	tmp.custom_divisor	= state->custom_divisor;
2462 //	tmp.hub6		= state->hub6;
2463 //	tmp.io_type		= state->io_type;
2464 
2465 
2466 	if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2467 		return -EFAULT;
2468 	return 0;
2469 }
2470 
edge_ioctl(struct usb_serial_port * port,struct file * file,unsigned int cmd,unsigned long arg)2471 static int edge_ioctl (struct usb_serial_port *port, struct file *file, unsigned int cmd, unsigned long arg)
2472 {
2473 	struct edgeport_port *edge_port = (struct edgeport_port *)(port->private);
2474 	struct async_icount cnow;
2475 	struct async_icount cprev;
2476 
2477 	dbg("%s - port %d, cmd = 0x%x", __FUNCTION__, port->number, cmd);
2478 
2479 	switch (cmd) {
2480 		case TIOCINQ:
2481 			dbg("%s - (%d) TIOCINQ", __FUNCTION__, port->number);
2482 //			return get_number_bytes_avail(edge_port, (unsigned int *) arg);
2483 			break;
2484 
2485 		case TIOCSERGETLSR:
2486 			dbg("%s - (%d) TIOCSERGETLSR", __FUNCTION__, port->number);
2487 //			return get_lsr_info(edge_port, (unsigned int *) arg);
2488 			break;
2489 
2490 		case TIOCMBIS:
2491 		case TIOCMBIC:
2492 		case TIOCMSET:
2493 			dbg("%s - (%d) TIOCMSET/TIOCMBIC/TIOCMSET", __FUNCTION__, port->number);
2494 			return set_modem_info(edge_port, cmd, (unsigned int *) arg);
2495 			break;
2496 
2497 		case TIOCMGET:
2498 			dbg("%s - (%d) TIOCMGET", __FUNCTION__, port->number);
2499 			return get_modem_info(edge_port, (unsigned int *) arg);
2500 			break;
2501 
2502 		case TIOCGSERIAL:
2503 			dbg("%s - (%d) TIOCGSERIAL", __FUNCTION__, port->number);
2504 			return get_serial_info(edge_port, (struct serial_struct *) arg);
2505 			break;
2506 
2507 		case TIOCSSERIAL:
2508 			dbg("%s - (%d) TIOCSSERIAL", __FUNCTION__, port->number);
2509 			break;
2510 
2511 		case TIOCMIWAIT:
2512 			dbg("%s - (%d) TIOCMIWAIT", __FUNCTION__, port->number);
2513 			cprev = edge_port->icount;
2514 			while (1) {
2515 				interruptible_sleep_on(&edge_port->delta_msr_wait);
2516 				/* see if a signal did it */
2517 				if (signal_pending(current))
2518 					return -ERESTARTSYS;
2519 				cnow = edge_port->icount;
2520 				if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
2521 				    cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
2522 					return -EIO; /* no change => error */
2523 				if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
2524 				    ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
2525 				    ((arg & TIOCM_CD)  && (cnow.dcd != cprev.dcd)) ||
2526 				    ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)) ) {
2527 					return 0;
2528 				}
2529 				cprev = cnow;
2530 			}
2531 			/* not reached */
2532 			break;
2533 
2534 		case TIOCGICOUNT:
2535 			dbg ("%s - (%d) TIOCGICOUNT RX=%d, TX=%d", __FUNCTION__,
2536 			     port->number, edge_port->icount.rx, edge_port->icount.tx);
2537 			if (copy_to_user((void *)arg, &edge_port->icount, sizeof(edge_port->icount)))
2538 				return -EFAULT;
2539 			return 0;
2540 	}
2541 
2542 	return -ENOIOCTLCMD;
2543 }
2544 
edge_break(struct usb_serial_port * port,int break_state)2545 static void edge_break (struct usb_serial_port *port, int break_state)
2546 {
2547 	struct edgeport_port *edge_port = (struct edgeport_port *)(port->private);
2548 	int status;
2549 
2550 	dbg ("%s - state = %d", __FUNCTION__, break_state);
2551 
2552 	/* chase the port close */
2553 	TIChasePort (edge_port);
2554 
2555 	if (break_state == -1) {
2556 		status = TISetBreak (edge_port);
2557 	} else {
2558 		status = TIClearBreak (edge_port);
2559 	}
2560 	if (status) {
2561 		dbg ("%s - error %d sending break set/clear command.",
2562 		     __FUNCTION__, status);
2563 	}
2564 }
2565 
edge_startup(struct usb_serial * serial)2566 static int edge_startup (struct usb_serial *serial)
2567 {
2568 	struct edgeport_serial *edge_serial;
2569 	struct edgeport_port *edge_port;
2570 	struct usb_device *dev;
2571 	int status;
2572 	int i;
2573 
2574 	dev = serial->dev;
2575 
2576 	/* create our private serial structure */
2577 	edge_serial = kmalloc (sizeof(struct edgeport_serial), GFP_KERNEL);
2578 	if (edge_serial == NULL) {
2579 		err("%s - Out of memory", __FUNCTION__);
2580 		return -ENOMEM;
2581 	}
2582 	memset (edge_serial, 0, sizeof(struct edgeport_serial));
2583 	edge_serial->serial = serial;
2584 	serial->private = edge_serial;
2585 
2586 	status = TIDownloadFirmware (edge_serial);
2587 	if (status) {
2588 		kfree (edge_serial);
2589 		return status;
2590 	}
2591 
2592 	/* set up our port private structures */
2593 	for (i = 0; i < serial->num_ports; ++i) {
2594 		edge_port = kmalloc (sizeof(struct edgeport_port), GFP_KERNEL);
2595 		if (edge_port == NULL) {
2596 			err("%s - Out of memory", __FUNCTION__);
2597 			return -ENOMEM;
2598 		}
2599 		memset (edge_port, 0, sizeof(struct edgeport_port));
2600 		edge_port->port = &serial->port[i];
2601 		edge_port->edge_serial = edge_serial;
2602 		serial->port[i].private = edge_port;
2603 	}
2604 
2605 	return 0;
2606 }
2607 
edge_shutdown(struct usb_serial * serial)2608 static void edge_shutdown (struct usb_serial *serial)
2609 {
2610 	int i;
2611 
2612 	dbg ("%s", __FUNCTION__);
2613 
2614 	for (i=0; i < serial->num_ports; ++i) {
2615 		kfree (serial->port[i].private);
2616 		serial->port[i].private = NULL;
2617 	}
2618 	kfree (serial->private);
2619 	serial->private = NULL;
2620 }
2621 
2622 
2623 static struct usb_serial_device_type edgeport_1port_device = {
2624 	owner:			THIS_MODULE,
2625 	name:			"Edgeport TI 1 port adapter",
2626 	id_table:		edgeport_1port_id_table,
2627 	num_interrupt_in:	1,
2628 	num_bulk_in:		1,
2629 	num_bulk_out:		1,
2630 	num_ports:		1,
2631 	open:			edge_open,
2632 	close:			edge_close,
2633 	throttle:		edge_throttle,
2634 	unthrottle:		edge_unthrottle,
2635 	startup:		edge_startup,
2636 	shutdown:		edge_shutdown,
2637 	ioctl:			edge_ioctl,
2638 	set_termios:		edge_set_termios,
2639 	write:			edge_write,
2640 	write_room:		edge_write_room,
2641 	chars_in_buffer:	edge_chars_in_buffer,
2642 	break_ctl:		edge_break,
2643 };
2644 
2645 static struct usb_serial_device_type edgeport_2port_device = {
2646 	owner:			THIS_MODULE,
2647 	name:			"Edgeport TI 2 port adapter",
2648 	id_table:		edgeport_2port_id_table,
2649 	num_interrupt_in:	1,
2650 	num_bulk_in:		2,
2651 	num_bulk_out:		2,
2652 	num_ports:		2,
2653 	open:			edge_open,
2654 	close:			edge_close,
2655 	throttle:		edge_throttle,
2656 	unthrottle:		edge_unthrottle,
2657 	startup:		edge_startup,
2658 	shutdown:		edge_shutdown,
2659 	ioctl:			edge_ioctl,
2660 	set_termios:		edge_set_termios,
2661 	write:			edge_write,
2662 	write_room:		edge_write_room,
2663 	chars_in_buffer:	edge_chars_in_buffer,
2664 	break_ctl:		edge_break,
2665 };
2666 
2667 
edgeport_init(void)2668 static int __init edgeport_init(void)
2669 {
2670 	usb_serial_register (&edgeport_1port_device);
2671 	usb_serial_register (&edgeport_2port_device);
2672 	info(DRIVER_DESC " " DRIVER_VERSION);
2673 	return 0;
2674 }
2675 
edgeport_exit(void)2676 static void __exit edgeport_exit (void)
2677 {
2678 	usb_serial_deregister (&edgeport_1port_device);
2679 	usb_serial_deregister (&edgeport_2port_device);
2680 }
2681 
2682 module_init(edgeport_init);
2683 module_exit(edgeport_exit);
2684 
2685 /* Module information */
2686 MODULE_AUTHOR(DRIVER_AUTHOR);
2687 MODULE_DESCRIPTION(DRIVER_DESC);
2688 MODULE_LICENSE("GPL");
2689 
2690 MODULE_PARM(debug, "i");
2691 MODULE_PARM_DESC(debug, "Debug enabled or not");
2692 
2693 MODULE_PARM(ignore_cpu_rev, "i");
2694 MODULE_PARM_DESC(ignore_cpu_rev, "Ignore the cpu revision when connecting to a device");
2695 
2696