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