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