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