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 <asm/uaccess.h>
46 #include <linux/usb.h>
47 #include <linux/usb/serial.h>
48 #include "io_edgeport.h"
49 #include "io_ionsp.h" /* info for the iosp messages */
50 #include "io_16654.h" /* 16654 UART defines */
55 #define DRIVER_VERSION "v2.7"
56 #define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com> and David Iacovelli"
57 #define DRIVER_DESC "Edgeport USB Serial Driver"
59 /* First, the latest boot code - for first generation edgeports */
60 #define IMAGE_ARRAY_NAME BootCodeImage_GEN1
61 #define IMAGE_VERSION_NAME BootCodeImageVersion_GEN1
62 #include "io_fw_boot.h" /* the bootloader firmware to download to a device, if it needs it */
64 /* for second generation edgeports */
65 #define IMAGE_ARRAY_NAME BootCodeImage_GEN2
66 #define IMAGE_VERSION_NAME BootCodeImageVersion_GEN2
67 #include "io_fw_boot2.h" /* the bootloader firmware to download to a device, if it needs it */
69 /* Then finally the main run-time operational code - for first generation edgeports */
70 #define IMAGE_ARRAY_NAME OperationalCodeImage_GEN1
71 #define IMAGE_VERSION_NAME OperationalCodeImageVersion_GEN1
72 #include "io_fw_down.h" /* Define array OperationalCodeImage[] */
74 /* for second generation edgeports */
75 #define IMAGE_ARRAY_NAME OperationalCodeImage_GEN2
76 #define IMAGE_VERSION_NAME OperationalCodeImageVersion_GEN2
77 #include "io_fw_down2.h" /* Define array OperationalCodeImage[] */
79 #define MAX_NAME_LEN 64
81 #define CHASE_TIMEOUT (5*HZ) /* 5 seconds */
82 #define OPEN_TIMEOUT (5*HZ) /* 5 seconds */
83 #define COMMAND_TIMEOUT (5*HZ) /* 5 seconds */
85 /* receive port state */
87 EXPECT_HDR1 = 0, /* Expect header byte 1 */
88 EXPECT_HDR2 = 1, /* Expect header byte 2 */
89 EXPECT_DATA = 2, /* Expect 'RxBytesRemaining' data */
90 EXPECT_HDR3 = 3, /* Expect header byte 3 (for status hdrs only) */
95 * This Transmit queue is an extension of the edgeport Rx buffer.
96 * The maximum amount of data buffered in both the edgeport
97 * Rx buffer (maxTxCredits) and this buffer will never exceed maxTxCredits.
100 unsigned int head; /* index to head pointer (write) */
101 unsigned int tail; /* index to tail pointer (read) */
102 unsigned int count; /* Bytes in queue */
103 unsigned int size; /* Max size of queue (equal to Max number of TxCredits) */
104 unsigned char *fifo; /* allocated Buffer */
107 /* This structure holds all of the local port information */
108 struct edgeport_port {
109 __u16 txCredits; /* our current credits for this port */
110 __u16 maxTxCredits; /* the max size of the port */
112 struct TxFifo txfifo; /* transmit fifo -- size will be maxTxCredits */
113 struct urb *write_urb; /* write URB for this port */
114 char write_in_progress; /* TRUE while a write URB is outstanding */
117 __u8 shadowLCR; /* last LCR value received */
118 __u8 shadowMCR; /* last MCR value received */
119 __u8 shadowMSR; /* last MSR value received */
120 __u8 shadowLSR; /* last LSR value received */
121 __u8 shadowXonChar; /* last value set as XON char in Edgeport */
122 __u8 shadowXoffChar; /* last value set as XOFF char in Edgeport */
130 char chaseResponsePending;
132 wait_queue_head_t wait_chase; /* for handling sleeping while waiting for chase to finish */
133 wait_queue_head_t wait_open; /* for handling sleeping while waiting for open to finish */
134 wait_queue_head_t wait_command; /* for handling sleeping while waiting for command to finish */
135 wait_queue_head_t delta_msr_wait; /* for handling sleeping while waiting for msr change to happen */
137 struct async_icount icount;
138 struct usb_serial_port *port; /* loop back to the owner of this object */
142 /* This structure holds all of the individual device information */
143 struct edgeport_serial {
144 char name[MAX_NAME_LEN+2]; /* string name of this device */
146 struct edge_manuf_descriptor manuf_descriptor; /* the manufacturer descriptor */
147 struct edge_boot_descriptor boot_descriptor; /* the boot firmware descriptor */
148 struct edgeport_product_info product_info; /* Product Info */
150 __u8 interrupt_in_endpoint; /* the interrupt endpoint handle */
151 unsigned char * interrupt_in_buffer; /* the buffer we use for the interrupt endpoint */
152 struct urb * interrupt_read_urb; /* our interrupt urb */
154 __u8 bulk_in_endpoint; /* the bulk in endpoint handle */
155 unsigned char * bulk_in_buffer; /* the buffer we use for the bulk in endpoint */
156 struct urb * read_urb; /* our bulk read urb */
157 int read_in_progress;
160 __u8 bulk_out_endpoint; /* the bulk out endpoint handle */
162 __s16 rxBytesAvail; /* the number of bytes that we need to read from this device */
164 enum RXSTATE rxState; /* the current state of the bulk receive processor */
165 __u8 rxHeader1; /* receive header byte 1 */
166 __u8 rxHeader2; /* receive header byte 2 */
167 __u8 rxHeader3; /* receive header byte 3 */
168 __u8 rxPort; /* the port that we are currently receiving data for */
169 __u8 rxStatusCode; /* the receive status code */
170 __u8 rxStatusParam; /* the receive status paramater */
171 __s16 rxBytesRemaining; /* the number of port bytes left to read */
172 struct usb_serial *serial; /* loop back to the owner of this object */
175 /* baud rate information */
176 struct divisor_table_entry {
182 // Define table of divisors for Rev A EdgePort/4 hardware
183 // These assume a 3.6864MHz crystal, the standard /16, and
186 static const struct divisor_table_entry divisor_table[] = {
189 { 110, 2095}, /* 2094.545455 => 230450 => .0217 % over */
190 { 134, 1713}, /* 1713.011152 => 230398.5 => .00065% under */
208 /* local variables */
211 static int low_latency = 1; /* tty low latency flag, on by default */
213 static int CmdUrbs = 0; /* Number of outstanding Command Write Urbs */
216 /* local function prototypes */
218 /* function prototypes for all URB callbacks */
219 static void edge_interrupt_callback (struct urb *urb);
220 static void edge_bulk_in_callback (struct urb *urb);
221 static void edge_bulk_out_data_callback (struct urb *urb);
222 static void edge_bulk_out_cmd_callback (struct urb *urb);
224 /* function prototypes for the usbserial callbacks */
225 static int edge_open (struct usb_serial_port *port, struct file *filp);
226 static void edge_close (struct usb_serial_port *port, struct file *filp);
227 static int edge_write (struct usb_serial_port *port, const unsigned char *buf, int count);
228 static int edge_write_room (struct usb_serial_port *port);
229 static int edge_chars_in_buffer (struct usb_serial_port *port);
230 static void edge_throttle (struct usb_serial_port *port);
231 static void edge_unthrottle (struct usb_serial_port *port);
232 static void edge_set_termios (struct usb_serial_port *port, struct ktermios *old_termios);
233 static int edge_ioctl (struct usb_serial_port *port, struct file *file, unsigned int cmd, unsigned long arg);
234 static void edge_break (struct usb_serial_port *port, int break_state);
235 static int edge_tiocmget (struct usb_serial_port *port, struct file *file);
236 static int edge_tiocmset (struct usb_serial_port *port, struct file *file, unsigned int set, unsigned int clear);
237 static int edge_startup (struct usb_serial *serial);
238 static void edge_shutdown (struct usb_serial *serial);
241 #include "io_tables.h" /* all of the devices that this driver supports */
243 static struct usb_driver io_driver = {
244 .name = "io_edgeport",
245 .probe = usb_serial_probe,
246 .disconnect = usb_serial_disconnect,
247 .id_table = id_table_combined,
251 /* function prototypes for all of our local functions */
252 static void process_rcvd_data (struct edgeport_serial *edge_serial, unsigned char *buffer, __u16 bufferLength);
253 static void process_rcvd_status (struct edgeport_serial *edge_serial, __u8 byte2, __u8 byte3);
254 static void edge_tty_recv (struct device *dev, struct tty_struct *tty, unsigned char *data, int length);
255 static void handle_new_msr (struct edgeport_port *edge_port, __u8 newMsr);
256 static void handle_new_lsr (struct edgeport_port *edge_port, __u8 lsrData, __u8 lsr, __u8 data);
257 static int send_iosp_ext_cmd (struct edgeport_port *edge_port, __u8 command, __u8 param);
258 static int calc_baud_rate_divisor (int baud_rate, int *divisor);
259 static int send_cmd_write_baud_rate (struct edgeport_port *edge_port, int baudRate);
260 static void change_port_settings (struct edgeport_port *edge_port, struct ktermios *old_termios);
261 static int send_cmd_write_uart_register (struct edgeport_port *edge_port, __u8 regNum, __u8 regValue);
262 static int write_cmd_usb (struct edgeport_port *edge_port, unsigned char *buffer, int writeLength);
263 static void send_more_port_data (struct edgeport_serial *edge_serial, struct edgeport_port *edge_port);
265 static int sram_write (struct usb_serial *serial, __u16 extAddr, __u16 addr, __u16 length, __u8 *data);
266 static int rom_read (struct usb_serial *serial, __u16 extAddr, __u16 addr, __u16 length, __u8 *data);
267 static int rom_write (struct usb_serial *serial, __u16 extAddr, __u16 addr, __u16 length, __u8 *data);
268 static void get_manufacturing_desc (struct edgeport_serial *edge_serial);
269 static void get_boot_desc (struct edgeport_serial *edge_serial);
270 static void load_application_firmware (struct edgeport_serial *edge_serial);
272 static void unicode_to_ascii(char *string, int buflen, __le16 *unicode, int unicode_size);
275 // ************************************************************************
276 // ************************************************************************
277 // ************************************************************************
278 // ************************************************************************
280 /************************************************************************
282 * update_edgeport_E2PROM() Compare current versions of *
283 * Boot ROM and Manufacture *
284 * Descriptors with versions *
285 * embedded in this driver *
287 ************************************************************************/
288 static void update_edgeport_E2PROM (struct edgeport_serial *edge_serial)
292 __u8 BootMajorVersion;
293 __u8 BootMinorVersion;
294 __le16 BootBuildNumber;
297 struct edge_firmware_image_record *record;
298 unsigned char *firmware;
302 switch (edge_serial->product_info.iDownloadFile) {
303 case EDGE_DOWNLOAD_FILE_I930:
304 BootMajorVersion = BootCodeImageVersion_GEN1.MajorVersion;
305 BootMinorVersion = BootCodeImageVersion_GEN1.MinorVersion;
306 BootBuildNumber = cpu_to_le16(BootCodeImageVersion_GEN1.BuildNumber);
307 BootImage = &BootCodeImage_GEN1[0];
308 BootSize = sizeof( BootCodeImage_GEN1 );
311 case EDGE_DOWNLOAD_FILE_80251:
312 BootMajorVersion = BootCodeImageVersion_GEN2.MajorVersion;
313 BootMinorVersion = BootCodeImageVersion_GEN2.MinorVersion;
314 BootBuildNumber = cpu_to_le16(BootCodeImageVersion_GEN2.BuildNumber);
315 BootImage = &BootCodeImage_GEN2[0];
316 BootSize = sizeof( BootCodeImage_GEN2 );
323 // Check Boot Image Version
324 BootCurVer = (edge_serial->boot_descriptor.MajorVersion << 24) +
325 (edge_serial->boot_descriptor.MinorVersion << 16) +
326 le16_to_cpu(edge_serial->boot_descriptor.BuildNumber);
328 BootNewVer = (BootMajorVersion << 24) +
329 (BootMinorVersion << 16) +
330 le16_to_cpu(BootBuildNumber);
332 dbg("Current Boot Image version %d.%d.%d",
333 edge_serial->boot_descriptor.MajorVersion,
334 edge_serial->boot_descriptor.MinorVersion,
335 le16_to_cpu(edge_serial->boot_descriptor.BuildNumber));
338 if (BootNewVer > BootCurVer) {
339 dbg("**Update Boot Image from %d.%d.%d to %d.%d.%d",
340 edge_serial->boot_descriptor.MajorVersion,
341 edge_serial->boot_descriptor.MinorVersion,
342 le16_to_cpu(edge_serial->boot_descriptor.BuildNumber),
345 le16_to_cpu(BootBuildNumber));
348 dbg("Downloading new Boot Image");
350 firmware = BootImage;
353 record = (struct edge_firmware_image_record *)firmware;
354 response = rom_write (edge_serial->serial, le16_to_cpu(record->ExtAddr), le16_to_cpu(record->Addr), le16_to_cpu(record->Len), &record->Data[0]);
356 dev_err(&edge_serial->serial->dev->dev, "rom_write failed (%x, %x, %d)\n", le16_to_cpu(record->ExtAddr), le16_to_cpu(record->Addr), le16_to_cpu(record->Len));
359 firmware += sizeof (struct edge_firmware_image_record) + le16_to_cpu(record->Len);
360 if (firmware >= &BootImage[BootSize]) {
365 dbg("Boot Image -- already up to date");
370 /************************************************************************
372 * Get string descriptor from device *
374 ************************************************************************/
375 static int get_string (struct usb_device *dev, int Id, char *string, int buflen)
377 struct usb_string_descriptor StringDesc;
378 struct usb_string_descriptor *pStringDesc;
380 dbg("%s - USB String ID = %d", __FUNCTION__, Id );
382 if (!usb_get_descriptor(dev, USB_DT_STRING, Id, &StringDesc, sizeof(StringDesc))) {
386 pStringDesc = kmalloc (StringDesc.bLength, GFP_KERNEL);
392 if (!usb_get_descriptor(dev, USB_DT_STRING, Id, pStringDesc, StringDesc.bLength )) {
397 unicode_to_ascii(string, buflen, pStringDesc->wData, pStringDesc->bLength/2);
400 return strlen(string);
405 /************************************************************************
407 * Get string descriptor from device
409 ************************************************************************/
410 static int get_string_desc (struct usb_device *dev, int Id, struct usb_string_descriptor **pRetDesc)
412 struct usb_string_descriptor StringDesc;
413 struct usb_string_descriptor *pStringDesc;
415 dbg("%s - USB String ID = %d", __FUNCTION__, Id );
417 if (!usb_get_descriptor(dev, USB_DT_STRING, Id, &StringDesc, sizeof(StringDesc))) {
421 pStringDesc = kmalloc (StringDesc.bLength, GFP_KERNEL);
427 if (!usb_get_descriptor(dev, USB_DT_STRING, Id, pStringDesc, StringDesc.bLength )) {
432 *pRetDesc = pStringDesc;
437 static void get_product_info(struct edgeport_serial *edge_serial)
439 struct edgeport_product_info *product_info = &edge_serial->product_info;
441 memset (product_info, 0, sizeof(struct edgeport_product_info));
443 product_info->ProductId = (__u16)(le16_to_cpu(edge_serial->serial->dev->descriptor.idProduct) & ~ION_DEVICE_ID_80251_NETCHIP);
444 product_info->NumPorts = edge_serial->manuf_descriptor.NumPorts;
445 product_info->ProdInfoVer = 0;
447 product_info->RomSize = edge_serial->manuf_descriptor.RomSize;
448 product_info->RamSize = edge_serial->manuf_descriptor.RamSize;
449 product_info->CpuRev = edge_serial->manuf_descriptor.CpuRev;
450 product_info->BoardRev = edge_serial->manuf_descriptor.BoardRev;
452 product_info->BootMajorVersion = edge_serial->boot_descriptor.MajorVersion;
453 product_info->BootMinorVersion = edge_serial->boot_descriptor.MinorVersion;
454 product_info->BootBuildNumber = edge_serial->boot_descriptor.BuildNumber;
456 memcpy(product_info->ManufactureDescDate, edge_serial->manuf_descriptor.DescDate, sizeof(edge_serial->manuf_descriptor.DescDate));
458 // check if this is 2nd generation hardware
459 if (le16_to_cpu(edge_serial->serial->dev->descriptor.idProduct) & ION_DEVICE_ID_80251_NETCHIP) {
460 product_info->FirmwareMajorVersion = OperationalCodeImageVersion_GEN2.MajorVersion;
461 product_info->FirmwareMinorVersion = OperationalCodeImageVersion_GEN2.MinorVersion;
462 product_info->FirmwareBuildNumber = cpu_to_le16(OperationalCodeImageVersion_GEN2.BuildNumber);
463 product_info->iDownloadFile = EDGE_DOWNLOAD_FILE_80251;
465 product_info->FirmwareMajorVersion = OperationalCodeImageVersion_GEN1.MajorVersion;
466 product_info->FirmwareMinorVersion = OperationalCodeImageVersion_GEN1.MinorVersion;
467 product_info->FirmwareBuildNumber = cpu_to_le16(OperationalCodeImageVersion_GEN1.BuildNumber);
468 product_info->iDownloadFile = EDGE_DOWNLOAD_FILE_I930;
471 // Determine Product type and set appropriate flags
472 switch (DEVICE_ID_FROM_USB_PRODUCT_ID(product_info->ProductId)) {
473 case ION_DEVICE_ID_EDGEPORT_COMPATIBLE:
474 case ION_DEVICE_ID_EDGEPORT_4T:
475 case ION_DEVICE_ID_EDGEPORT_4:
476 case ION_DEVICE_ID_EDGEPORT_2:
477 case ION_DEVICE_ID_EDGEPORT_8_DUAL_CPU:
478 case ION_DEVICE_ID_EDGEPORT_8:
479 case ION_DEVICE_ID_EDGEPORT_421:
480 case ION_DEVICE_ID_EDGEPORT_21:
481 case ION_DEVICE_ID_EDGEPORT_2_DIN:
482 case ION_DEVICE_ID_EDGEPORT_4_DIN:
483 case ION_DEVICE_ID_EDGEPORT_16_DUAL_CPU:
484 product_info->IsRS232 = 1;
487 case ION_DEVICE_ID_EDGEPORT_2I: // Edgeport/2 RS422/RS485
488 product_info->IsRS422 = 1;
489 product_info->IsRS485 = 1;
492 case ION_DEVICE_ID_EDGEPORT_8I: // Edgeport/4 RS422
493 case ION_DEVICE_ID_EDGEPORT_4I: // Edgeport/4 RS422
494 product_info->IsRS422 = 1;
498 // Dump Product Info structure
499 dbg("**Product Information:");
500 dbg(" ProductId %x", product_info->ProductId );
501 dbg(" NumPorts %d", product_info->NumPorts );
502 dbg(" ProdInfoVer %d", product_info->ProdInfoVer );
503 dbg(" IsServer %d", product_info->IsServer);
504 dbg(" IsRS232 %d", product_info->IsRS232 );
505 dbg(" IsRS422 %d", product_info->IsRS422 );
506 dbg(" IsRS485 %d", product_info->IsRS485 );
507 dbg(" RomSize %d", product_info->RomSize );
508 dbg(" RamSize %d", product_info->RamSize );
509 dbg(" CpuRev %x", product_info->CpuRev );
510 dbg(" BoardRev %x", product_info->BoardRev);
511 dbg(" BootMajorVersion %d.%d.%d", product_info->BootMajorVersion,
512 product_info->BootMinorVersion,
513 le16_to_cpu(product_info->BootBuildNumber));
514 dbg(" FirmwareMajorVersion %d.%d.%d", product_info->FirmwareMajorVersion,
515 product_info->FirmwareMinorVersion,
516 le16_to_cpu(product_info->FirmwareBuildNumber));
517 dbg(" ManufactureDescDate %d/%d/%d", product_info->ManufactureDescDate[0],
518 product_info->ManufactureDescDate[1],
519 product_info->ManufactureDescDate[2]+1900);
520 dbg(" iDownloadFile 0x%x", product_info->iDownloadFile);
525 /************************************************************************/
526 /************************************************************************/
527 /* U S B C A L L B A C K F U N C T I O N S */
528 /* U S B C A L L B A C K F U N C T I O N S */
529 /************************************************************************/
530 /************************************************************************/
532 /*****************************************************************************
533 * edge_interrupt_callback
534 * this is the callback function for when we have received data on the
535 * interrupt endpoint.
536 *****************************************************************************/
537 static void edge_interrupt_callback (struct urb *urb)
539 struct edgeport_serial *edge_serial = (struct edgeport_serial *)urb->context;
540 struct edgeport_port *edge_port;
541 struct usb_serial_port *port;
542 unsigned char *data = urb->transfer_buffer;
543 int length = urb->actual_length;
550 dbg("%s", __FUNCTION__);
552 switch (urb->status) {
559 /* this urb is terminated, clean up */
560 dbg("%s - urb shutting down with status: %d", __FUNCTION__, urb->status);
563 dbg("%s - nonzero urb status received: %d", __FUNCTION__, urb->status);
567 // process this interrupt-read even if there are no ports open
569 usb_serial_debug_data(debug, &edge_serial->serial->dev->dev, __FUNCTION__, length, data);
572 bytes_avail = data[0] | (data[1] << 8);
574 spin_lock(&edge_serial->es_lock);
575 edge_serial->rxBytesAvail += bytes_avail;
576 dbg("%s - bytes_avail=%d, rxBytesAvail=%d, read_in_progress=%d", __FUNCTION__, bytes_avail, edge_serial->rxBytesAvail, edge_serial->read_in_progress);
578 if (edge_serial->rxBytesAvail > 0 &&
579 !edge_serial->read_in_progress) {
580 dbg("%s - posting a read", __FUNCTION__);
581 edge_serial->read_in_progress = TRUE;
583 /* we have pending bytes on the bulk in pipe, send a request */
584 edge_serial->read_urb->dev = edge_serial->serial->dev;
585 result = usb_submit_urb(edge_serial->read_urb, GFP_ATOMIC);
587 dev_err(&edge_serial->serial->dev->dev, "%s - usb_submit_urb(read bulk) failed with result = %d\n", __FUNCTION__, result);
588 edge_serial->read_in_progress = FALSE;
591 spin_unlock(&edge_serial->es_lock);
594 /* grab the txcredits for the ports if available */
597 while ((position < length) && (portNumber < edge_serial->serial->num_ports)) {
598 txCredits = data[position] | (data[position+1] << 8);
600 port = edge_serial->serial->port[portNumber];
601 edge_port = usb_get_serial_port_data(port);
602 if (edge_port->open) {
603 spin_lock(&edge_port->ep_lock);
604 edge_port->txCredits += txCredits;
605 spin_unlock(&edge_port->ep_lock);
606 dbg("%s - txcredits for port%d = %d", __FUNCTION__, portNumber, edge_port->txCredits);
608 /* tell the tty driver that something has changed */
609 if (edge_port->port->tty)
610 tty_wakeup(edge_port->port->tty);
612 // Since we have more credit, check if more data can be sent
613 send_more_port_data(edge_serial, edge_port);
622 result = usb_submit_urb (urb, GFP_ATOMIC);
624 dev_err(&urb->dev->dev, "%s - Error %d submitting control urb\n", __FUNCTION__, result);
629 /*****************************************************************************
630 * edge_bulk_in_callback
631 * this is the callback function for when we have received data on the
633 *****************************************************************************/
634 static void edge_bulk_in_callback (struct urb *urb)
636 struct edgeport_serial *edge_serial = (struct edgeport_serial *)urb->context;
637 unsigned char *data = urb->transfer_buffer;
639 __u16 raw_data_length;
641 dbg("%s", __FUNCTION__);
644 dbg("%s - nonzero read bulk status received: %d", __FUNCTION__, urb->status);
645 edge_serial->read_in_progress = FALSE;
649 if (urb->actual_length == 0) {
650 dbg("%s - read bulk callback with no data", __FUNCTION__);
651 edge_serial->read_in_progress = FALSE;
655 raw_data_length = urb->actual_length;
657 usb_serial_debug_data(debug, &edge_serial->serial->dev->dev, __FUNCTION__, raw_data_length, data);
659 spin_lock(&edge_serial->es_lock);
661 /* decrement our rxBytes available by the number that we just got */
662 edge_serial->rxBytesAvail -= raw_data_length;
664 dbg("%s - Received = %d, rxBytesAvail %d", __FUNCTION__, raw_data_length, edge_serial->rxBytesAvail);
666 process_rcvd_data (edge_serial, data, urb->actual_length);
668 /* check to see if there's any more data for us to read */
669 if (edge_serial->rxBytesAvail > 0) {
670 dbg("%s - posting a read", __FUNCTION__);
671 edge_serial->read_urb->dev = edge_serial->serial->dev;
672 status = usb_submit_urb(edge_serial->read_urb, GFP_ATOMIC);
674 dev_err(&urb->dev->dev, "%s - usb_submit_urb(read bulk) failed, status = %d\n", __FUNCTION__, status);
675 edge_serial->read_in_progress = FALSE;
678 edge_serial->read_in_progress = FALSE;
681 spin_unlock(&edge_serial->es_lock);
685 /*****************************************************************************
686 * edge_bulk_out_data_callback
687 * this is the callback function for when we have finished sending serial data
688 * on the bulk out endpoint.
689 *****************************************************************************/
690 static void edge_bulk_out_data_callback (struct urb *urb)
692 struct edgeport_port *edge_port = (struct edgeport_port *)urb->context;
693 struct tty_struct *tty;
695 dbg("%s", __FUNCTION__);
698 dbg("%s - nonzero write bulk status received: %d", __FUNCTION__, urb->status);
701 tty = edge_port->port->tty;
703 if (tty && edge_port->open) {
704 /* let the tty driver wakeup if it has a special write_wakeup function */
708 // Release the Write URB
709 edge_port->write_in_progress = FALSE;
711 // Check if more data needs to be sent
712 send_more_port_data((struct edgeport_serial *)(usb_get_serial_data(edge_port->port->serial)), edge_port);
716 /*****************************************************************************
718 * this is the callback function for when we have finished sending a command
719 * on the bulk out endpoint.
720 *****************************************************************************/
721 static void edge_bulk_out_cmd_callback (struct urb *urb)
723 struct edgeport_port *edge_port = (struct edgeport_port *)urb->context;
724 struct tty_struct *tty;
725 int status = urb->status;
727 dbg("%s", __FUNCTION__);
730 dbg("%s - FREE URB %p (outstanding %d)", __FUNCTION__, urb, CmdUrbs);
733 /* clean up the transfer buffer */
734 kfree(urb->transfer_buffer);
736 /* Free the command urb */
740 dbg("%s - nonzero write bulk status received: %d", __FUNCTION__, status);
744 /* Get pointer to tty */
745 tty = edge_port->port->tty;
747 /* tell the tty driver that something has changed */
748 if (tty && edge_port->open)
751 /* we have completed the command */
752 edge_port->commandPending = FALSE;
753 wake_up(&edge_port->wait_command);
757 /*****************************************************************************
758 * Driver tty interface functions
759 *****************************************************************************/
761 /*****************************************************************************
763 * this function is called by the tty driver when a port is opened
764 * If successful, we return 0
765 * Otherwise we return a negative error number.
766 *****************************************************************************/
767 static int edge_open (struct usb_serial_port *port, struct file * filp)
769 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
770 struct usb_serial *serial;
771 struct edgeport_serial *edge_serial;
774 dbg("%s - port %d", __FUNCTION__, port->number);
776 if (edge_port == NULL)
780 port->tty->low_latency = low_latency;
782 /* see if we've set up our endpoint info yet (can't set it up in edge_startup
783 as the structures were not set up at that time.) */
784 serial = port->serial;
785 edge_serial = usb_get_serial_data(serial);
786 if (edge_serial == NULL) {
789 if (edge_serial->interrupt_in_buffer == NULL) {
790 struct usb_serial_port *port0 = serial->port[0];
792 /* not set up yet, so do it now */
793 edge_serial->interrupt_in_buffer = port0->interrupt_in_buffer;
794 edge_serial->interrupt_in_endpoint = port0->interrupt_in_endpointAddress;
795 edge_serial->interrupt_read_urb = port0->interrupt_in_urb;
796 edge_serial->bulk_in_buffer = port0->bulk_in_buffer;
797 edge_serial->bulk_in_endpoint = port0->bulk_in_endpointAddress;
798 edge_serial->read_urb = port0->read_urb;
799 edge_serial->bulk_out_endpoint = port0->bulk_out_endpointAddress;
801 /* set up our interrupt urb */
802 usb_fill_int_urb(edge_serial->interrupt_read_urb,
804 usb_rcvintpipe(serial->dev,
805 port0->interrupt_in_endpointAddress),
806 port0->interrupt_in_buffer,
807 edge_serial->interrupt_read_urb->transfer_buffer_length,
808 edge_interrupt_callback, edge_serial,
809 edge_serial->interrupt_read_urb->interval);
811 /* set up our bulk in urb */
812 usb_fill_bulk_urb(edge_serial->read_urb, serial->dev,
813 usb_rcvbulkpipe(serial->dev,
814 port0->bulk_in_endpointAddress),
815 port0->bulk_in_buffer,
816 edge_serial->read_urb->transfer_buffer_length,
817 edge_bulk_in_callback, edge_serial);
818 edge_serial->read_in_progress = FALSE;
820 /* start interrupt read for this edgeport
821 * this interrupt will continue as long as the edgeport is connected */
822 response = usb_submit_urb (edge_serial->interrupt_read_urb, GFP_KERNEL);
824 dev_err(&port->dev, "%s - Error %d submitting control urb\n", __FUNCTION__, response);
828 /* initialize our wait queues */
829 init_waitqueue_head(&edge_port->wait_open);
830 init_waitqueue_head(&edge_port->wait_chase);
831 init_waitqueue_head(&edge_port->delta_msr_wait);
832 init_waitqueue_head(&edge_port->wait_command);
834 /* initialize our icount structure */
835 memset (&(edge_port->icount), 0x00, sizeof(edge_port->icount));
837 /* initialize our port settings */
838 edge_port->txCredits = 0; /* Can't send any data yet */
839 edge_port->shadowMCR = MCR_MASTER_IE; /* Must always set this bit to enable ints! */
840 edge_port->chaseResponsePending = FALSE;
842 /* send a open port command */
843 edge_port->openPending = TRUE;
844 edge_port->open = FALSE;
845 response = send_iosp_ext_cmd (edge_port, IOSP_CMD_OPEN_PORT, 0);
848 dev_err(&port->dev, "%s - error sending open port command\n", __FUNCTION__);
849 edge_port->openPending = FALSE;
853 /* now wait for the port to be completely opened */
854 wait_event_timeout(edge_port->wait_open, (edge_port->openPending != TRUE), OPEN_TIMEOUT);
856 if (edge_port->open == FALSE) {
858 dbg("%s - open timedout", __FUNCTION__);
859 edge_port->openPending = FALSE;
863 /* create the txfifo */
864 edge_port->txfifo.head = 0;
865 edge_port->txfifo.tail = 0;
866 edge_port->txfifo.count = 0;
867 edge_port->txfifo.size = edge_port->maxTxCredits;
868 edge_port->txfifo.fifo = kmalloc (edge_port->maxTxCredits, GFP_KERNEL);
870 if (!edge_port->txfifo.fifo) {
871 dbg("%s - no memory", __FUNCTION__);
872 edge_close (port, filp);
876 /* Allocate a URB for the write */
877 edge_port->write_urb = usb_alloc_urb (0, GFP_KERNEL);
878 edge_port->write_in_progress = FALSE;
880 if (!edge_port->write_urb) {
881 dbg("%s - no memory", __FUNCTION__);
882 edge_close (port, filp);
886 dbg("%s(%d) - Initialize TX fifo to %d bytes", __FUNCTION__, port->number, edge_port->maxTxCredits);
888 dbg("%s exited", __FUNCTION__);
894 /************************************************************************
896 * block_until_chase_response
898 * This function will block the close until one of the following:
899 * 1. Response to our Chase comes from Edgeport
900 * 2. A timout of 10 seconds without activity has expired
901 * (1K of Edgeport data @ 2400 baud ==> 4 sec to empty)
903 ************************************************************************/
904 static void block_until_chase_response(struct edgeport_port *edge_port)
913 lastCredits = edge_port->txCredits;
915 // Did we get our Chase response
916 if (edge_port->chaseResponsePending == FALSE) {
917 dbg("%s - Got Chase Response", __FUNCTION__);
919 // did we get all of our credit back?
920 if (edge_port->txCredits == edge_port->maxTxCredits ) {
921 dbg("%s - Got all credits", __FUNCTION__);
926 // Block the thread for a while
927 prepare_to_wait(&edge_port->wait_chase, &wait, TASK_UNINTERRUPTIBLE);
928 schedule_timeout(timeout);
929 finish_wait(&edge_port->wait_chase, &wait);
931 if (lastCredits == edge_port->txCredits) {
932 // No activity.. count down.
935 edge_port->chaseResponsePending = FALSE;
936 dbg("%s - Chase TIMEOUT", __FUNCTION__);
940 // Reset timout value back to 10 seconds
941 dbg("%s - Last %d, Current %d", __FUNCTION__, lastCredits, edge_port->txCredits);
948 /************************************************************************
950 * block_until_tx_empty
952 * This function will block the close until one of the following:
954 * 2. The edgeport has stopped
955 * 3. A timout of 3 seconds without activity has expired
957 ************************************************************************/
958 static void block_until_tx_empty (struct edgeport_port *edge_port)
961 struct TxFifo *fifo = &edge_port->txfifo;
968 lastCount = fifo->count;
970 // Is the Edgeport Buffer empty?
971 if (lastCount == 0) {
972 dbg("%s - TX Buffer Empty", __FUNCTION__);
976 // Block the thread for a while
977 prepare_to_wait (&edge_port->wait_chase, &wait, TASK_UNINTERRUPTIBLE);
978 schedule_timeout(timeout);
979 finish_wait(&edge_port->wait_chase, &wait);
981 dbg("%s wait", __FUNCTION__);
983 if (lastCount == fifo->count) {
984 // No activity.. count down.
987 dbg("%s - TIMEOUT", __FUNCTION__);
991 // Reset timout value back to seconds
998 /*****************************************************************************
1000 * this function is called by the tty driver when a port is closed
1001 *****************************************************************************/
1002 static void edge_close (struct usb_serial_port *port, struct file * filp)
1004 struct edgeport_serial *edge_serial;
1005 struct edgeport_port *edge_port;
1008 dbg("%s - port %d", __FUNCTION__, port->number);
1010 edge_serial = usb_get_serial_data(port->serial);
1011 edge_port = usb_get_serial_port_data(port);
1012 if ((edge_serial == NULL) || (edge_port == NULL))
1015 // block until tx is empty
1016 block_until_tx_empty(edge_port);
1018 edge_port->closePending = TRUE;
1020 /* flush and chase */
1021 edge_port->chaseResponsePending = TRUE;
1023 dbg("%s - Sending IOSP_CMD_CHASE_PORT", __FUNCTION__);
1024 status = send_iosp_ext_cmd (edge_port, IOSP_CMD_CHASE_PORT, 0);
1026 // block until chase finished
1027 block_until_chase_response(edge_port);
1029 edge_port->chaseResponsePending = FALSE;
1032 /* close the port */
1033 dbg("%s - Sending IOSP_CMD_CLOSE_PORT", __FUNCTION__);
1034 send_iosp_ext_cmd (edge_port, IOSP_CMD_CLOSE_PORT, 0);
1036 //port->close = TRUE;
1037 edge_port->closePending = FALSE;
1038 edge_port->open = FALSE;
1039 edge_port->openPending = FALSE;
1041 usb_kill_urb(edge_port->write_urb);
1043 if (edge_port->write_urb) {
1044 /* if this urb had a transfer buffer already (old transfer) free it */
1045 kfree(edge_port->write_urb->transfer_buffer);
1046 usb_free_urb(edge_port->write_urb);
1047 edge_port->write_urb = NULL;
1049 kfree(edge_port->txfifo.fifo);
1050 edge_port->txfifo.fifo = NULL;
1052 dbg("%s exited", __FUNCTION__);
1055 /*****************************************************************************
1057 * this function is called by the tty driver when data should be written to
1059 * If successful, we return the number of bytes written, otherwise we return
1060 * a negative error number.
1061 *****************************************************************************/
1062 static int edge_write (struct usb_serial_port *port, const unsigned char *data, int count)
1064 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1065 struct TxFifo *fifo;
1070 unsigned long flags;
1072 dbg("%s - port %d", __FUNCTION__, port->number);
1074 if (edge_port == NULL)
1077 // get a pointer to the Tx fifo
1078 fifo = &edge_port->txfifo;
1080 spin_lock_irqsave(&edge_port->ep_lock, flags);
1082 // calculate number of bytes to put in fifo
1083 copySize = min ((unsigned int)count, (edge_port->txCredits - fifo->count));
1085 dbg("%s(%d) of %d byte(s) Fifo room %d -- will copy %d bytes", __FUNCTION__,
1086 port->number, count, edge_port->txCredits - fifo->count, copySize);
1088 /* catch writes of 0 bytes which the tty driver likes to give us, and when txCredits is empty */
1089 if (copySize == 0) {
1090 dbg("%s - copySize = Zero", __FUNCTION__);
1095 // since we can never overflow the buffer we do not have to check for full condition
1097 // the copy is done is two parts -- first fill to the end of the buffer
1098 // then copy the reset from the start of the buffer
1100 bytesleft = fifo->size - fifo->head;
1101 firsthalf = min (bytesleft, copySize);
1102 dbg("%s - copy %d bytes of %d into fifo ", __FUNCTION__, firsthalf, bytesleft);
1104 /* now copy our data */
1105 memcpy(&fifo->fifo[fifo->head], data, firsthalf);
1106 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, firsthalf, &fifo->fifo[fifo->head]);
1108 // update the index and size
1109 fifo->head += firsthalf;
1110 fifo->count += firsthalf;
1113 if (fifo->head == fifo->size) {
1117 secondhalf = copySize-firsthalf;
1120 dbg("%s - copy rest of data %d", __FUNCTION__, secondhalf);
1121 memcpy(&fifo->fifo[fifo->head], &data[firsthalf], secondhalf);
1122 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, secondhalf, &fifo->fifo[fifo->head]);
1123 // update the index and size
1124 fifo->count += secondhalf;
1125 fifo->head += secondhalf;
1126 // No need to check for wrap since we can not get to end of fifo in this part
1130 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1132 send_more_port_data((struct edgeport_serial *)usb_get_serial_data(port->serial), edge_port);
1134 dbg("%s wrote %d byte(s) TxCredits %d, Fifo %d", __FUNCTION__, copySize, edge_port->txCredits, fifo->count);
1140 /************************************************************************
1142 * send_more_port_data()
1144 * This routine attempts to write additional UART transmit data
1145 * to a port over the USB bulk pipe. It is called (1) when new
1146 * data has been written to a port's TxBuffer from higher layers
1147 * (2) when the peripheral sends us additional TxCredits indicating
1148 * that it can accept more Tx data for a given port; and (3) when
1149 * a bulk write completes successfully and we want to see if we
1150 * can transmit more.
1152 ************************************************************************/
1153 static void send_more_port_data(struct edgeport_serial *edge_serial, struct edgeport_port *edge_port)
1155 struct TxFifo *fifo = &edge_port->txfifo;
1157 unsigned char *buffer;
1163 unsigned long flags;
1165 dbg("%s(%d)", __FUNCTION__, edge_port->port->number);
1167 spin_lock_irqsave(&edge_port->ep_lock, flags);
1169 if (edge_port->write_in_progress ||
1171 (fifo->count == 0)) {
1172 dbg("%s(%d) EXIT - fifo %d, PendingWrite = %d", __FUNCTION__, edge_port->port->number, fifo->count, edge_port->write_in_progress);
1176 // since the amount of data in the fifo will always fit into the
1177 // edgeport buffer we do not need to check the write length
1179 // Do we have enough credits for this port to make it worthwhile
1180 // to bother queueing a write. If it's too small, say a few bytes,
1181 // it's better to wait for more credits so we can do a larger
1183 if (edge_port->txCredits < EDGE_FW_GET_TX_CREDITS_SEND_THRESHOLD(edge_port->maxTxCredits,EDGE_FW_BULK_MAX_PACKET_SIZE)) {
1184 dbg("%s(%d) Not enough credit - fifo %d TxCredit %d", __FUNCTION__, edge_port->port->number, fifo->count, edge_port->txCredits );
1189 edge_port->write_in_progress = TRUE;
1191 // get a pointer to the write_urb
1192 urb = edge_port->write_urb;
1194 /* make sure transfer buffer is freed */
1195 kfree(urb->transfer_buffer);
1196 urb->transfer_buffer = NULL;
1198 /* build the data header for the buffer and port that we are about to send out */
1199 count = fifo->count;
1200 buffer = kmalloc (count+2, GFP_ATOMIC);
1201 if (buffer == NULL) {
1202 dev_err(&edge_port->port->dev, "%s - no more kernel memory...\n", __FUNCTION__);
1203 edge_port->write_in_progress = FALSE;
1206 buffer[0] = IOSP_BUILD_DATA_HDR1 (edge_port->port->number - edge_port->port->serial->minor, count);
1207 buffer[1] = IOSP_BUILD_DATA_HDR2 (edge_port->port->number - edge_port->port->serial->minor, count);
1209 /* now copy our data */
1210 bytesleft = fifo->size - fifo->tail;
1211 firsthalf = min (bytesleft, count);
1212 memcpy(&buffer[2], &fifo->fifo[fifo->tail], firsthalf);
1213 fifo->tail += firsthalf;
1214 fifo->count -= firsthalf;
1215 if (fifo->tail == fifo->size) {
1219 secondhalf = count-firsthalf;
1221 memcpy(&buffer[2+firsthalf], &fifo->fifo[fifo->tail], secondhalf);
1222 fifo->tail += secondhalf;
1223 fifo->count -= secondhalf;
1227 usb_serial_debug_data(debug, &edge_port->port->dev, __FUNCTION__, count, &buffer[2]);
1229 /* fill up the urb with all of our data and submit it */
1230 usb_fill_bulk_urb (urb, edge_serial->serial->dev,
1231 usb_sndbulkpipe(edge_serial->serial->dev, edge_serial->bulk_out_endpoint),
1232 buffer, count+2, edge_bulk_out_data_callback, edge_port);
1234 /* decrement the number of credits we have by the number we just sent */
1235 edge_port->txCredits -= count;
1236 edge_port->icount.tx += count;
1238 urb->dev = edge_serial->serial->dev;
1239 status = usb_submit_urb(urb, GFP_ATOMIC);
1241 /* something went wrong */
1242 dev_err(&edge_port->port->dev, "%s - usb_submit_urb(write bulk) failed, status = %d, data lost\n", __FUNCTION__, status);
1243 edge_port->write_in_progress = FALSE;
1245 /* revert the credits as something bad happened. */
1246 edge_port->txCredits += count;
1247 edge_port->icount.tx -= count;
1249 dbg("%s wrote %d byte(s) TxCredit %d, Fifo %d", __FUNCTION__, count, edge_port->txCredits, fifo->count);
1252 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1256 /*****************************************************************************
1258 * this function is called by the tty driver when it wants to know how many
1259 * bytes of data we can accept for a specific port.
1260 * If successful, we return the amount of room that we have for this port
1262 * Otherwise we return a negative error number.
1263 *****************************************************************************/
1264 static int edge_write_room (struct usb_serial_port *port)
1266 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1268 unsigned long flags;
1270 dbg("%s", __FUNCTION__);
1272 if (edge_port == NULL)
1274 if (edge_port->closePending == TRUE)
1277 dbg("%s - port %d", __FUNCTION__, port->number);
1279 if (!edge_port->open) {
1280 dbg("%s - port not opened", __FUNCTION__);
1284 // total of both buffers is still txCredit
1285 spin_lock_irqsave(&edge_port->ep_lock, flags);
1286 room = edge_port->txCredits - edge_port->txfifo.count;
1287 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1289 dbg("%s - returns %d", __FUNCTION__, room);
1294 /*****************************************************************************
1295 * edge_chars_in_buffer
1296 * this function is called by the tty driver when it wants to know how many
1297 * bytes of data we currently have outstanding in the port (data that has
1298 * been written, but hasn't made it out the port yet)
1299 * If successful, we return the number of bytes left to be written in the
1301 * Otherwise we return a negative error number.
1302 *****************************************************************************/
1303 static int edge_chars_in_buffer (struct usb_serial_port *port)
1305 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1307 unsigned long flags;
1309 dbg("%s", __FUNCTION__);
1311 if (edge_port == NULL)
1313 if (edge_port->closePending == TRUE)
1316 if (!edge_port->open) {
1317 dbg("%s - port not opened", __FUNCTION__);
1321 spin_lock_irqsave(&edge_port->ep_lock, flags);
1322 num_chars = edge_port->maxTxCredits - edge_port->txCredits + edge_port->txfifo.count;
1323 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1325 dbg("%s(port %d) - returns %d", __FUNCTION__, port->number, num_chars);
1332 /*****************************************************************************
1334 * this function is called by the tty driver when it wants to stop the data
1335 * being read from the port.
1336 *****************************************************************************/
1337 static void edge_throttle (struct usb_serial_port *port)
1339 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1340 struct tty_struct *tty;
1343 dbg("%s - port %d", __FUNCTION__, port->number);
1345 if (edge_port == NULL)
1348 if (!edge_port->open) {
1349 dbg("%s - port not opened", __FUNCTION__);
1355 dbg ("%s - no tty available", __FUNCTION__);
1359 /* if we are implementing XON/XOFF, send the stop character */
1361 unsigned char stop_char = STOP_CHAR(tty);
1362 status = edge_write (port, &stop_char, 1);
1368 /* if we are implementing RTS/CTS, toggle that line */
1369 if (tty->termios->c_cflag & CRTSCTS) {
1370 edge_port->shadowMCR &= ~MCR_RTS;
1371 status = send_cmd_write_uart_register(edge_port, MCR, edge_port->shadowMCR);
1381 /*****************************************************************************
1383 * this function is called by the tty driver when it wants to resume the data
1384 * being read from the port (called after SerialThrottle is called)
1385 *****************************************************************************/
1386 static void edge_unthrottle (struct usb_serial_port *port)
1388 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1389 struct tty_struct *tty;
1392 dbg("%s - port %d", __FUNCTION__, port->number);
1394 if (edge_port == NULL)
1397 if (!edge_port->open) {
1398 dbg("%s - port not opened", __FUNCTION__);
1404 dbg ("%s - no tty available", __FUNCTION__);
1408 /* if we are implementing XON/XOFF, send the start character */
1410 unsigned char start_char = START_CHAR(tty);
1411 status = edge_write (port, &start_char, 1);
1417 /* if we are implementing RTS/CTS, toggle that line */
1418 if (tty->termios->c_cflag & CRTSCTS) {
1419 edge_port->shadowMCR |= MCR_RTS;
1420 status = send_cmd_write_uart_register(edge_port, MCR, edge_port->shadowMCR);
1430 /*****************************************************************************
1432 * this function is called by the tty driver when it wants to change the termios structure
1433 *****************************************************************************/
1434 static void edge_set_termios (struct usb_serial_port *port, struct ktermios *old_termios)
1436 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1437 struct tty_struct *tty = port->tty;
1440 if (!port->tty || !port->tty->termios) {
1441 dbg ("%s - no tty or termios", __FUNCTION__);
1445 cflag = tty->termios->c_cflag;
1446 /* check that they really want us to change something */
1448 if (cflag == old_termios->c_cflag &&
1449 tty->termios->c_iflag == old_termios->c_iflag) {
1450 dbg("%s - nothing to change", __FUNCTION__);
1455 dbg("%s - clfag %08x iflag %08x", __FUNCTION__,
1456 tty->termios->c_cflag, tty->termios->c_iflag);
1458 dbg("%s - old clfag %08x old iflag %08x", __FUNCTION__,
1459 old_termios->c_cflag, old_termios->c_iflag);
1462 dbg("%s - port %d", __FUNCTION__, port->number);
1464 if (edge_port == NULL)
1467 if (!edge_port->open) {
1468 dbg("%s - port not opened", __FUNCTION__);
1472 /* change the port settings to the new ones specified */
1473 change_port_settings (edge_port, old_termios);
1479 /*****************************************************************************
1480 * get_lsr_info - get line status register info
1482 * Purpose: Let user call ioctl() to get info when the UART physically
1483 * is emptied. On bus types like RS485, the transmitter must
1484 * release the bus after transmitting. This must be done when
1485 * the transmit shift register is empty, not be done when the
1486 * transmit holding register is empty. This functionality
1487 * allows an RS485 driver to be written in user space.
1488 *****************************************************************************/
1489 static int get_lsr_info(struct edgeport_port *edge_port, unsigned int __user *value)
1491 unsigned int result = 0;
1492 unsigned long flags;
1494 spin_lock_irqsave(&edge_port->ep_lock, flags);
1495 if (edge_port->maxTxCredits == edge_port->txCredits &&
1496 edge_port->txfifo.count == 0) {
1497 dbg("%s -- Empty", __FUNCTION__);
1498 result = TIOCSER_TEMT;
1500 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1502 if (copy_to_user(value, &result, sizeof(int)))
1507 static int get_number_bytes_avail(struct edgeport_port *edge_port, unsigned int __user *value)
1509 unsigned int result = 0;
1510 struct tty_struct *tty = edge_port->port->tty;
1513 return -ENOIOCTLCMD;
1515 result = tty->read_cnt;
1517 dbg("%s(%d) = %d", __FUNCTION__, edge_port->port->number, result);
1518 if (copy_to_user(value, &result, sizeof(int)))
1521 return -ENOIOCTLCMD;
1524 static int edge_tiocmset (struct usb_serial_port *port, struct file *file, unsigned int set, unsigned int clear)
1526 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1529 dbg("%s - port %d", __FUNCTION__, port->number);
1531 mcr = edge_port->shadowMCR;
1532 if (set & TIOCM_RTS)
1534 if (set & TIOCM_DTR)
1536 if (set & TIOCM_LOOP)
1537 mcr |= MCR_LOOPBACK;
1539 if (clear & TIOCM_RTS)
1541 if (clear & TIOCM_DTR)
1543 if (clear & TIOCM_LOOP)
1544 mcr &= ~MCR_LOOPBACK;
1546 edge_port->shadowMCR = mcr;
1548 send_cmd_write_uart_register(edge_port, MCR, edge_port->shadowMCR);
1553 static int edge_tiocmget(struct usb_serial_port *port, struct file *file)
1555 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1556 unsigned int result = 0;
1560 dbg("%s - port %d", __FUNCTION__, 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", __FUNCTION__, 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;
1595 // tmp.custom_divisor = state->custom_divisor;
1596 // tmp.hub6 = state->hub6;
1597 // tmp.io_type = state->io_type;
1599 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1606 /*****************************************************************************
1608 * this function handles any ioctl calls to the driver
1609 *****************************************************************************/
1610 static int edge_ioctl (struct usb_serial_port *port, struct file *file, unsigned int cmd, unsigned long arg)
1613 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1614 struct async_icount cnow;
1615 struct async_icount cprev;
1616 struct serial_icounter_struct icount;
1618 dbg("%s - port %d, cmd = 0x%x", __FUNCTION__, port->number, cmd);
1621 // return number of bytes available
1623 dbg("%s (%d) TIOCINQ", __FUNCTION__, port->number);
1624 return get_number_bytes_avail(edge_port, (unsigned int __user *) arg);
1628 dbg("%s (%d) TIOCSERGETLSR", __FUNCTION__, port->number);
1629 return get_lsr_info(edge_port, (unsigned int __user *) arg);
1633 dbg("%s (%d) TIOCGSERIAL", __FUNCTION__, port->number);
1634 return get_serial_info(edge_port, (struct serial_struct __user *) arg);
1637 dbg("%s (%d) TIOCSSERIAL", __FUNCTION__, port->number);
1641 dbg("%s (%d) TIOCMIWAIT", __FUNCTION__, port->number);
1642 cprev = edge_port->icount;
1644 prepare_to_wait(&edge_port->delta_msr_wait, &wait, TASK_INTERRUPTIBLE);
1646 finish_wait(&edge_port->delta_msr_wait, &wait);
1647 /* see if a signal did it */
1648 if (signal_pending(current))
1649 return -ERESTARTSYS;
1650 cnow = edge_port->icount;
1651 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
1652 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
1653 return -EIO; /* no change => error */
1654 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1655 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1656 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1657 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)) ) {
1666 cnow = edge_port->icount;
1667 memset(&icount, 0, sizeof(icount));
1668 icount.cts = cnow.cts;
1669 icount.dsr = cnow.dsr;
1670 icount.rng = cnow.rng;
1671 icount.dcd = cnow.dcd;
1672 icount.rx = cnow.rx;
1673 icount.tx = cnow.tx;
1674 icount.frame = cnow.frame;
1675 icount.overrun = cnow.overrun;
1676 icount.parity = cnow.parity;
1677 icount.brk = cnow.brk;
1678 icount.buf_overrun = cnow.buf_overrun;
1680 dbg("%s (%d) TIOCGICOUNT RX=%d, TX=%d", __FUNCTION__, port->number, icount.rx, icount.tx );
1681 if (copy_to_user((void __user *)arg, &icount, sizeof(icount)))
1686 return -ENOIOCTLCMD;
1690 /*****************************************************************************
1692 * this function sends a break to the port
1693 *****************************************************************************/
1694 static void edge_break (struct usb_serial_port *port, int break_state)
1696 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1699 /* flush and chase */
1700 edge_port->chaseResponsePending = TRUE;
1702 dbg("%s - Sending IOSP_CMD_CHASE_PORT", __FUNCTION__);
1703 status = send_iosp_ext_cmd (edge_port, IOSP_CMD_CHASE_PORT, 0);
1705 // block until chase finished
1706 block_until_chase_response(edge_port);
1708 edge_port->chaseResponsePending = FALSE;
1711 if (break_state == -1) {
1712 dbg("%s - Sending IOSP_CMD_SET_BREAK", __FUNCTION__);
1713 status = send_iosp_ext_cmd (edge_port, IOSP_CMD_SET_BREAK, 0);
1715 dbg("%s - Sending IOSP_CMD_CLEAR_BREAK", __FUNCTION__);
1716 status = send_iosp_ext_cmd (edge_port, IOSP_CMD_CLEAR_BREAK, 0);
1719 dbg("%s - error sending break set/clear command.", __FUNCTION__);
1726 /*****************************************************************************
1728 * this function handles the data received on the bulk in pipe.
1729 *****************************************************************************/
1730 static void process_rcvd_data (struct edgeport_serial *edge_serial, unsigned char * buffer, __u16 bufferLength)
1732 struct usb_serial_port *port;
1733 struct edgeport_port *edge_port;
1734 struct tty_struct *tty;
1735 __u16 lastBufferLength;
1738 dbg("%s", __FUNCTION__);
1740 lastBufferLength = bufferLength + 1;
1742 while (bufferLength > 0) {
1743 /* failsafe incase we get a message that we don't understand */
1744 if (lastBufferLength == bufferLength) {
1745 dbg("%s - stuck in loop, exiting it.", __FUNCTION__);
1748 lastBufferLength = bufferLength;
1750 switch (edge_serial->rxState) {
1752 edge_serial->rxHeader1 = *buffer;
1756 if (bufferLength == 0) {
1757 edge_serial->rxState = EXPECT_HDR2;
1760 /* otherwise, drop on through */
1763 edge_serial->rxHeader2 = *buffer;
1767 dbg("%s - Hdr1=%02X Hdr2=%02X", __FUNCTION__, edge_serial->rxHeader1, edge_serial->rxHeader2);
1769 // Process depending on whether this header is
1772 if (IS_CMD_STAT_HDR(edge_serial->rxHeader1)) {
1773 // Decode this status header and goto EXPECT_HDR1 (if we
1774 // can process the status with only 2 bytes), or goto
1775 // EXPECT_HDR3 to get the third byte.
1777 edge_serial->rxPort = IOSP_GET_HDR_PORT(edge_serial->rxHeader1);
1778 edge_serial->rxStatusCode = IOSP_GET_STATUS_CODE(edge_serial->rxHeader1);
1780 if (!IOSP_STATUS_IS_2BYTE(edge_serial->rxStatusCode)) {
1781 // This status needs additional bytes. Save what we have
1782 // and then wait for more data.
1783 edge_serial->rxStatusParam = edge_serial->rxHeader2;
1785 edge_serial->rxState = EXPECT_HDR3;
1789 // We have all the header bytes, process the status now
1790 process_rcvd_status (edge_serial, edge_serial->rxHeader2, 0);
1791 edge_serial->rxState = EXPECT_HDR1;
1794 edge_serial->rxPort = IOSP_GET_HDR_PORT(edge_serial->rxHeader1);
1795 edge_serial->rxBytesRemaining = IOSP_GET_HDR_DATA_LEN(edge_serial->rxHeader1, edge_serial->rxHeader2);
1797 dbg("%s - Data for Port %u Len %u", __FUNCTION__, edge_serial->rxPort, edge_serial->rxBytesRemaining);
1799 //ASSERT( DevExt->RxPort < DevExt->NumPorts );
1800 //ASSERT( DevExt->RxBytesRemaining < IOSP_MAX_DATA_LENGTH );
1802 if (bufferLength == 0 ) {
1803 edge_serial->rxState = EXPECT_DATA;
1806 // Else, drop through
1809 case EXPECT_DATA: // Expect data
1811 if (bufferLength < edge_serial->rxBytesRemaining) {
1812 rxLen = bufferLength;
1813 edge_serial->rxState = EXPECT_DATA; // Expect data to start next buffer
1815 // BufLen >= RxBytesRemaining
1816 rxLen = edge_serial->rxBytesRemaining;
1817 edge_serial->rxState = EXPECT_HDR1; // Start another header next time
1820 bufferLength -= rxLen;
1821 edge_serial->rxBytesRemaining -= rxLen;
1823 /* spit this data back into the tty driver if this port is open */
1825 port = edge_serial->serial->port[edge_serial->rxPort];
1826 edge_port = usb_get_serial_port_data(port);
1827 if (edge_port->open) {
1828 tty = edge_port->port->tty;
1830 dbg("%s - Sending %d bytes to TTY for port %d", __FUNCTION__, rxLen, edge_serial->rxPort);
1831 edge_tty_recv(&edge_serial->serial->dev->dev, tty, buffer, rxLen);
1833 edge_port->icount.rx += rxLen;
1840 case EXPECT_HDR3: // Expect 3rd byte of status header
1841 edge_serial->rxHeader3 = *buffer;
1845 // We have all the header bytes, process the status now
1846 process_rcvd_status (edge_serial, edge_serial->rxStatusParam, edge_serial->rxHeader3);
1847 edge_serial->rxState = EXPECT_HDR1;
1855 /*****************************************************************************
1856 * process_rcvd_status
1857 * this function handles the any status messages received on the bulk in pipe.
1858 *****************************************************************************/
1859 static void process_rcvd_status (struct edgeport_serial *edge_serial, __u8 byte2, __u8 byte3)
1861 struct usb_serial_port *port;
1862 struct edgeport_port *edge_port;
1863 __u8 code = edge_serial->rxStatusCode;
1865 /* switch the port pointer to the one being currently talked about */
1866 port = edge_serial->serial->port[edge_serial->rxPort];
1867 edge_port = usb_get_serial_port_data(port);
1868 if (edge_port == NULL) {
1869 dev_err(&edge_serial->serial->dev->dev, "%s - edge_port == NULL for port %d\n", __FUNCTION__, edge_serial->rxPort);
1873 dbg("%s - port %d", __FUNCTION__, edge_serial->rxPort);
1875 if (code == IOSP_EXT_STATUS) {
1877 case IOSP_EXT_STATUS_CHASE_RSP:
1878 // we want to do EXT status regardless of port open/closed
1879 dbg("%s - Port %u EXT CHASE_RSP Data = %02x", __FUNCTION__, edge_serial->rxPort, byte3 );
1880 // Currently, the only EXT_STATUS is Chase, so process here instead of one more call
1881 // to one more subroutine. If/when more EXT_STATUS, there'll be more work to do.
1882 // Also, we currently clear flag and close the port regardless of content of above's Byte3.
1883 // We could choose to do something else when Byte3 says Timeout on Chase from Edgeport,
1884 // like wait longer in block_until_chase_response, but for now we don't.
1885 edge_port->chaseResponsePending = FALSE;
1886 wake_up (&edge_port->wait_chase);
1889 case IOSP_EXT_STATUS_RX_CHECK_RSP:
1890 dbg("%s ========== Port %u CHECK_RSP Sequence = %02x =============\n", __FUNCTION__, edge_serial->rxPort, byte3 );
1891 //Port->RxCheckRsp = TRUE;
1896 if (code == IOSP_STATUS_OPEN_RSP) {
1897 edge_port->txCredits = GET_TX_BUFFER_SIZE(byte3);
1898 edge_port->maxTxCredits = edge_port->txCredits;
1899 dbg("%s - Port %u Open Response Inital MSR = %02x TxBufferSize = %d", __FUNCTION__, edge_serial->rxPort, byte2, edge_port->txCredits);
1900 handle_new_msr (edge_port, byte2);
1902 /* send the current line settings to the port so we are in sync with any further termios calls */
1903 if (edge_port->port->tty)
1904 change_port_settings (edge_port, edge_port->port->tty->termios);
1906 /* we have completed the open */
1907 edge_port->openPending = FALSE;
1908 edge_port->open = TRUE;
1909 wake_up(&edge_port->wait_open);
1913 // If port is closed, silently discard all rcvd status. We can
1914 // have cases where buffered status is received AFTER the close
1915 // port command is sent to the Edgeport.
1916 if ((!edge_port->open ) || (edge_port->closePending)) {
1921 // Not currently sent by Edgeport
1922 case IOSP_STATUS_LSR:
1923 dbg("%s - Port %u LSR Status = %02x", __FUNCTION__, edge_serial->rxPort, byte2);
1924 handle_new_lsr (edge_port, FALSE, byte2, 0);
1927 case IOSP_STATUS_LSR_DATA:
1928 dbg("%s - Port %u LSR Status = %02x, Data = %02x", __FUNCTION__, edge_serial->rxPort, byte2, byte3);
1929 // byte2 is LSR Register
1930 // byte3 is broken data byte
1931 handle_new_lsr (edge_port, TRUE, byte2, byte3);
1934 // case IOSP_EXT_4_STATUS:
1935 // dbg("%s - Port %u LSR Status = %02x Data = %02x", __FUNCTION__, edge_serial->rxPort, byte2, byte3);
1938 case IOSP_STATUS_MSR:
1939 dbg("%s - Port %u MSR Status = %02x", __FUNCTION__, edge_serial->rxPort, byte2);
1941 // Process this new modem status and generate appropriate
1942 // events, etc, based on the new status. This routine
1943 // also saves the MSR in Port->ShadowMsr.
1944 handle_new_msr(edge_port, byte2);
1948 dbg("%s - Unrecognized IOSP status code %u\n", __FUNCTION__, code);
1956 /*****************************************************************************
1958 * this function passes data on to the tty flip buffer
1959 *****************************************************************************/
1960 static void edge_tty_recv(struct device *dev, struct tty_struct *tty, unsigned char *data, int length)
1965 cnt = tty_buffer_request_room(tty, length);
1967 dev_err(dev, "%s - dropping data, %d bytes lost\n",
1968 __FUNCTION__, length - cnt);
1972 tty_insert_flip_string(tty, data, cnt);
1975 } while (length > 0);
1977 tty_flip_buffer_push(tty);
1981 /*****************************************************************************
1983 * this function handles any change to the msr register for a port.
1984 *****************************************************************************/
1985 static void handle_new_msr(struct edgeport_port *edge_port, __u8 newMsr)
1987 struct async_icount *icount;
1989 dbg("%s %02x", __FUNCTION__, newMsr);
1991 if (newMsr & (EDGEPORT_MSR_DELTA_CTS | EDGEPORT_MSR_DELTA_DSR | EDGEPORT_MSR_DELTA_RI | EDGEPORT_MSR_DELTA_CD)) {
1992 icount = &edge_port->icount;
1994 /* update input line counters */
1995 if (newMsr & EDGEPORT_MSR_DELTA_CTS) {
1998 if (newMsr & EDGEPORT_MSR_DELTA_DSR) {
2001 if (newMsr & EDGEPORT_MSR_DELTA_CD) {
2004 if (newMsr & EDGEPORT_MSR_DELTA_RI) {
2007 wake_up_interruptible(&edge_port->delta_msr_wait);
2010 /* Save the new modem status */
2011 edge_port->shadowMSR = newMsr & 0xf0;
2017 /*****************************************************************************
2019 * this function handles any change to the lsr register for a port.
2020 *****************************************************************************/
2021 static void handle_new_lsr(struct edgeport_port *edge_port, __u8 lsrData, __u8 lsr, __u8 data)
2023 __u8 newLsr = (__u8)(lsr & (__u8)(LSR_OVER_ERR | LSR_PAR_ERR | LSR_FRM_ERR | LSR_BREAK));
2024 struct async_icount *icount;
2026 dbg("%s - %02x", __FUNCTION__, newLsr);
2028 edge_port->shadowLSR = lsr;
2030 if (newLsr & LSR_BREAK) {
2032 // Parity and Framing errors only count if they
2033 // occur exclusive of a break being
2036 newLsr &= (__u8)(LSR_OVER_ERR | LSR_BREAK);
2039 /* Place LSR data byte into Rx buffer */
2040 if (lsrData && edge_port->port->tty)
2041 edge_tty_recv(&edge_port->port->dev, edge_port->port->tty, &data, 1);
2043 /* update input line counters */
2044 icount = &edge_port->icount;
2045 if (newLsr & LSR_BREAK) {
2048 if (newLsr & LSR_OVER_ERR) {
2051 if (newLsr & LSR_PAR_ERR) {
2054 if (newLsr & LSR_FRM_ERR) {
2062 /****************************************************************************
2064 * writes a number of bytes to the Edgeport device's sram starting at the
2066 * If successful returns the number of bytes written, otherwise it returns
2067 * a negative error number of the problem.
2068 ****************************************************************************/
2069 static int sram_write (struct usb_serial *serial, __u16 extAddr, __u16 addr, __u16 length, __u8 *data)
2072 __u16 current_length;
2073 unsigned char *transfer_buffer;
2075 dbg("%s - %x, %x, %d", __FUNCTION__, extAddr, addr, length);
2077 transfer_buffer = kmalloc (64, GFP_KERNEL);
2078 if (!transfer_buffer) {
2079 dev_err(&serial->dev->dev, "%s - kmalloc(%d) failed.\n", __FUNCTION__, 64);
2083 /* need to split these writes up into 64 byte chunks */
2085 while (length > 0) {
2087 current_length = 64;
2089 current_length = length;
2091 // dbg("%s - writing %x, %x, %d", __FUNCTION__, extAddr, addr, current_length);
2092 memcpy (transfer_buffer, data, current_length);
2093 result = usb_control_msg (serial->dev, usb_sndctrlpipe(serial->dev, 0), USB_REQUEST_ION_WRITE_RAM,
2094 0x40, addr, extAddr, transfer_buffer, current_length, 300);
2097 length -= current_length;
2098 addr += current_length;
2099 data += current_length;
2102 kfree (transfer_buffer);
2107 /****************************************************************************
2109 * writes a number of bytes to the Edgeport device's ROM starting at the
2111 * If successful returns the number of bytes written, otherwise it returns
2112 * a negative error number of the problem.
2113 ****************************************************************************/
2114 static int rom_write (struct usb_serial *serial, __u16 extAddr, __u16 addr, __u16 length, __u8 *data)
2117 __u16 current_length;
2118 unsigned char *transfer_buffer;
2120 // dbg("%s - %x, %x, %d", __FUNCTION__, extAddr, addr, length);
2122 transfer_buffer = kmalloc (64, GFP_KERNEL);
2123 if (!transfer_buffer) {
2124 dev_err(&serial->dev->dev, "%s - kmalloc(%d) failed.\n", __FUNCTION__, 64);
2128 /* need to split these writes up into 64 byte chunks */
2130 while (length > 0) {
2132 current_length = 64;
2134 current_length = length;
2136 // dbg("%s - writing %x, %x, %d", __FUNCTION__, extAddr, addr, current_length);
2137 memcpy (transfer_buffer, data, current_length);
2138 result = usb_control_msg (serial->dev, usb_sndctrlpipe(serial->dev, 0), USB_REQUEST_ION_WRITE_ROM,
2139 0x40, addr, extAddr, transfer_buffer, current_length, 300);
2142 length -= current_length;
2143 addr += current_length;
2144 data += current_length;
2147 kfree (transfer_buffer);
2152 /****************************************************************************
2154 * reads a number of bytes from the Edgeport device starting at the given
2156 * If successful returns the number of bytes read, otherwise it returns
2157 * a negative error number of the problem.
2158 ****************************************************************************/
2159 static int rom_read (struct usb_serial *serial, __u16 extAddr, __u16 addr, __u16 length, __u8 *data)
2162 __u16 current_length;
2163 unsigned char *transfer_buffer;
2165 dbg("%s - %x, %x, %d", __FUNCTION__, extAddr, addr, length);
2167 transfer_buffer = kmalloc (64, GFP_KERNEL);
2168 if (!transfer_buffer) {
2169 dev_err(&serial->dev->dev, "%s - kmalloc(%d) failed.\n", __FUNCTION__, 64);
2173 /* need to split these reads up into 64 byte chunks */
2175 while (length > 0) {
2177 current_length = 64;
2179 current_length = length;
2181 // dbg("%s - %x, %x, %d", __FUNCTION__, extAddr, addr, current_length);
2182 result = usb_control_msg (serial->dev, usb_rcvctrlpipe(serial->dev, 0), USB_REQUEST_ION_READ_ROM,
2183 0xC0, addr, extAddr, transfer_buffer, current_length, 300);
2186 memcpy (data, transfer_buffer, current_length);
2187 length -= current_length;
2188 addr += current_length;
2189 data += current_length;
2192 kfree (transfer_buffer);
2197 /****************************************************************************
2199 * Is used to send a IOSP message to the Edgeport device
2200 ****************************************************************************/
2201 static int send_iosp_ext_cmd (struct edgeport_port *edge_port, __u8 command, __u8 param)
2203 unsigned char *buffer;
2204 unsigned char *currentCommand;
2208 dbg("%s - %d, %d", __FUNCTION__, command, param);
2210 buffer = kmalloc (10, GFP_ATOMIC);
2212 dev_err(&edge_port->port->dev, "%s - kmalloc(%d) failed.\n", __FUNCTION__, 10);
2216 currentCommand = buffer;
2218 MAKE_CMD_EXT_CMD (¤tCommand, &length,
2219 edge_port->port->number - edge_port->port->serial->minor,
2222 status = write_cmd_usb (edge_port, buffer, length);
2224 /* something bad happened, let's free up the memory */
2232 /*****************************************************************************
2234 * this function writes the given buffer out to the bulk write endpoint.
2235 *****************************************************************************/
2236 static int write_cmd_usb (struct edgeport_port *edge_port, unsigned char *buffer, int length)
2238 struct edgeport_serial *edge_serial = usb_get_serial_data(edge_port->port->serial);
2243 usb_serial_debug_data(debug, &edge_port->port->dev, __FUNCTION__, length, buffer);
2245 /* Allocate our next urb */
2246 urb = usb_alloc_urb (0, GFP_ATOMIC);
2251 dbg("%s - ALLOCATE URB %p (outstanding %d)", __FUNCTION__, urb, CmdUrbs);
2253 usb_fill_bulk_urb (urb, edge_serial->serial->dev,
2254 usb_sndbulkpipe(edge_serial->serial->dev, edge_serial->bulk_out_endpoint),
2255 buffer, length, edge_bulk_out_cmd_callback, edge_port);
2257 edge_port->commandPending = TRUE;
2258 status = usb_submit_urb(urb, GFP_ATOMIC);
2261 /* something went wrong */
2262 dev_err(&edge_port->port->dev, "%s - usb_submit_urb(write command) failed, status = %d\n", __FUNCTION__, status);
2269 // wait for command to finish
2270 timeout = COMMAND_TIMEOUT;
2272 wait_event (&edge_port->wait_command, (edge_port->commandPending == FALSE));
2274 if (edge_port->commandPending == TRUE) {
2275 /* command timed out */
2276 dbg("%s - command timed out", __FUNCTION__);
2284 /*****************************************************************************
2285 * send_cmd_write_baud_rate
2286 * this function sends the proper command to change the baud rate of the
2288 *****************************************************************************/
2289 static int send_cmd_write_baud_rate (struct edgeport_port *edge_port, int baudRate)
2291 unsigned char *cmdBuffer;
2292 unsigned char *currCmd;
2296 unsigned char number = edge_port->port->number - edge_port->port->serial->minor;
2298 dbg("%s - port = %d, baud = %d", __FUNCTION__, edge_port->port->number, baudRate);
2300 status = calc_baud_rate_divisor (baudRate, &divisor);
2302 dev_err(&edge_port->port->dev, "%s - bad baud rate\n", __FUNCTION__);
2306 // Alloc memory for the string of commands.
2307 cmdBuffer = kmalloc (0x100, GFP_ATOMIC);
2309 dev_err(&edge_port->port->dev, "%s - kmalloc(%d) failed.\n", __FUNCTION__, 0x100);
2312 currCmd = cmdBuffer;
2314 // Enable access to divisor latch
2315 MAKE_CMD_WRITE_REG( &currCmd, &cmdLen, number, LCR, LCR_DL_ENABLE );
2317 // Write the divisor itself
2318 MAKE_CMD_WRITE_REG( &currCmd, &cmdLen, number, DLL, LOW8 (divisor) );
2319 MAKE_CMD_WRITE_REG( &currCmd, &cmdLen, number, DLM, HIGH8(divisor) );
2321 // Restore original value to disable access to divisor latch
2322 MAKE_CMD_WRITE_REG( &currCmd, &cmdLen, number, LCR, edge_port->shadowLCR);
2324 status = write_cmd_usb(edge_port, cmdBuffer, cmdLen );
2326 /* something bad happened, let's free up the memory */
2334 /*****************************************************************************
2335 * calc_baud_rate_divisor
2336 * this function calculates the proper baud rate divisor for the specified
2338 *****************************************************************************/
2339 static int calc_baud_rate_divisor (int baudrate, int *divisor)
2345 dbg("%s - %d", __FUNCTION__, baudrate);
2347 for (i = 0; i < ARRAY_SIZE(divisor_table); i++) {
2348 if ( divisor_table[i].BaudRate == baudrate ) {
2349 *divisor = divisor_table[i].Divisor;
2354 // We have tried all of the standard baud rates
2355 // lets try to calculate the divisor for this baud rate
2356 // Make sure the baud rate is reasonable
2357 if (baudrate > 50 && baudrate < 230400) {
2359 custom = (__u16)((230400L + baudrate/2) / baudrate);
2363 dbg("%s - Baud %d = %d\n", __FUNCTION__, baudrate, custom);
2371 /*****************************************************************************
2372 * send_cmd_write_uart_register
2373 * this function builds up a uart register message and sends to to the device.
2374 *****************************************************************************/
2375 static int send_cmd_write_uart_register (struct edgeport_port *edge_port, __u8 regNum, __u8 regValue)
2377 unsigned char *cmdBuffer;
2378 unsigned char *currCmd;
2379 unsigned long cmdLen = 0;
2382 dbg("%s - write to %s register 0x%02x", (regNum == MCR) ? "MCR" : "LCR", __FUNCTION__, regValue);
2384 // Alloc memory for the string of commands.
2385 cmdBuffer = kmalloc (0x10, GFP_ATOMIC);
2386 if (cmdBuffer == NULL ) {
2390 currCmd = cmdBuffer;
2392 // Build a cmd in the buffer to write the given register
2393 MAKE_CMD_WRITE_REG (&currCmd, &cmdLen,
2394 edge_port->port->number - edge_port->port->serial->minor,
2397 status = write_cmd_usb(edge_port, cmdBuffer, cmdLen);
2399 /* something bad happened, let's free up the memory */
2407 /*****************************************************************************
2408 * change_port_settings
2409 * This routine is called to set the UART on the device to match the specified
2411 *****************************************************************************/
2415 static void change_port_settings (struct edgeport_port *edge_port, struct ktermios *old_termios)
2417 struct tty_struct *tty;
2428 dbg("%s - port %d", __FUNCTION__, edge_port->port->number);
2430 if ((!edge_port->open) &&
2431 (!edge_port->openPending)) {
2432 dbg("%s - port not opened", __FUNCTION__);
2436 tty = edge_port->port->tty;
2439 dbg("%s - no tty structures", __FUNCTION__);
2443 cflag = tty->termios->c_cflag;
2445 switch (cflag & CSIZE) {
2446 case CS5: lData = LCR_BITS_5; mask = 0x1f; dbg("%s - data bits = 5", __FUNCTION__); break;
2447 case CS6: lData = LCR_BITS_6; mask = 0x3f; dbg("%s - data bits = 6", __FUNCTION__); break;
2448 case CS7: lData = LCR_BITS_7; mask = 0x7f; dbg("%s - data bits = 7", __FUNCTION__); break;
2450 case CS8: lData = LCR_BITS_8; dbg("%s - data bits = 8", __FUNCTION__); break;
2453 lParity = LCR_PAR_NONE;
2454 if (cflag & PARENB) {
2455 if (cflag & CMSPAR) {
2456 if (cflag & PARODD) {
2457 lParity = LCR_PAR_MARK;
2458 dbg("%s - parity = mark", __FUNCTION__);
2460 lParity = LCR_PAR_SPACE;
2461 dbg("%s - parity = space", __FUNCTION__);
2463 } else if (cflag & PARODD) {
2464 lParity = LCR_PAR_ODD;
2465 dbg("%s - parity = odd", __FUNCTION__);
2467 lParity = LCR_PAR_EVEN;
2468 dbg("%s - parity = even", __FUNCTION__);
2471 dbg("%s - parity = none", __FUNCTION__);
2474 if (cflag & CSTOPB) {
2476 dbg("%s - stop bits = 2", __FUNCTION__);
2479 dbg("%s - stop bits = 1", __FUNCTION__);
2482 /* figure out the flow control settings */
2483 rxFlow = txFlow = 0x00;
2484 if (cflag & CRTSCTS) {
2485 rxFlow |= IOSP_RX_FLOW_RTS;
2486 txFlow |= IOSP_TX_FLOW_CTS;
2487 dbg("%s - RTS/CTS is enabled", __FUNCTION__);
2489 dbg("%s - RTS/CTS is disabled", __FUNCTION__);
2492 /* if we are implementing XON/XOFF, set the start and stop character in the device */
2493 if (I_IXOFF(tty) || I_IXON(tty)) {
2494 unsigned char stop_char = STOP_CHAR(tty);
2495 unsigned char start_char = START_CHAR(tty);
2497 send_iosp_ext_cmd (edge_port, IOSP_CMD_SET_XON_CHAR, start_char);
2498 send_iosp_ext_cmd (edge_port, IOSP_CMD_SET_XOFF_CHAR, stop_char);
2500 /* if we are implementing INBOUND XON/XOFF */
2502 rxFlow |= IOSP_RX_FLOW_XON_XOFF;
2503 dbg("%s - INBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x", __FUNCTION__, start_char, stop_char);
2505 dbg("%s - INBOUND XON/XOFF is disabled", __FUNCTION__);
2508 /* if we are implementing OUTBOUND XON/XOFF */
2510 txFlow |= IOSP_TX_FLOW_XON_XOFF;
2511 dbg("%s - OUTBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x", __FUNCTION__, start_char, stop_char);
2513 dbg("%s - OUTBOUND XON/XOFF is disabled", __FUNCTION__);
2517 /* Set flow control to the configured value */
2518 send_iosp_ext_cmd (edge_port, IOSP_CMD_SET_RX_FLOW, rxFlow);
2519 send_iosp_ext_cmd (edge_port, IOSP_CMD_SET_TX_FLOW, txFlow);
2522 edge_port->shadowLCR &= ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
2523 edge_port->shadowLCR |= (lData | lParity | lStop);
2525 edge_port->validDataMask = mask;
2527 /* Send the updated LCR value to the EdgePort */
2528 status = send_cmd_write_uart_register(edge_port, LCR, edge_port->shadowLCR);
2533 /* set up the MCR register and send it to the EdgePort */
2534 edge_port->shadowMCR = MCR_MASTER_IE;
2535 if (cflag & CBAUD) {
2536 edge_port->shadowMCR |= (MCR_DTR | MCR_RTS);
2538 status = send_cmd_write_uart_register(edge_port, MCR, edge_port->shadowMCR);
2543 /* Determine divisor based on baud rate */
2544 baud = tty_get_baud_rate(tty);
2546 /* pick a default, any default... */
2550 dbg("%s - baud rate = %d", __FUNCTION__, baud);
2551 status = send_cmd_write_baud_rate (edge_port, baud);
2557 /****************************************************************************
2559 * Turns a string from Unicode into ASCII.
2560 * Doesn't do a good job with any characters that are outside the normal
2561 * ASCII range, but it's only for debugging...
2562 * NOTE: expects the unicode in LE format
2563 ****************************************************************************/
2564 static void unicode_to_ascii(char *string, int buflen, __le16 *unicode, int unicode_size)
2568 if (buflen <= 0) /* never happens, but... */
2570 --buflen; /* space for nul */
2572 for (i = 0; i < unicode_size; i++) {
2575 string[i] = (char)(le16_to_cpu(unicode[i]));
2581 /****************************************************************************
2582 * get_manufacturing_desc
2583 * reads in the manufacturing descriptor and stores it into the serial
2585 ****************************************************************************/
2586 static void get_manufacturing_desc (struct edgeport_serial *edge_serial)
2590 dbg("getting manufacturer descriptor");
2592 response = rom_read (edge_serial->serial, (EDGE_MANUF_DESC_ADDR & 0xffff0000) >> 16,
2593 (__u16)(EDGE_MANUF_DESC_ADDR & 0x0000ffff), EDGE_MANUF_DESC_LEN,
2594 (__u8 *)(&edge_serial->manuf_descriptor));
2597 dev_err(&edge_serial->serial->dev->dev, "error in getting manufacturer descriptor\n");
2600 dbg("**Manufacturer Descriptor");
2601 dbg(" RomSize: %dK", edge_serial->manuf_descriptor.RomSize);
2602 dbg(" RamSize: %dK", edge_serial->manuf_descriptor.RamSize);
2603 dbg(" CpuRev: %d", edge_serial->manuf_descriptor.CpuRev);
2604 dbg(" BoardRev: %d", edge_serial->manuf_descriptor.BoardRev);
2605 dbg(" NumPorts: %d", edge_serial->manuf_descriptor.NumPorts);
2606 dbg(" DescDate: %d/%d/%d", edge_serial->manuf_descriptor.DescDate[0], edge_serial->manuf_descriptor.DescDate[1], edge_serial->manuf_descriptor.DescDate[2]+1900);
2607 unicode_to_ascii(string, sizeof(string),
2608 edge_serial->manuf_descriptor.SerialNumber,
2609 edge_serial->manuf_descriptor.SerNumLength/2);
2610 dbg(" SerialNumber: %s", string);
2611 unicode_to_ascii(string, sizeof(string),
2612 edge_serial->manuf_descriptor.AssemblyNumber,
2613 edge_serial->manuf_descriptor.AssemblyNumLength/2);
2614 dbg(" AssemblyNumber: %s", string);
2615 unicode_to_ascii(string, sizeof(string),
2616 edge_serial->manuf_descriptor.OemAssyNumber,
2617 edge_serial->manuf_descriptor.OemAssyNumLength/2);
2618 dbg(" OemAssyNumber: %s", string);
2619 dbg(" UartType: %d", edge_serial->manuf_descriptor.UartType);
2620 dbg(" IonPid: %d", edge_serial->manuf_descriptor.IonPid);
2621 dbg(" IonConfig: %d", edge_serial->manuf_descriptor.IonConfig);
2626 /****************************************************************************
2628 * reads in the bootloader descriptor and stores it into the serial
2630 ****************************************************************************/
2631 static void get_boot_desc (struct edgeport_serial *edge_serial)
2635 dbg("getting boot descriptor");
2637 response = rom_read (edge_serial->serial, (EDGE_BOOT_DESC_ADDR & 0xffff0000) >> 16,
2638 (__u16)(EDGE_BOOT_DESC_ADDR & 0x0000ffff), EDGE_BOOT_DESC_LEN,
2639 (__u8 *)(&edge_serial->boot_descriptor));
2642 dev_err(&edge_serial->serial->dev->dev, "error in getting boot descriptor\n");
2644 dbg("**Boot Descriptor:");
2645 dbg(" BootCodeLength: %d", le16_to_cpu(edge_serial->boot_descriptor.BootCodeLength));
2646 dbg(" MajorVersion: %d", edge_serial->boot_descriptor.MajorVersion);
2647 dbg(" MinorVersion: %d", edge_serial->boot_descriptor.MinorVersion);
2648 dbg(" BuildNumber: %d", le16_to_cpu(edge_serial->boot_descriptor.BuildNumber));
2649 dbg(" Capabilities: 0x%x", le16_to_cpu(edge_serial->boot_descriptor.Capabilities));
2650 dbg(" UConfig0: %d", edge_serial->boot_descriptor.UConfig0);
2651 dbg(" UConfig1: %d", edge_serial->boot_descriptor.UConfig1);
2656 /****************************************************************************
2657 * load_application_firmware
2658 * This is called to load the application firmware to the device
2659 ****************************************************************************/
2660 static void load_application_firmware (struct edgeport_serial *edge_serial)
2662 struct edge_firmware_image_record *record;
2663 unsigned char *firmware;
2664 unsigned char *FirmwareImage;
2669 switch (edge_serial->product_info.iDownloadFile) {
2670 case EDGE_DOWNLOAD_FILE_I930:
2671 dbg("downloading firmware version (930) %d.%d.%d",
2672 OperationalCodeImageVersion_GEN1.MajorVersion,
2673 OperationalCodeImageVersion_GEN1.MinorVersion,
2674 OperationalCodeImageVersion_GEN1.BuildNumber);
2675 firmware = &OperationalCodeImage_GEN1[0];
2676 FirmwareImage = &OperationalCodeImage_GEN1[0];
2677 ImageSize = sizeof(OperationalCodeImage_GEN1);
2680 case EDGE_DOWNLOAD_FILE_80251:
2681 dbg("downloading firmware version (80251) %d.%d.%d",
2682 OperationalCodeImageVersion_GEN2.MajorVersion,
2683 OperationalCodeImageVersion_GEN2.MinorVersion,
2684 OperationalCodeImageVersion_GEN2.BuildNumber);
2685 firmware = &OperationalCodeImage_GEN2[0];
2686 FirmwareImage = &OperationalCodeImage_GEN2[0];
2687 ImageSize = sizeof(OperationalCodeImage_GEN2);
2690 case EDGE_DOWNLOAD_FILE_NONE:
2691 dbg ("No download file specified, skipping download\n");
2700 record = (struct edge_firmware_image_record *)firmware;
2701 response = sram_write (edge_serial->serial, le16_to_cpu(record->ExtAddr), le16_to_cpu(record->Addr), le16_to_cpu(record->Len), &record->Data[0]);
2703 dev_err(&edge_serial->serial->dev->dev, "sram_write failed (%x, %x, %d)\n", le16_to_cpu(record->ExtAddr), le16_to_cpu(record->Addr), le16_to_cpu(record->Len));
2706 firmware += sizeof (struct edge_firmware_image_record) + le16_to_cpu(record->Len);
2707 if (firmware >= &FirmwareImage[ImageSize]) {
2712 dbg("sending exec_dl_code");
2713 response = usb_control_msg (edge_serial->serial->dev,
2714 usb_sndctrlpipe(edge_serial->serial->dev, 0),
2715 USB_REQUEST_ION_EXEC_DL_CODE,
2716 0x40, 0x4000, 0x0001, NULL, 0, 3000);
2722 /****************************************************************************
2724 ****************************************************************************/
2725 static int edge_startup (struct usb_serial *serial)
2727 struct edgeport_serial *edge_serial;
2728 struct edgeport_port *edge_port;
2729 struct usb_device *dev;
2734 /* create our private serial structure */
2735 edge_serial = kzalloc(sizeof(struct edgeport_serial), GFP_KERNEL);
2736 if (edge_serial == NULL) {
2737 dev_err(&serial->dev->dev, "%s - Out of memory\n", __FUNCTION__);
2740 spin_lock_init(&edge_serial->es_lock);
2741 edge_serial->serial = serial;
2742 usb_set_serial_data(serial, edge_serial);
2744 /* get the name for the device from the device */
2745 i = get_string(dev, dev->descriptor.iManufacturer,
2746 &edge_serial->name[0], MAX_NAME_LEN+1);
2747 edge_serial->name[i++] = ' ';
2748 get_string(dev, dev->descriptor.iProduct,
2749 &edge_serial->name[i], MAX_NAME_LEN+2 - i);
2751 dev_info(&serial->dev->dev, "%s detected\n", edge_serial->name);
2753 /* get the manufacturing descriptor for this device */
2754 get_manufacturing_desc (edge_serial);
2756 /* get the boot descriptor */
2757 get_boot_desc (edge_serial);
2759 get_product_info(edge_serial);
2761 /* set the number of ports from the manufacturing description */
2762 /* serial->num_ports = serial->product_info.NumPorts; */
2763 if (edge_serial->product_info.NumPorts != serial->num_ports) {
2764 warn("%s - Device Reported %d serial ports vs core "
2765 "thinking we have %d ports, email greg@kroah.com this info.",
2766 __FUNCTION__, edge_serial->product_info.NumPorts,
2770 dbg("%s - time 1 %ld", __FUNCTION__, jiffies);
2772 /* now load the application firmware into this device */
2773 load_application_firmware (edge_serial);
2775 dbg("%s - time 2 %ld", __FUNCTION__, jiffies);
2777 /* Check current Edgeport EEPROM and update if necessary */
2778 update_edgeport_E2PROM (edge_serial);
2780 dbg("%s - time 3 %ld", __FUNCTION__, jiffies);
2782 /* set the configuration to use #1 */
2783 // dbg("set_configuration 1");
2784 // usb_set_configuration (dev, 1);
2786 /* we set up the pointers to the endpoints in the edge_open function,
2787 * as the structures aren't created yet. */
2789 /* set up our port private structures */
2790 for (i = 0; i < serial->num_ports; ++i) {
2791 edge_port = kmalloc (sizeof(struct edgeport_port), GFP_KERNEL);
2792 if (edge_port == NULL) {
2793 dev_err(&serial->dev->dev, "%s - Out of memory\n", __FUNCTION__);
2794 for (j = 0; j < i; ++j) {
2795 kfree (usb_get_serial_port_data(serial->port[j]));
2796 usb_set_serial_port_data(serial->port[j], NULL);
2798 usb_set_serial_data(serial, NULL);
2802 memset (edge_port, 0, sizeof(struct edgeport_port));
2803 spin_lock_init(&edge_port->ep_lock);
2804 edge_port->port = serial->port[i];
2805 usb_set_serial_port_data(serial->port[i], edge_port);
2812 /****************************************************************************
2814 * This function is called whenever the device is removed from the usb bus.
2815 ****************************************************************************/
2816 static void edge_shutdown (struct usb_serial *serial)
2820 dbg("%s", __FUNCTION__);
2822 /* stop reads and writes on all ports */
2823 for (i=0; i < serial->num_ports; ++i) {
2824 kfree (usb_get_serial_port_data(serial->port[i]));
2825 usb_set_serial_port_data(serial->port[i], NULL);
2827 kfree (usb_get_serial_data(serial));
2828 usb_set_serial_data(serial, NULL);
2832 /****************************************************************************
2834 * This is called by the module subsystem, or on startup to initialize us
2835 ****************************************************************************/
2836 static int __init edgeport_init(void)
2840 retval = usb_serial_register(&edgeport_2port_device);
2842 goto failed_2port_device_register;
2843 retval = usb_serial_register(&edgeport_4port_device);
2845 goto failed_4port_device_register;
2846 retval = usb_serial_register(&edgeport_8port_device);
2848 goto failed_8port_device_register;
2849 retval = usb_register(&io_driver);
2851 goto failed_usb_register;
2852 info(DRIVER_DESC " " DRIVER_VERSION);
2855 failed_usb_register:
2856 usb_serial_deregister(&edgeport_8port_device);
2857 failed_8port_device_register:
2858 usb_serial_deregister(&edgeport_4port_device);
2859 failed_4port_device_register:
2860 usb_serial_deregister(&edgeport_2port_device);
2861 failed_2port_device_register:
2866 /****************************************************************************
2868 * Called when the driver is about to be unloaded.
2869 ****************************************************************************/
2870 static void __exit edgeport_exit (void)
2872 usb_deregister (&io_driver);
2873 usb_serial_deregister (&edgeport_2port_device);
2874 usb_serial_deregister (&edgeport_4port_device);
2875 usb_serial_deregister (&edgeport_8port_device);
2878 module_init(edgeport_init);
2879 module_exit(edgeport_exit);
2881 /* Module information */
2882 MODULE_AUTHOR( DRIVER_AUTHOR );
2883 MODULE_DESCRIPTION( DRIVER_DESC );
2884 MODULE_LICENSE("GPL");
2886 module_param(debug, bool, S_IRUGO | S_IWUSR);
2887 MODULE_PARM_DESC(debug, "Debug enabled or not");
2889 module_param(low_latency, bool, S_IRUGO | S_IWUSR);
2890 MODULE_PARM_DESC(low_latency, "Low latency enabled or not");