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/config.h>
33 #include <linux/kernel.h>
34 #include <linux/jiffies.h>
35 #include <linux/errno.h>
36 #include <linux/init.h>
37 #include <linux/slab.h>
38 #include <linux/tty.h>
39 #include <linux/tty_driver.h>
40 #include <linux/tty_flip.h>
41 #include <linux/module.h>
42 #include <linux/spinlock.h>
43 #include <linux/serial.h>
44 #include <linux/ioctl.h>
45 #include <linux/wait.h>
46 #include <asm/uaccess.h>
47 #include <linux/usb.h>
48 #include "usb-serial.h"
49 #include "io_edgeport.h"
50 #include "io_ionsp.h" /* info for the iosp messages */
51 #include "io_16654.h" /* 16654 UART defines */
56 #define DRIVER_VERSION "v2.7"
57 #define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com> and David Iacovelli"
58 #define DRIVER_DESC "Edgeport USB Serial Driver"
60 /* First, the latest boot code - for first generation edgeports */
61 #define IMAGE_ARRAY_NAME BootCodeImage_GEN1
62 #define IMAGE_VERSION_NAME BootCodeImageVersion_GEN1
63 #include "io_fw_boot.h" /* the bootloader firmware to download to a device, if it needs it */
65 /* for second generation edgeports */
66 #define IMAGE_ARRAY_NAME BootCodeImage_GEN2
67 #define IMAGE_VERSION_NAME BootCodeImageVersion_GEN2
68 #include "io_fw_boot2.h" /* the bootloader firmware to download to a device, if it needs it */
70 /* Then finally the main run-time operational code - for first generation edgeports */
71 #define IMAGE_ARRAY_NAME OperationalCodeImage_GEN1
72 #define IMAGE_VERSION_NAME OperationalCodeImageVersion_GEN1
73 #include "io_fw_down.h" /* Define array OperationalCodeImage[] */
75 /* for second generation edgeports */
76 #define IMAGE_ARRAY_NAME OperationalCodeImage_GEN2
77 #define IMAGE_VERSION_NAME OperationalCodeImageVersion_GEN2
78 #include "io_fw_down2.h" /* Define array OperationalCodeImage[] */
80 #define MAX_NAME_LEN 64
82 #define CHASE_TIMEOUT (5*HZ) /* 5 seconds */
83 #define OPEN_TIMEOUT (5*HZ) /* 5 seconds */
84 #define COMMAND_TIMEOUT (5*HZ) /* 5 seconds */
86 /* receive port state */
88 EXPECT_HDR1 = 0, /* Expect header byte 1 */
89 EXPECT_HDR2 = 1, /* Expect header byte 2 */
90 EXPECT_DATA = 2, /* Expect 'RxBytesRemaining' data */
91 EXPECT_HDR3 = 3, /* Expect header byte 3 (for status hdrs only) */
96 * This Transmit queue is an extension of the edgeport Rx buffer.
97 * The maximum amount of data buffered in both the edgeport
98 * Rx buffer (maxTxCredits) and this buffer will never exceed maxTxCredits.
101 unsigned int head; /* index to head pointer (write) */
102 unsigned int tail; /* index to tail pointer (read) */
103 unsigned int count; /* Bytes in queue */
104 unsigned int size; /* Max size of queue (equal to Max number of TxCredits) */
105 unsigned char *fifo; /* allocated Buffer */
108 /* This structure holds all of the local port information */
109 struct edgeport_port {
110 __u16 txCredits; /* our current credits for this port */
111 __u16 maxTxCredits; /* the max size of the port */
113 struct TxFifo txfifo; /* transmit fifo -- size will be maxTxCredits */
114 struct urb *write_urb; /* write URB for this port */
115 char write_in_progress; /* TRUE while a write URB is outstanding */
118 __u8 shadowLCR; /* last LCR value received */
119 __u8 shadowMCR; /* last MCR value received */
120 __u8 shadowMSR; /* last MSR value received */
121 __u8 shadowLSR; /* last LSR value received */
122 __u8 shadowXonChar; /* last value set as XON char in Edgeport */
123 __u8 shadowXoffChar; /* last value set as XOFF char in Edgeport */
131 char chaseResponsePending;
133 wait_queue_head_t wait_chase; /* for handling sleeping while waiting for chase to finish */
134 wait_queue_head_t wait_open; /* for handling sleeping while waiting for open to finish */
135 wait_queue_head_t wait_command; /* for handling sleeping while waiting for command to finish */
136 wait_queue_head_t delta_msr_wait; /* for handling sleeping while waiting for msr change to happen */
138 struct async_icount icount;
139 struct usb_serial_port *port; /* loop back to the owner of this object */
143 /* This structure holds all of the individual device information */
144 struct edgeport_serial {
145 char name[MAX_NAME_LEN+1]; /* string name of this device */
147 struct edge_manuf_descriptor manuf_descriptor; /* the manufacturer descriptor */
148 struct edge_boot_descriptor boot_descriptor; /* the boot firmware descriptor */
149 struct edgeport_product_info product_info; /* Product Info */
151 __u8 interrupt_in_endpoint; /* the interrupt endpoint handle */
152 unsigned char * interrupt_in_buffer; /* the buffer we use for the interrupt endpoint */
153 struct urb * interrupt_read_urb; /* our interrupt urb */
155 __u8 bulk_in_endpoint; /* the bulk in endpoint handle */
156 unsigned char * bulk_in_buffer; /* the buffer we use for the bulk in endpoint */
157 struct urb * read_urb; /* our bulk read urb */
158 int read_in_progress;
161 __u8 bulk_out_endpoint; /* the bulk out endpoint handle */
163 __s16 rxBytesAvail; /* the number of bytes that we need to read from this device */
165 enum RXSTATE rxState; /* the current state of the bulk receive processor */
166 __u8 rxHeader1; /* receive header byte 1 */
167 __u8 rxHeader2; /* receive header byte 2 */
168 __u8 rxHeader3; /* receive header byte 3 */
169 __u8 rxPort; /* the port that we are currently receiving data for */
170 __u8 rxStatusCode; /* the receive status code */
171 __u8 rxStatusParam; /* the receive status paramater */
172 __s16 rxBytesRemaining; /* the number of port bytes left to read */
173 struct usb_serial *serial; /* loop back to the owner of this object */
176 /* baud rate information */
177 struct divisor_table_entry {
183 // Define table of divisors for Rev A EdgePort/4 hardware
184 // These assume a 3.6864MHz crystal, the standard /16, and
187 static struct divisor_table_entry divisor_table[] = {
190 { 110, 2095}, /* 2094.545455 => 230450 => .0217 % over */
191 { 134, 1713}, /* 1713.011152 => 230398.5 => .00065% under */
209 /* local variables */
212 static int low_latency = 1; /* tty low latency flag, on by default */
214 static int CmdUrbs = 0; /* Number of outstanding Command Write Urbs */
217 /* local function prototypes */
219 /* function prototypes for all URB callbacks */
220 static void edge_interrupt_callback (struct urb *urb, struct pt_regs *regs);
221 static void edge_bulk_in_callback (struct urb *urb, struct pt_regs *regs);
222 static void edge_bulk_out_data_callback (struct urb *urb, struct pt_regs *regs);
223 static void edge_bulk_out_cmd_callback (struct urb *urb, struct pt_regs *regs);
225 /* function prototypes for the usbserial callbacks */
226 static int edge_open (struct usb_serial_port *port, struct file *filp);
227 static void edge_close (struct usb_serial_port *port, struct file *filp);
228 static int edge_write (struct usb_serial_port *port, const unsigned char *buf, int count);
229 static int edge_write_room (struct usb_serial_port *port);
230 static int edge_chars_in_buffer (struct usb_serial_port *port);
231 static void edge_throttle (struct usb_serial_port *port);
232 static void edge_unthrottle (struct usb_serial_port *port);
233 static void edge_set_termios (struct usb_serial_port *port, struct termios *old_termios);
234 static int edge_ioctl (struct usb_serial_port *port, struct file *file, unsigned int cmd, unsigned long arg);
235 static void edge_break (struct usb_serial_port *port, int break_state);
236 static int edge_tiocmget (struct usb_serial_port *port, struct file *file);
237 static int edge_tiocmset (struct usb_serial_port *port, struct file *file, unsigned int set, unsigned int clear);
238 static int edge_startup (struct usb_serial *serial);
239 static void edge_shutdown (struct usb_serial *serial);
242 #include "io_tables.h" /* all of the devices that this driver supports */
244 static struct usb_driver io_driver = {
245 .owner = THIS_MODULE,
246 .name = "io_edgeport",
247 .probe = usb_serial_probe,
248 .disconnect = usb_serial_disconnect,
249 .id_table = id_table_combined,
253 /* function prototypes for all of our local functions */
254 static void process_rcvd_data (struct edgeport_serial *edge_serial, unsigned char *buffer, __u16 bufferLength);
255 static void process_rcvd_status (struct edgeport_serial *edge_serial, __u8 byte2, __u8 byte3);
256 static void edge_tty_recv (struct device *dev, struct tty_struct *tty, unsigned char *data, int length);
257 static void handle_new_msr (struct edgeport_port *edge_port, __u8 newMsr);
258 static void handle_new_lsr (struct edgeport_port *edge_port, __u8 lsrData, __u8 lsr, __u8 data);
259 static int send_iosp_ext_cmd (struct edgeport_port *edge_port, __u8 command, __u8 param);
260 static int calc_baud_rate_divisor (int baud_rate, int *divisor);
261 static int send_cmd_write_baud_rate (struct edgeport_port *edge_port, int baudRate);
262 static void change_port_settings (struct edgeport_port *edge_port, struct termios *old_termios);
263 static int send_cmd_write_uart_register (struct edgeport_port *edge_port, __u8 regNum, __u8 regValue);
264 static int write_cmd_usb (struct edgeport_port *edge_port, unsigned char *buffer, int writeLength);
265 static void send_more_port_data (struct edgeport_serial *edge_serial, struct edgeport_port *edge_port);
267 static int sram_write (struct usb_serial *serial, __u16 extAddr, __u16 addr, __u16 length, __u8 *data);
268 static int rom_read (struct usb_serial *serial, __u16 extAddr, __u16 addr, __u16 length, __u8 *data);
269 static int rom_write (struct usb_serial *serial, __u16 extAddr, __u16 addr, __u16 length, __u8 *data);
270 static void get_manufacturing_desc (struct edgeport_serial *edge_serial);
271 static void get_boot_desc (struct edgeport_serial *edge_serial);
272 static void load_application_firmware (struct edgeport_serial *edge_serial);
274 static void unicode_to_ascii (char *string, __le16 *unicode, int unicode_size);
277 // ************************************************************************
278 // ************************************************************************
279 // ************************************************************************
280 // ************************************************************************
282 /************************************************************************
284 * update_edgeport_E2PROM() Compare current versions of *
285 * Boot ROM and Manufacture *
286 * Descriptors with versions *
287 * embedded in this driver *
289 ************************************************************************/
290 static void update_edgeport_E2PROM (struct edgeport_serial *edge_serial)
294 __u8 BootMajorVersion;
295 __u8 BootMinorVersion;
296 __le16 BootBuildNumber;
299 struct edge_firmware_image_record *record;
300 unsigned char *firmware;
304 switch (edge_serial->product_info.iDownloadFile) {
305 case EDGE_DOWNLOAD_FILE_I930:
306 BootMajorVersion = BootCodeImageVersion_GEN1.MajorVersion;
307 BootMinorVersion = BootCodeImageVersion_GEN1.MinorVersion;
308 BootBuildNumber = cpu_to_le16(BootCodeImageVersion_GEN1.BuildNumber);
309 BootImage = &BootCodeImage_GEN1[0];
310 BootSize = sizeof( BootCodeImage_GEN1 );
313 case EDGE_DOWNLOAD_FILE_80251:
314 BootMajorVersion = BootCodeImageVersion_GEN2.MajorVersion;
315 BootMinorVersion = BootCodeImageVersion_GEN2.MinorVersion;
316 BootBuildNumber = cpu_to_le16(BootCodeImageVersion_GEN2.BuildNumber);
317 BootImage = &BootCodeImage_GEN2[0];
318 BootSize = sizeof( BootCodeImage_GEN2 );
325 // Check Boot Image Version
326 BootCurVer = (edge_serial->boot_descriptor.MajorVersion << 24) +
327 (edge_serial->boot_descriptor.MinorVersion << 16) +
328 le16_to_cpu(edge_serial->boot_descriptor.BuildNumber);
330 BootNewVer = (BootMajorVersion << 24) +
331 (BootMinorVersion << 16) +
332 le16_to_cpu(BootBuildNumber);
334 dbg("Current Boot Image version %d.%d.%d",
335 edge_serial->boot_descriptor.MajorVersion,
336 edge_serial->boot_descriptor.MinorVersion,
337 le16_to_cpu(edge_serial->boot_descriptor.BuildNumber));
340 if (BootNewVer > BootCurVer) {
341 dbg("**Update Boot Image from %d.%d.%d to %d.%d.%d",
342 edge_serial->boot_descriptor.MajorVersion,
343 edge_serial->boot_descriptor.MinorVersion,
344 le16_to_cpu(edge_serial->boot_descriptor.BuildNumber),
347 le16_to_cpu(BootBuildNumber));
350 dbg("Downloading new Boot Image");
352 firmware = BootImage;
355 record = (struct edge_firmware_image_record *)firmware;
356 response = rom_write (edge_serial->serial, le16_to_cpu(record->ExtAddr), le16_to_cpu(record->Addr), le16_to_cpu(record->Len), &record->Data[0]);
358 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));
361 firmware += sizeof (struct edge_firmware_image_record) + le16_to_cpu(record->Len);
362 if (firmware >= &BootImage[BootSize]) {
367 dbg("Boot Image -- already up to date");
372 /************************************************************************
374 * Get string descriptor from device *
376 ************************************************************************/
377 static int get_string (struct usb_device *dev, int Id, char *string)
379 struct usb_string_descriptor StringDesc;
380 struct usb_string_descriptor *pStringDesc;
382 dbg("%s - USB String ID = %d", __FUNCTION__, Id );
384 if (!usb_get_descriptor(dev, USB_DT_STRING, Id, &StringDesc, sizeof(StringDesc))) {
388 pStringDesc = kmalloc (StringDesc.bLength, GFP_KERNEL);
394 if (!usb_get_descriptor(dev, USB_DT_STRING, Id, pStringDesc, StringDesc.bLength )) {
399 unicode_to_ascii(string, pStringDesc->wData, pStringDesc->bLength/2-1);
402 return strlen(string);
407 /************************************************************************
409 * Get string descriptor from device
411 ************************************************************************/
412 static int get_string_desc (struct usb_device *dev, int Id, struct usb_string_descriptor **pRetDesc)
414 struct usb_string_descriptor StringDesc;
415 struct usb_string_descriptor *pStringDesc;
417 dbg("%s - USB String ID = %d", __FUNCTION__, Id );
419 if (!usb_get_descriptor(dev, USB_DT_STRING, Id, &StringDesc, sizeof(StringDesc))) {
423 pStringDesc = kmalloc (StringDesc.bLength, GFP_KERNEL);
429 if (!usb_get_descriptor(dev, USB_DT_STRING, Id, pStringDesc, StringDesc.bLength )) {
434 *pRetDesc = pStringDesc;
439 static void get_product_info(struct edgeport_serial *edge_serial)
441 struct edgeport_product_info *product_info = &edge_serial->product_info;
443 memset (product_info, 0, sizeof(struct edgeport_product_info));
445 product_info->ProductId = (__u16)(le16_to_cpu(edge_serial->serial->dev->descriptor.idProduct) & ~ION_DEVICE_ID_80251_NETCHIP);
446 product_info->NumPorts = edge_serial->manuf_descriptor.NumPorts;
447 product_info->ProdInfoVer = 0;
449 product_info->RomSize = edge_serial->manuf_descriptor.RomSize;
450 product_info->RamSize = edge_serial->manuf_descriptor.RamSize;
451 product_info->CpuRev = edge_serial->manuf_descriptor.CpuRev;
452 product_info->BoardRev = edge_serial->manuf_descriptor.BoardRev;
454 product_info->BootMajorVersion = edge_serial->boot_descriptor.MajorVersion;
455 product_info->BootMinorVersion = edge_serial->boot_descriptor.MinorVersion;
456 product_info->BootBuildNumber = edge_serial->boot_descriptor.BuildNumber;
458 memcpy(product_info->ManufactureDescDate, edge_serial->manuf_descriptor.DescDate, sizeof(edge_serial->manuf_descriptor.DescDate));
460 // check if this is 2nd generation hardware
461 if (le16_to_cpu(edge_serial->serial->dev->descriptor.idProduct) & ION_DEVICE_ID_80251_NETCHIP) {
462 product_info->FirmwareMajorVersion = OperationalCodeImageVersion_GEN2.MajorVersion;
463 product_info->FirmwareMinorVersion = OperationalCodeImageVersion_GEN2.MinorVersion;
464 product_info->FirmwareBuildNumber = cpu_to_le16(OperationalCodeImageVersion_GEN2.BuildNumber);
465 product_info->iDownloadFile = EDGE_DOWNLOAD_FILE_80251;
467 product_info->FirmwareMajorVersion = OperationalCodeImageVersion_GEN1.MajorVersion;
468 product_info->FirmwareMinorVersion = OperationalCodeImageVersion_GEN1.MinorVersion;
469 product_info->FirmwareBuildNumber = cpu_to_le16(OperationalCodeImageVersion_GEN1.BuildNumber);
470 product_info->iDownloadFile = EDGE_DOWNLOAD_FILE_I930;
473 // Determine Product type and set appropriate flags
474 switch (DEVICE_ID_FROM_USB_PRODUCT_ID(product_info->ProductId)) {
475 case ION_DEVICE_ID_EDGEPORT_COMPATIBLE:
476 case ION_DEVICE_ID_EDGEPORT_4T:
477 case ION_DEVICE_ID_EDGEPORT_4:
478 case ION_DEVICE_ID_EDGEPORT_2:
479 case ION_DEVICE_ID_EDGEPORT_8_DUAL_CPU:
480 case ION_DEVICE_ID_EDGEPORT_8:
481 case ION_DEVICE_ID_EDGEPORT_421:
482 case ION_DEVICE_ID_EDGEPORT_21:
483 case ION_DEVICE_ID_EDGEPORT_2_DIN:
484 case ION_DEVICE_ID_EDGEPORT_4_DIN:
485 case ION_DEVICE_ID_EDGEPORT_16_DUAL_CPU:
486 product_info->IsRS232 = 1;
489 case ION_DEVICE_ID_EDGEPORT_2I: // Edgeport/2 RS422/RS485
490 product_info->IsRS422 = 1;
491 product_info->IsRS485 = 1;
494 case ION_DEVICE_ID_EDGEPORT_8I: // Edgeport/4 RS422
495 case ION_DEVICE_ID_EDGEPORT_4I: // Edgeport/4 RS422
496 product_info->IsRS422 = 1;
500 // Dump Product Info structure
501 dbg("**Product Information:");
502 dbg(" ProductId %x", product_info->ProductId );
503 dbg(" NumPorts %d", product_info->NumPorts );
504 dbg(" ProdInfoVer %d", product_info->ProdInfoVer );
505 dbg(" IsServer %d", product_info->IsServer);
506 dbg(" IsRS232 %d", product_info->IsRS232 );
507 dbg(" IsRS422 %d", product_info->IsRS422 );
508 dbg(" IsRS485 %d", product_info->IsRS485 );
509 dbg(" RomSize %d", product_info->RomSize );
510 dbg(" RamSize %d", product_info->RamSize );
511 dbg(" CpuRev %x", product_info->CpuRev );
512 dbg(" BoardRev %x", product_info->BoardRev);
513 dbg(" BootMajorVersion %d.%d.%d", product_info->BootMajorVersion,
514 product_info->BootMinorVersion,
515 le16_to_cpu(product_info->BootBuildNumber));
516 dbg(" FirmwareMajorVersion %d.%d.%d", product_info->FirmwareMajorVersion,
517 product_info->FirmwareMinorVersion,
518 le16_to_cpu(product_info->FirmwareBuildNumber));
519 dbg(" ManufactureDescDate %d/%d/%d", product_info->ManufactureDescDate[0],
520 product_info->ManufactureDescDate[1],
521 product_info->ManufactureDescDate[2]+1900);
522 dbg(" iDownloadFile 0x%x", product_info->iDownloadFile);
527 /************************************************************************/
528 /************************************************************************/
529 /* U S B C A L L B A C K F U N C T I O N S */
530 /* U S B C A L L B A C K F U N C T I O N S */
531 /************************************************************************/
532 /************************************************************************/
534 /*****************************************************************************
535 * edge_interrupt_callback
536 * this is the callback function for when we have received data on the
537 * interrupt endpoint.
538 *****************************************************************************/
539 static void edge_interrupt_callback (struct urb *urb, struct pt_regs *regs)
541 struct edgeport_serial *edge_serial = (struct edgeport_serial *)urb->context;
542 struct edgeport_port *edge_port;
543 struct usb_serial_port *port;
544 unsigned char *data = urb->transfer_buffer;
545 int length = urb->actual_length;
552 dbg("%s", __FUNCTION__);
554 switch (urb->status) {
561 /* this urb is terminated, clean up */
562 dbg("%s - urb shutting down with status: %d", __FUNCTION__, urb->status);
565 dbg("%s - nonzero urb status received: %d", __FUNCTION__, urb->status);
569 // process this interrupt-read even if there are no ports open
571 usb_serial_debug_data(debug, &edge_serial->serial->dev->dev, __FUNCTION__, length, data);
574 bytes_avail = data[0] | (data[1] << 8);
576 spin_lock(&edge_serial->es_lock);
577 edge_serial->rxBytesAvail += bytes_avail;
578 dbg("%s - bytes_avail=%d, rxBytesAvail=%d, read_in_progress=%d", __FUNCTION__, bytes_avail, edge_serial->rxBytesAvail, edge_serial->read_in_progress);
580 if (edge_serial->rxBytesAvail > 0 &&
581 !edge_serial->read_in_progress) {
582 dbg("%s - posting a read", __FUNCTION__);
583 edge_serial->read_in_progress = TRUE;
585 /* we have pending bytes on the bulk in pipe, send a request */
586 edge_serial->read_urb->dev = edge_serial->serial->dev;
587 result = usb_submit_urb(edge_serial->read_urb, GFP_ATOMIC);
589 dev_err(&edge_serial->serial->dev->dev, "%s - usb_submit_urb(read bulk) failed with result = %d\n", __FUNCTION__, result);
590 edge_serial->read_in_progress = FALSE;
593 spin_unlock(&edge_serial->es_lock);
596 /* grab the txcredits for the ports if available */
599 while ((position < length) && (portNumber < edge_serial->serial->num_ports)) {
600 txCredits = data[position] | (data[position+1] << 8);
602 port = edge_serial->serial->port[portNumber];
603 edge_port = usb_get_serial_port_data(port);
604 if (edge_port->open) {
605 spin_lock(&edge_port->ep_lock);
606 edge_port->txCredits += txCredits;
607 spin_unlock(&edge_port->ep_lock);
608 dbg("%s - txcredits for port%d = %d", __FUNCTION__, portNumber, edge_port->txCredits);
610 /* tell the tty driver that something has changed */
611 if (edge_port->port->tty)
612 tty_wakeup(edge_port->port->tty);
614 // Since we have more credit, check if more data can be sent
615 send_more_port_data(edge_serial, edge_port);
624 result = usb_submit_urb (urb, GFP_ATOMIC);
626 dev_err(&urb->dev->dev, "%s - Error %d submitting control urb\n", __FUNCTION__, result);
631 /*****************************************************************************
632 * edge_bulk_in_callback
633 * this is the callback function for when we have received data on the
635 *****************************************************************************/
636 static void edge_bulk_in_callback (struct urb *urb, struct pt_regs *regs)
638 struct edgeport_serial *edge_serial = (struct edgeport_serial *)urb->context;
639 unsigned char *data = urb->transfer_buffer;
641 __u16 raw_data_length;
643 dbg("%s", __FUNCTION__);
646 dbg("%s - nonzero read bulk status received: %d", __FUNCTION__, urb->status);
647 edge_serial->read_in_progress = FALSE;
651 if (urb->actual_length == 0) {
652 dbg("%s - read bulk callback with no data", __FUNCTION__);
653 edge_serial->read_in_progress = FALSE;
657 raw_data_length = urb->actual_length;
659 usb_serial_debug_data(debug, &edge_serial->serial->dev->dev, __FUNCTION__, raw_data_length, data);
661 spin_lock(&edge_serial->es_lock);
663 /* decrement our rxBytes available by the number that we just got */
664 edge_serial->rxBytesAvail -= raw_data_length;
666 dbg("%s - Received = %d, rxBytesAvail %d", __FUNCTION__, raw_data_length, edge_serial->rxBytesAvail);
668 process_rcvd_data (edge_serial, data, urb->actual_length);
670 /* check to see if there's any more data for us to read */
671 if (edge_serial->rxBytesAvail > 0) {
672 dbg("%s - posting a read", __FUNCTION__);
673 edge_serial->read_urb->dev = edge_serial->serial->dev;
674 status = usb_submit_urb(edge_serial->read_urb, GFP_ATOMIC);
676 dev_err(&urb->dev->dev, "%s - usb_submit_urb(read bulk) failed, status = %d\n", __FUNCTION__, status);
677 edge_serial->read_in_progress = FALSE;
680 edge_serial->read_in_progress = FALSE;
683 spin_unlock(&edge_serial->es_lock);
687 /*****************************************************************************
688 * edge_bulk_out_data_callback
689 * this is the callback function for when we have finished sending serial data
690 * on the bulk out endpoint.
691 *****************************************************************************/
692 static void edge_bulk_out_data_callback (struct urb *urb, struct pt_regs *regs)
694 struct edgeport_port *edge_port = (struct edgeport_port *)urb->context;
695 struct tty_struct *tty;
697 dbg("%s", __FUNCTION__);
700 dbg("%s - nonzero write bulk status received: %d", __FUNCTION__, urb->status);
703 tty = edge_port->port->tty;
705 if (tty && edge_port->open) {
706 /* let the tty driver wakeup if it has a special write_wakeup function */
710 // Release the Write URB
711 edge_port->write_in_progress = FALSE;
713 // Check if more data needs to be sent
714 send_more_port_data((struct edgeport_serial *)(usb_get_serial_data(edge_port->port->serial)), edge_port);
718 /*****************************************************************************
720 * this is the callback function for when we have finished sending a command
721 * on the bulk out endpoint.
722 *****************************************************************************/
723 static void edge_bulk_out_cmd_callback (struct urb *urb, struct pt_regs *regs)
725 struct edgeport_port *edge_port = (struct edgeport_port *)urb->context;
726 struct tty_struct *tty;
727 int status = urb->status;
729 dbg("%s", __FUNCTION__);
732 dbg("%s - FREE URB %p (outstanding %d)", __FUNCTION__, urb, CmdUrbs);
735 /* clean up the transfer buffer */
736 kfree(urb->transfer_buffer);
738 /* Free the command urb */
742 dbg("%s - nonzero write bulk status received: %d", __FUNCTION__, status);
746 /* Get pointer to tty */
747 tty = edge_port->port->tty;
749 /* tell the tty driver that something has changed */
750 if (tty && edge_port->open)
753 /* we have completed the command */
754 edge_port->commandPending = FALSE;
755 wake_up(&edge_port->wait_command);
759 /*****************************************************************************
760 * Driver tty interface functions
761 *****************************************************************************/
763 /*****************************************************************************
765 * this function is called by the tty driver when a port is opened
766 * If successful, we return 0
767 * Otherwise we return a negative error number.
768 *****************************************************************************/
769 static int edge_open (struct usb_serial_port *port, struct file * filp)
771 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
772 struct usb_serial *serial;
773 struct edgeport_serial *edge_serial;
776 dbg("%s - port %d", __FUNCTION__, port->number);
778 if (edge_port == NULL)
782 port->tty->low_latency = low_latency;
784 /* see if we've set up our endpoint info yet (can't set it up in edge_startup
785 as the structures were not set up at that time.) */
786 serial = port->serial;
787 edge_serial = usb_get_serial_data(serial);
788 if (edge_serial == NULL) {
791 if (edge_serial->interrupt_in_buffer == NULL) {
792 struct usb_serial_port *port0 = serial->port[0];
794 /* not set up yet, so do it now */
795 edge_serial->interrupt_in_buffer = port0->interrupt_in_buffer;
796 edge_serial->interrupt_in_endpoint = port0->interrupt_in_endpointAddress;
797 edge_serial->interrupt_read_urb = port0->interrupt_in_urb;
798 edge_serial->bulk_in_buffer = port0->bulk_in_buffer;
799 edge_serial->bulk_in_endpoint = port0->bulk_in_endpointAddress;
800 edge_serial->read_urb = port0->read_urb;
801 edge_serial->bulk_out_endpoint = port0->bulk_out_endpointAddress;
803 /* set up our interrupt urb */
804 usb_fill_int_urb(edge_serial->interrupt_read_urb,
806 usb_rcvintpipe(serial->dev,
807 port0->interrupt_in_endpointAddress),
808 port0->interrupt_in_buffer,
809 edge_serial->interrupt_read_urb->transfer_buffer_length,
810 edge_interrupt_callback, edge_serial,
811 edge_serial->interrupt_read_urb->interval);
813 /* set up our bulk in urb */
814 usb_fill_bulk_urb(edge_serial->read_urb, serial->dev,
815 usb_rcvbulkpipe(serial->dev,
816 port0->bulk_in_endpointAddress),
817 port0->bulk_in_buffer,
818 edge_serial->read_urb->transfer_buffer_length,
819 edge_bulk_in_callback, edge_serial);
820 edge_serial->read_in_progress = FALSE;
822 /* start interrupt read for this edgeport
823 * this interrupt will continue as long as the edgeport is connected */
824 response = usb_submit_urb (edge_serial->interrupt_read_urb, GFP_KERNEL);
826 dev_err(&port->dev, "%s - Error %d submitting control urb\n", __FUNCTION__, response);
830 /* initialize our wait queues */
831 init_waitqueue_head(&edge_port->wait_open);
832 init_waitqueue_head(&edge_port->wait_chase);
833 init_waitqueue_head(&edge_port->delta_msr_wait);
834 init_waitqueue_head(&edge_port->wait_command);
836 /* initialize our icount structure */
837 memset (&(edge_port->icount), 0x00, sizeof(edge_port->icount));
839 /* initialize our port settings */
840 edge_port->txCredits = 0; /* Can't send any data yet */
841 edge_port->shadowMCR = MCR_MASTER_IE; /* Must always set this bit to enable ints! */
842 edge_port->chaseResponsePending = FALSE;
844 /* send a open port command */
845 edge_port->openPending = TRUE;
846 edge_port->open = FALSE;
847 response = send_iosp_ext_cmd (edge_port, IOSP_CMD_OPEN_PORT, 0);
850 dev_err(&port->dev, "%s - error sending open port command\n", __FUNCTION__);
851 edge_port->openPending = FALSE;
855 /* now wait for the port to be completely opened */
856 wait_event_timeout(edge_port->wait_open, (edge_port->openPending != TRUE), OPEN_TIMEOUT);
858 if (edge_port->open == FALSE) {
860 dbg("%s - open timedout", __FUNCTION__);
861 edge_port->openPending = FALSE;
865 /* create the txfifo */
866 edge_port->txfifo.head = 0;
867 edge_port->txfifo.tail = 0;
868 edge_port->txfifo.count = 0;
869 edge_port->txfifo.size = edge_port->maxTxCredits;
870 edge_port->txfifo.fifo = kmalloc (edge_port->maxTxCredits, GFP_KERNEL);
872 if (!edge_port->txfifo.fifo) {
873 dbg("%s - no memory", __FUNCTION__);
874 edge_close (port, filp);
878 /* Allocate a URB for the write */
879 edge_port->write_urb = usb_alloc_urb (0, GFP_KERNEL);
880 edge_port->write_in_progress = FALSE;
882 if (!edge_port->write_urb) {
883 dbg("%s - no memory", __FUNCTION__);
884 edge_close (port, filp);
888 dbg("%s(%d) - Initialize TX fifo to %d bytes", __FUNCTION__, port->number, edge_port->maxTxCredits);
890 dbg("%s exited", __FUNCTION__);
896 /************************************************************************
898 * block_until_chase_response
900 * This function will block the close until one of the following:
901 * 1. Response to our Chase comes from Edgeport
902 * 2. A timout of 10 seconds without activity has expired
903 * (1K of Edgeport data @ 2400 baud ==> 4 sec to empty)
905 ************************************************************************/
906 static void block_until_chase_response(struct edgeport_port *edge_port)
915 lastCredits = edge_port->txCredits;
917 // Did we get our Chase response
918 if (edge_port->chaseResponsePending == FALSE) {
919 dbg("%s - Got Chase Response", __FUNCTION__);
921 // did we get all of our credit back?
922 if (edge_port->txCredits == edge_port->maxTxCredits ) {
923 dbg("%s - Got all credits", __FUNCTION__);
928 // Block the thread for a while
929 prepare_to_wait(&edge_port->wait_chase, &wait, TASK_UNINTERRUPTIBLE);
930 schedule_timeout(timeout);
931 finish_wait(&edge_port->wait_chase, &wait);
933 if (lastCredits == edge_port->txCredits) {
934 // No activity.. count down.
937 edge_port->chaseResponsePending = FALSE;
938 dbg("%s - Chase TIMEOUT", __FUNCTION__);
942 // Reset timout value back to 10 seconds
943 dbg("%s - Last %d, Current %d", __FUNCTION__, lastCredits, edge_port->txCredits);
950 /************************************************************************
952 * block_until_tx_empty
954 * This function will block the close until one of the following:
956 * 2. The edgeport has stopped
957 * 3. A timout of 3 seconds without activity has expired
959 ************************************************************************/
960 static void block_until_tx_empty (struct edgeport_port *edge_port)
963 struct TxFifo *fifo = &edge_port->txfifo;
970 lastCount = fifo->count;
972 // Is the Edgeport Buffer empty?
973 if (lastCount == 0) {
974 dbg("%s - TX Buffer Empty", __FUNCTION__);
978 // Block the thread for a while
979 prepare_to_wait (&edge_port->wait_chase, &wait, TASK_UNINTERRUPTIBLE);
980 schedule_timeout(timeout);
981 finish_wait(&edge_port->wait_chase, &wait);
983 dbg("%s wait", __FUNCTION__);
985 if (lastCount == fifo->count) {
986 // No activity.. count down.
989 dbg("%s - TIMEOUT", __FUNCTION__);
993 // Reset timout value back to seconds
1000 /*****************************************************************************
1002 * this function is called by the tty driver when a port is closed
1003 *****************************************************************************/
1004 static void edge_close (struct usb_serial_port *port, struct file * filp)
1006 struct edgeport_serial *edge_serial;
1007 struct edgeport_port *edge_port;
1010 dbg("%s - port %d", __FUNCTION__, port->number);
1012 edge_serial = usb_get_serial_data(port->serial);
1013 edge_port = usb_get_serial_port_data(port);
1014 if ((edge_serial == NULL) || (edge_port == NULL))
1017 // block until tx is empty
1018 block_until_tx_empty(edge_port);
1020 edge_port->closePending = TRUE;
1022 /* flush and chase */
1023 edge_port->chaseResponsePending = TRUE;
1025 dbg("%s - Sending IOSP_CMD_CHASE_PORT", __FUNCTION__);
1026 status = send_iosp_ext_cmd (edge_port, IOSP_CMD_CHASE_PORT, 0);
1028 // block until chase finished
1029 block_until_chase_response(edge_port);
1031 edge_port->chaseResponsePending = FALSE;
1034 /* close the port */
1035 dbg("%s - Sending IOSP_CMD_CLOSE_PORT", __FUNCTION__);
1036 send_iosp_ext_cmd (edge_port, IOSP_CMD_CLOSE_PORT, 0);
1038 //port->close = TRUE;
1039 edge_port->closePending = FALSE;
1040 edge_port->open = FALSE;
1041 edge_port->openPending = FALSE;
1043 if (edge_port->write_urb) {
1044 usb_kill_urb(edge_port->write_urb);
1047 if (edge_port->write_urb) {
1048 /* if this urb had a transfer buffer already (old transfer) free it */
1049 kfree(edge_port->write_urb->transfer_buffer);
1050 usb_free_urb(edge_port->write_urb);
1051 edge_port->write_urb = NULL;
1053 kfree(edge_port->txfifo.fifo);
1054 edge_port->txfifo.fifo = NULL;
1056 dbg("%s exited", __FUNCTION__);
1059 /*****************************************************************************
1061 * this function is called by the tty driver when data should be written to
1063 * If successful, we return the number of bytes written, otherwise we return
1064 * a negative error number.
1065 *****************************************************************************/
1066 static int edge_write (struct usb_serial_port *port, const unsigned char *data, int count)
1068 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1069 struct TxFifo *fifo;
1074 unsigned long flags;
1076 dbg("%s - port %d", __FUNCTION__, port->number);
1078 if (edge_port == NULL)
1081 // get a pointer to the Tx fifo
1082 fifo = &edge_port->txfifo;
1084 spin_lock_irqsave(&edge_port->ep_lock, flags);
1086 // calculate number of bytes to put in fifo
1087 copySize = min ((unsigned int)count, (edge_port->txCredits - fifo->count));
1089 dbg("%s(%d) of %d byte(s) Fifo room %d -- will copy %d bytes", __FUNCTION__,
1090 port->number, count, edge_port->txCredits - fifo->count, copySize);
1092 /* catch writes of 0 bytes which the tty driver likes to give us, and when txCredits is empty */
1093 if (copySize == 0) {
1094 dbg("%s - copySize = Zero", __FUNCTION__);
1099 // since we can never overflow the buffer we do not have to check for full condition
1101 // the copy is done is two parts -- first fill to the end of the buffer
1102 // then copy the reset from the start of the buffer
1104 bytesleft = fifo->size - fifo->head;
1105 firsthalf = min (bytesleft, copySize);
1106 dbg("%s - copy %d bytes of %d into fifo ", __FUNCTION__, firsthalf, bytesleft);
1108 /* now copy our data */
1109 memcpy(&fifo->fifo[fifo->head], data, firsthalf);
1110 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, firsthalf, &fifo->fifo[fifo->head]);
1112 // update the index and size
1113 fifo->head += firsthalf;
1114 fifo->count += firsthalf;
1117 if (fifo->head == fifo->size) {
1121 secondhalf = copySize-firsthalf;
1124 dbg("%s - copy rest of data %d", __FUNCTION__, secondhalf);
1125 memcpy(&fifo->fifo[fifo->head], &data[firsthalf], secondhalf);
1126 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, secondhalf, &fifo->fifo[fifo->head]);
1127 // update the index and size
1128 fifo->count += secondhalf;
1129 fifo->head += secondhalf;
1130 // No need to check for wrap since we can not get to end of fifo in this part
1134 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1136 send_more_port_data((struct edgeport_serial *)usb_get_serial_data(port->serial), edge_port);
1138 dbg("%s wrote %d byte(s) TxCredits %d, Fifo %d", __FUNCTION__, copySize, edge_port->txCredits, fifo->count);
1144 /************************************************************************
1146 * send_more_port_data()
1148 * This routine attempts to write additional UART transmit data
1149 * to a port over the USB bulk pipe. It is called (1) when new
1150 * data has been written to a port's TxBuffer from higher layers
1151 * (2) when the peripheral sends us additional TxCredits indicating
1152 * that it can accept more Tx data for a given port; and (3) when
1153 * a bulk write completes successfully and we want to see if we
1154 * can transmit more.
1156 ************************************************************************/
1157 static void send_more_port_data(struct edgeport_serial *edge_serial, struct edgeport_port *edge_port)
1159 struct TxFifo *fifo = &edge_port->txfifo;
1161 unsigned char *buffer;
1167 unsigned long flags;
1169 dbg("%s(%d)", __FUNCTION__, edge_port->port->number);
1171 spin_lock_irqsave(&edge_port->ep_lock, flags);
1173 if (edge_port->write_in_progress ||
1175 (fifo->count == 0)) {
1176 dbg("%s(%d) EXIT - fifo %d, PendingWrite = %d", __FUNCTION__, edge_port->port->number, fifo->count, edge_port->write_in_progress);
1180 // since the amount of data in the fifo will always fit into the
1181 // edgeport buffer we do not need to check the write length
1183 // Do we have enough credits for this port to make it worthwhile
1184 // to bother queueing a write. If it's too small, say a few bytes,
1185 // it's better to wait for more credits so we can do a larger
1187 if (edge_port->txCredits < EDGE_FW_GET_TX_CREDITS_SEND_THRESHOLD(edge_port->maxTxCredits,EDGE_FW_BULK_MAX_PACKET_SIZE)) {
1188 dbg("%s(%d) Not enough credit - fifo %d TxCredit %d", __FUNCTION__, edge_port->port->number, fifo->count, edge_port->txCredits );
1193 edge_port->write_in_progress = TRUE;
1195 // get a pointer to the write_urb
1196 urb = edge_port->write_urb;
1198 /* make sure transfer buffer is freed */
1199 kfree(urb->transfer_buffer);
1200 urb->transfer_buffer = NULL;
1202 /* build the data header for the buffer and port that we are about to send out */
1203 count = fifo->count;
1204 buffer = kmalloc (count+2, GFP_ATOMIC);
1205 if (buffer == NULL) {
1206 dev_err(&edge_port->port->dev, "%s - no more kernel memory...\n", __FUNCTION__);
1207 edge_port->write_in_progress = FALSE;
1210 buffer[0] = IOSP_BUILD_DATA_HDR1 (edge_port->port->number - edge_port->port->serial->minor, count);
1211 buffer[1] = IOSP_BUILD_DATA_HDR2 (edge_port->port->number - edge_port->port->serial->minor, count);
1213 /* now copy our data */
1214 bytesleft = fifo->size - fifo->tail;
1215 firsthalf = min (bytesleft, count);
1216 memcpy(&buffer[2], &fifo->fifo[fifo->tail], firsthalf);
1217 fifo->tail += firsthalf;
1218 fifo->count -= firsthalf;
1219 if (fifo->tail == fifo->size) {
1223 secondhalf = count-firsthalf;
1225 memcpy(&buffer[2+firsthalf], &fifo->fifo[fifo->tail], secondhalf);
1226 fifo->tail += secondhalf;
1227 fifo->count -= secondhalf;
1231 usb_serial_debug_data(debug, &edge_port->port->dev, __FUNCTION__, count, &buffer[2]);
1233 /* fill up the urb with all of our data and submit it */
1234 usb_fill_bulk_urb (urb, edge_serial->serial->dev,
1235 usb_sndbulkpipe(edge_serial->serial->dev, edge_serial->bulk_out_endpoint),
1236 buffer, count+2, edge_bulk_out_data_callback, edge_port);
1238 /* decrement the number of credits we have by the number we just sent */
1239 edge_port->txCredits -= count;
1240 edge_port->icount.tx += count;
1242 urb->dev = edge_serial->serial->dev;
1243 status = usb_submit_urb(urb, GFP_ATOMIC);
1245 /* something went wrong */
1246 dev_err(&edge_port->port->dev, "%s - usb_submit_urb(write bulk) failed, status = %d, data lost\n", __FUNCTION__, status);
1247 edge_port->write_in_progress = FALSE;
1249 /* revert the credits as something bad happened. */
1250 edge_port->txCredits += count;
1251 edge_port->icount.tx -= count;
1253 dbg("%s wrote %d byte(s) TxCredit %d, Fifo %d", __FUNCTION__, count, edge_port->txCredits, fifo->count);
1256 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1260 /*****************************************************************************
1262 * this function is called by the tty driver when it wants to know how many
1263 * bytes of data we can accept for a specific port.
1264 * If successful, we return the amount of room that we have for this port
1266 * Otherwise we return a negative error number.
1267 *****************************************************************************/
1268 static int edge_write_room (struct usb_serial_port *port)
1270 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1272 unsigned long flags;
1274 dbg("%s", __FUNCTION__);
1276 if (edge_port == NULL)
1278 if (edge_port->closePending == TRUE)
1281 dbg("%s - port %d", __FUNCTION__, port->number);
1283 if (!edge_port->open) {
1284 dbg("%s - port not opened", __FUNCTION__);
1288 // total of both buffers is still txCredit
1289 spin_lock_irqsave(&edge_port->ep_lock, flags);
1290 room = edge_port->txCredits - edge_port->txfifo.count;
1291 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1293 dbg("%s - returns %d", __FUNCTION__, room);
1298 /*****************************************************************************
1299 * edge_chars_in_buffer
1300 * this function is called by the tty driver when it wants to know how many
1301 * bytes of data we currently have outstanding in the port (data that has
1302 * been written, but hasn't made it out the port yet)
1303 * If successful, we return the number of bytes left to be written in the
1305 * Otherwise we return a negative error number.
1306 *****************************************************************************/
1307 static int edge_chars_in_buffer (struct usb_serial_port *port)
1309 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1311 unsigned long flags;
1313 dbg("%s", __FUNCTION__);
1315 if (edge_port == NULL)
1317 if (edge_port->closePending == TRUE)
1320 if (!edge_port->open) {
1321 dbg("%s - port not opened", __FUNCTION__);
1325 spin_lock_irqsave(&edge_port->ep_lock, flags);
1326 num_chars = edge_port->maxTxCredits - edge_port->txCredits + edge_port->txfifo.count;
1327 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1329 dbg("%s(port %d) - returns %d", __FUNCTION__, port->number, num_chars);
1336 /*****************************************************************************
1338 * this function is called by the tty driver when it wants to stop the data
1339 * being read from the port.
1340 *****************************************************************************/
1341 static void edge_throttle (struct usb_serial_port *port)
1343 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1344 struct tty_struct *tty;
1347 dbg("%s - port %d", __FUNCTION__, port->number);
1349 if (edge_port == NULL)
1352 if (!edge_port->open) {
1353 dbg("%s - port not opened", __FUNCTION__);
1359 dbg ("%s - no tty available", __FUNCTION__);
1363 /* if we are implementing XON/XOFF, send the stop character */
1365 unsigned char stop_char = STOP_CHAR(tty);
1366 status = edge_write (port, &stop_char, 1);
1372 /* if we are implementing RTS/CTS, toggle that line */
1373 if (tty->termios->c_cflag & CRTSCTS) {
1374 edge_port->shadowMCR &= ~MCR_RTS;
1375 status = send_cmd_write_uart_register(edge_port, MCR, edge_port->shadowMCR);
1385 /*****************************************************************************
1387 * this function is called by the tty driver when it wants to resume the data
1388 * being read from the port (called after SerialThrottle is called)
1389 *****************************************************************************/
1390 static void edge_unthrottle (struct usb_serial_port *port)
1392 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1393 struct tty_struct *tty;
1396 dbg("%s - port %d", __FUNCTION__, port->number);
1398 if (edge_port == NULL)
1401 if (!edge_port->open) {
1402 dbg("%s - port not opened", __FUNCTION__);
1408 dbg ("%s - no tty available", __FUNCTION__);
1412 /* if we are implementing XON/XOFF, send the start character */
1414 unsigned char start_char = START_CHAR(tty);
1415 status = edge_write (port, &start_char, 1);
1421 /* if we are implementing RTS/CTS, toggle that line */
1422 if (tty->termios->c_cflag & CRTSCTS) {
1423 edge_port->shadowMCR |= MCR_RTS;
1424 status = send_cmd_write_uart_register(edge_port, MCR, edge_port->shadowMCR);
1434 /*****************************************************************************
1436 * this function is called by the tty driver when it wants to change the termios structure
1437 *****************************************************************************/
1438 static void edge_set_termios (struct usb_serial_port *port, struct termios *old_termios)
1440 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1441 struct tty_struct *tty = port->tty;
1444 if (!port->tty || !port->tty->termios) {
1445 dbg ("%s - no tty or termios", __FUNCTION__);
1449 cflag = tty->termios->c_cflag;
1450 /* check that they really want us to change something */
1452 if (cflag == old_termios->c_cflag &&
1453 tty->termios->c_iflag == old_termios->c_iflag) {
1454 dbg("%s - nothing to change", __FUNCTION__);
1459 dbg("%s - clfag %08x iflag %08x", __FUNCTION__,
1460 tty->termios->c_cflag, tty->termios->c_iflag);
1462 dbg("%s - old clfag %08x old iflag %08x", __FUNCTION__,
1463 old_termios->c_cflag, old_termios->c_iflag);
1466 dbg("%s - port %d", __FUNCTION__, port->number);
1468 if (edge_port == NULL)
1471 if (!edge_port->open) {
1472 dbg("%s - port not opened", __FUNCTION__);
1476 /* change the port settings to the new ones specified */
1477 change_port_settings (edge_port, old_termios);
1483 /*****************************************************************************
1484 * get_lsr_info - get line status register info
1486 * Purpose: Let user call ioctl() to get info when the UART physically
1487 * is emptied. On bus types like RS485, the transmitter must
1488 * release the bus after transmitting. This must be done when
1489 * the transmit shift register is empty, not be done when the
1490 * transmit holding register is empty. This functionality
1491 * allows an RS485 driver to be written in user space.
1492 *****************************************************************************/
1493 static int get_lsr_info(struct edgeport_port *edge_port, unsigned int __user *value)
1495 unsigned int result = 0;
1496 unsigned long flags;
1498 spin_lock_irqsave(&edge_port->ep_lock, flags);
1499 if (edge_port->maxTxCredits == edge_port->txCredits &&
1500 edge_port->txfifo.count == 0) {
1501 dbg("%s -- Empty", __FUNCTION__);
1502 result = TIOCSER_TEMT;
1504 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1506 if (copy_to_user(value, &result, sizeof(int)))
1511 static int get_number_bytes_avail(struct edgeport_port *edge_port, unsigned int __user *value)
1513 unsigned int result = 0;
1514 struct tty_struct *tty = edge_port->port->tty;
1517 return -ENOIOCTLCMD;
1519 result = tty->read_cnt;
1521 dbg("%s(%d) = %d", __FUNCTION__, edge_port->port->number, result);
1522 if (copy_to_user(value, &result, sizeof(int)))
1525 return -ENOIOCTLCMD;
1528 static int edge_tiocmset (struct usb_serial_port *port, struct file *file, unsigned int set, unsigned int clear)
1530 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1533 dbg("%s - port %d", __FUNCTION__, port->number);
1535 mcr = edge_port->shadowMCR;
1536 if (set & TIOCM_RTS)
1538 if (set & TIOCM_DTR)
1540 if (set & TIOCM_LOOP)
1541 mcr |= MCR_LOOPBACK;
1543 if (clear & TIOCM_RTS)
1545 if (clear & TIOCM_DTR)
1547 if (clear & TIOCM_LOOP)
1548 mcr &= ~MCR_LOOPBACK;
1550 edge_port->shadowMCR = mcr;
1552 send_cmd_write_uart_register(edge_port, MCR, edge_port->shadowMCR);
1557 static int edge_tiocmget(struct usb_serial_port *port, struct file *file)
1559 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1560 unsigned int result = 0;
1564 dbg("%s - port %d", __FUNCTION__, port->number);
1566 msr = edge_port->shadowMSR;
1567 mcr = edge_port->shadowMCR;
1568 result = ((mcr & MCR_DTR) ? TIOCM_DTR: 0) /* 0x002 */
1569 | ((mcr & MCR_RTS) ? TIOCM_RTS: 0) /* 0x004 */
1570 | ((msr & EDGEPORT_MSR_CTS) ? TIOCM_CTS: 0) /* 0x020 */
1571 | ((msr & EDGEPORT_MSR_CD) ? TIOCM_CAR: 0) /* 0x040 */
1572 | ((msr & EDGEPORT_MSR_RI) ? TIOCM_RI: 0) /* 0x080 */
1573 | ((msr & EDGEPORT_MSR_DSR) ? TIOCM_DSR: 0); /* 0x100 */
1576 dbg("%s -- %x", __FUNCTION__, result);
1581 static int get_serial_info(struct edgeport_port *edge_port, struct serial_struct __user *retinfo)
1583 struct serial_struct tmp;
1588 memset(&tmp, 0, sizeof(tmp));
1590 tmp.type = PORT_16550A;
1591 tmp.line = edge_port->port->serial->minor;
1592 tmp.port = edge_port->port->number;
1594 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
1595 tmp.xmit_fifo_size = edge_port->maxTxCredits;
1596 tmp.baud_base = 9600;
1597 tmp.close_delay = 5*HZ;
1598 tmp.closing_wait = 30*HZ;
1599 // tmp.custom_divisor = state->custom_divisor;
1600 // tmp.hub6 = state->hub6;
1601 // tmp.io_type = state->io_type;
1603 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1610 /*****************************************************************************
1612 * this function handles any ioctl calls to the driver
1613 *****************************************************************************/
1614 static int edge_ioctl (struct usb_serial_port *port, struct file *file, unsigned int cmd, unsigned long arg)
1617 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1618 struct async_icount cnow;
1619 struct async_icount cprev;
1620 struct serial_icounter_struct icount;
1622 dbg("%s - port %d, cmd = 0x%x", __FUNCTION__, port->number, cmd);
1625 // return number of bytes available
1627 dbg("%s (%d) TIOCINQ", __FUNCTION__, port->number);
1628 return get_number_bytes_avail(edge_port, (unsigned int __user *) arg);
1632 dbg("%s (%d) TIOCSERGETLSR", __FUNCTION__, port->number);
1633 return get_lsr_info(edge_port, (unsigned int __user *) arg);
1637 dbg("%s (%d) TIOCGSERIAL", __FUNCTION__, port->number);
1638 return get_serial_info(edge_port, (struct serial_struct __user *) arg);
1641 dbg("%s (%d) TIOCSSERIAL", __FUNCTION__, port->number);
1645 dbg("%s (%d) TIOCMIWAIT", __FUNCTION__, port->number);
1646 cprev = edge_port->icount;
1648 prepare_to_wait(&edge_port->delta_msr_wait, &wait, TASK_INTERRUPTIBLE);
1650 finish_wait(&edge_port->delta_msr_wait, &wait);
1651 /* see if a signal did it */
1652 if (signal_pending(current))
1653 return -ERESTARTSYS;
1654 cnow = edge_port->icount;
1655 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
1656 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
1657 return -EIO; /* no change => error */
1658 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1659 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1660 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1661 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)) ) {
1670 cnow = edge_port->icount;
1671 memset(&icount, 0, sizeof(icount));
1672 icount.cts = cnow.cts;
1673 icount.dsr = cnow.dsr;
1674 icount.rng = cnow.rng;
1675 icount.dcd = cnow.dcd;
1676 icount.rx = cnow.rx;
1677 icount.tx = cnow.tx;
1678 icount.frame = cnow.frame;
1679 icount.overrun = cnow.overrun;
1680 icount.parity = cnow.parity;
1681 icount.brk = cnow.brk;
1682 icount.buf_overrun = cnow.buf_overrun;
1684 dbg("%s (%d) TIOCGICOUNT RX=%d, TX=%d", __FUNCTION__, port->number, icount.rx, icount.tx );
1685 if (copy_to_user((void __user *)arg, &icount, sizeof(icount)))
1690 return -ENOIOCTLCMD;
1694 /*****************************************************************************
1696 * this function sends a break to the port
1697 *****************************************************************************/
1698 static void edge_break (struct usb_serial_port *port, int break_state)
1700 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1703 /* flush and chase */
1704 edge_port->chaseResponsePending = TRUE;
1706 dbg("%s - Sending IOSP_CMD_CHASE_PORT", __FUNCTION__);
1707 status = send_iosp_ext_cmd (edge_port, IOSP_CMD_CHASE_PORT, 0);
1709 // block until chase finished
1710 block_until_chase_response(edge_port);
1712 edge_port->chaseResponsePending = FALSE;
1715 if (break_state == -1) {
1716 dbg("%s - Sending IOSP_CMD_SET_BREAK", __FUNCTION__);
1717 status = send_iosp_ext_cmd (edge_port, IOSP_CMD_SET_BREAK, 0);
1719 dbg("%s - Sending IOSP_CMD_CLEAR_BREAK", __FUNCTION__);
1720 status = send_iosp_ext_cmd (edge_port, IOSP_CMD_CLEAR_BREAK, 0);
1723 dbg("%s - error sending break set/clear command.", __FUNCTION__);
1730 /*****************************************************************************
1732 * this function handles the data received on the bulk in pipe.
1733 *****************************************************************************/
1734 static void process_rcvd_data (struct edgeport_serial *edge_serial, unsigned char * buffer, __u16 bufferLength)
1736 struct usb_serial_port *port;
1737 struct edgeport_port *edge_port;
1738 struct tty_struct *tty;
1739 __u16 lastBufferLength;
1742 dbg("%s", __FUNCTION__);
1744 lastBufferLength = bufferLength + 1;
1746 while (bufferLength > 0) {
1747 /* failsafe incase we get a message that we don't understand */
1748 if (lastBufferLength == bufferLength) {
1749 dbg("%s - stuck in loop, exiting it.", __FUNCTION__);
1752 lastBufferLength = bufferLength;
1754 switch (edge_serial->rxState) {
1756 edge_serial->rxHeader1 = *buffer;
1760 if (bufferLength == 0) {
1761 edge_serial->rxState = EXPECT_HDR2;
1764 /* otherwise, drop on through */
1767 edge_serial->rxHeader2 = *buffer;
1771 dbg("%s - Hdr1=%02X Hdr2=%02X", __FUNCTION__, edge_serial->rxHeader1, edge_serial->rxHeader2);
1773 // Process depending on whether this header is
1776 if (IS_CMD_STAT_HDR(edge_serial->rxHeader1)) {
1777 // Decode this status header and goto EXPECT_HDR1 (if we
1778 // can process the status with only 2 bytes), or goto
1779 // EXPECT_HDR3 to get the third byte.
1781 edge_serial->rxPort = IOSP_GET_HDR_PORT(edge_serial->rxHeader1);
1782 edge_serial->rxStatusCode = IOSP_GET_STATUS_CODE(edge_serial->rxHeader1);
1784 if (!IOSP_STATUS_IS_2BYTE(edge_serial->rxStatusCode)) {
1785 // This status needs additional bytes. Save what we have
1786 // and then wait for more data.
1787 edge_serial->rxStatusParam = edge_serial->rxHeader2;
1789 edge_serial->rxState = EXPECT_HDR3;
1793 // We have all the header bytes, process the status now
1794 process_rcvd_status (edge_serial, edge_serial->rxHeader2, 0);
1795 edge_serial->rxState = EXPECT_HDR1;
1798 edge_serial->rxPort = IOSP_GET_HDR_PORT(edge_serial->rxHeader1);
1799 edge_serial->rxBytesRemaining = IOSP_GET_HDR_DATA_LEN(edge_serial->rxHeader1, edge_serial->rxHeader2);
1801 dbg("%s - Data for Port %u Len %u", __FUNCTION__, edge_serial->rxPort, edge_serial->rxBytesRemaining);
1803 //ASSERT( DevExt->RxPort < DevExt->NumPorts );
1804 //ASSERT( DevExt->RxBytesRemaining < IOSP_MAX_DATA_LENGTH );
1806 if (bufferLength == 0 ) {
1807 edge_serial->rxState = EXPECT_DATA;
1810 // Else, drop through
1813 case EXPECT_DATA: // Expect data
1815 if (bufferLength < edge_serial->rxBytesRemaining) {
1816 rxLen = bufferLength;
1817 edge_serial->rxState = EXPECT_DATA; // Expect data to start next buffer
1819 // BufLen >= RxBytesRemaining
1820 rxLen = edge_serial->rxBytesRemaining;
1821 edge_serial->rxState = EXPECT_HDR1; // Start another header next time
1824 bufferLength -= rxLen;
1825 edge_serial->rxBytesRemaining -= rxLen;
1827 /* spit this data back into the tty driver if this port is open */
1829 port = edge_serial->serial->port[edge_serial->rxPort];
1830 edge_port = usb_get_serial_port_data(port);
1831 if (edge_port->open) {
1832 tty = edge_port->port->tty;
1834 dbg("%s - Sending %d bytes to TTY for port %d", __FUNCTION__, rxLen, edge_serial->rxPort);
1835 edge_tty_recv(&edge_serial->serial->dev->dev, tty, buffer, rxLen);
1837 edge_port->icount.rx += rxLen;
1844 case EXPECT_HDR3: // Expect 3rd byte of status header
1845 edge_serial->rxHeader3 = *buffer;
1849 // We have all the header bytes, process the status now
1850 process_rcvd_status (edge_serial, edge_serial->rxStatusParam, edge_serial->rxHeader3);
1851 edge_serial->rxState = EXPECT_HDR1;
1859 /*****************************************************************************
1860 * process_rcvd_status
1861 * this function handles the any status messages received on the bulk in pipe.
1862 *****************************************************************************/
1863 static void process_rcvd_status (struct edgeport_serial *edge_serial, __u8 byte2, __u8 byte3)
1865 struct usb_serial_port *port;
1866 struct edgeport_port *edge_port;
1867 __u8 code = edge_serial->rxStatusCode;
1869 /* switch the port pointer to the one being currently talked about */
1870 port = edge_serial->serial->port[edge_serial->rxPort];
1871 edge_port = usb_get_serial_port_data(port);
1872 if (edge_port == NULL) {
1873 dev_err(&edge_serial->serial->dev->dev, "%s - edge_port == NULL for port %d\n", __FUNCTION__, edge_serial->rxPort);
1877 dbg("%s - port %d", __FUNCTION__, edge_serial->rxPort);
1879 if (code == IOSP_EXT_STATUS) {
1881 case IOSP_EXT_STATUS_CHASE_RSP:
1882 // we want to do EXT status regardless of port open/closed
1883 dbg("%s - Port %u EXT CHASE_RSP Data = %02x", __FUNCTION__, edge_serial->rxPort, byte3 );
1884 // Currently, the only EXT_STATUS is Chase, so process here instead of one more call
1885 // to one more subroutine. If/when more EXT_STATUS, there'll be more work to do.
1886 // Also, we currently clear flag and close the port regardless of content of above's Byte3.
1887 // We could choose to do something else when Byte3 says Timeout on Chase from Edgeport,
1888 // like wait longer in block_until_chase_response, but for now we don't.
1889 edge_port->chaseResponsePending = FALSE;
1890 wake_up (&edge_port->wait_chase);
1893 case IOSP_EXT_STATUS_RX_CHECK_RSP:
1894 dbg("%s ========== Port %u CHECK_RSP Sequence = %02x =============\n", __FUNCTION__, edge_serial->rxPort, byte3 );
1895 //Port->RxCheckRsp = TRUE;
1900 if (code == IOSP_STATUS_OPEN_RSP) {
1901 edge_port->txCredits = GET_TX_BUFFER_SIZE(byte3);
1902 edge_port->maxTxCredits = edge_port->txCredits;
1903 dbg("%s - Port %u Open Response Inital MSR = %02x TxBufferSize = %d", __FUNCTION__, edge_serial->rxPort, byte2, edge_port->txCredits);
1904 handle_new_msr (edge_port, byte2);
1906 /* send the current line settings to the port so we are in sync with any further termios calls */
1907 if (edge_port->port->tty)
1908 change_port_settings (edge_port, edge_port->port->tty->termios);
1910 /* we have completed the open */
1911 edge_port->openPending = FALSE;
1912 edge_port->open = TRUE;
1913 wake_up(&edge_port->wait_open);
1917 // If port is closed, silently discard all rcvd status. We can
1918 // have cases where buffered status is received AFTER the close
1919 // port command is sent to the Edgeport.
1920 if ((!edge_port->open ) || (edge_port->closePending)) {
1925 // Not currently sent by Edgeport
1926 case IOSP_STATUS_LSR:
1927 dbg("%s - Port %u LSR Status = %02x", __FUNCTION__, edge_serial->rxPort, byte2);
1928 handle_new_lsr (edge_port, FALSE, byte2, 0);
1931 case IOSP_STATUS_LSR_DATA:
1932 dbg("%s - Port %u LSR Status = %02x, Data = %02x", __FUNCTION__, edge_serial->rxPort, byte2, byte3);
1933 // byte2 is LSR Register
1934 // byte3 is broken data byte
1935 handle_new_lsr (edge_port, TRUE, byte2, byte3);
1938 // case IOSP_EXT_4_STATUS:
1939 // dbg("%s - Port %u LSR Status = %02x Data = %02x", __FUNCTION__, edge_serial->rxPort, byte2, byte3);
1942 case IOSP_STATUS_MSR:
1943 dbg("%s - Port %u MSR Status = %02x", __FUNCTION__, edge_serial->rxPort, byte2);
1945 // Process this new modem status and generate appropriate
1946 // events, etc, based on the new status. This routine
1947 // also saves the MSR in Port->ShadowMsr.
1948 handle_new_msr(edge_port, byte2);
1952 dbg("%s - Unrecognized IOSP status code %u\n", __FUNCTION__, code);
1960 /*****************************************************************************
1962 * this function passes data on to the tty flip buffer
1963 *****************************************************************************/
1964 static void edge_tty_recv(struct device *dev, struct tty_struct *tty, unsigned char *data, int length)
1969 if (tty->flip.count >= TTY_FLIPBUF_SIZE) {
1970 tty_flip_buffer_push(tty);
1971 if (tty->flip.count >= TTY_FLIPBUF_SIZE) {
1972 dev_err(dev, "%s - dropping data, %d bytes lost\n",
1973 __FUNCTION__, length);
1977 cnt = min(length, TTY_FLIPBUF_SIZE - tty->flip.count);
1978 memcpy(tty->flip.char_buf_ptr, data, cnt);
1979 memset(tty->flip.flag_buf_ptr, 0, cnt);
1980 tty->flip.char_buf_ptr += cnt;
1981 tty->flip.flag_buf_ptr += cnt;
1982 tty->flip.count += cnt;
1985 } while (length > 0);
1987 tty_flip_buffer_push(tty);
1991 /*****************************************************************************
1993 * this function handles any change to the msr register for a port.
1994 *****************************************************************************/
1995 static void handle_new_msr(struct edgeport_port *edge_port, __u8 newMsr)
1997 struct async_icount *icount;
1999 dbg("%s %02x", __FUNCTION__, newMsr);
2001 if (newMsr & (EDGEPORT_MSR_DELTA_CTS | EDGEPORT_MSR_DELTA_DSR | EDGEPORT_MSR_DELTA_RI | EDGEPORT_MSR_DELTA_CD)) {
2002 icount = &edge_port->icount;
2004 /* update input line counters */
2005 if (newMsr & EDGEPORT_MSR_DELTA_CTS) {
2008 if (newMsr & EDGEPORT_MSR_DELTA_DSR) {
2011 if (newMsr & EDGEPORT_MSR_DELTA_CD) {
2014 if (newMsr & EDGEPORT_MSR_DELTA_RI) {
2017 wake_up_interruptible(&edge_port->delta_msr_wait);
2020 /* Save the new modem status */
2021 edge_port->shadowMSR = newMsr & 0xf0;
2027 /*****************************************************************************
2029 * this function handles any change to the lsr register for a port.
2030 *****************************************************************************/
2031 static void handle_new_lsr(struct edgeport_port *edge_port, __u8 lsrData, __u8 lsr, __u8 data)
2033 __u8 newLsr = (__u8)(lsr & (__u8)(LSR_OVER_ERR | LSR_PAR_ERR | LSR_FRM_ERR | LSR_BREAK));
2034 struct async_icount *icount;
2036 dbg("%s - %02x", __FUNCTION__, newLsr);
2038 edge_port->shadowLSR = lsr;
2040 if (newLsr & LSR_BREAK) {
2042 // Parity and Framing errors only count if they
2043 // occur exclusive of a break being
2046 newLsr &= (__u8)(LSR_OVER_ERR | LSR_BREAK);
2049 /* Place LSR data byte into Rx buffer */
2050 if (lsrData && edge_port->port->tty)
2051 edge_tty_recv(&edge_port->port->dev, edge_port->port->tty, &data, 1);
2053 /* update input line counters */
2054 icount = &edge_port->icount;
2055 if (newLsr & LSR_BREAK) {
2058 if (newLsr & LSR_OVER_ERR) {
2061 if (newLsr & LSR_PAR_ERR) {
2064 if (newLsr & LSR_FRM_ERR) {
2072 /****************************************************************************
2074 * writes a number of bytes to the Edgeport device's sram starting at the
2076 * If successful returns the number of bytes written, otherwise it returns
2077 * a negative error number of the problem.
2078 ****************************************************************************/
2079 static int sram_write (struct usb_serial *serial, __u16 extAddr, __u16 addr, __u16 length, __u8 *data)
2082 __u16 current_length;
2083 unsigned char *transfer_buffer;
2085 dbg("%s - %x, %x, %d", __FUNCTION__, extAddr, addr, length);
2087 transfer_buffer = kmalloc (64, GFP_KERNEL);
2088 if (!transfer_buffer) {
2089 dev_err(&serial->dev->dev, "%s - kmalloc(%d) failed.\n", __FUNCTION__, 64);
2093 /* need to split these writes up into 64 byte chunks */
2095 while (length > 0) {
2097 current_length = 64;
2099 current_length = length;
2101 // dbg("%s - writing %x, %x, %d", __FUNCTION__, extAddr, addr, current_length);
2102 memcpy (transfer_buffer, data, current_length);
2103 result = usb_control_msg (serial->dev, usb_sndctrlpipe(serial->dev, 0), USB_REQUEST_ION_WRITE_RAM,
2104 0x40, addr, extAddr, transfer_buffer, current_length, 300);
2107 length -= current_length;
2108 addr += current_length;
2109 data += current_length;
2112 kfree (transfer_buffer);
2117 /****************************************************************************
2119 * writes a number of bytes to the Edgeport device's ROM starting at the
2121 * If successful returns the number of bytes written, otherwise it returns
2122 * a negative error number of the problem.
2123 ****************************************************************************/
2124 static int rom_write (struct usb_serial *serial, __u16 extAddr, __u16 addr, __u16 length, __u8 *data)
2127 __u16 current_length;
2128 unsigned char *transfer_buffer;
2130 // dbg("%s - %x, %x, %d", __FUNCTION__, extAddr, addr, length);
2132 transfer_buffer = kmalloc (64, GFP_KERNEL);
2133 if (!transfer_buffer) {
2134 dev_err(&serial->dev->dev, "%s - kmalloc(%d) failed.\n", __FUNCTION__, 64);
2138 /* need to split these writes up into 64 byte chunks */
2140 while (length > 0) {
2142 current_length = 64;
2144 current_length = length;
2146 // dbg("%s - writing %x, %x, %d", __FUNCTION__, extAddr, addr, current_length);
2147 memcpy (transfer_buffer, data, current_length);
2148 result = usb_control_msg (serial->dev, usb_sndctrlpipe(serial->dev, 0), USB_REQUEST_ION_WRITE_ROM,
2149 0x40, addr, extAddr, transfer_buffer, current_length, 300);
2152 length -= current_length;
2153 addr += current_length;
2154 data += current_length;
2157 kfree (transfer_buffer);
2162 /****************************************************************************
2164 * reads a number of bytes from the Edgeport device starting at the given
2166 * If successful returns the number of bytes read, otherwise it returns
2167 * a negative error number of the problem.
2168 ****************************************************************************/
2169 static int rom_read (struct usb_serial *serial, __u16 extAddr, __u16 addr, __u16 length, __u8 *data)
2172 __u16 current_length;
2173 unsigned char *transfer_buffer;
2175 dbg("%s - %x, %x, %d", __FUNCTION__, extAddr, addr, length);
2177 transfer_buffer = kmalloc (64, GFP_KERNEL);
2178 if (!transfer_buffer) {
2179 dev_err(&serial->dev->dev, "%s - kmalloc(%d) failed.\n", __FUNCTION__, 64);
2183 /* need to split these reads up into 64 byte chunks */
2185 while (length > 0) {
2187 current_length = 64;
2189 current_length = length;
2191 // dbg("%s - %x, %x, %d", __FUNCTION__, extAddr, addr, current_length);
2192 result = usb_control_msg (serial->dev, usb_rcvctrlpipe(serial->dev, 0), USB_REQUEST_ION_READ_ROM,
2193 0xC0, addr, extAddr, transfer_buffer, current_length, 300);
2196 memcpy (data, transfer_buffer, current_length);
2197 length -= current_length;
2198 addr += current_length;
2199 data += current_length;
2202 kfree (transfer_buffer);
2207 /****************************************************************************
2209 * Is used to send a IOSP message to the Edgeport device
2210 ****************************************************************************/
2211 static int send_iosp_ext_cmd (struct edgeport_port *edge_port, __u8 command, __u8 param)
2213 unsigned char *buffer;
2214 unsigned char *currentCommand;
2218 dbg("%s - %d, %d", __FUNCTION__, command, param);
2220 buffer = kmalloc (10, GFP_ATOMIC);
2222 dev_err(&edge_port->port->dev, "%s - kmalloc(%d) failed.\n", __FUNCTION__, 10);
2226 currentCommand = buffer;
2228 MAKE_CMD_EXT_CMD (¤tCommand, &length,
2229 edge_port->port->number - edge_port->port->serial->minor,
2232 status = write_cmd_usb (edge_port, buffer, length);
2234 /* something bad happened, let's free up the memory */
2242 /*****************************************************************************
2244 * this function writes the given buffer out to the bulk write endpoint.
2245 *****************************************************************************/
2246 static int write_cmd_usb (struct edgeport_port *edge_port, unsigned char *buffer, int length)
2248 struct edgeport_serial *edge_serial = usb_get_serial_data(edge_port->port->serial);
2253 usb_serial_debug_data(debug, &edge_port->port->dev, __FUNCTION__, length, buffer);
2255 /* Allocate our next urb */
2256 urb = usb_alloc_urb (0, GFP_ATOMIC);
2261 dbg("%s - ALLOCATE URB %p (outstanding %d)", __FUNCTION__, urb, CmdUrbs);
2263 usb_fill_bulk_urb (urb, edge_serial->serial->dev,
2264 usb_sndbulkpipe(edge_serial->serial->dev, edge_serial->bulk_out_endpoint),
2265 buffer, length, edge_bulk_out_cmd_callback, edge_port);
2267 edge_port->commandPending = TRUE;
2268 status = usb_submit_urb(urb, GFP_ATOMIC);
2271 /* something went wrong */
2272 dev_err(&edge_port->port->dev, "%s - usb_submit_urb(write command) failed, status = %d\n", __FUNCTION__, status);
2279 // wait for command to finish
2280 timeout = COMMAND_TIMEOUT;
2282 wait_event (&edge_port->wait_command, (edge_port->commandPending == FALSE));
2284 if (edge_port->commandPending == TRUE) {
2285 /* command timed out */
2286 dbg("%s - command timed out", __FUNCTION__);
2294 /*****************************************************************************
2295 * send_cmd_write_baud_rate
2296 * this function sends the proper command to change the baud rate of the
2298 *****************************************************************************/
2299 static int send_cmd_write_baud_rate (struct edgeport_port *edge_port, int baudRate)
2301 unsigned char *cmdBuffer;
2302 unsigned char *currCmd;
2306 unsigned char number = edge_port->port->number - edge_port->port->serial->minor;
2308 dbg("%s - port = %d, baud = %d", __FUNCTION__, edge_port->port->number, baudRate);
2310 status = calc_baud_rate_divisor (baudRate, &divisor);
2312 dev_err(&edge_port->port->dev, "%s - bad baud rate\n", __FUNCTION__);
2316 // Alloc memory for the string of commands.
2317 cmdBuffer = kmalloc (0x100, GFP_ATOMIC);
2319 dev_err(&edge_port->port->dev, "%s - kmalloc(%d) failed.\n", __FUNCTION__, 0x100);
2322 currCmd = cmdBuffer;
2324 // Enable access to divisor latch
2325 MAKE_CMD_WRITE_REG( &currCmd, &cmdLen, number, LCR, LCR_DL_ENABLE );
2327 // Write the divisor itself
2328 MAKE_CMD_WRITE_REG( &currCmd, &cmdLen, number, DLL, LOW8 (divisor) );
2329 MAKE_CMD_WRITE_REG( &currCmd, &cmdLen, number, DLM, HIGH8(divisor) );
2331 // Restore original value to disable access to divisor latch
2332 MAKE_CMD_WRITE_REG( &currCmd, &cmdLen, number, LCR, edge_port->shadowLCR);
2334 status = write_cmd_usb(edge_port, cmdBuffer, cmdLen );
2336 /* something bad happened, let's free up the memory */
2344 /*****************************************************************************
2345 * calc_baud_rate_divisor
2346 * this function calculates the proper baud rate divisor for the specified
2348 *****************************************************************************/
2349 static int calc_baud_rate_divisor (int baudrate, int *divisor)
2355 dbg("%s - %d", __FUNCTION__, baudrate);
2357 for (i = 0; i < NUM_ENTRIES(divisor_table); i++) {
2358 if ( divisor_table[i].BaudRate == baudrate ) {
2359 *divisor = divisor_table[i].Divisor;
2364 // We have tried all of the standard baud rates
2365 // lets try to calculate the divisor for this baud rate
2366 // Make sure the baud rate is reasonable
2367 if (baudrate > 50 && baudrate < 230400) {
2369 custom = (__u16)((230400L + baudrate/2) / baudrate);
2373 dbg("%s - Baud %d = %d\n", __FUNCTION__, baudrate, custom);
2381 /*****************************************************************************
2382 * send_cmd_write_uart_register
2383 * this function builds up a uart register message and sends to to the device.
2384 *****************************************************************************/
2385 static int send_cmd_write_uart_register (struct edgeport_port *edge_port, __u8 regNum, __u8 regValue)
2387 unsigned char *cmdBuffer;
2388 unsigned char *currCmd;
2389 unsigned long cmdLen = 0;
2392 dbg("%s - write to %s register 0x%02x", (regNum == MCR) ? "MCR" : "LCR", __FUNCTION__, regValue);
2394 // Alloc memory for the string of commands.
2395 cmdBuffer = kmalloc (0x10, GFP_ATOMIC);
2396 if (cmdBuffer == NULL ) {
2400 currCmd = cmdBuffer;
2402 // Build a cmd in the buffer to write the given register
2403 MAKE_CMD_WRITE_REG (&currCmd, &cmdLen,
2404 edge_port->port->number - edge_port->port->serial->minor,
2407 status = write_cmd_usb(edge_port, cmdBuffer, cmdLen);
2409 /* something bad happened, let's free up the memory */
2417 /*****************************************************************************
2418 * change_port_settings
2419 * This routine is called to set the UART on the device to match the specified
2421 *****************************************************************************/
2425 static void change_port_settings (struct edgeport_port *edge_port, struct termios *old_termios)
2427 struct tty_struct *tty;
2438 dbg("%s - port %d", __FUNCTION__, edge_port->port->number);
2440 if ((!edge_port->open) &&
2441 (!edge_port->openPending)) {
2442 dbg("%s - port not opened", __FUNCTION__);
2446 tty = edge_port->port->tty;
2449 dbg("%s - no tty structures", __FUNCTION__);
2453 cflag = tty->termios->c_cflag;
2455 switch (cflag & CSIZE) {
2456 case CS5: lData = LCR_BITS_5; mask = 0x1f; dbg("%s - data bits = 5", __FUNCTION__); break;
2457 case CS6: lData = LCR_BITS_6; mask = 0x3f; dbg("%s - data bits = 6", __FUNCTION__); break;
2458 case CS7: lData = LCR_BITS_7; mask = 0x7f; dbg("%s - data bits = 7", __FUNCTION__); break;
2460 case CS8: lData = LCR_BITS_8; dbg("%s - data bits = 8", __FUNCTION__); break;
2463 lParity = LCR_PAR_NONE;
2464 if (cflag & PARENB) {
2465 if (cflag & CMSPAR) {
2466 if (cflag & PARODD) {
2467 lParity = LCR_PAR_MARK;
2468 dbg("%s - parity = mark", __FUNCTION__);
2470 lParity = LCR_PAR_SPACE;
2471 dbg("%s - parity = space", __FUNCTION__);
2473 } else if (cflag & PARODD) {
2474 lParity = LCR_PAR_ODD;
2475 dbg("%s - parity = odd", __FUNCTION__);
2477 lParity = LCR_PAR_EVEN;
2478 dbg("%s - parity = even", __FUNCTION__);
2481 dbg("%s - parity = none", __FUNCTION__);
2484 if (cflag & CSTOPB) {
2486 dbg("%s - stop bits = 2", __FUNCTION__);
2489 dbg("%s - stop bits = 1", __FUNCTION__);
2492 /* figure out the flow control settings */
2493 rxFlow = txFlow = 0x00;
2494 if (cflag & CRTSCTS) {
2495 rxFlow |= IOSP_RX_FLOW_RTS;
2496 txFlow |= IOSP_TX_FLOW_CTS;
2497 dbg("%s - RTS/CTS is enabled", __FUNCTION__);
2499 dbg("%s - RTS/CTS is disabled", __FUNCTION__);
2502 /* if we are implementing XON/XOFF, set the start and stop character in the device */
2503 if (I_IXOFF(tty) || I_IXON(tty)) {
2504 unsigned char stop_char = STOP_CHAR(tty);
2505 unsigned char start_char = START_CHAR(tty);
2507 send_iosp_ext_cmd (edge_port, IOSP_CMD_SET_XON_CHAR, start_char);
2508 send_iosp_ext_cmd (edge_port, IOSP_CMD_SET_XOFF_CHAR, stop_char);
2510 /* if we are implementing INBOUND XON/XOFF */
2512 rxFlow |= IOSP_RX_FLOW_XON_XOFF;
2513 dbg("%s - INBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x", __FUNCTION__, start_char, stop_char);
2515 dbg("%s - INBOUND XON/XOFF is disabled", __FUNCTION__);
2518 /* if we are implementing OUTBOUND XON/XOFF */
2520 txFlow |= IOSP_TX_FLOW_XON_XOFF;
2521 dbg("%s - OUTBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x", __FUNCTION__, start_char, stop_char);
2523 dbg("%s - OUTBOUND XON/XOFF is disabled", __FUNCTION__);
2527 /* Set flow control to the configured value */
2528 send_iosp_ext_cmd (edge_port, IOSP_CMD_SET_RX_FLOW, rxFlow);
2529 send_iosp_ext_cmd (edge_port, IOSP_CMD_SET_TX_FLOW, txFlow);
2532 edge_port->shadowLCR &= ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
2533 edge_port->shadowLCR |= (lData | lParity | lStop);
2535 edge_port->validDataMask = mask;
2537 /* Send the updated LCR value to the EdgePort */
2538 status = send_cmd_write_uart_register(edge_port, LCR, edge_port->shadowLCR);
2543 /* set up the MCR register and send it to the EdgePort */
2544 edge_port->shadowMCR = MCR_MASTER_IE;
2545 if (cflag & CBAUD) {
2546 edge_port->shadowMCR |= (MCR_DTR | MCR_RTS);
2548 status = send_cmd_write_uart_register(edge_port, MCR, edge_port->shadowMCR);
2553 /* Determine divisor based on baud rate */
2554 baud = tty_get_baud_rate(tty);
2556 /* pick a default, any default... */
2560 dbg("%s - baud rate = %d", __FUNCTION__, baud);
2561 status = send_cmd_write_baud_rate (edge_port, baud);
2567 /****************************************************************************
2569 * Turns a string from Unicode into ASCII.
2570 * Doesn't do a good job with any characters that are outside the normal
2571 * ASCII range, but it's only for debugging...
2572 * NOTE: expects the unicode in LE format
2573 ****************************************************************************/
2574 static void unicode_to_ascii (char *string, __le16 *unicode, int unicode_size)
2578 if (unicode_size <= 0)
2581 for (i = 0; i < unicode_size; ++i)
2582 string[i] = (char)(le16_to_cpu(unicode[i]));
2583 string[unicode_size] = 0x00;
2587 /****************************************************************************
2588 * get_manufacturing_desc
2589 * reads in the manufacturing descriptor and stores it into the serial
2591 ****************************************************************************/
2592 static void get_manufacturing_desc (struct edgeport_serial *edge_serial)
2596 dbg("getting manufacturer descriptor");
2598 response = rom_read (edge_serial->serial, (EDGE_MANUF_DESC_ADDR & 0xffff0000) >> 16,
2599 (__u16)(EDGE_MANUF_DESC_ADDR & 0x0000ffff), EDGE_MANUF_DESC_LEN,
2600 (__u8 *)(&edge_serial->manuf_descriptor));
2603 dev_err(&edge_serial->serial->dev->dev, "error in getting manufacturer descriptor\n");
2606 dbg("**Manufacturer Descriptor");
2607 dbg(" RomSize: %dK", edge_serial->manuf_descriptor.RomSize);
2608 dbg(" RamSize: %dK", edge_serial->manuf_descriptor.RamSize);
2609 dbg(" CpuRev: %d", edge_serial->manuf_descriptor.CpuRev);
2610 dbg(" BoardRev: %d", edge_serial->manuf_descriptor.BoardRev);
2611 dbg(" NumPorts: %d", edge_serial->manuf_descriptor.NumPorts);
2612 dbg(" DescDate: %d/%d/%d", edge_serial->manuf_descriptor.DescDate[0], edge_serial->manuf_descriptor.DescDate[1], edge_serial->manuf_descriptor.DescDate[2]+1900);
2613 unicode_to_ascii (string, edge_serial->manuf_descriptor.SerialNumber, edge_serial->manuf_descriptor.SerNumLength/2-1);
2614 dbg(" SerialNumber: %s", string);
2615 unicode_to_ascii (string, edge_serial->manuf_descriptor.AssemblyNumber, edge_serial->manuf_descriptor.AssemblyNumLength/2-1);
2616 dbg(" AssemblyNumber: %s", string);
2617 unicode_to_ascii (string, edge_serial->manuf_descriptor.OemAssyNumber, edge_serial->manuf_descriptor.OemAssyNumLength/2-1);
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 = kmalloc (sizeof(struct edgeport_serial), GFP_KERNEL);
2736 if (edge_serial == NULL) {
2737 dev_err(&serial->dev->dev, "%s - Out of memory\n", __FUNCTION__);
2740 memset (edge_serial, 0, sizeof(struct edgeport_serial));
2741 spin_lock_init(&edge_serial->es_lock);
2742 edge_serial->serial = serial;
2743 usb_set_serial_data(serial, edge_serial);
2745 /* get the name for the device from the device */
2746 if ( (i = get_string(dev, dev->descriptor.iManufacturer, &edge_serial->name[0])) != 0) {
2747 edge_serial->name[i-1] = ' ';
2750 get_string(dev, dev->descriptor.iProduct, &edge_serial->name[i]);
2752 dev_info(&serial->dev->dev, "%s detected\n", edge_serial->name);
2754 /* get the manufacturing descriptor for this device */
2755 get_manufacturing_desc (edge_serial);
2757 /* get the boot descriptor */
2758 get_boot_desc (edge_serial);
2760 get_product_info(edge_serial);
2762 /* set the number of ports from the manufacturing description */
2763 /* serial->num_ports = serial->product_info.NumPorts; */
2764 if (edge_serial->product_info.NumPorts != serial->num_ports) {
2765 warn("%s - Device Reported %d serial ports vs core "
2766 "thinking we have %d ports, email greg@kroah.com this info.",
2767 __FUNCTION__, edge_serial->product_info.NumPorts,
2771 dbg("%s - time 1 %ld", __FUNCTION__, jiffies);
2773 /* now load the application firmware into this device */
2774 load_application_firmware (edge_serial);
2776 dbg("%s - time 2 %ld", __FUNCTION__, jiffies);
2778 /* Check current Edgeport EEPROM and update if necessary */
2779 update_edgeport_E2PROM (edge_serial);
2781 dbg("%s - time 3 %ld", __FUNCTION__, jiffies);
2783 /* set the configuration to use #1 */
2784 // dbg("set_configuration 1");
2785 // usb_set_configuration (dev, 1);
2787 /* we set up the pointers to the endpoints in the edge_open function,
2788 * as the structures aren't created yet. */
2790 /* set up our port private structures */
2791 for (i = 0; i < serial->num_ports; ++i) {
2792 edge_port = kmalloc (sizeof(struct edgeport_port), GFP_KERNEL);
2793 if (edge_port == NULL) {
2794 dev_err(&serial->dev->dev, "%s - Out of memory\n", __FUNCTION__);
2795 usb_set_serial_data(serial, NULL);
2799 memset (edge_port, 0, sizeof(struct edgeport_port));
2800 spin_lock_init(&edge_port->ep_lock);
2801 edge_port->port = serial->port[i];
2802 usb_set_serial_port_data(serial->port[i], edge_port);
2809 /****************************************************************************
2811 * This function is called whenever the device is removed from the usb bus.
2812 ****************************************************************************/
2813 static void edge_shutdown (struct usb_serial *serial)
2817 dbg("%s", __FUNCTION__);
2819 /* stop reads and writes on all ports */
2820 for (i=0; i < serial->num_ports; ++i) {
2821 kfree (usb_get_serial_port_data(serial->port[i]));
2822 usb_set_serial_port_data(serial->port[i], NULL);
2824 kfree (usb_get_serial_data(serial));
2825 usb_set_serial_data(serial, NULL);
2829 /****************************************************************************
2831 * This is called by the module subsystem, or on startup to initialize us
2832 ****************************************************************************/
2833 static int __init edgeport_init(void)
2837 retval = usb_serial_register(&edgeport_2port_device);
2839 goto failed_2port_device_register;
2840 retval = usb_serial_register(&edgeport_4port_device);
2842 goto failed_4port_device_register;
2843 retval = usb_serial_register(&edgeport_8port_device);
2845 goto failed_8port_device_register;
2846 retval = usb_register(&io_driver);
2848 goto failed_usb_register;
2849 info(DRIVER_DESC " " DRIVER_VERSION);
2852 failed_usb_register:
2853 usb_serial_deregister(&edgeport_8port_device);
2854 failed_8port_device_register:
2855 usb_serial_deregister(&edgeport_4port_device);
2856 failed_4port_device_register:
2857 usb_serial_deregister(&edgeport_2port_device);
2858 failed_2port_device_register:
2863 /****************************************************************************
2865 * Called when the driver is about to be unloaded.
2866 ****************************************************************************/
2867 static void __exit edgeport_exit (void)
2869 usb_deregister (&io_driver);
2870 usb_serial_deregister (&edgeport_2port_device);
2871 usb_serial_deregister (&edgeport_4port_device);
2872 usb_serial_deregister (&edgeport_8port_device);
2875 module_init(edgeport_init);
2876 module_exit(edgeport_exit);
2878 /* Module information */
2879 MODULE_AUTHOR( DRIVER_AUTHOR );
2880 MODULE_DESCRIPTION( DRIVER_DESC );
2881 MODULE_LICENSE("GPL");
2883 module_param(debug, bool, S_IRUGO | S_IWUSR);
2884 MODULE_PARM_DESC(debug, "Debug enabled or not");
2886 module_param(low_latency, bool, S_IRUGO | S_IWUSR);
2887 MODULE_PARM_DESC(low_latency, "Low latency enabled or not");