2 * Edgeport USB Serial Converter driver
4 * Copyright (C) 2000 Inside Out Networks, All rights reserved.
5 * Copyright (C) 2001-2002 Greg Kroah-Hartman <greg@kroah.com>
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.
12 * Supports the following devices:
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>.
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 <asm/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 */
57 #define DRIVER_VERSION "v2.7"
58 #define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com> and David Iacovelli"
59 #define DRIVER_DESC "Edgeport USB Serial Driver"
61 #define MAX_NAME_LEN 64
63 #define CHASE_TIMEOUT (5*HZ) /* 5 seconds */
64 #define OPEN_TIMEOUT (5*HZ) /* 5 seconds */
65 #define COMMAND_TIMEOUT (5*HZ) /* 5 seconds */
67 /* receive port state */
69 EXPECT_HDR1 = 0, /* Expect header byte 1 */
70 EXPECT_HDR2 = 1, /* Expect header byte 2 */
71 EXPECT_DATA = 2, /* Expect 'RxBytesRemaining' data */
72 EXPECT_HDR3 = 3, /* Expect header byte 3 (for status hdrs only) */
77 * This Transmit queue is an extension of the edgeport Rx buffer.
78 * The maximum amount of data buffered in both the edgeport
79 * Rx buffer (maxTxCredits) and this buffer will never exceed maxTxCredits.
82 unsigned int head; /* index to head pointer (write) */
83 unsigned int tail; /* index to tail pointer (read) */
84 unsigned int count; /* Bytes in queue */
85 unsigned int size; /* Max size of queue (equal to Max number of TxCredits) */
86 unsigned char *fifo; /* allocated Buffer */
89 /* This structure holds all of the local port information */
90 struct edgeport_port {
91 __u16 txCredits; /* our current credits for this port */
92 __u16 maxTxCredits; /* the max size of the port */
94 struct TxFifo txfifo; /* transmit fifo -- size will be maxTxCredits */
95 struct urb *write_urb; /* write URB for this port */
96 bool write_in_progress; /* 'true' while a write URB is outstanding */
99 __u8 shadowLCR; /* last LCR value received */
100 __u8 shadowMCR; /* last MCR value received */
101 __u8 shadowMSR; /* last MSR value received */
102 __u8 shadowLSR; /* last LSR value received */
103 __u8 shadowXonChar; /* last value set as XON char in Edgeport */
104 __u8 shadowXoffChar; /* last value set as XOFF char in Edgeport */
112 bool chaseResponsePending;
114 wait_queue_head_t wait_chase; /* for handling sleeping while waiting for chase to finish */
115 wait_queue_head_t wait_open; /* for handling sleeping while waiting for open to finish */
116 wait_queue_head_t wait_command; /* for handling sleeping while waiting for command to finish */
117 wait_queue_head_t delta_msr_wait; /* for handling sleeping while waiting for msr change to happen */
119 struct async_icount icount;
120 struct usb_serial_port *port; /* loop back to the owner of this object */
124 /* This structure holds all of the individual device information */
125 struct edgeport_serial {
126 char name[MAX_NAME_LEN+2]; /* string name of this device */
128 struct edge_manuf_descriptor manuf_descriptor; /* the manufacturer descriptor */
129 struct edge_boot_descriptor boot_descriptor; /* the boot firmware descriptor */
130 struct edgeport_product_info product_info; /* Product Info */
131 struct edge_compatibility_descriptor epic_descriptor; /* Edgeport compatible descriptor */
132 int is_epic; /* flag if EPiC device or not */
134 __u8 interrupt_in_endpoint; /* the interrupt endpoint handle */
135 unsigned char * interrupt_in_buffer; /* the buffer we use for the interrupt endpoint */
136 struct urb * interrupt_read_urb; /* our interrupt urb */
138 __u8 bulk_in_endpoint; /* the bulk in endpoint handle */
139 unsigned char * bulk_in_buffer; /* the buffer we use for the bulk in endpoint */
140 struct urb * read_urb; /* our bulk read urb */
141 bool read_in_progress;
144 __u8 bulk_out_endpoint; /* the bulk out endpoint handle */
146 __s16 rxBytesAvail; /* the number of bytes that we need to read from this device */
148 enum RXSTATE rxState; /* the current state of the bulk receive processor */
149 __u8 rxHeader1; /* receive header byte 1 */
150 __u8 rxHeader2; /* receive header byte 2 */
151 __u8 rxHeader3; /* receive header byte 3 */
152 __u8 rxPort; /* the port that we are currently receiving data for */
153 __u8 rxStatusCode; /* the receive status code */
154 __u8 rxStatusParam; /* the receive status paramater */
155 __s16 rxBytesRemaining; /* the number of port bytes left to read */
156 struct usb_serial *serial; /* loop back to the owner of this object */
159 /* baud rate information */
160 struct divisor_table_entry {
166 // Define table of divisors for Rev A EdgePort/4 hardware
167 // These assume a 3.6864MHz crystal, the standard /16, and
170 static const struct divisor_table_entry divisor_table[] = {
173 { 110, 2095}, /* 2094.545455 => 230450 => .0217 % over */
174 { 134, 1713}, /* 1713.011152 => 230398.5 => .00065% under */
192 /* local variables */
195 static int low_latency = 1; /* tty low latency flag, on by default */
197 static atomic_t CmdUrbs; /* Number of outstanding Command Write Urbs */
200 /* local function prototypes */
202 /* function prototypes for all URB callbacks */
203 static void edge_interrupt_callback (struct urb *urb);
204 static void edge_bulk_in_callback (struct urb *urb);
205 static void edge_bulk_out_data_callback (struct urb *urb);
206 static void edge_bulk_out_cmd_callback (struct urb *urb);
208 /* function prototypes for the usbserial callbacks */
209 static int edge_open (struct tty_struct *tty, struct usb_serial_port *port, struct file *filp);
210 static void edge_close (struct tty_struct *tty, struct usb_serial_port *port, struct file *filp);
211 static int edge_write (struct tty_struct *tty, struct usb_serial_port *port, const unsigned char *buf, int count);
212 static int edge_write_room (struct tty_struct *tty);
213 static int edge_chars_in_buffer (struct tty_struct *tty);
214 static void edge_throttle (struct tty_struct *tty);
215 static void edge_unthrottle (struct tty_struct *tty);
216 static void edge_set_termios (struct tty_struct *tty, struct usb_serial_port *port, struct ktermios *old_termios);
217 static int edge_ioctl (struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg);
218 static void edge_break (struct tty_struct *tty, int break_state);
219 static int edge_tiocmget (struct tty_struct *tty, struct file *file);
220 static int edge_tiocmset (struct tty_struct *tty, struct file *file, unsigned int set, unsigned int clear);
221 static int edge_startup (struct usb_serial *serial);
222 static void edge_shutdown (struct usb_serial *serial);
225 #include "io_tables.h" /* all of the devices that this driver supports */
227 /* function prototypes for all of our local functions */
228 static void process_rcvd_data (struct edgeport_serial *edge_serial, unsigned char *buffer, __u16 bufferLength);
229 static void process_rcvd_status (struct edgeport_serial *edge_serial, __u8 byte2, __u8 byte3);
230 static void edge_tty_recv (struct device *dev, struct tty_struct *tty, unsigned char *data, int length);
231 static void handle_new_msr (struct edgeport_port *edge_port, __u8 newMsr);
232 static void handle_new_lsr (struct edgeport_port *edge_port, __u8 lsrData, __u8 lsr, __u8 data);
233 static int send_iosp_ext_cmd (struct edgeport_port *edge_port, __u8 command, __u8 param);
234 static int calc_baud_rate_divisor (int baud_rate, int *divisor);
235 static int send_cmd_write_baud_rate (struct edgeport_port *edge_port, int baudRate);
236 static void change_port_settings (struct tty_struct *tty, struct edgeport_port *edge_port,
237 struct ktermios *old_termios);
238 static int send_cmd_write_uart_register (struct edgeport_port *edge_port, __u8 regNum, __u8 regValue);
239 static int write_cmd_usb (struct edgeport_port *edge_port, unsigned char *buffer, int writeLength);
240 static void send_more_port_data (struct edgeport_serial *edge_serial, struct edgeport_port *edge_port);
242 static int sram_write (struct usb_serial *serial, __u16 extAddr, __u16 addr, __u16 length, const __u8 *data);
243 static int rom_read (struct usb_serial *serial, __u16 extAddr, __u16 addr, __u16 length, __u8 *data);
244 static int rom_write (struct usb_serial *serial, __u16 extAddr, __u16 addr, __u16 length, const __u8 *data);
245 static void get_manufacturing_desc (struct edgeport_serial *edge_serial);
246 static void get_boot_desc (struct edgeport_serial *edge_serial);
247 static void load_application_firmware (struct edgeport_serial *edge_serial);
249 static void unicode_to_ascii(char *string, int buflen, __le16 *unicode, int unicode_size);
252 // ************************************************************************
253 // ************************************************************************
254 // ************************************************************************
255 // ************************************************************************
257 /************************************************************************
259 * update_edgeport_E2PROM() Compare current versions of *
260 * Boot ROM and Manufacture *
261 * Descriptors with versions *
262 * embedded in this driver *
264 ************************************************************************/
265 static void update_edgeport_E2PROM (struct edgeport_serial *edge_serial)
269 __u8 BootMajorVersion;
270 __u8 BootMinorVersion;
271 __u16 BootBuildNumber;
273 const struct ihex_binrec *rec;
274 const struct firmware *fw;
278 switch (edge_serial->product_info.iDownloadFile) {
279 case EDGE_DOWNLOAD_FILE_I930:
280 fw_name = "edgeport/boot.fw";
283 case EDGE_DOWNLOAD_FILE_80251:
284 fw_name = "edgeport/boot2.fw";
291 response = request_ihex_firmware(&fw, fw_name,
292 &edge_serial->serial->dev->dev);
294 printk(KERN_ERR "Failed to load image \"%s\" err %d\n",
299 rec = (const struct ihex_binrec *)fw->data;
300 BootMajorVersion = rec->data[0];
301 BootMinorVersion = rec->data[1];
302 BootBuildNumber = (rec->data[2] << 8) | rec->data[3];
304 // Check Boot Image Version
305 BootCurVer = (edge_serial->boot_descriptor.MajorVersion << 24) +
306 (edge_serial->boot_descriptor.MinorVersion << 16) +
307 le16_to_cpu(edge_serial->boot_descriptor.BuildNumber);
309 BootNewVer = (BootMajorVersion << 24) +
310 (BootMinorVersion << 16) +
313 dbg("Current Boot Image version %d.%d.%d",
314 edge_serial->boot_descriptor.MajorVersion,
315 edge_serial->boot_descriptor.MinorVersion,
316 le16_to_cpu(edge_serial->boot_descriptor.BuildNumber));
319 if (BootNewVer > BootCurVer) {
320 dbg("**Update Boot Image from %d.%d.%d to %d.%d.%d",
321 edge_serial->boot_descriptor.MajorVersion,
322 edge_serial->boot_descriptor.MinorVersion,
323 le16_to_cpu(edge_serial->boot_descriptor.BuildNumber),
324 BootMajorVersion, BootMinorVersion, BootBuildNumber);
326 dbg("Downloading new Boot Image");
328 for (rec = ihex_next_binrec(rec); rec;
329 rec = ihex_next_binrec(rec)) {
330 Bootaddr = be32_to_cpu(rec->addr);
331 response = rom_write(edge_serial->serial,
334 be16_to_cpu(rec->len),
337 dev_err(&edge_serial->serial->dev->dev,
338 "rom_write failed (%x, %x, %d)\n",
339 Bootaddr >> 16, Bootaddr & 0xFFFF,
340 be16_to_cpu(rec->len));
345 dbg("Boot Image -- already up to date");
347 release_firmware(fw);
351 /************************************************************************
353 * Get string descriptor from device *
355 ************************************************************************/
356 static int get_string (struct usb_device *dev, int Id, char *string, int buflen)
358 struct usb_string_descriptor StringDesc;
359 struct usb_string_descriptor *pStringDesc;
361 dbg("%s - USB String ID = %d", __func__, Id );
363 if (!usb_get_descriptor(dev, USB_DT_STRING, Id, &StringDesc, sizeof(StringDesc))) {
367 pStringDesc = kmalloc (StringDesc.bLength, GFP_KERNEL);
373 if (!usb_get_descriptor(dev, USB_DT_STRING, Id, pStringDesc, StringDesc.bLength )) {
378 unicode_to_ascii(string, buflen, pStringDesc->wData, pStringDesc->bLength/2);
381 dbg("%s - USB String %s", __func__, string);
382 return strlen(string);
387 /************************************************************************
389 * Get string descriptor from device
391 ************************************************************************/
392 static int get_string_desc (struct usb_device *dev, int Id, struct usb_string_descriptor **pRetDesc)
394 struct usb_string_descriptor StringDesc;
395 struct usb_string_descriptor *pStringDesc;
397 dbg("%s - USB String ID = %d", __func__, Id );
399 if (!usb_get_descriptor(dev, USB_DT_STRING, Id, &StringDesc, sizeof(StringDesc))) {
403 pStringDesc = kmalloc (StringDesc.bLength, GFP_KERNEL);
409 if (!usb_get_descriptor(dev, USB_DT_STRING, Id, pStringDesc, StringDesc.bLength )) {
414 *pRetDesc = pStringDesc;
419 static void dump_product_info(struct edgeport_product_info *product_info)
421 // Dump Product Info structure
422 dbg("**Product Information:");
423 dbg(" ProductId %x", product_info->ProductId );
424 dbg(" NumPorts %d", product_info->NumPorts );
425 dbg(" ProdInfoVer %d", product_info->ProdInfoVer );
426 dbg(" IsServer %d", product_info->IsServer);
427 dbg(" IsRS232 %d", product_info->IsRS232 );
428 dbg(" IsRS422 %d", product_info->IsRS422 );
429 dbg(" IsRS485 %d", product_info->IsRS485 );
430 dbg(" RomSize %d", product_info->RomSize );
431 dbg(" RamSize %d", product_info->RamSize );
432 dbg(" CpuRev %x", product_info->CpuRev );
433 dbg(" BoardRev %x", product_info->BoardRev);
434 dbg(" BootMajorVersion %d.%d.%d", product_info->BootMajorVersion,
435 product_info->BootMinorVersion,
436 le16_to_cpu(product_info->BootBuildNumber));
437 dbg(" ManufactureDescDate %d/%d/%d", product_info->ManufactureDescDate[0],
438 product_info->ManufactureDescDate[1],
439 product_info->ManufactureDescDate[2]+1900);
440 dbg(" iDownloadFile 0x%x", product_info->iDownloadFile);
441 dbg(" EpicVer %d", product_info->EpicVer);
444 static void get_product_info(struct edgeport_serial *edge_serial)
446 struct edgeport_product_info *product_info = &edge_serial->product_info;
448 memset (product_info, 0, sizeof(struct edgeport_product_info));
450 product_info->ProductId = (__u16)(le16_to_cpu(edge_serial->serial->dev->descriptor.idProduct) & ~ION_DEVICE_ID_80251_NETCHIP);
451 product_info->NumPorts = edge_serial->manuf_descriptor.NumPorts;
452 product_info->ProdInfoVer = 0;
454 product_info->RomSize = edge_serial->manuf_descriptor.RomSize;
455 product_info->RamSize = edge_serial->manuf_descriptor.RamSize;
456 product_info->CpuRev = edge_serial->manuf_descriptor.CpuRev;
457 product_info->BoardRev = edge_serial->manuf_descriptor.BoardRev;
459 product_info->BootMajorVersion = edge_serial->boot_descriptor.MajorVersion;
460 product_info->BootMinorVersion = edge_serial->boot_descriptor.MinorVersion;
461 product_info->BootBuildNumber = edge_serial->boot_descriptor.BuildNumber;
463 memcpy(product_info->ManufactureDescDate, edge_serial->manuf_descriptor.DescDate, sizeof(edge_serial->manuf_descriptor.DescDate));
465 // check if this is 2nd generation hardware
466 if (le16_to_cpu(edge_serial->serial->dev->descriptor.idProduct) & ION_DEVICE_ID_80251_NETCHIP) {
467 product_info->iDownloadFile = EDGE_DOWNLOAD_FILE_80251;
469 product_info->iDownloadFile = EDGE_DOWNLOAD_FILE_I930;
472 // Determine Product type and set appropriate flags
473 switch (DEVICE_ID_FROM_USB_PRODUCT_ID(product_info->ProductId)) {
474 case ION_DEVICE_ID_EDGEPORT_COMPATIBLE:
475 case ION_DEVICE_ID_EDGEPORT_4T:
476 case ION_DEVICE_ID_EDGEPORT_4:
477 case ION_DEVICE_ID_EDGEPORT_2:
478 case ION_DEVICE_ID_EDGEPORT_8_DUAL_CPU:
479 case ION_DEVICE_ID_EDGEPORT_8:
480 case ION_DEVICE_ID_EDGEPORT_421:
481 case ION_DEVICE_ID_EDGEPORT_21:
482 case ION_DEVICE_ID_EDGEPORT_2_DIN:
483 case ION_DEVICE_ID_EDGEPORT_4_DIN:
484 case ION_DEVICE_ID_EDGEPORT_16_DUAL_CPU:
485 product_info->IsRS232 = 1;
488 case ION_DEVICE_ID_EDGEPORT_2I: // Edgeport/2 RS422/RS485
489 product_info->IsRS422 = 1;
490 product_info->IsRS485 = 1;
493 case ION_DEVICE_ID_EDGEPORT_8I: // Edgeport/4 RS422
494 case ION_DEVICE_ID_EDGEPORT_4I: // Edgeport/4 RS422
495 product_info->IsRS422 = 1;
499 dump_product_info(product_info);
502 static int get_epic_descriptor(struct edgeport_serial *ep)
505 struct usb_serial *serial = ep->serial;
506 struct edgeport_product_info *product_info = &ep->product_info;
507 struct edge_compatibility_descriptor *epic = &ep->epic_descriptor;
508 struct edge_compatibility_bits *bits;
511 result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
512 USB_REQUEST_ION_GET_EPIC_DESC,
514 &ep->epic_descriptor,
515 sizeof(struct edge_compatibility_descriptor),
518 dbg("%s result = %d", __func__, result);
522 memset(product_info, 0, sizeof(struct edgeport_product_info));
524 product_info->NumPorts = epic->NumPorts;
525 product_info->ProdInfoVer = 0;
526 product_info->FirmwareMajorVersion = epic->MajorVersion;
527 product_info->FirmwareMinorVersion = epic->MinorVersion;
528 product_info->FirmwareBuildNumber = epic->BuildNumber;
529 product_info->iDownloadFile = epic->iDownloadFile;
530 product_info->EpicVer = epic->EpicVer;
531 product_info->Epic = epic->Supports;
532 product_info->ProductId = ION_DEVICE_ID_EDGEPORT_COMPATIBLE;
533 dump_product_info(product_info);
535 bits = &ep->epic_descriptor.Supports;
536 dbg("**EPIC descriptor:");
537 dbg(" VendEnableSuspend: %s", bits->VendEnableSuspend ? "TRUE": "FALSE");
538 dbg(" IOSPOpen : %s", bits->IOSPOpen ? "TRUE": "FALSE" );
539 dbg(" IOSPClose : %s", bits->IOSPClose ? "TRUE": "FALSE" );
540 dbg(" IOSPChase : %s", bits->IOSPChase ? "TRUE": "FALSE" );
541 dbg(" IOSPSetRxFlow : %s", bits->IOSPSetRxFlow ? "TRUE": "FALSE" );
542 dbg(" IOSPSetTxFlow : %s", bits->IOSPSetTxFlow ? "TRUE": "FALSE" );
543 dbg(" IOSPSetXChar : %s", bits->IOSPSetXChar ? "TRUE": "FALSE" );
544 dbg(" IOSPRxCheck : %s", bits->IOSPRxCheck ? "TRUE": "FALSE" );
545 dbg(" IOSPSetClrBreak : %s", bits->IOSPSetClrBreak ? "TRUE": "FALSE" );
546 dbg(" IOSPWriteMCR : %s", bits->IOSPWriteMCR ? "TRUE": "FALSE" );
547 dbg(" IOSPWriteLCR : %s", bits->IOSPWriteLCR ? "TRUE": "FALSE" );
548 dbg(" IOSPSetBaudRate : %s", bits->IOSPSetBaudRate ? "TRUE": "FALSE" );
549 dbg(" TrueEdgeport : %s", bits->TrueEdgeport ? "TRUE": "FALSE" );
556 /************************************************************************/
557 /************************************************************************/
558 /* U S B C A L L B A C K F U N C T I O N S */
559 /* U S B C A L L B A C K F U N C T I O N S */
560 /************************************************************************/
561 /************************************************************************/
563 /*****************************************************************************
564 * edge_interrupt_callback
565 * this is the callback function for when we have received data on the
566 * interrupt endpoint.
567 *****************************************************************************/
568 static void edge_interrupt_callback (struct urb *urb)
570 struct edgeport_serial *edge_serial = urb->context;
571 struct edgeport_port *edge_port;
572 struct usb_serial_port *port;
573 unsigned char *data = urb->transfer_buffer;
574 int length = urb->actual_length;
580 int status = urb->status;
591 /* this urb is terminated, clean up */
592 dbg("%s - urb shutting down with status: %d",
596 dbg("%s - nonzero urb status received: %d",
601 // process this interrupt-read even if there are no ports open
603 usb_serial_debug_data(debug, &edge_serial->serial->dev->dev, __func__, length, data);
606 bytes_avail = data[0] | (data[1] << 8);
608 spin_lock(&edge_serial->es_lock);
609 edge_serial->rxBytesAvail += bytes_avail;
610 dbg("%s - bytes_avail=%d, rxBytesAvail=%d, read_in_progress=%d", __func__, bytes_avail, edge_serial->rxBytesAvail, edge_serial->read_in_progress);
612 if (edge_serial->rxBytesAvail > 0 &&
613 !edge_serial->read_in_progress) {
614 dbg("%s - posting a read", __func__);
615 edge_serial->read_in_progress = true;
617 /* we have pending bytes on the bulk in pipe, send a request */
618 edge_serial->read_urb->dev = edge_serial->serial->dev;
619 result = usb_submit_urb(edge_serial->read_urb, GFP_ATOMIC);
621 dev_err(&edge_serial->serial->dev->dev, "%s - usb_submit_urb(read bulk) failed with result = %d\n", __func__, result);
622 edge_serial->read_in_progress = false;
625 spin_unlock(&edge_serial->es_lock);
628 /* grab the txcredits for the ports if available */
631 while ((position < length) && (portNumber < edge_serial->serial->num_ports)) {
632 txCredits = data[position] | (data[position+1] << 8);
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 dbg("%s - txcredits for port%d = %d", __func__, portNumber, edge_port->txCredits);
642 /* tell the tty driver that something has changed */
643 if (edge_port->port->port.tty)
644 tty_wakeup(edge_port->port->port.tty);
646 // Since we have more credit, check if more data can be sent
647 send_more_port_data(edge_serial, edge_port);
656 result = usb_submit_urb (urb, GFP_ATOMIC);
658 dev_err(&urb->dev->dev, "%s - Error %d submitting control urb\n", __func__, result);
663 /*****************************************************************************
664 * edge_bulk_in_callback
665 * this is the callback function for when we have received data on the
667 *****************************************************************************/
668 static void edge_bulk_in_callback (struct urb *urb)
670 struct edgeport_serial *edge_serial = urb->context;
671 unsigned char *data = urb->transfer_buffer;
673 __u16 raw_data_length;
674 int status = urb->status;
679 dbg("%s - nonzero read bulk status received: %d",
681 edge_serial->read_in_progress = false;
685 if (urb->actual_length == 0) {
686 dbg("%s - read bulk callback with no data", __func__);
687 edge_serial->read_in_progress = false;
691 raw_data_length = urb->actual_length;
693 usb_serial_debug_data(debug, &edge_serial->serial->dev->dev, __func__, raw_data_length, data);
695 spin_lock(&edge_serial->es_lock);
697 /* decrement our rxBytes available by the number that we just got */
698 edge_serial->rxBytesAvail -= raw_data_length;
700 dbg("%s - Received = %d, rxBytesAvail %d", __func__, raw_data_length, edge_serial->rxBytesAvail);
702 process_rcvd_data (edge_serial, data, urb->actual_length);
704 /* check to see if there's any more data for us to read */
705 if (edge_serial->rxBytesAvail > 0) {
706 dbg("%s - posting a read", __func__);
707 edge_serial->read_urb->dev = edge_serial->serial->dev;
708 retval = usb_submit_urb(edge_serial->read_urb, GFP_ATOMIC);
710 dev_err(&urb->dev->dev,
711 "%s - usb_submit_urb(read bulk) failed, "
712 "retval = %d\n", __func__, retval);
713 edge_serial->read_in_progress = false;
716 edge_serial->read_in_progress = false;
719 spin_unlock(&edge_serial->es_lock);
723 /*****************************************************************************
724 * edge_bulk_out_data_callback
725 * this is the callback function for when we have finished sending serial data
726 * on the bulk out endpoint.
727 *****************************************************************************/
728 static void edge_bulk_out_data_callback (struct urb *urb)
730 struct edgeport_port *edge_port = urb->context;
731 struct tty_struct *tty;
732 int status = urb->status;
737 dbg("%s - nonzero write bulk status received: %d",
741 tty = edge_port->port->port.tty;
743 if (tty && edge_port->open) {
744 /* let the tty driver wakeup if it has a special write_wakeup function */
748 // Release the Write URB
749 edge_port->write_in_progress = false;
751 // Check if more data needs to be sent
752 send_more_port_data((struct edgeport_serial *)(usb_get_serial_data(edge_port->port->serial)), edge_port);
756 /*****************************************************************************
758 * this is the callback function for when we have finished sending a command
759 * on the bulk out endpoint.
760 *****************************************************************************/
761 static void edge_bulk_out_cmd_callback (struct urb *urb)
763 struct edgeport_port *edge_port = urb->context;
764 struct tty_struct *tty;
765 int status = urb->status;
769 atomic_dec(&CmdUrbs);
770 dbg("%s - FREE URB %p (outstanding %d)", __func__, urb, atomic_read(&CmdUrbs));
773 /* clean up the transfer buffer */
774 kfree(urb->transfer_buffer);
776 /* Free the command urb */
780 dbg("%s - nonzero write bulk status received: %d", __func__, status);
784 /* Get pointer to tty */
785 tty = edge_port->port->port.tty;
787 /* tell the tty driver that something has changed */
788 if (tty && edge_port->open)
791 /* we have completed the command */
792 edge_port->commandPending = false;
793 wake_up(&edge_port->wait_command);
797 /*****************************************************************************
798 * Driver tty interface functions
799 *****************************************************************************/
801 /*****************************************************************************
803 * this function is called by the tty driver when a port is opened
804 * If successful, we return 0
805 * Otherwise we return a negative error number.
806 *****************************************************************************/
807 static int edge_open(struct tty_struct *tty,
808 struct usb_serial_port *port, struct file * filp)
810 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
811 struct usb_serial *serial;
812 struct edgeport_serial *edge_serial;
815 dbg("%s - port %d", __func__, port->number);
817 if (edge_port == NULL)
821 tty->low_latency = low_latency;
823 /* see if we've set up our endpoint info yet (can't set it up in edge_startup
824 as the structures were not set up at that time.) */
825 serial = port->serial;
826 edge_serial = usb_get_serial_data(serial);
827 if (edge_serial == NULL)
829 if (edge_serial->interrupt_in_buffer == NULL) {
830 struct usb_serial_port *port0 = serial->port[0];
832 /* not set up yet, so do it now */
833 edge_serial->interrupt_in_buffer = port0->interrupt_in_buffer;
834 edge_serial->interrupt_in_endpoint = port0->interrupt_in_endpointAddress;
835 edge_serial->interrupt_read_urb = port0->interrupt_in_urb;
836 edge_serial->bulk_in_buffer = port0->bulk_in_buffer;
837 edge_serial->bulk_in_endpoint = port0->bulk_in_endpointAddress;
838 edge_serial->read_urb = port0->read_urb;
839 edge_serial->bulk_out_endpoint = port0->bulk_out_endpointAddress;
841 /* set up our interrupt urb */
842 usb_fill_int_urb(edge_serial->interrupt_read_urb,
844 usb_rcvintpipe(serial->dev,
845 port0->interrupt_in_endpointAddress),
846 port0->interrupt_in_buffer,
847 edge_serial->interrupt_read_urb->transfer_buffer_length,
848 edge_interrupt_callback, edge_serial,
849 edge_serial->interrupt_read_urb->interval);
851 /* set up our bulk in urb */
852 usb_fill_bulk_urb(edge_serial->read_urb, serial->dev,
853 usb_rcvbulkpipe(serial->dev,
854 port0->bulk_in_endpointAddress),
855 port0->bulk_in_buffer,
856 edge_serial->read_urb->transfer_buffer_length,
857 edge_bulk_in_callback, edge_serial);
858 edge_serial->read_in_progress = false;
860 /* start interrupt read for this edgeport
861 * this interrupt will continue as long as the edgeport is connected */
862 response = usb_submit_urb (edge_serial->interrupt_read_urb, GFP_KERNEL);
864 dev_err(&port->dev, "%s - Error %d submitting control urb\n", __func__, response);
868 /* initialize our wait queues */
869 init_waitqueue_head(&edge_port->wait_open);
870 init_waitqueue_head(&edge_port->wait_chase);
871 init_waitqueue_head(&edge_port->delta_msr_wait);
872 init_waitqueue_head(&edge_port->wait_command);
874 /* initialize our icount structure */
875 memset (&(edge_port->icount), 0x00, sizeof(edge_port->icount));
877 /* initialize our port settings */
878 edge_port->txCredits = 0; /* Can't send any data yet */
879 edge_port->shadowMCR = MCR_MASTER_IE; /* Must always set this bit to enable ints! */
880 edge_port->chaseResponsePending = false;
882 /* send a open port command */
883 edge_port->openPending = true;
884 edge_port->open = false;
885 response = send_iosp_ext_cmd (edge_port, IOSP_CMD_OPEN_PORT, 0);
888 dev_err(&port->dev, "%s - error sending open port command\n", __func__);
889 edge_port->openPending = false;
893 /* now wait for the port to be completely opened */
894 wait_event_timeout(edge_port->wait_open, !edge_port->openPending, OPEN_TIMEOUT);
896 if (!edge_port->open) {
898 dbg("%s - open timedout", __func__);
899 edge_port->openPending = false;
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);
910 if (!edge_port->txfifo.fifo) {
911 dbg("%s - no memory", __func__);
912 edge_close (tty, port, filp);
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;
920 if (!edge_port->write_urb) {
921 dbg("%s - no memory", __func__);
922 edge_close (tty, port, filp);
926 dbg("%s(%d) - Initialize TX fifo to %d bytes", __func__, port->number, edge_port->maxTxCredits);
928 dbg("%s exited", __func__);
934 /************************************************************************
936 * block_until_chase_response
938 * This function will block the close until one of the following:
939 * 1. Response to our Chase comes from Edgeport
940 * 2. A timeout of 10 seconds without activity has expired
941 * (1K of Edgeport data @ 2400 baud ==> 4 sec to empty)
943 ************************************************************************/
944 static void block_until_chase_response(struct edgeport_port *edge_port)
953 lastCredits = edge_port->txCredits;
955 // Did we get our Chase response
956 if (!edge_port->chaseResponsePending) {
957 dbg("%s - Got Chase Response", __func__);
959 // did we get all of our credit back?
960 if (edge_port->txCredits == edge_port->maxTxCredits ) {
961 dbg("%s - Got all credits", __func__);
966 // Block the thread for a while
967 prepare_to_wait(&edge_port->wait_chase, &wait, TASK_UNINTERRUPTIBLE);
968 schedule_timeout(timeout);
969 finish_wait(&edge_port->wait_chase, &wait);
971 if (lastCredits == edge_port->txCredits) {
972 // No activity.. count down.
975 edge_port->chaseResponsePending = false;
976 dbg("%s - Chase TIMEOUT", __func__);
980 // Reset timeout value back to 10 seconds
981 dbg("%s - Last %d, Current %d", __func__, lastCredits, edge_port->txCredits);
988 /************************************************************************
990 * block_until_tx_empty
992 * This function will block the close until one of the following:
994 * 2. The edgeport has stopped
995 * 3. A timeout of 3 seconds without activity has expired
997 ************************************************************************/
998 static void block_until_tx_empty (struct edgeport_port *edge_port)
1001 struct TxFifo *fifo = &edge_port->txfifo;
1003 int timeout = HZ/10;
1008 lastCount = fifo->count;
1010 // Is the Edgeport Buffer empty?
1011 if (lastCount == 0) {
1012 dbg("%s - TX Buffer Empty", __func__);
1016 // Block the thread for a while
1017 prepare_to_wait (&edge_port->wait_chase, &wait, TASK_UNINTERRUPTIBLE);
1018 schedule_timeout(timeout);
1019 finish_wait(&edge_port->wait_chase, &wait);
1021 dbg("%s wait", __func__);
1023 if (lastCount == fifo->count) {
1024 // No activity.. count down.
1027 dbg("%s - TIMEOUT", __func__);
1031 // Reset timeout value back to seconds
1038 /*****************************************************************************
1040 * this function is called by the tty driver when a port is closed
1041 *****************************************************************************/
1042 static void edge_close(struct tty_struct *tty,
1043 struct usb_serial_port *port, struct file * filp)
1045 struct edgeport_serial *edge_serial;
1046 struct edgeport_port *edge_port;
1049 dbg("%s - port %d", __func__, port->number);
1051 edge_serial = usb_get_serial_data(port->serial);
1052 edge_port = usb_get_serial_port_data(port);
1053 if ((edge_serial == NULL) || (edge_port == NULL))
1056 // block until tx is empty
1057 block_until_tx_empty(edge_port);
1059 edge_port->closePending = true;
1061 if ((!edge_serial->is_epic) ||
1062 ((edge_serial->is_epic) &&
1063 (edge_serial->epic_descriptor.Supports.IOSPChase))) {
1064 /* flush and chase */
1065 edge_port->chaseResponsePending = true;
1067 dbg("%s - Sending IOSP_CMD_CHASE_PORT", __func__);
1068 status = send_iosp_ext_cmd (edge_port, IOSP_CMD_CHASE_PORT, 0);
1070 // block until chase finished
1071 block_until_chase_response(edge_port);
1073 edge_port->chaseResponsePending = false;
1077 if ((!edge_serial->is_epic) ||
1078 ((edge_serial->is_epic) &&
1079 (edge_serial->epic_descriptor.Supports.IOSPClose))) {
1080 /* close the port */
1081 dbg("%s - Sending IOSP_CMD_CLOSE_PORT", __func__);
1082 send_iosp_ext_cmd (edge_port, IOSP_CMD_CLOSE_PORT, 0);
1085 //port->close = true;
1086 edge_port->closePending = false;
1087 edge_port->open = false;
1088 edge_port->openPending = false;
1090 usb_kill_urb(edge_port->write_urb);
1092 if (edge_port->write_urb) {
1093 /* if this urb had a transfer buffer already (old transfer) free it */
1094 kfree(edge_port->write_urb->transfer_buffer);
1095 usb_free_urb(edge_port->write_urb);
1096 edge_port->write_urb = NULL;
1098 kfree(edge_port->txfifo.fifo);
1099 edge_port->txfifo.fifo = NULL;
1101 dbg("%s exited", __func__);
1104 /*****************************************************************************
1106 * this function is called by the tty driver when data should be written to
1108 * If successful, we return the number of bytes written, otherwise we return
1109 * a negative error number.
1110 *****************************************************************************/
1111 static int edge_write(struct tty_struct *tty, struct usb_serial_port *port,
1112 const unsigned char *data, int count)
1114 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1115 struct TxFifo *fifo;
1120 unsigned long flags;
1122 dbg("%s - port %d", __func__, port->number);
1124 if (edge_port == NULL)
1127 // get a pointer to the Tx fifo
1128 fifo = &edge_port->txfifo;
1130 spin_lock_irqsave(&edge_port->ep_lock, flags);
1132 // calculate number of bytes to put in fifo
1133 copySize = min ((unsigned int)count, (edge_port->txCredits - fifo->count));
1135 dbg("%s(%d) of %d byte(s) Fifo room %d -- will copy %d bytes", __func__,
1136 port->number, count, edge_port->txCredits - fifo->count, copySize);
1138 /* catch writes of 0 bytes which the tty driver likes to give us, and when txCredits is empty */
1139 if (copySize == 0) {
1140 dbg("%s - copySize = Zero", __func__);
1145 // since we can never overflow the buffer we do not have to check for full condition
1147 // the copy is done is two parts -- first fill to the end of the buffer
1148 // then copy the reset from the start of the buffer
1150 bytesleft = fifo->size - fifo->head;
1151 firsthalf = min (bytesleft, copySize);
1152 dbg("%s - copy %d bytes of %d into fifo ", __func__, firsthalf, bytesleft);
1154 /* now copy our data */
1155 memcpy(&fifo->fifo[fifo->head], data, firsthalf);
1156 usb_serial_debug_data(debug, &port->dev, __func__, firsthalf, &fifo->fifo[fifo->head]);
1158 // update the index and size
1159 fifo->head += firsthalf;
1160 fifo->count += firsthalf;
1163 if (fifo->head == fifo->size) {
1167 secondhalf = copySize-firsthalf;
1170 dbg("%s - copy rest of data %d", __func__, secondhalf);
1171 memcpy(&fifo->fifo[fifo->head], &data[firsthalf], secondhalf);
1172 usb_serial_debug_data(debug, &port->dev, __func__, secondhalf, &fifo->fifo[fifo->head]);
1173 // update the index and size
1174 fifo->count += secondhalf;
1175 fifo->head += secondhalf;
1176 // No need to check for wrap since we can not get to end of fifo in this part
1180 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1182 send_more_port_data((struct edgeport_serial *)usb_get_serial_data(port->serial), edge_port);
1184 dbg("%s wrote %d byte(s) TxCredits %d, Fifo %d", __func__, copySize, edge_port->txCredits, fifo->count);
1190 /************************************************************************
1192 * send_more_port_data()
1194 * This routine attempts to write additional UART transmit data
1195 * to a port over the USB bulk pipe. It is called (1) when new
1196 * data has been written to a port's TxBuffer from higher layers
1197 * (2) when the peripheral sends us additional TxCredits indicating
1198 * that it can accept more Tx data for a given port; and (3) when
1199 * a bulk write completes successfully and we want to see if we
1200 * can transmit more.
1202 ************************************************************************/
1203 static void send_more_port_data(struct edgeport_serial *edge_serial, struct edgeport_port *edge_port)
1205 struct TxFifo *fifo = &edge_port->txfifo;
1207 unsigned char *buffer;
1213 unsigned long flags;
1215 dbg("%s(%d)", __func__, edge_port->port->number);
1217 spin_lock_irqsave(&edge_port->ep_lock, flags);
1219 if (edge_port->write_in_progress ||
1221 (fifo->count == 0)) {
1222 dbg("%s(%d) EXIT - fifo %d, PendingWrite = %d", __func__, edge_port->port->number, fifo->count, edge_port->write_in_progress);
1226 // since the amount of data in the fifo will always fit into the
1227 // edgeport buffer we do not need to check the write length
1229 // Do we have enough credits for this port to make it worthwhile
1230 // to bother queueing a write. If it's too small, say a few bytes,
1231 // it's better to wait for more credits so we can do a larger
1233 if (edge_port->txCredits < EDGE_FW_GET_TX_CREDITS_SEND_THRESHOLD(edge_port->maxTxCredits,EDGE_FW_BULK_MAX_PACKET_SIZE)) {
1234 dbg("%s(%d) Not enough credit - fifo %d TxCredit %d", __func__, edge_port->port->number, fifo->count, edge_port->txCredits );
1239 edge_port->write_in_progress = true;
1241 // get a pointer to the write_urb
1242 urb = edge_port->write_urb;
1244 /* make sure transfer buffer is freed */
1245 kfree(urb->transfer_buffer);
1246 urb->transfer_buffer = NULL;
1248 /* build the data header for the buffer and port that we are about to send out */
1249 count = fifo->count;
1250 buffer = kmalloc (count+2, GFP_ATOMIC);
1251 if (buffer == NULL) {
1252 dev_err(&edge_port->port->dev, "%s - no more kernel memory...\n", __func__);
1253 edge_port->write_in_progress = false;
1256 buffer[0] = IOSP_BUILD_DATA_HDR1 (edge_port->port->number - edge_port->port->serial->minor, count);
1257 buffer[1] = IOSP_BUILD_DATA_HDR2 (edge_port->port->number - edge_port->port->serial->minor, count);
1259 /* now copy our data */
1260 bytesleft = fifo->size - fifo->tail;
1261 firsthalf = min (bytesleft, count);
1262 memcpy(&buffer[2], &fifo->fifo[fifo->tail], firsthalf);
1263 fifo->tail += firsthalf;
1264 fifo->count -= firsthalf;
1265 if (fifo->tail == fifo->size) {
1269 secondhalf = count-firsthalf;
1271 memcpy(&buffer[2+firsthalf], &fifo->fifo[fifo->tail], secondhalf);
1272 fifo->tail += secondhalf;
1273 fifo->count -= secondhalf;
1277 usb_serial_debug_data(debug, &edge_port->port->dev, __func__, count, &buffer[2]);
1279 /* fill up the urb with all of our data and submit it */
1280 usb_fill_bulk_urb (urb, edge_serial->serial->dev,
1281 usb_sndbulkpipe(edge_serial->serial->dev, edge_serial->bulk_out_endpoint),
1282 buffer, count+2, edge_bulk_out_data_callback, edge_port);
1284 /* decrement the number of credits we have by the number we just sent */
1285 edge_port->txCredits -= count;
1286 edge_port->icount.tx += count;
1288 urb->dev = edge_serial->serial->dev;
1289 status = usb_submit_urb(urb, GFP_ATOMIC);
1291 /* something went wrong */
1292 dev_err(&edge_port->port->dev, "%s - usb_submit_urb(write bulk) failed, status = %d, data lost\n", __func__, status);
1293 edge_port->write_in_progress = false;
1295 /* revert the credits as something bad happened. */
1296 edge_port->txCredits += count;
1297 edge_port->icount.tx -= count;
1299 dbg("%s wrote %d byte(s) TxCredit %d, Fifo %d", __func__, count, edge_port->txCredits, fifo->count);
1302 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1306 /*****************************************************************************
1308 * this function is called by the tty driver when it wants to know how many
1309 * bytes of data we can accept for a specific port.
1310 * If successful, we return the amount of room that we have for this port
1312 * Otherwise we return a negative error number.
1313 *****************************************************************************/
1314 static int edge_write_room(struct tty_struct *tty)
1316 struct usb_serial_port *port = tty->driver_data;
1317 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1319 unsigned long flags;
1321 dbg("%s", __func__);
1323 if (edge_port == NULL)
1325 if (edge_port->closePending)
1328 dbg("%s - port %d", __func__, port->number);
1330 if (!edge_port->open) {
1331 dbg("%s - port not opened", __func__);
1335 // total of both buffers is still txCredit
1336 spin_lock_irqsave(&edge_port->ep_lock, flags);
1337 room = edge_port->txCredits - edge_port->txfifo.count;
1338 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1340 dbg("%s - returns %d", __func__, room);
1345 /*****************************************************************************
1346 * edge_chars_in_buffer
1347 * this function is called by the tty driver when it wants to know how many
1348 * bytes of data we currently have outstanding in the port (data that has
1349 * been written, but hasn't made it out the port yet)
1350 * If successful, we return the number of bytes left to be written in the
1352 * Otherwise we return a negative error number.
1353 *****************************************************************************/
1354 static int edge_chars_in_buffer(struct tty_struct *tty)
1356 struct usb_serial_port *port = tty->driver_data;
1357 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1359 unsigned long flags;
1361 dbg("%s", __func__);
1363 if (edge_port == NULL)
1365 if (edge_port->closePending)
1368 if (!edge_port->open) {
1369 dbg("%s - port not opened", __func__);
1373 spin_lock_irqsave(&edge_port->ep_lock, flags);
1374 num_chars = edge_port->maxTxCredits - edge_port->txCredits + edge_port->txfifo.count;
1375 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1377 dbg("%s(port %d) - returns %d", __func__, port->number, num_chars);
1384 /*****************************************************************************
1386 * this function is called by the tty driver when it wants to stop the data
1387 * being read from the port.
1388 *****************************************************************************/
1389 static void edge_throttle(struct tty_struct *tty)
1391 struct usb_serial_port *port = tty->driver_data;
1392 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1395 dbg("%s - port %d", __func__, port->number);
1397 if (edge_port == NULL)
1400 if (!edge_port->open) {
1401 dbg("%s - port not opened", __func__);
1405 /* if we are implementing XON/XOFF, send the stop character */
1407 unsigned char stop_char = STOP_CHAR(tty);
1408 status = edge_write (tty, port, &stop_char, 1);
1414 /* if we are implementing RTS/CTS, toggle that line */
1415 if (tty->termios->c_cflag & CRTSCTS) {
1416 edge_port->shadowMCR &= ~MCR_RTS;
1417 status = send_cmd_write_uart_register(edge_port, MCR, edge_port->shadowMCR);
1427 /*****************************************************************************
1429 * this function is called by the tty driver when it wants to resume the data
1430 * being read from the port (called after SerialThrottle is called)
1431 *****************************************************************************/
1432 static void edge_unthrottle(struct tty_struct *tty)
1434 struct usb_serial_port *port = tty->driver_data;
1435 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1438 dbg("%s - port %d", __func__, port->number);
1440 if (edge_port == NULL)
1443 if (!edge_port->open) {
1444 dbg("%s - port not opened", __func__);
1448 /* if we are implementing XON/XOFF, send the start character */
1450 unsigned char start_char = START_CHAR(tty);
1451 status = edge_write(tty, port, &start_char, 1);
1455 /* if we are implementing RTS/CTS, toggle that line */
1456 if (tty->termios->c_cflag & CRTSCTS) {
1457 edge_port->shadowMCR |= MCR_RTS;
1458 send_cmd_write_uart_register(edge_port, MCR, edge_port->shadowMCR);
1463 /*****************************************************************************
1465 * this function is called by the tty driver when it wants to change the termios structure
1466 *****************************************************************************/
1467 static void edge_set_termios(struct tty_struct *tty,
1468 struct usb_serial_port *port, struct ktermios *old_termios)
1470 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1473 cflag = tty->termios->c_cflag;
1474 dbg("%s - clfag %08x iflag %08x", __func__,
1475 tty->termios->c_cflag, tty->termios->c_iflag);
1476 dbg("%s - old clfag %08x old iflag %08x", __func__,
1477 old_termios->c_cflag, old_termios->c_iflag);
1479 dbg("%s - port %d", __func__, port->number);
1481 if (edge_port == NULL)
1484 if (!edge_port->open) {
1485 dbg("%s - port not opened", __func__);
1489 /* change the port settings to the new ones specified */
1490 change_port_settings(tty, edge_port, old_termios);
1494 /*****************************************************************************
1495 * get_lsr_info - get line status register info
1497 * Purpose: Let user call ioctl() to get info when the UART physically
1498 * is emptied. On bus types like RS485, the transmitter must
1499 * release the bus after transmitting. This must be done when
1500 * the transmit shift register is empty, not be done when the
1501 * transmit holding register is empty. This functionality
1502 * allows an RS485 driver to be written in user space.
1503 *****************************************************************************/
1504 static int get_lsr_info(struct edgeport_port *edge_port, unsigned int __user *value)
1506 unsigned int result = 0;
1507 unsigned long flags;
1509 spin_lock_irqsave(&edge_port->ep_lock, flags);
1510 if (edge_port->maxTxCredits == edge_port->txCredits &&
1511 edge_port->txfifo.count == 0) {
1512 dbg("%s -- Empty", __func__);
1513 result = TIOCSER_TEMT;
1515 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1517 if (copy_to_user(value, &result, sizeof(int)))
1522 static int edge_tiocmset(struct tty_struct *tty, struct file *file, unsigned int set, unsigned int clear)
1524 struct usb_serial_port *port = tty->driver_data;
1525 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1528 dbg("%s - port %d", __func__, port->number);
1530 mcr = edge_port->shadowMCR;
1531 if (set & TIOCM_RTS)
1533 if (set & TIOCM_DTR)
1535 if (set & TIOCM_LOOP)
1536 mcr |= MCR_LOOPBACK;
1538 if (clear & TIOCM_RTS)
1540 if (clear & TIOCM_DTR)
1542 if (clear & TIOCM_LOOP)
1543 mcr &= ~MCR_LOOPBACK;
1545 edge_port->shadowMCR = mcr;
1547 send_cmd_write_uart_register(edge_port, MCR, edge_port->shadowMCR);
1552 static int edge_tiocmget(struct tty_struct *tty, struct file *file)
1554 struct usb_serial_port *port = tty->driver_data;
1555 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1556 unsigned int result = 0;
1560 dbg("%s - port %d", __func__, port->number);
1562 msr = edge_port->shadowMSR;
1563 mcr = edge_port->shadowMCR;
1564 result = ((mcr & MCR_DTR) ? TIOCM_DTR: 0) /* 0x002 */
1565 | ((mcr & MCR_RTS) ? TIOCM_RTS: 0) /* 0x004 */
1566 | ((msr & EDGEPORT_MSR_CTS) ? TIOCM_CTS: 0) /* 0x020 */
1567 | ((msr & EDGEPORT_MSR_CD) ? TIOCM_CAR: 0) /* 0x040 */
1568 | ((msr & EDGEPORT_MSR_RI) ? TIOCM_RI: 0) /* 0x080 */
1569 | ((msr & EDGEPORT_MSR_DSR) ? TIOCM_DSR: 0); /* 0x100 */
1572 dbg("%s -- %x", __func__, result);
1577 static int get_serial_info(struct edgeport_port *edge_port, struct serial_struct __user *retinfo)
1579 struct serial_struct tmp;
1584 memset(&tmp, 0, sizeof(tmp));
1586 tmp.type = PORT_16550A;
1587 tmp.line = edge_port->port->serial->minor;
1588 tmp.port = edge_port->port->number;
1590 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
1591 tmp.xmit_fifo_size = edge_port->maxTxCredits;
1592 tmp.baud_base = 9600;
1593 tmp.close_delay = 5*HZ;
1594 tmp.closing_wait = 30*HZ;
1596 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1603 /*****************************************************************************
1605 * this function handles any ioctl calls to the driver
1606 *****************************************************************************/
1607 static int edge_ioctl(struct tty_struct *tty, struct file *file,
1608 unsigned int cmd, unsigned long arg)
1610 struct usb_serial_port *port = tty->driver_data;
1612 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1613 struct async_icount cnow;
1614 struct async_icount cprev;
1615 struct serial_icounter_struct icount;
1617 dbg("%s - port %d, cmd = 0x%x", __func__, port->number, cmd);
1621 dbg("%s (%d) TIOCSERGETLSR", __func__, port->number);
1622 return get_lsr_info(edge_port, (unsigned int __user *) arg);
1625 dbg("%s (%d) TIOCGSERIAL", __func__, port->number);
1626 return get_serial_info(edge_port, (struct serial_struct __user *) arg);
1629 dbg("%s (%d) TIOCMIWAIT", __func__, port->number);
1630 cprev = edge_port->icount;
1632 prepare_to_wait(&edge_port->delta_msr_wait, &wait, TASK_INTERRUPTIBLE);
1634 finish_wait(&edge_port->delta_msr_wait, &wait);
1635 /* see if a signal did it */
1636 if (signal_pending(current))
1637 return -ERESTARTSYS;
1638 cnow = edge_port->icount;
1639 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
1640 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
1641 return -EIO; /* no change => error */
1642 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1643 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1644 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1645 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)) ) {
1654 cnow = edge_port->icount;
1655 memset(&icount, 0, sizeof(icount));
1656 icount.cts = cnow.cts;
1657 icount.dsr = cnow.dsr;
1658 icount.rng = cnow.rng;
1659 icount.dcd = cnow.dcd;
1660 icount.rx = cnow.rx;
1661 icount.tx = cnow.tx;
1662 icount.frame = cnow.frame;
1663 icount.overrun = cnow.overrun;
1664 icount.parity = cnow.parity;
1665 icount.brk = cnow.brk;
1666 icount.buf_overrun = cnow.buf_overrun;
1668 dbg("%s (%d) TIOCGICOUNT RX=%d, TX=%d", __func__, port->number, icount.rx, icount.tx );
1669 if (copy_to_user((void __user *)arg, &icount, sizeof(icount)))
1674 return -ENOIOCTLCMD;
1678 /*****************************************************************************
1680 * this function sends a break to the port
1681 *****************************************************************************/
1682 static void edge_break (struct tty_struct *tty, int break_state)
1684 struct usb_serial_port *port = tty->driver_data;
1685 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1686 struct edgeport_serial *edge_serial = usb_get_serial_data(port->serial);
1689 if ((!edge_serial->is_epic) ||
1690 ((edge_serial->is_epic) &&
1691 (edge_serial->epic_descriptor.Supports.IOSPChase))) {
1692 /* flush and chase */
1693 edge_port->chaseResponsePending = true;
1695 dbg("%s - Sending IOSP_CMD_CHASE_PORT", __func__);
1696 status = send_iosp_ext_cmd (edge_port, IOSP_CMD_CHASE_PORT, 0);
1698 // block until chase finished
1699 block_until_chase_response(edge_port);
1701 edge_port->chaseResponsePending = false;
1705 if ((!edge_serial->is_epic) ||
1706 ((edge_serial->is_epic) &&
1707 (edge_serial->epic_descriptor.Supports.IOSPSetClrBreak))) {
1708 if (break_state == -1) {
1709 dbg("%s - Sending IOSP_CMD_SET_BREAK", __func__);
1710 status = send_iosp_ext_cmd (edge_port, IOSP_CMD_SET_BREAK, 0);
1712 dbg("%s - Sending IOSP_CMD_CLEAR_BREAK", __func__);
1713 status = send_iosp_ext_cmd (edge_port, IOSP_CMD_CLEAR_BREAK, 0);
1716 dbg("%s - error sending break set/clear command.", __func__);
1724 /*****************************************************************************
1726 * this function handles the data received on the bulk in pipe.
1727 *****************************************************************************/
1728 static void process_rcvd_data (struct edgeport_serial *edge_serial, unsigned char * buffer, __u16 bufferLength)
1730 struct usb_serial_port *port;
1731 struct edgeport_port *edge_port;
1732 struct tty_struct *tty;
1733 __u16 lastBufferLength;
1736 dbg("%s", __func__);
1738 lastBufferLength = bufferLength + 1;
1740 while (bufferLength > 0) {
1741 /* failsafe incase we get a message that we don't understand */
1742 if (lastBufferLength == bufferLength) {
1743 dbg("%s - stuck in loop, exiting it.", __func__);
1746 lastBufferLength = bufferLength;
1748 switch (edge_serial->rxState) {
1750 edge_serial->rxHeader1 = *buffer;
1754 if (bufferLength == 0) {
1755 edge_serial->rxState = EXPECT_HDR2;
1758 /* otherwise, drop on through */
1761 edge_serial->rxHeader2 = *buffer;
1765 dbg("%s - Hdr1=%02X Hdr2=%02X", __func__, edge_serial->rxHeader1, edge_serial->rxHeader2);
1767 // Process depending on whether this header is
1770 if (IS_CMD_STAT_HDR(edge_serial->rxHeader1)) {
1771 // Decode this status header and goto EXPECT_HDR1 (if we
1772 // can process the status with only 2 bytes), or goto
1773 // EXPECT_HDR3 to get the third byte.
1775 edge_serial->rxPort = IOSP_GET_HDR_PORT(edge_serial->rxHeader1);
1776 edge_serial->rxStatusCode = IOSP_GET_STATUS_CODE(edge_serial->rxHeader1);
1778 if (!IOSP_STATUS_IS_2BYTE(edge_serial->rxStatusCode)) {
1779 // This status needs additional bytes. Save what we have
1780 // and then wait for more data.
1781 edge_serial->rxStatusParam = edge_serial->rxHeader2;
1783 edge_serial->rxState = EXPECT_HDR3;
1787 // We have all the header bytes, process the status now
1788 process_rcvd_status (edge_serial, edge_serial->rxHeader2, 0);
1789 edge_serial->rxState = EXPECT_HDR1;
1792 edge_serial->rxPort = IOSP_GET_HDR_PORT(edge_serial->rxHeader1);
1793 edge_serial->rxBytesRemaining = IOSP_GET_HDR_DATA_LEN(edge_serial->rxHeader1, edge_serial->rxHeader2);
1795 dbg("%s - Data for Port %u Len %u", __func__, edge_serial->rxPort, edge_serial->rxBytesRemaining);
1797 //ASSERT( DevExt->RxPort < DevExt->NumPorts );
1798 //ASSERT( DevExt->RxBytesRemaining < IOSP_MAX_DATA_LENGTH );
1800 if (bufferLength == 0 ) {
1801 edge_serial->rxState = EXPECT_DATA;
1804 // Else, drop through
1807 case EXPECT_DATA: // Expect data
1809 if (bufferLength < edge_serial->rxBytesRemaining) {
1810 rxLen = bufferLength;
1811 edge_serial->rxState = EXPECT_DATA; // Expect data to start next buffer
1813 // BufLen >= RxBytesRemaining
1814 rxLen = edge_serial->rxBytesRemaining;
1815 edge_serial->rxState = EXPECT_HDR1; // Start another header next time
1818 bufferLength -= rxLen;
1819 edge_serial->rxBytesRemaining -= rxLen;
1821 /* spit this data back into the tty driver if this port is open */
1823 port = edge_serial->serial->port[edge_serial->rxPort];
1824 edge_port = usb_get_serial_port_data(port);
1825 if (edge_port->open) {
1826 tty = edge_port->port->port.tty;
1828 dbg("%s - Sending %d bytes to TTY for port %d", __func__, rxLen, edge_serial->rxPort);
1829 edge_tty_recv(&edge_serial->serial->dev->dev, tty, buffer, rxLen);
1831 edge_port->icount.rx += rxLen;
1838 case EXPECT_HDR3: // Expect 3rd byte of status header
1839 edge_serial->rxHeader3 = *buffer;
1843 // We have all the header bytes, process the status now
1844 process_rcvd_status (edge_serial, edge_serial->rxStatusParam, edge_serial->rxHeader3);
1845 edge_serial->rxState = EXPECT_HDR1;
1853 /*****************************************************************************
1854 * process_rcvd_status
1855 * this function handles the any status messages received on the bulk in pipe.
1856 *****************************************************************************/
1857 static void process_rcvd_status (struct edgeport_serial *edge_serial, __u8 byte2, __u8 byte3)
1859 struct usb_serial_port *port;
1860 struct edgeport_port *edge_port;
1861 __u8 code = edge_serial->rxStatusCode;
1863 /* switch the port pointer to the one being currently talked about */
1864 port = edge_serial->serial->port[edge_serial->rxPort];
1865 edge_port = usb_get_serial_port_data(port);
1866 if (edge_port == NULL) {
1867 dev_err(&edge_serial->serial->dev->dev, "%s - edge_port == NULL for port %d\n", __func__, edge_serial->rxPort);
1871 dbg("%s - port %d", __func__, edge_serial->rxPort);
1873 if (code == IOSP_EXT_STATUS) {
1875 case IOSP_EXT_STATUS_CHASE_RSP:
1876 // we want to do EXT status regardless of port open/closed
1877 dbg("%s - Port %u EXT CHASE_RSP Data = %02x", __func__, edge_serial->rxPort, byte3 );
1878 // Currently, the only EXT_STATUS is Chase, so process here instead of one more call
1879 // to one more subroutine. If/when more EXT_STATUS, there'll be more work to do.
1880 // Also, we currently clear flag and close the port regardless of content of above's Byte3.
1881 // We could choose to do something else when Byte3 says Timeout on Chase from Edgeport,
1882 // like wait longer in block_until_chase_response, but for now we don't.
1883 edge_port->chaseResponsePending = false;
1884 wake_up (&edge_port->wait_chase);
1887 case IOSP_EXT_STATUS_RX_CHECK_RSP:
1888 dbg("%s ========== Port %u CHECK_RSP Sequence = %02x =============\n", __func__, edge_serial->rxPort, byte3 );
1889 //Port->RxCheckRsp = true;
1894 if (code == IOSP_STATUS_OPEN_RSP) {
1895 edge_port->txCredits = GET_TX_BUFFER_SIZE(byte3);
1896 edge_port->maxTxCredits = edge_port->txCredits;
1897 dbg("%s - Port %u Open Response Inital MSR = %02x TxBufferSize = %d", __func__, edge_serial->rxPort, byte2, edge_port->txCredits);
1898 handle_new_msr (edge_port, byte2);
1900 /* send the current line settings to the port so we are in sync with any further termios calls */
1901 /* FIXME: locking on tty */
1902 if (edge_port->port->port.tty)
1903 change_port_settings(edge_port->port->port.tty, edge_port, edge_port->port->port.tty->termios);
1905 /* we have completed the open */
1906 edge_port->openPending = false;
1907 edge_port->open = true;
1908 wake_up(&edge_port->wait_open);
1912 // If port is closed, silently discard all rcvd status. We can
1913 // have cases where buffered status is received AFTER the close
1914 // port command is sent to the Edgeport.
1915 if (!edge_port->open || edge_port->closePending) {
1920 // Not currently sent by Edgeport
1921 case IOSP_STATUS_LSR:
1922 dbg("%s - Port %u LSR Status = %02x", __func__, edge_serial->rxPort, byte2);
1923 handle_new_lsr(edge_port, false, byte2, 0);
1926 case IOSP_STATUS_LSR_DATA:
1927 dbg("%s - Port %u LSR Status = %02x, Data = %02x", __func__, edge_serial->rxPort, byte2, byte3);
1928 // byte2 is LSR Register
1929 // byte3 is broken data byte
1930 handle_new_lsr(edge_port, true, byte2, byte3);
1933 // case IOSP_EXT_4_STATUS:
1934 // dbg("%s - Port %u LSR Status = %02x Data = %02x", __func__, edge_serial->rxPort, byte2, byte3);
1937 case IOSP_STATUS_MSR:
1938 dbg("%s - Port %u MSR Status = %02x", __func__, edge_serial->rxPort, byte2);
1940 // Process this new modem status and generate appropriate
1941 // events, etc, based on the new status. This routine
1942 // also saves the MSR in Port->ShadowMsr.
1943 handle_new_msr(edge_port, byte2);
1947 dbg("%s - Unrecognized IOSP status code %u\n", __func__, code);
1955 /*****************************************************************************
1957 * this function passes data on to the tty flip buffer
1958 *****************************************************************************/
1959 static void edge_tty_recv(struct device *dev, struct tty_struct *tty, unsigned char *data, int length)
1964 cnt = tty_buffer_request_room(tty, length);
1966 dev_err(dev, "%s - dropping data, %d bytes lost\n",
1967 __func__, length - cnt);
1971 tty_insert_flip_string(tty, data, cnt);
1974 } while (length > 0);
1976 tty_flip_buffer_push(tty);
1980 /*****************************************************************************
1982 * this function handles any change to the msr register for a port.
1983 *****************************************************************************/
1984 static void handle_new_msr(struct edgeport_port *edge_port, __u8 newMsr)
1986 struct async_icount *icount;
1988 dbg("%s %02x", __func__, newMsr);
1990 if (newMsr & (EDGEPORT_MSR_DELTA_CTS | EDGEPORT_MSR_DELTA_DSR | EDGEPORT_MSR_DELTA_RI | EDGEPORT_MSR_DELTA_CD)) {
1991 icount = &edge_port->icount;
1993 /* update input line counters */
1994 if (newMsr & EDGEPORT_MSR_DELTA_CTS) {
1997 if (newMsr & EDGEPORT_MSR_DELTA_DSR) {
2000 if (newMsr & EDGEPORT_MSR_DELTA_CD) {
2003 if (newMsr & EDGEPORT_MSR_DELTA_RI) {
2006 wake_up_interruptible(&edge_port->delta_msr_wait);
2009 /* Save the new modem status */
2010 edge_port->shadowMSR = newMsr & 0xf0;
2016 /*****************************************************************************
2018 * this function handles any change to the lsr register for a port.
2019 *****************************************************************************/
2020 static void handle_new_lsr(struct edgeport_port *edge_port, __u8 lsrData, __u8 lsr, __u8 data)
2022 __u8 newLsr = (__u8)(lsr & (__u8)(LSR_OVER_ERR | LSR_PAR_ERR | LSR_FRM_ERR | LSR_BREAK));
2023 struct async_icount *icount;
2025 dbg("%s - %02x", __func__, newLsr);
2027 edge_port->shadowLSR = lsr;
2029 if (newLsr & LSR_BREAK) {
2031 // Parity and Framing errors only count if they
2032 // occur exclusive of a break being
2035 newLsr &= (__u8)(LSR_OVER_ERR | LSR_BREAK);
2038 /* Place LSR data byte into Rx buffer */
2039 if (lsrData && edge_port->port->port.tty)
2040 edge_tty_recv(&edge_port->port->dev, edge_port->port->port.tty, &data, 1);
2042 /* update input line counters */
2043 icount = &edge_port->icount;
2044 if (newLsr & LSR_BREAK) {
2047 if (newLsr & LSR_OVER_ERR) {
2050 if (newLsr & LSR_PAR_ERR) {
2053 if (newLsr & LSR_FRM_ERR) {
2061 /****************************************************************************
2063 * writes a number of bytes to the Edgeport device's sram starting at the
2065 * If successful returns the number of bytes written, otherwise it returns
2066 * a negative error number of the problem.
2067 ****************************************************************************/
2068 static int sram_write(struct usb_serial *serial, __u16 extAddr, __u16 addr, __u16 length, const __u8 *data)
2071 __u16 current_length;
2072 unsigned char *transfer_buffer;
2074 dbg("%s - %x, %x, %d", __func__, extAddr, addr, length);
2076 transfer_buffer = kmalloc (64, GFP_KERNEL);
2077 if (!transfer_buffer) {
2078 dev_err(&serial->dev->dev, "%s - kmalloc(%d) failed.\n", __func__, 64);
2082 /* need to split these writes up into 64 byte chunks */
2084 while (length > 0) {
2086 current_length = 64;
2088 current_length = length;
2090 // dbg("%s - writing %x, %x, %d", __func__, extAddr, addr, current_length);
2091 memcpy (transfer_buffer, data, current_length);
2092 result = usb_control_msg (serial->dev, usb_sndctrlpipe(serial->dev, 0), USB_REQUEST_ION_WRITE_RAM,
2093 0x40, addr, extAddr, transfer_buffer, current_length, 300);
2096 length -= current_length;
2097 addr += current_length;
2098 data += current_length;
2101 kfree (transfer_buffer);
2106 /****************************************************************************
2108 * writes a number of bytes to the Edgeport device's ROM starting at the
2110 * If successful returns the number of bytes written, otherwise it returns
2111 * a negative error number of the problem.
2112 ****************************************************************************/
2113 static int rom_write(struct usb_serial *serial, __u16 extAddr, __u16 addr, __u16 length, const __u8 *data)
2116 __u16 current_length;
2117 unsigned char *transfer_buffer;
2119 // dbg("%s - %x, %x, %d", __func__, extAddr, addr, length);
2121 transfer_buffer = kmalloc (64, GFP_KERNEL);
2122 if (!transfer_buffer) {
2123 dev_err(&serial->dev->dev, "%s - kmalloc(%d) failed.\n", __func__, 64);
2127 /* need to split these writes up into 64 byte chunks */
2129 while (length > 0) {
2131 current_length = 64;
2133 current_length = length;
2135 // dbg("%s - writing %x, %x, %d", __func__, extAddr, addr, current_length);
2136 memcpy (transfer_buffer, data, current_length);
2137 result = usb_control_msg (serial->dev, usb_sndctrlpipe(serial->dev, 0), USB_REQUEST_ION_WRITE_ROM,
2138 0x40, addr, extAddr, transfer_buffer, current_length, 300);
2141 length -= current_length;
2142 addr += current_length;
2143 data += current_length;
2146 kfree (transfer_buffer);
2151 /****************************************************************************
2153 * reads a number of bytes from the Edgeport device starting at the given
2155 * If successful returns the number of bytes read, otherwise it returns
2156 * a negative error number of the problem.
2157 ****************************************************************************/
2158 static int rom_read (struct usb_serial *serial, __u16 extAddr, __u16 addr, __u16 length, __u8 *data)
2161 __u16 current_length;
2162 unsigned char *transfer_buffer;
2164 dbg("%s - %x, %x, %d", __func__, extAddr, addr, length);
2166 transfer_buffer = kmalloc (64, GFP_KERNEL);
2167 if (!transfer_buffer) {
2168 dev_err(&serial->dev->dev, "%s - kmalloc(%d) failed.\n", __func__, 64);
2172 /* need to split these reads up into 64 byte chunks */
2174 while (length > 0) {
2176 current_length = 64;
2178 current_length = length;
2180 // dbg("%s - %x, %x, %d", __func__, extAddr, addr, current_length);
2181 result = usb_control_msg (serial->dev, usb_rcvctrlpipe(serial->dev, 0), USB_REQUEST_ION_READ_ROM,
2182 0xC0, addr, extAddr, transfer_buffer, current_length, 300);
2185 memcpy (data, transfer_buffer, current_length);
2186 length -= current_length;
2187 addr += current_length;
2188 data += current_length;
2191 kfree (transfer_buffer);
2196 /****************************************************************************
2198 * Is used to send a IOSP message to the Edgeport device
2199 ****************************************************************************/
2200 static int send_iosp_ext_cmd (struct edgeport_port *edge_port, __u8 command, __u8 param)
2202 unsigned char *buffer;
2203 unsigned char *currentCommand;
2207 dbg("%s - %d, %d", __func__, command, param);
2209 buffer = kmalloc (10, GFP_ATOMIC);
2211 dev_err(&edge_port->port->dev, "%s - kmalloc(%d) failed.\n", __func__, 10);
2215 currentCommand = buffer;
2217 MAKE_CMD_EXT_CMD (¤tCommand, &length,
2218 edge_port->port->number - edge_port->port->serial->minor,
2221 status = write_cmd_usb (edge_port, buffer, length);
2223 /* something bad happened, let's free up the memory */
2231 /*****************************************************************************
2233 * this function writes the given buffer out to the bulk write endpoint.
2234 *****************************************************************************/
2235 static int write_cmd_usb (struct edgeport_port *edge_port, unsigned char *buffer, int length)
2237 struct edgeport_serial *edge_serial = usb_get_serial_data(edge_port->port->serial);
2242 usb_serial_debug_data(debug, &edge_port->port->dev, __func__, length, buffer);
2244 /* Allocate our next urb */
2245 urb = usb_alloc_urb (0, GFP_ATOMIC);
2249 atomic_inc(&CmdUrbs);
2250 dbg("%s - ALLOCATE URB %p (outstanding %d)", __func__, urb, atomic_read(&CmdUrbs));
2252 usb_fill_bulk_urb (urb, edge_serial->serial->dev,
2253 usb_sndbulkpipe(edge_serial->serial->dev, edge_serial->bulk_out_endpoint),
2254 buffer, length, edge_bulk_out_cmd_callback, edge_port);
2256 edge_port->commandPending = true;
2257 status = usb_submit_urb(urb, GFP_ATOMIC);
2260 /* something went wrong */
2261 dev_err(&edge_port->port->dev, "%s - usb_submit_urb(write command) failed, status = %d\n", __func__, status);
2264 atomic_dec(&CmdUrbs);
2268 // wait for command to finish
2269 timeout = COMMAND_TIMEOUT;
2271 wait_event (&edge_port->wait_command, !edge_port->commandPending);
2273 if (edge_port->commandPending) {
2274 /* command timed out */
2275 dbg("%s - command timed out", __func__);
2283 /*****************************************************************************
2284 * send_cmd_write_baud_rate
2285 * this function sends the proper command to change the baud rate of the
2287 *****************************************************************************/
2288 static int send_cmd_write_baud_rate (struct edgeport_port *edge_port, int baudRate)
2290 struct edgeport_serial *edge_serial = usb_get_serial_data(edge_port->port->serial);
2291 unsigned char *cmdBuffer;
2292 unsigned char *currCmd;
2296 unsigned char number = edge_port->port->number - edge_port->port->serial->minor;
2298 if (edge_serial->is_epic &&
2299 !edge_serial->epic_descriptor.Supports.IOSPSetBaudRate) {
2300 dbg("SendCmdWriteBaudRate - NOT Setting baud rate for port = %d, baud = %d",
2301 edge_port->port->number, baudRate);
2305 dbg("%s - port = %d, baud = %d", __func__, edge_port->port->number, baudRate);
2307 status = calc_baud_rate_divisor (baudRate, &divisor);
2309 dev_err(&edge_port->port->dev, "%s - bad baud rate\n", __func__);
2313 // Alloc memory for the string of commands.
2314 cmdBuffer = kmalloc (0x100, GFP_ATOMIC);
2316 dev_err(&edge_port->port->dev, "%s - kmalloc(%d) failed.\n", __func__, 0x100);
2319 currCmd = cmdBuffer;
2321 // Enable access to divisor latch
2322 MAKE_CMD_WRITE_REG( &currCmd, &cmdLen, number, LCR, LCR_DL_ENABLE );
2324 // Write the divisor itself
2325 MAKE_CMD_WRITE_REG( &currCmd, &cmdLen, number, DLL, LOW8 (divisor) );
2326 MAKE_CMD_WRITE_REG( &currCmd, &cmdLen, number, DLM, HIGH8(divisor) );
2328 // Restore original value to disable access to divisor latch
2329 MAKE_CMD_WRITE_REG( &currCmd, &cmdLen, number, LCR, edge_port->shadowLCR);
2331 status = write_cmd_usb(edge_port, cmdBuffer, cmdLen );
2333 /* something bad happened, let's free up the memory */
2341 /*****************************************************************************
2342 * calc_baud_rate_divisor
2343 * this function calculates the proper baud rate divisor for the specified
2345 *****************************************************************************/
2346 static int calc_baud_rate_divisor (int baudrate, int *divisor)
2352 dbg("%s - %d", __func__, baudrate);
2354 for (i = 0; i < ARRAY_SIZE(divisor_table); i++) {
2355 if ( divisor_table[i].BaudRate == baudrate ) {
2356 *divisor = divisor_table[i].Divisor;
2361 // We have tried all of the standard baud rates
2362 // lets try to calculate the divisor for this baud rate
2363 // Make sure the baud rate is reasonable
2364 if (baudrate > 50 && baudrate < 230400) {
2366 custom = (__u16)((230400L + baudrate/2) / baudrate);
2370 dbg("%s - Baud %d = %d\n", __func__, baudrate, custom);
2378 /*****************************************************************************
2379 * send_cmd_write_uart_register
2380 * this function builds up a uart register message and sends to to the device.
2381 *****************************************************************************/
2382 static int send_cmd_write_uart_register (struct edgeport_port *edge_port, __u8 regNum, __u8 regValue)
2384 struct edgeport_serial *edge_serial = usb_get_serial_data(edge_port->port->serial);
2385 unsigned char *cmdBuffer;
2386 unsigned char *currCmd;
2387 unsigned long cmdLen = 0;
2390 dbg("%s - write to %s register 0x%02x", (regNum == MCR) ? "MCR" : "LCR", __func__, regValue);
2392 if (edge_serial->is_epic &&
2393 !edge_serial->epic_descriptor.Supports.IOSPWriteMCR &&
2395 dbg("SendCmdWriteUartReg - Not writing to MCR Register");
2399 if (edge_serial->is_epic &&
2400 !edge_serial->epic_descriptor.Supports.IOSPWriteLCR &&
2402 dbg ("SendCmdWriteUartReg - Not writing to LCR Register");
2406 // Alloc memory for the string of commands.
2407 cmdBuffer = kmalloc (0x10, GFP_ATOMIC);
2408 if (cmdBuffer == NULL ) {
2412 currCmd = cmdBuffer;
2414 // Build a cmd in the buffer to write the given register
2415 MAKE_CMD_WRITE_REG (&currCmd, &cmdLen,
2416 edge_port->port->number - edge_port->port->serial->minor,
2419 status = write_cmd_usb(edge_port, cmdBuffer, cmdLen);
2421 /* something bad happened, let's free up the memory */
2429 /*****************************************************************************
2430 * change_port_settings
2431 * This routine is called to set the UART on the device to match the specified
2433 *****************************************************************************/
2435 static void change_port_settings(struct tty_struct *tty,
2436 struct edgeport_port *edge_port, struct ktermios *old_termios)
2438 struct edgeport_serial *edge_serial = usb_get_serial_data(edge_port->port->serial);
2449 dbg("%s - port %d", __func__, edge_port->port->number);
2451 if (!edge_port->open &&
2452 !edge_port->openPending) {
2453 dbg("%s - port not opened", __func__);
2457 cflag = tty->termios->c_cflag;
2459 switch (cflag & CSIZE) {
2460 case CS5: lData = LCR_BITS_5; mask = 0x1f; dbg("%s - data bits = 5", __func__); break;
2461 case CS6: lData = LCR_BITS_6; mask = 0x3f; dbg("%s - data bits = 6", __func__); break;
2462 case CS7: lData = LCR_BITS_7; mask = 0x7f; dbg("%s - data bits = 7", __func__); break;
2464 case CS8: lData = LCR_BITS_8; dbg("%s - data bits = 8", __func__); break;
2467 lParity = LCR_PAR_NONE;
2468 if (cflag & PARENB) {
2469 if (cflag & CMSPAR) {
2470 if (cflag & PARODD) {
2471 lParity = LCR_PAR_MARK;
2472 dbg("%s - parity = mark", __func__);
2474 lParity = LCR_PAR_SPACE;
2475 dbg("%s - parity = space", __func__);
2477 } else if (cflag & PARODD) {
2478 lParity = LCR_PAR_ODD;
2479 dbg("%s - parity = odd", __func__);
2481 lParity = LCR_PAR_EVEN;
2482 dbg("%s - parity = even", __func__);
2485 dbg("%s - parity = none", __func__);
2488 if (cflag & CSTOPB) {
2490 dbg("%s - stop bits = 2", __func__);
2493 dbg("%s - stop bits = 1", __func__);
2496 /* figure out the flow control settings */
2497 rxFlow = txFlow = 0x00;
2498 if (cflag & CRTSCTS) {
2499 rxFlow |= IOSP_RX_FLOW_RTS;
2500 txFlow |= IOSP_TX_FLOW_CTS;
2501 dbg("%s - RTS/CTS is enabled", __func__);
2503 dbg("%s - RTS/CTS is disabled", __func__);
2506 /* if we are implementing XON/XOFF, set the start and stop character in the device */
2507 if (I_IXOFF(tty) || I_IXON(tty)) {
2508 unsigned char stop_char = STOP_CHAR(tty);
2509 unsigned char start_char = START_CHAR(tty);
2511 if ((!edge_serial->is_epic) ||
2512 ((edge_serial->is_epic) &&
2513 (edge_serial->epic_descriptor.Supports.IOSPSetXChar))) {
2514 send_iosp_ext_cmd(edge_port, IOSP_CMD_SET_XON_CHAR, start_char);
2515 send_iosp_ext_cmd(edge_port, IOSP_CMD_SET_XOFF_CHAR, stop_char);
2518 /* if we are implementing INBOUND XON/XOFF */
2520 rxFlow |= IOSP_RX_FLOW_XON_XOFF;
2521 dbg("%s - INBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x", __func__, start_char, stop_char);
2523 dbg("%s - INBOUND XON/XOFF is disabled", __func__);
2526 /* if we are implementing OUTBOUND XON/XOFF */
2528 txFlow |= IOSP_TX_FLOW_XON_XOFF;
2529 dbg("%s - OUTBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x", __func__, start_char, stop_char);
2531 dbg("%s - OUTBOUND XON/XOFF is disabled", __func__);
2535 /* Set flow control to the configured value */
2536 if ((!edge_serial->is_epic) ||
2537 ((edge_serial->is_epic) &&
2538 (edge_serial->epic_descriptor.Supports.IOSPSetRxFlow)))
2539 send_iosp_ext_cmd(edge_port, IOSP_CMD_SET_RX_FLOW, rxFlow);
2540 if ((!edge_serial->is_epic) ||
2541 ((edge_serial->is_epic) &&
2542 (edge_serial->epic_descriptor.Supports.IOSPSetTxFlow)))
2543 send_iosp_ext_cmd(edge_port, IOSP_CMD_SET_TX_FLOW, txFlow);
2546 edge_port->shadowLCR &= ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
2547 edge_port->shadowLCR |= (lData | lParity | lStop);
2549 edge_port->validDataMask = mask;
2551 /* Send the updated LCR value to the EdgePort */
2552 status = send_cmd_write_uart_register(edge_port, LCR, edge_port->shadowLCR);
2557 /* set up the MCR register and send it to the EdgePort */
2558 edge_port->shadowMCR = MCR_MASTER_IE;
2559 if (cflag & CBAUD) {
2560 edge_port->shadowMCR |= (MCR_DTR | MCR_RTS);
2562 status = send_cmd_write_uart_register(edge_port, MCR, edge_port->shadowMCR);
2567 /* Determine divisor based on baud rate */
2568 baud = tty_get_baud_rate(tty);
2570 /* pick a default, any default... */
2574 dbg("%s - baud rate = %d", __func__, baud);
2575 status = send_cmd_write_baud_rate (edge_port, baud);
2577 /* Speed change was not possible - put back the old speed */
2578 baud = tty_termios_baud_rate(old_termios);
2579 tty_encode_baud_rate(tty, baud, baud);
2585 /****************************************************************************
2587 * Turns a string from Unicode into ASCII.
2588 * Doesn't do a good job with any characters that are outside the normal
2589 * ASCII range, but it's only for debugging...
2590 * NOTE: expects the unicode in LE format
2591 ****************************************************************************/
2592 static void unicode_to_ascii(char *string, int buflen, __le16 *unicode, int unicode_size)
2596 if (buflen <= 0) /* never happens, but... */
2598 --buflen; /* space for nul */
2600 for (i = 0; i < unicode_size; i++) {
2603 string[i] = (char)(le16_to_cpu(unicode[i]));
2609 /****************************************************************************
2610 * get_manufacturing_desc
2611 * reads in the manufacturing descriptor and stores it into the serial
2613 ****************************************************************************/
2614 static void get_manufacturing_desc (struct edgeport_serial *edge_serial)
2618 dbg("getting manufacturer descriptor");
2620 response = rom_read (edge_serial->serial, (EDGE_MANUF_DESC_ADDR & 0xffff0000) >> 16,
2621 (__u16)(EDGE_MANUF_DESC_ADDR & 0x0000ffff), EDGE_MANUF_DESC_LEN,
2622 (__u8 *)(&edge_serial->manuf_descriptor));
2625 dev_err(&edge_serial->serial->dev->dev, "error in getting manufacturer descriptor\n");
2628 dbg("**Manufacturer Descriptor");
2629 dbg(" RomSize: %dK", edge_serial->manuf_descriptor.RomSize);
2630 dbg(" RamSize: %dK", edge_serial->manuf_descriptor.RamSize);
2631 dbg(" CpuRev: %d", edge_serial->manuf_descriptor.CpuRev);
2632 dbg(" BoardRev: %d", edge_serial->manuf_descriptor.BoardRev);
2633 dbg(" NumPorts: %d", edge_serial->manuf_descriptor.NumPorts);
2634 dbg(" DescDate: %d/%d/%d", edge_serial->manuf_descriptor.DescDate[0], edge_serial->manuf_descriptor.DescDate[1], edge_serial->manuf_descriptor.DescDate[2]+1900);
2635 unicode_to_ascii(string, sizeof(string),
2636 edge_serial->manuf_descriptor.SerialNumber,
2637 edge_serial->manuf_descriptor.SerNumLength/2);
2638 dbg(" SerialNumber: %s", string);
2639 unicode_to_ascii(string, sizeof(string),
2640 edge_serial->manuf_descriptor.AssemblyNumber,
2641 edge_serial->manuf_descriptor.AssemblyNumLength/2);
2642 dbg(" AssemblyNumber: %s", string);
2643 unicode_to_ascii(string, sizeof(string),
2644 edge_serial->manuf_descriptor.OemAssyNumber,
2645 edge_serial->manuf_descriptor.OemAssyNumLength/2);
2646 dbg(" OemAssyNumber: %s", string);
2647 dbg(" UartType: %d", edge_serial->manuf_descriptor.UartType);
2648 dbg(" IonPid: %d", edge_serial->manuf_descriptor.IonPid);
2649 dbg(" IonConfig: %d", edge_serial->manuf_descriptor.IonConfig);
2654 /****************************************************************************
2656 * reads in the bootloader descriptor and stores it into the serial
2658 ****************************************************************************/
2659 static void get_boot_desc (struct edgeport_serial *edge_serial)
2663 dbg("getting boot descriptor");
2665 response = rom_read (edge_serial->serial, (EDGE_BOOT_DESC_ADDR & 0xffff0000) >> 16,
2666 (__u16)(EDGE_BOOT_DESC_ADDR & 0x0000ffff), EDGE_BOOT_DESC_LEN,
2667 (__u8 *)(&edge_serial->boot_descriptor));
2670 dev_err(&edge_serial->serial->dev->dev, "error in getting boot descriptor\n");
2672 dbg("**Boot Descriptor:");
2673 dbg(" BootCodeLength: %d", le16_to_cpu(edge_serial->boot_descriptor.BootCodeLength));
2674 dbg(" MajorVersion: %d", edge_serial->boot_descriptor.MajorVersion);
2675 dbg(" MinorVersion: %d", edge_serial->boot_descriptor.MinorVersion);
2676 dbg(" BuildNumber: %d", le16_to_cpu(edge_serial->boot_descriptor.BuildNumber));
2677 dbg(" Capabilities: 0x%x", le16_to_cpu(edge_serial->boot_descriptor.Capabilities));
2678 dbg(" UConfig0: %d", edge_serial->boot_descriptor.UConfig0);
2679 dbg(" UConfig1: %d", edge_serial->boot_descriptor.UConfig1);
2684 /****************************************************************************
2685 * load_application_firmware
2686 * This is called to load the application firmware to the device
2687 ****************************************************************************/
2688 static void load_application_firmware (struct edgeport_serial *edge_serial)
2690 const struct ihex_binrec *rec;
2691 const struct firmware *fw;
2692 const char *fw_name;
2693 const char *fw_info;
2698 switch (edge_serial->product_info.iDownloadFile) {
2699 case EDGE_DOWNLOAD_FILE_I930:
2700 fw_info = "downloading firmware version (930)";
2701 fw_name = "edgeport/down.fw";
2704 case EDGE_DOWNLOAD_FILE_80251:
2705 fw_info = "downloading firmware version (80251)";
2706 fw_name = "edgeport/down2.fw";
2709 case EDGE_DOWNLOAD_FILE_NONE:
2710 dbg ("No download file specified, skipping download\n");
2717 response = request_ihex_firmware(&fw, fw_name,
2718 &edge_serial->serial->dev->dev);
2720 printk(KERN_ERR "Failed to load image \"%s\" err %d\n",
2725 rec = (const struct ihex_binrec *)fw->data;
2726 build = (rec->data[2] << 8) | rec->data[3];
2728 dbg("%s %d.%d.%d", fw_info, rec->data[0], rec->data[1], build);
2730 edge_serial->product_info.FirmwareMajorVersion = fw->data[0];
2731 edge_serial->product_info.FirmwareMinorVersion = fw->data[1];
2732 edge_serial->product_info.FirmwareBuildNumber = cpu_to_le16(build);
2734 for (rec = ihex_next_binrec(rec); rec;
2735 rec = ihex_next_binrec(rec)) {
2736 Operaddr = be32_to_cpu(rec->addr);
2737 response = sram_write(edge_serial->serial,
2740 be16_to_cpu(rec->len),
2743 dev_err(&edge_serial->serial->dev->dev,
2744 "sram_write failed (%x, %x, %d)\n",
2745 Operaddr >> 16, Operaddr & 0xFFFF,
2746 be16_to_cpu(rec->len));
2751 dbg("sending exec_dl_code");
2752 response = usb_control_msg (edge_serial->serial->dev,
2753 usb_sndctrlpipe(edge_serial->serial->dev, 0),
2754 USB_REQUEST_ION_EXEC_DL_CODE,
2755 0x40, 0x4000, 0x0001, NULL, 0, 3000);
2757 release_firmware(fw);
2762 /****************************************************************************
2764 ****************************************************************************/
2765 static int edge_startup (struct usb_serial *serial)
2767 struct edgeport_serial *edge_serial;
2768 struct edgeport_port *edge_port;
2769 struct usb_device *dev;
2772 bool interrupt_in_found;
2774 bool bulk_out_found;
2775 static __u32 descriptor[3] = { EDGE_COMPATIBILITY_MASK0,
2776 EDGE_COMPATIBILITY_MASK1,
2777 EDGE_COMPATIBILITY_MASK2 };
2781 /* create our private serial structure */
2782 edge_serial = kzalloc(sizeof(struct edgeport_serial), GFP_KERNEL);
2783 if (edge_serial == NULL) {
2784 dev_err(&serial->dev->dev, "%s - Out of memory\n", __func__);
2787 spin_lock_init(&edge_serial->es_lock);
2788 edge_serial->serial = serial;
2789 usb_set_serial_data(serial, edge_serial);
2791 /* get the name for the device from the device */
2792 i = get_string(dev, dev->descriptor.iManufacturer,
2793 &edge_serial->name[0], MAX_NAME_LEN+1);
2794 edge_serial->name[i++] = ' ';
2795 get_string(dev, dev->descriptor.iProduct,
2796 &edge_serial->name[i], MAX_NAME_LEN+2 - i);
2798 dev_info(&serial->dev->dev, "%s detected\n", edge_serial->name);
2800 /* Read the epic descriptor */
2801 if (get_epic_descriptor(edge_serial) <= 0) {
2802 /* memcpy descriptor to Supports structures */
2803 memcpy(&edge_serial->epic_descriptor.Supports, descriptor,
2804 sizeof(struct edge_compatibility_bits));
2806 /* get the manufacturing descriptor for this device */
2807 get_manufacturing_desc (edge_serial);
2809 /* get the boot descriptor */
2810 get_boot_desc (edge_serial);
2812 get_product_info(edge_serial);
2815 /* set the number of ports from the manufacturing description */
2816 /* serial->num_ports = serial->product_info.NumPorts; */
2817 if ((!edge_serial->is_epic) &&
2818 (edge_serial->product_info.NumPorts != serial->num_ports)) {
2819 dev_warn(&serial->dev->dev, "Device Reported %d serial ports "
2820 "vs. core thinking we have %d ports, email "
2821 "greg@kroah.com this information.\n",
2822 edge_serial->product_info.NumPorts,
2826 dbg("%s - time 1 %ld", __func__, jiffies);
2828 /* If not an EPiC device */
2829 if (!edge_serial->is_epic) {
2830 /* now load the application firmware into this device */
2831 load_application_firmware (edge_serial);
2833 dbg("%s - time 2 %ld", __func__, jiffies);
2835 /* Check current Edgeport EEPROM and update if necessary */
2836 update_edgeport_E2PROM (edge_serial);
2838 dbg("%s - time 3 %ld", __func__, jiffies);
2840 /* set the configuration to use #1 */
2841 // dbg("set_configuration 1");
2842 // usb_set_configuration (dev, 1);
2844 dbg(" FirmwareMajorVersion %d.%d.%d",
2845 edge_serial->product_info.FirmwareMajorVersion,
2846 edge_serial->product_info.FirmwareMinorVersion,
2847 le16_to_cpu(edge_serial->product_info.FirmwareBuildNumber));
2849 /* we set up the pointers to the endpoints in the edge_open function,
2850 * as the structures aren't created yet. */
2852 /* set up our port private structures */
2853 for (i = 0; i < serial->num_ports; ++i) {
2854 edge_port = kmalloc (sizeof(struct edgeport_port), GFP_KERNEL);
2855 if (edge_port == NULL) {
2856 dev_err(&serial->dev->dev, "%s - Out of memory\n", __func__);
2857 for (j = 0; j < i; ++j) {
2858 kfree (usb_get_serial_port_data(serial->port[j]));
2859 usb_set_serial_port_data(serial->port[j], NULL);
2861 usb_set_serial_data(serial, NULL);
2865 memset (edge_port, 0, sizeof(struct edgeport_port));
2866 spin_lock_init(&edge_port->ep_lock);
2867 edge_port->port = serial->port[i];
2868 usb_set_serial_port_data(serial->port[i], edge_port);
2873 if (edge_serial->is_epic) {
2874 /* EPIC thing, set up our interrupt polling now and our read urb, so
2875 * that the device knows it really is connected. */
2876 interrupt_in_found = bulk_in_found = bulk_out_found = false;
2877 for (i = 0; i < serial->interface->altsetting[0].desc.bNumEndpoints; ++i) {
2878 struct usb_endpoint_descriptor *endpoint;
2881 endpoint = &serial->interface->altsetting[0].endpoint[i].desc;
2882 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
2883 if (!interrupt_in_found &&
2884 (usb_endpoint_is_int_in(endpoint))) {
2885 /* we found a interrupt in endpoint */
2886 dbg("found interrupt in");
2888 /* not set up yet, so do it now */
2889 edge_serial->interrupt_read_urb = usb_alloc_urb(0, GFP_KERNEL);
2890 if (!edge_serial->interrupt_read_urb) {
2891 err("out of memory");
2894 edge_serial->interrupt_in_buffer = kmalloc(buffer_size, GFP_KERNEL);
2895 if (!edge_serial->interrupt_in_buffer) {
2896 err("out of memory");
2897 usb_free_urb(edge_serial->interrupt_read_urb);
2900 edge_serial->interrupt_in_endpoint = endpoint->bEndpointAddress;
2902 /* set up our interrupt urb */
2903 usb_fill_int_urb(edge_serial->interrupt_read_urb,
2905 usb_rcvintpipe(dev, endpoint->bEndpointAddress),
2906 edge_serial->interrupt_in_buffer,
2908 edge_interrupt_callback,
2910 endpoint->bInterval);
2912 interrupt_in_found = true;
2915 if (!bulk_in_found &&
2916 (usb_endpoint_is_bulk_in(endpoint))) {
2917 /* we found a bulk in endpoint */
2918 dbg("found bulk in");
2920 /* not set up yet, so do it now */
2921 edge_serial->read_urb = usb_alloc_urb(0, GFP_KERNEL);
2922 if (!edge_serial->read_urb) {
2923 err("out of memory");
2926 edge_serial->bulk_in_buffer = kmalloc(buffer_size, GFP_KERNEL);
2927 if (!edge_serial->bulk_in_buffer) {
2928 err ("out of memory");
2929 usb_free_urb(edge_serial->read_urb);
2932 edge_serial->bulk_in_endpoint = endpoint->bEndpointAddress;
2934 /* set up our bulk in urb */
2935 usb_fill_bulk_urb(edge_serial->read_urb, dev,
2936 usb_rcvbulkpipe(dev, endpoint->bEndpointAddress),
2937 edge_serial->bulk_in_buffer,
2938 le16_to_cpu(endpoint->wMaxPacketSize),
2939 edge_bulk_in_callback,
2941 bulk_in_found = true;
2944 if (!bulk_out_found &&
2945 (usb_endpoint_is_bulk_out(endpoint))) {
2946 /* we found a bulk out endpoint */
2947 dbg("found bulk out");
2948 edge_serial->bulk_out_endpoint = endpoint->bEndpointAddress;
2949 bulk_out_found = true;
2953 if (!interrupt_in_found || !bulk_in_found || !bulk_out_found) {
2954 err ("Error - the proper endpoints were not found!");
2958 /* start interrupt read for this edgeport this interrupt will
2959 * continue as long as the edgeport is connected */
2960 response = usb_submit_urb(edge_serial->interrupt_read_urb, GFP_KERNEL);
2962 err("%s - Error %d submitting control urb", __func__, response);
2968 /****************************************************************************
2970 * This function is called whenever the device is removed from the usb bus.
2971 ****************************************************************************/
2972 static void edge_shutdown (struct usb_serial *serial)
2974 struct edgeport_serial *edge_serial = usb_get_serial_data(serial);
2977 dbg("%s", __func__);
2979 /* stop reads and writes on all ports */
2980 for (i=0; i < serial->num_ports; ++i) {
2981 kfree (usb_get_serial_port_data(serial->port[i]));
2982 usb_set_serial_port_data(serial->port[i], NULL);
2984 /* free up our endpoint stuff */
2985 if (edge_serial->is_epic) {
2986 usb_kill_urb(edge_serial->interrupt_read_urb);
2987 usb_free_urb(edge_serial->interrupt_read_urb);
2988 kfree(edge_serial->interrupt_in_buffer);
2990 usb_kill_urb(edge_serial->read_urb);
2991 usb_free_urb(edge_serial->read_urb);
2992 kfree(edge_serial->bulk_in_buffer);
2996 usb_set_serial_data(serial, NULL);
3000 /****************************************************************************
3002 * This is called by the module subsystem, or on startup to initialize us
3003 ****************************************************************************/
3004 static int __init edgeport_init(void)
3008 retval = usb_serial_register(&edgeport_2port_device);
3010 goto failed_2port_device_register;
3011 retval = usb_serial_register(&edgeport_4port_device);
3013 goto failed_4port_device_register;
3014 retval = usb_serial_register(&edgeport_8port_device);
3016 goto failed_8port_device_register;
3017 retval = usb_serial_register(&epic_device);
3019 goto failed_epic_device_register;
3020 retval = usb_register(&io_driver);
3022 goto failed_usb_register;
3023 atomic_set(&CmdUrbs, 0);
3024 info(DRIVER_DESC " " DRIVER_VERSION);
3027 failed_usb_register:
3028 usb_serial_deregister(&epic_device);
3029 failed_epic_device_register:
3030 usb_serial_deregister(&edgeport_8port_device);
3031 failed_8port_device_register:
3032 usb_serial_deregister(&edgeport_4port_device);
3033 failed_4port_device_register:
3034 usb_serial_deregister(&edgeport_2port_device);
3035 failed_2port_device_register:
3040 /****************************************************************************
3042 * Called when the driver is about to be unloaded.
3043 ****************************************************************************/
3044 static void __exit edgeport_exit (void)
3046 usb_deregister (&io_driver);
3047 usb_serial_deregister (&edgeport_2port_device);
3048 usb_serial_deregister (&edgeport_4port_device);
3049 usb_serial_deregister (&edgeport_8port_device);
3050 usb_serial_deregister (&epic_device);
3053 module_init(edgeport_init);
3054 module_exit(edgeport_exit);
3056 /* Module information */
3057 MODULE_AUTHOR( DRIVER_AUTHOR );
3058 MODULE_DESCRIPTION( DRIVER_DESC );
3059 MODULE_LICENSE("GPL");
3060 MODULE_FIRMWARE("edgeport/boot.fw");
3061 MODULE_FIRMWARE("edgeport/boot2.fw");
3062 MODULE_FIRMWARE("edgeport/down.fw");
3063 MODULE_FIRMWARE("edgeport/down2.fw");
3065 module_param(debug, bool, S_IRUGO | S_IWUSR);
3066 MODULE_PARM_DESC(debug, "Debug enabled or not");
3068 module_param(low_latency, bool, S_IRUGO | S_IWUSR);
3069 MODULE_PARM_DESC(low_latency, "Low latency enabled or not");