Merge with Linux 2.5.74.
[linux-2.6/linux-mips.git] / drivers / usb / serial / io_edgeport.c
blob07e020e258cee6f31a92eaf61bf0f1a27b5d62b9
1 /*
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:
13 * Edgeport/4
14 * Edgeport/4t
15 * Edgeport/2
16 * Edgeport/4i
17 * Edgeport/2i
18 * Edgeport/421
19 * Edgeport/21
20 * Rapidport/4
21 * Edgeport/8
22 * Edgeport/2D8
23 * Edgeport/4D8
24 * Edgeport/8i
26 * Version history:
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
37 * than once.
38 * - fixed memory leak on device removal.
39 * - fixed potential double free of memory when command urb submitting
40 * failed.
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.
46 * (04/08/2001) gb
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
185 * added EPIC support
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.
216 * Known problems:
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>
263 #ifdef CONFIG_USB_SERIAL_DEBUG
264 static int debug = 1;
265 #else
266 static int debug;
267 #endif
269 #include "usb-serial.h"
271 #include "io_edgeport.h"
272 #include "io_ionsp.h" /* info for the iosp messages */
273 #include "io_16654.h" /* 16654 UART defines */
276 * Version Information
278 #define DRIVER_VERSION "v2.3"
279 #define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com> and David Iacovelli"
280 #define DRIVER_DESC "Edgeport USB Serial Driver"
282 /* First, the latest boot code - for first generation edgeports */
283 #define IMAGE_ARRAY_NAME BootCodeImage_GEN1
284 #define IMAGE_VERSION_NAME BootCodeImageVersion_GEN1
285 #include "io_fw_boot.h" /* the bootloader firmware to download to a device, if it needs it */
287 /* for second generation edgeports */
288 #define IMAGE_ARRAY_NAME BootCodeImage_GEN2
289 #define IMAGE_VERSION_NAME BootCodeImageVersion_GEN2
290 #include "io_fw_boot2.h" /* the bootloader firmware to download to a device, if it needs it */
292 /* Then finally the main run-time operational code - for first generation edgeports */
293 #define IMAGE_ARRAY_NAME OperationalCodeImage_GEN1
294 #define IMAGE_VERSION_NAME OperationalCodeImageVersion_GEN1
295 #include "io_fw_down.h" /* Define array OperationalCodeImage[] */
297 /* for second generation edgeports */
298 #define IMAGE_ARRAY_NAME OperationalCodeImage_GEN2
299 #define IMAGE_VERSION_NAME OperationalCodeImageVersion_GEN2
300 #include "io_fw_down2.h" /* Define array OperationalCodeImage[] */
303 #define MAX_NAME_LEN 64
306 #define CHASE_TIMEOUT (5*HZ) /* 5 seconds */
307 #define OPEN_TIMEOUT (5*HZ) /* 5 seconds */
308 #define COMMAND_TIMEOUT (5*HZ) /* 5 seconds */
310 #ifndef SERIAL_MAGIC
311 #define SERIAL_MAGIC 0x6702
312 #endif
313 #define PORT_MAGIC 0x7301
316 /* receive port state */
317 enum RXSTATE {
318 EXPECT_HDR1 = 0, /* Expect header byte 1 */
319 EXPECT_HDR2 = 1, /* Expect header byte 2 */
320 EXPECT_DATA = 2, /* Expect 'RxBytesRemaining' data */
321 EXPECT_HDR3 = 3, /* Expect header byte 3 (for status hdrs only) */
325 /* Transmit Fifo
326 * This Transmit queue is an extension of the edgeport Rx buffer.
327 * The maximum amount of data buffered in both the edgeport
328 * Rx buffer (maxTxCredits) and this buffer will never exceed maxTxCredits.
330 struct TxFifo {
331 unsigned int head; /* index to head pointer (write) */
332 unsigned int tail; /* index to tail pointer (read) */
333 unsigned int count; /* Bytes in queue */
334 unsigned int size; /* Max size of queue (equal to Max number of TxCredits) */
335 unsigned char *fifo; /* allocated Buffer */
338 /* This structure holds all of the local port information */
339 struct edgeport_port {
340 __u16 txCredits; /* our current credits for this port */
341 __u16 maxTxCredits; /* the max size of the port */
343 struct TxFifo txfifo; /* transmit fifo -- size will be maxTxCredits */
344 struct urb *write_urb; /* write URB for this port */
345 char write_in_progress; /* TRUE while a write URB is outstanding */
347 __u8 shadowLCR; /* last LCR value received */
348 __u8 shadowMCR; /* last MCR value received */
349 __u8 shadowMSR; /* last MSR value received */
350 __u8 shadowLSR; /* last LSR value received */
351 __u8 shadowXonChar; /* last value set as XON char in Edgeport */
352 __u8 shadowXoffChar; /* last value set as XOFF char in Edgeport */
353 __u8 validDataMask;
354 __u32 baudRate;
356 char open;
357 char openPending;
358 char commandPending;
359 char closePending;
360 char chaseResponsePending;
362 wait_queue_head_t wait_chase; /* for handling sleeping while waiting for chase to finish */
363 wait_queue_head_t wait_open; /* for handling sleeping while waiting for open to finish */
364 wait_queue_head_t wait_command; /* for handling sleeping while waiting for command to finish */
365 wait_queue_head_t delta_msr_wait; /* for handling sleeping while waiting for msr change to happen */
367 struct async_icount icount;
368 struct usb_serial_port *port; /* loop back to the owner of this object */
372 /* This structure holds all of the individual device information */
373 struct edgeport_serial {
374 char name[MAX_NAME_LEN+1]; /* string name of this device */
376 struct edge_manuf_descriptor manuf_descriptor; /* the manufacturer descriptor */
377 struct edge_boot_descriptor boot_descriptor; /* the boot firmware descriptor */
378 struct edgeport_product_info product_info; /* Product Info */
380 __u8 interrupt_in_endpoint; /* the interrupt endpoint handle */
381 unsigned char * interrupt_in_buffer; /* the buffer we use for the interrupt endpoint */
382 struct urb * interrupt_read_urb; /* our interrupt urb */
384 __u8 bulk_in_endpoint; /* the bulk in endpoint handle */
385 unsigned char * bulk_in_buffer; /* the buffer we use for the bulk in endpoint */
386 struct urb * read_urb; /* our bulk read urb */
388 __u8 bulk_out_endpoint; /* the bulk out endpoint handle */
390 __s16 rxBytesAvail; /* the number of bytes that we need to read from this device */
392 enum RXSTATE rxState; /* the current state of the bulk receive processor */
393 __u8 rxHeader1; /* receive header byte 1 */
394 __u8 rxHeader2; /* receive header byte 2 */
395 __u8 rxHeader3; /* receive header byte 3 */
396 __u8 rxPort; /* the port that we are currently receiving data for */
397 __u8 rxStatusCode; /* the receive status code */
398 __u8 rxStatusParam; /* the receive status paramater */
399 __s16 rxBytesRemaining; /* the number of port bytes left to read */
400 struct usb_serial *serial; /* loop back to the owner of this object */
403 /* baud rate information */
404 struct divisor_table_entry {
405 __u32 BaudRate;
406 __u16 Divisor;
410 // Define table of divisors for Rev A EdgePort/4 hardware
411 // These assume a 3.6864MHz crystal, the standard /16, and
412 // MCR.7 = 0.
414 static struct divisor_table_entry divisor_table[] = {
415 { 50, 4608},
416 { 75, 3072},
417 { 110, 2095}, /* 2094.545455 => 230450 => .0217 % over */
418 { 134, 1713}, /* 1713.011152 => 230398.5 => .00065% under */
419 { 150, 1536},
420 { 300, 768},
421 { 600, 384},
422 { 1200, 192},
423 { 1800, 128},
424 { 2400, 96},
425 { 4800, 48},
426 { 7200, 32},
427 { 9600, 24},
428 { 14400, 16},
429 { 19200, 12},
430 { 38400, 6},
431 { 57600, 4},
432 { 115200, 2},
433 { 230400, 1},
436 /* local variables */
437 static int CmdUrbs = 0; /* Number of outstanding Command Write Urbs */
440 /* local function prototypes */
442 /* function prototypes for all URB callbacks */
443 static void edge_interrupt_callback (struct urb *urb, struct pt_regs *regs);
444 static void edge_bulk_in_callback (struct urb *urb, struct pt_regs *regs);
445 static void edge_bulk_out_data_callback (struct urb *urb, struct pt_regs *regs);
446 static void edge_bulk_out_cmd_callback (struct urb *urb, struct pt_regs *regs);
448 /* function prototypes for the usbserial callbacks */
449 static int edge_open (struct usb_serial_port *port, struct file *filp);
450 static void edge_close (struct usb_serial_port *port, struct file *filp);
451 static int edge_write (struct usb_serial_port *port, int from_user, const unsigned char *buf, int count);
452 static int edge_write_room (struct usb_serial_port *port);
453 static int edge_chars_in_buffer (struct usb_serial_port *port);
454 static void edge_throttle (struct usb_serial_port *port);
455 static void edge_unthrottle (struct usb_serial_port *port);
456 static void edge_set_termios (struct usb_serial_port *port, struct termios *old_termios);
457 static int edge_ioctl (struct usb_serial_port *port, struct file *file, unsigned int cmd, unsigned long arg);
458 static void edge_break (struct usb_serial_port *port, int break_state);
459 static int edge_tiocmget (struct usb_serial_port *port, struct file *file);
460 static int edge_tiocmset (struct usb_serial_port *port, struct file *file, unsigned int set, unsigned int clear);
461 static int edge_startup (struct usb_serial *serial);
462 static void edge_shutdown (struct usb_serial *serial);
465 #include "io_tables.h" /* all of the devices that this driver supports */
467 static struct usb_driver io_driver = {
468 .owner = THIS_MODULE,
469 .name = "io_edgeport",
470 .probe = usb_serial_probe,
471 .disconnect = usb_serial_disconnect,
472 .id_table = id_table_combined,
475 /* function prototypes for all of our local functions */
476 static int process_rcvd_data (struct edgeport_serial *edge_serial, unsigned char *buffer, __u16 bufferLength);
477 static void process_rcvd_status (struct edgeport_serial *edge_serial, __u8 byte2, __u8 byte3);
478 static void handle_new_msr (struct edgeport_port *edge_port, __u8 newMsr);
479 static void handle_new_lsr (struct edgeport_port *edge_port, __u8 lsrData, __u8 lsr, __u8 data);
480 static int send_iosp_ext_cmd (struct edgeport_port *edge_port, __u8 command, __u8 param);
481 static int calc_baud_rate_divisor (int baud_rate, int *divisor);
482 static int send_cmd_write_baud_rate (struct edgeport_port *edge_port, int baudRate);
483 static void change_port_settings (struct edgeport_port *edge_port, struct termios *old_termios);
484 static int send_cmd_write_uart_register (struct edgeport_port *edge_port, __u8 regNum, __u8 regValue);
485 static int write_cmd_usb (struct edgeport_port *edge_port, unsigned char *buffer, int writeLength);
486 static void send_more_port_data (struct edgeport_serial *edge_serial, struct edgeport_port *edge_port);
488 static int sram_write (struct usb_serial *serial, __u16 extAddr, __u16 addr, __u16 length, __u8 *data);
489 static int rom_read (struct usb_serial *serial, __u16 extAddr, __u16 addr, __u16 length, __u8 *data);
490 static int rom_write (struct usb_serial *serial, __u16 extAddr, __u16 addr, __u16 length, __u8 *data);
491 static void get_manufacturing_desc (struct edgeport_serial *edge_serial);
492 static void get_boot_desc (struct edgeport_serial *edge_serial);
493 static void load_application_firmware (struct edgeport_serial *edge_serial);
496 static void unicode_to_ascii (char *string, short *unicode, int unicode_size);
502 // ************************************************************************
503 // ************************************************************************
504 // ************************************************************************
505 // ************************************************************************
507 /************************************************************************
509 * update_edgeport_E2PROM() Compare current versions of *
510 * Boot ROM and Manufacture *
511 * Descriptors with versions *
512 * embedded in this driver *
514 ************************************************************************/
515 static void update_edgeport_E2PROM (struct edgeport_serial *edge_serial)
517 __u32 BootCurVer;
518 __u32 BootNewVer;
519 __u8 BootMajorVersion;
520 __u8 BootMinorVersion;
521 __u16 BootBuildNumber;
522 __u8 *BootImage;
523 __u32 BootSize;
524 struct edge_firmware_image_record *record;
525 unsigned char *firmware;
526 int response;
529 switch (edge_serial->product_info.iDownloadFile) {
530 case EDGE_DOWNLOAD_FILE_I930:
531 BootMajorVersion = BootCodeImageVersion_GEN1.MajorVersion;
532 BootMinorVersion = BootCodeImageVersion_GEN1.MinorVersion;
533 BootBuildNumber = cpu_to_le16(BootCodeImageVersion_GEN1.BuildNumber);
534 BootImage = &BootCodeImage_GEN1[0];
535 BootSize = sizeof( BootCodeImage_GEN1 );
536 break;
538 case EDGE_DOWNLOAD_FILE_80251:
539 BootMajorVersion = BootCodeImageVersion_GEN2.MajorVersion;
540 BootMinorVersion = BootCodeImageVersion_GEN2.MinorVersion;
541 BootBuildNumber = cpu_to_le16(BootCodeImageVersion_GEN2.BuildNumber);
542 BootImage = &BootCodeImage_GEN2[0];
543 BootSize = sizeof( BootCodeImage_GEN2 );
544 break;
546 default:
547 return;
550 // Check Boot Image Version
551 BootCurVer = (edge_serial->boot_descriptor.MajorVersion << 24) +
552 (edge_serial->boot_descriptor.MinorVersion << 16) +
553 le16_to_cpu(edge_serial->boot_descriptor.BuildNumber);
555 BootNewVer = (BootMajorVersion << 24) +
556 (BootMinorVersion << 16) +
557 le16_to_cpu(BootBuildNumber);
559 dbg("Current Boot Image version %d.%d.%d",
560 edge_serial->boot_descriptor.MajorVersion,
561 edge_serial->boot_descriptor.MinorVersion,
562 le16_to_cpu(edge_serial->boot_descriptor.BuildNumber));
565 if (BootNewVer > BootCurVer) {
566 dbg("**Update Boot Image from %d.%d.%d to %d.%d.%d",
567 edge_serial->boot_descriptor.MajorVersion,
568 edge_serial->boot_descriptor.MinorVersion,
569 le16_to_cpu(edge_serial->boot_descriptor.BuildNumber),
570 BootMajorVersion,
571 BootMinorVersion,
572 le16_to_cpu(BootBuildNumber));
575 dbg("Downloading new Boot Image");
577 firmware = BootImage;
579 for (;;) {
580 record = (struct edge_firmware_image_record *)firmware;
581 response = rom_write (edge_serial->serial, le16_to_cpu(record->ExtAddr), le16_to_cpu(record->Addr), le16_to_cpu(record->Len), &record->Data[0]);
582 if (response < 0) {
583 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));
584 break;
586 firmware += sizeof (struct edge_firmware_image_record) + le16_to_cpu(record->Len);
587 if (firmware >= &BootImage[BootSize]) {
588 break;
591 } else {
592 dbg("Boot Image -- already up to date");
597 /************************************************************************
599 * Get string descriptor from device *
601 ************************************************************************/
602 static int get_string (struct usb_device *dev, int Id, char *string)
604 struct usb_string_descriptor StringDesc;
605 struct usb_string_descriptor *pStringDesc;
607 dbg("%s - USB String ID = %d", __FUNCTION__, Id );
609 if (!usb_get_descriptor(dev, USB_DT_STRING, Id, &StringDesc, sizeof(StringDesc))) {
610 return 0;
613 pStringDesc = kmalloc (StringDesc.bLength, GFP_KERNEL);
615 if (!pStringDesc) {
616 return 0;
619 if (!usb_get_descriptor(dev, USB_DT_STRING, Id, pStringDesc, StringDesc.bLength )) {
620 kfree(pStringDesc);
621 return 0;
624 unicode_to_ascii(string, pStringDesc->wData, pStringDesc->bLength/2-1);
626 kfree(pStringDesc);
627 return strlen(string);
631 #if 0
632 /************************************************************************
634 * Get string descriptor from device
636 ************************************************************************/
637 static int get_string_desc (struct usb_device *dev, int Id, struct usb_string_descriptor **pRetDesc)
639 struct usb_string_descriptor StringDesc;
640 struct usb_string_descriptor *pStringDesc;
642 dbg("%s - USB String ID = %d", __FUNCTION__, Id );
644 if (!usb_get_descriptor(dev, USB_DT_STRING, Id, &StringDesc, sizeof(StringDesc))) {
645 return 0;
648 pStringDesc = kmalloc (StringDesc.bLength, GFP_KERNEL);
650 if (!pStringDesc) {
651 return -1;
654 if (!usb_get_descriptor(dev, USB_DT_STRING, Id, pStringDesc, StringDesc.bLength )) {
655 kfree(pStringDesc);
656 return -1;
659 *pRetDesc = pStringDesc;
660 return 0;
662 #endif
664 static void get_product_info(struct edgeport_serial *edge_serial)
666 struct edgeport_product_info *product_info = &edge_serial->product_info;
668 memset (product_info, 0, sizeof(struct edgeport_product_info));
670 product_info->ProductId = (__u16)(edge_serial->serial->dev->descriptor.idProduct & ~ION_DEVICE_ID_GENERATION_2);
671 product_info->NumPorts = edge_serial->manuf_descriptor.NumPorts;
672 product_info->ProdInfoVer = 0;
674 product_info->RomSize = edge_serial->manuf_descriptor.RomSize;
675 product_info->RamSize = edge_serial->manuf_descriptor.RamSize;
676 product_info->CpuRev = edge_serial->manuf_descriptor.CpuRev;
677 product_info->BoardRev = edge_serial->manuf_descriptor.BoardRev;
679 product_info->BootMajorVersion = edge_serial->boot_descriptor.MajorVersion;
680 product_info->BootMinorVersion = edge_serial->boot_descriptor.MinorVersion;
681 product_info->BootBuildNumber = edge_serial->boot_descriptor.BuildNumber;
683 memcpy(product_info->ManufactureDescDate, edge_serial->manuf_descriptor.DescDate, sizeof(edge_serial->manuf_descriptor.DescDate));
685 // check if this is 2nd generation hardware
686 if (edge_serial->serial->dev->descriptor.idProduct & ION_DEVICE_ID_GENERATION_2) {
687 product_info->FirmwareMajorVersion = OperationalCodeImageVersion_GEN2.MajorVersion;
688 product_info->FirmwareMinorVersion = OperationalCodeImageVersion_GEN2.MinorVersion;
689 product_info->FirmwareBuildNumber = cpu_to_le16(OperationalCodeImageVersion_GEN2.BuildNumber);
690 product_info->iDownloadFile = EDGE_DOWNLOAD_FILE_80251;
691 } else {
692 product_info->FirmwareMajorVersion = OperationalCodeImageVersion_GEN1.MajorVersion;
693 product_info->FirmwareMinorVersion = OperationalCodeImageVersion_GEN1.MinorVersion;
694 product_info->FirmwareBuildNumber = cpu_to_le16(OperationalCodeImageVersion_GEN1.BuildNumber);
695 product_info->iDownloadFile = EDGE_DOWNLOAD_FILE_I930;
698 // Determine Product type and set appropriate flags
699 switch (DEVICE_ID_FROM_USB_PRODUCT_ID(product_info->ProductId)) {
700 case ION_DEVICE_ID_EDGEPORT_COMPATIBLE:
701 case ION_DEVICE_ID_EDGEPORT_4T:
702 case ION_DEVICE_ID_EDGEPORT_4:
703 case ION_DEVICE_ID_EDGEPORT_2:
704 case ION_DEVICE_ID_EDGEPORT_8_DUAL_CPU:
705 case ION_DEVICE_ID_EDGEPORT_8:
706 case ION_DEVICE_ID_EDGEPORT_421:
707 case ION_DEVICE_ID_EDGEPORT_21:
708 case ION_DEVICE_ID_EDGEPORT_2_DIN:
709 case ION_DEVICE_ID_EDGEPORT_4_DIN:
710 case ION_DEVICE_ID_EDGEPORT_16_DUAL_CPU:
711 product_info->IsRS232 = 1;
712 break;
714 case ION_DEVICE_ID_EDGEPORT_2I: // Edgeport/2 RS422/RS485
715 product_info->IsRS422 = 1;
716 product_info->IsRS485 = 1;
717 break;
719 case ION_DEVICE_ID_EDGEPORT_8I: // Edgeport/4 RS422
720 case ION_DEVICE_ID_EDGEPORT_4I: // Edgeport/4 RS422
721 product_info->IsRS422 = 1;
722 break;
725 // Dump Product Info structure
726 dbg("**Product Information:");
727 dbg(" ProductId %x", product_info->ProductId );
728 dbg(" NumPorts %d", product_info->NumPorts );
729 dbg(" ProdInfoVer %d", product_info->ProdInfoVer );
730 dbg(" IsServer %d", product_info->IsServer);
731 dbg(" IsRS232 %d", product_info->IsRS232 );
732 dbg(" IsRS422 %d", product_info->IsRS422 );
733 dbg(" IsRS485 %d", product_info->IsRS485 );
734 dbg(" RomSize %d", product_info->RomSize );
735 dbg(" RamSize %d", product_info->RamSize );
736 dbg(" CpuRev %x", product_info->CpuRev );
737 dbg(" BoardRev %x", product_info->BoardRev);
738 dbg(" BootMajorVersion %d.%d.%d", product_info->BootMajorVersion,
739 product_info->BootMinorVersion,
740 le16_to_cpu(product_info->BootBuildNumber));
741 dbg(" FirmwareMajorVersion %d.%d.%d", product_info->FirmwareMajorVersion,
742 product_info->FirmwareMinorVersion,
743 le16_to_cpu(product_info->FirmwareBuildNumber));
744 dbg(" ManufactureDescDate %d/%d/%d", product_info->ManufactureDescDate[0],
745 product_info->ManufactureDescDate[1],
746 product_info->ManufactureDescDate[2]+1900);
747 dbg(" iDownloadFile 0x%x", product_info->iDownloadFile);
752 /************************************************************************/
753 /************************************************************************/
754 /* U S B C A L L B A C K F U N C T I O N S */
755 /* U S B C A L L B A C K F U N C T I O N S */
756 /************************************************************************/
757 /************************************************************************/
759 /*****************************************************************************
760 * edge_interrupt_callback
761 * this is the callback function for when we have received data on the
762 * interrupt endpoint.
763 *****************************************************************************/
764 static void edge_interrupt_callback (struct urb *urb, struct pt_regs *regs)
766 struct edgeport_serial *edge_serial = (struct edgeport_serial *)urb->context;
767 struct edgeport_port *edge_port;
768 struct usb_serial_port *port;
769 unsigned char *data = urb->transfer_buffer;
770 int length = urb->actual_length;
771 int bytes_avail;
772 int position;
773 int txCredits;
774 int portNumber;
775 int result;
777 dbg("%s", __FUNCTION__);
779 if (serial_paranoia_check (edge_serial->serial, __FUNCTION__)) {
780 return;
783 switch (urb->status) {
784 case 0:
785 /* success */
786 break;
787 case -ECONNRESET:
788 case -ENOENT:
789 case -ESHUTDOWN:
790 /* this urb is terminated, clean up */
791 dbg("%s - urb shutting down with status: %d", __FUNCTION__, urb->status);
792 return;
793 default:
794 dbg("%s - nonzero urb status received: %d", __FUNCTION__, urb->status);
795 goto exit;
798 // process this interrupt-read even if there are no ports open
799 if (length) {
800 usb_serial_debug_data (__FILE__, __FUNCTION__, length, data);
802 if (length > 1) {
803 bytes_avail = data[0] | (data[1] << 8);
804 if (bytes_avail) {
805 edge_serial->rxBytesAvail += bytes_avail;
806 dbg("%s - bytes_avail = %d, rxBytesAvail %d", __FUNCTION__, bytes_avail, edge_serial->rxBytesAvail);
808 if ((edge_serial->rxBytesAvail > 0) &&
809 (edge_serial->read_urb->status != -EINPROGRESS)) {
810 dbg(" --- Posting a read");
812 /* we have pending bytes on the bulk in pipe, send a request */
813 edge_serial->read_urb->dev = edge_serial->serial->dev;
814 result = usb_submit_urb(edge_serial->read_urb, GFP_ATOMIC);
815 if (result) {
816 dbg("%s - usb_submit_urb(read bulk) failed with result = %d", __FUNCTION__, result);
821 /* grab the txcredits for the ports if available */
822 position = 2;
823 portNumber = 0;
824 while ((position < length) && (portNumber < edge_serial->serial->num_ports)) {
825 txCredits = data[position] | (data[position+1] << 8);
826 if (txCredits) {
827 port = &edge_serial->serial->port[portNumber];
828 if (port_paranoia_check (port, __FUNCTION__) == 0) {
829 edge_port = usb_get_serial_port_data(port);
830 if (edge_port->open) {
831 edge_port->txCredits += txCredits;
832 dbg("%s - txcredits for port%d = %d", __FUNCTION__, portNumber, edge_port->txCredits);
834 /* tell the tty driver that something has changed */
835 if (edge_port->port->tty)
836 wake_up_interruptible(&edge_port->port->tty->write_wait);
838 // Since we have more credit, check if more data can be sent
839 send_more_port_data(edge_serial, edge_port);
843 position += 2;
844 ++portNumber;
848 exit:
849 result = usb_submit_urb (urb, GFP_ATOMIC);
850 if (result) {
851 dev_err(&urb->dev->dev, "%s - Error %d submitting control urb\n", __FUNCTION__, result);
856 /*****************************************************************************
857 * edge_bulk_in_callback
858 * this is the callback function for when we have received data on the
859 * bulk in endpoint.
860 *****************************************************************************/
861 static void edge_bulk_in_callback (struct urb *urb, struct pt_regs *regs)
863 struct edgeport_serial *edge_serial = (struct edgeport_serial *)urb->context;
864 unsigned char *data = urb->transfer_buffer;
865 int status;
866 __u16 raw_data_length;
868 dbg("%s", __FUNCTION__);
870 if (serial_paranoia_check (edge_serial->serial, __FUNCTION__)) {
871 return;
874 if (urb->status) {
875 dbg("%s - nonzero read bulk status received: %d", __FUNCTION__, urb->status);
876 return;
879 if (urb->actual_length) {
880 raw_data_length = urb->actual_length;
882 usb_serial_debug_data (__FILE__, __FUNCTION__, raw_data_length, data);
884 /* decrement our rxBytes available by the number that we just got */
885 edge_serial->rxBytesAvail -= raw_data_length;
887 dbg("%s - Received = %d, rxBytesAvail %d", __FUNCTION__, raw_data_length, edge_serial->rxBytesAvail);
889 process_rcvd_data (edge_serial, data, urb->actual_length);
891 /* check to see if there's any more data for us to read */
892 if ((edge_serial->rxBytesAvail > 0) &&
893 (edge_serial->read_urb->status != -EINPROGRESS)) {
894 dbg(" --- Posting a read");
896 /* there is, so resubmit our urb */
897 edge_serial->read_urb->dev = edge_serial->serial->dev;
898 status = usb_submit_urb(edge_serial->read_urb, GFP_ATOMIC);
899 if (status) {
900 dev_err(&urb->dev->dev, "%s - usb_submit_urb(read bulk) failed, status = %d\n", __FUNCTION__, status);
907 /*****************************************************************************
908 * edge_bulk_out_data_callback
909 * this is the callback function for when we have finished sending serial data
910 * on the bulk out endpoint.
911 *****************************************************************************/
912 static void edge_bulk_out_data_callback (struct urb *urb, struct pt_regs *regs)
914 struct edgeport_port *edge_port = (struct edgeport_port *)urb->context;
915 struct tty_struct *tty;
917 dbg("%s", __FUNCTION__);
919 if (port_paranoia_check (edge_port->port, __FUNCTION__)) {
920 return;
923 if (urb->status) {
924 dbg("%s - nonzero write bulk status received: %d", __FUNCTION__, urb->status);
927 tty = edge_port->port->tty;
929 if (tty && edge_port->open) {
930 /* let the tty driver wakeup if it has a special write_wakeup function */
931 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) && tty->ldisc.write_wakeup) {
932 (tty->ldisc.write_wakeup)(tty);
935 /* tell the tty driver that something has changed */
936 wake_up_interruptible(&tty->write_wait);
939 // Release the Write URB
940 edge_port->write_in_progress = FALSE;
942 // Check if more data needs to be sent
943 send_more_port_data((struct edgeport_serial *)(usb_get_serial_data(edge_port->port->serial)), edge_port);
947 /*****************************************************************************
948 * BulkOutCmdCallback
949 * this is the callback function for when we have finished sending a command
950 * on the bulk out endpoint.
951 *****************************************************************************/
952 static void edge_bulk_out_cmd_callback (struct urb *urb, struct pt_regs *regs)
954 struct edgeport_port *edge_port = (struct edgeport_port *)urb->context;
955 struct tty_struct *tty;
956 int status = urb->status;
958 dbg("%s", __FUNCTION__);
960 CmdUrbs--;
961 dbg("%s - FREE URB %p (outstanding %d)", __FUNCTION__, urb, CmdUrbs);
964 /* clean up the transfer buffer */
965 if (urb->transfer_buffer != NULL) {
966 kfree(urb->transfer_buffer);
969 /* Free the command urb */
970 usb_free_urb (urb);
972 if (port_paranoia_check (edge_port->port, __FUNCTION__)) {
973 return;
976 if (status) {
977 dbg("%s - nonzero write bulk status received: %d", __FUNCTION__, status);
978 return;
981 /* Get pointer to tty */
982 tty = edge_port->port->tty;
984 /* tell the tty driver that something has changed */
985 if (tty && edge_port->open)
986 wake_up_interruptible(&tty->write_wait);
988 /* we have completed the command */
989 edge_port->commandPending = FALSE;
990 wake_up_interruptible(&edge_port->wait_command);
994 /*****************************************************************************
995 * Driver tty interface functions
996 *****************************************************************************/
998 /*****************************************************************************
999 * SerialOpen
1000 * this function is called by the tty driver when a port is opened
1001 * If successful, we return 0
1002 * Otherwise we return a negative error number.
1003 *****************************************************************************/
1004 static int edge_open (struct usb_serial_port *port, struct file * filp)
1006 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1007 struct usb_serial *serial;
1008 struct edgeport_serial *edge_serial;
1009 int response;
1010 int timeout;
1012 if (port_paranoia_check (port, __FUNCTION__))
1013 return -ENODEV;
1015 dbg("%s - port %d", __FUNCTION__, port->number);
1017 if (edge_port == NULL)
1018 return -ENODEV;
1020 /* force low_latency on so that our tty_push actually forces the data through,
1021 otherwise it is scheduled, and with high data rates (like with OHCI) data
1022 can get lost. */
1023 if (port->tty)
1024 port->tty->low_latency = 1;
1026 /* see if we've set up our endpoint info yet (can't set it up in edge_startup
1027 as the structures were not set up at that time.) */
1028 serial = port->serial;
1029 edge_serial = usb_get_serial_data(serial);
1030 if (edge_serial == NULL) {
1031 return -ENODEV;
1033 if (edge_serial->interrupt_in_buffer == NULL) {
1034 struct usb_serial_port *port0 = &serial->port[0];
1036 /* not set up yet, so do it now */
1037 edge_serial->interrupt_in_buffer = port0->interrupt_in_buffer;
1038 edge_serial->interrupt_in_endpoint = port0->interrupt_in_endpointAddress;
1039 edge_serial->interrupt_read_urb = port0->interrupt_in_urb;
1040 edge_serial->bulk_in_buffer = port0->bulk_in_buffer;
1041 edge_serial->bulk_in_endpoint = port0->bulk_in_endpointAddress;
1042 edge_serial->read_urb = port0->read_urb;
1043 edge_serial->bulk_out_endpoint = port0->bulk_out_endpointAddress;
1045 /* set up our interrupt urb */
1046 usb_fill_int_urb(edge_serial->interrupt_read_urb,
1047 serial->dev,
1048 usb_rcvintpipe(serial->dev,
1049 port0->interrupt_in_endpointAddress),
1050 port0->interrupt_in_buffer,
1051 edge_serial->interrupt_read_urb->transfer_buffer_length,
1052 edge_interrupt_callback, edge_serial,
1053 edge_serial->interrupt_read_urb->interval);
1055 /* set up our bulk in urb */
1056 usb_fill_bulk_urb(edge_serial->read_urb, serial->dev,
1057 usb_rcvbulkpipe(serial->dev,
1058 port0->bulk_in_endpointAddress),
1059 port0->bulk_in_buffer,
1060 edge_serial->read_urb->transfer_buffer_length,
1061 edge_bulk_in_callback, edge_serial);
1063 /* start interrupt read for this edgeport
1064 * this interrupt will continue as long as the edgeport is connected */
1065 response = usb_submit_urb (edge_serial->interrupt_read_urb, GFP_KERNEL);
1066 if (response) {
1067 dev_err(&port->dev, "%s - Error %d submitting control urb\n", __FUNCTION__, response);
1071 /* initialize our wait queues */
1072 init_waitqueue_head(&edge_port->wait_open);
1073 init_waitqueue_head(&edge_port->wait_chase);
1074 init_waitqueue_head(&edge_port->delta_msr_wait);
1075 init_waitqueue_head(&edge_port->wait_command);
1077 /* initialize our icount structure */
1078 memset (&(edge_port->icount), 0x00, sizeof(edge_port->icount));
1080 /* initialize our port settings */
1081 edge_port->txCredits = 0; /* Can't send any data yet */
1082 edge_port->shadowMCR = MCR_MASTER_IE; /* Must always set this bit to enable ints! */
1083 edge_port->chaseResponsePending = FALSE;
1085 /* send a open port command */
1086 edge_port->openPending = TRUE;
1087 edge_port->open = FALSE;
1088 response = send_iosp_ext_cmd (edge_port, IOSP_CMD_OPEN_PORT, 0);
1090 if (response < 0) {
1091 dev_err(&port->dev, "%s - error sending open port command\n", __FUNCTION__);
1092 edge_port->openPending = FALSE;
1093 return -ENODEV;
1096 /* now wait for the port to be completely opened */
1097 timeout = OPEN_TIMEOUT;
1098 while (timeout && edge_port->openPending == TRUE) {
1099 timeout = interruptible_sleep_on_timeout (&edge_port->wait_open, timeout);
1102 if (edge_port->open == FALSE) {
1103 /* open timed out */
1104 dbg("%s - open timedout", __FUNCTION__);
1105 edge_port->openPending = FALSE;
1106 return -ENODEV;
1109 /* create the txfifo */
1110 edge_port->txfifo.head = 0;
1111 edge_port->txfifo.tail = 0;
1112 edge_port->txfifo.count = 0;
1113 edge_port->txfifo.size = edge_port->maxTxCredits;
1114 edge_port->txfifo.fifo = kmalloc (edge_port->maxTxCredits, GFP_KERNEL);
1116 if (!edge_port->txfifo.fifo) {
1117 dbg("%s - no memory", __FUNCTION__);
1118 edge_close (port, filp);
1119 return -ENOMEM;
1122 /* Allocate a URB for the write */
1123 edge_port->write_urb = usb_alloc_urb (0, GFP_KERNEL);
1125 if (!edge_port->write_urb) {
1126 dbg("%s - no memory", __FUNCTION__);
1127 edge_close (port, filp);
1128 return -ENOMEM;
1131 dbg("%s(%d) - Initialize TX fifo to %d bytes", __FUNCTION__, port->number, edge_port->maxTxCredits);
1133 dbg("%s exited", __FUNCTION__);
1135 return 0;
1139 /************************************************************************
1141 * block_until_chase_response
1143 * This function will block the close until one of the following:
1144 * 1. Response to our Chase comes from Edgeport
1145 * 2. A timout of 10 seconds without activity has expired
1146 * (1K of Edgeport data @ 2400 baud ==> 4 sec to empty)
1148 ************************************************************************/
1149 static void block_until_chase_response(struct edgeport_port *edge_port)
1151 __u16 lastCredits;
1152 int timeout = 1*HZ;
1153 int wait = 10;
1155 while (1) {
1156 // Save Last credits
1157 lastCredits = edge_port->txCredits;
1159 // Did we get our Chase response
1160 if (edge_port->chaseResponsePending == FALSE) {
1161 dbg("%s - Got Chase Response", __FUNCTION__);
1163 // did we get all of our credit back?
1164 if (edge_port->txCredits == edge_port->maxTxCredits ) {
1165 dbg("%s - Got all credits", __FUNCTION__);
1166 return;
1170 // Block the thread for a while
1171 interruptible_sleep_on_timeout (&edge_port->wait_chase, timeout);
1173 if (lastCredits == edge_port->txCredits) {
1174 // No activity.. count down.
1175 wait--;
1176 if (wait == 0) {
1177 edge_port->chaseResponsePending = FALSE;
1178 dbg("%s - Chase TIMEOUT", __FUNCTION__);
1179 return;
1181 } else {
1182 // Reset timout value back to 10 seconds
1183 dbg("%s - Last %d, Current %d", __FUNCTION__, lastCredits, edge_port->txCredits);
1184 wait = 10;
1190 /************************************************************************
1192 * block_until_tx_empty
1194 * This function will block the close until one of the following:
1195 * 1. TX count are 0
1196 * 2. The edgeport has stopped
1197 * 3. A timout of 3 seconds without activity has expired
1199 ************************************************************************/
1200 static void block_until_tx_empty (struct edgeport_port *edge_port)
1202 struct TxFifo *fifo = &edge_port->txfifo;
1203 __u32 lastCount;
1204 int timeout = HZ/10;
1205 int wait = 30;
1207 while (1) {
1208 // Save Last count
1209 lastCount = fifo->count;
1211 // Is the Edgeport Buffer empty?
1212 if (lastCount == 0) {
1213 dbg("%s - TX Buffer Empty", __FUNCTION__);
1214 return;
1217 // Block the thread for a while
1218 interruptible_sleep_on_timeout (&edge_port->wait_chase, timeout);
1220 dbg("%s wait", __FUNCTION__);
1222 if (lastCount == fifo->count) {
1223 // No activity.. count down.
1224 wait--;
1225 if (wait == 0) {
1226 dbg("%s - TIMEOUT", __FUNCTION__);
1227 return;
1229 } else {
1230 // Reset timout value back to seconds
1231 wait = 30;
1237 /*****************************************************************************
1238 * edge_close
1239 * this function is called by the tty driver when a port is closed
1240 *****************************************************************************/
1241 static void edge_close (struct usb_serial_port *port, struct file * filp)
1243 struct usb_serial *serial;
1244 struct edgeport_serial *edge_serial;
1245 struct edgeport_port *edge_port;
1246 int status;
1248 if (port_paranoia_check (port, __FUNCTION__))
1249 return;
1251 dbg("%s - port %d", __FUNCTION__, port->number);
1253 serial = get_usb_serial (port, __FUNCTION__);
1254 if (!serial)
1255 return;
1257 edge_serial = usb_get_serial_data(serial);
1258 edge_port = usb_get_serial_port_data(port);
1259 if ((edge_serial == NULL) || (edge_port == NULL))
1260 return;
1262 if (serial->dev) {
1263 // block until tx is empty
1264 block_until_tx_empty(edge_port);
1266 edge_port->closePending = TRUE;
1268 /* flush and chase */
1269 edge_port->chaseResponsePending = TRUE;
1271 dbg("%s - Sending IOSP_CMD_CHASE_PORT", __FUNCTION__);
1272 status = send_iosp_ext_cmd (edge_port, IOSP_CMD_CHASE_PORT, 0);
1273 if (status == 0) {
1274 // block until chase finished
1275 block_until_chase_response(edge_port);
1276 } else {
1277 edge_port->chaseResponsePending = FALSE;
1280 /* close the port */
1281 dbg("%s - Sending IOSP_CMD_CLOSE_PORT", __FUNCTION__);
1282 send_iosp_ext_cmd (edge_port, IOSP_CMD_CLOSE_PORT, 0);
1284 //port->close = TRUE;
1285 edge_port->closePending = FALSE;
1286 edge_port->open = FALSE;
1287 edge_port->openPending = FALSE;
1289 if (edge_port->write_urb) {
1290 usb_unlink_urb (edge_port->write_urb);
1294 if (edge_port->write_urb) {
1295 /* if this urb had a transfer buffer already (old transfer) free it */
1296 if (edge_port->write_urb->transfer_buffer != NULL) {
1297 kfree(edge_port->write_urb->transfer_buffer);
1299 usb_free_urb (edge_port->write_urb);
1301 if (edge_port->txfifo.fifo) {
1302 kfree(edge_port->txfifo.fifo);
1305 dbg("%s exited", __FUNCTION__);
1308 /*****************************************************************************
1309 * SerialWrite
1310 * this function is called by the tty driver when data should be written to
1311 * the port.
1312 * If successful, we return the number of bytes written, otherwise we return
1313 * a negative error number.
1314 *****************************************************************************/
1315 static int edge_write (struct usb_serial_port *port, int from_user, const unsigned char *data, int count)
1317 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1318 struct TxFifo *fifo;
1319 int copySize;
1320 int bytesleft;
1321 int firsthalf;
1322 int secondhalf;
1324 dbg("%s - port %d", __FUNCTION__, port->number);
1326 if (edge_port == NULL)
1327 return -ENODEV;
1329 // get a pointer to the Tx fifo
1330 fifo = &edge_port->txfifo;
1332 // calculate number of bytes to put in fifo
1333 copySize = min ((unsigned int)count, (edge_port->txCredits - fifo->count));
1335 dbg("%s(%d) of %d byte(s) Fifo room %d -- will copy %d bytes", __FUNCTION__,
1336 port->number, count, edge_port->txCredits - fifo->count, copySize);
1338 /* catch writes of 0 bytes which the tty driver likes to give us, and when txCredits is empty */
1339 if (copySize == 0) {
1340 dbg("%s - copySize = Zero", __FUNCTION__);
1341 return 0;
1344 // queue the data
1345 // since we can never overflow the buffer we do not have to check for full condition
1347 // the copy is done is two parts -- first fill to the end of the buffer
1348 // then copy the reset from the start of the buffer
1350 bytesleft = fifo->size - fifo->head;
1351 firsthalf = min (bytesleft, copySize);
1352 dbg("%s - copy %d bytes of %d into fifo ", __FUNCTION__, firsthalf, bytesleft);
1354 /* now copy our data */
1355 if (from_user) {
1356 if (copy_from_user(&fifo->fifo[fifo->head], data, firsthalf))
1357 return -EFAULT;
1358 } else {
1359 memcpy(&fifo->fifo[fifo->head], data, firsthalf);
1361 usb_serial_debug_data (__FILE__, __FUNCTION__, firsthalf, &fifo->fifo[fifo->head]);
1363 // update the index and size
1364 fifo->head += firsthalf;
1365 fifo->count += firsthalf;
1367 // wrap the index
1368 if (fifo->head == fifo->size) {
1369 fifo->head = 0;
1372 secondhalf = copySize-firsthalf;
1374 if (secondhalf) {
1375 dbg("%s - copy rest of data %d", __FUNCTION__, secondhalf);
1376 if (from_user) {
1377 if (copy_from_user(&fifo->fifo[fifo->head], &data[firsthalf], secondhalf))
1378 return -EFAULT;
1379 } else {
1380 memcpy(&fifo->fifo[fifo->head], &data[firsthalf], secondhalf);
1382 usb_serial_debug_data (__FILE__, __FUNCTION__, secondhalf, &fifo->fifo[fifo->head]);
1383 // update the index and size
1384 fifo->count += secondhalf;
1385 fifo->head += secondhalf;
1386 // No need to check for wrap since we can not get to end of fifo in this part
1389 send_more_port_data((struct edgeport_serial *)usb_get_serial_data(port->serial), edge_port);
1391 dbg("%s wrote %d byte(s) TxCredits %d, Fifo %d", __FUNCTION__, copySize, edge_port->txCredits, fifo->count);
1393 return copySize;
1397 /************************************************************************
1399 * send_more_port_data()
1401 * This routine attempts to write additional UART transmit data
1402 * to a port over the USB bulk pipe. It is called (1) when new
1403 * data has been written to a port's TxBuffer from higher layers
1404 * (2) when the peripheral sends us additional TxCredits indicating
1405 * that it can accept more Tx data for a given port; and (3) when
1406 * a bulk write completes successfully and we want to see if we
1407 * can transmit more.
1409 ************************************************************************/
1410 static void send_more_port_data(struct edgeport_serial *edge_serial, struct edgeport_port *edge_port)
1412 struct TxFifo *fifo = &edge_port->txfifo;
1413 struct urb *urb;
1414 unsigned char *buffer;
1415 int status;
1416 int count;
1417 int bytesleft;
1418 int firsthalf;
1419 int secondhalf;
1421 dbg("%s(%d)", __FUNCTION__, edge_port->port->number);
1423 if (edge_port->write_in_progress ||
1424 !edge_port->open ||
1425 (fifo->count == 0)) {
1426 dbg("%s(%d) EXIT - fifo %d, PendingWrite = %d", __FUNCTION__, edge_port->port->number, fifo->count, edge_port->write_in_progress);
1427 return;
1430 // since the amount of data in the fifo will always fit into the
1431 // edgeport buffer we do not need to check the write length
1433 // Do we have enough credits for this port to make it worthwhile
1434 // to bother queueing a write. If it's too small, say a few bytes,
1435 // it's better to wait for more credits so we can do a larger
1436 // write.
1437 if (edge_port->txCredits < EDGE_FW_GET_TX_CREDITS_SEND_THRESHOLD(edge_port->maxTxCredits)) {
1438 dbg("%s(%d) Not enough credit - fifo %d TxCredit %d", __FUNCTION__, edge_port->port->number, fifo->count, edge_port->txCredits );
1439 return;
1442 // lock this write
1443 edge_port->write_in_progress = TRUE;
1445 // get a pointer to the write_urb
1446 urb = edge_port->write_urb;
1448 /* if this urb had a transfer buffer already (old transfer) free it */
1449 if (urb->transfer_buffer != NULL) {
1450 kfree(urb->transfer_buffer);
1451 urb->transfer_buffer = NULL;
1454 /* build the data header for the buffer and port that we are about to send out */
1455 count = fifo->count;
1456 buffer = kmalloc (count+2, GFP_ATOMIC);
1457 if (buffer == NULL) {
1458 dev_err(&edge_serial->serial->dev->dev, "%s - no more kernel memory...\n", __FUNCTION__);
1459 edge_port->write_in_progress = FALSE;
1460 return;
1462 buffer[0] = IOSP_BUILD_DATA_HDR1 (edge_port->port->number - edge_port->port->serial->minor, count);
1463 buffer[1] = IOSP_BUILD_DATA_HDR2 (edge_port->port->number - edge_port->port->serial->minor, count);
1465 /* now copy our data */
1466 bytesleft = fifo->size - fifo->tail;
1467 firsthalf = min (bytesleft, count);
1468 memcpy(&buffer[2], &fifo->fifo[fifo->tail], firsthalf);
1469 fifo->tail += firsthalf;
1470 fifo->count -= firsthalf;
1471 if (fifo->tail == fifo->size) {
1472 fifo->tail = 0;
1475 secondhalf = count-firsthalf;
1476 if (secondhalf) {
1477 memcpy(&buffer[2+firsthalf], &fifo->fifo[fifo->tail], secondhalf);
1478 fifo->tail += secondhalf;
1479 fifo->count -= secondhalf;
1482 if (count) {
1483 usb_serial_debug_data (__FILE__, __FUNCTION__, count, &buffer[2]);
1486 /* fill up the urb with all of our data and submit it */
1487 usb_fill_bulk_urb (urb, edge_serial->serial->dev,
1488 usb_sndbulkpipe(edge_serial->serial->dev, edge_serial->bulk_out_endpoint),
1489 buffer, count+2, edge_bulk_out_data_callback, edge_port);
1491 urb->dev = edge_serial->serial->dev;
1492 status = usb_submit_urb(urb, GFP_ATOMIC);
1493 if (status) {
1494 /* something went wrong */
1495 dbg("%s - usb_submit_urb(write bulk) failed", __FUNCTION__);
1496 edge_port->write_in_progress = FALSE;
1497 } else {
1498 /* decrement the number of credits we have by the number we just sent */
1499 edge_port->txCredits -= count;
1500 edge_port->icount.tx += count;
1502 dbg("%s wrote %d byte(s) TxCredit %d, Fifo %d", __FUNCTION__, count, edge_port->txCredits, fifo->count);
1506 /*****************************************************************************
1507 * edge_write_room
1508 * this function is called by the tty driver when it wants to know how many
1509 * bytes of data we can accept for a specific port.
1510 * If successful, we return the amount of room that we have for this port
1511 * (the txCredits),
1512 * Otherwise we return a negative error number.
1513 *****************************************************************************/
1514 static int edge_write_room (struct usb_serial_port *port)
1516 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1517 int room;
1519 dbg("%s", __FUNCTION__);
1521 if (edge_port == NULL)
1522 return -ENODEV;
1523 if (edge_port->closePending == TRUE)
1524 return -ENODEV;
1526 dbg("%s - port %d", __FUNCTION__, port->number);
1528 if (!edge_port->open) {
1529 dbg("%s - port not opened", __FUNCTION__);
1530 return -EINVAL;
1533 // total of both buffers is still txCredit
1534 room = edge_port->txCredits - edge_port->txfifo.count;
1536 dbg("%s - returns %d", __FUNCTION__, room);
1537 return room;
1541 /*****************************************************************************
1542 * edge_chars_in_buffer
1543 * this function is called by the tty driver when it wants to know how many
1544 * bytes of data we currently have outstanding in the port (data that has
1545 * been written, but hasn't made it out the port yet)
1546 * If successful, we return the number of bytes left to be written in the
1547 * system,
1548 * Otherwise we return a negative error number.
1549 *****************************************************************************/
1550 static int edge_chars_in_buffer (struct usb_serial_port *port)
1552 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1553 int num_chars;
1555 dbg("%s", __FUNCTION__);
1557 if (edge_port == NULL)
1558 return -ENODEV;
1559 if (edge_port->closePending == TRUE)
1560 return -ENODEV;
1562 if (!edge_port->open) {
1563 dbg("%s - port not opened", __FUNCTION__);
1564 return -EINVAL;
1567 num_chars = edge_port->maxTxCredits - edge_port->txCredits + edge_port->txfifo.count;
1568 if (num_chars) {
1569 dbg("%s(port %d) - returns %d", __FUNCTION__, port->number, num_chars);
1572 return num_chars;
1576 /*****************************************************************************
1577 * SerialThrottle
1578 * this function is called by the tty driver when it wants to stop the data
1579 * being read from the port.
1580 *****************************************************************************/
1581 static void edge_throttle (struct usb_serial_port *port)
1583 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1584 struct tty_struct *tty;
1585 int status;
1587 dbg("%s - port %d", __FUNCTION__, port->number);
1589 if (edge_port == NULL)
1590 return;
1592 if (!edge_port->open) {
1593 dbg("%s - port not opened", __FUNCTION__);
1594 return;
1597 tty = port->tty;
1598 if (!tty) {
1599 dbg ("%s - no tty available", __FUNCTION__);
1600 return;
1603 /* if we are implementing XON/XOFF, send the stop character */
1604 if (I_IXOFF(tty)) {
1605 unsigned char stop_char = STOP_CHAR(tty);
1606 status = edge_write (port, 0, &stop_char, 1);
1607 if (status <= 0) {
1608 return;
1612 /* if we are implementing RTS/CTS, toggle that line */
1613 if (tty->termios->c_cflag & CRTSCTS) {
1614 edge_port->shadowMCR &= ~MCR_RTS;
1615 status = send_cmd_write_uart_register(edge_port, MCR, edge_port->shadowMCR);
1616 if (status != 0) {
1617 return;
1621 return;
1625 /*****************************************************************************
1626 * edge_unthrottle
1627 * this function is called by the tty driver when it wants to resume the data
1628 * being read from the port (called after SerialThrottle is called)
1629 *****************************************************************************/
1630 static void edge_unthrottle (struct usb_serial_port *port)
1632 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1633 struct tty_struct *tty;
1634 int status;
1636 dbg("%s - port %d", __FUNCTION__, port->number);
1638 if (edge_port == NULL)
1639 return;
1641 if (!edge_port->open) {
1642 dbg("%s - port not opened", __FUNCTION__);
1643 return;
1646 tty = port->tty;
1647 if (!tty) {
1648 dbg ("%s - no tty available", __FUNCTION__);
1649 return;
1652 /* if we are implementing XON/XOFF, send the start character */
1653 if (I_IXOFF(tty)) {
1654 unsigned char start_char = START_CHAR(tty);
1655 status = edge_write (port, 0, &start_char, 1);
1656 if (status <= 0) {
1657 return;
1661 /* if we are implementing RTS/CTS, toggle that line */
1662 if (tty->termios->c_cflag & CRTSCTS) {
1663 edge_port->shadowMCR |= MCR_RTS;
1664 status = send_cmd_write_uart_register(edge_port, MCR, edge_port->shadowMCR);
1665 if (status != 0) {
1666 return;
1670 return;
1674 /*****************************************************************************
1675 * SerialSetTermios
1676 * this function is called by the tty driver when it wants to change the termios structure
1677 *****************************************************************************/
1678 static void edge_set_termios (struct usb_serial_port *port, struct termios *old_termios)
1680 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1681 struct tty_struct *tty = port->tty;
1682 unsigned int cflag;
1684 if (!port->tty || !port->tty->termios) {
1685 dbg ("%s - no tty or termios", __FUNCTION__);
1686 return;
1689 cflag = tty->termios->c_cflag;
1690 /* check that they really want us to change something */
1691 if (old_termios) {
1692 if ((cflag == old_termios->c_cflag) &&
1693 (RELEVANT_IFLAG(tty->termios->c_iflag) == RELEVANT_IFLAG(old_termios->c_iflag))) {
1694 dbg("%s - nothing to change", __FUNCTION__);
1695 return;
1699 dbg("%s - clfag %08x iflag %08x", __FUNCTION__,
1700 tty->termios->c_cflag,
1701 RELEVANT_IFLAG(tty->termios->c_iflag));
1702 if (old_termios) {
1703 dbg("%s - old clfag %08x old iflag %08x", __FUNCTION__,
1704 old_termios->c_cflag,
1705 RELEVANT_IFLAG(old_termios->c_iflag));
1708 dbg("%s - port %d", __FUNCTION__, port->number);
1710 if (edge_port == NULL)
1711 return;
1713 if (!edge_port->open) {
1714 dbg("%s - port not opened", __FUNCTION__);
1715 return;
1718 /* change the port settings to the new ones specified */
1719 change_port_settings (edge_port, old_termios);
1721 return;
1725 /*****************************************************************************
1726 * get_lsr_info - get line status register info
1728 * Purpose: Let user call ioctl() to get info when the UART physically
1729 * is emptied. On bus types like RS485, the transmitter must
1730 * release the bus after transmitting. This must be done when
1731 * the transmit shift register is empty, not be done when the
1732 * transmit holding register is empty. This functionality
1733 * allows an RS485 driver to be written in user space.
1734 *****************************************************************************/
1735 static int get_lsr_info(struct edgeport_port *edge_port, unsigned int *value)
1737 unsigned int result = 0;
1739 if (edge_port->maxTxCredits == edge_port->txCredits &&
1740 edge_port->txfifo.count == 0) {
1741 dbg("%s -- Empty", __FUNCTION__);
1742 result = TIOCSER_TEMT;
1745 if (copy_to_user(value, &result, sizeof(int)))
1746 return -EFAULT;
1747 return 0;
1750 static int get_number_bytes_avail(struct edgeport_port *edge_port, unsigned int *value)
1752 unsigned int result = 0;
1753 struct tty_struct *tty = edge_port->port->tty;
1755 if (!tty)
1756 return -ENOIOCTLCMD;
1758 result = tty->read_cnt;
1760 dbg("%s(%d) = %d", __FUNCTION__, edge_port->port->number, result);
1761 if (copy_to_user(value, &result, sizeof(int)))
1762 return -EFAULT;
1763 //return 0;
1764 return -ENOIOCTLCMD;
1767 static int edge_tiocmset (struct usb_serial_port *port, struct file *file, unsigned int set, unsigned int clear)
1769 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1770 unsigned int mcr;
1772 dbg("%s - port %d", __FUNCTION__, port->number);
1774 mcr = edge_port->shadowMCR;
1775 if (set & TIOCM_RTS)
1776 mcr |= MCR_RTS;
1777 if (set & TIOCM_DTR)
1778 mcr |= MCR_DTR;
1779 if (set & TIOCM_LOOP)
1780 mcr |= MCR_LOOPBACK;
1782 if (clear & TIOCM_RTS)
1783 mcr &= ~MCR_RTS;
1784 if (clear & TIOCM_DTR)
1785 mcr &= ~MCR_DTR;
1786 if (clear & TIOCM_LOOP)
1787 mcr &= ~MCR_LOOPBACK;
1789 edge_port->shadowMCR = mcr;
1791 send_cmd_write_uart_register(edge_port, MCR, edge_port->shadowMCR);
1793 return 0;
1796 static int edge_tiocmget(struct usb_serial_port *port, struct file *file)
1798 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1799 unsigned int result = 0;
1800 unsigned int msr;
1801 unsigned int mcr;
1803 dbg("%s - port %d", __FUNCTION__, port->number);
1805 msr = edge_port->shadowMSR;
1806 mcr = edge_port->shadowMCR;
1807 result = ((mcr & MCR_DTR) ? TIOCM_DTR: 0) /* 0x002 */
1808 | ((mcr & MCR_RTS) ? TIOCM_RTS: 0) /* 0x004 */
1809 | ((msr & MSR_CTS) ? TIOCM_CTS: 0) /* 0x020 */
1810 | ((msr & MSR_CD) ? TIOCM_CAR: 0) /* 0x040 */
1811 | ((msr & MSR_RI) ? TIOCM_RI: 0) /* 0x080 */
1812 | ((msr & MSR_DSR) ? TIOCM_DSR: 0); /* 0x100 */
1815 dbg("%s -- %x", __FUNCTION__, result);
1817 return result;
1820 static int get_serial_info(struct edgeport_port *edge_port, struct serial_struct * retinfo)
1822 struct serial_struct tmp;
1824 if (!retinfo)
1825 return -EFAULT;
1827 memset(&tmp, 0, sizeof(tmp));
1829 tmp.type = PORT_16550A;
1830 tmp.line = edge_port->port->serial->minor;
1831 tmp.port = edge_port->port->number;
1832 tmp.irq = 0;
1833 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
1834 tmp.xmit_fifo_size = edge_port->maxTxCredits;
1835 tmp.baud_base = 9600;
1836 tmp.close_delay = 5*HZ;
1837 tmp.closing_wait = 30*HZ;
1838 // tmp.custom_divisor = state->custom_divisor;
1839 // tmp.hub6 = state->hub6;
1840 // tmp.io_type = state->io_type;
1843 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1844 return -EFAULT;
1845 return 0;
1850 /*****************************************************************************
1851 * SerialIoctl
1852 * this function handles any ioctl calls to the driver
1853 *****************************************************************************/
1854 static int edge_ioctl (struct usb_serial_port *port, struct file *file, unsigned int cmd, unsigned long arg)
1856 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1857 struct async_icount cnow;
1858 struct async_icount cprev;
1859 struct serial_icounter_struct icount;
1862 dbg("%s - port %d, cmd = 0x%x", __FUNCTION__, port->number, cmd);
1864 switch (cmd) {
1865 // return number of bytes available
1866 case TIOCINQ:
1867 dbg("%s (%d) TIOCINQ", __FUNCTION__, port->number);
1868 return get_number_bytes_avail(edge_port, (unsigned int *) arg);
1869 break;
1871 case TIOCSERGETLSR:
1872 dbg("%s (%d) TIOCSERGETLSR", __FUNCTION__, port->number);
1873 return get_lsr_info(edge_port, (unsigned int *) arg);
1874 return 0;
1876 case TIOCGSERIAL:
1877 dbg("%s (%d) TIOCGSERIAL", __FUNCTION__, port->number);
1878 return get_serial_info(edge_port, (struct serial_struct *) arg);
1880 case TIOCSSERIAL:
1881 dbg("%s (%d) TIOCSSERIAL", __FUNCTION__, port->number);
1882 break;
1884 case TIOCMIWAIT:
1885 dbg("%s (%d) TIOCMIWAIT", __FUNCTION__, port->number);
1886 cprev = edge_port->icount;
1887 while (1) {
1888 interruptible_sleep_on(&edge_port->delta_msr_wait);
1889 /* see if a signal did it */
1890 if (signal_pending(current))
1891 return -ERESTARTSYS;
1892 cnow = edge_port->icount;
1893 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
1894 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
1895 return -EIO; /* no change => error */
1896 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1897 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1898 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1899 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)) ) {
1900 return 0;
1902 cprev = cnow;
1904 /* NOTREACHED */
1905 break;
1907 case TIOCGICOUNT:
1908 cnow = edge_port->icount;
1909 icount.cts = cnow.cts;
1910 icount.dsr = cnow.dsr;
1911 icount.rng = cnow.rng;
1912 icount.dcd = cnow.dcd;
1913 icount.rx = cnow.rx;
1914 icount.tx = cnow.tx;
1915 icount.frame = cnow.frame;
1916 icount.overrun = cnow.overrun;
1917 icount.parity = cnow.parity;
1918 icount.brk = cnow.brk;
1919 icount.buf_overrun = cnow.buf_overrun;
1921 dbg("%s (%d) TIOCGICOUNT RX=%d, TX=%d", __FUNCTION__, port->number, icount.rx, icount.tx );
1922 if (copy_to_user((void *)arg, &icount, sizeof(icount)))
1923 return -EFAULT;
1924 return 0;
1927 return -ENOIOCTLCMD;
1931 /*****************************************************************************
1932 * SerialBreak
1933 * this function sends a break to the port
1934 *****************************************************************************/
1935 static void edge_break (struct usb_serial_port *port, int break_state)
1937 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1938 int status;
1940 /* flush and chase */
1941 edge_port->chaseResponsePending = TRUE;
1943 dbg("%s - Sending IOSP_CMD_CHASE_PORT", __FUNCTION__);
1944 status = send_iosp_ext_cmd (edge_port, IOSP_CMD_CHASE_PORT, 0);
1945 if (status == 0) {
1946 // block until chase finished
1947 block_until_chase_response(edge_port);
1948 } else {
1949 edge_port->chaseResponsePending = FALSE;
1952 if (break_state == -1) {
1953 dbg("%s - Sending IOSP_CMD_SET_BREAK", __FUNCTION__);
1954 status = send_iosp_ext_cmd (edge_port, IOSP_CMD_SET_BREAK, 0);
1955 } else {
1956 dbg("%s - Sending IOSP_CMD_CLEAR_BREAK", __FUNCTION__);
1957 status = send_iosp_ext_cmd (edge_port, IOSP_CMD_CLEAR_BREAK, 0);
1959 if (status) {
1960 dbg("%s - error sending break set/clear command.", __FUNCTION__);
1963 return;
1967 /*****************************************************************************
1968 * process_rcvd_data
1969 * this function handles the data received on the bulk in pipe.
1970 *****************************************************************************/
1971 static int process_rcvd_data (struct edgeport_serial *edge_serial, unsigned char * buffer, __u16 bufferLength)
1973 struct usb_serial_port *port;
1974 struct edgeport_port *edge_port;
1975 struct tty_struct *tty;
1976 __u16 lastBufferLength;
1977 __u16 rxLen;
1978 int i;
1980 dbg("%s", __FUNCTION__);
1982 lastBufferLength = bufferLength + 1;
1984 while (bufferLength > 0) {
1985 /* failsafe incase we get a message that we don't understand */
1986 if (lastBufferLength == bufferLength) {
1987 dbg("%s - stuck in loop, exiting it.", __FUNCTION__);
1988 break;
1990 lastBufferLength = bufferLength;
1992 switch (edge_serial->rxState) {
1993 case EXPECT_HDR1:
1994 edge_serial->rxHeader1 = *buffer;
1995 ++buffer;
1996 --bufferLength;
1998 if (bufferLength == 0) {
1999 edge_serial->rxState = EXPECT_HDR2;
2000 break;
2002 /* otherwise, drop on through */
2004 case EXPECT_HDR2:
2005 edge_serial->rxHeader2 = *buffer;
2006 ++buffer;
2007 --bufferLength;
2009 dbg("%s - Hdr1=%02X Hdr2=%02X", __FUNCTION__, edge_serial->rxHeader1, edge_serial->rxHeader2);
2011 // Process depending on whether this header is
2012 // data or status
2014 if (IS_CMD_STAT_HDR(edge_serial->rxHeader1)) {
2015 // Decode this status header and goto EXPECT_HDR1 (if we
2016 // can process the status with only 2 bytes), or goto
2017 // EXPECT_HDR3 to get the third byte.
2019 edge_serial->rxPort = IOSP_GET_HDR_PORT(edge_serial->rxHeader1);
2020 edge_serial->rxStatusCode = IOSP_GET_STATUS_CODE(edge_serial->rxHeader1);
2022 if (!IOSP_STATUS_IS_2BYTE(edge_serial->rxStatusCode)) {
2023 // This status needs additional bytes. Save what we have
2024 // and then wait for more data.
2025 edge_serial->rxStatusParam = edge_serial->rxHeader2;
2027 edge_serial->rxState = EXPECT_HDR3;
2028 break;
2031 // We have all the header bytes, process the status now
2032 process_rcvd_status (edge_serial, edge_serial->rxHeader2, 0);
2034 edge_serial->rxState = EXPECT_HDR1;
2035 break;
2036 } else {
2037 edge_serial->rxPort = IOSP_GET_HDR_PORT(edge_serial->rxHeader1);
2038 edge_serial->rxBytesRemaining = IOSP_GET_HDR_DATA_LEN(edge_serial->rxHeader1, edge_serial->rxHeader2);
2040 dbg("%s - Data for Port %u Len %u", __FUNCTION__, edge_serial->rxPort, edge_serial->rxBytesRemaining);
2042 //ASSERT( DevExt->RxPort < DevExt->NumPorts );
2043 //ASSERT( DevExt->RxBytesRemaining < IOSP_MAX_DATA_LENGTH );
2045 if (bufferLength == 0 ) {
2046 edge_serial->rxState = EXPECT_DATA;
2047 break;
2049 // Else, drop through
2052 case EXPECT_DATA: // Expect data
2054 if (bufferLength < edge_serial->rxBytesRemaining) {
2055 rxLen = bufferLength;
2056 edge_serial->rxState = EXPECT_DATA; // Expect data to start next buffer
2057 } else {
2058 // BufLen >= RxBytesRemaining
2059 rxLen = edge_serial->rxBytesRemaining;
2060 edge_serial->rxState = EXPECT_HDR1; // Start another header next time
2063 bufferLength -= rxLen;
2064 edge_serial->rxBytesRemaining -= rxLen;
2066 /* spit this data back into the tty driver if this port is open */
2067 if (rxLen) {
2068 port = &edge_serial->serial->port[edge_serial->rxPort];
2069 if (port_paranoia_check (port, __FUNCTION__) == 0) {
2070 edge_port = usb_get_serial_port_data(port);
2071 if (edge_port->open) {
2072 tty = edge_port->port->tty;
2073 if (tty) {
2074 dbg("%s - Sending %d bytes to TTY for port %d", __FUNCTION__, rxLen, edge_serial->rxPort);
2075 for (i = 0; i < rxLen ; ++i) {
2076 /* if we insert more than TTY_FLIPBUF_SIZE characters, we drop them. */
2077 if(tty->flip.count >= TTY_FLIPBUF_SIZE) {
2078 tty_flip_buffer_push(tty);
2080 /* this doesn't actually push the data through unless tty->low_latency is set */
2081 tty_insert_flip_char(tty, buffer[i], 0);
2083 tty_flip_buffer_push(tty);
2085 edge_port->icount.rx += rxLen;
2088 buffer += rxLen;
2091 break;
2093 case EXPECT_HDR3: // Expect 3rd byte of status header
2094 edge_serial->rxHeader3 = *buffer;
2095 ++buffer;
2096 --bufferLength;
2098 // We have all the header bytes, process the status now
2099 process_rcvd_status (edge_serial, edge_serial->rxStatusParam, edge_serial->rxHeader3);
2100 edge_serial->rxState = EXPECT_HDR1;
2101 break;
2106 return 0;
2110 /*****************************************************************************
2111 * process_rcvd_status
2112 * this function handles the any status messages received on the bulk in pipe.
2113 *****************************************************************************/
2114 static void process_rcvd_status (struct edgeport_serial *edge_serial, __u8 byte2, __u8 byte3)
2116 struct usb_serial_port *port;
2117 struct edgeport_port *edge_port;
2118 __u8 code = edge_serial->rxStatusCode;
2120 /* switch the port pointer to the one being currently talked about */
2121 port = &edge_serial->serial->port[edge_serial->rxPort];
2122 if (port_paranoia_check (port, __FUNCTION__)) {
2123 return;
2125 edge_port = usb_get_serial_port_data(port);
2126 if (edge_port == NULL) {
2127 dev_err(&edge_serial->serial->dev->dev, "%s - edge_port == NULL for port %d\n", __FUNCTION__, edge_serial->rxPort);
2128 return;
2131 dbg("%s - port %d", __FUNCTION__, edge_serial->rxPort);
2133 if (code == IOSP_EXT_STATUS) {
2134 switch (byte2) {
2135 case IOSP_EXT_STATUS_CHASE_RSP:
2136 // we want to do EXT status regardless of port open/closed
2137 dbg("%s - Port %u EXT CHASE_RSP Data = %02x", __FUNCTION__, edge_serial->rxPort, byte3 );
2138 // Currently, the only EXT_STATUS is Chase, so process here instead of one more call
2139 // to one more subroutine. If/when more EXT_STATUS, there'll be more work to do.
2140 // Also, we currently clear flag and close the port regardless of content of above's Byte3.
2141 // We could choose to do something else when Byte3 says Timeout on Chase from Edgeport,
2142 // like wait longer in block_until_chase_response, but for now we don't.
2143 edge_port->chaseResponsePending = FALSE;
2144 wake_up_interruptible (&edge_port->wait_chase);
2145 return;
2147 case IOSP_EXT_STATUS_RX_CHECK_RSP:
2148 dbg("%s ========== Port %u CHECK_RSP Sequence = %02x =============\n", __FUNCTION__, edge_serial->rxPort, byte3 );
2149 //Port->RxCheckRsp = TRUE;
2150 return;
2154 if (code == IOSP_STATUS_OPEN_RSP) {
2155 edge_port->txCredits = GET_TX_BUFFER_SIZE(byte3);
2156 edge_port->maxTxCredits = edge_port->txCredits;
2157 dbg("%s - Port %u Open Response Inital MSR = %02x TxBufferSize = %d", __FUNCTION__, edge_serial->rxPort, byte2, edge_port->txCredits);
2158 handle_new_msr (edge_port, byte2);
2160 /* send the current line settings to the port so we are in sync with any further termios calls */
2161 if (edge_port->port->tty)
2162 change_port_settings (edge_port, edge_port->port->tty->termios);
2164 /* we have completed the open */
2165 edge_port->openPending = FALSE;
2166 edge_port->open = TRUE;
2167 wake_up_interruptible(&edge_port->wait_open);
2168 return;
2171 // If port is closed, silently discard all rcvd status. We can
2172 // have cases where buffered status is received AFTER the close
2173 // port command is sent to the Edgeport.
2174 if ((!edge_port->open ) || (edge_port->closePending)) {
2175 return;
2178 switch (code) {
2179 // Not currently sent by Edgeport
2180 case IOSP_STATUS_LSR:
2181 dbg("%s - Port %u LSR Status = %02x", __FUNCTION__, edge_serial->rxPort, byte2);
2182 handle_new_lsr (edge_port, FALSE, byte2, 0);
2183 break;
2185 case IOSP_STATUS_LSR_DATA:
2186 dbg("%s - Port %u LSR Status = %02x, Data = %02x", __FUNCTION__, edge_serial->rxPort, byte2, byte3);
2187 // byte2 is LSR Register
2188 // byte3 is broken data byte
2189 handle_new_lsr (edge_port, TRUE, byte2, byte3);
2190 break;
2192 // case IOSP_EXT_4_STATUS:
2193 // dbg("%s - Port %u LSR Status = %02x Data = %02x", __FUNCTION__, edge_serial->rxPort, byte2, byte3);
2194 // break;
2196 case IOSP_STATUS_MSR:
2197 dbg("%s - Port %u MSR Status = %02x", __FUNCTION__, edge_serial->rxPort, byte2);
2199 // Process this new modem status and generate appropriate
2200 // events, etc, based on the new status. This routine
2201 // also saves the MSR in Port->ShadowMsr.
2202 handle_new_msr(edge_port, byte2);
2203 break;
2205 default:
2206 dbg("%s - Unrecognized IOSP status code %u\n", __FUNCTION__, code);
2207 break;
2210 return;
2214 /*****************************************************************************
2215 * handle_new_msr
2216 * this function handles any change to the msr register for a port.
2217 *****************************************************************************/
2218 static void handle_new_msr(struct edgeport_port *edge_port, __u8 newMsr)
2220 struct async_icount *icount;
2222 dbg("%s %02x", __FUNCTION__, newMsr);
2224 if (newMsr & (MSR_DELTA_CTS | MSR_DELTA_DSR | MSR_DELTA_RI | MSR_DELTA_CD)) {
2225 icount = &edge_port->icount;
2227 /* update input line counters */
2228 if (newMsr & MSR_DELTA_CTS) {
2229 icount->cts++;
2231 if (newMsr & MSR_DELTA_DSR) {
2232 icount->dsr++;
2234 if (newMsr & MSR_DELTA_CD) {
2235 icount->dcd++;
2237 if (newMsr & MSR_DELTA_RI) {
2238 icount->rng++;
2240 wake_up_interruptible(&edge_port->delta_msr_wait);
2243 /* Save the new modem status */
2244 edge_port->shadowMSR = newMsr & 0xf0;
2246 return;
2250 /*****************************************************************************
2251 * handle_new_lsr
2252 * this function handles any change to the lsr register for a port.
2253 *****************************************************************************/
2254 static void handle_new_lsr(struct edgeport_port *edge_port, __u8 lsrData, __u8 lsr, __u8 data)
2256 __u8 newLsr = (__u8)(lsr & (__u8)(LSR_OVER_ERR | LSR_PAR_ERR | LSR_FRM_ERR | LSR_BREAK));
2257 struct async_icount *icount;
2259 dbg("%s - %02x", __FUNCTION__, newLsr);
2261 edge_port->shadowLSR = lsr;
2263 if (newLsr & LSR_BREAK) {
2265 // Parity and Framing errors only count if they
2266 // occur exclusive of a break being
2267 // received.
2269 newLsr &= (__u8)(LSR_OVER_ERR | LSR_BREAK);
2272 /* Place LSR data byte into Rx buffer */
2273 if (lsrData && edge_port->port->tty) {
2274 tty_insert_flip_char(edge_port->port->tty, data, 0);
2275 tty_flip_buffer_push(edge_port->port->tty);
2278 /* update input line counters */
2279 icount = &edge_port->icount;
2280 if (newLsr & LSR_BREAK) {
2281 icount->brk++;
2283 if (newLsr & LSR_OVER_ERR) {
2284 icount->overrun++;
2286 if (newLsr & LSR_PAR_ERR) {
2287 icount->parity++;
2289 if (newLsr & LSR_FRM_ERR) {
2290 icount->frame++;
2293 return;
2297 /****************************************************************************
2298 * sram_write
2299 * writes a number of bytes to the Edgeport device's sram starting at the
2300 * given address.
2301 * If successful returns the number of bytes written, otherwise it returns
2302 * a negative error number of the problem.
2303 ****************************************************************************/
2304 static int sram_write (struct usb_serial *serial, __u16 extAddr, __u16 addr, __u16 length, __u8 *data)
2306 int result;
2307 __u16 current_length;
2308 unsigned char *transfer_buffer;
2310 dbg("%s - %x, %x, %d", __FUNCTION__, extAddr, addr, length);
2312 transfer_buffer = kmalloc (64, GFP_KERNEL);
2313 if (!transfer_buffer) {
2314 dev_err(&serial->dev->dev, "%s - kmalloc(%d) failed.\n", __FUNCTION__, 64);
2315 return -ENOMEM;
2318 /* need to split these writes up into 64 byte chunks */
2319 result = 0;
2320 while (length > 0) {
2321 if (length > 64) {
2322 current_length = 64;
2323 } else {
2324 current_length = length;
2326 // dbg("%s - writing %x, %x, %d", __FUNCTION__, extAddr, addr, current_length);
2327 memcpy (transfer_buffer, data, current_length);
2328 result = usb_control_msg (serial->dev, usb_sndctrlpipe(serial->dev, 0), USB_REQUEST_ION_WRITE_RAM,
2329 0x40, addr, extAddr, transfer_buffer, current_length, 300);
2330 if (result < 0)
2331 break;
2332 length -= current_length;
2333 addr += current_length;
2334 data += current_length;
2337 kfree (transfer_buffer);
2338 return result;
2342 /****************************************************************************
2343 * rom_write
2344 * writes a number of bytes to the Edgeport device's ROM starting at the
2345 * given address.
2346 * If successful returns the number of bytes written, otherwise it returns
2347 * a negative error number of the problem.
2348 ****************************************************************************/
2349 static int rom_write (struct usb_serial *serial, __u16 extAddr, __u16 addr, __u16 length, __u8 *data)
2351 int result;
2352 __u16 current_length;
2353 unsigned char *transfer_buffer;
2355 // dbg("%s - %x, %x, %d", __FUNCTION__, extAddr, addr, length);
2357 transfer_buffer = kmalloc (64, GFP_KERNEL);
2358 if (!transfer_buffer) {
2359 dev_err(&serial->dev->dev, "%s - kmalloc(%d) failed.\n", __FUNCTION__, 64);
2360 return -ENOMEM;
2363 /* need to split these writes up into 64 byte chunks */
2364 result = 0;
2365 while (length > 0) {
2366 if (length > 64) {
2367 current_length = 64;
2368 } else {
2369 current_length = length;
2371 // dbg("%s - writing %x, %x, %d", __FUNCTION__, extAddr, addr, current_length);
2372 memcpy (transfer_buffer, data, current_length);
2373 result = usb_control_msg (serial->dev, usb_sndctrlpipe(serial->dev, 0), USB_REQUEST_ION_WRITE_ROM,
2374 0x40, addr, extAddr, transfer_buffer, current_length, 300);
2375 if (result < 0)
2376 break;
2377 length -= current_length;
2378 addr += current_length;
2379 data += current_length;
2382 kfree (transfer_buffer);
2383 return result;
2387 /****************************************************************************
2388 * rom_read
2389 * reads a number of bytes from the Edgeport device starting at the given
2390 * address.
2391 * If successful returns the number of bytes read, otherwise it returns
2392 * a negative error number of the problem.
2393 ****************************************************************************/
2394 static int rom_read (struct usb_serial *serial, __u16 extAddr, __u16 addr, __u16 length, __u8 *data)
2396 int result;
2397 __u16 current_length;
2398 unsigned char *transfer_buffer;
2400 dbg("%s - %x, %x, %d", __FUNCTION__, extAddr, addr, length);
2402 transfer_buffer = kmalloc (64, GFP_KERNEL);
2403 if (!transfer_buffer) {
2404 dev_err(&serial->dev->dev, "%s - kmalloc(%d) failed.\n", __FUNCTION__, 64);
2405 return -ENOMEM;
2408 /* need to split these reads up into 64 byte chunks */
2409 result = 0;
2410 while (length > 0) {
2411 if (length > 64) {
2412 current_length = 64;
2413 } else {
2414 current_length = length;
2416 // dbg("%s - %x, %x, %d", __FUNCTION__, extAddr, addr, current_length);
2417 result = usb_control_msg (serial->dev, usb_rcvctrlpipe(serial->dev, 0), USB_REQUEST_ION_READ_ROM,
2418 0xC0, addr, extAddr, transfer_buffer, current_length, 300);
2419 if (result < 0)
2420 break;
2421 memcpy (data, transfer_buffer, current_length);
2422 length -= current_length;
2423 addr += current_length;
2424 data += current_length;
2427 kfree (transfer_buffer);
2428 return result;
2432 /****************************************************************************
2433 * send_iosp_ext_cmd
2434 * Is used to send a IOSP message to the Edgeport device
2435 ****************************************************************************/
2436 static int send_iosp_ext_cmd (struct edgeport_port *edge_port, __u8 command, __u8 param)
2438 unsigned char *buffer;
2439 unsigned char *currentCommand;
2440 int length = 0;
2441 int status = 0;
2443 dbg("%s - %d, %d", __FUNCTION__, command, param);
2445 buffer = kmalloc (10, GFP_ATOMIC);
2446 if (!buffer) {
2447 dev_err(&edge_port->port->dev, "%s - kmalloc(%d) failed.\n", __FUNCTION__, 10);
2448 return -ENOMEM;
2451 currentCommand = buffer;
2453 MAKE_CMD_EXT_CMD (&currentCommand, &length,
2454 edge_port->port->number - edge_port->port->serial->minor,
2455 command, param);
2457 status = write_cmd_usb (edge_port, buffer, length);
2458 if (status) {
2459 /* something bad happened, let's free up the memory */
2460 kfree(buffer);
2463 return status;
2467 /*****************************************************************************
2468 * write_cmd_usb
2469 * this function writes the given buffer out to the bulk write endpoint.
2470 *****************************************************************************/
2471 static int write_cmd_usb (struct edgeport_port *edge_port, unsigned char *buffer, int length)
2473 struct edgeport_serial *edge_serial = usb_get_serial_data(edge_port->port->serial);
2474 int status = 0;
2475 struct urb *urb;
2476 int timeout;
2478 usb_serial_debug_data (__FILE__, __FUNCTION__, length, buffer);
2480 /* Allocate our next urb */
2481 urb = usb_alloc_urb (0, GFP_ATOMIC);
2482 if (!urb)
2483 return -ENOMEM;
2485 CmdUrbs++;
2486 dbg("%s - ALLOCATE URB %p (outstanding %d)", __FUNCTION__, urb, CmdUrbs);
2488 usb_fill_bulk_urb (urb, edge_serial->serial->dev,
2489 usb_sndbulkpipe(edge_serial->serial->dev, edge_serial->bulk_out_endpoint),
2490 buffer, length, edge_bulk_out_cmd_callback, edge_port);
2492 edge_port->commandPending = TRUE;
2493 status = usb_submit_urb(urb, GFP_ATOMIC);
2495 if (status) {
2496 /* something went wrong */
2497 dbg("%s - usb_submit_urb(write bulk) failed", __FUNCTION__);
2498 usb_unlink_urb (urb);
2499 usb_free_urb (urb);
2500 return status;
2503 // wait for command to finish
2504 timeout = COMMAND_TIMEOUT;
2505 #if 0
2506 while (timeout && edge_port->commandPending == TRUE) {
2507 timeout = interruptible_sleep_on_timeout (&edge_port->wait_command, timeout);
2510 if (edge_port->commandPending == TRUE) {
2511 /* command timed out */
2512 dbg("%s - command timed out", __FUNCTION__);
2513 status = -EINVAL;
2515 #endif
2516 return status;
2520 /*****************************************************************************
2521 * send_cmd_write_baud_rate
2522 * this function sends the proper command to change the baud rate of the
2523 * specified port.
2524 *****************************************************************************/
2525 static int send_cmd_write_baud_rate (struct edgeport_port *edge_port, int baudRate)
2527 unsigned char *cmdBuffer;
2528 unsigned char *currCmd;
2529 int cmdLen = 0;
2530 int divisor;
2531 int status;
2532 unsigned char number = edge_port->port->number - edge_port->port->serial->minor;
2534 dbg("%s - port = %d, baud = %d", __FUNCTION__, edge_port->port->number, baudRate);
2536 status = calc_baud_rate_divisor (baudRate, &divisor);
2537 if (status) {
2538 dev_err(&edge_port->port->dev, "%s - bad baud rate\n", __FUNCTION__);
2539 return status;
2542 // Alloc memory for the string of commands.
2543 cmdBuffer = kmalloc (0x100, GFP_ATOMIC);
2544 if (!cmdBuffer) {
2545 dev_err(&edge_port->port->dev, "%s - kmalloc(%d) failed.\n", __FUNCTION__, 0x100);
2546 return -ENOMEM;
2548 currCmd = cmdBuffer;
2550 // Enable access to divisor latch
2551 MAKE_CMD_WRITE_REG( &currCmd, &cmdLen, number, LCR, LCR_DL_ENABLE );
2553 // Write the divisor itself
2554 MAKE_CMD_WRITE_REG( &currCmd, &cmdLen, number, DLL, LOW8 (divisor) );
2555 MAKE_CMD_WRITE_REG( &currCmd, &cmdLen, number, DLM, HIGH8(divisor) );
2557 // Restore original value to disable access to divisor latch
2558 MAKE_CMD_WRITE_REG( &currCmd, &cmdLen, number, LCR, edge_port->shadowLCR);
2560 status = write_cmd_usb(edge_port, cmdBuffer, cmdLen );
2561 if (status) {
2562 /* something bad happened, let's free up the memory */
2563 kfree (cmdBuffer);
2566 return status;
2570 /*****************************************************************************
2571 * calc_baud_rate_divisor
2572 * this function calculates the proper baud rate divisor for the specified
2573 * baud rate.
2574 *****************************************************************************/
2575 static int calc_baud_rate_divisor (int baudrate, int *divisor)
2577 int i;
2578 __u16 custom;
2579 __u16 round1;
2580 __u16 round;
2583 dbg("%s - %d", __FUNCTION__, baudrate);
2585 for (i = 0; i < NUM_ENTRIES(divisor_table); i++) {
2586 if ( divisor_table[i].BaudRate == baudrate ) {
2587 *divisor = divisor_table[i].Divisor;
2588 return 0;
2592 // We have tried all of the standard baud rates
2593 // lets try to calculate the divisor for this baud rate
2594 // Make sure the baud rate is reasonable
2595 if (baudrate < 230400) {
2596 // get divisor
2597 custom = (__u16)(230400L / baudrate);
2599 // Check for round off
2600 round1 = (__u16)(2304000L / baudrate);
2601 round = (__u16)(round1 - (custom * 10));
2602 if (round > 4) {
2603 custom++;
2605 *divisor = custom;
2607 dbg("%s - Baud %d = %d\n", __FUNCTION__, baudrate, custom);
2608 return 0;
2611 return -1;
2615 /*****************************************************************************
2616 * send_cmd_write_uart_register
2617 * this function builds up a uart register message and sends to to the device.
2618 *****************************************************************************/
2619 static int send_cmd_write_uart_register (struct edgeport_port *edge_port, __u8 regNum, __u8 regValue)
2621 unsigned char *cmdBuffer;
2622 unsigned char *currCmd;
2623 unsigned long cmdLen = 0;
2624 int status;
2626 dbg("%s - write to %s register 0x%02x", (regNum == MCR) ? "MCR" : "LCR", __FUNCTION__, regValue);
2628 // Alloc memory for the string of commands.
2629 cmdBuffer = kmalloc (0x10, GFP_ATOMIC);
2630 if (cmdBuffer == NULL ) {
2631 return -ENOMEM;
2634 currCmd = cmdBuffer;
2636 // Build a cmd in the buffer to write the given register
2637 MAKE_CMD_WRITE_REG (&currCmd, &cmdLen,
2638 edge_port->port->number - edge_port->port->serial->minor,
2639 regNum, regValue);
2641 status = write_cmd_usb(edge_port, cmdBuffer, cmdLen);
2642 if (status) {
2643 /* something bad happened, let's free up the memory */
2644 kfree (cmdBuffer);
2647 return status;
2651 /*****************************************************************************
2652 * change_port_settings
2653 * This routine is called to set the UART on the device to match the specified
2654 * new settings.
2655 *****************************************************************************/
2656 static void change_port_settings (struct edgeport_port *edge_port, struct termios *old_termios)
2658 struct tty_struct *tty;
2659 int baud;
2660 unsigned cflag;
2661 __u8 mask = 0xff;
2662 __u8 lData;
2663 __u8 lParity;
2664 __u8 lStop;
2665 __u8 rxFlow;
2666 __u8 txFlow;
2667 int status;
2669 dbg("%s - port %d", __FUNCTION__, edge_port->port->number);
2671 if ((!edge_port->open) &&
2672 (!edge_port->openPending)) {
2673 dbg("%s - port not opened", __FUNCTION__);
2674 return;
2677 tty = edge_port->port->tty;
2678 if ((!tty) ||
2679 (!tty->termios)) {
2680 dbg("%s - no tty structures", __FUNCTION__);
2681 return;
2684 cflag = tty->termios->c_cflag;
2686 switch (cflag & CSIZE) {
2687 case CS5: lData = LCR_BITS_5; mask = 0x1f; dbg("%s - data bits = 5", __FUNCTION__); break;
2688 case CS6: lData = LCR_BITS_6; mask = 0x3f; dbg("%s - data bits = 6", __FUNCTION__); break;
2689 case CS7: lData = LCR_BITS_7; mask = 0x7f; dbg("%s - data bits = 7", __FUNCTION__); break;
2690 default:
2691 case CS8: lData = LCR_BITS_8; dbg("%s - data bits = 8", __FUNCTION__); break;
2694 lParity = LCR_PAR_NONE;
2695 if (cflag & PARENB) {
2696 if (cflag & PARODD) {
2697 lParity = LCR_PAR_ODD;
2698 dbg("%s - parity = odd", __FUNCTION__);
2699 } else {
2700 lParity = LCR_PAR_EVEN;
2701 dbg("%s - parity = even", __FUNCTION__);
2703 } else {
2704 dbg("%s - parity = none", __FUNCTION__);
2707 if (cflag & CSTOPB) {
2708 lStop = LCR_STOP_2;
2709 dbg("%s - stop bits = 2", __FUNCTION__);
2710 } else {
2711 lStop = LCR_STOP_1;
2712 dbg("%s - stop bits = 1", __FUNCTION__);
2715 /* figure out the flow control settings */
2716 rxFlow = txFlow = 0x00;
2717 if (cflag & CRTSCTS) {
2718 rxFlow |= IOSP_RX_FLOW_RTS;
2719 txFlow |= IOSP_TX_FLOW_CTS;
2720 dbg("%s - RTS/CTS is enabled", __FUNCTION__);
2721 } else {
2722 dbg("%s - RTS/CTS is disabled", __FUNCTION__);
2725 /* if we are implementing XON/XOFF, set the start and stop character in the device */
2726 if (I_IXOFF(tty) || I_IXON(tty)) {
2727 unsigned char stop_char = STOP_CHAR(tty);
2728 unsigned char start_char = START_CHAR(tty);
2730 send_iosp_ext_cmd (edge_port, IOSP_CMD_SET_XON_CHAR, start_char);
2731 send_iosp_ext_cmd (edge_port, IOSP_CMD_SET_XOFF_CHAR, stop_char);
2733 /* if we are implementing INBOUND XON/XOFF */
2734 if (I_IXOFF(tty)) {
2735 rxFlow |= IOSP_RX_FLOW_XON_XOFF;
2736 dbg("%s - INBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x", __FUNCTION__, start_char, stop_char);
2737 } else {
2738 dbg("%s - INBOUND XON/XOFF is disabled", __FUNCTION__);
2741 /* if we are implementing OUTBOUND XON/XOFF */
2742 if (I_IXON(tty)) {
2743 txFlow |= IOSP_TX_FLOW_XON_XOFF;
2744 dbg("%s - OUTBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x", __FUNCTION__, start_char, stop_char);
2745 } else {
2746 dbg("%s - OUTBOUND XON/XOFF is disabled", __FUNCTION__);
2750 /* Set flow control to the configured value */
2751 send_iosp_ext_cmd (edge_port, IOSP_CMD_SET_RX_FLOW, rxFlow);
2752 send_iosp_ext_cmd (edge_port, IOSP_CMD_SET_TX_FLOW, txFlow);
2755 edge_port->shadowLCR &= ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
2756 edge_port->shadowLCR |= (lData | lParity | lStop);
2758 edge_port->validDataMask = mask;
2760 /* Send the updated LCR value to the EdgePort */
2761 status = send_cmd_write_uart_register(edge_port, LCR, edge_port->shadowLCR);
2762 if (status != 0) {
2763 return;
2766 /* set up the MCR register and send it to the EdgePort */
2767 edge_port->shadowMCR = MCR_MASTER_IE;
2768 if (cflag & CBAUD) {
2769 edge_port->shadowMCR |= (MCR_DTR | MCR_RTS);
2771 status = send_cmd_write_uart_register(edge_port, MCR, edge_port->shadowMCR);
2772 if (status != 0) {
2773 return;
2776 /* Determine divisor based on baud rate */
2777 baud = tty_get_baud_rate(tty);
2778 if (!baud) {
2779 /* pick a default, any default... */
2780 baud = 9600;
2783 dbg("%s - baud rate = %d", __FUNCTION__, baud);
2784 status = send_cmd_write_baud_rate (edge_port, baud);
2786 return;
2790 /****************************************************************************
2791 * unicode_to_ascii
2792 * Turns a string from Unicode into ASCII.
2793 * Doesn't do a good job with any characters that are outside the normal
2794 * ASCII range, but it's only for debugging...
2795 * NOTE: expects the unicode in LE format
2796 ****************************************************************************/
2797 static void unicode_to_ascii (char *string, short *unicode, int unicode_size)
2799 int i;
2800 for (i = 0; i < unicode_size; ++i) {
2801 string[i] = (char)(le16_to_cpu(unicode[i]));
2803 string[unicode_size] = 0x00;
2807 /****************************************************************************
2808 * get_manufacturing_desc
2809 * reads in the manufacturing descriptor and stores it into the serial
2810 * structure.
2811 ****************************************************************************/
2812 static void get_manufacturing_desc (struct edgeport_serial *edge_serial)
2814 int response;
2816 dbg("getting manufacturer descriptor");
2818 response = rom_read (edge_serial->serial, (EDGE_MANUF_DESC_ADDR & 0xffff0000) >> 16,
2819 (__u16)(EDGE_MANUF_DESC_ADDR & 0x0000ffff), EDGE_MANUF_DESC_LEN,
2820 (__u8 *)(&edge_serial->manuf_descriptor));
2822 if (response < 1) {
2823 dev_err(&edge_serial->serial->dev->dev, "error in getting manufacturer descriptor\n");
2824 } else {
2825 char string[30];
2826 dbg("**Manufacturer Descriptor");
2827 dbg(" RomSize: %dK", edge_serial->manuf_descriptor.RomSize);
2828 dbg(" RamSize: %dK", edge_serial->manuf_descriptor.RamSize);
2829 dbg(" CpuRev: %d", edge_serial->manuf_descriptor.CpuRev);
2830 dbg(" BoardRev: %d", edge_serial->manuf_descriptor.BoardRev);
2831 dbg(" NumPorts: %d", edge_serial->manuf_descriptor.NumPorts);
2832 dbg(" DescDate: %d/%d/%d", edge_serial->manuf_descriptor.DescDate[0], edge_serial->manuf_descriptor.DescDate[1], edge_serial->manuf_descriptor.DescDate[2]+1900);
2833 unicode_to_ascii (string, edge_serial->manuf_descriptor.SerialNumber, edge_serial->manuf_descriptor.SerNumLength/2-1);
2834 dbg(" SerialNumber: %s", string);
2835 unicode_to_ascii (string, edge_serial->manuf_descriptor.AssemblyNumber, edge_serial->manuf_descriptor.AssemblyNumLength/2-1);
2836 dbg(" AssemblyNumber: %s", string);
2837 unicode_to_ascii (string, edge_serial->manuf_descriptor.OemAssyNumber, edge_serial->manuf_descriptor.OemAssyNumLength/2-1);
2838 dbg(" OemAssyNumber: %s", string);
2839 dbg(" UartType: %d", edge_serial->manuf_descriptor.UartType);
2840 dbg(" IonPid: %d", edge_serial->manuf_descriptor.IonPid);
2841 dbg(" IonConfig: %d", edge_serial->manuf_descriptor.IonConfig);
2846 /****************************************************************************
2847 * get_boot_desc
2848 * reads in the bootloader descriptor and stores it into the serial
2849 * structure.
2850 ****************************************************************************/
2851 static void get_boot_desc (struct edgeport_serial *edge_serial)
2853 int response;
2855 dbg("getting boot descriptor");
2857 response = rom_read (edge_serial->serial, (EDGE_BOOT_DESC_ADDR & 0xffff0000) >> 16,
2858 (__u16)(EDGE_BOOT_DESC_ADDR & 0x0000ffff), EDGE_BOOT_DESC_LEN,
2859 (__u8 *)(&edge_serial->boot_descriptor));
2861 if (response < 1) {
2862 dev_err(&edge_serial->serial->dev->dev, "error in getting boot descriptor\n");
2863 } else {
2864 dbg("**Boot Descriptor:");
2865 dbg(" BootCodeLength: %d", le16_to_cpu(edge_serial->boot_descriptor.BootCodeLength));
2866 dbg(" MajorVersion: %d", edge_serial->boot_descriptor.MajorVersion);
2867 dbg(" MinorVersion: %d", edge_serial->boot_descriptor.MinorVersion);
2868 dbg(" BuildNumber: %d", le16_to_cpu(edge_serial->boot_descriptor.BuildNumber));
2869 dbg(" Capabilities: 0x%x", le16_to_cpu(edge_serial->boot_descriptor.Capabilities));
2870 dbg(" UConfig0: %d", edge_serial->boot_descriptor.UConfig0);
2871 dbg(" UConfig1: %d", edge_serial->boot_descriptor.UConfig1);
2876 /****************************************************************************
2877 * load_application_firmware
2878 * This is called to load the application firmware to the device
2879 ****************************************************************************/
2880 static void load_application_firmware (struct edgeport_serial *edge_serial)
2882 struct edge_firmware_image_record *record;
2883 unsigned char *firmware;
2884 unsigned char *FirmwareImage;
2885 int ImageSize;
2886 int response;
2889 switch (edge_serial->product_info.iDownloadFile) {
2890 case EDGE_DOWNLOAD_FILE_I930:
2891 dbg("downloading firmware version (930) %d.%d.%d",
2892 OperationalCodeImageVersion_GEN1.MajorVersion,
2893 OperationalCodeImageVersion_GEN1.MinorVersion,
2894 OperationalCodeImageVersion_GEN1.BuildNumber);
2895 firmware = &OperationalCodeImage_GEN1[0];
2896 FirmwareImage = &OperationalCodeImage_GEN1[0];
2897 ImageSize = sizeof(OperationalCodeImage_GEN1);
2898 break;
2900 case EDGE_DOWNLOAD_FILE_80251:
2901 dbg("downloading firmware version (80251) %d.%d.%d",
2902 OperationalCodeImageVersion_GEN2.MajorVersion,
2903 OperationalCodeImageVersion_GEN2.MinorVersion,
2904 OperationalCodeImageVersion_GEN2.BuildNumber);
2905 firmware = &OperationalCodeImage_GEN2[0];
2906 FirmwareImage = &OperationalCodeImage_GEN2[0];
2907 ImageSize = sizeof(OperationalCodeImage_GEN2);
2908 break;
2910 case EDGE_DOWNLOAD_FILE_NONE:
2911 dbg ("No download file specified, skipping download\n");
2912 return;
2914 default:
2915 return;
2919 for (;;) {
2920 record = (struct edge_firmware_image_record *)firmware;
2921 response = sram_write (edge_serial->serial, le16_to_cpu(record->ExtAddr), le16_to_cpu(record->Addr), le16_to_cpu(record->Len), &record->Data[0]);
2922 if (response < 0) {
2923 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);
2924 break;
2926 firmware += sizeof (struct edge_firmware_image_record) + le16_to_cpu(record->Len);
2927 if (firmware >= &FirmwareImage[ImageSize]) {
2928 break;
2932 dbg("sending exec_dl_code");
2933 response = usb_control_msg (edge_serial->serial->dev,
2934 usb_sndctrlpipe(edge_serial->serial->dev, 0),
2935 USB_REQUEST_ION_EXEC_DL_CODE,
2936 0x40, 0x4000, 0x0001, NULL, 0, 3000);
2938 return;
2944 /****************************************************************************
2945 * edge_startup
2946 ****************************************************************************/
2947 static int edge_startup (struct usb_serial *serial)
2949 struct edgeport_serial *edge_serial;
2950 struct edgeport_port *edge_port;
2951 struct usb_device *dev;
2952 int i;
2954 dev = serial->dev;
2956 /* create our private serial structure */
2957 edge_serial = kmalloc (sizeof(struct edgeport_serial), GFP_KERNEL);
2958 if (edge_serial == NULL) {
2959 dev_err(&serial->dev->dev, "%s - Out of memory", __FUNCTION__);
2960 return -ENOMEM;
2962 memset (edge_serial, 0, sizeof(struct edgeport_serial));
2963 edge_serial->serial = serial;
2964 usb_set_serial_data(serial, edge_serial);
2966 /* get the name for the device from the device */
2967 if ( (i = get_string(dev, dev->descriptor.iManufacturer, &edge_serial->name[0])) != 0) {
2968 edge_serial->name[i-1] = ' ';
2971 get_string(dev, dev->descriptor.iProduct, &edge_serial->name[i]);
2973 dev_info(&serial->dev->dev, "%s detected\n", edge_serial->name);
2975 /* get the manufacturing descriptor for this device */
2976 get_manufacturing_desc (edge_serial);
2978 /* get the boot descriptor */
2979 get_boot_desc (edge_serial);
2981 get_product_info(edge_serial);
2983 /* set the number of ports from the manufacturing description */
2984 /* serial->num_ports = serial->product_info.NumPorts; */
2985 if (edge_serial->product_info.NumPorts != serial->num_ports) {
2986 warn("%s - Device Reported %d serial ports vs core "
2987 "thinking we have %d ports, email greg@kroah.com this info.",
2988 __FUNCTION__, edge_serial->product_info.NumPorts,
2989 serial->num_ports);
2992 dbg("%s - time 1 %ld", __FUNCTION__, jiffies);
2994 /* now load the application firmware into this device */
2995 load_application_firmware (edge_serial);
2997 dbg("%s - time 2 %ld", __FUNCTION__, jiffies);
2999 /* Check current Edgeport EEPROM and update if necessary */
3000 update_edgeport_E2PROM (edge_serial);
3002 dbg("%s - time 3 %ld", __FUNCTION__, jiffies);
3004 /* set the configuration to use #1 */
3005 // dbg("set_configuration 1");
3006 // usb_set_configuration (dev, 1);
3008 /* we set up the pointers to the endpoints in the edge_open function,
3009 * as the structures aren't created yet. */
3011 /* set up our port private structures */
3012 for (i = 0; i < serial->num_ports; ++i) {
3013 edge_port = kmalloc (sizeof(struct edgeport_port), GFP_KERNEL);
3014 if (edge_port == NULL) {
3015 dev_err(&serial->dev->dev, "%s - Out of memory", __FUNCTION__);
3016 usb_set_serial_data(serial, NULL);
3017 kfree(edge_serial);
3018 return -ENOMEM;
3020 memset (edge_port, 0, sizeof(struct edgeport_port));
3021 edge_port->port = &serial->port[i];
3022 usb_set_serial_port_data(&serial->port[i], edge_port);
3025 return 0;
3030 /****************************************************************************
3031 * edge_shutdown
3032 * This function is called whenever the device is removed from the usb bus.
3033 ****************************************************************************/
3034 static void edge_shutdown (struct usb_serial *serial)
3036 int i;
3038 dbg("%s", __FUNCTION__);
3040 /* stop reads and writes on all ports */
3041 for (i=0; i < serial->num_ports; ++i) {
3042 kfree (usb_get_serial_port_data(&serial->port[i]));
3043 usb_set_serial_port_data(&serial->port[i], NULL);
3045 kfree (usb_get_serial_data(serial));
3046 usb_set_serial_data(serial, NULL);
3050 /****************************************************************************
3051 * edgeport_init
3052 * This is called by the module subsystem, or on startup to initialize us
3053 ****************************************************************************/
3054 int __init edgeport_init(void)
3056 usb_serial_register (&edgeport_1port_device);
3057 usb_serial_register (&edgeport_2port_device);
3058 usb_serial_register (&edgeport_4port_device);
3059 usb_serial_register (&edgeport_8port_device);
3060 usb_register (&io_driver);
3061 info(DRIVER_DESC " " DRIVER_VERSION);
3062 return 0;
3067 /****************************************************************************
3068 * edgeport_exit
3069 * Called when the driver is about to be unloaded.
3070 ****************************************************************************/
3071 void __exit edgeport_exit (void)
3073 usb_deregister (&io_driver);
3074 usb_serial_deregister (&edgeport_1port_device);
3075 usb_serial_deregister (&edgeport_2port_device);
3076 usb_serial_deregister (&edgeport_4port_device);
3077 usb_serial_deregister (&edgeport_8port_device);
3080 module_init(edgeport_init);
3081 module_exit(edgeport_exit);
3083 /* Module information */
3084 MODULE_AUTHOR( DRIVER_AUTHOR );
3085 MODULE_DESCRIPTION( DRIVER_DESC );
3086 MODULE_LICENSE("GPL");
3088 MODULE_PARM(debug, "i");
3089 MODULE_PARM_DESC(debug, "Debug enabled or not");