]> Pileus Git - ~andy/linux/blob - drivers/usb/serial/io_edgeport.c
USB: serial: remove debug parameter from usb_serial_debug_data()
[~andy/linux] / drivers / usb / serial / io_edgeport.c
1 /*
2  * Edgeport USB Serial Converter driver
3  *
4  * Copyright (C) 2000 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  *      Edgeport/4
14  *      Edgeport/4t
15  *      Edgeport/2
16  *      Edgeport/4i
17  *      Edgeport/2i
18  *      Edgeport/421
19  *      Edgeport/21
20  *      Rapidport/4
21  *      Edgeport/8
22  *      Edgeport/2D8
23  *      Edgeport/4D8
24  *      Edgeport/8i
25  *
26  * For questions or problems with this driver, contact Inside Out
27  * Networks technical support, or Peter Berger <pberger@brimson.com>,
28  * or Al Borchers <alborchers@steinerpoint.com>.
29  *
30  */
31
32 #include <linux/kernel.h>
33 #include <linux/jiffies.h>
34 #include <linux/errno.h>
35 #include <linux/init.h>
36 #include <linux/slab.h>
37 #include <linux/tty.h>
38 #include <linux/tty_driver.h>
39 #include <linux/tty_flip.h>
40 #include <linux/module.h>
41 #include <linux/spinlock.h>
42 #include <linux/serial.h>
43 #include <linux/ioctl.h>
44 #include <linux/wait.h>
45 #include <linux/firmware.h>
46 #include <linux/ihex.h>
47 #include <linux/uaccess.h>
48 #include <linux/usb.h>
49 #include <linux/usb/serial.h>
50 #include "io_edgeport.h"
51 #include "io_ionsp.h"           /* info for the iosp messages */
52 #include "io_16654.h"           /* 16654 UART defines */
53
54 /*
55  * Version Information
56  */
57 #define DRIVER_VERSION "v2.7"
58 #define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com> and David Iacovelli"
59 #define DRIVER_DESC "Edgeport USB Serial Driver"
60
61 #define MAX_NAME_LEN            64
62
63 #define CHASE_TIMEOUT           (5*HZ)          /* 5 seconds */
64 #define OPEN_TIMEOUT            (5*HZ)          /* 5 seconds */
65 #define COMMAND_TIMEOUT         (5*HZ)          /* 5 seconds */
66
67 /* receive port state */
68 enum RXSTATE {
69         EXPECT_HDR1 = 0,    /* Expect header byte 1 */
70         EXPECT_HDR2 = 1,    /* Expect header byte 2 */
71         EXPECT_DATA = 2,    /* Expect 'RxBytesRemaining' data */
72         EXPECT_HDR3 = 3,    /* Expect header byte 3 (for status hdrs only) */
73 };
74
75
76 /* Transmit Fifo
77  * This Transmit queue is an extension of the edgeport Rx buffer.
78  * The maximum amount of data buffered in both the edgeport
79  * Rx buffer (maxTxCredits) and this buffer will never exceed maxTxCredits.
80  */
81 struct TxFifo {
82         unsigned int    head;   /* index to head pointer (write) */
83         unsigned int    tail;   /* index to tail pointer (read)  */
84         unsigned int    count;  /* Bytes in queue */
85         unsigned int    size;   /* Max size of queue (equal to Max number of TxCredits) */
86         unsigned char   *fifo;  /* allocated Buffer */
87 };
88
89 /* This structure holds all of the local port information */
90 struct edgeport_port {
91         __u16                   txCredits;              /* our current credits for this port */
92         __u16                   maxTxCredits;           /* the max size of the port */
93
94         struct TxFifo           txfifo;                 /* transmit fifo -- size will be maxTxCredits */
95         struct urb              *write_urb;             /* write URB for this port */
96         bool                    write_in_progress;      /* 'true' while a write URB is outstanding */
97         spinlock_t              ep_lock;
98
99         __u8                    shadowLCR;              /* last LCR value received */
100         __u8                    shadowMCR;              /* last MCR value received */
101         __u8                    shadowMSR;              /* last MSR value received */
102         __u8                    shadowLSR;              /* last LSR value received */
103         __u8                    shadowXonChar;          /* last value set as XON char in Edgeport */
104         __u8                    shadowXoffChar;         /* last value set as XOFF char in Edgeport */
105         __u8                    validDataMask;
106         __u32                   baudRate;
107
108         bool                    open;
109         bool                    openPending;
110         bool                    commandPending;
111         bool                    closePending;
112         bool                    chaseResponsePending;
113
114         wait_queue_head_t       wait_chase;             /* for handling sleeping while waiting for chase to finish */
115         wait_queue_head_t       wait_open;              /* for handling sleeping while waiting for open to finish */
116         wait_queue_head_t       wait_command;           /* for handling sleeping while waiting for command to finish */
117         wait_queue_head_t       delta_msr_wait;         /* for handling sleeping while waiting for msr change to happen */
118
119         struct async_icount     icount;
120         struct usb_serial_port  *port;                  /* loop back to the owner of this object */
121 };
122
123
124 /* This structure holds all of the individual device information */
125 struct edgeport_serial {
126         char                    name[MAX_NAME_LEN+2];           /* string name of this device */
127
128         struct edge_manuf_descriptor    manuf_descriptor;       /* the manufacturer descriptor */
129         struct edge_boot_descriptor     boot_descriptor;        /* the boot firmware descriptor */
130         struct edgeport_product_info    product_info;           /* Product Info */
131         struct edge_compatibility_descriptor epic_descriptor;   /* Edgeport compatible descriptor */
132         int                     is_epic;                        /* flag if EPiC device or not */
133
134         __u8                    interrupt_in_endpoint;          /* the interrupt endpoint handle */
135         unsigned char           *interrupt_in_buffer;           /* the buffer we use for the interrupt endpoint */
136         struct urb              *interrupt_read_urb;            /* our interrupt urb */
137
138         __u8                    bulk_in_endpoint;               /* the bulk in endpoint handle */
139         unsigned char           *bulk_in_buffer;                /* the buffer we use for the bulk in endpoint */
140         struct urb              *read_urb;                      /* our bulk read urb */
141         bool                    read_in_progress;
142         spinlock_t              es_lock;
143
144         __u8                    bulk_out_endpoint;              /* the bulk out endpoint handle */
145
146         __s16                   rxBytesAvail;                   /* the number of bytes that we need to read from this device */
147
148         enum RXSTATE            rxState;                        /* the current state of the bulk receive processor */
149         __u8                    rxHeader1;                      /* receive header byte 1 */
150         __u8                    rxHeader2;                      /* receive header byte 2 */
151         __u8                    rxHeader3;                      /* receive header byte 3 */
152         __u8                    rxPort;                         /* the port that we are currently receiving data for */
153         __u8                    rxStatusCode;                   /* the receive status code */
154         __u8                    rxStatusParam;                  /* the receive status paramater */
155         __s16                   rxBytesRemaining;               /* the number of port bytes left to read */
156         struct usb_serial       *serial;                        /* loop back to the owner of this object */
157 };
158
159 /* baud rate information */
160 struct divisor_table_entry {
161         __u32   BaudRate;
162         __u16  Divisor;
163 };
164
165 /*
166  * Define table of divisors for Rev A EdgePort/4 hardware
167  * These assume a 3.6864MHz crystal, the standard /16, and
168  * MCR.7 = 0.
169  */
170
171 static const struct divisor_table_entry divisor_table[] = {
172         {   50,         4608},
173         {   75,         3072},
174         {   110,        2095},  /* 2094.545455 => 230450   => .0217 % over */
175         {   134,        1713},  /* 1713.011152 => 230398.5 => .00065% under */
176         {   150,        1536},
177         {   300,        768},
178         {   600,        384},
179         {   1200,       192},
180         {   1800,       128},
181         {   2400,       96},
182         {   4800,       48},
183         {   7200,       32},
184         {   9600,       24},
185         {   14400,      16},
186         {   19200,      12},
187         {   38400,      6},
188         {   57600,      4},
189         {   115200,     2},
190         {   230400,     1},
191 };
192
193 /* local variables */
194 static bool debug;
195
196 /* Number of outstanding Command Write Urbs */
197 static atomic_t CmdUrbs = ATOMIC_INIT(0);
198
199
200 /* local function prototypes */
201
202 /* function prototypes for all URB callbacks */
203 static void edge_interrupt_callback(struct urb *urb);
204 static void edge_bulk_in_callback(struct urb *urb);
205 static void edge_bulk_out_data_callback(struct urb *urb);
206 static void edge_bulk_out_cmd_callback(struct urb *urb);
207
208 /* function prototypes for the usbserial callbacks */
209 static int edge_open(struct tty_struct *tty, struct usb_serial_port *port);
210 static void edge_close(struct usb_serial_port *port);
211 static int edge_write(struct tty_struct *tty, struct usb_serial_port *port,
212                                         const unsigned char *buf, int count);
213 static int edge_write_room(struct tty_struct *tty);
214 static int edge_chars_in_buffer(struct tty_struct *tty);
215 static void edge_throttle(struct tty_struct *tty);
216 static void edge_unthrottle(struct tty_struct *tty);
217 static void edge_set_termios(struct tty_struct *tty,
218                                         struct usb_serial_port *port,
219                                         struct ktermios *old_termios);
220 static int  edge_ioctl(struct tty_struct *tty,
221                                         unsigned int cmd, unsigned long arg);
222 static void edge_break(struct tty_struct *tty, int break_state);
223 static int  edge_tiocmget(struct tty_struct *tty);
224 static int  edge_tiocmset(struct tty_struct *tty,
225                                         unsigned int set, unsigned int clear);
226 static int  edge_get_icount(struct tty_struct *tty,
227                                 struct serial_icounter_struct *icount);
228 static int  edge_startup(struct usb_serial *serial);
229 static void edge_disconnect(struct usb_serial *serial);
230 static void edge_release(struct usb_serial *serial);
231
232 #include "io_tables.h"  /* all of the devices that this driver supports */
233
234 /* function prototypes for all of our local functions */
235
236 static void  process_rcvd_data(struct edgeport_serial *edge_serial,
237                                 unsigned char *buffer, __u16 bufferLength);
238 static void process_rcvd_status(struct edgeport_serial *edge_serial,
239                                 __u8 byte2, __u8 byte3);
240 static void edge_tty_recv(struct device *dev, struct tty_struct *tty,
241                                 unsigned char *data, int length);
242 static void handle_new_msr(struct edgeport_port *edge_port, __u8 newMsr);
243 static void handle_new_lsr(struct edgeport_port *edge_port, __u8 lsrData,
244                                 __u8 lsr, __u8 data);
245 static int  send_iosp_ext_cmd(struct edgeport_port *edge_port, __u8 command,
246                                 __u8 param);
247 static int  calc_baud_rate_divisor(struct device *dev, int baud_rate, int *divisor);
248 static int  send_cmd_write_baud_rate(struct edgeport_port *edge_port,
249                                 int baudRate);
250 static void change_port_settings(struct tty_struct *tty,
251                                 struct edgeport_port *edge_port,
252                                 struct ktermios *old_termios);
253 static int  send_cmd_write_uart_register(struct edgeport_port *edge_port,
254                                 __u8 regNum, __u8 regValue);
255 static int  write_cmd_usb(struct edgeport_port *edge_port,
256                                 unsigned char *buffer, int writeLength);
257 static void send_more_port_data(struct edgeport_serial *edge_serial,
258                                 struct edgeport_port *edge_port);
259
260 static int sram_write(struct usb_serial *serial, __u16 extAddr, __u16 addr,
261                                         __u16 length, const __u8 *data);
262 static int rom_read(struct usb_serial *serial, __u16 extAddr, __u16 addr,
263                                                 __u16 length, __u8 *data);
264 static int rom_write(struct usb_serial *serial, __u16 extAddr, __u16 addr,
265                                         __u16 length, const __u8 *data);
266 static void get_manufacturing_desc(struct edgeport_serial *edge_serial);
267 static void get_boot_desc(struct edgeport_serial *edge_serial);
268 static void load_application_firmware(struct edgeport_serial *edge_serial);
269
270 static void unicode_to_ascii(char *string, int buflen,
271                                 __le16 *unicode, int unicode_size);
272
273
274 /* ************************************************************************ */
275 /* ************************************************************************ */
276 /* ************************************************************************ */
277 /* ************************************************************************ */
278
279 /************************************************************************
280  *                                                                      *
281  * update_edgeport_E2PROM()     Compare current versions of             *
282  *                              Boot ROM and Manufacture                *
283  *                              Descriptors with versions               *
284  *                              embedded in this driver                 *
285  *                                                                      *
286  ************************************************************************/
287 static void update_edgeport_E2PROM(struct edgeport_serial *edge_serial)
288 {
289         struct device *dev = &edge_serial->serial->dev->dev;
290         __u32 BootCurVer;
291         __u32 BootNewVer;
292         __u8 BootMajorVersion;
293         __u8 BootMinorVersion;
294         __u16 BootBuildNumber;
295         __u32 Bootaddr;
296         const struct ihex_binrec *rec;
297         const struct firmware *fw;
298         const char *fw_name;
299         int response;
300
301         switch (edge_serial->product_info.iDownloadFile) {
302         case EDGE_DOWNLOAD_FILE_I930:
303                 fw_name = "edgeport/boot.fw";
304                 break;
305         case EDGE_DOWNLOAD_FILE_80251:
306                 fw_name = "edgeport/boot2.fw";
307                 break;
308         default:
309                 return;
310         }
311
312         response = request_ihex_firmware(&fw, fw_name,
313                                          &edge_serial->serial->dev->dev);
314         if (response) {
315                 dev_err(dev, "Failed to load image \"%s\" err %d\n",
316                        fw_name, response);
317                 return;
318         }
319
320         rec = (const struct ihex_binrec *)fw->data;
321         BootMajorVersion = rec->data[0];
322         BootMinorVersion = rec->data[1];
323         BootBuildNumber = (rec->data[2] << 8) | rec->data[3];
324
325         /* Check Boot Image Version */
326         BootCurVer = (edge_serial->boot_descriptor.MajorVersion << 24) +
327                      (edge_serial->boot_descriptor.MinorVersion << 16) +
328                       le16_to_cpu(edge_serial->boot_descriptor.BuildNumber);
329
330         BootNewVer = (BootMajorVersion << 24) +
331                      (BootMinorVersion << 16) +
332                       BootBuildNumber;
333
334         dev_dbg(dev, "Current Boot Image version %d.%d.%d\n",
335             edge_serial->boot_descriptor.MajorVersion,
336             edge_serial->boot_descriptor.MinorVersion,
337             le16_to_cpu(edge_serial->boot_descriptor.BuildNumber));
338
339
340         if (BootNewVer > BootCurVer) {
341                 dev_dbg(dev, "**Update Boot Image from %d.%d.%d to %d.%d.%d\n",
342                     edge_serial->boot_descriptor.MajorVersion,
343                     edge_serial->boot_descriptor.MinorVersion,
344                     le16_to_cpu(edge_serial->boot_descriptor.BuildNumber),
345                     BootMajorVersion, BootMinorVersion, BootBuildNumber);
346
347                 dev_dbg(dev, "Downloading new Boot Image\n");
348
349                 for (rec = ihex_next_binrec(rec); rec;
350                      rec = ihex_next_binrec(rec)) {
351                         Bootaddr = be32_to_cpu(rec->addr);
352                         response = rom_write(edge_serial->serial,
353                                              Bootaddr >> 16,
354                                              Bootaddr & 0xFFFF,
355                                              be16_to_cpu(rec->len),
356                                              &rec->data[0]);
357                         if (response < 0) {
358                                 dev_err(&edge_serial->serial->dev->dev,
359                                         "rom_write failed (%x, %x, %d)\n",
360                                         Bootaddr >> 16, Bootaddr & 0xFFFF,
361                                         be16_to_cpu(rec->len));
362                                 break;
363                         }
364                 }
365         } else {
366                 dev_dbg(dev, "Boot Image -- already up to date\n");
367         }
368         release_firmware(fw);
369 }
370
371 #if 0
372 /************************************************************************
373  *
374  *  Get string descriptor from device
375  *
376  ************************************************************************/
377 static int get_string_desc(struct usb_device *dev, int Id,
378                                 struct usb_string_descriptor **pRetDesc)
379 {
380         struct usb_string_descriptor StringDesc;
381         struct usb_string_descriptor *pStringDesc;
382
383         dev_dbg(&dev->dev, "%s - USB String ID = %d\n", __func__, Id);
384
385         if (!usb_get_descriptor(dev, USB_DT_STRING, Id, &StringDesc,
386                                                 sizeof(StringDesc)))
387                 return 0;
388
389         pStringDesc = kmalloc(StringDesc.bLength, GFP_KERNEL);
390         if (!pStringDesc)
391                 return -1;
392
393         if (!usb_get_descriptor(dev, USB_DT_STRING, Id, pStringDesc,
394                                                         StringDesc.bLength)) {
395                 kfree(pStringDesc);
396                 return -1;
397         }
398
399         *pRetDesc = pStringDesc;
400         return 0;
401 }
402 #endif
403
404 static void dump_product_info(struct edgeport_serial *edge_serial,
405                               struct edgeport_product_info *product_info)
406 {
407         struct device *dev = &edge_serial->serial->dev->dev;
408
409         /* Dump Product Info structure */
410         dev_dbg(dev, "**Product Information:\n");
411         dev_dbg(dev, "  ProductId             %x\n", product_info->ProductId);
412         dev_dbg(dev, "  NumPorts              %d\n", product_info->NumPorts);
413         dev_dbg(dev, "  ProdInfoVer           %d\n", product_info->ProdInfoVer);
414         dev_dbg(dev, "  IsServer              %d\n", product_info->IsServer);
415         dev_dbg(dev, "  IsRS232               %d\n", product_info->IsRS232);
416         dev_dbg(dev, "  IsRS422               %d\n", product_info->IsRS422);
417         dev_dbg(dev, "  IsRS485               %d\n", product_info->IsRS485);
418         dev_dbg(dev, "  RomSize               %d\n", product_info->RomSize);
419         dev_dbg(dev, "  RamSize               %d\n", product_info->RamSize);
420         dev_dbg(dev, "  CpuRev                %x\n", product_info->CpuRev);
421         dev_dbg(dev, "  BoardRev              %x\n", product_info->BoardRev);
422         dev_dbg(dev, "  BootMajorVersion      %d.%d.%d\n",
423                 product_info->BootMajorVersion,
424                 product_info->BootMinorVersion,
425                 le16_to_cpu(product_info->BootBuildNumber));
426         dev_dbg(dev, "  FirmwareMajorVersion  %d.%d.%d\n",
427                 product_info->FirmwareMajorVersion,
428                 product_info->FirmwareMinorVersion,
429                 le16_to_cpu(product_info->FirmwareBuildNumber));
430         dev_dbg(dev, "  ManufactureDescDate   %d/%d/%d\n",
431                 product_info->ManufactureDescDate[0],
432                 product_info->ManufactureDescDate[1],
433                 product_info->ManufactureDescDate[2]+1900);
434         dev_dbg(dev, "  iDownloadFile         0x%x\n",
435                 product_info->iDownloadFile);
436         dev_dbg(dev, "  EpicVer               %d\n", product_info->EpicVer);
437 }
438
439 static void get_product_info(struct edgeport_serial *edge_serial)
440 {
441         struct edgeport_product_info *product_info = &edge_serial->product_info;
442
443         memset(product_info, 0, sizeof(struct edgeport_product_info));
444
445         product_info->ProductId = (__u16)(le16_to_cpu(edge_serial->serial->dev->descriptor.idProduct) & ~ION_DEVICE_ID_80251_NETCHIP);
446         product_info->NumPorts = edge_serial->manuf_descriptor.NumPorts;
447         product_info->ProdInfoVer = 0;
448
449         product_info->RomSize = edge_serial->manuf_descriptor.RomSize;
450         product_info->RamSize = edge_serial->manuf_descriptor.RamSize;
451         product_info->CpuRev = edge_serial->manuf_descriptor.CpuRev;
452         product_info->BoardRev = edge_serial->manuf_descriptor.BoardRev;
453
454         product_info->BootMajorVersion =
455                                 edge_serial->boot_descriptor.MajorVersion;
456         product_info->BootMinorVersion =
457                                 edge_serial->boot_descriptor.MinorVersion;
458         product_info->BootBuildNumber =
459                                 edge_serial->boot_descriptor.BuildNumber;
460
461         memcpy(product_info->ManufactureDescDate,
462                         edge_serial->manuf_descriptor.DescDate,
463                         sizeof(edge_serial->manuf_descriptor.DescDate));
464
465         /* check if this is 2nd generation hardware */
466         if (le16_to_cpu(edge_serial->serial->dev->descriptor.idProduct)
467                                             & ION_DEVICE_ID_80251_NETCHIP)
468                 product_info->iDownloadFile = EDGE_DOWNLOAD_FILE_80251;
469         else
470                 product_info->iDownloadFile = EDGE_DOWNLOAD_FILE_I930;
471
472         /* Determine Product type and set appropriate flags */
473         switch (DEVICE_ID_FROM_USB_PRODUCT_ID(product_info->ProductId)) {
474         case ION_DEVICE_ID_EDGEPORT_COMPATIBLE:
475         case ION_DEVICE_ID_EDGEPORT_4T:
476         case ION_DEVICE_ID_EDGEPORT_4:
477         case ION_DEVICE_ID_EDGEPORT_2:
478         case ION_DEVICE_ID_EDGEPORT_8_DUAL_CPU:
479         case ION_DEVICE_ID_EDGEPORT_8:
480         case ION_DEVICE_ID_EDGEPORT_421:
481         case ION_DEVICE_ID_EDGEPORT_21:
482         case ION_DEVICE_ID_EDGEPORT_2_DIN:
483         case ION_DEVICE_ID_EDGEPORT_4_DIN:
484         case ION_DEVICE_ID_EDGEPORT_16_DUAL_CPU:
485                 product_info->IsRS232 = 1;
486                 break;
487
488         case ION_DEVICE_ID_EDGEPORT_2I: /* Edgeport/2 RS422/RS485 */
489                 product_info->IsRS422 = 1;
490                 product_info->IsRS485 = 1;
491                 break;
492
493         case ION_DEVICE_ID_EDGEPORT_8I: /* Edgeport/4 RS422 */
494         case ION_DEVICE_ID_EDGEPORT_4I: /* Edgeport/4 RS422 */
495                 product_info->IsRS422 = 1;
496                 break;
497         }
498
499         dump_product_info(edge_serial, product_info);
500 }
501
502 static int get_epic_descriptor(struct edgeport_serial *ep)
503 {
504         int result;
505         struct usb_serial *serial = ep->serial;
506         struct edgeport_product_info *product_info = &ep->product_info;
507         struct edge_compatibility_descriptor *epic = &ep->epic_descriptor;
508         struct edge_compatibility_bits *bits;
509         struct device *dev = &serial->dev->dev;
510
511         ep->is_epic = 0;
512         result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
513                                  USB_REQUEST_ION_GET_EPIC_DESC,
514                                  0xC0, 0x00, 0x00,
515                                  &ep->epic_descriptor,
516                                  sizeof(struct edge_compatibility_descriptor),
517                                  300);
518
519         if (result > 0) {
520                 ep->is_epic = 1;
521                 memset(product_info, 0, sizeof(struct edgeport_product_info));
522
523                 product_info->NumPorts = epic->NumPorts;
524                 product_info->ProdInfoVer = 0;
525                 product_info->FirmwareMajorVersion = epic->MajorVersion;
526                 product_info->FirmwareMinorVersion = epic->MinorVersion;
527                 product_info->FirmwareBuildNumber = epic->BuildNumber;
528                 product_info->iDownloadFile = epic->iDownloadFile;
529                 product_info->EpicVer = epic->EpicVer;
530                 product_info->Epic = epic->Supports;
531                 product_info->ProductId = ION_DEVICE_ID_EDGEPORT_COMPATIBLE;
532                 dump_product_info(ep, product_info);
533
534                 bits = &ep->epic_descriptor.Supports;
535                 dev_dbg(dev, "**EPIC descriptor:\n");
536                 dev_dbg(dev, "  VendEnableSuspend: %s\n", bits->VendEnableSuspend ? "TRUE": "FALSE");
537                 dev_dbg(dev, "  IOSPOpen         : %s\n", bits->IOSPOpen        ? "TRUE": "FALSE");
538                 dev_dbg(dev, "  IOSPClose        : %s\n", bits->IOSPClose       ? "TRUE": "FALSE");
539                 dev_dbg(dev, "  IOSPChase        : %s\n", bits->IOSPChase       ? "TRUE": "FALSE");
540                 dev_dbg(dev, "  IOSPSetRxFlow    : %s\n", bits->IOSPSetRxFlow   ? "TRUE": "FALSE");
541                 dev_dbg(dev, "  IOSPSetTxFlow    : %s\n", bits->IOSPSetTxFlow   ? "TRUE": "FALSE");
542                 dev_dbg(dev, "  IOSPSetXChar     : %s\n", bits->IOSPSetXChar    ? "TRUE": "FALSE");
543                 dev_dbg(dev, "  IOSPRxCheck      : %s\n", bits->IOSPRxCheck     ? "TRUE": "FALSE");
544                 dev_dbg(dev, "  IOSPSetClrBreak  : %s\n", bits->IOSPSetClrBreak ? "TRUE": "FALSE");
545                 dev_dbg(dev, "  IOSPWriteMCR     : %s\n", bits->IOSPWriteMCR    ? "TRUE": "FALSE");
546                 dev_dbg(dev, "  IOSPWriteLCR     : %s\n", bits->IOSPWriteLCR    ? "TRUE": "FALSE");
547                 dev_dbg(dev, "  IOSPSetBaudRate  : %s\n", bits->IOSPSetBaudRate ? "TRUE": "FALSE");
548                 dev_dbg(dev, "  TrueEdgeport     : %s\n", bits->TrueEdgeport    ? "TRUE": "FALSE");
549         }
550
551         return result;
552 }
553
554
555 /************************************************************************/
556 /************************************************************************/
557 /*            U S B  C A L L B A C K   F U N C T I O N S                */
558 /*            U S B  C A L L B A C K   F U N C T I O N S                */
559 /************************************************************************/
560 /************************************************************************/
561
562 /*****************************************************************************
563  * edge_interrupt_callback
564  *      this is the callback function for when we have received data on the
565  *      interrupt endpoint.
566  *****************************************************************************/
567 static void edge_interrupt_callback(struct urb *urb)
568 {
569         struct edgeport_serial *edge_serial = urb->context;
570         struct device *dev;
571         struct edgeport_port *edge_port;
572         struct usb_serial_port *port;
573         struct tty_struct *tty;
574         unsigned char *data = urb->transfer_buffer;
575         int length = urb->actual_length;
576         int bytes_avail;
577         int position;
578         int txCredits;
579         int portNumber;
580         int result;
581         int status = urb->status;
582
583         switch (status) {
584         case 0:
585                 /* success */
586                 break;
587         case -ECONNRESET:
588         case -ENOENT:
589         case -ESHUTDOWN:
590                 /* this urb is terminated, clean up */
591                 dev_dbg(&urb->dev->dev, "%s - urb shutting down with status: %d\n", __func__, status);
592                 return;
593         default:
594                 dev_dbg(&urb->dev->dev, "%s - nonzero urb status received: %d\n", __func__, status);
595                 goto exit;
596         }
597
598         dev = &edge_serial->serial->dev->dev;
599
600         /* process this interrupt-read even if there are no ports open */
601         if (length) {
602                 usb_serial_debug_data(dev, __func__, length, data);
603
604                 if (length > 1) {
605                         bytes_avail = data[0] | (data[1] << 8);
606                         if (bytes_avail) {
607                                 spin_lock(&edge_serial->es_lock);
608                                 edge_serial->rxBytesAvail += bytes_avail;
609                                 dev_dbg(dev,
610                                         "%s - bytes_avail=%d, rxBytesAvail=%d, read_in_progress=%d\n",
611                                         __func__, bytes_avail,
612                                         edge_serial->rxBytesAvail,
613                                         edge_serial->read_in_progress);
614
615                                 if (edge_serial->rxBytesAvail > 0 &&
616                                     !edge_serial->read_in_progress) {
617                                         dev_dbg(dev, "%s - posting a read\n", __func__);
618                                         edge_serial->read_in_progress = true;
619
620                                         /* we have pending bytes on the
621                                            bulk in pipe, send a request */
622                                         result = usb_submit_urb(edge_serial->read_urb, GFP_ATOMIC);
623                                         if (result) {
624                                                 dev_err(dev,
625                                                         "%s - usb_submit_urb(read bulk) failed with result = %d\n",
626                                                         __func__, result);
627                                                 edge_serial->read_in_progress = false;
628                                         }
629                                 }
630                                 spin_unlock(&edge_serial->es_lock);
631                         }
632                 }
633                 /* grab the txcredits for the ports if available */
634                 position = 2;
635                 portNumber = 0;
636                 while ((position < length) &&
637                                 (portNumber < edge_serial->serial->num_ports)) {
638                         txCredits = data[position] | (data[position+1] << 8);
639                         if (txCredits) {
640                                 port = edge_serial->serial->port[portNumber];
641                                 edge_port = usb_get_serial_port_data(port);
642                                 if (edge_port->open) {
643                                         spin_lock(&edge_port->ep_lock);
644                                         edge_port->txCredits += txCredits;
645                                         spin_unlock(&edge_port->ep_lock);
646                                         dev_dbg(dev, "%s - txcredits for port%d = %d\n",
647                                                 __func__, portNumber,
648                                                 edge_port->txCredits);
649
650                                         /* tell the tty driver that something
651                                            has changed */
652                                         tty = tty_port_tty_get(
653                                                 &edge_port->port->port);
654                                         if (tty) {
655                                                 tty_wakeup(tty);
656                                                 tty_kref_put(tty);
657                                         }
658                                         /* Since we have more credit, check
659                                            if more data can be sent */
660                                         send_more_port_data(edge_serial,
661                                                                 edge_port);
662                                 }
663                         }
664                         position += 2;
665                         ++portNumber;
666                 }
667         }
668
669 exit:
670         result = usb_submit_urb(urb, GFP_ATOMIC);
671         if (result)
672                 dev_err(&urb->dev->dev,
673                         "%s - Error %d submitting control urb\n",
674                                                 __func__, result);
675 }
676
677
678 /*****************************************************************************
679  * edge_bulk_in_callback
680  *      this is the callback function for when we have received data on the
681  *      bulk in endpoint.
682  *****************************************************************************/
683 static void edge_bulk_in_callback(struct urb *urb)
684 {
685         struct edgeport_serial  *edge_serial = urb->context;
686         struct device *dev;
687         unsigned char           *data = urb->transfer_buffer;
688         int                     retval;
689         __u16                   raw_data_length;
690         int status = urb->status;
691
692         if (status) {
693                 dev_dbg(&urb->dev->dev, "%s - nonzero read bulk status received: %d\n",
694                         __func__, status);
695                 edge_serial->read_in_progress = false;
696                 return;
697         }
698
699         if (urb->actual_length == 0) {
700                 dev_dbg(&urb->dev->dev, "%s - read bulk callback with no data\n", __func__);
701                 edge_serial->read_in_progress = false;
702                 return;
703         }
704
705         dev = &edge_serial->serial->dev->dev;
706         raw_data_length = urb->actual_length;
707
708         usb_serial_debug_data(dev, __func__, raw_data_length, data);
709
710         spin_lock(&edge_serial->es_lock);
711
712         /* decrement our rxBytes available by the number that we just got */
713         edge_serial->rxBytesAvail -= raw_data_length;
714
715         dev_dbg(dev, "%s - Received = %d, rxBytesAvail %d\n", __func__,
716                 raw_data_length, edge_serial->rxBytesAvail);
717
718         process_rcvd_data(edge_serial, data, urb->actual_length);
719
720         /* check to see if there's any more data for us to read */
721         if (edge_serial->rxBytesAvail > 0) {
722                 dev_dbg(dev, "%s - posting a read\n", __func__);
723                 retval = usb_submit_urb(edge_serial->read_urb, GFP_ATOMIC);
724                 if (retval) {
725                         dev_err(dev,
726                                 "%s - usb_submit_urb(read bulk) failed, retval = %d\n",
727                                 __func__, retval);
728                         edge_serial->read_in_progress = false;
729                 }
730         } else {
731                 edge_serial->read_in_progress = false;
732         }
733
734         spin_unlock(&edge_serial->es_lock);
735 }
736
737
738 /*****************************************************************************
739  * edge_bulk_out_data_callback
740  *      this is the callback function for when we have finished sending
741  *      serial data on the bulk out endpoint.
742  *****************************************************************************/
743 static void edge_bulk_out_data_callback(struct urb *urb)
744 {
745         struct edgeport_port *edge_port = urb->context;
746         struct tty_struct *tty;
747         int status = urb->status;
748
749         if (status) {
750                 dev_dbg(&urb->dev->dev,
751                         "%s - nonzero write bulk status received: %d\n",
752                         __func__, status);
753         }
754
755         tty = tty_port_tty_get(&edge_port->port->port);
756
757         if (tty && edge_port->open) {
758                 /* let the tty driver wakeup if it has a special
759                    write_wakeup function */
760                 tty_wakeup(tty);
761         }
762         tty_kref_put(tty);
763
764         /* Release the Write URB */
765         edge_port->write_in_progress = false;
766
767         /* Check if more data needs to be sent */
768         send_more_port_data((struct edgeport_serial *)
769                 (usb_get_serial_data(edge_port->port->serial)), edge_port);
770 }
771
772
773 /*****************************************************************************
774  * BulkOutCmdCallback
775  *      this is the callback function for when we have finished sending a
776  *      command on the bulk out endpoint.
777  *****************************************************************************/
778 static void edge_bulk_out_cmd_callback(struct urb *urb)
779 {
780         struct edgeport_port *edge_port = urb->context;
781         struct tty_struct *tty;
782         int status = urb->status;
783
784         atomic_dec(&CmdUrbs);
785         dev_dbg(&urb->dev->dev, "%s - FREE URB %p (outstanding %d)\n",
786                 __func__, urb, atomic_read(&CmdUrbs));
787
788
789         /* clean up the transfer buffer */
790         kfree(urb->transfer_buffer);
791
792         /* Free the command urb */
793         usb_free_urb(urb);
794
795         if (status) {
796                 dev_dbg(&urb->dev->dev,
797                         "%s - nonzero write bulk status received: %d\n",
798                         __func__, status);
799                 return;
800         }
801
802         /* Get pointer to tty */
803         tty = tty_port_tty_get(&edge_port->port->port);
804
805         /* tell the tty driver that something has changed */
806         if (tty && edge_port->open)
807                 tty_wakeup(tty);
808         tty_kref_put(tty);
809
810         /* we have completed the command */
811         edge_port->commandPending = false;
812         wake_up(&edge_port->wait_command);
813 }
814
815
816 /*****************************************************************************
817  * Driver tty interface functions
818  *****************************************************************************/
819
820 /*****************************************************************************
821  * SerialOpen
822  *      this function is called by the tty driver when a port is opened
823  *      If successful, we return 0
824  *      Otherwise we return a negative error number.
825  *****************************************************************************/
826 static int edge_open(struct tty_struct *tty, struct usb_serial_port *port)
827 {
828         struct edgeport_port *edge_port = usb_get_serial_port_data(port);
829         struct device *dev = &port->dev;
830         struct usb_serial *serial;
831         struct edgeport_serial *edge_serial;
832         int response;
833
834         if (edge_port == NULL)
835                 return -ENODEV;
836
837         /* see if we've set up our endpoint info yet (can't set it up
838            in edge_startup as the structures were not set up at that time.) */
839         serial = port->serial;
840         edge_serial = usb_get_serial_data(serial);
841         if (edge_serial == NULL)
842                 return -ENODEV;
843         if (edge_serial->interrupt_in_buffer == NULL) {
844                 struct usb_serial_port *port0 = serial->port[0];
845
846                 /* not set up yet, so do it now */
847                 edge_serial->interrupt_in_buffer =
848                                         port0->interrupt_in_buffer;
849                 edge_serial->interrupt_in_endpoint =
850                                         port0->interrupt_in_endpointAddress;
851                 edge_serial->interrupt_read_urb = port0->interrupt_in_urb;
852                 edge_serial->bulk_in_buffer = port0->bulk_in_buffer;
853                 edge_serial->bulk_in_endpoint =
854                                         port0->bulk_in_endpointAddress;
855                 edge_serial->read_urb = port0->read_urb;
856                 edge_serial->bulk_out_endpoint =
857                                         port0->bulk_out_endpointAddress;
858
859                 /* set up our interrupt urb */
860                 usb_fill_int_urb(edge_serial->interrupt_read_urb,
861                       serial->dev,
862                       usb_rcvintpipe(serial->dev,
863                                 port0->interrupt_in_endpointAddress),
864                       port0->interrupt_in_buffer,
865                       edge_serial->interrupt_read_urb->transfer_buffer_length,
866                       edge_interrupt_callback, edge_serial,
867                       edge_serial->interrupt_read_urb->interval);
868
869                 /* set up our bulk in urb */
870                 usb_fill_bulk_urb(edge_serial->read_urb, serial->dev,
871                         usb_rcvbulkpipe(serial->dev,
872                                 port0->bulk_in_endpointAddress),
873                         port0->bulk_in_buffer,
874                         edge_serial->read_urb->transfer_buffer_length,
875                         edge_bulk_in_callback, edge_serial);
876                 edge_serial->read_in_progress = false;
877
878                 /* start interrupt read for this edgeport
879                  * this interrupt will continue as long
880                  * as the edgeport is connected */
881                 response = usb_submit_urb(edge_serial->interrupt_read_urb,
882                                                                 GFP_KERNEL);
883                 if (response) {
884                         dev_err(dev, "%s - Error %d submitting control urb\n",
885                                 __func__, response);
886                 }
887         }
888
889         /* initialize our wait queues */
890         init_waitqueue_head(&edge_port->wait_open);
891         init_waitqueue_head(&edge_port->wait_chase);
892         init_waitqueue_head(&edge_port->delta_msr_wait);
893         init_waitqueue_head(&edge_port->wait_command);
894
895         /* initialize our icount structure */
896         memset(&(edge_port->icount), 0x00, sizeof(edge_port->icount));
897
898         /* initialize our port settings */
899         edge_port->txCredits = 0;       /* Can't send any data yet */
900         /* Must always set this bit to enable ints! */
901         edge_port->shadowMCR = MCR_MASTER_IE;
902         edge_port->chaseResponsePending = false;
903
904         /* send a open port command */
905         edge_port->openPending = true;
906         edge_port->open        = false;
907         response = send_iosp_ext_cmd(edge_port, IOSP_CMD_OPEN_PORT, 0);
908
909         if (response < 0) {
910                 dev_err(dev, "%s - error sending open port command\n", __func__);
911                 edge_port->openPending = false;
912                 return -ENODEV;
913         }
914
915         /* now wait for the port to be completely opened */
916         wait_event_timeout(edge_port->wait_open, !edge_port->openPending,
917                                                                 OPEN_TIMEOUT);
918
919         if (!edge_port->open) {
920                 /* open timed out */
921                 dev_dbg(dev, "%s - open timedout\n", __func__);
922                 edge_port->openPending = false;
923                 return -ENODEV;
924         }
925
926         /* create the txfifo */
927         edge_port->txfifo.head  = 0;
928         edge_port->txfifo.tail  = 0;
929         edge_port->txfifo.count = 0;
930         edge_port->txfifo.size  = edge_port->maxTxCredits;
931         edge_port->txfifo.fifo  = kmalloc(edge_port->maxTxCredits, GFP_KERNEL);
932
933         if (!edge_port->txfifo.fifo) {
934                 dev_dbg(dev, "%s - no memory\n", __func__);
935                 edge_close(port);
936                 return -ENOMEM;
937         }
938
939         /* Allocate a URB for the write */
940         edge_port->write_urb = usb_alloc_urb(0, GFP_KERNEL);
941         edge_port->write_in_progress = false;
942
943         if (!edge_port->write_urb) {
944                 dev_dbg(dev, "%s - no memory\n", __func__);
945                 edge_close(port);
946                 return -ENOMEM;
947         }
948
949         dev_dbg(dev, "%s(%d) - Initialize TX fifo to %d bytes\n",
950                 __func__, port->number, edge_port->maxTxCredits);
951
952         return 0;
953 }
954
955
956 /************************************************************************
957  *
958  * block_until_chase_response
959  *
960  *      This function will block the close until one of the following:
961  *              1. Response to our Chase comes from Edgeport
962  *              2. A timeout of 10 seconds without activity has expired
963  *                 (1K of Edgeport data @ 2400 baud ==> 4 sec to empty)
964  *
965  ************************************************************************/
966 static void block_until_chase_response(struct edgeport_port *edge_port)
967 {
968         struct device *dev = &edge_port->port->dev;
969         DEFINE_WAIT(wait);
970         __u16 lastCredits;
971         int timeout = 1*HZ;
972         int loop = 10;
973
974         while (1) {
975                 /* Save Last credits */
976                 lastCredits = edge_port->txCredits;
977
978                 /* Did we get our Chase response */
979                 if (!edge_port->chaseResponsePending) {
980                         dev_dbg(dev, "%s - Got Chase Response\n", __func__);
981
982                         /* did we get all of our credit back? */
983                         if (edge_port->txCredits == edge_port->maxTxCredits) {
984                                 dev_dbg(dev, "%s - Got all credits\n", __func__);
985                                 return;
986                         }
987                 }
988
989                 /* Block the thread for a while */
990                 prepare_to_wait(&edge_port->wait_chase, &wait,
991                                                 TASK_UNINTERRUPTIBLE);
992                 schedule_timeout(timeout);
993                 finish_wait(&edge_port->wait_chase, &wait);
994
995                 if (lastCredits == edge_port->txCredits) {
996                         /* No activity.. count down. */
997                         loop--;
998                         if (loop == 0) {
999                                 edge_port->chaseResponsePending = false;
1000                                 dev_dbg(dev, "%s - Chase TIMEOUT\n", __func__);
1001                                 return;
1002                         }
1003                 } else {
1004                         /* Reset timeout value back to 10 seconds */
1005                         dev_dbg(dev, "%s - Last %d, Current %d\n", __func__,
1006                                         lastCredits, edge_port->txCredits);
1007                         loop = 10;
1008                 }
1009         }
1010 }
1011
1012
1013 /************************************************************************
1014  *
1015  * block_until_tx_empty
1016  *
1017  *      This function will block the close until one of the following:
1018  *              1. TX count are 0
1019  *              2. The edgeport has stopped
1020  *              3. A timeout of 3 seconds without activity has expired
1021  *
1022  ************************************************************************/
1023 static void block_until_tx_empty(struct edgeport_port *edge_port)
1024 {
1025         struct device *dev = &edge_port->port->dev;
1026         DEFINE_WAIT(wait);
1027         struct TxFifo *fifo = &edge_port->txfifo;
1028         __u32 lastCount;
1029         int timeout = HZ/10;
1030         int loop = 30;
1031
1032         while (1) {
1033                 /* Save Last count */
1034                 lastCount = fifo->count;
1035
1036                 /* Is the Edgeport Buffer empty? */
1037                 if (lastCount == 0) {
1038                         dev_dbg(dev, "%s - TX Buffer Empty\n", __func__);
1039                         return;
1040                 }
1041
1042                 /* Block the thread for a while */
1043                 prepare_to_wait(&edge_port->wait_chase, &wait,
1044                                                 TASK_UNINTERRUPTIBLE);
1045                 schedule_timeout(timeout);
1046                 finish_wait(&edge_port->wait_chase, &wait);
1047
1048                 dev_dbg(dev, "%s wait\n", __func__);
1049
1050                 if (lastCount == fifo->count) {
1051                         /* No activity.. count down. */
1052                         loop--;
1053                         if (loop == 0) {
1054                                 dev_dbg(dev, "%s - TIMEOUT\n", __func__);
1055                                 return;
1056                         }
1057                 } else {
1058                         /* Reset timeout value back to seconds */
1059                         loop = 30;
1060                 }
1061         }
1062 }
1063
1064
1065 /*****************************************************************************
1066  * edge_close
1067  *      this function is called by the tty driver when a port is closed
1068  *****************************************************************************/
1069 static void edge_close(struct usb_serial_port *port)
1070 {
1071         struct edgeport_serial *edge_serial;
1072         struct edgeport_port *edge_port;
1073         int status;
1074
1075         edge_serial = usb_get_serial_data(port->serial);
1076         edge_port = usb_get_serial_port_data(port);
1077         if (edge_serial == NULL || edge_port == NULL)
1078                 return;
1079
1080         /* block until tx is empty */
1081         block_until_tx_empty(edge_port);
1082
1083         edge_port->closePending = true;
1084
1085         if ((!edge_serial->is_epic) ||
1086             ((edge_serial->is_epic) &&
1087              (edge_serial->epic_descriptor.Supports.IOSPChase))) {
1088                 /* flush and chase */
1089                 edge_port->chaseResponsePending = true;
1090
1091                 dev_dbg(&port->dev, "%s - Sending IOSP_CMD_CHASE_PORT\n", __func__);
1092                 status = send_iosp_ext_cmd(edge_port, IOSP_CMD_CHASE_PORT, 0);
1093                 if (status == 0)
1094                         /* block until chase finished */
1095                         block_until_chase_response(edge_port);
1096                 else
1097                         edge_port->chaseResponsePending = false;
1098         }
1099
1100         if ((!edge_serial->is_epic) ||
1101             ((edge_serial->is_epic) &&
1102              (edge_serial->epic_descriptor.Supports.IOSPClose))) {
1103                /* close the port */
1104                 dev_dbg(&port->dev, "%s - Sending IOSP_CMD_CLOSE_PORT\n", __func__);
1105                 send_iosp_ext_cmd(edge_port, IOSP_CMD_CLOSE_PORT, 0);
1106         }
1107
1108         /* port->close = true; */
1109         edge_port->closePending = false;
1110         edge_port->open = false;
1111         edge_port->openPending = false;
1112
1113         usb_kill_urb(edge_port->write_urb);
1114
1115         if (edge_port->write_urb) {
1116                 /* if this urb had a transfer buffer already
1117                                 (old transfer) free it */
1118                 kfree(edge_port->write_urb->transfer_buffer);
1119                 usb_free_urb(edge_port->write_urb);
1120                 edge_port->write_urb = NULL;
1121         }
1122         kfree(edge_port->txfifo.fifo);
1123         edge_port->txfifo.fifo = NULL;
1124 }
1125
1126 /*****************************************************************************
1127  * SerialWrite
1128  *      this function is called by the tty driver when data should be written
1129  *      to the port.
1130  *      If successful, we return the number of bytes written, otherwise we
1131  *      return a negative error number.
1132  *****************************************************************************/
1133 static int edge_write(struct tty_struct *tty, struct usb_serial_port *port,
1134                                         const unsigned char *data, int count)
1135 {
1136         struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1137         struct TxFifo *fifo;
1138         int copySize;
1139         int bytesleft;
1140         int firsthalf;
1141         int secondhalf;
1142         unsigned long flags;
1143
1144         if (edge_port == NULL)
1145                 return -ENODEV;
1146
1147         /* get a pointer to the Tx fifo */
1148         fifo = &edge_port->txfifo;
1149
1150         spin_lock_irqsave(&edge_port->ep_lock, flags);
1151
1152         /* calculate number of bytes to put in fifo */
1153         copySize = min((unsigned int)count,
1154                                 (edge_port->txCredits - fifo->count));
1155
1156         dev_dbg(&port->dev, "%s(%d) of %d byte(s) Fifo room  %d -- will copy %d bytes\n",
1157                 __func__, port->number, count,
1158                         edge_port->txCredits - fifo->count, copySize);
1159
1160         /* catch writes of 0 bytes which the tty driver likes to give us,
1161            and when txCredits is empty */
1162         if (copySize == 0) {
1163                 dev_dbg(&port->dev, "%s - copySize = Zero\n", __func__);
1164                 goto finish_write;
1165         }
1166
1167         /* queue the data
1168          * since we can never overflow the buffer we do not have to check for a
1169          * full condition
1170          *
1171          * the copy is done is two parts -- first fill to the end of the buffer
1172          * then copy the reset from the start of the buffer
1173          */
1174         bytesleft = fifo->size - fifo->head;
1175         firsthalf = min(bytesleft, copySize);
1176         dev_dbg(&port->dev, "%s - copy %d bytes of %d into fifo \n", __func__,
1177                 firsthalf, bytesleft);
1178
1179         /* now copy our data */
1180         memcpy(&fifo->fifo[fifo->head], data, firsthalf);
1181         usb_serial_debug_data(&port->dev, __func__, firsthalf, &fifo->fifo[fifo->head]);
1182
1183         /* update the index and size */
1184         fifo->head  += firsthalf;
1185         fifo->count += firsthalf;
1186
1187         /* wrap the index */
1188         if (fifo->head == fifo->size)
1189                 fifo->head = 0;
1190
1191         secondhalf = copySize-firsthalf;
1192
1193         if (secondhalf) {
1194                 dev_dbg(&port->dev, "%s - copy rest of data %d\n", __func__, secondhalf);
1195                 memcpy(&fifo->fifo[fifo->head], &data[firsthalf], secondhalf);
1196                 usb_serial_debug_data(&port->dev, __func__, secondhalf, &fifo->fifo[fifo->head]);
1197                 /* update the index and size */
1198                 fifo->count += secondhalf;
1199                 fifo->head  += secondhalf;
1200                 /* No need to check for wrap since we can not get to end of
1201                  * the fifo in this part
1202                  */
1203         }
1204
1205 finish_write:
1206         spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1207
1208         send_more_port_data((struct edgeport_serial *)
1209                         usb_get_serial_data(port->serial), edge_port);
1210
1211         dev_dbg(&port->dev, "%s wrote %d byte(s) TxCredits %d, Fifo %d\n",
1212                 __func__, copySize, edge_port->txCredits, fifo->count);
1213
1214         return copySize;
1215 }
1216
1217
1218 /************************************************************************
1219  *
1220  * send_more_port_data()
1221  *
1222  *      This routine attempts to write additional UART transmit data
1223  *      to a port over the USB bulk pipe. It is called (1) when new
1224  *      data has been written to a port's TxBuffer from higher layers
1225  *      (2) when the peripheral sends us additional TxCredits indicating
1226  *      that it can accept more Tx data for a given port; and (3) when
1227  *      a bulk write completes successfully and we want to see if we
1228  *      can transmit more.
1229  *
1230  ************************************************************************/
1231 static void send_more_port_data(struct edgeport_serial *edge_serial,
1232                                         struct edgeport_port *edge_port)
1233 {
1234         struct TxFifo   *fifo = &edge_port->txfifo;
1235         struct device   *dev = &edge_port->port->dev;
1236         struct urb      *urb;
1237         unsigned char   *buffer;
1238         int             status;
1239         int             count;
1240         int             bytesleft;
1241         int             firsthalf;
1242         int             secondhalf;
1243         unsigned long   flags;
1244
1245         spin_lock_irqsave(&edge_port->ep_lock, flags);
1246
1247         if (edge_port->write_in_progress ||
1248             !edge_port->open             ||
1249             (fifo->count == 0)) {
1250                 dev_dbg(dev, "%s(%d) EXIT - fifo %d, PendingWrite = %d\n",
1251                         __func__, edge_port->port->number,
1252                         fifo->count, edge_port->write_in_progress);
1253                 goto exit_send;
1254         }
1255
1256         /* since the amount of data in the fifo will always fit into the
1257          * edgeport buffer we do not need to check the write length
1258          *
1259          * Do we have enough credits for this port to make it worthwhile
1260          * to bother queueing a write. If it's too small, say a few bytes,
1261          * it's better to wait for more credits so we can do a larger write.
1262          */
1263         if (edge_port->txCredits < EDGE_FW_GET_TX_CREDITS_SEND_THRESHOLD(edge_port->maxTxCredits, EDGE_FW_BULK_MAX_PACKET_SIZE)) {
1264                 dev_dbg(dev, "%s(%d) Not enough credit - fifo %d TxCredit %d\n",
1265                         __func__, edge_port->port->number, fifo->count,
1266                         edge_port->txCredits);
1267                 goto exit_send;
1268         }
1269
1270         /* lock this write */
1271         edge_port->write_in_progress = true;
1272
1273         /* get a pointer to the write_urb */
1274         urb = edge_port->write_urb;
1275
1276         /* make sure transfer buffer is freed */
1277         kfree(urb->transfer_buffer);
1278         urb->transfer_buffer = NULL;
1279
1280         /* build the data header for the buffer and port that we are about
1281            to send out */
1282         count = fifo->count;
1283         buffer = kmalloc(count+2, GFP_ATOMIC);
1284         if (buffer == NULL) {
1285                 dev_err_console(edge_port->port,
1286                                 "%s - no more kernel memory...\n", __func__);
1287                 edge_port->write_in_progress = false;
1288                 goto exit_send;
1289         }
1290         buffer[0] = IOSP_BUILD_DATA_HDR1(edge_port->port->number
1291                                 - edge_port->port->serial->minor, count);
1292         buffer[1] = IOSP_BUILD_DATA_HDR2(edge_port->port->number
1293                                 - edge_port->port->serial->minor, count);
1294
1295         /* now copy our data */
1296         bytesleft =  fifo->size - fifo->tail;
1297         firsthalf = min(bytesleft, count);
1298         memcpy(&buffer[2], &fifo->fifo[fifo->tail], firsthalf);
1299         fifo->tail  += firsthalf;
1300         fifo->count -= firsthalf;
1301         if (fifo->tail == fifo->size)
1302                 fifo->tail = 0;
1303
1304         secondhalf = count-firsthalf;
1305         if (secondhalf) {
1306                 memcpy(&buffer[2+firsthalf], &fifo->fifo[fifo->tail],
1307                                                                 secondhalf);
1308                 fifo->tail  += secondhalf;
1309                 fifo->count -= secondhalf;
1310         }
1311
1312         if (count)
1313                 usb_serial_debug_data(&edge_port->port->dev, __func__, count, &buffer[2]);
1314
1315         /* fill up the urb with all of our data and submit it */
1316         usb_fill_bulk_urb(urb, edge_serial->serial->dev,
1317                         usb_sndbulkpipe(edge_serial->serial->dev,
1318                                         edge_serial->bulk_out_endpoint),
1319                         buffer, count+2,
1320                         edge_bulk_out_data_callback, edge_port);
1321
1322         /* decrement the number of credits we have by the number we just sent */
1323         edge_port->txCredits -= count;
1324         edge_port->icount.tx += count;
1325
1326         status = usb_submit_urb(urb, GFP_ATOMIC);
1327         if (status) {
1328                 /* something went wrong */
1329                 dev_err_console(edge_port->port,
1330                         "%s - usb_submit_urb(write bulk) failed, status = %d, data lost\n",
1331                                 __func__, status);
1332                 edge_port->write_in_progress = false;
1333
1334                 /* revert the credits as something bad happened. */
1335                 edge_port->txCredits += count;
1336                 edge_port->icount.tx -= count;
1337         }
1338         dev_dbg(dev, "%s wrote %d byte(s) TxCredit %d, Fifo %d\n",
1339                 __func__, count, edge_port->txCredits, fifo->count);
1340
1341 exit_send:
1342         spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1343 }
1344
1345
1346 /*****************************************************************************
1347  * edge_write_room
1348  *      this function is called by the tty driver when it wants to know how
1349  *      many bytes of data we can accept for a specific port. If successful,
1350  *      we return the amount of room that we have for this port (the txCredits)
1351  *      otherwise we return a negative error number.
1352  *****************************************************************************/
1353 static int edge_write_room(struct tty_struct *tty)
1354 {
1355         struct usb_serial_port *port = tty->driver_data;
1356         struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1357         int room;
1358         unsigned long flags;
1359
1360         if (edge_port == NULL)
1361                 return 0;
1362         if (edge_port->closePending)
1363                 return 0;
1364
1365         if (!edge_port->open) {
1366                 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
1367                 return 0;
1368         }
1369
1370         /* total of both buffers is still txCredit */
1371         spin_lock_irqsave(&edge_port->ep_lock, flags);
1372         room = edge_port->txCredits - edge_port->txfifo.count;
1373         spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1374
1375         dev_dbg(&port->dev, "%s - returns %d\n", __func__, room);
1376         return room;
1377 }
1378
1379
1380 /*****************************************************************************
1381  * edge_chars_in_buffer
1382  *      this function is called by the tty driver when it wants to know how
1383  *      many bytes of data we currently have outstanding in the port (data that
1384  *      has been written, but hasn't made it out the port yet)
1385  *      If successful, we return the number of bytes left to be written in the
1386  *      system,
1387  *      Otherwise we return a negative error number.
1388  *****************************************************************************/
1389 static int edge_chars_in_buffer(struct tty_struct *tty)
1390 {
1391         struct usb_serial_port *port = tty->driver_data;
1392         struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1393         int num_chars;
1394         unsigned long flags;
1395
1396         if (edge_port == NULL)
1397                 return 0;
1398         if (edge_port->closePending)
1399                 return 0;
1400
1401         if (!edge_port->open) {
1402                 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
1403                 return 0;
1404         }
1405
1406         spin_lock_irqsave(&edge_port->ep_lock, flags);
1407         num_chars = edge_port->maxTxCredits - edge_port->txCredits +
1408                                                 edge_port->txfifo.count;
1409         spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1410         if (num_chars) {
1411                 dev_dbg(&port->dev, "%s(port %d) - returns %d\n", __func__,
1412                         port->number, num_chars);
1413         }
1414
1415         return num_chars;
1416 }
1417
1418
1419 /*****************************************************************************
1420  * SerialThrottle
1421  *      this function is called by the tty driver when it wants to stop the data
1422  *      being read from the port.
1423  *****************************************************************************/
1424 static void edge_throttle(struct tty_struct *tty)
1425 {
1426         struct usb_serial_port *port = tty->driver_data;
1427         struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1428         int status;
1429
1430         if (edge_port == NULL)
1431                 return;
1432
1433         if (!edge_port->open) {
1434                 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
1435                 return;
1436         }
1437
1438         /* if we are implementing XON/XOFF, send the stop character */
1439         if (I_IXOFF(tty)) {
1440                 unsigned char stop_char = STOP_CHAR(tty);
1441                 status = edge_write(tty, port, &stop_char, 1);
1442                 if (status <= 0)
1443                         return;
1444         }
1445
1446         /* if we are implementing RTS/CTS, toggle that line */
1447         if (tty->termios->c_cflag & CRTSCTS) {
1448                 edge_port->shadowMCR &= ~MCR_RTS;
1449                 status = send_cmd_write_uart_register(edge_port, MCR,
1450                                                         edge_port->shadowMCR);
1451                 if (status != 0)
1452                         return;
1453         }
1454 }
1455
1456
1457 /*****************************************************************************
1458  * edge_unthrottle
1459  *      this function is called by the tty driver when it wants to resume the
1460  *      data being read from the port (called after SerialThrottle is called)
1461  *****************************************************************************/
1462 static void edge_unthrottle(struct tty_struct *tty)
1463 {
1464         struct usb_serial_port *port = tty->driver_data;
1465         struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1466         int status;
1467
1468         if (edge_port == NULL)
1469                 return;
1470
1471         if (!edge_port->open) {
1472                 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
1473                 return;
1474         }
1475
1476         /* if we are implementing XON/XOFF, send the start character */
1477         if (I_IXOFF(tty)) {
1478                 unsigned char start_char = START_CHAR(tty);
1479                 status = edge_write(tty, port, &start_char, 1);
1480                 if (status <= 0)
1481                         return;
1482         }
1483         /* if we are implementing RTS/CTS, toggle that line */
1484         if (tty->termios->c_cflag & CRTSCTS) {
1485                 edge_port->shadowMCR |= MCR_RTS;
1486                 send_cmd_write_uart_register(edge_port, MCR,
1487                                                 edge_port->shadowMCR);
1488         }
1489 }
1490
1491
1492 /*****************************************************************************
1493  * SerialSetTermios
1494  *      this function is called by the tty driver when it wants to change
1495  * the termios structure
1496  *****************************************************************************/
1497 static void edge_set_termios(struct tty_struct *tty,
1498         struct usb_serial_port *port, struct ktermios *old_termios)
1499 {
1500         struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1501         unsigned int cflag;
1502
1503         cflag = tty->termios->c_cflag;
1504         dev_dbg(&port->dev, "%s - clfag %08x iflag %08x\n", __func__, tty->termios->c_cflag, tty->termios->c_iflag);
1505         dev_dbg(&port->dev, "%s - old clfag %08x old iflag %08x\n", __func__, old_termios->c_cflag, old_termios->c_iflag);
1506
1507         if (edge_port == NULL)
1508                 return;
1509
1510         if (!edge_port->open) {
1511                 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
1512                 return;
1513         }
1514
1515         /* change the port settings to the new ones specified */
1516         change_port_settings(tty, edge_port, old_termios);
1517 }
1518
1519
1520 /*****************************************************************************
1521  * get_lsr_info - get line status register info
1522  *
1523  * Purpose: Let user call ioctl() to get info when the UART physically
1524  *          is emptied.  On bus types like RS485, the transmitter must
1525  *          release the bus after transmitting. This must be done when
1526  *          the transmit shift register is empty, not be done when the
1527  *          transmit holding register is empty.  This functionality
1528  *          allows an RS485 driver to be written in user space.
1529  *****************************************************************************/
1530 static int get_lsr_info(struct edgeport_port *edge_port,
1531                                                 unsigned int __user *value)
1532 {
1533         unsigned int result = 0;
1534         unsigned long flags;
1535
1536         spin_lock_irqsave(&edge_port->ep_lock, flags);
1537         if (edge_port->maxTxCredits == edge_port->txCredits &&
1538             edge_port->txfifo.count == 0) {
1539                 dev_dbg(&edge_port->port->dev, "%s -- Empty\n", __func__);
1540                 result = TIOCSER_TEMT;
1541         }
1542         spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1543
1544         if (copy_to_user(value, &result, sizeof(int)))
1545                 return -EFAULT;
1546         return 0;
1547 }
1548
1549 static int edge_tiocmset(struct tty_struct *tty,
1550                                         unsigned int set, unsigned int clear)
1551 {
1552         struct usb_serial_port *port = tty->driver_data;
1553         struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1554         unsigned int mcr;
1555
1556         mcr = edge_port->shadowMCR;
1557         if (set & TIOCM_RTS)
1558                 mcr |= MCR_RTS;
1559         if (set & TIOCM_DTR)
1560                 mcr |= MCR_DTR;
1561         if (set & TIOCM_LOOP)
1562                 mcr |= MCR_LOOPBACK;
1563
1564         if (clear & TIOCM_RTS)
1565                 mcr &= ~MCR_RTS;
1566         if (clear & TIOCM_DTR)
1567                 mcr &= ~MCR_DTR;
1568         if (clear & TIOCM_LOOP)
1569                 mcr &= ~MCR_LOOPBACK;
1570
1571         edge_port->shadowMCR = mcr;
1572
1573         send_cmd_write_uart_register(edge_port, MCR, edge_port->shadowMCR);
1574
1575         return 0;
1576 }
1577
1578 static int edge_tiocmget(struct tty_struct *tty)
1579 {
1580         struct usb_serial_port *port = tty->driver_data;
1581         struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1582         unsigned int result = 0;
1583         unsigned int msr;
1584         unsigned int mcr;
1585
1586         msr = edge_port->shadowMSR;
1587         mcr = edge_port->shadowMCR;
1588         result = ((mcr & MCR_DTR)       ? TIOCM_DTR: 0)   /* 0x002 */
1589                   | ((mcr & MCR_RTS)    ? TIOCM_RTS: 0)   /* 0x004 */
1590                   | ((msr & EDGEPORT_MSR_CTS)   ? TIOCM_CTS: 0)   /* 0x020 */
1591                   | ((msr & EDGEPORT_MSR_CD)    ? TIOCM_CAR: 0)   /* 0x040 */
1592                   | ((msr & EDGEPORT_MSR_RI)    ? TIOCM_RI:  0)   /* 0x080 */
1593                   | ((msr & EDGEPORT_MSR_DSR)   ? TIOCM_DSR: 0);  /* 0x100 */
1594
1595         return result;
1596 }
1597
1598 static int edge_get_icount(struct tty_struct *tty,
1599                                 struct serial_icounter_struct *icount)
1600 {
1601         struct usb_serial_port *port = tty->driver_data;
1602         struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1603         struct async_icount cnow;
1604         cnow = edge_port->icount;
1605
1606         icount->cts = cnow.cts;
1607         icount->dsr = cnow.dsr;
1608         icount->rng = cnow.rng;
1609         icount->dcd = cnow.dcd;
1610         icount->rx = cnow.rx;
1611         icount->tx = cnow.tx;
1612         icount->frame = cnow.frame;
1613         icount->overrun = cnow.overrun;
1614         icount->parity = cnow.parity;
1615         icount->brk = cnow.brk;
1616         icount->buf_overrun = cnow.buf_overrun;
1617
1618         dev_dbg(&port->dev, "%s (%d) TIOCGICOUNT RX=%d, TX=%d\n", __func__,
1619                 port->number, icount->rx, icount->tx);
1620         return 0;
1621 }
1622
1623 static int get_serial_info(struct edgeport_port *edge_port,
1624                                 struct serial_struct __user *retinfo)
1625 {
1626         struct serial_struct tmp;
1627
1628         if (!retinfo)
1629                 return -EFAULT;
1630
1631         memset(&tmp, 0, sizeof(tmp));
1632
1633         tmp.type                = PORT_16550A;
1634         tmp.line                = edge_port->port->serial->minor;
1635         tmp.port                = edge_port->port->number;
1636         tmp.irq                 = 0;
1637         tmp.flags               = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
1638         tmp.xmit_fifo_size      = edge_port->maxTxCredits;
1639         tmp.baud_base           = 9600;
1640         tmp.close_delay         = 5*HZ;
1641         tmp.closing_wait        = 30*HZ;
1642
1643         if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1644                 return -EFAULT;
1645         return 0;
1646 }
1647
1648
1649 /*****************************************************************************
1650  * SerialIoctl
1651  *      this function handles any ioctl calls to the driver
1652  *****************************************************************************/
1653 static int edge_ioctl(struct tty_struct *tty,
1654                                         unsigned int cmd, unsigned long arg)
1655 {
1656         struct usb_serial_port *port = tty->driver_data;
1657         DEFINE_WAIT(wait);
1658         struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1659         struct async_icount cnow;
1660         struct async_icount cprev;
1661
1662         dev_dbg(&port->dev, "%s - port %d, cmd = 0x%x\n", __func__, port->number, cmd);
1663
1664         switch (cmd) {
1665         case TIOCSERGETLSR:
1666                 dev_dbg(&port->dev, "%s (%d) TIOCSERGETLSR\n", __func__,  port->number);
1667                 return get_lsr_info(edge_port, (unsigned int __user *) arg);
1668
1669         case TIOCGSERIAL:
1670                 dev_dbg(&port->dev, "%s (%d) TIOCGSERIAL\n", __func__,  port->number);
1671                 return get_serial_info(edge_port, (struct serial_struct __user *) arg);
1672
1673         case TIOCMIWAIT:
1674                 dev_dbg(&port->dev, "%s (%d) TIOCMIWAIT\n", __func__,  port->number);
1675                 cprev = edge_port->icount;
1676                 while (1) {
1677                         prepare_to_wait(&edge_port->delta_msr_wait,
1678                                                 &wait, TASK_INTERRUPTIBLE);
1679                         schedule();
1680                         finish_wait(&edge_port->delta_msr_wait, &wait);
1681                         /* see if a signal did it */
1682                         if (signal_pending(current))
1683                                 return -ERESTARTSYS;
1684                         cnow = edge_port->icount;
1685                         if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
1686                             cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
1687                                 return -EIO; /* no change => error */
1688                         if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1689                             ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1690                             ((arg & TIOCM_CD)  && (cnow.dcd != cprev.dcd)) ||
1691                             ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
1692                                 return 0;
1693                         }
1694                         cprev = cnow;
1695                 }
1696                 /* NOTREACHED */
1697                 break;
1698
1699         }
1700         return -ENOIOCTLCMD;
1701 }
1702
1703
1704 /*****************************************************************************
1705  * SerialBreak
1706  *      this function sends a break to the port
1707  *****************************************************************************/
1708 static void edge_break(struct tty_struct *tty, int break_state)
1709 {
1710         struct usb_serial_port *port = tty->driver_data;
1711         struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1712         struct edgeport_serial *edge_serial = usb_get_serial_data(port->serial);
1713         int status;
1714
1715         if ((!edge_serial->is_epic) ||
1716             ((edge_serial->is_epic) &&
1717              (edge_serial->epic_descriptor.Supports.IOSPChase))) {
1718                 /* flush and chase */
1719                 edge_port->chaseResponsePending = true;
1720
1721                 dev_dbg(&port->dev, "%s - Sending IOSP_CMD_CHASE_PORT\n", __func__);
1722                 status = send_iosp_ext_cmd(edge_port, IOSP_CMD_CHASE_PORT, 0);
1723                 if (status == 0) {
1724                         /* block until chase finished */
1725                         block_until_chase_response(edge_port);
1726                 } else {
1727                         edge_port->chaseResponsePending = false;
1728                 }
1729         }
1730
1731         if ((!edge_serial->is_epic) ||
1732             ((edge_serial->is_epic) &&
1733              (edge_serial->epic_descriptor.Supports.IOSPSetClrBreak))) {
1734                 if (break_state == -1) {
1735                         dev_dbg(&port->dev, "%s - Sending IOSP_CMD_SET_BREAK\n", __func__);
1736                         status = send_iosp_ext_cmd(edge_port,
1737                                                 IOSP_CMD_SET_BREAK, 0);
1738                 } else {
1739                         dev_dbg(&port->dev, "%s - Sending IOSP_CMD_CLEAR_BREAK\n", __func__);
1740                         status = send_iosp_ext_cmd(edge_port,
1741                                                 IOSP_CMD_CLEAR_BREAK, 0);
1742                 }
1743                 if (status)
1744                         dev_dbg(&port->dev, "%s - error sending break set/clear command.\n",
1745                                 __func__);
1746         }
1747 }
1748
1749
1750 /*****************************************************************************
1751  * process_rcvd_data
1752  *      this function handles the data received on the bulk in pipe.
1753  *****************************************************************************/
1754 static void process_rcvd_data(struct edgeport_serial *edge_serial,
1755                                 unsigned char *buffer, __u16 bufferLength)
1756 {
1757         struct device *dev = &edge_serial->serial->dev->dev;
1758         struct usb_serial_port *port;
1759         struct edgeport_port *edge_port;
1760         struct tty_struct *tty;
1761         __u16 lastBufferLength;
1762         __u16 rxLen;
1763
1764         lastBufferLength = bufferLength + 1;
1765
1766         while (bufferLength > 0) {
1767                 /* failsafe incase we get a message that we don't understand */
1768                 if (lastBufferLength == bufferLength) {
1769                         dev_dbg(dev, "%s - stuck in loop, exiting it.\n", __func__);
1770                         break;
1771                 }
1772                 lastBufferLength = bufferLength;
1773
1774                 switch (edge_serial->rxState) {
1775                 case EXPECT_HDR1:
1776                         edge_serial->rxHeader1 = *buffer;
1777                         ++buffer;
1778                         --bufferLength;
1779
1780                         if (bufferLength == 0) {
1781                                 edge_serial->rxState = EXPECT_HDR2;
1782                                 break;
1783                         }
1784                         /* otherwise, drop on through */
1785                 case EXPECT_HDR2:
1786                         edge_serial->rxHeader2 = *buffer;
1787                         ++buffer;
1788                         --bufferLength;
1789
1790                         dev_dbg(dev, "%s - Hdr1=%02X Hdr2=%02X\n", __func__,
1791                                 edge_serial->rxHeader1, edge_serial->rxHeader2);
1792                         /* Process depending on whether this header is
1793                          * data or status */
1794
1795                         if (IS_CMD_STAT_HDR(edge_serial->rxHeader1)) {
1796                                 /* Decode this status header and go to
1797                                  * EXPECT_HDR1 (if we can process the status
1798                                  * with only 2 bytes), or go to EXPECT_HDR3 to
1799                                  * get the third byte. */
1800                                 edge_serial->rxPort =
1801                                     IOSP_GET_HDR_PORT(edge_serial->rxHeader1);
1802                                 edge_serial->rxStatusCode =
1803                                     IOSP_GET_STATUS_CODE(
1804                                                 edge_serial->rxHeader1);
1805
1806                                 if (!IOSP_STATUS_IS_2BYTE(
1807                                                 edge_serial->rxStatusCode)) {
1808                                         /* This status needs additional bytes.
1809                                          * Save what we have and then wait for
1810                                          * more data.
1811                                          */
1812                                         edge_serial->rxStatusParam
1813                                                 = edge_serial->rxHeader2;
1814                                         edge_serial->rxState = EXPECT_HDR3;
1815                                         break;
1816                                 }
1817                                 /* We have all the header bytes, process the
1818                                    status now */
1819                                 process_rcvd_status(edge_serial,
1820                                                 edge_serial->rxHeader2, 0);
1821                                 edge_serial->rxState = EXPECT_HDR1;
1822                                 break;
1823                         } else {
1824                                 edge_serial->rxPort =
1825                                     IOSP_GET_HDR_PORT(edge_serial->rxHeader1);
1826                                 edge_serial->rxBytesRemaining =
1827                                     IOSP_GET_HDR_DATA_LEN(
1828                                                 edge_serial->rxHeader1,
1829                                                 edge_serial->rxHeader2);
1830                                 dev_dbg(dev, "%s - Data for Port %u Len %u\n",
1831                                         __func__,
1832                                         edge_serial->rxPort,
1833                                         edge_serial->rxBytesRemaining);
1834
1835                                 /* ASSERT(DevExt->RxPort < DevExt->NumPorts);
1836                                  * ASSERT(DevExt->RxBytesRemaining <
1837                                  *              IOSP_MAX_DATA_LENGTH);
1838                                  */
1839
1840                                 if (bufferLength == 0) {
1841                                         edge_serial->rxState = EXPECT_DATA;
1842                                         break;
1843                                 }
1844                                 /* Else, drop through */
1845                         }
1846                 case EXPECT_DATA: /* Expect data */
1847                         if (bufferLength < edge_serial->rxBytesRemaining) {
1848                                 rxLen = bufferLength;
1849                                 /* Expect data to start next buffer */
1850                                 edge_serial->rxState = EXPECT_DATA;
1851                         } else {
1852                                 /* BufLen >= RxBytesRemaining */
1853                                 rxLen = edge_serial->rxBytesRemaining;
1854                                 /* Start another header next time */
1855                                 edge_serial->rxState = EXPECT_HDR1;
1856                         }
1857
1858                         bufferLength -= rxLen;
1859                         edge_serial->rxBytesRemaining -= rxLen;
1860
1861                         /* spit this data back into the tty driver if this
1862                            port is open */
1863                         if (rxLen) {
1864                                 port = edge_serial->serial->port[
1865                                                         edge_serial->rxPort];
1866                                 edge_port = usb_get_serial_port_data(port);
1867                                 if (edge_port->open) {
1868                                         tty = tty_port_tty_get(
1869                                                 &edge_port->port->port);
1870                                         if (tty) {
1871                                                 dev_dbg(dev, "%s - Sending %d bytes to TTY for port %d\n",
1872                                                         __func__, rxLen, edge_serial->rxPort);
1873                                                 edge_tty_recv(&edge_serial->serial->dev->dev, tty, buffer, rxLen);
1874                                                 tty_kref_put(tty);
1875                                         }
1876                                         edge_port->icount.rx += rxLen;
1877                                 }
1878                                 buffer += rxLen;
1879                         }
1880                         break;
1881
1882                 case EXPECT_HDR3:       /* Expect 3rd byte of status header */
1883                         edge_serial->rxHeader3 = *buffer;
1884                         ++buffer;
1885                         --bufferLength;
1886
1887                         /* We have all the header bytes, process the
1888                            status now */
1889                         process_rcvd_status(edge_serial,
1890                                 edge_serial->rxStatusParam,
1891                                 edge_serial->rxHeader3);
1892                         edge_serial->rxState = EXPECT_HDR1;
1893                         break;
1894                 }
1895         }
1896 }
1897
1898
1899 /*****************************************************************************
1900  * process_rcvd_status
1901  *      this function handles the any status messages received on the
1902  *      bulk in pipe.
1903  *****************************************************************************/
1904 static void process_rcvd_status(struct edgeport_serial *edge_serial,
1905                                                 __u8 byte2, __u8 byte3)
1906 {
1907         struct usb_serial_port *port;
1908         struct edgeport_port *edge_port;
1909         struct tty_struct *tty;
1910         struct device *dev;
1911         __u8 code = edge_serial->rxStatusCode;
1912
1913         /* switch the port pointer to the one being currently talked about */
1914         port = edge_serial->serial->port[edge_serial->rxPort];
1915         edge_port = usb_get_serial_port_data(port);
1916         if (edge_port == NULL) {
1917                 dev_err(&edge_serial->serial->dev->dev,
1918                         "%s - edge_port == NULL for port %d\n",
1919                                         __func__, edge_serial->rxPort);
1920                 return;
1921         }
1922         dev = &port->dev;
1923
1924         if (code == IOSP_EXT_STATUS) {
1925                 switch (byte2) {
1926                 case IOSP_EXT_STATUS_CHASE_RSP:
1927                         /* we want to do EXT status regardless of port
1928                          * open/closed */
1929                         dev_dbg(dev, "%s - Port %u EXT CHASE_RSP Data = %02x\n",
1930                                 __func__, edge_serial->rxPort, byte3);
1931                         /* Currently, the only EXT_STATUS is Chase, so process
1932                          * here instead of one more call to one more subroutine
1933                          * If/when more EXT_STATUS, there'll be more work to do
1934                          * Also, we currently clear flag and close the port
1935                          * regardless of content of above's Byte3.
1936                          * We could choose to do something else when Byte3 says
1937                          * Timeout on Chase from Edgeport, like wait longer in
1938                          * block_until_chase_response, but for now we don't.
1939                          */
1940                         edge_port->chaseResponsePending = false;
1941                         wake_up(&edge_port->wait_chase);
1942                         return;
1943
1944                 case IOSP_EXT_STATUS_RX_CHECK_RSP:
1945                         dev_dbg(dev, "%s ========== Port %u CHECK_RSP Sequence = %02x =============\n",
1946                                 __func__, edge_serial->rxPort, byte3);
1947                         /* Port->RxCheckRsp = true; */
1948                         return;
1949                 }
1950         }
1951
1952         if (code == IOSP_STATUS_OPEN_RSP) {
1953                 edge_port->txCredits = GET_TX_BUFFER_SIZE(byte3);
1954                 edge_port->maxTxCredits = edge_port->txCredits;
1955                 dev_dbg(dev, "%s - Port %u Open Response Initial MSR = %02x TxBufferSize = %d\n",
1956                         __func__, edge_serial->rxPort, byte2, edge_port->txCredits);
1957                 handle_new_msr(edge_port, byte2);
1958
1959                 /* send the current line settings to the port so we are
1960                    in sync with any further termios calls */
1961                 tty = tty_port_tty_get(&edge_port->port->port);
1962                 if (tty) {
1963                         change_port_settings(tty,
1964                                 edge_port, tty->termios);
1965                         tty_kref_put(tty);
1966                 }
1967
1968                 /* we have completed the open */
1969                 edge_port->openPending = false;
1970                 edge_port->open = true;
1971                 wake_up(&edge_port->wait_open);
1972                 return;
1973         }
1974
1975         /* If port is closed, silently discard all rcvd status. We can
1976          * have cases where buffered status is received AFTER the close
1977          * port command is sent to the Edgeport.
1978          */
1979         if (!edge_port->open || edge_port->closePending)
1980                 return;
1981
1982         switch (code) {
1983         /* Not currently sent by Edgeport */
1984         case IOSP_STATUS_LSR:
1985                 dev_dbg(dev, "%s - Port %u LSR Status = %02x\n",
1986                         __func__, edge_serial->rxPort, byte2);
1987                 handle_new_lsr(edge_port, false, byte2, 0);
1988                 break;
1989
1990         case IOSP_STATUS_LSR_DATA:
1991                 dev_dbg(dev, "%s - Port %u LSR Status = %02x, Data = %02x\n",
1992                         __func__, edge_serial->rxPort, byte2, byte3);
1993                 /* byte2 is LSR Register */
1994                 /* byte3 is broken data byte */
1995                 handle_new_lsr(edge_port, true, byte2, byte3);
1996                 break;
1997         /*
1998          *      case IOSP_EXT_4_STATUS:
1999          *              dev_dbg(dev, "%s - Port %u LSR Status = %02x Data = %02x\n",
2000          *                      __func__, edge_serial->rxPort, byte2, byte3);
2001          *              break;
2002          */
2003         case IOSP_STATUS_MSR:
2004                 dev_dbg(dev, "%s - Port %u MSR Status = %02x\n",
2005                         __func__, edge_serial->rxPort, byte2);
2006                 /*
2007                  * Process this new modem status and generate appropriate
2008                  * events, etc, based on the new status. This routine
2009                  * also saves the MSR in Port->ShadowMsr.
2010                  */
2011                 handle_new_msr(edge_port, byte2);
2012                 break;
2013
2014         default:
2015                 dev_dbg(dev, "%s - Unrecognized IOSP status code %u\n", __func__, code);
2016                 break;
2017         }
2018 }
2019
2020
2021 /*****************************************************************************
2022  * edge_tty_recv
2023  *      this function passes data on to the tty flip buffer
2024  *****************************************************************************/
2025 static void edge_tty_recv(struct device *dev, struct tty_struct *tty,
2026                                         unsigned char *data, int length)
2027 {
2028         int cnt;
2029
2030         cnt = tty_insert_flip_string(tty, data, length);
2031         if (cnt < length) {
2032                 dev_err(dev, "%s - dropping data, %d bytes lost\n",
2033                                 __func__, length - cnt);
2034         }
2035         data += cnt;
2036         length -= cnt;
2037
2038         tty_flip_buffer_push(tty);
2039 }
2040
2041
2042 /*****************************************************************************
2043  * handle_new_msr
2044  *      this function handles any change to the msr register for a port.
2045  *****************************************************************************/
2046 static void handle_new_msr(struct edgeport_port *edge_port, __u8 newMsr)
2047 {
2048         struct  async_icount *icount;
2049
2050         if (newMsr & (EDGEPORT_MSR_DELTA_CTS | EDGEPORT_MSR_DELTA_DSR |
2051                         EDGEPORT_MSR_DELTA_RI | EDGEPORT_MSR_DELTA_CD)) {
2052                 icount = &edge_port->icount;
2053
2054                 /* update input line counters */
2055                 if (newMsr & EDGEPORT_MSR_DELTA_CTS)
2056                         icount->cts++;
2057                 if (newMsr & EDGEPORT_MSR_DELTA_DSR)
2058                         icount->dsr++;
2059                 if (newMsr & EDGEPORT_MSR_DELTA_CD)
2060                         icount->dcd++;
2061                 if (newMsr & EDGEPORT_MSR_DELTA_RI)
2062                         icount->rng++;
2063                 wake_up_interruptible(&edge_port->delta_msr_wait);
2064         }
2065
2066         /* Save the new modem status */
2067         edge_port->shadowMSR = newMsr & 0xf0;
2068 }
2069
2070
2071 /*****************************************************************************
2072  * handle_new_lsr
2073  *      this function handles any change to the lsr register for a port.
2074  *****************************************************************************/
2075 static void handle_new_lsr(struct edgeport_port *edge_port, __u8 lsrData,
2076                                                         __u8 lsr, __u8 data)
2077 {
2078         __u8 newLsr = (__u8) (lsr & (__u8)
2079                 (LSR_OVER_ERR | LSR_PAR_ERR | LSR_FRM_ERR | LSR_BREAK));
2080         struct async_icount *icount;
2081
2082         edge_port->shadowLSR = lsr;
2083
2084         if (newLsr & LSR_BREAK) {
2085                 /*
2086                  * Parity and Framing errors only count if they
2087                  * occur exclusive of a break being
2088                  * received.
2089                  */
2090                 newLsr &= (__u8)(LSR_OVER_ERR | LSR_BREAK);
2091         }
2092
2093         /* Place LSR data byte into Rx buffer */
2094         if (lsrData) {
2095                 struct tty_struct *tty =
2096                                 tty_port_tty_get(&edge_port->port->port);
2097                 if (tty) {
2098                         edge_tty_recv(&edge_port->port->dev, tty, &data, 1);
2099                         tty_kref_put(tty);
2100                 }
2101         }
2102         /* update input line counters */
2103         icount = &edge_port->icount;
2104         if (newLsr & LSR_BREAK)
2105                 icount->brk++;
2106         if (newLsr & LSR_OVER_ERR)
2107                 icount->overrun++;
2108         if (newLsr & LSR_PAR_ERR)
2109                 icount->parity++;
2110         if (newLsr & LSR_FRM_ERR)
2111                 icount->frame++;
2112 }
2113
2114
2115 /****************************************************************************
2116  * sram_write
2117  *      writes a number of bytes to the Edgeport device's sram starting at the
2118  *      given address.
2119  *      If successful returns the number of bytes written, otherwise it returns
2120  *      a negative error number of the problem.
2121  ****************************************************************************/
2122 static int sram_write(struct usb_serial *serial, __u16 extAddr, __u16 addr,
2123                                         __u16 length, const __u8 *data)
2124 {
2125         int result;
2126         __u16 current_length;
2127         unsigned char *transfer_buffer;
2128
2129         dev_dbg(&serial->dev->dev, "%s - %x, %x, %d\n", __func__, extAddr, addr, length);
2130
2131         transfer_buffer =  kmalloc(64, GFP_KERNEL);
2132         if (!transfer_buffer) {
2133                 dev_err(&serial->dev->dev, "%s - kmalloc(%d) failed.\n",
2134                                                         __func__, 64);
2135                 return -ENOMEM;
2136         }
2137
2138         /* need to split these writes up into 64 byte chunks */
2139         result = 0;
2140         while (length > 0) {
2141                 if (length > 64)
2142                         current_length = 64;
2143                 else
2144                         current_length = length;
2145
2146 /*              dev_dbg(&serial->dev->dev, "%s - writing %x, %x, %d\n", __func__, extAddr, addr, current_length); */
2147                 memcpy(transfer_buffer, data, current_length);
2148                 result = usb_control_msg(serial->dev,
2149                                         usb_sndctrlpipe(serial->dev, 0),
2150                                         USB_REQUEST_ION_WRITE_RAM,
2151                                         0x40, addr, extAddr, transfer_buffer,
2152                                         current_length, 300);
2153                 if (result < 0)
2154                         break;
2155                 length -= current_length;
2156                 addr += current_length;
2157                 data += current_length;
2158         }
2159
2160         kfree(transfer_buffer);
2161         return result;
2162 }
2163
2164
2165 /****************************************************************************
2166  * rom_write
2167  *      writes a number of bytes to the Edgeport device's ROM starting at the
2168  *      given address.
2169  *      If successful returns the number of bytes written, otherwise it returns
2170  *      a negative error number of the problem.
2171  ****************************************************************************/
2172 static int rom_write(struct usb_serial *serial, __u16 extAddr, __u16 addr,
2173                                         __u16 length, const __u8 *data)
2174 {
2175         int result;
2176         __u16 current_length;
2177         unsigned char *transfer_buffer;
2178
2179         transfer_buffer =  kmalloc(64, GFP_KERNEL);
2180         if (!transfer_buffer) {
2181                 dev_err(&serial->dev->dev, "%s - kmalloc(%d) failed.\n",
2182                                                                 __func__, 64);
2183                 return -ENOMEM;
2184         }
2185
2186         /* need to split these writes up into 64 byte chunks */
2187         result = 0;
2188         while (length > 0) {
2189                 if (length > 64)
2190                         current_length = 64;
2191                 else
2192                         current_length = length;
2193                 memcpy(transfer_buffer, data, current_length);
2194                 result = usb_control_msg(serial->dev,
2195                                         usb_sndctrlpipe(serial->dev, 0),
2196                                         USB_REQUEST_ION_WRITE_ROM, 0x40,
2197                                         addr, extAddr,
2198                                         transfer_buffer, current_length, 300);
2199                 if (result < 0)
2200                         break;
2201                 length -= current_length;
2202                 addr += current_length;
2203                 data += current_length;
2204         }
2205
2206         kfree(transfer_buffer);
2207         return result;
2208 }
2209
2210
2211 /****************************************************************************
2212  * rom_read
2213  *      reads a number of bytes from the Edgeport device starting at the given
2214  *      address.
2215  *      If successful returns the number of bytes read, otherwise it returns
2216  *      a negative error number of the problem.
2217  ****************************************************************************/
2218 static int rom_read(struct usb_serial *serial, __u16 extAddr,
2219                                         __u16 addr, __u16 length, __u8 *data)
2220 {
2221         int result;
2222         __u16 current_length;
2223         unsigned char *transfer_buffer;
2224
2225         transfer_buffer =  kmalloc(64, GFP_KERNEL);
2226         if (!transfer_buffer) {
2227                 dev_err(&serial->dev->dev,
2228                         "%s - kmalloc(%d) failed.\n", __func__, 64);
2229                 return -ENOMEM;
2230         }
2231
2232         /* need to split these reads up into 64 byte chunks */
2233         result = 0;
2234         while (length > 0) {
2235                 if (length > 64)
2236                         current_length = 64;
2237                 else
2238                         current_length = length;
2239                 result = usb_control_msg(serial->dev,
2240                                         usb_rcvctrlpipe(serial->dev, 0),
2241                                         USB_REQUEST_ION_READ_ROM,
2242                                         0xC0, addr, extAddr, transfer_buffer,
2243                                         current_length, 300);
2244                 if (result < 0)
2245                         break;
2246                 memcpy(data, transfer_buffer, current_length);
2247                 length -= current_length;
2248                 addr += current_length;
2249                 data += current_length;
2250         }
2251
2252         kfree(transfer_buffer);
2253         return result;
2254 }
2255
2256
2257 /****************************************************************************
2258  * send_iosp_ext_cmd
2259  *      Is used to send a IOSP message to the Edgeport device
2260  ****************************************************************************/
2261 static int send_iosp_ext_cmd(struct edgeport_port *edge_port,
2262                                                 __u8 command, __u8 param)
2263 {
2264         unsigned char   *buffer;
2265         unsigned char   *currentCommand;
2266         int             length = 0;
2267         int             status = 0;
2268
2269         buffer = kmalloc(10, GFP_ATOMIC);
2270         if (!buffer) {
2271                 dev_err(&edge_port->port->dev,
2272                                 "%s - kmalloc(%d) failed.\n", __func__, 10);
2273                 return -ENOMEM;
2274         }
2275
2276         currentCommand = buffer;
2277
2278         MAKE_CMD_EXT_CMD(&currentCommand, &length,
2279                 edge_port->port->number - edge_port->port->serial->minor,
2280                 command, param);
2281
2282         status = write_cmd_usb(edge_port, buffer, length);
2283         if (status) {
2284                 /* something bad happened, let's free up the memory */
2285                 kfree(buffer);
2286         }
2287
2288         return status;
2289 }
2290
2291
2292 /*****************************************************************************
2293  * write_cmd_usb
2294  *      this function writes the given buffer out to the bulk write endpoint.
2295  *****************************************************************************/
2296 static int write_cmd_usb(struct edgeport_port *edge_port,
2297                                         unsigned char *buffer, int length)
2298 {
2299         struct edgeport_serial *edge_serial =
2300                                 usb_get_serial_data(edge_port->port->serial);
2301         struct device *dev = &edge_port->port->dev;
2302         int status = 0;
2303         struct urb *urb;
2304
2305         usb_serial_debug_data(dev, __func__, length, buffer);
2306
2307         /* Allocate our next urb */
2308         urb = usb_alloc_urb(0, GFP_ATOMIC);
2309         if (!urb)
2310                 return -ENOMEM;
2311
2312         atomic_inc(&CmdUrbs);
2313         dev_dbg(dev, "%s - ALLOCATE URB %p (outstanding %d)\n",
2314                 __func__, urb, atomic_read(&CmdUrbs));
2315
2316         usb_fill_bulk_urb(urb, edge_serial->serial->dev,
2317                         usb_sndbulkpipe(edge_serial->serial->dev,
2318                                         edge_serial->bulk_out_endpoint),
2319                         buffer, length, edge_bulk_out_cmd_callback, edge_port);
2320
2321         edge_port->commandPending = true;
2322         status = usb_submit_urb(urb, GFP_ATOMIC);
2323
2324         if (status) {
2325                 /* something went wrong */
2326                 dev_err(dev, "%s - usb_submit_urb(write command) failed, status = %d\n",
2327                         __func__, status);
2328                 usb_kill_urb(urb);
2329                 usb_free_urb(urb);
2330                 atomic_dec(&CmdUrbs);
2331                 return status;
2332         }
2333
2334 #if 0
2335         wait_event(&edge_port->wait_command, !edge_port->commandPending);
2336
2337         if (edge_port->commandPending) {
2338                 /* command timed out */
2339                 dev_dbg(dev, "%s - command timed out\n", __func__);
2340                 status = -EINVAL;
2341         }
2342 #endif
2343         return status;
2344 }
2345
2346
2347 /*****************************************************************************
2348  * send_cmd_write_baud_rate
2349  *      this function sends the proper command to change the baud rate of the
2350  *      specified port.
2351  *****************************************************************************/
2352 static int send_cmd_write_baud_rate(struct edgeport_port *edge_port,
2353                                                                 int baudRate)
2354 {
2355         struct edgeport_serial *edge_serial =
2356                                 usb_get_serial_data(edge_port->port->serial);
2357         struct device *dev = &edge_port->port->dev;
2358         unsigned char *cmdBuffer;
2359         unsigned char *currCmd;
2360         int cmdLen = 0;
2361         int divisor;
2362         int status;
2363         unsigned char number =
2364                 edge_port->port->number - edge_port->port->serial->minor;
2365
2366         if (edge_serial->is_epic &&
2367             !edge_serial->epic_descriptor.Supports.IOSPSetBaudRate) {
2368                 dev_dbg(dev, "SendCmdWriteBaudRate - NOT Setting baud rate for port = %d, baud = %d\n",
2369                         edge_port->port->number, baudRate);
2370                 return 0;
2371         }
2372
2373         dev_dbg(dev, "%s - port = %d, baud = %d\n", __func__,
2374                 edge_port->port->number, baudRate);
2375
2376         status = calc_baud_rate_divisor(dev, baudRate, &divisor);
2377         if (status) {
2378                 dev_err(dev, "%s - bad baud rate\n", __func__);
2379                 return status;
2380         }
2381
2382         /* Alloc memory for the string of commands. */
2383         cmdBuffer =  kmalloc(0x100, GFP_ATOMIC);
2384         if (!cmdBuffer) {
2385                 dev_err(dev, "%s - kmalloc(%d) failed.\n", __func__, 0x100);
2386                 return -ENOMEM;
2387         }
2388         currCmd = cmdBuffer;
2389
2390         /* Enable access to divisor latch */
2391         MAKE_CMD_WRITE_REG(&currCmd, &cmdLen, number, LCR, LCR_DL_ENABLE);
2392
2393         /* Write the divisor itself */
2394         MAKE_CMD_WRITE_REG(&currCmd, &cmdLen, number, DLL, LOW8(divisor));
2395         MAKE_CMD_WRITE_REG(&currCmd, &cmdLen, number, DLM, HIGH8(divisor));
2396
2397         /* Restore original value to disable access to divisor latch */
2398         MAKE_CMD_WRITE_REG(&currCmd, &cmdLen, number, LCR,
2399                                                 edge_port->shadowLCR);
2400
2401         status = write_cmd_usb(edge_port, cmdBuffer, cmdLen);
2402         if (status) {
2403                 /* something bad happened, let's free up the memory */
2404                 kfree(cmdBuffer);
2405         }
2406
2407         return status;
2408 }
2409
2410
2411 /*****************************************************************************
2412  * calc_baud_rate_divisor
2413  *      this function calculates the proper baud rate divisor for the specified
2414  *      baud rate.
2415  *****************************************************************************/
2416 static int calc_baud_rate_divisor(struct device *dev, int baudrate, int *divisor)
2417 {
2418         int i;
2419         __u16 custom;
2420
2421         for (i = 0; i < ARRAY_SIZE(divisor_table); i++) {
2422                 if (divisor_table[i].BaudRate == baudrate) {
2423                         *divisor = divisor_table[i].Divisor;
2424                         return 0;
2425                 }
2426         }
2427
2428         /* We have tried all of the standard baud rates
2429          * lets try to calculate the divisor for this baud rate
2430          * Make sure the baud rate is reasonable */
2431         if (baudrate > 50 && baudrate < 230400) {
2432                 /* get divisor */
2433                 custom = (__u16)((230400L + baudrate/2) / baudrate);
2434
2435                 *divisor = custom;
2436
2437                 dev_dbg(dev, "%s - Baud %d = %d\n", __func__, baudrate, custom);
2438                 return 0;
2439         }
2440
2441         return -1;
2442 }
2443
2444
2445 /*****************************************************************************
2446  * send_cmd_write_uart_register
2447  *  this function builds up a uart register message and sends to the device.
2448  *****************************************************************************/
2449 static int send_cmd_write_uart_register(struct edgeport_port *edge_port,
2450                                                 __u8 regNum, __u8 regValue)
2451 {
2452         struct edgeport_serial *edge_serial =
2453                                 usb_get_serial_data(edge_port->port->serial);
2454         struct device *dev = &edge_port->port->dev;
2455         unsigned char *cmdBuffer;
2456         unsigned char *currCmd;
2457         unsigned long cmdLen = 0;
2458         int status;
2459
2460         dev_dbg(dev, "%s - write to %s register 0x%02x\n",
2461                 (regNum == MCR) ? "MCR" : "LCR", __func__, regValue);
2462
2463         if (edge_serial->is_epic &&
2464             !edge_serial->epic_descriptor.Supports.IOSPWriteMCR &&
2465             regNum == MCR) {
2466                 dev_dbg(dev, "SendCmdWriteUartReg - Not writing to MCR Register\n");
2467                 return 0;
2468         }
2469
2470         if (edge_serial->is_epic &&
2471             !edge_serial->epic_descriptor.Supports.IOSPWriteLCR &&
2472             regNum == LCR) {
2473                 dev_dbg(dev, "SendCmdWriteUartReg - Not writing to LCR Register\n");
2474                 return 0;
2475         }
2476
2477         /* Alloc memory for the string of commands. */
2478         cmdBuffer = kmalloc(0x10, GFP_ATOMIC);
2479         if (cmdBuffer == NULL)
2480                 return -ENOMEM;
2481
2482         currCmd = cmdBuffer;
2483
2484         /* Build a cmd in the buffer to write the given register */
2485         MAKE_CMD_WRITE_REG(&currCmd, &cmdLen,
2486                 edge_port->port->number - edge_port->port->serial->minor,
2487                 regNum, regValue);
2488
2489         status = write_cmd_usb(edge_port, cmdBuffer, cmdLen);
2490         if (status) {
2491                 /* something bad happened, let's free up the memory */
2492                 kfree(cmdBuffer);
2493         }
2494
2495         return status;
2496 }
2497
2498
2499 /*****************************************************************************
2500  * change_port_settings
2501  *      This routine is called to set the UART on the device to match the
2502  *      specified new settings.
2503  *****************************************************************************/
2504
2505 static void change_port_settings(struct tty_struct *tty,
2506         struct edgeport_port *edge_port, struct ktermios *old_termios)
2507 {
2508         struct device *dev = &edge_port->port->dev;
2509         struct edgeport_serial *edge_serial =
2510                         usb_get_serial_data(edge_port->port->serial);
2511         int baud;
2512         unsigned cflag;
2513         __u8 mask = 0xff;
2514         __u8 lData;
2515         __u8 lParity;
2516         __u8 lStop;
2517         __u8 rxFlow;
2518         __u8 txFlow;
2519         int status;
2520
2521         dev_dbg(dev, "%s - port %d\n", __func__, edge_port->port->number);
2522
2523         if (!edge_port->open &&
2524             !edge_port->openPending) {
2525                 dev_dbg(dev, "%s - port not opened\n", __func__);
2526                 return;
2527         }
2528
2529         cflag = tty->termios->c_cflag;
2530
2531         switch (cflag & CSIZE) {
2532         case CS5:
2533                 lData = LCR_BITS_5; mask = 0x1f;
2534                 dev_dbg(dev, "%s - data bits = 5\n", __func__);
2535                 break;
2536         case CS6:
2537                 lData = LCR_BITS_6; mask = 0x3f;
2538                 dev_dbg(dev, "%s - data bits = 6\n", __func__);
2539                 break;
2540         case CS7:
2541                 lData = LCR_BITS_7; mask = 0x7f;
2542                 dev_dbg(dev, "%s - data bits = 7\n", __func__);
2543                 break;
2544         default:
2545         case CS8:
2546                 lData = LCR_BITS_8;
2547                 dev_dbg(dev, "%s - data bits = 8\n", __func__);
2548                 break;
2549         }
2550
2551         lParity = LCR_PAR_NONE;
2552         if (cflag & PARENB) {
2553                 if (cflag & CMSPAR) {
2554                         if (cflag & PARODD) {
2555                                 lParity = LCR_PAR_MARK;
2556                                 dev_dbg(dev, "%s - parity = mark\n", __func__);
2557                         } else {
2558                                 lParity = LCR_PAR_SPACE;
2559                                 dev_dbg(dev, "%s - parity = space\n", __func__);
2560                         }
2561                 } else if (cflag & PARODD) {
2562                         lParity = LCR_PAR_ODD;
2563                         dev_dbg(dev, "%s - parity = odd\n", __func__);
2564                 } else {
2565                         lParity = LCR_PAR_EVEN;
2566                         dev_dbg(dev, "%s - parity = even\n", __func__);
2567                 }
2568         } else {
2569                 dev_dbg(dev, "%s - parity = none\n", __func__);
2570         }
2571
2572         if (cflag & CSTOPB) {
2573                 lStop = LCR_STOP_2;
2574                 dev_dbg(dev, "%s - stop bits = 2\n", __func__);
2575         } else {
2576                 lStop = LCR_STOP_1;
2577                 dev_dbg(dev, "%s - stop bits = 1\n", __func__);
2578         }
2579
2580         /* figure out the flow control settings */
2581         rxFlow = txFlow = 0x00;
2582         if (cflag & CRTSCTS) {
2583                 rxFlow |= IOSP_RX_FLOW_RTS;
2584                 txFlow |= IOSP_TX_FLOW_CTS;
2585                 dev_dbg(dev, "%s - RTS/CTS is enabled\n", __func__);
2586         } else {
2587                 dev_dbg(dev, "%s - RTS/CTS is disabled\n", __func__);
2588         }
2589
2590         /* if we are implementing XON/XOFF, set the start and stop character
2591            in the device */
2592         if (I_IXOFF(tty) || I_IXON(tty)) {
2593                 unsigned char stop_char  = STOP_CHAR(tty);
2594                 unsigned char start_char = START_CHAR(tty);
2595
2596                 if ((!edge_serial->is_epic) ||
2597                     ((edge_serial->is_epic) &&
2598                      (edge_serial->epic_descriptor.Supports.IOSPSetXChar))) {
2599                         send_iosp_ext_cmd(edge_port,
2600                                         IOSP_CMD_SET_XON_CHAR, start_char);
2601                         send_iosp_ext_cmd(edge_port,
2602                                         IOSP_CMD_SET_XOFF_CHAR, stop_char);
2603                 }
2604
2605                 /* if we are implementing INBOUND XON/XOFF */
2606                 if (I_IXOFF(tty)) {
2607                         rxFlow |= IOSP_RX_FLOW_XON_XOFF;
2608                         dev_dbg(dev, "%s - INBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x\n",
2609                                 __func__, start_char, stop_char);
2610                 } else {
2611                         dev_dbg(dev, "%s - INBOUND XON/XOFF is disabled\n", __func__);
2612                 }
2613
2614                 /* if we are implementing OUTBOUND XON/XOFF */
2615                 if (I_IXON(tty)) {
2616                         txFlow |= IOSP_TX_FLOW_XON_XOFF;
2617                         dev_dbg(dev, "%s - OUTBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x\n",
2618                                 __func__, start_char, stop_char);
2619                 } else {
2620                         dev_dbg(dev, "%s - OUTBOUND XON/XOFF is disabled\n", __func__);
2621                 }
2622         }
2623
2624         /* Set flow control to the configured value */
2625         if ((!edge_serial->is_epic) ||
2626             ((edge_serial->is_epic) &&
2627              (edge_serial->epic_descriptor.Supports.IOSPSetRxFlow)))
2628                 send_iosp_ext_cmd(edge_port, IOSP_CMD_SET_RX_FLOW, rxFlow);
2629         if ((!edge_serial->is_epic) ||
2630             ((edge_serial->is_epic) &&
2631              (edge_serial->epic_descriptor.Supports.IOSPSetTxFlow)))
2632                 send_iosp_ext_cmd(edge_port, IOSP_CMD_SET_TX_FLOW, txFlow);
2633
2634
2635         edge_port->shadowLCR &= ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
2636         edge_port->shadowLCR |= (lData | lParity | lStop);
2637
2638         edge_port->validDataMask = mask;
2639
2640         /* Send the updated LCR value to the EdgePort */
2641         status = send_cmd_write_uart_register(edge_port, LCR,
2642                                                         edge_port->shadowLCR);
2643         if (status != 0)
2644                 return;
2645
2646         /* set up the MCR register and send it to the EdgePort */
2647         edge_port->shadowMCR = MCR_MASTER_IE;
2648         if (cflag & CBAUD)
2649                 edge_port->shadowMCR |= (MCR_DTR | MCR_RTS);
2650
2651         status = send_cmd_write_uart_register(edge_port, MCR,
2652                                                 edge_port->shadowMCR);
2653         if (status != 0)
2654                 return;
2655
2656         /* Determine divisor based on baud rate */
2657         baud = tty_get_baud_rate(tty);
2658         if (!baud) {
2659                 /* pick a default, any default... */
2660                 baud = 9600;
2661         }
2662
2663         dev_dbg(dev, "%s - baud rate = %d\n", __func__, baud);
2664         status = send_cmd_write_baud_rate(edge_port, baud);
2665         if (status == -1) {
2666                 /* Speed change was not possible - put back the old speed */
2667                 baud = tty_termios_baud_rate(old_termios);
2668                 tty_encode_baud_rate(tty, baud, baud);
2669         }
2670 }
2671
2672
2673 /****************************************************************************
2674  * unicode_to_ascii
2675  *      Turns a string from Unicode into ASCII.
2676  *      Doesn't do a good job with any characters that are outside the normal
2677  *      ASCII range, but it's only for debugging...
2678  *      NOTE: expects the unicode in LE format
2679  ****************************************************************************/
2680 static void unicode_to_ascii(char *string, int buflen,
2681                                         __le16 *unicode, int unicode_size)
2682 {
2683         int i;
2684
2685         if (buflen <= 0)        /* never happens, but... */
2686                 return;
2687         --buflen;               /* space for nul */
2688
2689         for (i = 0; i < unicode_size; i++) {
2690                 if (i >= buflen)
2691                         break;
2692                 string[i] = (char)(le16_to_cpu(unicode[i]));
2693         }
2694         string[i] = 0x00;
2695 }
2696
2697
2698 /****************************************************************************
2699  * get_manufacturing_desc
2700  *      reads in the manufacturing descriptor and stores it into the serial
2701  *      structure.
2702  ****************************************************************************/
2703 static void get_manufacturing_desc(struct edgeport_serial *edge_serial)
2704 {
2705         struct device *dev = &edge_serial->serial->dev->dev;
2706         int response;
2707
2708         dev_dbg(dev, "getting manufacturer descriptor\n");
2709
2710         response = rom_read(edge_serial->serial,
2711                                 (EDGE_MANUF_DESC_ADDR & 0xffff0000) >> 16,
2712                                 (__u16)(EDGE_MANUF_DESC_ADDR & 0x0000ffff),
2713                                 EDGE_MANUF_DESC_LEN,
2714                                 (__u8 *)(&edge_serial->manuf_descriptor));
2715
2716         if (response < 1)
2717                 dev_err(dev, "error in getting manufacturer descriptor\n");
2718         else {
2719                 char string[30];
2720                 dev_dbg(dev, "**Manufacturer Descriptor\n");
2721                 dev_dbg(dev, "  RomSize:        %dK\n",
2722                         edge_serial->manuf_descriptor.RomSize);
2723                 dev_dbg(dev, "  RamSize:        %dK\n",
2724                         edge_serial->manuf_descriptor.RamSize);
2725                 dev_dbg(dev, "  CpuRev:         %d\n",
2726                         edge_serial->manuf_descriptor.CpuRev);
2727                 dev_dbg(dev, "  BoardRev:       %d\n",
2728                         edge_serial->manuf_descriptor.BoardRev);
2729                 dev_dbg(dev, "  NumPorts:       %d\n",
2730                         edge_serial->manuf_descriptor.NumPorts);
2731                 dev_dbg(dev, "  DescDate:       %d/%d/%d\n",
2732                         edge_serial->manuf_descriptor.DescDate[0],
2733                         edge_serial->manuf_descriptor.DescDate[1],
2734                         edge_serial->manuf_descriptor.DescDate[2]+1900);
2735                 unicode_to_ascii(string, sizeof(string),
2736                         edge_serial->manuf_descriptor.SerialNumber,
2737                         edge_serial->manuf_descriptor.SerNumLength/2);
2738                 dev_dbg(dev, "  SerialNumber: %s\n", string);
2739                 unicode_to_ascii(string, sizeof(string),
2740                         edge_serial->manuf_descriptor.AssemblyNumber,
2741                         edge_serial->manuf_descriptor.AssemblyNumLength/2);
2742                 dev_dbg(dev, "  AssemblyNumber: %s\n", string);
2743                 unicode_to_ascii(string, sizeof(string),
2744                     edge_serial->manuf_descriptor.OemAssyNumber,
2745                     edge_serial->manuf_descriptor.OemAssyNumLength/2);
2746                 dev_dbg(dev, "  OemAssyNumber:  %s\n", string);
2747                 dev_dbg(dev, "  UartType:       %d\n",
2748                         edge_serial->manuf_descriptor.UartType);
2749                 dev_dbg(dev, "  IonPid:         %d\n",
2750                         edge_serial->manuf_descriptor.IonPid);
2751                 dev_dbg(dev, "  IonConfig:      %d\n",
2752                         edge_serial->manuf_descriptor.IonConfig);
2753         }
2754 }
2755
2756
2757 /****************************************************************************
2758  * get_boot_desc
2759  *      reads in the bootloader descriptor and stores it into the serial
2760  *      structure.
2761  ****************************************************************************/
2762 static void get_boot_desc(struct edgeport_serial *edge_serial)
2763 {
2764         struct device *dev = &edge_serial->serial->dev->dev;
2765         int response;
2766
2767         dev_dbg(dev, "getting boot descriptor\n");
2768
2769         response = rom_read(edge_serial->serial,
2770                                 (EDGE_BOOT_DESC_ADDR & 0xffff0000) >> 16,
2771                                 (__u16)(EDGE_BOOT_DESC_ADDR & 0x0000ffff),
2772                                 EDGE_BOOT_DESC_LEN,
2773                                 (__u8 *)(&edge_serial->boot_descriptor));
2774
2775         if (response < 1)
2776                 dev_err(dev, "error in getting boot descriptor\n");
2777         else {
2778                 dev_dbg(dev, "**Boot Descriptor:\n");
2779                 dev_dbg(dev, "  BootCodeLength: %d\n",
2780                         le16_to_cpu(edge_serial->boot_descriptor.BootCodeLength));
2781                 dev_dbg(dev, "  MajorVersion:   %d\n",
2782                         edge_serial->boot_descriptor.MajorVersion);
2783                 dev_dbg(dev, "  MinorVersion:   %d\n",
2784                         edge_serial->boot_descriptor.MinorVersion);
2785                 dev_dbg(dev, "  BuildNumber:    %d\n",
2786                         le16_to_cpu(edge_serial->boot_descriptor.BuildNumber));
2787                 dev_dbg(dev, "  Capabilities:   0x%x\n",
2788                       le16_to_cpu(edge_serial->boot_descriptor.Capabilities));
2789                 dev_dbg(dev, "  UConfig0:       %d\n",
2790                         edge_serial->boot_descriptor.UConfig0);
2791                 dev_dbg(dev, "  UConfig1:       %d\n",
2792                         edge_serial->boot_descriptor.UConfig1);
2793         }
2794 }
2795
2796
2797 /****************************************************************************
2798  * load_application_firmware
2799  *      This is called to load the application firmware to the device
2800  ****************************************************************************/
2801 static void load_application_firmware(struct edgeport_serial *edge_serial)
2802 {
2803         struct device *dev = &edge_serial->serial->dev->dev;
2804         const struct ihex_binrec *rec;
2805         const struct firmware *fw;
2806         const char *fw_name;
2807         const char *fw_info;
2808         int response;
2809         __u32 Operaddr;
2810         __u16 build;
2811
2812         switch (edge_serial->product_info.iDownloadFile) {
2813                 case EDGE_DOWNLOAD_FILE_I930:
2814                         fw_info = "downloading firmware version (930)";
2815                         fw_name = "edgeport/down.fw";
2816                         break;
2817
2818                 case EDGE_DOWNLOAD_FILE_80251:
2819                         fw_info = "downloading firmware version (80251)";
2820                         fw_name = "edgeport/down2.fw";
2821                         break;
2822
2823                 case EDGE_DOWNLOAD_FILE_NONE:
2824                         dev_dbg(dev, "No download file specified, skipping download\n");
2825                         return;
2826
2827                 default:
2828                         return;
2829         }
2830
2831         response = request_ihex_firmware(&fw, fw_name,
2832                                     &edge_serial->serial->dev->dev);
2833         if (response) {
2834                 dev_err(dev, "Failed to load image \"%s\" err %d\n",
2835                        fw_name, response);
2836                 return;
2837         }
2838
2839         rec = (const struct ihex_binrec *)fw->data;
2840         build = (rec->data[2] << 8) | rec->data[3];
2841
2842         dev_dbg(dev, "%s %d.%d.%d\n", fw_info, rec->data[0], rec->data[1], build);
2843
2844         edge_serial->product_info.FirmwareMajorVersion = rec->data[0];
2845         edge_serial->product_info.FirmwareMinorVersion = rec->data[1];
2846         edge_serial->product_info.FirmwareBuildNumber = cpu_to_le16(build);
2847
2848         for (rec = ihex_next_binrec(rec); rec;
2849              rec = ihex_next_binrec(rec)) {
2850                 Operaddr = be32_to_cpu(rec->addr);
2851                 response = sram_write(edge_serial->serial,
2852                                      Operaddr >> 16,
2853                                      Operaddr & 0xFFFF,
2854                                      be16_to_cpu(rec->len),
2855                                      &rec->data[0]);
2856                 if (response < 0) {
2857                         dev_err(&edge_serial->serial->dev->dev,
2858                                 "sram_write failed (%x, %x, %d)\n",
2859                                 Operaddr >> 16, Operaddr & 0xFFFF,
2860                                 be16_to_cpu(rec->len));
2861                         break;
2862                 }
2863         }
2864
2865         dev_dbg(dev, "sending exec_dl_code\n");
2866         response = usb_control_msg (edge_serial->serial->dev,
2867                                     usb_sndctrlpipe(edge_serial->serial->dev, 0),
2868                                     USB_REQUEST_ION_EXEC_DL_CODE,
2869                                     0x40, 0x4000, 0x0001, NULL, 0, 3000);
2870
2871         release_firmware(fw);
2872 }
2873
2874
2875 /****************************************************************************
2876  * edge_startup
2877  ****************************************************************************/
2878 static int edge_startup(struct usb_serial *serial)
2879 {
2880         struct edgeport_serial *edge_serial;
2881         struct edgeport_port *edge_port;
2882         struct usb_device *dev;
2883         struct device *ddev = &serial->dev->dev;
2884         int i, j;
2885         int response;
2886         bool interrupt_in_found;
2887         bool bulk_in_found;
2888         bool bulk_out_found;
2889         static __u32 descriptor[3] = {  EDGE_COMPATIBILITY_MASK0,
2890                                         EDGE_COMPATIBILITY_MASK1,
2891                                         EDGE_COMPATIBILITY_MASK2 };
2892
2893         dev = serial->dev;
2894
2895         /* create our private serial structure */
2896         edge_serial = kzalloc(sizeof(struct edgeport_serial), GFP_KERNEL);
2897         if (edge_serial == NULL) {
2898                 dev_err(&serial->dev->dev, "%s - Out of memory\n", __func__);
2899                 return -ENOMEM;
2900         }
2901         spin_lock_init(&edge_serial->es_lock);
2902         edge_serial->serial = serial;
2903         usb_set_serial_data(serial, edge_serial);
2904
2905         /* get the name for the device from the device */
2906         i = usb_string(dev, dev->descriptor.iManufacturer,
2907             &edge_serial->name[0], MAX_NAME_LEN+1);
2908         if (i < 0)
2909                 i = 0;
2910         edge_serial->name[i++] = ' ';
2911         usb_string(dev, dev->descriptor.iProduct,
2912             &edge_serial->name[i], MAX_NAME_LEN+2 - i);
2913
2914         dev_info(&serial->dev->dev, "%s detected\n", edge_serial->name);
2915
2916         /* Read the epic descriptor */
2917         if (get_epic_descriptor(edge_serial) <= 0) {
2918                 /* memcpy descriptor to Supports structures */
2919                 memcpy(&edge_serial->epic_descriptor.Supports, descriptor,
2920                        sizeof(struct edge_compatibility_bits));
2921
2922                 /* get the manufacturing descriptor for this device */
2923                 get_manufacturing_desc(edge_serial);
2924
2925                 /* get the boot descriptor */
2926                 get_boot_desc(edge_serial);
2927
2928                 get_product_info(edge_serial);
2929         }
2930
2931         /* set the number of ports from the manufacturing description */
2932         /* serial->num_ports = serial->product_info.NumPorts; */
2933         if ((!edge_serial->is_epic) &&
2934             (edge_serial->product_info.NumPorts != serial->num_ports)) {
2935                 dev_warn(ddev,
2936                         "Device Reported %d serial ports vs. core thinking we have %d ports, email greg@kroah.com this information.\n",
2937                          edge_serial->product_info.NumPorts,
2938                          serial->num_ports);
2939         }
2940
2941         dev_dbg(ddev, "%s - time 1 %ld\n", __func__, jiffies);
2942
2943         /* If not an EPiC device */
2944         if (!edge_serial->is_epic) {
2945                 /* now load the application firmware into this device */
2946                 load_application_firmware(edge_serial);
2947
2948                 dev_dbg(ddev, "%s - time 2 %ld\n", __func__, jiffies);
2949
2950                 /* Check current Edgeport EEPROM and update if necessary */
2951                 update_edgeport_E2PROM(edge_serial);
2952
2953                 dev_dbg(ddev, "%s - time 3 %ld\n", __func__, jiffies);
2954
2955                 /* set the configuration to use #1 */
2956 /*              dev_dbg(ddev, "set_configuration 1\n"); */
2957 /*              usb_set_configuration (dev, 1); */
2958         }
2959         dev_dbg(ddev, "  FirmwareMajorVersion  %d.%d.%d\n",
2960             edge_serial->product_info.FirmwareMajorVersion,
2961             edge_serial->product_info.FirmwareMinorVersion,
2962             le16_to_cpu(edge_serial->product_info.FirmwareBuildNumber));
2963
2964         /* we set up the pointers to the endpoints in the edge_open function,
2965          * as the structures aren't created yet. */
2966
2967         /* set up our port private structures */
2968         for (i = 0; i < serial->num_ports; ++i) {
2969                 edge_port = kzalloc(sizeof(struct edgeport_port), GFP_KERNEL);
2970                 if (edge_port == NULL) {
2971                         dev_err(ddev, "%s - Out of memory\n", __func__);
2972                         for (j = 0; j < i; ++j) {
2973                                 kfree(usb_get_serial_port_data(serial->port[j]));
2974                                 usb_set_serial_port_data(serial->port[j],
2975                                                                         NULL);
2976                         }
2977                         usb_set_serial_data(serial, NULL);
2978                         kfree(edge_serial);
2979                         return -ENOMEM;
2980                 }
2981                 spin_lock_init(&edge_port->ep_lock);
2982                 edge_port->port = serial->port[i];
2983                 usb_set_serial_port_data(serial->port[i], edge_port);
2984         }
2985
2986         response = 0;
2987
2988         if (edge_serial->is_epic) {
2989                 /* EPIC thing, set up our interrupt polling now and our read
2990                  * urb, so that the device knows it really is connected. */
2991                 interrupt_in_found = bulk_in_found = bulk_out_found = false;
2992                 for (i = 0; i < serial->interface->altsetting[0]
2993                                                 .desc.bNumEndpoints; ++i) {
2994                         struct usb_endpoint_descriptor *endpoint;
2995                         int buffer_size;
2996
2997                         endpoint = &serial->interface->altsetting[0].
2998                                                         endpoint[i].desc;
2999                         buffer_size = usb_endpoint_maxp(endpoint);
3000                         if (!interrupt_in_found &&
3001                             (usb_endpoint_is_int_in(endpoint))) {
3002                                 /* we found a interrupt in endpoint */
3003                                 dev_dbg(ddev, "found interrupt in\n");
3004
3005                                 /* not set up yet, so do it now */
3006                                 edge_serial->interrupt_read_urb =
3007                                                 usb_alloc_urb(0, GFP_KERNEL);
3008                                 if (!edge_serial->interrupt_read_urb) {
3009                                         dev_err(ddev, "out of memory\n");
3010                                         return -ENOMEM;
3011                                 }
3012                                 edge_serial->interrupt_in_buffer =
3013                                         kmalloc(buffer_size, GFP_KERNEL);
3014                                 if (!edge_serial->interrupt_in_buffer) {
3015                                         dev_err(ddev, "out of memory\n");
3016                                         usb_free_urb(edge_serial->interrupt_read_urb);
3017                                         return -ENOMEM;
3018                                 }
3019                                 edge_serial->interrupt_in_endpoint =
3020                                                 endpoint->bEndpointAddress;
3021
3022                                 /* set up our interrupt urb */
3023                                 usb_fill_int_urb(
3024                                         edge_serial->interrupt_read_urb,
3025                                         dev,
3026                                         usb_rcvintpipe(dev,
3027                                                 endpoint->bEndpointAddress),
3028                                         edge_serial->interrupt_in_buffer,
3029                                         buffer_size,
3030                                         edge_interrupt_callback,
3031                                         edge_serial,
3032                                         endpoint->bInterval);
3033
3034                                 interrupt_in_found = true;
3035                         }
3036
3037                         if (!bulk_in_found &&
3038                                 (usb_endpoint_is_bulk_in(endpoint))) {
3039                                 /* we found a bulk in endpoint */
3040                                 dev_dbg(ddev, "found bulk in\n");
3041
3042                                 /* not set up yet, so do it now */
3043                                 edge_serial->read_urb =
3044                                                 usb_alloc_urb(0, GFP_KERNEL);
3045                                 if (!edge_serial->read_urb) {
3046                                         dev_err(ddev, "out of memory\n");
3047                                         return -ENOMEM;
3048                                 }
3049                                 edge_serial->bulk_in_buffer =
3050                                         kmalloc(buffer_size, GFP_KERNEL);
3051                                 if (!edge_serial->bulk_in_buffer) {
3052                                         dev_err(&dev->dev, "out of memory\n");
3053                                         usb_free_urb(edge_serial->read_urb);
3054                                         return -ENOMEM;
3055                                 }
3056                                 edge_serial->bulk_in_endpoint =
3057                                                 endpoint->bEndpointAddress;
3058
3059                                 /* set up our bulk in urb */
3060                                 usb_fill_bulk_urb(edge_serial->read_urb, dev,
3061                                         usb_rcvbulkpipe(dev,
3062                                                 endpoint->bEndpointAddress),
3063                                         edge_serial->bulk_in_buffer,
3064                                         usb_endpoint_maxp(endpoint),
3065                                         edge_bulk_in_callback,
3066                                         edge_serial);
3067                                 bulk_in_found = true;
3068                         }
3069
3070                         if (!bulk_out_found &&
3071                             (usb_endpoint_is_bulk_out(endpoint))) {
3072                                 /* we found a bulk out endpoint */
3073                                 dev_dbg(ddev, "found bulk out\n");
3074                                 edge_serial->bulk_out_endpoint =
3075                                                 endpoint->bEndpointAddress;
3076                                 bulk_out_found = true;
3077                         }
3078                 }
3079
3080                 if (!interrupt_in_found || !bulk_in_found || !bulk_out_found) {
3081                         dev_err(ddev, "Error - the proper endpoints were not found!\n");
3082                         return -ENODEV;
3083                 }
3084
3085                 /* start interrupt read for this edgeport this interrupt will
3086                  * continue as long as the edgeport is connected */
3087                 response = usb_submit_urb(edge_serial->interrupt_read_urb,
3088                                                                 GFP_KERNEL);
3089                 if (response)
3090                         dev_err(ddev, "%s - Error %d submitting control urb\n",
3091                                 __func__, response);
3092         }
3093         return response;
3094 }
3095
3096
3097 /****************************************************************************
3098  * edge_disconnect
3099  *      This function is called whenever the device is removed from the usb bus.
3100  ****************************************************************************/
3101 static void edge_disconnect(struct usb_serial *serial)
3102 {
3103         struct edgeport_serial *edge_serial = usb_get_serial_data(serial);
3104
3105         /* stop reads and writes on all ports */
3106         /* free up our endpoint stuff */
3107         if (edge_serial->is_epic) {
3108                 usb_kill_urb(edge_serial->interrupt_read_urb);
3109                 usb_free_urb(edge_serial->interrupt_read_urb);
3110                 kfree(edge_serial->interrupt_in_buffer);
3111
3112                 usb_kill_urb(edge_serial->read_urb);
3113                 usb_free_urb(edge_serial->read_urb);
3114                 kfree(edge_serial->bulk_in_buffer);
3115         }
3116 }
3117
3118
3119 /****************************************************************************
3120  * edge_release
3121  *      This function is called when the device structure is deallocated.
3122  ****************************************************************************/
3123 static void edge_release(struct usb_serial *serial)
3124 {
3125         struct edgeport_serial *edge_serial = usb_get_serial_data(serial);
3126         int i;
3127
3128         for (i = 0; i < serial->num_ports; ++i)
3129                 kfree(usb_get_serial_port_data(serial->port[i]));
3130
3131         kfree(edge_serial);
3132 }
3133
3134 module_usb_serial_driver(serial_drivers, id_table_combined);
3135
3136 MODULE_AUTHOR(DRIVER_AUTHOR);
3137 MODULE_DESCRIPTION(DRIVER_DESC);
3138 MODULE_LICENSE("GPL");
3139 MODULE_FIRMWARE("edgeport/boot.fw");
3140 MODULE_FIRMWARE("edgeport/boot2.fw");
3141 MODULE_FIRMWARE("edgeport/down.fw");
3142 MODULE_FIRMWARE("edgeport/down2.fw");
3143
3144 module_param(debug, bool, S_IRUGO | S_IWUSR);
3145 MODULE_PARM_DESC(debug, "Debug enabled or not");