pandora: defconfig: update
[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
1685         if (port_number >= edge_serial->serial->num_ports) {
1686                 dev_err(&urb->dev->dev, "bad port number %d\n", port_number);
1687                 goto exit;
1688         }
1689
1690         port = edge_serial->serial->port[port_number];
1691         edge_port = usb_get_serial_port_data(port);
1692         if (!edge_port) {
1693                 dbg("%s - edge_port not found", __func__);
1694                 return;
1695         }
1696         switch (function) {
1697         case TIUMP_INTERRUPT_CODE_LSR:
1698                 lsr = map_line_status(data[1]);
1699                 if (lsr & UMP_UART_LSR_DATA_MASK) {
1700                         /* Save the LSR event for bulk read
1701                            completion routine */
1702                         dbg("%s - LSR Event Port %u LSR Status = %02x",
1703                              __func__, port_number, lsr);
1704                         edge_port->lsr_event = 1;
1705                         edge_port->lsr_mask = lsr;
1706                 } else {
1707                         dbg("%s - ===== Port %d LSR Status = %02x ======",
1708                              __func__, port_number, lsr);
1709                         handle_new_lsr(edge_port, 0, lsr, 0);
1710                 }
1711                 break;
1712
1713         case TIUMP_INTERRUPT_CODE_MSR:  /* MSR */
1714                 /* Copy MSR from UMP */
1715                 msr = data[1];
1716                 dbg("%s - ===== Port %u MSR Status = %02x ======",
1717                      __func__, port_number, msr);
1718                 handle_new_msr(edge_port, msr);
1719                 break;
1720
1721         default:
1722                 dev_err(&urb->dev->dev,
1723                         "%s - Unknown Interrupt code from UMP %x\n",
1724                         __func__, data[1]);
1725                 break;
1726
1727         }
1728
1729 exit:
1730         retval = usb_submit_urb(urb, GFP_ATOMIC);
1731         if (retval)
1732                 dev_err(&urb->dev->dev,
1733                         "%s - usb_submit_urb failed with result %d\n",
1734                          __func__, retval);
1735 }
1736
1737 static void edge_bulk_in_callback(struct urb *urb)
1738 {
1739         struct edgeport_port *edge_port = urb->context;
1740         unsigned char *data = urb->transfer_buffer;
1741         struct tty_struct *tty;
1742         int retval = 0;
1743         int port_number;
1744         int status = urb->status;
1745
1746         dbg("%s", __func__);
1747
1748         switch (status) {
1749         case 0:
1750                 /* success */
1751                 break;
1752         case -ECONNRESET:
1753         case -ENOENT:
1754         case -ESHUTDOWN:
1755                 /* this urb is terminated, clean up */
1756                 dbg("%s - urb shutting down with status: %d",
1757                     __func__, status);
1758                 return;
1759         default:
1760                 dev_err(&urb->dev->dev,
1761                         "%s - nonzero read bulk status received: %d\n",
1762                              __func__, status);
1763         }
1764
1765         if (status == -EPIPE)
1766                 goto exit;
1767
1768         if (status) {
1769                 dev_err(&urb->dev->dev, "%s - stopping read!\n", __func__);
1770                 return;
1771         }
1772
1773         port_number = edge_port->port->number - edge_port->port->serial->minor;
1774
1775         if (urb->actual_length > 0 && edge_port->lsr_event) {
1776                 edge_port->lsr_event = 0;
1777                 dbg("%s ===== Port %u LSR Status = %02x, Data = %02x ======",
1778                      __func__, port_number, edge_port->lsr_mask, *data);
1779                 handle_new_lsr(edge_port, 1, edge_port->lsr_mask, *data);
1780                 /* Adjust buffer length/pointer */
1781                 --urb->actual_length;
1782                 ++data;
1783         }
1784
1785         tty = tty_port_tty_get(&edge_port->port->port);
1786         if (tty && urb->actual_length) {
1787                 usb_serial_debug_data(debug, &edge_port->port->dev,
1788                                         __func__, urb->actual_length, data);
1789                 if (edge_port->close_pending)
1790                         dbg("%s - close pending, dropping data on the floor",
1791                                                                 __func__);
1792                 else
1793                         edge_tty_recv(&edge_port->port->dev, tty, data,
1794                                                         urb->actual_length);
1795                 edge_port->icount.rx += urb->actual_length;
1796         }
1797         tty_kref_put(tty);
1798
1799 exit:
1800         /* continue read unless stopped */
1801         spin_lock(&edge_port->ep_lock);
1802         if (edge_port->ep_read_urb_state == EDGE_READ_URB_RUNNING) {
1803                 urb->dev = edge_port->port->serial->dev;
1804                 retval = usb_submit_urb(urb, GFP_ATOMIC);
1805         } else if (edge_port->ep_read_urb_state == EDGE_READ_URB_STOPPING) {
1806                 edge_port->ep_read_urb_state = EDGE_READ_URB_STOPPED;
1807         }
1808         spin_unlock(&edge_port->ep_lock);
1809         if (retval)
1810                 dev_err(&urb->dev->dev,
1811                         "%s - usb_submit_urb failed with result %d\n",
1812                          __func__, retval);
1813 }
1814
1815 static void edge_tty_recv(struct device *dev, struct tty_struct *tty,
1816                                         unsigned char *data, int length)
1817 {
1818         int queued;
1819
1820         queued = tty_insert_flip_string(tty, data, length);
1821         if (queued < length)
1822                 dev_err(dev, "%s - dropping data, %d bytes lost\n",
1823                         __func__, length - queued);
1824         tty_flip_buffer_push(tty);
1825 }
1826
1827 static void edge_bulk_out_callback(struct urb *urb)
1828 {
1829         struct usb_serial_port *port = urb->context;
1830         struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1831         int status = urb->status;
1832         struct tty_struct *tty;
1833
1834         dbg("%s - port %d", __func__, port->number);
1835
1836         edge_port->ep_write_urb_in_use = 0;
1837
1838         switch (status) {
1839         case 0:
1840                 /* success */
1841                 break;
1842         case -ECONNRESET:
1843         case -ENOENT:
1844         case -ESHUTDOWN:
1845                 /* this urb is terminated, clean up */
1846                 dbg("%s - urb shutting down with status: %d",
1847                     __func__, status);
1848                 return;
1849         default:
1850                 dev_err(&urb->dev->dev, "%s - nonzero write bulk status "
1851                         "received: %d\n", __func__, status);
1852         }
1853
1854         /* send any buffered data */
1855         tty = tty_port_tty_get(&port->port);
1856         edge_send(tty);
1857         tty_kref_put(tty);
1858 }
1859
1860 static int edge_open(struct tty_struct *tty, struct usb_serial_port *port)
1861 {
1862         struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1863         struct edgeport_serial *edge_serial;
1864         struct usb_device *dev;
1865         struct urb *urb;
1866         int port_number;
1867         int status;
1868         u16 open_settings;
1869         u8 transaction_timeout;
1870
1871         dbg("%s - port %d", __func__, port->number);
1872
1873         if (edge_port == NULL)
1874                 return -ENODEV;
1875
1876         port_number = port->number - port->serial->minor;
1877         switch (port_number) {
1878         case 0:
1879                 edge_port->uart_base = UMPMEM_BASE_UART1;
1880                 edge_port->dma_address = UMPD_OEDB1_ADDRESS;
1881                 break;
1882         case 1:
1883                 edge_port->uart_base = UMPMEM_BASE_UART2;
1884                 edge_port->dma_address = UMPD_OEDB2_ADDRESS;
1885                 break;
1886         default:
1887                 dev_err(&port->dev, "Unknown port number!!!\n");
1888                 return -ENODEV;
1889         }
1890
1891         dbg("%s - port_number = %d, uart_base = %04x, dma_address = %04x",
1892                                 __func__, port_number, edge_port->uart_base,
1893                                 edge_port->dma_address);
1894
1895         dev = port->serial->dev;
1896
1897         memset(&(edge_port->icount), 0x00, sizeof(edge_port->icount));
1898
1899         /* turn off loopback */
1900         status = ti_do_config(edge_port, UMPC_SET_CLR_LOOPBACK, 0);
1901         if (status) {
1902                 dev_err(&port->dev,
1903                                 "%s - cannot send clear loopback command, %d\n",
1904                         __func__, status);
1905                 return status;
1906         }
1907
1908         /* set up the port settings */
1909         if (tty)
1910                 edge_set_termios(tty, port, tty->termios);
1911
1912         /* open up the port */
1913
1914         /* milliseconds to timeout for DMA transfer */
1915         transaction_timeout = 2;
1916
1917         edge_port->ump_read_timeout =
1918                                 max(20, ((transaction_timeout * 3) / 2));
1919
1920         /* milliseconds to timeout for DMA transfer */
1921         open_settings = (u8)(UMP_DMA_MODE_CONTINOUS |
1922                              UMP_PIPE_TRANS_TIMEOUT_ENA |
1923                              (transaction_timeout << 2));
1924
1925         dbg("%s - Sending UMPC_OPEN_PORT", __func__);
1926
1927         /* Tell TI to open and start the port */
1928         status = send_cmd(dev, UMPC_OPEN_PORT,
1929                 (u8)(UMPM_UART1_PORT + port_number), open_settings, NULL, 0);
1930         if (status) {
1931                 dev_err(&port->dev, "%s - cannot send open command, %d\n",
1932                                                         __func__, status);
1933                 return status;
1934         }
1935
1936         /* Start the DMA? */
1937         status = send_cmd(dev, UMPC_START_PORT,
1938                 (u8)(UMPM_UART1_PORT + port_number), 0, NULL, 0);
1939         if (status) {
1940                 dev_err(&port->dev, "%s - cannot send start DMA command, %d\n",
1941                                                         __func__, status);
1942                 return status;
1943         }
1944
1945         /* Clear TX and RX buffers in UMP */
1946         status = purge_port(port, UMP_PORT_DIR_OUT | UMP_PORT_DIR_IN);
1947         if (status) {
1948                 dev_err(&port->dev,
1949                         "%s - cannot send clear buffers command, %d\n",
1950                         __func__, status);
1951                 return status;
1952         }
1953
1954         /* Read Initial MSR */
1955         status = ti_vread_sync(dev, UMPC_READ_MSR, 0,
1956                                 (__u16)(UMPM_UART1_PORT + port_number),
1957                                 &edge_port->shadow_msr, 1);
1958         if (status) {
1959                 dev_err(&port->dev, "%s - cannot send read MSR command, %d\n",
1960                                                         __func__, status);
1961                 return status;
1962         }
1963
1964         dbg("ShadowMSR 0x%X", edge_port->shadow_msr);
1965
1966         /* Set Initial MCR */
1967         edge_port->shadow_mcr = MCR_RTS | MCR_DTR;
1968         dbg("ShadowMCR 0x%X", edge_port->shadow_mcr);
1969
1970         edge_serial = edge_port->edge_serial;
1971         if (mutex_lock_interruptible(&edge_serial->es_lock))
1972                 return -ERESTARTSYS;
1973         if (edge_serial->num_ports_open == 0) {
1974                 /* we are the first port to open, post the interrupt urb */
1975                 urb = edge_serial->serial->port[0]->interrupt_in_urb;
1976                 if (!urb) {
1977                         dev_err(&port->dev,
1978                                 "%s - no interrupt urb present, exiting\n",
1979                                 __func__);
1980                         status = -EINVAL;
1981                         goto release_es_lock;
1982                 }
1983                 urb->complete = edge_interrupt_callback;
1984                 urb->context = edge_serial;
1985                 urb->dev = dev;
1986                 status = usb_submit_urb(urb, GFP_KERNEL);
1987                 if (status) {
1988                         dev_err(&port->dev,
1989                                 "%s - usb_submit_urb failed with value %d\n",
1990                                         __func__, status);
1991                         goto release_es_lock;
1992                 }
1993         }
1994
1995         /*
1996          * reset the data toggle on the bulk endpoints to work around bug in
1997          * host controllers where things get out of sync some times
1998          */
1999         usb_clear_halt(dev, port->write_urb->pipe);
2000         usb_clear_halt(dev, port->read_urb->pipe);
2001
2002         /* start up our bulk read urb */
2003         urb = port->read_urb;
2004         if (!urb) {
2005                 dev_err(&port->dev, "%s - no read urb present, exiting\n",
2006                                                                 __func__);
2007                 status = -EINVAL;
2008                 goto unlink_int_urb;
2009         }
2010         edge_port->ep_read_urb_state = EDGE_READ_URB_RUNNING;
2011         urb->complete = edge_bulk_in_callback;
2012         urb->context = edge_port;
2013         urb->dev = dev;
2014         status = usb_submit_urb(urb, GFP_KERNEL);
2015         if (status) {
2016                 dev_err(&port->dev,
2017                         "%s - read bulk usb_submit_urb failed with value %d\n",
2018                                 __func__, status);
2019                 goto unlink_int_urb;
2020         }
2021
2022         ++edge_serial->num_ports_open;
2023
2024         dbg("%s - exited", __func__);
2025
2026         goto release_es_lock;
2027
2028 unlink_int_urb:
2029         if (edge_port->edge_serial->num_ports_open == 0)
2030                 usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
2031 release_es_lock:
2032         mutex_unlock(&edge_serial->es_lock);
2033         return status;
2034 }
2035
2036 static void edge_close(struct usb_serial_port *port)
2037 {
2038         struct edgeport_serial *edge_serial;
2039         struct edgeport_port *edge_port;
2040         int port_number;
2041         int status;
2042
2043         dbg("%s - port %d", __func__, port->number);
2044
2045         edge_serial = usb_get_serial_data(port->serial);
2046         edge_port = usb_get_serial_port_data(port);
2047         if (edge_serial == NULL || edge_port == NULL)
2048                 return;
2049
2050         /* The bulkreadcompletion routine will check
2051          * this flag and dump add read data */
2052         edge_port->close_pending = 1;
2053
2054         /* chase the port close and flush */
2055         chase_port(edge_port, (HZ * closing_wait) / 100, 1);
2056
2057         usb_kill_urb(port->read_urb);
2058         usb_kill_urb(port->write_urb);
2059         edge_port->ep_write_urb_in_use = 0;
2060
2061         /* assuming we can still talk to the device,
2062          * send a close port command to it */
2063         dbg("%s - send umpc_close_port", __func__);
2064         port_number = port->number - port->serial->minor;
2065         status = send_cmd(port->serial->dev,
2066                                      UMPC_CLOSE_PORT,
2067                                      (__u8)(UMPM_UART1_PORT + port_number),
2068                                      0,
2069                                      NULL,
2070                                      0);
2071         mutex_lock(&edge_serial->es_lock);
2072         --edge_port->edge_serial->num_ports_open;
2073         if (edge_port->edge_serial->num_ports_open <= 0) {
2074                 /* last port is now closed, let's shut down our interrupt urb */
2075                 usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
2076                 edge_port->edge_serial->num_ports_open = 0;
2077         }
2078         mutex_unlock(&edge_serial->es_lock);
2079         edge_port->close_pending = 0;
2080
2081         dbg("%s - exited", __func__);
2082 }
2083
2084 static int edge_write(struct tty_struct *tty, struct usb_serial_port *port,
2085                                 const unsigned char *data, int count)
2086 {
2087         struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2088
2089         dbg("%s - port %d", __func__, port->number);
2090
2091         if (count == 0) {
2092                 dbg("%s - write request of 0 bytes", __func__);
2093                 return 0;
2094         }
2095
2096         if (edge_port == NULL)
2097                 return -ENODEV;
2098         if (edge_port->close_pending == 1)
2099                 return -ENODEV;
2100
2101         count = kfifo_in_locked(&edge_port->write_fifo, data, count,
2102                                                         &edge_port->ep_lock);
2103         edge_send(tty);
2104
2105         return count;
2106 }
2107
2108 static void edge_send(struct tty_struct *tty)
2109 {
2110         struct usb_serial_port *port = tty->driver_data;
2111         int count, result;
2112         struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2113         unsigned long flags;
2114
2115
2116         dbg("%s - port %d", __func__, port->number);
2117
2118         spin_lock_irqsave(&edge_port->ep_lock, flags);
2119
2120         if (edge_port->ep_write_urb_in_use) {
2121                 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2122                 return;
2123         }
2124
2125         count = kfifo_out(&edge_port->write_fifo,
2126                                 port->write_urb->transfer_buffer,
2127                                 port->bulk_out_size);
2128
2129         if (count == 0) {
2130                 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2131                 return;
2132         }
2133
2134         edge_port->ep_write_urb_in_use = 1;
2135
2136         spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2137
2138         usb_serial_debug_data(debug, &port->dev, __func__, count,
2139                                 port->write_urb->transfer_buffer);
2140
2141         /* set up our urb */
2142         usb_fill_bulk_urb(port->write_urb, port->serial->dev,
2143                            usb_sndbulkpipe(port->serial->dev,
2144                                             port->bulk_out_endpointAddress),
2145                            port->write_urb->transfer_buffer, count,
2146                            edge_bulk_out_callback,
2147                            port);
2148
2149         /* send the data out the bulk port */
2150         result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
2151         if (result) {
2152                 dev_err(&port->dev,
2153                         "%s - failed submitting write urb, error %d\n",
2154                                 __func__, result);
2155                 edge_port->ep_write_urb_in_use = 0;
2156                 /* TODO: reschedule edge_send */
2157         } else
2158                 edge_port->icount.tx += count;
2159
2160         /* wakeup any process waiting for writes to complete */
2161         /* there is now more room in the buffer for new writes */
2162         if (tty)
2163                 tty_wakeup(tty);
2164 }
2165
2166 static int edge_write_room(struct tty_struct *tty)
2167 {
2168         struct usb_serial_port *port = tty->driver_data;
2169         struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2170         int room = 0;
2171         unsigned long flags;
2172
2173         dbg("%s - port %d", __func__, port->number);
2174
2175         if (edge_port == NULL)
2176                 return 0;
2177         if (edge_port->close_pending == 1)
2178                 return 0;
2179
2180         spin_lock_irqsave(&edge_port->ep_lock, flags);
2181         room = kfifo_avail(&edge_port->write_fifo);
2182         spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2183
2184         dbg("%s - returns %d", __func__, room);
2185         return room;
2186 }
2187
2188 static int edge_chars_in_buffer(struct tty_struct *tty)
2189 {
2190         struct usb_serial_port *port = tty->driver_data;
2191         struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2192         int chars = 0;
2193         unsigned long flags;
2194
2195         dbg("%s - port %d", __func__, port->number);
2196
2197         if (edge_port == NULL)
2198                 return 0;
2199         if (edge_port->close_pending == 1)
2200                 return 0;
2201
2202         spin_lock_irqsave(&edge_port->ep_lock, flags);
2203         chars = kfifo_len(&edge_port->write_fifo);
2204         spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2205
2206         dbg("%s - returns %d", __func__, chars);
2207         return chars;
2208 }
2209
2210 static void edge_throttle(struct tty_struct *tty)
2211 {
2212         struct usb_serial_port *port = tty->driver_data;
2213         struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2214         int status;
2215
2216         dbg("%s - port %d", __func__, port->number);
2217
2218         if (edge_port == NULL)
2219                 return;
2220
2221         /* if we are implementing XON/XOFF, send the stop character */
2222         if (I_IXOFF(tty)) {
2223                 unsigned char stop_char = STOP_CHAR(tty);
2224                 status = edge_write(tty, port, &stop_char, 1);
2225                 if (status <= 0) {
2226                         dev_err(&port->dev, "%s - failed to write stop character, %d\n", __func__, status);
2227                 }
2228         }
2229
2230         /* if we are implementing RTS/CTS, stop reads */
2231         /* and the Edgeport will clear the RTS line */
2232         if (C_CRTSCTS(tty))
2233                 stop_read(edge_port);
2234
2235 }
2236
2237 static void edge_unthrottle(struct tty_struct *tty)
2238 {
2239         struct usb_serial_port *port = tty->driver_data;
2240         struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2241         int status;
2242
2243         dbg("%s - port %d", __func__, port->number);
2244
2245         if (edge_port == NULL)
2246                 return;
2247
2248         /* if we are implementing XON/XOFF, send the start character */
2249         if (I_IXOFF(tty)) {
2250                 unsigned char start_char = START_CHAR(tty);
2251                 status = edge_write(tty, port, &start_char, 1);
2252                 if (status <= 0) {
2253                         dev_err(&port->dev, "%s - failed to write start character, %d\n", __func__, status);
2254                 }
2255         }
2256         /* if we are implementing RTS/CTS, restart reads */
2257         /* are the Edgeport will assert the RTS line */
2258         if (C_CRTSCTS(tty)) {
2259                 status = restart_read(edge_port);
2260                 if (status)
2261                         dev_err(&port->dev,
2262                                 "%s - read bulk usb_submit_urb failed: %d\n",
2263                                                         __func__, status);
2264         }
2265
2266 }
2267
2268 static void stop_read(struct edgeport_port *edge_port)
2269 {
2270         unsigned long flags;
2271
2272         spin_lock_irqsave(&edge_port->ep_lock, flags);
2273
2274         if (edge_port->ep_read_urb_state == EDGE_READ_URB_RUNNING)
2275                 edge_port->ep_read_urb_state = EDGE_READ_URB_STOPPING;
2276         edge_port->shadow_mcr &= ~MCR_RTS;
2277
2278         spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2279 }
2280
2281 static int restart_read(struct edgeport_port *edge_port)
2282 {
2283         struct urb *urb;
2284         int status = 0;
2285         unsigned long flags;
2286
2287         spin_lock_irqsave(&edge_port->ep_lock, flags);
2288
2289         if (edge_port->ep_read_urb_state == EDGE_READ_URB_STOPPED) {
2290                 urb = edge_port->port->read_urb;
2291                 urb->complete = edge_bulk_in_callback;
2292                 urb->context = edge_port;
2293                 urb->dev = edge_port->port->serial->dev;
2294                 status = usb_submit_urb(urb, GFP_ATOMIC);
2295         }
2296         edge_port->ep_read_urb_state = EDGE_READ_URB_RUNNING;
2297         edge_port->shadow_mcr |= MCR_RTS;
2298
2299         spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2300
2301         return status;
2302 }
2303
2304 static void change_port_settings(struct tty_struct *tty,
2305                 struct edgeport_port *edge_port, struct ktermios *old_termios)
2306 {
2307         struct ump_uart_config *config;
2308         int baud;
2309         unsigned cflag;
2310         int status;
2311         int port_number = edge_port->port->number -
2312                                         edge_port->port->serial->minor;
2313
2314         dbg("%s - port %d", __func__, edge_port->port->number);
2315
2316         config = kmalloc (sizeof (*config), GFP_KERNEL);
2317         if (!config) {
2318                 *tty->termios = *old_termios;
2319                 dev_err(&edge_port->port->dev, "%s - out of memory\n",
2320                                                                 __func__);
2321                 return;
2322         }
2323
2324         cflag = tty->termios->c_cflag;
2325
2326         config->wFlags = 0;
2327
2328         /* These flags must be set */
2329         config->wFlags |= UMP_MASK_UART_FLAGS_RECEIVE_MS_INT;
2330         config->wFlags |= UMP_MASK_UART_FLAGS_AUTO_START_ON_ERR;
2331         config->bUartMode = (__u8)(edge_port->bUartMode);
2332
2333         switch (cflag & CSIZE) {
2334         case CS5:
2335                     config->bDataBits = UMP_UART_CHAR5BITS;
2336                     dbg("%s - data bits = 5", __func__);
2337                     break;
2338         case CS6:
2339                     config->bDataBits = UMP_UART_CHAR6BITS;
2340                     dbg("%s - data bits = 6", __func__);
2341                     break;
2342         case CS7:
2343                     config->bDataBits = UMP_UART_CHAR7BITS;
2344                     dbg("%s - data bits = 7", __func__);
2345                     break;
2346         default:
2347         case CS8:
2348                     config->bDataBits = UMP_UART_CHAR8BITS;
2349                     dbg("%s - data bits = 8", __func__);
2350                             break;
2351         }
2352
2353         if (cflag & PARENB) {
2354                 if (cflag & PARODD) {
2355                         config->wFlags |= UMP_MASK_UART_FLAGS_PARITY;
2356                         config->bParity = UMP_UART_ODDPARITY;
2357                         dbg("%s - parity = odd", __func__);
2358                 } else {
2359                         config->wFlags |= UMP_MASK_UART_FLAGS_PARITY;
2360                         config->bParity = UMP_UART_EVENPARITY;
2361                         dbg("%s - parity = even", __func__);
2362                 }
2363         } else {
2364                 config->bParity = UMP_UART_NOPARITY;
2365                 dbg("%s - parity = none", __func__);
2366         }
2367
2368         if (cflag & CSTOPB) {
2369                 config->bStopBits = UMP_UART_STOPBIT2;
2370                 dbg("%s - stop bits = 2", __func__);
2371         } else {
2372                 config->bStopBits = UMP_UART_STOPBIT1;
2373                 dbg("%s - stop bits = 1", __func__);
2374         }
2375
2376         /* figure out the flow control settings */
2377         if (cflag & CRTSCTS) {
2378                 config->wFlags |= UMP_MASK_UART_FLAGS_OUT_X_CTS_FLOW;
2379                 config->wFlags |= UMP_MASK_UART_FLAGS_RTS_FLOW;
2380                 dbg("%s - RTS/CTS is enabled", __func__);
2381         } else {
2382                 dbg("%s - RTS/CTS is disabled", __func__);
2383                 tty->hw_stopped = 0;
2384                 restart_read(edge_port);
2385         }
2386
2387         /* if we are implementing XON/XOFF, set the start and stop
2388            character in the device */
2389         config->cXon  = START_CHAR(tty);
2390         config->cXoff = STOP_CHAR(tty);
2391
2392         /* if we are implementing INBOUND XON/XOFF */
2393         if (I_IXOFF(tty)) {
2394                 config->wFlags |= UMP_MASK_UART_FLAGS_IN_X;
2395                 dbg("%s - INBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x",
2396                      __func__, config->cXon, config->cXoff);
2397         } else
2398                 dbg("%s - INBOUND XON/XOFF is disabled", __func__);
2399
2400         /* if we are implementing OUTBOUND XON/XOFF */
2401         if (I_IXON(tty)) {
2402                 config->wFlags |= UMP_MASK_UART_FLAGS_OUT_X;
2403                 dbg("%s - OUTBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x",
2404                      __func__, config->cXon, config->cXoff);
2405         } else
2406                 dbg("%s - OUTBOUND XON/XOFF is disabled", __func__);
2407
2408         tty->termios->c_cflag &= ~CMSPAR;
2409
2410         /* Round the baud rate */
2411         baud = tty_get_baud_rate(tty);
2412         if (!baud) {
2413                 /* pick a default, any default... */
2414                 baud = 9600;
2415         } else {
2416                 /* Avoid a zero divisor. */
2417                 baud = min(baud, 461550);
2418                 tty_encode_baud_rate(tty, baud, baud);
2419         }
2420
2421         edge_port->baud_rate = baud;
2422         config->wBaudRate = (__u16)((461550L + baud/2) / baud);
2423
2424         /* FIXME: Recompute actual baud from divisor here */
2425
2426         dbg("%s - baud rate = %d, wBaudRate = %d", __func__, baud,
2427                                                         config->wBaudRate);
2428
2429         dbg("wBaudRate:   %d", (int)(461550L / config->wBaudRate));
2430         dbg("wFlags:    0x%x", config->wFlags);
2431         dbg("bDataBits:   %d", config->bDataBits);
2432         dbg("bParity:     %d", config->bParity);
2433         dbg("bStopBits:   %d", config->bStopBits);
2434         dbg("cXon:        %d", config->cXon);
2435         dbg("cXoff:       %d", config->cXoff);
2436         dbg("bUartMode:   %d", config->bUartMode);
2437
2438         /* move the word values into big endian mode */
2439         cpu_to_be16s(&config->wFlags);
2440         cpu_to_be16s(&config->wBaudRate);
2441
2442         status = send_cmd(edge_port->port->serial->dev, UMPC_SET_CONFIG,
2443                                 (__u8)(UMPM_UART1_PORT + port_number),
2444                                 0, (__u8 *)config, sizeof(*config));
2445         if (status)
2446                 dbg("%s - error %d when trying to write config to device",
2447                      __func__, status);
2448         kfree(config);
2449 }
2450
2451 static void edge_set_termios(struct tty_struct *tty,
2452                 struct usb_serial_port *port, struct ktermios *old_termios)
2453 {
2454         struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2455         unsigned int cflag;
2456
2457         cflag = tty->termios->c_cflag;
2458
2459         dbg("%s - clfag %08x iflag %08x", __func__,
2460             tty->termios->c_cflag, tty->termios->c_iflag);
2461         dbg("%s - old clfag %08x old iflag %08x", __func__,
2462             old_termios->c_cflag, old_termios->c_iflag);
2463         dbg("%s - port %d", __func__, port->number);
2464
2465         if (edge_port == NULL)
2466                 return;
2467         /* change the port settings to the new ones specified */
2468         change_port_settings(tty, edge_port, old_termios);
2469 }
2470
2471 static int edge_tiocmset(struct tty_struct *tty,
2472                                         unsigned int set, unsigned int clear)
2473 {
2474         struct usb_serial_port *port = tty->driver_data;
2475         struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2476         unsigned int mcr;
2477         unsigned long flags;
2478
2479         dbg("%s - port %d", __func__, port->number);
2480
2481         spin_lock_irqsave(&edge_port->ep_lock, flags);
2482         mcr = edge_port->shadow_mcr;
2483         if (set & TIOCM_RTS)
2484                 mcr |= MCR_RTS;
2485         if (set & TIOCM_DTR)
2486                 mcr |= MCR_DTR;
2487         if (set & TIOCM_LOOP)
2488                 mcr |= MCR_LOOPBACK;
2489
2490         if (clear & TIOCM_RTS)
2491                 mcr &= ~MCR_RTS;
2492         if (clear & TIOCM_DTR)
2493                 mcr &= ~MCR_DTR;
2494         if (clear & TIOCM_LOOP)
2495                 mcr &= ~MCR_LOOPBACK;
2496
2497         edge_port->shadow_mcr = mcr;
2498         spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2499
2500         restore_mcr(edge_port, mcr);
2501         return 0;
2502 }
2503
2504 static int edge_tiocmget(struct tty_struct *tty)
2505 {
2506         struct usb_serial_port *port = tty->driver_data;
2507         struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2508         unsigned int result = 0;
2509         unsigned int msr;
2510         unsigned int mcr;
2511         unsigned long flags;
2512
2513         dbg("%s - port %d", __func__, port->number);
2514
2515         spin_lock_irqsave(&edge_port->ep_lock, flags);
2516
2517         msr = edge_port->shadow_msr;
2518         mcr = edge_port->shadow_mcr;
2519         result = ((mcr & MCR_DTR)       ? TIOCM_DTR: 0)   /* 0x002 */
2520                   | ((mcr & MCR_RTS)    ? TIOCM_RTS: 0)   /* 0x004 */
2521                   | ((msr & EDGEPORT_MSR_CTS)   ? TIOCM_CTS: 0)   /* 0x020 */
2522                   | ((msr & EDGEPORT_MSR_CD)    ? TIOCM_CAR: 0)   /* 0x040 */
2523                   | ((msr & EDGEPORT_MSR_RI)    ? TIOCM_RI:  0)   /* 0x080 */
2524                   | ((msr & EDGEPORT_MSR_DSR)   ? TIOCM_DSR: 0);  /* 0x100 */
2525
2526
2527         dbg("%s -- %x", __func__, result);
2528         spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2529
2530         return result;
2531 }
2532
2533 static int edge_get_icount(struct tty_struct *tty,
2534                                 struct serial_icounter_struct *icount)
2535 {
2536         struct usb_serial_port *port = tty->driver_data;
2537         struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2538         struct async_icount *ic = &edge_port->icount;
2539
2540         icount->cts = ic->cts;
2541         icount->dsr = ic->dsr;
2542         icount->rng = ic->rng;
2543         icount->dcd = ic->dcd;
2544         icount->tx = ic->tx;
2545         icount->rx = ic->rx;
2546         icount->frame = ic->frame;
2547         icount->parity = ic->parity;
2548         icount->overrun = ic->overrun;
2549         icount->brk = ic->brk;
2550         icount->buf_overrun = ic->buf_overrun;
2551         return 0;
2552 }
2553
2554 static int get_serial_info(struct edgeport_port *edge_port,
2555                                 struct serial_struct __user *retinfo)
2556 {
2557         struct serial_struct tmp;
2558
2559         if (!retinfo)
2560                 return -EFAULT;
2561
2562         memset(&tmp, 0, sizeof(tmp));
2563
2564         tmp.type                = PORT_16550A;
2565         tmp.line                = edge_port->port->serial->minor;
2566         tmp.port                = edge_port->port->number;
2567         tmp.irq                 = 0;
2568         tmp.flags               = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
2569         tmp.xmit_fifo_size      = edge_port->port->bulk_out_size;
2570         tmp.baud_base           = 9600;
2571         tmp.close_delay         = 5*HZ;
2572         tmp.closing_wait        = closing_wait;
2573
2574         if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2575                 return -EFAULT;
2576         return 0;
2577 }
2578
2579 static int edge_ioctl(struct tty_struct *tty,
2580                                         unsigned int cmd, unsigned long arg)
2581 {
2582         struct usb_serial_port *port = tty->driver_data;
2583         struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2584         struct async_icount cnow;
2585         struct async_icount cprev;
2586
2587         dbg("%s - port %d, cmd = 0x%x", __func__, port->number, cmd);
2588
2589         switch (cmd) {
2590         case TIOCGSERIAL:
2591                 dbg("%s - (%d) TIOCGSERIAL", __func__, port->number);
2592                 return get_serial_info(edge_port,
2593                                 (struct serial_struct __user *) arg);
2594         case TIOCMIWAIT:
2595                 dbg("%s - (%d) TIOCMIWAIT", __func__, port->number);
2596                 cprev = edge_port->icount;
2597                 while (1) {
2598                         interruptible_sleep_on(&port->delta_msr_wait);
2599                         /* see if a signal did it */
2600                         if (signal_pending(current))
2601                                 return -ERESTARTSYS;
2602
2603                         if (port->serial->disconnected)
2604                                 return -EIO;
2605
2606                         cnow = edge_port->icount;
2607                         if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
2608                             cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
2609                                 return -EIO; /* no change => error */
2610                         if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
2611                             ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
2612                             ((arg & TIOCM_CD)  && (cnow.dcd != cprev.dcd)) ||
2613                             ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
2614                                 return 0;
2615                         }
2616                         cprev = cnow;
2617                 }
2618                 /* not reached */
2619                 break;
2620         }
2621         return -ENOIOCTLCMD;
2622 }
2623
2624 static void edge_break(struct tty_struct *tty, int break_state)
2625 {
2626         struct usb_serial_port *port = tty->driver_data;
2627         struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2628         int status;
2629         int bv = 0;     /* Off */
2630
2631         dbg("%s - state = %d", __func__, break_state);
2632
2633         /* chase the port close */
2634         chase_port(edge_port, 0, 0);
2635
2636         if (break_state == -1)
2637                 bv = 1; /* On */
2638         status = ti_do_config(edge_port, UMPC_SET_CLR_BREAK, bv);
2639         if (status)
2640                 dbg("%s - error %d sending break set/clear command.",
2641                      __func__, status);
2642 }
2643
2644 static int edge_startup(struct usb_serial *serial)
2645 {
2646         struct edgeport_serial *edge_serial;
2647         struct edgeport_port *edge_port;
2648         struct usb_device *dev;
2649         int status;
2650         int i;
2651
2652         dev = serial->dev;
2653
2654         /* Make sure we have the required endpoints when in download mode. */
2655         if (serial->interface->cur_altsetting->desc.bNumEndpoints > 1) {
2656                 if (serial->num_bulk_in < serial->num_ports ||
2657                                 serial->num_bulk_out < serial->num_ports)
2658                         return -ENODEV;
2659         }
2660
2661         /* create our private serial structure */
2662         edge_serial = kzalloc(sizeof(struct edgeport_serial), GFP_KERNEL);
2663         if (edge_serial == NULL) {
2664                 dev_err(&serial->dev->dev, "%s - Out of memory\n", __func__);
2665                 return -ENOMEM;
2666         }
2667         mutex_init(&edge_serial->es_lock);
2668         edge_serial->serial = serial;
2669         usb_set_serial_data(serial, edge_serial);
2670
2671         status = download_fw(edge_serial);
2672         if (status < 0) {
2673                 kfree(edge_serial);
2674                 return status;
2675         }
2676
2677         if (status > 0)
2678                 return 1;       /* bind but do not register any ports */
2679
2680         /* set up our port private structures */
2681         for (i = 0; i < serial->num_ports; ++i) {
2682                 edge_port = kzalloc(sizeof(struct edgeport_port), GFP_KERNEL);
2683                 if (edge_port == NULL) {
2684                         dev_err(&serial->dev->dev, "%s - Out of memory\n",
2685                                                                 __func__);
2686                         goto cleanup;
2687                 }
2688                 spin_lock_init(&edge_port->ep_lock);
2689                 if (kfifo_alloc(&edge_port->write_fifo, EDGE_OUT_BUF_SIZE,
2690                                                                 GFP_KERNEL)) {
2691                         dev_err(&serial->dev->dev, "%s - Out of memory\n",
2692                                                                 __func__);
2693                         kfree(edge_port);
2694                         goto cleanup;
2695                 }
2696                 edge_port->port = serial->port[i];
2697                 edge_port->edge_serial = edge_serial;
2698                 usb_set_serial_port_data(serial->port[i], edge_port);
2699                 edge_port->bUartMode = default_uart_mode;
2700         }
2701
2702         return 0;
2703
2704 cleanup:
2705         for (--i; i >= 0; --i) {
2706                 edge_port = usb_get_serial_port_data(serial->port[i]);
2707                 kfifo_free(&edge_port->write_fifo);
2708                 kfree(edge_port);
2709                 usb_set_serial_port_data(serial->port[i], NULL);
2710         }
2711         kfree(edge_serial);
2712         usb_set_serial_data(serial, NULL);
2713         return -ENOMEM;
2714 }
2715
2716 static void edge_disconnect(struct usb_serial *serial)
2717 {
2718         dbg("%s", __func__);
2719 }
2720
2721 static void edge_release(struct usb_serial *serial)
2722 {
2723         int i;
2724         struct edgeport_port *edge_port;
2725
2726         dbg("%s", __func__);
2727
2728         for (i = 0; i < serial->num_ports; ++i) {
2729                 edge_port = usb_get_serial_port_data(serial->port[i]);
2730                 if (!edge_port)
2731                         continue;
2732                 kfifo_free(&edge_port->write_fifo);
2733                 kfree(edge_port);
2734         }
2735         kfree(usb_get_serial_data(serial));
2736 }
2737
2738
2739 /* Sysfs Attributes */
2740
2741 static ssize_t show_uart_mode(struct device *dev,
2742         struct device_attribute *attr, char *buf)
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
2747         return sprintf(buf, "%d\n", edge_port->bUartMode);
2748 }
2749
2750 static ssize_t store_uart_mode(struct device *dev,
2751         struct device_attribute *attr, const char *valbuf, size_t count)
2752 {
2753         struct usb_serial_port *port = to_usb_serial_port(dev);
2754         struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2755         unsigned int v = simple_strtoul(valbuf, NULL, 0);
2756
2757         dbg("%s: setting uart_mode = %d", __func__, v);
2758
2759         if (v < 256)
2760                 edge_port->bUartMode = v;
2761         else
2762                 dev_err(dev, "%s - uart_mode %d is invalid\n", __func__, v);
2763
2764         return count;
2765 }
2766
2767 static DEVICE_ATTR(uart_mode, S_IWUSR | S_IRUGO, show_uart_mode,
2768                                                         store_uart_mode);
2769
2770 static int edge_create_sysfs_attrs(struct usb_serial_port *port)
2771 {
2772         return device_create_file(&port->dev, &dev_attr_uart_mode);
2773 }
2774
2775 static int edge_remove_sysfs_attrs(struct usb_serial_port *port)
2776 {
2777         device_remove_file(&port->dev, &dev_attr_uart_mode);
2778         return 0;
2779 }
2780
2781
2782 static struct usb_serial_driver edgeport_1port_device = {
2783         .driver = {
2784                 .owner          = THIS_MODULE,
2785                 .name           = "edgeport_ti_1",
2786         },
2787         .description            = "Edgeport TI 1 port adapter",
2788         .usb_driver             = &io_driver,
2789         .id_table               = edgeport_1port_id_table,
2790         .num_ports              = 1,
2791         .open                   = edge_open,
2792         .close                  = edge_close,
2793         .throttle               = edge_throttle,
2794         .unthrottle             = edge_unthrottle,
2795         .attach                 = edge_startup,
2796         .disconnect             = edge_disconnect,
2797         .release                = edge_release,
2798         .port_probe             = edge_create_sysfs_attrs,
2799         .port_remove            = edge_remove_sysfs_attrs,
2800         .ioctl                  = edge_ioctl,
2801         .set_termios            = edge_set_termios,
2802         .tiocmget               = edge_tiocmget,
2803         .tiocmset               = edge_tiocmset,
2804         .get_icount             = edge_get_icount,
2805         .write                  = edge_write,
2806         .write_room             = edge_write_room,
2807         .chars_in_buffer        = edge_chars_in_buffer,
2808         .break_ctl              = edge_break,
2809         .read_int_callback      = edge_interrupt_callback,
2810         .read_bulk_callback     = edge_bulk_in_callback,
2811         .write_bulk_callback    = edge_bulk_out_callback,
2812 };
2813
2814 static struct usb_serial_driver edgeport_2port_device = {
2815         .driver = {
2816                 .owner          = THIS_MODULE,
2817                 .name           = "edgeport_ti_2",
2818         },
2819         .description            = "Edgeport TI 2 port adapter",
2820         .usb_driver             = &io_driver,
2821         .id_table               = edgeport_2port_id_table,
2822         .num_ports              = 2,
2823         .open                   = edge_open,
2824         .close                  = edge_close,
2825         .throttle               = edge_throttle,
2826         .unthrottle             = edge_unthrottle,
2827         .attach                 = edge_startup,
2828         .disconnect             = edge_disconnect,
2829         .release                = edge_release,
2830         .port_probe             = edge_create_sysfs_attrs,
2831         .port_remove            = edge_remove_sysfs_attrs,
2832         .ioctl                  = edge_ioctl,
2833         .set_termios            = edge_set_termios,
2834         .tiocmget               = edge_tiocmget,
2835         .tiocmset               = edge_tiocmset,
2836         .get_icount             = edge_get_icount,
2837         .write                  = edge_write,
2838         .write_room             = edge_write_room,
2839         .chars_in_buffer        = edge_chars_in_buffer,
2840         .break_ctl              = edge_break,
2841         .read_int_callback      = edge_interrupt_callback,
2842         .read_bulk_callback     = edge_bulk_in_callback,
2843         .write_bulk_callback    = edge_bulk_out_callback,
2844 };
2845
2846
2847 static int __init edgeport_init(void)
2848 {
2849         int retval;
2850         retval = usb_serial_register(&edgeport_1port_device);
2851         if (retval)
2852                 goto failed_1port_device_register;
2853         retval = usb_serial_register(&edgeport_2port_device);
2854         if (retval)
2855                 goto failed_2port_device_register;
2856         retval = usb_register(&io_driver);
2857         if (retval)
2858                 goto failed_usb_register;
2859         printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
2860                DRIVER_DESC "\n");
2861         return 0;
2862 failed_usb_register:
2863         usb_serial_deregister(&edgeport_2port_device);
2864 failed_2port_device_register:
2865         usb_serial_deregister(&edgeport_1port_device);
2866 failed_1port_device_register:
2867         return retval;
2868 }
2869
2870 static void __exit edgeport_exit(void)
2871 {
2872         usb_deregister(&io_driver);
2873         usb_serial_deregister(&edgeport_1port_device);
2874         usb_serial_deregister(&edgeport_2port_device);
2875 }
2876
2877 module_init(edgeport_init);
2878 module_exit(edgeport_exit);
2879
2880 /* Module information */
2881 MODULE_AUTHOR(DRIVER_AUTHOR);
2882 MODULE_DESCRIPTION(DRIVER_DESC);
2883 MODULE_LICENSE("GPL");
2884 MODULE_FIRMWARE("edgeport/down3.bin");
2885
2886 module_param(debug, bool, S_IRUGO | S_IWUSR);
2887 MODULE_PARM_DESC(debug, "Debug enabled or not");
2888
2889 module_param(closing_wait, int, S_IRUGO | S_IWUSR);
2890 MODULE_PARM_DESC(closing_wait, "Maximum wait for data to drain, in .01 secs");
2891
2892 module_param(ignore_cpu_rev, bool, S_IRUGO | S_IWUSR);
2893 MODULE_PARM_DESC(ignore_cpu_rev,
2894                         "Ignore the cpu revision when connecting to a device");
2895
2896 module_param(default_uart_mode, int, S_IRUGO | S_IWUSR);
2897 MODULE_PARM_DESC(default_uart_mode, "Default uart_mode, 0=RS232, ...");