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