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