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