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:
28 * 2003_04_03 al borchers
29 * - fixed a bug (that shows up with dosemu) where the tty struct is
30 * used in a callback after it has been freed
32 * 2.3 2002_03_08 greg kroah-hartman
33 * - fixed bug when multiple devices were attached at the same time.
35 * 2.2 2001_11_14 greg kroah-hartman
36 * - fixed bug in edge_close that kept the port from being used more
38 * - fixed memory leak on device removal.
39 * - fixed potential double free of memory when command urb submitting
41 * - other small cleanups when the device is removed
43 * 2.1 2001_07_09 greg kroah-hartman
44 * - added support for TIOCMBIS and TIOCMBIC.
47 * - Identify version on module load.
49 * 2.0 2001_03_05 greg kroah-hartman
50 * - reworked entire driver to fit properly in with the other usb-serial
51 * drivers. Occasional oopses still happen, but it's a good start.
53 * 1.2.3 (02/23/2001) greg kroah-hartman
54 * - changed device table to work properly for 2.4.x final format.
55 * - fixed problem with dropping data at high data rates.
57 * 1.2.2 (11/27/2000) greg kroah-hartman
58 * - cleaned up more NTisms.
59 * - Added device table for 2.4.0-test11
61 * 1.2.1 (11/08/2000) greg kroah-hartman
62 * - Started to clean up NTisms.
63 * - Fixed problem with dev field of urb for kernels >= 2.4.0-test9
65 * 1.2 (10/17/2000) David Iacovelli
66 * Remove all EPIC code and GPL source
67 * Fix RELEVANT_IFLAG macro to include flow control
68 * changes port configuration changes.
69 * Fix redefinition of SERIAL_MAGIC
70 * Change all timeout values to 5 seconds
71 * Tried to fix the UHCI multiple urb submission, but failed miserably.
72 * it seems to work fine with OHCI.
73 * ( Greg take a look at the #if 0 at end of WriteCmdUsb() we must
74 * find a way to work arount this UHCI bug )
76 * 1.1 (10/11/2000) David Iacovelli
77 * Fix XON/XOFF flow control to support both IXON and IXOFF
79 * 0.9.27 (06/30/2000) David Iacovelli
80 * Added transmit queue and now allocate urb for command writes.
82 * 0.9.26 (06/29/2000) David Iacovelli
83 * Add support for 80251 based edgeport
85 * 0.9.25 (06/27/2000) David Iacovelli
86 * Do not close the port if it has multiple opens.
88 * 0.9.24 (05/26/2000) David Iacovelli
89 * Add IOCTLs to support RXTX and JAVA POS
90 * and first cut at running BlackBox Demo
92 * 0.9.23 (05/24/2000) David Iacovelli
93 * Add IOCTLs to support RXTX and JAVA POS
95 * 0.9.22 (05/23/2000) David Iacovelli
96 * fixed bug in enumeration. If epconfig turns on mapping by
97 * path after a device is already plugged in, we now update
98 * the mapping correctly
100 * 0.9.21 (05/16/2000) David Iacovelli
101 * Added BlockUntilChaseResp() to also wait for txcredits
102 * Updated the way we allocate and handle write URBs
103 * Add debug code to dump buffers
105 * 0.9.20 (05/01/2000) David Iacovelli
106 * change driver to use usb/tts/
108 * 0.9.19 (05/01/2000) David Iacovelli
109 * Update code to compile if DEBUG is off
111 * 0.9.18 (04/28/2000) David Iacovelli
112 * cleanup and test tty_register with devfs
114 * 0.9.17 (04/27/2000) greg kroah-hartman
115 * changed tty_register around to be like the way it
116 * was before, but now it works properly with devfs.
118 * 0.9.16 (04/26/2000) david iacovelli
119 * Fixed bug in GetProductInfo()
121 * 0.9.15 (04/25/2000) david iacovelli
122 * Updated enumeration
124 * 0.9.14 (04/24/2000) david iacovelli
125 * Removed all config/status IOCTLS and
126 * converted to using /proc/edgeport
127 * still playing with devfs
129 * 0.9.13 (04/24/2000) david iacovelli
130 * Removed configuration based on ttyUSB0
131 * Added support for configuration using /prod/edgeport
132 * first attempt at using devfs (not working yet!)
133 * Added IOCTL to GetProductInfo()
134 * Added support for custom baud rates
135 * Add support for random port numbers
137 * 0.9.12 (04/18/2000) david iacovelli
138 * added additional configuration IOCTLs
139 * use ttyUSB0 for configuration
141 * 0.9.11 (04/17/2000) greg kroah-hartman
142 * fixed module initialization race conditions.
143 * made all urbs dynamically allocated.
144 * made driver devfs compatible. now it only registers the tty device
145 * when the device is actually plugged in.
147 * 0.9.10 (04/13/2000) greg kroah-hartman
148 * added proc interface framework.
150 * 0.9.9 (04/13/2000) david iacovelli
151 * added enumeration code and ioctls to configure the device
153 * 0.9.8 (04/12/2000) david iacovelli
154 * Change interrupt read start when device is plugged in
155 * and stop when device is removed
156 * process interrupt reads when all ports are closed
157 * (keep value of rxBytesAvail consistent with the edgeport)
158 * set the USB_BULK_QUEUE flag so that we can shove a bunch
159 * of urbs at once down the pipe
161 * 0.9.7 (04/10/2000) david iacovelli
162 * start to add enumeration code.
163 * generate serial number for epic devices
164 * add support for kdb
166 * 0.9.6 (03/30/2000) david iacovelli
167 * add IOCTL to get string, manufacture, and boot descriptors
169 * 0.9.5 (03/14/2000) greg kroah-hartman
170 * more error checking added to SerialOpen to try to fix UHCI open problem
172 * 0.9.4 (03/09/2000) greg kroah-hartman
173 * added more error checking to handle oops when data is hanging
174 * around and tty is abruptly closed.
176 * 0.9.3 (03/09/2000) david iacovelli
177 * Add epic support for xon/xoff chars
178 * play with performance
180 * 0.9.2 (03/08/2000) greg kroah-hartman
181 * changed most "info" calls to "dbg"
182 * implemented flow control properly in the termios call
184 * 0.9.1 (03/08/2000) david iacovelli
186 * enabled bootloader update
188 * 0.9 (03/08/2000) greg kroah-hartman
189 * Release to IO networks.
190 * Integrated changes that David made
191 * made getting urbs for writing SMP safe
193 * 0.8 (03/07/2000) greg kroah-hartman
194 * Release to IO networks.
195 * Fixed problems that were seen in code by David.
196 * Now both Edgeport/4 and Edgeport/2 works properly.
197 * Changed most of the functions to use port instead of serial.
199 * 0.7 (02/27/2000) greg kroah-hartman
200 * Milestone 3 release.
201 * Release to IO Networks
202 * ioctl for waiting on line change implemented.
203 * ioctl for getting statistics implemented.
204 * multiport support working.
205 * lsr and msr registers are now handled properly.
206 * change break now hooked up and working.
207 * support for all known Edgeport devices.
209 * 0.6 (02/22/2000) greg kroah-hartman
210 * Release to IO networks.
211 * CHASE is implemented correctly when port is closed.
212 * SerialOpen now blocks correctly until port is fully opened.
214 * 0.5 (02/20/2000) greg kroah-hartman
215 * Release to IO networks.
217 * modem status register changes are not sent on to the user
218 * CHASE is not implemented when the port is closed.
220 * 0.4 (02/16/2000) greg kroah-hartman
221 * Second cut at the CeBit demo.
222 * Doesn't leak memory on every write to the port
223 * Still small leaks on startup.
224 * Added support for Edgeport/2 and Edgeport/8
226 * 0.3 (02/15/2000) greg kroah-hartman
227 * CeBit demo release.
228 * Force the line settings to 4800, 8, 1, e for the demo.
229 * Warning! This version leaks memory like crazy!
231 * 0.2 (01/30/2000) greg kroah-hartman
232 * Milestone 1 release.
233 * Device is found by USB subsystem, enumerated, fimware is downloaded
234 * and the descriptors are printed to the debug log, config is set, and
235 * green light starts to blink. Open port works, and data can be sent
236 * and received at the default settings of the UART. Loopback connector
237 * and debug log confirms this.
239 * 0.1 (01/23/2000) greg kroah-hartman
240 * Initial release to help IO Networks try to set up their test system.
241 * Edgeport4 is recognized, firmware is downloaded, config is set so
242 * device blinks green light every 3 sec. Port is bound, but opening,
243 * closing, and sending data do not work properly.
247 #include <linux/config.h>
248 #include <linux/kernel.h>
249 #include <linux/jiffies.h>
250 #include <linux/errno.h>
251 #include <linux/init.h>
252 #include <linux/slab.h>
253 #include <linux/tty.h>
254 #include <linux/tty_driver.h>
255 #include <linux/tty_flip.h>
256 #include <linux/module.h>
257 #include <linux/spinlock.h>
258 #include <linux/serial.h>
259 #include <linux/ioctl.h>
260 #include <asm/uaccess.h>
261 #include <linux/usb.h>
262 #include "usb-serial.h"
263 #include "io_edgeport.h"
264 #include "io_ionsp.h" /* info for the iosp messages */
265 #include "io_16654.h" /* 16654 UART defines */
268 * Version Information
270 #define DRIVER_VERSION "v2.3"
271 #define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com> and David Iacovelli"
272 #define DRIVER_DESC "Edgeport USB Serial Driver"
274 /* First, the latest boot code - for first generation edgeports */
275 #define IMAGE_ARRAY_NAME BootCodeImage_GEN1
276 #define IMAGE_VERSION_NAME BootCodeImageVersion_GEN1
277 #include "io_fw_boot.h" /* the bootloader firmware to download to a device, if it needs it */
279 /* for second generation edgeports */
280 #define IMAGE_ARRAY_NAME BootCodeImage_GEN2
281 #define IMAGE_VERSION_NAME BootCodeImageVersion_GEN2
282 #include "io_fw_boot2.h" /* the bootloader firmware to download to a device, if it needs it */
284 /* Then finally the main run-time operational code - for first generation edgeports */
285 #define IMAGE_ARRAY_NAME OperationalCodeImage_GEN1
286 #define IMAGE_VERSION_NAME OperationalCodeImageVersion_GEN1
287 #include "io_fw_down.h" /* Define array OperationalCodeImage[] */
289 /* for second generation edgeports */
290 #define IMAGE_ARRAY_NAME OperationalCodeImage_GEN2
291 #define IMAGE_VERSION_NAME OperationalCodeImageVersion_GEN2
292 #include "io_fw_down2.h" /* Define array OperationalCodeImage[] */
294 #define MAX_NAME_LEN 64
296 #define CHASE_TIMEOUT (5*HZ) /* 5 seconds */
297 #define OPEN_TIMEOUT (5*HZ) /* 5 seconds */
298 #define COMMAND_TIMEOUT (5*HZ) /* 5 seconds */
302 /* receive port state */
304 EXPECT_HDR1 = 0, /* Expect header byte 1 */
305 EXPECT_HDR2 = 1, /* Expect header byte 2 */
306 EXPECT_DATA = 2, /* Expect 'RxBytesRemaining' data */
307 EXPECT_HDR3 = 3, /* Expect header byte 3 (for status hdrs only) */
312 * This Transmit queue is an extension of the edgeport Rx buffer.
313 * The maximum amount of data buffered in both the edgeport
314 * Rx buffer (maxTxCredits) and this buffer will never exceed maxTxCredits.
317 unsigned int head; /* index to head pointer (write) */
318 unsigned int tail; /* index to tail pointer (read) */
319 unsigned int count; /* Bytes in queue */
320 unsigned int size; /* Max size of queue (equal to Max number of TxCredits) */
321 unsigned char *fifo; /* allocated Buffer */
324 /* This structure holds all of the local port information */
325 struct edgeport_port {
326 __u16 txCredits; /* our current credits for this port */
327 __u16 maxTxCredits; /* the max size of the port */
329 struct TxFifo txfifo; /* transmit fifo -- size will be maxTxCredits */
330 struct urb *write_urb; /* write URB for this port */
331 char write_in_progress; /* TRUE while a write URB is outstanding */
333 __u8 shadowLCR; /* last LCR value received */
334 __u8 shadowMCR; /* last MCR value received */
335 __u8 shadowMSR; /* last MSR value received */
336 __u8 shadowLSR; /* last LSR value received */
337 __u8 shadowXonChar; /* last value set as XON char in Edgeport */
338 __u8 shadowXoffChar; /* last value set as XOFF char in Edgeport */
346 char chaseResponsePending;
348 wait_queue_head_t wait_chase; /* for handling sleeping while waiting for chase to finish */
349 wait_queue_head_t wait_open; /* for handling sleeping while waiting for open to finish */
350 wait_queue_head_t wait_command; /* for handling sleeping while waiting for command to finish */
351 wait_queue_head_t delta_msr_wait; /* for handling sleeping while waiting for msr change to happen */
353 struct async_icount icount;
354 struct usb_serial_port *port; /* loop back to the owner of this object */
358 /* This structure holds all of the individual device information */
359 struct edgeport_serial {
360 char name[MAX_NAME_LEN+1]; /* string name of this device */
362 struct edge_manuf_descriptor manuf_descriptor; /* the manufacturer descriptor */
363 struct edge_boot_descriptor boot_descriptor; /* the boot firmware descriptor */
364 struct edgeport_product_info product_info; /* Product Info */
366 __u8 interrupt_in_endpoint; /* the interrupt endpoint handle */
367 unsigned char * interrupt_in_buffer; /* the buffer we use for the interrupt endpoint */
368 struct urb * interrupt_read_urb; /* our interrupt urb */
370 __u8 bulk_in_endpoint; /* the bulk in endpoint handle */
371 unsigned char * bulk_in_buffer; /* the buffer we use for the bulk in endpoint */
372 struct urb * read_urb; /* our bulk read urb */
374 __u8 bulk_out_endpoint; /* the bulk out endpoint handle */
376 __s16 rxBytesAvail; /* the number of bytes that we need to read from this device */
378 enum RXSTATE rxState; /* the current state of the bulk receive processor */
379 __u8 rxHeader1; /* receive header byte 1 */
380 __u8 rxHeader2; /* receive header byte 2 */
381 __u8 rxHeader3; /* receive header byte 3 */
382 __u8 rxPort; /* the port that we are currently receiving data for */
383 __u8 rxStatusCode; /* the receive status code */
384 __u8 rxStatusParam; /* the receive status paramater */
385 __s16 rxBytesRemaining; /* the number of port bytes left to read */
386 struct usb_serial *serial; /* loop back to the owner of this object */
389 /* baud rate information */
390 struct divisor_table_entry {
396 // Define table of divisors for Rev A EdgePort/4 hardware
397 // These assume a 3.6864MHz crystal, the standard /16, and
400 static struct divisor_table_entry divisor_table[] = {
403 { 110, 2095}, /* 2094.545455 => 230450 => .0217 % over */
404 { 134, 1713}, /* 1713.011152 => 230398.5 => .00065% under */
422 /* local variables */
423 static int CmdUrbs = 0; /* Number of outstanding Command Write Urbs */
426 /* local function prototypes */
428 /* function prototypes for all URB callbacks */
429 static void edge_interrupt_callback (struct urb *urb, struct pt_regs *regs);
430 static void edge_bulk_in_callback (struct urb *urb, struct pt_regs *regs);
431 static void edge_bulk_out_data_callback (struct urb *urb, struct pt_regs *regs);
432 static void edge_bulk_out_cmd_callback (struct urb *urb, struct pt_regs *regs);
434 /* function prototypes for the usbserial callbacks */
435 static int edge_open (struct usb_serial_port *port, struct file *filp);
436 static void edge_close (struct usb_serial_port *port, struct file *filp);
437 static int edge_write (struct usb_serial_port *port, int from_user, const unsigned char *buf, int count);
438 static int edge_write_room (struct usb_serial_port *port);
439 static int edge_chars_in_buffer (struct usb_serial_port *port);
440 static void edge_throttle (struct usb_serial_port *port);
441 static void edge_unthrottle (struct usb_serial_port *port);
442 static void edge_set_termios (struct usb_serial_port *port, struct termios *old_termios);
443 static int edge_ioctl (struct usb_serial_port *port, struct file *file, unsigned int cmd, unsigned long arg);
444 static void edge_break (struct usb_serial_port *port, int break_state);
445 static int edge_tiocmget (struct usb_serial_port *port, struct file *file);
446 static int edge_tiocmset (struct usb_serial_port *port, struct file *file, unsigned int set, unsigned int clear);
447 static int edge_startup (struct usb_serial *serial);
448 static void edge_shutdown (struct usb_serial *serial);
451 #include "io_tables.h" /* all of the devices that this driver supports */
453 static struct usb_driver io_driver = {
454 .owner = THIS_MODULE,
455 .name = "io_edgeport",
456 .probe = usb_serial_probe,
457 .disconnect = usb_serial_disconnect,
458 .id_table = id_table_combined,
461 /* function prototypes for all of our local functions */
462 static int process_rcvd_data (struct edgeport_serial *edge_serial, unsigned char *buffer, __u16 bufferLength);
463 static void process_rcvd_status (struct edgeport_serial *edge_serial, __u8 byte2, __u8 byte3);
464 static void handle_new_msr (struct edgeport_port *edge_port, __u8 newMsr);
465 static void handle_new_lsr (struct edgeport_port *edge_port, __u8 lsrData, __u8 lsr, __u8 data);
466 static int send_iosp_ext_cmd (struct edgeport_port *edge_port, __u8 command, __u8 param);
467 static int calc_baud_rate_divisor (int baud_rate, int *divisor);
468 static int send_cmd_write_baud_rate (struct edgeport_port *edge_port, int baudRate);
469 static void change_port_settings (struct edgeport_port *edge_port, struct termios *old_termios);
470 static int send_cmd_write_uart_register (struct edgeport_port *edge_port, __u8 regNum, __u8 regValue);
471 static int write_cmd_usb (struct edgeport_port *edge_port, unsigned char *buffer, int writeLength);
472 static void send_more_port_data (struct edgeport_serial *edge_serial, struct edgeport_port *edge_port);
474 static int sram_write (struct usb_serial *serial, __u16 extAddr, __u16 addr, __u16 length, __u8 *data);
475 static int rom_read (struct usb_serial *serial, __u16 extAddr, __u16 addr, __u16 length, __u8 *data);
476 static int rom_write (struct usb_serial *serial, __u16 extAddr, __u16 addr, __u16 length, __u8 *data);
477 static void get_manufacturing_desc (struct edgeport_serial *edge_serial);
478 static void get_boot_desc (struct edgeport_serial *edge_serial);
479 static void load_application_firmware (struct edgeport_serial *edge_serial);
482 static void unicode_to_ascii (char *string, __le16 *unicode, int unicode_size);
488 // ************************************************************************
489 // ************************************************************************
490 // ************************************************************************
491 // ************************************************************************
493 /************************************************************************
495 * update_edgeport_E2PROM() Compare current versions of *
496 * Boot ROM and Manufacture *
497 * Descriptors with versions *
498 * embedded in this driver *
500 ************************************************************************/
501 static void update_edgeport_E2PROM (struct edgeport_serial *edge_serial)
505 __u8 BootMajorVersion;
506 __u8 BootMinorVersion;
507 __le16 BootBuildNumber;
510 struct edge_firmware_image_record *record;
511 unsigned char *firmware;
515 switch (edge_serial->product_info.iDownloadFile) {
516 case EDGE_DOWNLOAD_FILE_I930:
517 BootMajorVersion = BootCodeImageVersion_GEN1.MajorVersion;
518 BootMinorVersion = BootCodeImageVersion_GEN1.MinorVersion;
519 BootBuildNumber = cpu_to_le16(BootCodeImageVersion_GEN1.BuildNumber);
520 BootImage = &BootCodeImage_GEN1[0];
521 BootSize = sizeof( BootCodeImage_GEN1 );
524 case EDGE_DOWNLOAD_FILE_80251:
525 BootMajorVersion = BootCodeImageVersion_GEN2.MajorVersion;
526 BootMinorVersion = BootCodeImageVersion_GEN2.MinorVersion;
527 BootBuildNumber = cpu_to_le16(BootCodeImageVersion_GEN2.BuildNumber);
528 BootImage = &BootCodeImage_GEN2[0];
529 BootSize = sizeof( BootCodeImage_GEN2 );
536 // Check Boot Image Version
537 BootCurVer = (edge_serial->boot_descriptor.MajorVersion << 24) +
538 (edge_serial->boot_descriptor.MinorVersion << 16) +
539 le16_to_cpu(edge_serial->boot_descriptor.BuildNumber);
541 BootNewVer = (BootMajorVersion << 24) +
542 (BootMinorVersion << 16) +
543 le16_to_cpu(BootBuildNumber);
545 dbg("Current Boot Image version %d.%d.%d",
546 edge_serial->boot_descriptor.MajorVersion,
547 edge_serial->boot_descriptor.MinorVersion,
548 le16_to_cpu(edge_serial->boot_descriptor.BuildNumber));
551 if (BootNewVer > BootCurVer) {
552 dbg("**Update Boot Image from %d.%d.%d to %d.%d.%d",
553 edge_serial->boot_descriptor.MajorVersion,
554 edge_serial->boot_descriptor.MinorVersion,
555 le16_to_cpu(edge_serial->boot_descriptor.BuildNumber),
558 le16_to_cpu(BootBuildNumber));
561 dbg("Downloading new Boot Image");
563 firmware = BootImage;
566 record = (struct edge_firmware_image_record *)firmware;
567 response = rom_write (edge_serial->serial, le16_to_cpu(record->ExtAddr), le16_to_cpu(record->Addr), le16_to_cpu(record->Len), &record->Data[0]);
569 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));
572 firmware += sizeof (struct edge_firmware_image_record) + le16_to_cpu(record->Len);
573 if (firmware >= &BootImage[BootSize]) {
578 dbg("Boot Image -- already up to date");
583 /************************************************************************
585 * Get string descriptor from device *
587 ************************************************************************/
588 static int get_string (struct usb_device *dev, int Id, char *string)
590 struct usb_string_descriptor StringDesc;
591 struct usb_string_descriptor *pStringDesc;
593 dbg("%s - USB String ID = %d", __FUNCTION__, Id );
595 if (!usb_get_descriptor(dev, USB_DT_STRING, Id, &StringDesc, sizeof(StringDesc))) {
599 pStringDesc = kmalloc (StringDesc.bLength, GFP_KERNEL);
605 if (!usb_get_descriptor(dev, USB_DT_STRING, Id, pStringDesc, StringDesc.bLength )) {
610 unicode_to_ascii(string, pStringDesc->wData, pStringDesc->bLength/2-1);
613 return strlen(string);
618 /************************************************************************
620 * Get string descriptor from device
622 ************************************************************************/
623 static int get_string_desc (struct usb_device *dev, int Id, struct usb_string_descriptor **pRetDesc)
625 struct usb_string_descriptor StringDesc;
626 struct usb_string_descriptor *pStringDesc;
628 dbg("%s - USB String ID = %d", __FUNCTION__, Id );
630 if (!usb_get_descriptor(dev, USB_DT_STRING, Id, &StringDesc, sizeof(StringDesc))) {
634 pStringDesc = kmalloc (StringDesc.bLength, GFP_KERNEL);
640 if (!usb_get_descriptor(dev, USB_DT_STRING, Id, pStringDesc, StringDesc.bLength )) {
645 *pRetDesc = pStringDesc;
650 static void get_product_info(struct edgeport_serial *edge_serial)
652 struct edgeport_product_info *product_info = &edge_serial->product_info;
654 memset (product_info, 0, sizeof(struct edgeport_product_info));
656 product_info->ProductId = (__u16)(edge_serial->serial->dev->descriptor.idProduct & ~ION_DEVICE_ID_80251_NETCHIP);
657 product_info->NumPorts = edge_serial->manuf_descriptor.NumPorts;
658 product_info->ProdInfoVer = 0;
660 product_info->RomSize = edge_serial->manuf_descriptor.RomSize;
661 product_info->RamSize = edge_serial->manuf_descriptor.RamSize;
662 product_info->CpuRev = edge_serial->manuf_descriptor.CpuRev;
663 product_info->BoardRev = edge_serial->manuf_descriptor.BoardRev;
665 product_info->BootMajorVersion = edge_serial->boot_descriptor.MajorVersion;
666 product_info->BootMinorVersion = edge_serial->boot_descriptor.MinorVersion;
667 product_info->BootBuildNumber = edge_serial->boot_descriptor.BuildNumber;
669 memcpy(product_info->ManufactureDescDate, edge_serial->manuf_descriptor.DescDate, sizeof(edge_serial->manuf_descriptor.DescDate));
671 // check if this is 2nd generation hardware
672 if (edge_serial->serial->dev->descriptor.idProduct & ION_DEVICE_ID_80251_NETCHIP) {
673 product_info->FirmwareMajorVersion = OperationalCodeImageVersion_GEN2.MajorVersion;
674 product_info->FirmwareMinorVersion = OperationalCodeImageVersion_GEN2.MinorVersion;
675 product_info->FirmwareBuildNumber = cpu_to_le16(OperationalCodeImageVersion_GEN2.BuildNumber);
676 product_info->iDownloadFile = EDGE_DOWNLOAD_FILE_80251;
678 product_info->FirmwareMajorVersion = OperationalCodeImageVersion_GEN1.MajorVersion;
679 product_info->FirmwareMinorVersion = OperationalCodeImageVersion_GEN1.MinorVersion;
680 product_info->FirmwareBuildNumber = cpu_to_le16(OperationalCodeImageVersion_GEN1.BuildNumber);
681 product_info->iDownloadFile = EDGE_DOWNLOAD_FILE_I930;
684 // Determine Product type and set appropriate flags
685 switch (DEVICE_ID_FROM_USB_PRODUCT_ID(product_info->ProductId)) {
686 case ION_DEVICE_ID_EDGEPORT_COMPATIBLE:
687 case ION_DEVICE_ID_EDGEPORT_4T:
688 case ION_DEVICE_ID_EDGEPORT_4:
689 case ION_DEVICE_ID_EDGEPORT_2:
690 case ION_DEVICE_ID_EDGEPORT_8_DUAL_CPU:
691 case ION_DEVICE_ID_EDGEPORT_8:
692 case ION_DEVICE_ID_EDGEPORT_421:
693 case ION_DEVICE_ID_EDGEPORT_21:
694 case ION_DEVICE_ID_EDGEPORT_2_DIN:
695 case ION_DEVICE_ID_EDGEPORT_4_DIN:
696 case ION_DEVICE_ID_EDGEPORT_16_DUAL_CPU:
697 product_info->IsRS232 = 1;
700 case ION_DEVICE_ID_EDGEPORT_2I: // Edgeport/2 RS422/RS485
701 product_info->IsRS422 = 1;
702 product_info->IsRS485 = 1;
705 case ION_DEVICE_ID_EDGEPORT_8I: // Edgeport/4 RS422
706 case ION_DEVICE_ID_EDGEPORT_4I: // Edgeport/4 RS422
707 product_info->IsRS422 = 1;
711 // Dump Product Info structure
712 dbg("**Product Information:");
713 dbg(" ProductId %x", product_info->ProductId );
714 dbg(" NumPorts %d", product_info->NumPorts );
715 dbg(" ProdInfoVer %d", product_info->ProdInfoVer );
716 dbg(" IsServer %d", product_info->IsServer);
717 dbg(" IsRS232 %d", product_info->IsRS232 );
718 dbg(" IsRS422 %d", product_info->IsRS422 );
719 dbg(" IsRS485 %d", product_info->IsRS485 );
720 dbg(" RomSize %d", product_info->RomSize );
721 dbg(" RamSize %d", product_info->RamSize );
722 dbg(" CpuRev %x", product_info->CpuRev );
723 dbg(" BoardRev %x", product_info->BoardRev);
724 dbg(" BootMajorVersion %d.%d.%d", product_info->BootMajorVersion,
725 product_info->BootMinorVersion,
726 le16_to_cpu(product_info->BootBuildNumber));
727 dbg(" FirmwareMajorVersion %d.%d.%d", product_info->FirmwareMajorVersion,
728 product_info->FirmwareMinorVersion,
729 le16_to_cpu(product_info->FirmwareBuildNumber));
730 dbg(" ManufactureDescDate %d/%d/%d", product_info->ManufactureDescDate[0],
731 product_info->ManufactureDescDate[1],
732 product_info->ManufactureDescDate[2]+1900);
733 dbg(" iDownloadFile 0x%x", product_info->iDownloadFile);
738 /************************************************************************/
739 /************************************************************************/
740 /* U S B C A L L B A C K F U N C T I O N S */
741 /* U S B C A L L B A C K F U N C T I O N S */
742 /************************************************************************/
743 /************************************************************************/
745 /*****************************************************************************
746 * edge_interrupt_callback
747 * this is the callback function for when we have received data on the
748 * interrupt endpoint.
749 *****************************************************************************/
750 static void edge_interrupt_callback (struct urb *urb, struct pt_regs *regs)
752 struct edgeport_serial *edge_serial = (struct edgeport_serial *)urb->context;
753 struct edgeport_port *edge_port;
754 struct usb_serial_port *port;
755 unsigned char *data = urb->transfer_buffer;
756 int length = urb->actual_length;
763 dbg("%s", __FUNCTION__);
765 switch (urb->status) {
772 /* this urb is terminated, clean up */
773 dbg("%s - urb shutting down with status: %d", __FUNCTION__, urb->status);
776 dbg("%s - nonzero urb status received: %d", __FUNCTION__, urb->status);
780 // process this interrupt-read even if there are no ports open
782 usb_serial_debug_data(debug, &edge_serial->serial->dev->dev, __FUNCTION__, length, data);
785 bytes_avail = data[0] | (data[1] << 8);
787 edge_serial->rxBytesAvail += bytes_avail;
788 dbg("%s - bytes_avail = %d, rxBytesAvail %d", __FUNCTION__, bytes_avail, edge_serial->rxBytesAvail);
790 if ((edge_serial->rxBytesAvail > 0) &&
791 (edge_serial->read_urb->status != -EINPROGRESS)) {
792 dbg(" --- Posting a read");
794 /* we have pending bytes on the bulk in pipe, send a request */
795 edge_serial->read_urb->dev = edge_serial->serial->dev;
796 result = usb_submit_urb(edge_serial->read_urb, GFP_ATOMIC);
798 dbg("%s - usb_submit_urb(read bulk) failed with result = %d", __FUNCTION__, result);
803 /* grab the txcredits for the ports if available */
806 while ((position < length) && (portNumber < edge_serial->serial->num_ports)) {
807 txCredits = data[position] | (data[position+1] << 8);
809 port = edge_serial->serial->port[portNumber];
810 edge_port = usb_get_serial_port_data(port);
811 if (edge_port->open) {
812 edge_port->txCredits += txCredits;
813 dbg("%s - txcredits for port%d = %d", __FUNCTION__, portNumber, edge_port->txCredits);
815 /* tell the tty driver that something has changed */
816 if (edge_port->port->tty)
817 wake_up_interruptible(&edge_port->port->tty->write_wait);
819 // Since we have more credit, check if more data can be sent
820 send_more_port_data(edge_serial, edge_port);
829 result = usb_submit_urb (urb, GFP_ATOMIC);
831 dev_err(&urb->dev->dev, "%s - Error %d submitting control urb\n", __FUNCTION__, result);
836 /*****************************************************************************
837 * edge_bulk_in_callback
838 * this is the callback function for when we have received data on the
840 *****************************************************************************/
841 static void edge_bulk_in_callback (struct urb *urb, struct pt_regs *regs)
843 struct edgeport_serial *edge_serial = (struct edgeport_serial *)urb->context;
844 unsigned char *data = urb->transfer_buffer;
846 __u16 raw_data_length;
848 dbg("%s", __FUNCTION__);
851 dbg("%s - nonzero read bulk status received: %d", __FUNCTION__, urb->status);
855 if (urb->actual_length) {
856 raw_data_length = urb->actual_length;
858 usb_serial_debug_data(debug, &edge_serial->serial->dev->dev, __FUNCTION__, raw_data_length, data);
860 /* decrement our rxBytes available by the number that we just got */
861 edge_serial->rxBytesAvail -= raw_data_length;
863 dbg("%s - Received = %d, rxBytesAvail %d", __FUNCTION__, raw_data_length, edge_serial->rxBytesAvail);
865 process_rcvd_data (edge_serial, data, urb->actual_length);
867 /* check to see if there's any more data for us to read */
868 if ((edge_serial->rxBytesAvail > 0) &&
869 (edge_serial->read_urb->status != -EINPROGRESS)) {
870 dbg(" --- Posting a read");
872 /* there is, so resubmit our urb */
873 edge_serial->read_urb->dev = edge_serial->serial->dev;
874 status = usb_submit_urb(edge_serial->read_urb, GFP_ATOMIC);
876 dev_err(&urb->dev->dev, "%s - usb_submit_urb(read bulk) failed, status = %d\n", __FUNCTION__, status);
883 /*****************************************************************************
884 * edge_bulk_out_data_callback
885 * this is the callback function for when we have finished sending serial data
886 * on the bulk out endpoint.
887 *****************************************************************************/
888 static void edge_bulk_out_data_callback (struct urb *urb, struct pt_regs *regs)
890 struct edgeport_port *edge_port = (struct edgeport_port *)urb->context;
891 struct tty_struct *tty;
893 dbg("%s", __FUNCTION__);
896 dbg("%s - nonzero write bulk status received: %d", __FUNCTION__, urb->status);
899 tty = edge_port->port->tty;
901 if (tty && edge_port->open) {
902 /* let the tty driver wakeup if it has a special write_wakeup function */
906 // Release the Write URB
907 edge_port->write_in_progress = FALSE;
909 // Check if more data needs to be sent
910 send_more_port_data((struct edgeport_serial *)(usb_get_serial_data(edge_port->port->serial)), edge_port);
914 /*****************************************************************************
916 * this is the callback function for when we have finished sending a command
917 * on the bulk out endpoint.
918 *****************************************************************************/
919 static void edge_bulk_out_cmd_callback (struct urb *urb, struct pt_regs *regs)
921 struct edgeport_port *edge_port = (struct edgeport_port *)urb->context;
922 struct tty_struct *tty;
923 int status = urb->status;
925 dbg("%s", __FUNCTION__);
928 dbg("%s - FREE URB %p (outstanding %d)", __FUNCTION__, urb, CmdUrbs);
931 /* clean up the transfer buffer */
932 if (urb->transfer_buffer != NULL) {
933 kfree(urb->transfer_buffer);
936 /* Free the command urb */
940 dbg("%s - nonzero write bulk status received: %d", __FUNCTION__, status);
944 /* Get pointer to tty */
945 tty = edge_port->port->tty;
947 /* tell the tty driver that something has changed */
948 if (tty && edge_port->open)
949 wake_up_interruptible(&tty->write_wait);
951 /* we have completed the command */
952 edge_port->commandPending = FALSE;
953 wake_up_interruptible(&edge_port->wait_command);
957 /*****************************************************************************
958 * Driver tty interface functions
959 *****************************************************************************/
961 /*****************************************************************************
963 * this function is called by the tty driver when a port is opened
964 * If successful, we return 0
965 * Otherwise we return a negative error number.
966 *****************************************************************************/
967 static int edge_open (struct usb_serial_port *port, struct file * filp)
969 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
970 struct usb_serial *serial;
971 struct edgeport_serial *edge_serial;
975 dbg("%s - port %d", __FUNCTION__, port->number);
977 if (edge_port == NULL)
980 /* force low_latency on so that our tty_push actually forces the data through,
981 otherwise it is scheduled, and with high data rates (like with OHCI) data
984 port->tty->low_latency = 1;
986 /* see if we've set up our endpoint info yet (can't set it up in edge_startup
987 as the structures were not set up at that time.) */
988 serial = port->serial;
989 edge_serial = usb_get_serial_data(serial);
990 if (edge_serial == NULL) {
993 if (edge_serial->interrupt_in_buffer == NULL) {
994 struct usb_serial_port *port0 = serial->port[0];
996 /* not set up yet, so do it now */
997 edge_serial->interrupt_in_buffer = port0->interrupt_in_buffer;
998 edge_serial->interrupt_in_endpoint = port0->interrupt_in_endpointAddress;
999 edge_serial->interrupt_read_urb = port0->interrupt_in_urb;
1000 edge_serial->bulk_in_buffer = port0->bulk_in_buffer;
1001 edge_serial->bulk_in_endpoint = port0->bulk_in_endpointAddress;
1002 edge_serial->read_urb = port0->read_urb;
1003 edge_serial->bulk_out_endpoint = port0->bulk_out_endpointAddress;
1005 /* set up our interrupt urb */
1006 usb_fill_int_urb(edge_serial->interrupt_read_urb,
1008 usb_rcvintpipe(serial->dev,
1009 port0->interrupt_in_endpointAddress),
1010 port0->interrupt_in_buffer,
1011 edge_serial->interrupt_read_urb->transfer_buffer_length,
1012 edge_interrupt_callback, edge_serial,
1013 edge_serial->interrupt_read_urb->interval);
1015 /* set up our bulk in urb */
1016 usb_fill_bulk_urb(edge_serial->read_urb, serial->dev,
1017 usb_rcvbulkpipe(serial->dev,
1018 port0->bulk_in_endpointAddress),
1019 port0->bulk_in_buffer,
1020 edge_serial->read_urb->transfer_buffer_length,
1021 edge_bulk_in_callback, edge_serial);
1023 /* start interrupt read for this edgeport
1024 * this interrupt will continue as long as the edgeport is connected */
1025 response = usb_submit_urb (edge_serial->interrupt_read_urb, GFP_KERNEL);
1027 dev_err(&port->dev, "%s - Error %d submitting control urb\n", __FUNCTION__, response);
1031 /* initialize our wait queues */
1032 init_waitqueue_head(&edge_port->wait_open);
1033 init_waitqueue_head(&edge_port->wait_chase);
1034 init_waitqueue_head(&edge_port->delta_msr_wait);
1035 init_waitqueue_head(&edge_port->wait_command);
1037 /* initialize our icount structure */
1038 memset (&(edge_port->icount), 0x00, sizeof(edge_port->icount));
1040 /* initialize our port settings */
1041 edge_port->txCredits = 0; /* Can't send any data yet */
1042 edge_port->shadowMCR = MCR_MASTER_IE; /* Must always set this bit to enable ints! */
1043 edge_port->chaseResponsePending = FALSE;
1045 /* send a open port command */
1046 edge_port->openPending = TRUE;
1047 edge_port->open = FALSE;
1048 response = send_iosp_ext_cmd (edge_port, IOSP_CMD_OPEN_PORT, 0);
1051 dev_err(&port->dev, "%s - error sending open port command\n", __FUNCTION__);
1052 edge_port->openPending = FALSE;
1056 /* now wait for the port to be completely opened */
1057 timeout = OPEN_TIMEOUT;
1058 while (timeout && edge_port->openPending == TRUE) {
1059 timeout = interruptible_sleep_on_timeout (&edge_port->wait_open, timeout);
1062 if (edge_port->open == FALSE) {
1063 /* open timed out */
1064 dbg("%s - open timedout", __FUNCTION__);
1065 edge_port->openPending = FALSE;
1069 /* create the txfifo */
1070 edge_port->txfifo.head = 0;
1071 edge_port->txfifo.tail = 0;
1072 edge_port->txfifo.count = 0;
1073 edge_port->txfifo.size = edge_port->maxTxCredits;
1074 edge_port->txfifo.fifo = kmalloc (edge_port->maxTxCredits, GFP_KERNEL);
1076 if (!edge_port->txfifo.fifo) {
1077 dbg("%s - no memory", __FUNCTION__);
1078 edge_close (port, filp);
1082 /* Allocate a URB for the write */
1083 edge_port->write_urb = usb_alloc_urb (0, GFP_KERNEL);
1085 if (!edge_port->write_urb) {
1086 dbg("%s - no memory", __FUNCTION__);
1087 edge_close (port, filp);
1091 dbg("%s(%d) - Initialize TX fifo to %d bytes", __FUNCTION__, port->number, edge_port->maxTxCredits);
1093 dbg("%s exited", __FUNCTION__);
1099 /************************************************************************
1101 * block_until_chase_response
1103 * This function will block the close until one of the following:
1104 * 1. Response to our Chase comes from Edgeport
1105 * 2. A timout of 10 seconds without activity has expired
1106 * (1K of Edgeport data @ 2400 baud ==> 4 sec to empty)
1108 ************************************************************************/
1109 static void block_until_chase_response(struct edgeport_port *edge_port)
1116 // Save Last credits
1117 lastCredits = edge_port->txCredits;
1119 // Did we get our Chase response
1120 if (edge_port->chaseResponsePending == FALSE) {
1121 dbg("%s - Got Chase Response", __FUNCTION__);
1123 // did we get all of our credit back?
1124 if (edge_port->txCredits == edge_port->maxTxCredits ) {
1125 dbg("%s - Got all credits", __FUNCTION__);
1130 // Block the thread for a while
1131 interruptible_sleep_on_timeout (&edge_port->wait_chase, timeout);
1133 if (lastCredits == edge_port->txCredits) {
1134 // No activity.. count down.
1137 edge_port->chaseResponsePending = FALSE;
1138 dbg("%s - Chase TIMEOUT", __FUNCTION__);
1142 // Reset timout value back to 10 seconds
1143 dbg("%s - Last %d, Current %d", __FUNCTION__, lastCredits, edge_port->txCredits);
1150 /************************************************************************
1152 * block_until_tx_empty
1154 * This function will block the close until one of the following:
1156 * 2. The edgeport has stopped
1157 * 3. A timout of 3 seconds without activity has expired
1159 ************************************************************************/
1160 static void block_until_tx_empty (struct edgeport_port *edge_port)
1162 struct TxFifo *fifo = &edge_port->txfifo;
1164 int timeout = HZ/10;
1169 lastCount = fifo->count;
1171 // Is the Edgeport Buffer empty?
1172 if (lastCount == 0) {
1173 dbg("%s - TX Buffer Empty", __FUNCTION__);
1177 // Block the thread for a while
1178 interruptible_sleep_on_timeout (&edge_port->wait_chase, timeout);
1180 dbg("%s wait", __FUNCTION__);
1182 if (lastCount == fifo->count) {
1183 // No activity.. count down.
1186 dbg("%s - TIMEOUT", __FUNCTION__);
1190 // Reset timout value back to seconds
1197 /*****************************************************************************
1199 * this function is called by the tty driver when a port is closed
1200 *****************************************************************************/
1201 static void edge_close (struct usb_serial_port *port, struct file * filp)
1203 struct edgeport_serial *edge_serial;
1204 struct edgeport_port *edge_port;
1207 dbg("%s - port %d", __FUNCTION__, port->number);
1209 edge_serial = usb_get_serial_data(port->serial);
1210 edge_port = usb_get_serial_port_data(port);
1211 if ((edge_serial == NULL) || (edge_port == NULL))
1214 // block until tx is empty
1215 block_until_tx_empty(edge_port);
1217 edge_port->closePending = TRUE;
1219 /* flush and chase */
1220 edge_port->chaseResponsePending = TRUE;
1222 dbg("%s - Sending IOSP_CMD_CHASE_PORT", __FUNCTION__);
1223 status = send_iosp_ext_cmd (edge_port, IOSP_CMD_CHASE_PORT, 0);
1225 // block until chase finished
1226 block_until_chase_response(edge_port);
1228 edge_port->chaseResponsePending = FALSE;
1231 /* close the port */
1232 dbg("%s - Sending IOSP_CMD_CLOSE_PORT", __FUNCTION__);
1233 send_iosp_ext_cmd (edge_port, IOSP_CMD_CLOSE_PORT, 0);
1235 //port->close = TRUE;
1236 edge_port->closePending = FALSE;
1237 edge_port->open = FALSE;
1238 edge_port->openPending = FALSE;
1240 if (edge_port->write_urb) {
1241 usb_unlink_urb (edge_port->write_urb);
1244 if (edge_port->write_urb) {
1245 /* if this urb had a transfer buffer already (old transfer) free it */
1246 if (edge_port->write_urb->transfer_buffer != NULL) {
1247 kfree(edge_port->write_urb->transfer_buffer);
1249 usb_free_urb (edge_port->write_urb);
1251 if (edge_port->txfifo.fifo) {
1252 kfree(edge_port->txfifo.fifo);
1255 dbg("%s exited", __FUNCTION__);
1258 /*****************************************************************************
1260 * this function is called by the tty driver when data should be written to
1262 * If successful, we return the number of bytes written, otherwise we return
1263 * a negative error number.
1264 *****************************************************************************/
1265 static int edge_write (struct usb_serial_port *port, int from_user, const unsigned char *data, int count)
1267 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1268 struct TxFifo *fifo;
1274 dbg("%s - port %d", __FUNCTION__, port->number);
1276 if (edge_port == NULL)
1279 // get a pointer to the Tx fifo
1280 fifo = &edge_port->txfifo;
1282 // calculate number of bytes to put in fifo
1283 copySize = min ((unsigned int)count, (edge_port->txCredits - fifo->count));
1285 dbg("%s(%d) of %d byte(s) Fifo room %d -- will copy %d bytes", __FUNCTION__,
1286 port->number, count, edge_port->txCredits - fifo->count, copySize);
1288 /* catch writes of 0 bytes which the tty driver likes to give us, and when txCredits is empty */
1289 if (copySize == 0) {
1290 dbg("%s - copySize = Zero", __FUNCTION__);
1295 // since we can never overflow the buffer we do not have to check for full condition
1297 // the copy is done is two parts -- first fill to the end of the buffer
1298 // then copy the reset from the start of the buffer
1300 bytesleft = fifo->size - fifo->head;
1301 firsthalf = min (bytesleft, copySize);
1302 dbg("%s - copy %d bytes of %d into fifo ", __FUNCTION__, firsthalf, bytesleft);
1304 /* now copy our data */
1306 if (copy_from_user(&fifo->fifo[fifo->head], data, firsthalf))
1309 memcpy(&fifo->fifo[fifo->head], data, firsthalf);
1311 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, firsthalf, &fifo->fifo[fifo->head]);
1313 // update the index and size
1314 fifo->head += firsthalf;
1315 fifo->count += firsthalf;
1318 if (fifo->head == fifo->size) {
1322 secondhalf = copySize-firsthalf;
1325 dbg("%s - copy rest of data %d", __FUNCTION__, secondhalf);
1327 if (copy_from_user(&fifo->fifo[fifo->head], &data[firsthalf], secondhalf))
1330 memcpy(&fifo->fifo[fifo->head], &data[firsthalf], secondhalf);
1332 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, secondhalf, &fifo->fifo[fifo->head]);
1333 // update the index and size
1334 fifo->count += secondhalf;
1335 fifo->head += secondhalf;
1336 // No need to check for wrap since we can not get to end of fifo in this part
1339 send_more_port_data((struct edgeport_serial *)usb_get_serial_data(port->serial), edge_port);
1341 dbg("%s wrote %d byte(s) TxCredits %d, Fifo %d", __FUNCTION__, copySize, edge_port->txCredits, fifo->count);
1347 /************************************************************************
1349 * send_more_port_data()
1351 * This routine attempts to write additional UART transmit data
1352 * to a port over the USB bulk pipe. It is called (1) when new
1353 * data has been written to a port's TxBuffer from higher layers
1354 * (2) when the peripheral sends us additional TxCredits indicating
1355 * that it can accept more Tx data for a given port; and (3) when
1356 * a bulk write completes successfully and we want to see if we
1357 * can transmit more.
1359 ************************************************************************/
1360 static void send_more_port_data(struct edgeport_serial *edge_serial, struct edgeport_port *edge_port)
1362 struct TxFifo *fifo = &edge_port->txfifo;
1364 unsigned char *buffer;
1371 dbg("%s(%d)", __FUNCTION__, edge_port->port->number);
1373 if (edge_port->write_in_progress ||
1375 (fifo->count == 0)) {
1376 dbg("%s(%d) EXIT - fifo %d, PendingWrite = %d", __FUNCTION__, edge_port->port->number, fifo->count, edge_port->write_in_progress);
1380 // since the amount of data in the fifo will always fit into the
1381 // edgeport buffer we do not need to check the write length
1383 // Do we have enough credits for this port to make it worthwhile
1384 // to bother queueing a write. If it's too small, say a few bytes,
1385 // it's better to wait for more credits so we can do a larger
1387 if (edge_port->txCredits < EDGE_FW_GET_TX_CREDITS_SEND_THRESHOLD(edge_port->maxTxCredits,EDGE_FW_BULK_MAX_PACKET_SIZE)) {
1388 dbg("%s(%d) Not enough credit - fifo %d TxCredit %d", __FUNCTION__, edge_port->port->number, fifo->count, edge_port->txCredits );
1393 edge_port->write_in_progress = TRUE;
1395 // get a pointer to the write_urb
1396 urb = edge_port->write_urb;
1398 /* if this urb had a transfer buffer already (old transfer) free it */
1399 if (urb->transfer_buffer != NULL) {
1400 kfree(urb->transfer_buffer);
1401 urb->transfer_buffer = NULL;
1404 /* build the data header for the buffer and port that we are about to send out */
1405 count = fifo->count;
1406 buffer = kmalloc (count+2, GFP_ATOMIC);
1407 if (buffer == NULL) {
1408 dev_err(&edge_port->port->dev, "%s - no more kernel memory...\n", __FUNCTION__);
1409 edge_port->write_in_progress = FALSE;
1412 buffer[0] = IOSP_BUILD_DATA_HDR1 (edge_port->port->number - edge_port->port->serial->minor, count);
1413 buffer[1] = IOSP_BUILD_DATA_HDR2 (edge_port->port->number - edge_port->port->serial->minor, count);
1415 /* now copy our data */
1416 bytesleft = fifo->size - fifo->tail;
1417 firsthalf = min (bytesleft, count);
1418 memcpy(&buffer[2], &fifo->fifo[fifo->tail], firsthalf);
1419 fifo->tail += firsthalf;
1420 fifo->count -= firsthalf;
1421 if (fifo->tail == fifo->size) {
1425 secondhalf = count-firsthalf;
1427 memcpy(&buffer[2+firsthalf], &fifo->fifo[fifo->tail], secondhalf);
1428 fifo->tail += secondhalf;
1429 fifo->count -= secondhalf;
1433 usb_serial_debug_data(debug, &edge_port->port->dev, __FUNCTION__, count, &buffer[2]);
1435 /* fill up the urb with all of our data and submit it */
1436 usb_fill_bulk_urb (urb, edge_serial->serial->dev,
1437 usb_sndbulkpipe(edge_serial->serial->dev, edge_serial->bulk_out_endpoint),
1438 buffer, count+2, edge_bulk_out_data_callback, edge_port);
1440 /* decrement the number of credits we have by the number we just sent */
1441 edge_port->txCredits -= count;
1442 edge_port->icount.tx += count;
1444 urb->dev = edge_serial->serial->dev;
1445 status = usb_submit_urb(urb, GFP_ATOMIC);
1447 /* something went wrong */
1448 dbg("%s - usb_submit_urb(write bulk) failed", __FUNCTION__);
1449 edge_port->write_in_progress = FALSE;
1451 /* revert the credits as something bad happened. */
1452 edge_port->txCredits += count;
1453 edge_port->icount.tx -= count;
1455 dbg("%s wrote %d byte(s) TxCredit %d, Fifo %d", __FUNCTION__, count, edge_port->txCredits, fifo->count);
1459 /*****************************************************************************
1461 * this function is called by the tty driver when it wants to know how many
1462 * bytes of data we can accept for a specific port.
1463 * If successful, we return the amount of room that we have for this port
1465 * Otherwise we return a negative error number.
1466 *****************************************************************************/
1467 static int edge_write_room (struct usb_serial_port *port)
1469 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1472 dbg("%s", __FUNCTION__);
1474 if (edge_port == NULL)
1476 if (edge_port->closePending == TRUE)
1479 dbg("%s - port %d", __FUNCTION__, port->number);
1481 if (!edge_port->open) {
1482 dbg("%s - port not opened", __FUNCTION__);
1486 // total of both buffers is still txCredit
1487 room = edge_port->txCredits - edge_port->txfifo.count;
1489 dbg("%s - returns %d", __FUNCTION__, room);
1494 /*****************************************************************************
1495 * edge_chars_in_buffer
1496 * this function is called by the tty driver when it wants to know how many
1497 * bytes of data we currently have outstanding in the port (data that has
1498 * been written, but hasn't made it out the port yet)
1499 * If successful, we return the number of bytes left to be written in the
1501 * Otherwise we return a negative error number.
1502 *****************************************************************************/
1503 static int edge_chars_in_buffer (struct usb_serial_port *port)
1505 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1508 dbg("%s", __FUNCTION__);
1510 if (edge_port == NULL)
1512 if (edge_port->closePending == TRUE)
1515 if (!edge_port->open) {
1516 dbg("%s - port not opened", __FUNCTION__);
1520 num_chars = edge_port->maxTxCredits - edge_port->txCredits + edge_port->txfifo.count;
1522 dbg("%s(port %d) - returns %d", __FUNCTION__, port->number, num_chars);
1529 /*****************************************************************************
1531 * this function is called by the tty driver when it wants to stop the data
1532 * being read from the port.
1533 *****************************************************************************/
1534 static void edge_throttle (struct usb_serial_port *port)
1536 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1537 struct tty_struct *tty;
1540 dbg("%s - port %d", __FUNCTION__, port->number);
1542 if (edge_port == NULL)
1545 if (!edge_port->open) {
1546 dbg("%s - port not opened", __FUNCTION__);
1552 dbg ("%s - no tty available", __FUNCTION__);
1556 /* if we are implementing XON/XOFF, send the stop character */
1558 unsigned char stop_char = STOP_CHAR(tty);
1559 status = edge_write (port, 0, &stop_char, 1);
1565 /* if we are implementing RTS/CTS, toggle that line */
1566 if (tty->termios->c_cflag & CRTSCTS) {
1567 edge_port->shadowMCR &= ~MCR_RTS;
1568 status = send_cmd_write_uart_register(edge_port, MCR, edge_port->shadowMCR);
1578 /*****************************************************************************
1580 * this function is called by the tty driver when it wants to resume the data
1581 * being read from the port (called after SerialThrottle is called)
1582 *****************************************************************************/
1583 static void edge_unthrottle (struct usb_serial_port *port)
1585 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1586 struct tty_struct *tty;
1589 dbg("%s - port %d", __FUNCTION__, port->number);
1591 if (edge_port == NULL)
1594 if (!edge_port->open) {
1595 dbg("%s - port not opened", __FUNCTION__);
1601 dbg ("%s - no tty available", __FUNCTION__);
1605 /* if we are implementing XON/XOFF, send the start character */
1607 unsigned char start_char = START_CHAR(tty);
1608 status = edge_write (port, 0, &start_char, 1);
1614 /* if we are implementing RTS/CTS, toggle that line */
1615 if (tty->termios->c_cflag & CRTSCTS) {
1616 edge_port->shadowMCR |= MCR_RTS;
1617 status = send_cmd_write_uart_register(edge_port, MCR, edge_port->shadowMCR);
1627 /*****************************************************************************
1629 * this function is called by the tty driver when it wants to change the termios structure
1630 *****************************************************************************/
1631 static void edge_set_termios (struct usb_serial_port *port, struct termios *old_termios)
1633 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1634 struct tty_struct *tty = port->tty;
1637 if (!port->tty || !port->tty->termios) {
1638 dbg ("%s - no tty or termios", __FUNCTION__);
1642 cflag = tty->termios->c_cflag;
1643 /* check that they really want us to change something */
1645 if ((cflag == old_termios->c_cflag) &&
1646 (RELEVANT_IFLAG(tty->termios->c_iflag) == RELEVANT_IFLAG(old_termios->c_iflag))) {
1647 dbg("%s - nothing to change", __FUNCTION__);
1652 dbg("%s - clfag %08x iflag %08x", __FUNCTION__,
1653 tty->termios->c_cflag,
1654 RELEVANT_IFLAG(tty->termios->c_iflag));
1656 dbg("%s - old clfag %08x old iflag %08x", __FUNCTION__,
1657 old_termios->c_cflag,
1658 RELEVANT_IFLAG(old_termios->c_iflag));
1661 dbg("%s - port %d", __FUNCTION__, port->number);
1663 if (edge_port == NULL)
1666 if (!edge_port->open) {
1667 dbg("%s - port not opened", __FUNCTION__);
1671 /* change the port settings to the new ones specified */
1672 change_port_settings (edge_port, old_termios);
1678 /*****************************************************************************
1679 * get_lsr_info - get line status register info
1681 * Purpose: Let user call ioctl() to get info when the UART physically
1682 * is emptied. On bus types like RS485, the transmitter must
1683 * release the bus after transmitting. This must be done when
1684 * the transmit shift register is empty, not be done when the
1685 * transmit holding register is empty. This functionality
1686 * allows an RS485 driver to be written in user space.
1687 *****************************************************************************/
1688 static int get_lsr_info(struct edgeport_port *edge_port, unsigned int __user *value)
1690 unsigned int result = 0;
1692 if (edge_port->maxTxCredits == edge_port->txCredits &&
1693 edge_port->txfifo.count == 0) {
1694 dbg("%s -- Empty", __FUNCTION__);
1695 result = TIOCSER_TEMT;
1698 if (copy_to_user(value, &result, sizeof(int)))
1703 static int get_number_bytes_avail(struct edgeport_port *edge_port, unsigned int __user *value)
1705 unsigned int result = 0;
1706 struct tty_struct *tty = edge_port->port->tty;
1709 return -ENOIOCTLCMD;
1711 result = tty->read_cnt;
1713 dbg("%s(%d) = %d", __FUNCTION__, edge_port->port->number, result);
1714 if (copy_to_user(value, &result, sizeof(int)))
1717 return -ENOIOCTLCMD;
1720 static int edge_tiocmset (struct usb_serial_port *port, struct file *file, unsigned int set, unsigned int clear)
1722 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1725 dbg("%s - port %d", __FUNCTION__, port->number);
1727 mcr = edge_port->shadowMCR;
1728 if (set & TIOCM_RTS)
1730 if (set & TIOCM_DTR)
1732 if (set & TIOCM_LOOP)
1733 mcr |= MCR_LOOPBACK;
1735 if (clear & TIOCM_RTS)
1737 if (clear & TIOCM_DTR)
1739 if (clear & TIOCM_LOOP)
1740 mcr &= ~MCR_LOOPBACK;
1742 edge_port->shadowMCR = mcr;
1744 send_cmd_write_uart_register(edge_port, MCR, edge_port->shadowMCR);
1749 static int edge_tiocmget(struct usb_serial_port *port, struct file *file)
1751 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1752 unsigned int result = 0;
1756 dbg("%s - port %d", __FUNCTION__, port->number);
1758 msr = edge_port->shadowMSR;
1759 mcr = edge_port->shadowMCR;
1760 result = ((mcr & MCR_DTR) ? TIOCM_DTR: 0) /* 0x002 */
1761 | ((mcr & MCR_RTS) ? TIOCM_RTS: 0) /* 0x004 */
1762 | ((msr & EDGEPORT_MSR_CTS) ? TIOCM_CTS: 0) /* 0x020 */
1763 | ((msr & EDGEPORT_MSR_CD) ? TIOCM_CAR: 0) /* 0x040 */
1764 | ((msr & EDGEPORT_MSR_RI) ? TIOCM_RI: 0) /* 0x080 */
1765 | ((msr & EDGEPORT_MSR_DSR) ? TIOCM_DSR: 0); /* 0x100 */
1768 dbg("%s -- %x", __FUNCTION__, result);
1773 static int get_serial_info(struct edgeport_port *edge_port, struct serial_struct __user *retinfo)
1775 struct serial_struct tmp;
1780 memset(&tmp, 0, sizeof(tmp));
1782 tmp.type = PORT_16550A;
1783 tmp.line = edge_port->port->serial->minor;
1784 tmp.port = edge_port->port->number;
1786 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
1787 tmp.xmit_fifo_size = edge_port->maxTxCredits;
1788 tmp.baud_base = 9600;
1789 tmp.close_delay = 5*HZ;
1790 tmp.closing_wait = 30*HZ;
1791 // tmp.custom_divisor = state->custom_divisor;
1792 // tmp.hub6 = state->hub6;
1793 // tmp.io_type = state->io_type;
1795 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1802 /*****************************************************************************
1804 * this function handles any ioctl calls to the driver
1805 *****************************************************************************/
1806 static int edge_ioctl (struct usb_serial_port *port, struct file *file, unsigned int cmd, unsigned long arg)
1808 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1809 struct async_icount cnow;
1810 struct async_icount cprev;
1811 struct serial_icounter_struct icount;
1814 dbg("%s - port %d, cmd = 0x%x", __FUNCTION__, port->number, cmd);
1817 // return number of bytes available
1819 dbg("%s (%d) TIOCINQ", __FUNCTION__, port->number);
1820 return get_number_bytes_avail(edge_port, (unsigned int __user *) arg);
1824 dbg("%s (%d) TIOCSERGETLSR", __FUNCTION__, port->number);
1825 return get_lsr_info(edge_port, (unsigned int __user *) arg);
1829 dbg("%s (%d) TIOCGSERIAL", __FUNCTION__, port->number);
1830 return get_serial_info(edge_port, (struct serial_struct __user *) arg);
1833 dbg("%s (%d) TIOCSSERIAL", __FUNCTION__, port->number);
1837 dbg("%s (%d) TIOCMIWAIT", __FUNCTION__, port->number);
1838 cprev = edge_port->icount;
1840 interruptible_sleep_on(&edge_port->delta_msr_wait);
1841 /* see if a signal did it */
1842 if (signal_pending(current))
1843 return -ERESTARTSYS;
1844 cnow = edge_port->icount;
1845 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
1846 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
1847 return -EIO; /* no change => error */
1848 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1849 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1850 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1851 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)) ) {
1860 cnow = edge_port->icount;
1861 memset(&icount, 0, sizeof(icount));
1862 icount.cts = cnow.cts;
1863 icount.dsr = cnow.dsr;
1864 icount.rng = cnow.rng;
1865 icount.dcd = cnow.dcd;
1866 icount.rx = cnow.rx;
1867 icount.tx = cnow.tx;
1868 icount.frame = cnow.frame;
1869 icount.overrun = cnow.overrun;
1870 icount.parity = cnow.parity;
1871 icount.brk = cnow.brk;
1872 icount.buf_overrun = cnow.buf_overrun;
1874 dbg("%s (%d) TIOCGICOUNT RX=%d, TX=%d", __FUNCTION__, port->number, icount.rx, icount.tx );
1875 if (copy_to_user((void __user *)arg, &icount, sizeof(icount)))
1880 return -ENOIOCTLCMD;
1884 /*****************************************************************************
1886 * this function sends a break to the port
1887 *****************************************************************************/
1888 static void edge_break (struct usb_serial_port *port, int break_state)
1890 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1893 /* flush and chase */
1894 edge_port->chaseResponsePending = TRUE;
1896 dbg("%s - Sending IOSP_CMD_CHASE_PORT", __FUNCTION__);
1897 status = send_iosp_ext_cmd (edge_port, IOSP_CMD_CHASE_PORT, 0);
1899 // block until chase finished
1900 block_until_chase_response(edge_port);
1902 edge_port->chaseResponsePending = FALSE;
1905 if (break_state == -1) {
1906 dbg("%s - Sending IOSP_CMD_SET_BREAK", __FUNCTION__);
1907 status = send_iosp_ext_cmd (edge_port, IOSP_CMD_SET_BREAK, 0);
1909 dbg("%s - Sending IOSP_CMD_CLEAR_BREAK", __FUNCTION__);
1910 status = send_iosp_ext_cmd (edge_port, IOSP_CMD_CLEAR_BREAK, 0);
1913 dbg("%s - error sending break set/clear command.", __FUNCTION__);
1920 /*****************************************************************************
1922 * this function handles the data received on the bulk in pipe.
1923 *****************************************************************************/
1924 static int process_rcvd_data (struct edgeport_serial *edge_serial, unsigned char * buffer, __u16 bufferLength)
1926 struct usb_serial_port *port;
1927 struct edgeport_port *edge_port;
1928 struct tty_struct *tty;
1929 __u16 lastBufferLength;
1933 dbg("%s", __FUNCTION__);
1935 lastBufferLength = bufferLength + 1;
1937 while (bufferLength > 0) {
1938 /* failsafe incase we get a message that we don't understand */
1939 if (lastBufferLength == bufferLength) {
1940 dbg("%s - stuck in loop, exiting it.", __FUNCTION__);
1943 lastBufferLength = bufferLength;
1945 switch (edge_serial->rxState) {
1947 edge_serial->rxHeader1 = *buffer;
1951 if (bufferLength == 0) {
1952 edge_serial->rxState = EXPECT_HDR2;
1955 /* otherwise, drop on through */
1958 edge_serial->rxHeader2 = *buffer;
1962 dbg("%s - Hdr1=%02X Hdr2=%02X", __FUNCTION__, edge_serial->rxHeader1, edge_serial->rxHeader2);
1964 // Process depending on whether this header is
1967 if (IS_CMD_STAT_HDR(edge_serial->rxHeader1)) {
1968 // Decode this status header and goto EXPECT_HDR1 (if we
1969 // can process the status with only 2 bytes), or goto
1970 // EXPECT_HDR3 to get the third byte.
1972 edge_serial->rxPort = IOSP_GET_HDR_PORT(edge_serial->rxHeader1);
1973 edge_serial->rxStatusCode = IOSP_GET_STATUS_CODE(edge_serial->rxHeader1);
1975 if (!IOSP_STATUS_IS_2BYTE(edge_serial->rxStatusCode)) {
1976 // This status needs additional bytes. Save what we have
1977 // and then wait for more data.
1978 edge_serial->rxStatusParam = edge_serial->rxHeader2;
1980 edge_serial->rxState = EXPECT_HDR3;
1984 // We have all the header bytes, process the status now
1985 process_rcvd_status (edge_serial, edge_serial->rxHeader2, 0);
1987 edge_serial->rxState = EXPECT_HDR1;
1990 edge_serial->rxPort = IOSP_GET_HDR_PORT(edge_serial->rxHeader1);
1991 edge_serial->rxBytesRemaining = IOSP_GET_HDR_DATA_LEN(edge_serial->rxHeader1, edge_serial->rxHeader2);
1993 dbg("%s - Data for Port %u Len %u", __FUNCTION__, edge_serial->rxPort, edge_serial->rxBytesRemaining);
1995 //ASSERT( DevExt->RxPort < DevExt->NumPorts );
1996 //ASSERT( DevExt->RxBytesRemaining < IOSP_MAX_DATA_LENGTH );
1998 if (bufferLength == 0 ) {
1999 edge_serial->rxState = EXPECT_DATA;
2002 // Else, drop through
2005 case EXPECT_DATA: // Expect data
2007 if (bufferLength < edge_serial->rxBytesRemaining) {
2008 rxLen = bufferLength;
2009 edge_serial->rxState = EXPECT_DATA; // Expect data to start next buffer
2011 // BufLen >= RxBytesRemaining
2012 rxLen = edge_serial->rxBytesRemaining;
2013 edge_serial->rxState = EXPECT_HDR1; // Start another header next time
2016 bufferLength -= rxLen;
2017 edge_serial->rxBytesRemaining -= rxLen;
2019 /* spit this data back into the tty driver if this port is open */
2021 port = edge_serial->serial->port[edge_serial->rxPort];
2022 edge_port = usb_get_serial_port_data(port);
2023 if (edge_port->open) {
2024 tty = edge_port->port->tty;
2026 dbg("%s - Sending %d bytes to TTY for port %d", __FUNCTION__, rxLen, edge_serial->rxPort);
2027 for (i = 0; i < rxLen ; ++i) {
2028 /* if we insert more than TTY_FLIPBUF_SIZE characters, we drop them. */
2029 if(tty->flip.count >= TTY_FLIPBUF_SIZE) {
2030 tty_flip_buffer_push(tty);
2032 /* this doesn't actually push the data through unless tty->low_latency is set */
2033 tty_insert_flip_char(tty, buffer[i], 0);
2035 tty_flip_buffer_push(tty);
2037 edge_port->icount.rx += rxLen;
2044 case EXPECT_HDR3: // Expect 3rd byte of status header
2045 edge_serial->rxHeader3 = *buffer;
2049 // We have all the header bytes, process the status now
2050 process_rcvd_status (edge_serial, edge_serial->rxStatusParam, edge_serial->rxHeader3);
2051 edge_serial->rxState = EXPECT_HDR1;
2061 /*****************************************************************************
2062 * process_rcvd_status
2063 * this function handles the any status messages received on the bulk in pipe.
2064 *****************************************************************************/
2065 static void process_rcvd_status (struct edgeport_serial *edge_serial, __u8 byte2, __u8 byte3)
2067 struct usb_serial_port *port;
2068 struct edgeport_port *edge_port;
2069 __u8 code = edge_serial->rxStatusCode;
2071 /* switch the port pointer to the one being currently talked about */
2072 port = edge_serial->serial->port[edge_serial->rxPort];
2073 edge_port = usb_get_serial_port_data(port);
2074 if (edge_port == NULL) {
2075 dev_err(&edge_serial->serial->dev->dev, "%s - edge_port == NULL for port %d\n", __FUNCTION__, edge_serial->rxPort);
2079 dbg("%s - port %d", __FUNCTION__, edge_serial->rxPort);
2081 if (code == IOSP_EXT_STATUS) {
2083 case IOSP_EXT_STATUS_CHASE_RSP:
2084 // we want to do EXT status regardless of port open/closed
2085 dbg("%s - Port %u EXT CHASE_RSP Data = %02x", __FUNCTION__, edge_serial->rxPort, byte3 );
2086 // Currently, the only EXT_STATUS is Chase, so process here instead of one more call
2087 // to one more subroutine. If/when more EXT_STATUS, there'll be more work to do.
2088 // Also, we currently clear flag and close the port regardless of content of above's Byte3.
2089 // We could choose to do something else when Byte3 says Timeout on Chase from Edgeport,
2090 // like wait longer in block_until_chase_response, but for now we don't.
2091 edge_port->chaseResponsePending = FALSE;
2092 wake_up_interruptible (&edge_port->wait_chase);
2095 case IOSP_EXT_STATUS_RX_CHECK_RSP:
2096 dbg("%s ========== Port %u CHECK_RSP Sequence = %02x =============\n", __FUNCTION__, edge_serial->rxPort, byte3 );
2097 //Port->RxCheckRsp = TRUE;
2102 if (code == IOSP_STATUS_OPEN_RSP) {
2103 edge_port->txCredits = GET_TX_BUFFER_SIZE(byte3);
2104 edge_port->maxTxCredits = edge_port->txCredits;
2105 dbg("%s - Port %u Open Response Inital MSR = %02x TxBufferSize = %d", __FUNCTION__, edge_serial->rxPort, byte2, edge_port->txCredits);
2106 handle_new_msr (edge_port, byte2);
2108 /* send the current line settings to the port so we are in sync with any further termios calls */
2109 if (edge_port->port->tty)
2110 change_port_settings (edge_port, edge_port->port->tty->termios);
2112 /* we have completed the open */
2113 edge_port->openPending = FALSE;
2114 edge_port->open = TRUE;
2115 wake_up_interruptible(&edge_port->wait_open);
2119 // If port is closed, silently discard all rcvd status. We can
2120 // have cases where buffered status is received AFTER the close
2121 // port command is sent to the Edgeport.
2122 if ((!edge_port->open ) || (edge_port->closePending)) {
2127 // Not currently sent by Edgeport
2128 case IOSP_STATUS_LSR:
2129 dbg("%s - Port %u LSR Status = %02x", __FUNCTION__, edge_serial->rxPort, byte2);
2130 handle_new_lsr (edge_port, FALSE, byte2, 0);
2133 case IOSP_STATUS_LSR_DATA:
2134 dbg("%s - Port %u LSR Status = %02x, Data = %02x", __FUNCTION__, edge_serial->rxPort, byte2, byte3);
2135 // byte2 is LSR Register
2136 // byte3 is broken data byte
2137 handle_new_lsr (edge_port, TRUE, byte2, byte3);
2140 // case IOSP_EXT_4_STATUS:
2141 // dbg("%s - Port %u LSR Status = %02x Data = %02x", __FUNCTION__, edge_serial->rxPort, byte2, byte3);
2144 case IOSP_STATUS_MSR:
2145 dbg("%s - Port %u MSR Status = %02x", __FUNCTION__, edge_serial->rxPort, byte2);
2147 // Process this new modem status and generate appropriate
2148 // events, etc, based on the new status. This routine
2149 // also saves the MSR in Port->ShadowMsr.
2150 handle_new_msr(edge_port, byte2);
2154 dbg("%s - Unrecognized IOSP status code %u\n", __FUNCTION__, code);
2162 /*****************************************************************************
2164 * this function handles any change to the msr register for a port.
2165 *****************************************************************************/
2166 static void handle_new_msr(struct edgeport_port *edge_port, __u8 newMsr)
2168 struct async_icount *icount;
2170 dbg("%s %02x", __FUNCTION__, newMsr);
2172 if (newMsr & (EDGEPORT_MSR_DELTA_CTS | EDGEPORT_MSR_DELTA_DSR | EDGEPORT_MSR_DELTA_RI | EDGEPORT_MSR_DELTA_CD)) {
2173 icount = &edge_port->icount;
2175 /* update input line counters */
2176 if (newMsr & EDGEPORT_MSR_DELTA_CTS) {
2179 if (newMsr & EDGEPORT_MSR_DELTA_DSR) {
2182 if (newMsr & EDGEPORT_MSR_DELTA_CD) {
2185 if (newMsr & EDGEPORT_MSR_DELTA_RI) {
2188 wake_up_interruptible(&edge_port->delta_msr_wait);
2191 /* Save the new modem status */
2192 edge_port->shadowMSR = newMsr & 0xf0;
2198 /*****************************************************************************
2200 * this function handles any change to the lsr register for a port.
2201 *****************************************************************************/
2202 static void handle_new_lsr(struct edgeport_port *edge_port, __u8 lsrData, __u8 lsr, __u8 data)
2204 __u8 newLsr = (__u8)(lsr & (__u8)(LSR_OVER_ERR | LSR_PAR_ERR | LSR_FRM_ERR | LSR_BREAK));
2205 struct async_icount *icount;
2207 dbg("%s - %02x", __FUNCTION__, newLsr);
2209 edge_port->shadowLSR = lsr;
2211 if (newLsr & LSR_BREAK) {
2213 // Parity and Framing errors only count if they
2214 // occur exclusive of a break being
2217 newLsr &= (__u8)(LSR_OVER_ERR | LSR_BREAK);
2220 /* Place LSR data byte into Rx buffer */
2221 if (lsrData && edge_port->port->tty) {
2222 tty_insert_flip_char(edge_port->port->tty, data, 0);
2223 tty_flip_buffer_push(edge_port->port->tty);
2226 /* update input line counters */
2227 icount = &edge_port->icount;
2228 if (newLsr & LSR_BREAK) {
2231 if (newLsr & LSR_OVER_ERR) {
2234 if (newLsr & LSR_PAR_ERR) {
2237 if (newLsr & LSR_FRM_ERR) {
2245 /****************************************************************************
2247 * writes a number of bytes to the Edgeport device's sram starting at the
2249 * If successful returns the number of bytes written, otherwise it returns
2250 * a negative error number of the problem.
2251 ****************************************************************************/
2252 static int sram_write (struct usb_serial *serial, __u16 extAddr, __u16 addr, __u16 length, __u8 *data)
2255 __u16 current_length;
2256 unsigned char *transfer_buffer;
2258 dbg("%s - %x, %x, %d", __FUNCTION__, extAddr, addr, length);
2260 transfer_buffer = kmalloc (64, GFP_KERNEL);
2261 if (!transfer_buffer) {
2262 dev_err(&serial->dev->dev, "%s - kmalloc(%d) failed.\n", __FUNCTION__, 64);
2266 /* need to split these writes up into 64 byte chunks */
2268 while (length > 0) {
2270 current_length = 64;
2272 current_length = length;
2274 // dbg("%s - writing %x, %x, %d", __FUNCTION__, extAddr, addr, current_length);
2275 memcpy (transfer_buffer, data, current_length);
2276 result = usb_control_msg (serial->dev, usb_sndctrlpipe(serial->dev, 0), USB_REQUEST_ION_WRITE_RAM,
2277 0x40, addr, extAddr, transfer_buffer, current_length, 300);
2280 length -= current_length;
2281 addr += current_length;
2282 data += current_length;
2285 kfree (transfer_buffer);
2290 /****************************************************************************
2292 * writes a number of bytes to the Edgeport device's ROM starting at the
2294 * If successful returns the number of bytes written, otherwise it returns
2295 * a negative error number of the problem.
2296 ****************************************************************************/
2297 static int rom_write (struct usb_serial *serial, __u16 extAddr, __u16 addr, __u16 length, __u8 *data)
2300 __u16 current_length;
2301 unsigned char *transfer_buffer;
2303 // dbg("%s - %x, %x, %d", __FUNCTION__, extAddr, addr, length);
2305 transfer_buffer = kmalloc (64, GFP_KERNEL);
2306 if (!transfer_buffer) {
2307 dev_err(&serial->dev->dev, "%s - kmalloc(%d) failed.\n", __FUNCTION__, 64);
2311 /* need to split these writes up into 64 byte chunks */
2313 while (length > 0) {
2315 current_length = 64;
2317 current_length = length;
2319 // dbg("%s - writing %x, %x, %d", __FUNCTION__, extAddr, addr, current_length);
2320 memcpy (transfer_buffer, data, current_length);
2321 result = usb_control_msg (serial->dev, usb_sndctrlpipe(serial->dev, 0), USB_REQUEST_ION_WRITE_ROM,
2322 0x40, addr, extAddr, transfer_buffer, current_length, 300);
2325 length -= current_length;
2326 addr += current_length;
2327 data += current_length;
2330 kfree (transfer_buffer);
2335 /****************************************************************************
2337 * reads a number of bytes from the Edgeport device starting at the given
2339 * If successful returns the number of bytes read, otherwise it returns
2340 * a negative error number of the problem.
2341 ****************************************************************************/
2342 static int rom_read (struct usb_serial *serial, __u16 extAddr, __u16 addr, __u16 length, __u8 *data)
2345 __u16 current_length;
2346 unsigned char *transfer_buffer;
2348 dbg("%s - %x, %x, %d", __FUNCTION__, extAddr, addr, length);
2350 transfer_buffer = kmalloc (64, GFP_KERNEL);
2351 if (!transfer_buffer) {
2352 dev_err(&serial->dev->dev, "%s - kmalloc(%d) failed.\n", __FUNCTION__, 64);
2356 /* need to split these reads up into 64 byte chunks */
2358 while (length > 0) {
2360 current_length = 64;
2362 current_length = length;
2364 // dbg("%s - %x, %x, %d", __FUNCTION__, extAddr, addr, current_length);
2365 result = usb_control_msg (serial->dev, usb_rcvctrlpipe(serial->dev, 0), USB_REQUEST_ION_READ_ROM,
2366 0xC0, addr, extAddr, transfer_buffer, current_length, 300);
2369 memcpy (data, transfer_buffer, current_length);
2370 length -= current_length;
2371 addr += current_length;
2372 data += current_length;
2375 kfree (transfer_buffer);
2380 /****************************************************************************
2382 * Is used to send a IOSP message to the Edgeport device
2383 ****************************************************************************/
2384 static int send_iosp_ext_cmd (struct edgeport_port *edge_port, __u8 command, __u8 param)
2386 unsigned char *buffer;
2387 unsigned char *currentCommand;
2391 dbg("%s - %d, %d", __FUNCTION__, command, param);
2393 buffer = kmalloc (10, GFP_ATOMIC);
2395 dev_err(&edge_port->port->dev, "%s - kmalloc(%d) failed.\n", __FUNCTION__, 10);
2399 currentCommand = buffer;
2401 MAKE_CMD_EXT_CMD (¤tCommand, &length,
2402 edge_port->port->number - edge_port->port->serial->minor,
2405 status = write_cmd_usb (edge_port, buffer, length);
2407 /* something bad happened, let's free up the memory */
2415 /*****************************************************************************
2417 * this function writes the given buffer out to the bulk write endpoint.
2418 *****************************************************************************/
2419 static int write_cmd_usb (struct edgeport_port *edge_port, unsigned char *buffer, int length)
2421 struct edgeport_serial *edge_serial = usb_get_serial_data(edge_port->port->serial);
2426 usb_serial_debug_data(debug, &edge_port->port->dev, __FUNCTION__, length, buffer);
2428 /* Allocate our next urb */
2429 urb = usb_alloc_urb (0, GFP_ATOMIC);
2434 dbg("%s - ALLOCATE URB %p (outstanding %d)", __FUNCTION__, urb, CmdUrbs);
2436 usb_fill_bulk_urb (urb, edge_serial->serial->dev,
2437 usb_sndbulkpipe(edge_serial->serial->dev, edge_serial->bulk_out_endpoint),
2438 buffer, length, edge_bulk_out_cmd_callback, edge_port);
2440 edge_port->commandPending = TRUE;
2441 status = usb_submit_urb(urb, GFP_ATOMIC);
2444 /* something went wrong */
2445 dbg("%s - usb_submit_urb(write bulk) failed", __FUNCTION__);
2446 usb_unlink_urb (urb);
2451 // wait for command to finish
2452 timeout = COMMAND_TIMEOUT;
2454 while (timeout && edge_port->commandPending == TRUE) {
2455 timeout = interruptible_sleep_on_timeout (&edge_port->wait_command, timeout);
2458 if (edge_port->commandPending == TRUE) {
2459 /* command timed out */
2460 dbg("%s - command timed out", __FUNCTION__);
2468 /*****************************************************************************
2469 * send_cmd_write_baud_rate
2470 * this function sends the proper command to change the baud rate of the
2472 *****************************************************************************/
2473 static int send_cmd_write_baud_rate (struct edgeport_port *edge_port, int baudRate)
2475 unsigned char *cmdBuffer;
2476 unsigned char *currCmd;
2480 unsigned char number = edge_port->port->number - edge_port->port->serial->minor;
2482 dbg("%s - port = %d, baud = %d", __FUNCTION__, edge_port->port->number, baudRate);
2484 status = calc_baud_rate_divisor (baudRate, &divisor);
2486 dev_err(&edge_port->port->dev, "%s - bad baud rate\n", __FUNCTION__);
2490 // Alloc memory for the string of commands.
2491 cmdBuffer = kmalloc (0x100, GFP_ATOMIC);
2493 dev_err(&edge_port->port->dev, "%s - kmalloc(%d) failed.\n", __FUNCTION__, 0x100);
2496 currCmd = cmdBuffer;
2498 // Enable access to divisor latch
2499 MAKE_CMD_WRITE_REG( &currCmd, &cmdLen, number, LCR, LCR_DL_ENABLE );
2501 // Write the divisor itself
2502 MAKE_CMD_WRITE_REG( &currCmd, &cmdLen, number, DLL, LOW8 (divisor) );
2503 MAKE_CMD_WRITE_REG( &currCmd, &cmdLen, number, DLM, HIGH8(divisor) );
2505 // Restore original value to disable access to divisor latch
2506 MAKE_CMD_WRITE_REG( &currCmd, &cmdLen, number, LCR, edge_port->shadowLCR);
2508 status = write_cmd_usb(edge_port, cmdBuffer, cmdLen );
2510 /* something bad happened, let's free up the memory */
2518 /*****************************************************************************
2519 * calc_baud_rate_divisor
2520 * this function calculates the proper baud rate divisor for the specified
2522 *****************************************************************************/
2523 static int calc_baud_rate_divisor (int baudrate, int *divisor)
2531 dbg("%s - %d", __FUNCTION__, baudrate);
2533 for (i = 0; i < NUM_ENTRIES(divisor_table); i++) {
2534 if ( divisor_table[i].BaudRate == baudrate ) {
2535 *divisor = divisor_table[i].Divisor;
2540 // We have tried all of the standard baud rates
2541 // lets try to calculate the divisor for this baud rate
2542 // Make sure the baud rate is reasonable
2543 if (baudrate < 230400) {
2545 custom = (__u16)(230400L / baudrate);
2547 // Check for round off
2548 round1 = (__u16)(2304000L / baudrate);
2549 round = (__u16)(round1 - (custom * 10));
2555 dbg("%s - Baud %d = %d\n", __FUNCTION__, baudrate, custom);
2563 /*****************************************************************************
2564 * send_cmd_write_uart_register
2565 * this function builds up a uart register message and sends to to the device.
2566 *****************************************************************************/
2567 static int send_cmd_write_uart_register (struct edgeport_port *edge_port, __u8 regNum, __u8 regValue)
2569 unsigned char *cmdBuffer;
2570 unsigned char *currCmd;
2571 unsigned long cmdLen = 0;
2574 dbg("%s - write to %s register 0x%02x", (regNum == MCR) ? "MCR" : "LCR", __FUNCTION__, regValue);
2576 // Alloc memory for the string of commands.
2577 cmdBuffer = kmalloc (0x10, GFP_ATOMIC);
2578 if (cmdBuffer == NULL ) {
2582 currCmd = cmdBuffer;
2584 // Build a cmd in the buffer to write the given register
2585 MAKE_CMD_WRITE_REG (&currCmd, &cmdLen,
2586 edge_port->port->number - edge_port->port->serial->minor,
2589 status = write_cmd_usb(edge_port, cmdBuffer, cmdLen);
2591 /* something bad happened, let's free up the memory */
2599 /*****************************************************************************
2600 * change_port_settings
2601 * This routine is called to set the UART on the device to match the specified
2603 *****************************************************************************/
2604 static void change_port_settings (struct edgeport_port *edge_port, struct termios *old_termios)
2606 struct tty_struct *tty;
2617 dbg("%s - port %d", __FUNCTION__, edge_port->port->number);
2619 if ((!edge_port->open) &&
2620 (!edge_port->openPending)) {
2621 dbg("%s - port not opened", __FUNCTION__);
2625 tty = edge_port->port->tty;
2628 dbg("%s - no tty structures", __FUNCTION__);
2632 cflag = tty->termios->c_cflag;
2634 switch (cflag & CSIZE) {
2635 case CS5: lData = LCR_BITS_5; mask = 0x1f; dbg("%s - data bits = 5", __FUNCTION__); break;
2636 case CS6: lData = LCR_BITS_6; mask = 0x3f; dbg("%s - data bits = 6", __FUNCTION__); break;
2637 case CS7: lData = LCR_BITS_7; mask = 0x7f; dbg("%s - data bits = 7", __FUNCTION__); break;
2639 case CS8: lData = LCR_BITS_8; dbg("%s - data bits = 8", __FUNCTION__); break;
2642 lParity = LCR_PAR_NONE;
2643 if (cflag & PARENB) {
2644 if (cflag & PARODD) {
2645 lParity = LCR_PAR_ODD;
2646 dbg("%s - parity = odd", __FUNCTION__);
2648 lParity = LCR_PAR_EVEN;
2649 dbg("%s - parity = even", __FUNCTION__);
2652 dbg("%s - parity = none", __FUNCTION__);
2655 if (cflag & CSTOPB) {
2657 dbg("%s - stop bits = 2", __FUNCTION__);
2660 dbg("%s - stop bits = 1", __FUNCTION__);
2663 /* figure out the flow control settings */
2664 rxFlow = txFlow = 0x00;
2665 if (cflag & CRTSCTS) {
2666 rxFlow |= IOSP_RX_FLOW_RTS;
2667 txFlow |= IOSP_TX_FLOW_CTS;
2668 dbg("%s - RTS/CTS is enabled", __FUNCTION__);
2670 dbg("%s - RTS/CTS is disabled", __FUNCTION__);
2673 /* if we are implementing XON/XOFF, set the start and stop character in the device */
2674 if (I_IXOFF(tty) || I_IXON(tty)) {
2675 unsigned char stop_char = STOP_CHAR(tty);
2676 unsigned char start_char = START_CHAR(tty);
2678 send_iosp_ext_cmd (edge_port, IOSP_CMD_SET_XON_CHAR, start_char);
2679 send_iosp_ext_cmd (edge_port, IOSP_CMD_SET_XOFF_CHAR, stop_char);
2681 /* if we are implementing INBOUND XON/XOFF */
2683 rxFlow |= IOSP_RX_FLOW_XON_XOFF;
2684 dbg("%s - INBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x", __FUNCTION__, start_char, stop_char);
2686 dbg("%s - INBOUND XON/XOFF is disabled", __FUNCTION__);
2689 /* if we are implementing OUTBOUND XON/XOFF */
2691 txFlow |= IOSP_TX_FLOW_XON_XOFF;
2692 dbg("%s - OUTBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x", __FUNCTION__, start_char, stop_char);
2694 dbg("%s - OUTBOUND XON/XOFF is disabled", __FUNCTION__);
2698 /* Set flow control to the configured value */
2699 send_iosp_ext_cmd (edge_port, IOSP_CMD_SET_RX_FLOW, rxFlow);
2700 send_iosp_ext_cmd (edge_port, IOSP_CMD_SET_TX_FLOW, txFlow);
2703 edge_port->shadowLCR &= ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
2704 edge_port->shadowLCR |= (lData | lParity | lStop);
2706 edge_port->validDataMask = mask;
2708 /* Send the updated LCR value to the EdgePort */
2709 status = send_cmd_write_uart_register(edge_port, LCR, edge_port->shadowLCR);
2714 /* set up the MCR register and send it to the EdgePort */
2715 edge_port->shadowMCR = MCR_MASTER_IE;
2716 if (cflag & CBAUD) {
2717 edge_port->shadowMCR |= (MCR_DTR | MCR_RTS);
2719 status = send_cmd_write_uart_register(edge_port, MCR, edge_port->shadowMCR);
2724 /* Determine divisor based on baud rate */
2725 baud = tty_get_baud_rate(tty);
2727 /* pick a default, any default... */
2731 dbg("%s - baud rate = %d", __FUNCTION__, baud);
2732 status = send_cmd_write_baud_rate (edge_port, baud);
2738 /****************************************************************************
2740 * Turns a string from Unicode into ASCII.
2741 * Doesn't do a good job with any characters that are outside the normal
2742 * ASCII range, but it's only for debugging...
2743 * NOTE: expects the unicode in LE format
2744 ****************************************************************************/
2745 static void unicode_to_ascii (char *string, __le16 *unicode, int unicode_size)
2748 for (i = 0; i < unicode_size; ++i) {
2749 string[i] = (char)(le16_to_cpu(unicode[i]));
2751 string[unicode_size] = 0x00;
2755 /****************************************************************************
2756 * get_manufacturing_desc
2757 * reads in the manufacturing descriptor and stores it into the serial
2759 ****************************************************************************/
2760 static void get_manufacturing_desc (struct edgeport_serial *edge_serial)
2764 dbg("getting manufacturer descriptor");
2766 response = rom_read (edge_serial->serial, (EDGE_MANUF_DESC_ADDR & 0xffff0000) >> 16,
2767 (__u16)(EDGE_MANUF_DESC_ADDR & 0x0000ffff), EDGE_MANUF_DESC_LEN,
2768 (__u8 *)(&edge_serial->manuf_descriptor));
2771 dev_err(&edge_serial->serial->dev->dev, "error in getting manufacturer descriptor\n");
2774 dbg("**Manufacturer Descriptor");
2775 dbg(" RomSize: %dK", edge_serial->manuf_descriptor.RomSize);
2776 dbg(" RamSize: %dK", edge_serial->manuf_descriptor.RamSize);
2777 dbg(" CpuRev: %d", edge_serial->manuf_descriptor.CpuRev);
2778 dbg(" BoardRev: %d", edge_serial->manuf_descriptor.BoardRev);
2779 dbg(" NumPorts: %d", edge_serial->manuf_descriptor.NumPorts);
2780 dbg(" DescDate: %d/%d/%d", edge_serial->manuf_descriptor.DescDate[0], edge_serial->manuf_descriptor.DescDate[1], edge_serial->manuf_descriptor.DescDate[2]+1900);
2781 unicode_to_ascii (string, edge_serial->manuf_descriptor.SerialNumber, edge_serial->manuf_descriptor.SerNumLength/2-1);
2782 dbg(" SerialNumber: %s", string);
2783 unicode_to_ascii (string, edge_serial->manuf_descriptor.AssemblyNumber, edge_serial->manuf_descriptor.AssemblyNumLength/2-1);
2784 dbg(" AssemblyNumber: %s", string);
2785 unicode_to_ascii (string, edge_serial->manuf_descriptor.OemAssyNumber, edge_serial->manuf_descriptor.OemAssyNumLength/2-1);
2786 dbg(" OemAssyNumber: %s", string);
2787 dbg(" UartType: %d", edge_serial->manuf_descriptor.UartType);
2788 dbg(" IonPid: %d", edge_serial->manuf_descriptor.IonPid);
2789 dbg(" IonConfig: %d", edge_serial->manuf_descriptor.IonConfig);
2794 /****************************************************************************
2796 * reads in the bootloader descriptor and stores it into the serial
2798 ****************************************************************************/
2799 static void get_boot_desc (struct edgeport_serial *edge_serial)
2803 dbg("getting boot descriptor");
2805 response = rom_read (edge_serial->serial, (EDGE_BOOT_DESC_ADDR & 0xffff0000) >> 16,
2806 (__u16)(EDGE_BOOT_DESC_ADDR & 0x0000ffff), EDGE_BOOT_DESC_LEN,
2807 (__u8 *)(&edge_serial->boot_descriptor));
2810 dev_err(&edge_serial->serial->dev->dev, "error in getting boot descriptor\n");
2812 dbg("**Boot Descriptor:");
2813 dbg(" BootCodeLength: %d", le16_to_cpu(edge_serial->boot_descriptor.BootCodeLength));
2814 dbg(" MajorVersion: %d", edge_serial->boot_descriptor.MajorVersion);
2815 dbg(" MinorVersion: %d", edge_serial->boot_descriptor.MinorVersion);
2816 dbg(" BuildNumber: %d", le16_to_cpu(edge_serial->boot_descriptor.BuildNumber));
2817 dbg(" Capabilities: 0x%x", le16_to_cpu(edge_serial->boot_descriptor.Capabilities));
2818 dbg(" UConfig0: %d", edge_serial->boot_descriptor.UConfig0);
2819 dbg(" UConfig1: %d", edge_serial->boot_descriptor.UConfig1);
2824 /****************************************************************************
2825 * load_application_firmware
2826 * This is called to load the application firmware to the device
2827 ****************************************************************************/
2828 static void load_application_firmware (struct edgeport_serial *edge_serial)
2830 struct edge_firmware_image_record *record;
2831 unsigned char *firmware;
2832 unsigned char *FirmwareImage;
2837 switch (edge_serial->product_info.iDownloadFile) {
2838 case EDGE_DOWNLOAD_FILE_I930:
2839 dbg("downloading firmware version (930) %d.%d.%d",
2840 OperationalCodeImageVersion_GEN1.MajorVersion,
2841 OperationalCodeImageVersion_GEN1.MinorVersion,
2842 OperationalCodeImageVersion_GEN1.BuildNumber);
2843 firmware = &OperationalCodeImage_GEN1[0];
2844 FirmwareImage = &OperationalCodeImage_GEN1[0];
2845 ImageSize = sizeof(OperationalCodeImage_GEN1);
2848 case EDGE_DOWNLOAD_FILE_80251:
2849 dbg("downloading firmware version (80251) %d.%d.%d",
2850 OperationalCodeImageVersion_GEN2.MajorVersion,
2851 OperationalCodeImageVersion_GEN2.MinorVersion,
2852 OperationalCodeImageVersion_GEN2.BuildNumber);
2853 firmware = &OperationalCodeImage_GEN2[0];
2854 FirmwareImage = &OperationalCodeImage_GEN2[0];
2855 ImageSize = sizeof(OperationalCodeImage_GEN2);
2858 case EDGE_DOWNLOAD_FILE_NONE:
2859 dbg ("No download file specified, skipping download\n");
2868 record = (struct edge_firmware_image_record *)firmware;
2869 response = sram_write (edge_serial->serial, le16_to_cpu(record->ExtAddr), le16_to_cpu(record->Addr), le16_to_cpu(record->Len), &record->Data[0]);
2871 dev_err(&edge_serial->serial->dev->dev, "sram_write failed (%x, %x, %d)\n", le16_to_cpu(record->ExtAddr), le16_to_cpu(record->Addr), record->Len);
2874 firmware += sizeof (struct edge_firmware_image_record) + le16_to_cpu(record->Len);
2875 if (firmware >= &FirmwareImage[ImageSize]) {
2880 dbg("sending exec_dl_code");
2881 response = usb_control_msg (edge_serial->serial->dev,
2882 usb_sndctrlpipe(edge_serial->serial->dev, 0),
2883 USB_REQUEST_ION_EXEC_DL_CODE,
2884 0x40, 0x4000, 0x0001, NULL, 0, 3000);
2892 /****************************************************************************
2894 ****************************************************************************/
2895 static int edge_startup (struct usb_serial *serial)
2897 struct edgeport_serial *edge_serial;
2898 struct edgeport_port *edge_port;
2899 struct usb_device *dev;
2904 /* create our private serial structure */
2905 edge_serial = kmalloc (sizeof(struct edgeport_serial), GFP_KERNEL);
2906 if (edge_serial == NULL) {
2907 dev_err(&serial->dev->dev, "%s - Out of memory", __FUNCTION__);
2910 memset (edge_serial, 0, sizeof(struct edgeport_serial));
2911 edge_serial->serial = serial;
2912 usb_set_serial_data(serial, edge_serial);
2914 /* get the name for the device from the device */
2915 if ( (i = get_string(dev, dev->descriptor.iManufacturer, &edge_serial->name[0])) != 0) {
2916 edge_serial->name[i-1] = ' ';
2919 get_string(dev, dev->descriptor.iProduct, &edge_serial->name[i]);
2921 dev_info(&serial->dev->dev, "%s detected\n", edge_serial->name);
2923 /* get the manufacturing descriptor for this device */
2924 get_manufacturing_desc (edge_serial);
2926 /* get the boot descriptor */
2927 get_boot_desc (edge_serial);
2929 get_product_info(edge_serial);
2931 /* set the number of ports from the manufacturing description */
2932 /* serial->num_ports = serial->product_info.NumPorts; */
2933 if (edge_serial->product_info.NumPorts != serial->num_ports) {
2934 warn("%s - Device Reported %d serial ports vs core "
2935 "thinking we have %d ports, email greg@kroah.com this info.",
2936 __FUNCTION__, edge_serial->product_info.NumPorts,
2940 dbg("%s - time 1 %ld", __FUNCTION__, jiffies);
2942 /* now load the application firmware into this device */
2943 load_application_firmware (edge_serial);
2945 dbg("%s - time 2 %ld", __FUNCTION__, jiffies);
2947 /* Check current Edgeport EEPROM and update if necessary */
2948 update_edgeport_E2PROM (edge_serial);
2950 dbg("%s - time 3 %ld", __FUNCTION__, jiffies);
2952 /* set the configuration to use #1 */
2953 // dbg("set_configuration 1");
2954 // usb_set_configuration (dev, 1);
2956 /* we set up the pointers to the endpoints in the edge_open function,
2957 * as the structures aren't created yet. */
2959 /* set up our port private structures */
2960 for (i = 0; i < serial->num_ports; ++i) {
2961 edge_port = kmalloc (sizeof(struct edgeport_port), GFP_KERNEL);
2962 if (edge_port == NULL) {
2963 dev_err(&serial->dev->dev, "%s - Out of memory", __FUNCTION__);
2964 usb_set_serial_data(serial, NULL);
2968 memset (edge_port, 0, sizeof(struct edgeport_port));
2969 edge_port->port = serial->port[i];
2970 usb_set_serial_port_data(serial->port[i], edge_port);
2978 /****************************************************************************
2980 * This function is called whenever the device is removed from the usb bus.
2981 ****************************************************************************/
2982 static void edge_shutdown (struct usb_serial *serial)
2986 dbg("%s", __FUNCTION__);
2988 /* stop reads and writes on all ports */
2989 for (i=0; i < serial->num_ports; ++i) {
2990 kfree (usb_get_serial_port_data(serial->port[i]));
2991 usb_set_serial_port_data(serial->port[i], NULL);
2993 kfree (usb_get_serial_data(serial));
2994 usb_set_serial_data(serial, NULL);
2998 /****************************************************************************
3000 * This is called by the module subsystem, or on startup to initialize us
3001 ****************************************************************************/
3002 static int __init edgeport_init(void)
3005 retval = usb_serial_register(&edgeport_2port_device);
3007 goto failed_2port_device_register;
3008 retval = usb_serial_register(&edgeport_4port_device);
3010 goto failed_4port_device_register;
3011 retval = usb_serial_register(&edgeport_8port_device);
3013 goto failed_8port_device_register;
3014 retval = usb_register(&io_driver);
3016 goto failed_usb_register;
3017 info(DRIVER_DESC " " DRIVER_VERSION);
3019 failed_usb_register:
3020 usb_serial_deregister(&edgeport_8port_device);
3021 failed_8port_device_register:
3022 usb_serial_deregister(&edgeport_4port_device);
3023 failed_4port_device_register:
3024 usb_serial_deregister(&edgeport_2port_device);
3025 failed_2port_device_register:
3031 /****************************************************************************
3033 * Called when the driver is about to be unloaded.
3034 ****************************************************************************/
3035 static void __exit edgeport_exit (void)
3037 usb_deregister (&io_driver);
3038 usb_serial_deregister (&edgeport_2port_device);
3039 usb_serial_deregister (&edgeport_4port_device);
3040 usb_serial_deregister (&edgeport_8port_device);
3043 module_init(edgeport_init);
3044 module_exit(edgeport_exit);
3046 /* Module information */
3047 MODULE_AUTHOR( DRIVER_AUTHOR );
3048 MODULE_DESCRIPTION( DRIVER_DESC );
3049 MODULE_LICENSE("GPL");
3051 module_param(debug, bool, S_IRUGO | S_IWUSR);
3052 MODULE_PARM_DESC(debug, "Debug enabled or not");