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