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