USB: serial: io_edgeport: clean up urb->status usage
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / usb / serial / io_edgeport.c
blobcebb32f4a8319bda9bbd4c94a865b8cfb111ddd7
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 * For questions or problems with this driver, contact Inside Out
27 * Networks technical support, or Peter Berger <pberger@brimson.com>,
28 * or Al Borchers <alborchers@steinerpoint.com>.
32 #include <linux/kernel.h>
33 #include <linux/jiffies.h>
34 #include <linux/errno.h>
35 #include <linux/init.h>
36 #include <linux/slab.h>
37 #include <linux/tty.h>
38 #include <linux/tty_driver.h>
39 #include <linux/tty_flip.h>
40 #include <linux/module.h>
41 #include <linux/spinlock.h>
42 #include <linux/serial.h>
43 #include <linux/ioctl.h>
44 #include <linux/wait.h>
45 #include <asm/uaccess.h>
46 #include <linux/usb.h>
47 #include <linux/usb/serial.h>
48 #include "io_edgeport.h"
49 #include "io_ionsp.h" /* info for the iosp messages */
50 #include "io_16654.h" /* 16654 UART defines */
53 * Version Information
55 #define DRIVER_VERSION "v2.7"
56 #define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com> and David Iacovelli"
57 #define DRIVER_DESC "Edgeport USB Serial Driver"
59 /* First, the latest boot code - for first generation edgeports */
60 #define IMAGE_ARRAY_NAME BootCodeImage_GEN1
61 #define IMAGE_VERSION_NAME BootCodeImageVersion_GEN1
62 #include "io_fw_boot.h" /* the bootloader firmware to download to a device, if it needs it */
64 /* for second generation edgeports */
65 #define IMAGE_ARRAY_NAME BootCodeImage_GEN2
66 #define IMAGE_VERSION_NAME BootCodeImageVersion_GEN2
67 #include "io_fw_boot2.h" /* the bootloader firmware to download to a device, if it needs it */
69 /* Then finally the main run-time operational code - for first generation edgeports */
70 #define IMAGE_ARRAY_NAME OperationalCodeImage_GEN1
71 #define IMAGE_VERSION_NAME OperationalCodeImageVersion_GEN1
72 #include "io_fw_down.h" /* Define array OperationalCodeImage[] */
74 /* for second generation edgeports */
75 #define IMAGE_ARRAY_NAME OperationalCodeImage_GEN2
76 #define IMAGE_VERSION_NAME OperationalCodeImageVersion_GEN2
77 #include "io_fw_down2.h" /* Define array OperationalCodeImage[] */
79 #define MAX_NAME_LEN 64
81 #define CHASE_TIMEOUT (5*HZ) /* 5 seconds */
82 #define OPEN_TIMEOUT (5*HZ) /* 5 seconds */
83 #define COMMAND_TIMEOUT (5*HZ) /* 5 seconds */
85 /* receive port state */
86 enum RXSTATE {
87 EXPECT_HDR1 = 0, /* Expect header byte 1 */
88 EXPECT_HDR2 = 1, /* Expect header byte 2 */
89 EXPECT_DATA = 2, /* Expect 'RxBytesRemaining' data */
90 EXPECT_HDR3 = 3, /* Expect header byte 3 (for status hdrs only) */
94 /* Transmit Fifo
95 * This Transmit queue is an extension of the edgeport Rx buffer.
96 * The maximum amount of data buffered in both the edgeport
97 * Rx buffer (maxTxCredits) and this buffer will never exceed maxTxCredits.
99 struct TxFifo {
100 unsigned int head; /* index to head pointer (write) */
101 unsigned int tail; /* index to tail pointer (read) */
102 unsigned int count; /* Bytes in queue */
103 unsigned int size; /* Max size of queue (equal to Max number of TxCredits) */
104 unsigned char *fifo; /* allocated Buffer */
107 /* This structure holds all of the local port information */
108 struct edgeport_port {
109 __u16 txCredits; /* our current credits for this port */
110 __u16 maxTxCredits; /* the max size of the port */
112 struct TxFifo txfifo; /* transmit fifo -- size will be maxTxCredits */
113 struct urb *write_urb; /* write URB for this port */
114 bool write_in_progress; /* 'true' while a write URB is outstanding */
115 spinlock_t ep_lock;
117 __u8 shadowLCR; /* last LCR value received */
118 __u8 shadowMCR; /* last MCR value received */
119 __u8 shadowMSR; /* last MSR value received */
120 __u8 shadowLSR; /* last LSR value received */
121 __u8 shadowXonChar; /* last value set as XON char in Edgeport */
122 __u8 shadowXoffChar; /* last value set as XOFF char in Edgeport */
123 __u8 validDataMask;
124 __u32 baudRate;
126 bool open;
127 bool openPending;
128 bool commandPending;
129 bool closePending;
130 bool chaseResponsePending;
132 wait_queue_head_t wait_chase; /* for handling sleeping while waiting for chase to finish */
133 wait_queue_head_t wait_open; /* for handling sleeping while waiting for open to finish */
134 wait_queue_head_t wait_command; /* for handling sleeping while waiting for command to finish */
135 wait_queue_head_t delta_msr_wait; /* for handling sleeping while waiting for msr change to happen */
137 struct async_icount icount;
138 struct usb_serial_port *port; /* loop back to the owner of this object */
142 /* This structure holds all of the individual device information */
143 struct edgeport_serial {
144 char name[MAX_NAME_LEN+2]; /* string name of this device */
146 struct edge_manuf_descriptor manuf_descriptor; /* the manufacturer descriptor */
147 struct edge_boot_descriptor boot_descriptor; /* the boot firmware descriptor */
148 struct edgeport_product_info product_info; /* Product Info */
149 struct edge_compatibility_descriptor epic_descriptor; /* Edgeport compatible descriptor */
150 int is_epic; /* flag if EPiC device or not */
152 __u8 interrupt_in_endpoint; /* the interrupt endpoint handle */
153 unsigned char * interrupt_in_buffer; /* the buffer we use for the interrupt endpoint */
154 struct urb * interrupt_read_urb; /* our interrupt urb */
156 __u8 bulk_in_endpoint; /* the bulk in endpoint handle */
157 unsigned char * bulk_in_buffer; /* the buffer we use for the bulk in endpoint */
158 struct urb * read_urb; /* our bulk read urb */
159 bool read_in_progress;
160 spinlock_t es_lock;
162 __u8 bulk_out_endpoint; /* the bulk out endpoint handle */
164 __s16 rxBytesAvail; /* the number of bytes that we need to read from this device */
166 enum RXSTATE rxState; /* the current state of the bulk receive processor */
167 __u8 rxHeader1; /* receive header byte 1 */
168 __u8 rxHeader2; /* receive header byte 2 */
169 __u8 rxHeader3; /* receive header byte 3 */
170 __u8 rxPort; /* the port that we are currently receiving data for */
171 __u8 rxStatusCode; /* the receive status code */
172 __u8 rxStatusParam; /* the receive status paramater */
173 __s16 rxBytesRemaining; /* the number of port bytes left to read */
174 struct usb_serial *serial; /* loop back to the owner of this object */
177 /* baud rate information */
178 struct divisor_table_entry {
179 __u32 BaudRate;
180 __u16 Divisor;
184 // Define table of divisors for Rev A EdgePort/4 hardware
185 // These assume a 3.6864MHz crystal, the standard /16, and
186 // MCR.7 = 0.
188 static const struct divisor_table_entry divisor_table[] = {
189 { 50, 4608},
190 { 75, 3072},
191 { 110, 2095}, /* 2094.545455 => 230450 => .0217 % over */
192 { 134, 1713}, /* 1713.011152 => 230398.5 => .00065% under */
193 { 150, 1536},
194 { 300, 768},
195 { 600, 384},
196 { 1200, 192},
197 { 1800, 128},
198 { 2400, 96},
199 { 4800, 48},
200 { 7200, 32},
201 { 9600, 24},
202 { 14400, 16},
203 { 19200, 12},
204 { 38400, 6},
205 { 57600, 4},
206 { 115200, 2},
207 { 230400, 1},
210 /* local variables */
211 static int debug;
213 static int low_latency = 1; /* tty low latency flag, on by default */
215 static atomic_t CmdUrbs; /* Number of outstanding Command Write Urbs */
218 /* local function prototypes */
220 /* function prototypes for all URB callbacks */
221 static void edge_interrupt_callback (struct urb *urb);
222 static void edge_bulk_in_callback (struct urb *urb);
223 static void edge_bulk_out_data_callback (struct urb *urb);
224 static void edge_bulk_out_cmd_callback (struct urb *urb);
226 /* function prototypes for the usbserial callbacks */
227 static int edge_open (struct usb_serial_port *port, struct file *filp);
228 static void edge_close (struct usb_serial_port *port, struct file *filp);
229 static int edge_write (struct usb_serial_port *port, const unsigned char *buf, int count);
230 static int edge_write_room (struct usb_serial_port *port);
231 static int edge_chars_in_buffer (struct usb_serial_port *port);
232 static void edge_throttle (struct usb_serial_port *port);
233 static void edge_unthrottle (struct usb_serial_port *port);
234 static void edge_set_termios (struct usb_serial_port *port, struct ktermios *old_termios);
235 static int edge_ioctl (struct usb_serial_port *port, struct file *file, unsigned int cmd, unsigned long arg);
236 static void edge_break (struct usb_serial_port *port, int break_state);
237 static int edge_tiocmget (struct usb_serial_port *port, struct file *file);
238 static int edge_tiocmset (struct usb_serial_port *port, struct file *file, unsigned int set, unsigned int clear);
239 static int edge_startup (struct usb_serial *serial);
240 static void edge_shutdown (struct usb_serial *serial);
243 #include "io_tables.h" /* all of the devices that this driver supports */
245 /* function prototypes for all of our local functions */
246 static void process_rcvd_data (struct edgeport_serial *edge_serial, unsigned char *buffer, __u16 bufferLength);
247 static void process_rcvd_status (struct edgeport_serial *edge_serial, __u8 byte2, __u8 byte3);
248 static void edge_tty_recv (struct device *dev, struct tty_struct *tty, unsigned char *data, int length);
249 static void handle_new_msr (struct edgeport_port *edge_port, __u8 newMsr);
250 static void handle_new_lsr (struct edgeport_port *edge_port, __u8 lsrData, __u8 lsr, __u8 data);
251 static int send_iosp_ext_cmd (struct edgeport_port *edge_port, __u8 command, __u8 param);
252 static int calc_baud_rate_divisor (int baud_rate, int *divisor);
253 static int send_cmd_write_baud_rate (struct edgeport_port *edge_port, int baudRate);
254 static void change_port_settings (struct edgeport_port *edge_port, struct ktermios *old_termios);
255 static int send_cmd_write_uart_register (struct edgeport_port *edge_port, __u8 regNum, __u8 regValue);
256 static int write_cmd_usb (struct edgeport_port *edge_port, unsigned char *buffer, int writeLength);
257 static void send_more_port_data (struct edgeport_serial *edge_serial, struct edgeport_port *edge_port);
259 static int sram_write (struct usb_serial *serial, __u16 extAddr, __u16 addr, __u16 length, __u8 *data);
260 static int rom_read (struct usb_serial *serial, __u16 extAddr, __u16 addr, __u16 length, __u8 *data);
261 static int rom_write (struct usb_serial *serial, __u16 extAddr, __u16 addr, __u16 length, __u8 *data);
262 static void get_manufacturing_desc (struct edgeport_serial *edge_serial);
263 static void get_boot_desc (struct edgeport_serial *edge_serial);
264 static void load_application_firmware (struct edgeport_serial *edge_serial);
266 static void unicode_to_ascii(char *string, int buflen, __le16 *unicode, int unicode_size);
269 // ************************************************************************
270 // ************************************************************************
271 // ************************************************************************
272 // ************************************************************************
274 /************************************************************************
276 * update_edgeport_E2PROM() Compare current versions of *
277 * Boot ROM and Manufacture *
278 * Descriptors with versions *
279 * embedded in this driver *
281 ************************************************************************/
282 static void update_edgeport_E2PROM (struct edgeport_serial *edge_serial)
284 __u32 BootCurVer;
285 __u32 BootNewVer;
286 __u8 BootMajorVersion;
287 __u8 BootMinorVersion;
288 __le16 BootBuildNumber;
289 __u8 *BootImage;
290 __u32 BootSize;
291 struct edge_firmware_image_record *record;
292 unsigned char *firmware;
293 int response;
296 switch (edge_serial->product_info.iDownloadFile) {
297 case EDGE_DOWNLOAD_FILE_I930:
298 BootMajorVersion = BootCodeImageVersion_GEN1.MajorVersion;
299 BootMinorVersion = BootCodeImageVersion_GEN1.MinorVersion;
300 BootBuildNumber = cpu_to_le16(BootCodeImageVersion_GEN1.BuildNumber);
301 BootImage = &BootCodeImage_GEN1[0];
302 BootSize = sizeof( BootCodeImage_GEN1 );
303 break;
305 case EDGE_DOWNLOAD_FILE_80251:
306 BootMajorVersion = BootCodeImageVersion_GEN2.MajorVersion;
307 BootMinorVersion = BootCodeImageVersion_GEN2.MinorVersion;
308 BootBuildNumber = cpu_to_le16(BootCodeImageVersion_GEN2.BuildNumber);
309 BootImage = &BootCodeImage_GEN2[0];
310 BootSize = sizeof( BootCodeImage_GEN2 );
311 break;
313 default:
314 return;
317 // Check Boot Image Version
318 BootCurVer = (edge_serial->boot_descriptor.MajorVersion << 24) +
319 (edge_serial->boot_descriptor.MinorVersion << 16) +
320 le16_to_cpu(edge_serial->boot_descriptor.BuildNumber);
322 BootNewVer = (BootMajorVersion << 24) +
323 (BootMinorVersion << 16) +
324 le16_to_cpu(BootBuildNumber);
326 dbg("Current Boot Image version %d.%d.%d",
327 edge_serial->boot_descriptor.MajorVersion,
328 edge_serial->boot_descriptor.MinorVersion,
329 le16_to_cpu(edge_serial->boot_descriptor.BuildNumber));
332 if (BootNewVer > BootCurVer) {
333 dbg("**Update Boot Image from %d.%d.%d to %d.%d.%d",
334 edge_serial->boot_descriptor.MajorVersion,
335 edge_serial->boot_descriptor.MinorVersion,
336 le16_to_cpu(edge_serial->boot_descriptor.BuildNumber),
337 BootMajorVersion,
338 BootMinorVersion,
339 le16_to_cpu(BootBuildNumber));
342 dbg("Downloading new Boot Image");
344 firmware = BootImage;
346 for (;;) {
347 record = (struct edge_firmware_image_record *)firmware;
348 response = rom_write (edge_serial->serial, le16_to_cpu(record->ExtAddr), le16_to_cpu(record->Addr), le16_to_cpu(record->Len), &record->Data[0]);
349 if (response < 0) {
350 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));
351 break;
353 firmware += sizeof (struct edge_firmware_image_record) + le16_to_cpu(record->Len);
354 if (firmware >= &BootImage[BootSize]) {
355 break;
358 } else {
359 dbg("Boot Image -- already up to date");
364 /************************************************************************
366 * Get string descriptor from device *
368 ************************************************************************/
369 static int get_string (struct usb_device *dev, int Id, char *string, int buflen)
371 struct usb_string_descriptor StringDesc;
372 struct usb_string_descriptor *pStringDesc;
374 dbg("%s - USB String ID = %d", __FUNCTION__, Id );
376 if (!usb_get_descriptor(dev, USB_DT_STRING, Id, &StringDesc, sizeof(StringDesc))) {
377 return 0;
380 pStringDesc = kmalloc (StringDesc.bLength, GFP_KERNEL);
382 if (!pStringDesc) {
383 return 0;
386 if (!usb_get_descriptor(dev, USB_DT_STRING, Id, pStringDesc, StringDesc.bLength )) {
387 kfree(pStringDesc);
388 return 0;
391 unicode_to_ascii(string, buflen, pStringDesc->wData, pStringDesc->bLength/2);
393 kfree(pStringDesc);
394 dbg("%s - USB String %s", __FUNCTION__, string);
395 return strlen(string);
399 #if 0
400 /************************************************************************
402 * Get string descriptor from device
404 ************************************************************************/
405 static int get_string_desc (struct usb_device *dev, int Id, struct usb_string_descriptor **pRetDesc)
407 struct usb_string_descriptor StringDesc;
408 struct usb_string_descriptor *pStringDesc;
410 dbg("%s - USB String ID = %d", __FUNCTION__, Id );
412 if (!usb_get_descriptor(dev, USB_DT_STRING, Id, &StringDesc, sizeof(StringDesc))) {
413 return 0;
416 pStringDesc = kmalloc (StringDesc.bLength, GFP_KERNEL);
418 if (!pStringDesc) {
419 return -1;
422 if (!usb_get_descriptor(dev, USB_DT_STRING, Id, pStringDesc, StringDesc.bLength )) {
423 kfree(pStringDesc);
424 return -1;
427 *pRetDesc = pStringDesc;
428 return 0;
430 #endif
432 static void dump_product_info(struct edgeport_product_info *product_info)
434 // Dump Product Info structure
435 dbg("**Product Information:");
436 dbg(" ProductId %x", product_info->ProductId );
437 dbg(" NumPorts %d", product_info->NumPorts );
438 dbg(" ProdInfoVer %d", product_info->ProdInfoVer );
439 dbg(" IsServer %d", product_info->IsServer);
440 dbg(" IsRS232 %d", product_info->IsRS232 );
441 dbg(" IsRS422 %d", product_info->IsRS422 );
442 dbg(" IsRS485 %d", product_info->IsRS485 );
443 dbg(" RomSize %d", product_info->RomSize );
444 dbg(" RamSize %d", product_info->RamSize );
445 dbg(" CpuRev %x", product_info->CpuRev );
446 dbg(" BoardRev %x", product_info->BoardRev);
447 dbg(" BootMajorVersion %d.%d.%d", product_info->BootMajorVersion,
448 product_info->BootMinorVersion,
449 le16_to_cpu(product_info->BootBuildNumber));
450 dbg(" FirmwareMajorVersion %d.%d.%d", product_info->FirmwareMajorVersion,
451 product_info->FirmwareMinorVersion,
452 le16_to_cpu(product_info->FirmwareBuildNumber));
453 dbg(" ManufactureDescDate %d/%d/%d", product_info->ManufactureDescDate[0],
454 product_info->ManufactureDescDate[1],
455 product_info->ManufactureDescDate[2]+1900);
456 dbg(" iDownloadFile 0x%x", product_info->iDownloadFile);
457 dbg(" EpicVer %d", product_info->EpicVer);
460 static void get_product_info(struct edgeport_serial *edge_serial)
462 struct edgeport_product_info *product_info = &edge_serial->product_info;
464 memset (product_info, 0, sizeof(struct edgeport_product_info));
466 product_info->ProductId = (__u16)(le16_to_cpu(edge_serial->serial->dev->descriptor.idProduct) & ~ION_DEVICE_ID_80251_NETCHIP);
467 product_info->NumPorts = edge_serial->manuf_descriptor.NumPorts;
468 product_info->ProdInfoVer = 0;
470 product_info->RomSize = edge_serial->manuf_descriptor.RomSize;
471 product_info->RamSize = edge_serial->manuf_descriptor.RamSize;
472 product_info->CpuRev = edge_serial->manuf_descriptor.CpuRev;
473 product_info->BoardRev = edge_serial->manuf_descriptor.BoardRev;
475 product_info->BootMajorVersion = edge_serial->boot_descriptor.MajorVersion;
476 product_info->BootMinorVersion = edge_serial->boot_descriptor.MinorVersion;
477 product_info->BootBuildNumber = edge_serial->boot_descriptor.BuildNumber;
479 memcpy(product_info->ManufactureDescDate, edge_serial->manuf_descriptor.DescDate, sizeof(edge_serial->manuf_descriptor.DescDate));
481 // check if this is 2nd generation hardware
482 if (le16_to_cpu(edge_serial->serial->dev->descriptor.idProduct) & ION_DEVICE_ID_80251_NETCHIP) {
483 product_info->FirmwareMajorVersion = OperationalCodeImageVersion_GEN2.MajorVersion;
484 product_info->FirmwareMinorVersion = OperationalCodeImageVersion_GEN2.MinorVersion;
485 product_info->FirmwareBuildNumber = cpu_to_le16(OperationalCodeImageVersion_GEN2.BuildNumber);
486 product_info->iDownloadFile = EDGE_DOWNLOAD_FILE_80251;
487 } else {
488 product_info->FirmwareMajorVersion = OperationalCodeImageVersion_GEN1.MajorVersion;
489 product_info->FirmwareMinorVersion = OperationalCodeImageVersion_GEN1.MinorVersion;
490 product_info->FirmwareBuildNumber = cpu_to_le16(OperationalCodeImageVersion_GEN1.BuildNumber);
491 product_info->iDownloadFile = EDGE_DOWNLOAD_FILE_I930;
494 // Determine Product type and set appropriate flags
495 switch (DEVICE_ID_FROM_USB_PRODUCT_ID(product_info->ProductId)) {
496 case ION_DEVICE_ID_EDGEPORT_COMPATIBLE:
497 case ION_DEVICE_ID_EDGEPORT_4T:
498 case ION_DEVICE_ID_EDGEPORT_4:
499 case ION_DEVICE_ID_EDGEPORT_2:
500 case ION_DEVICE_ID_EDGEPORT_8_DUAL_CPU:
501 case ION_DEVICE_ID_EDGEPORT_8:
502 case ION_DEVICE_ID_EDGEPORT_421:
503 case ION_DEVICE_ID_EDGEPORT_21:
504 case ION_DEVICE_ID_EDGEPORT_2_DIN:
505 case ION_DEVICE_ID_EDGEPORT_4_DIN:
506 case ION_DEVICE_ID_EDGEPORT_16_DUAL_CPU:
507 product_info->IsRS232 = 1;
508 break;
510 case ION_DEVICE_ID_EDGEPORT_2I: // Edgeport/2 RS422/RS485
511 product_info->IsRS422 = 1;
512 product_info->IsRS485 = 1;
513 break;
515 case ION_DEVICE_ID_EDGEPORT_8I: // Edgeport/4 RS422
516 case ION_DEVICE_ID_EDGEPORT_4I: // Edgeport/4 RS422
517 product_info->IsRS422 = 1;
518 break;
521 dump_product_info(product_info);
524 static int get_epic_descriptor(struct edgeport_serial *ep)
526 int result;
527 struct usb_serial *serial = ep->serial;
528 struct edgeport_product_info *product_info = &ep->product_info;
529 struct edge_compatibility_descriptor *epic = &ep->epic_descriptor;
530 struct edge_compatibility_bits *bits;
532 ep->is_epic = 0;
533 result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
534 USB_REQUEST_ION_GET_EPIC_DESC,
535 0xC0, 0x00, 0x00,
536 &ep->epic_descriptor,
537 sizeof(struct edge_compatibility_descriptor),
538 300);
540 dbg("%s result = %d", __FUNCTION__, result);
542 if (result > 0) {
543 ep->is_epic = 1;
544 memset(product_info, 0, sizeof(struct edgeport_product_info));
546 product_info->NumPorts = epic->NumPorts;
547 product_info->ProdInfoVer = 0;
548 product_info->FirmwareMajorVersion = epic->MajorVersion;
549 product_info->FirmwareMinorVersion = epic->MinorVersion;
550 product_info->FirmwareBuildNumber = epic->BuildNumber;
551 product_info->iDownloadFile = epic->iDownloadFile;
552 product_info->EpicVer = epic->EpicVer;
553 product_info->Epic = epic->Supports;
554 product_info->ProductId = ION_DEVICE_ID_EDGEPORT_COMPATIBLE;
555 dump_product_info(product_info);
557 bits = &ep->epic_descriptor.Supports;
558 dbg("**EPIC descriptor:");
559 dbg(" VendEnableSuspend: %s", bits->VendEnableSuspend ? "TRUE": "FALSE");
560 dbg(" IOSPOpen : %s", bits->IOSPOpen ? "TRUE": "FALSE" );
561 dbg(" IOSPClose : %s", bits->IOSPClose ? "TRUE": "FALSE" );
562 dbg(" IOSPChase : %s", bits->IOSPChase ? "TRUE": "FALSE" );
563 dbg(" IOSPSetRxFlow : %s", bits->IOSPSetRxFlow ? "TRUE": "FALSE" );
564 dbg(" IOSPSetTxFlow : %s", bits->IOSPSetTxFlow ? "TRUE": "FALSE" );
565 dbg(" IOSPSetXChar : %s", bits->IOSPSetXChar ? "TRUE": "FALSE" );
566 dbg(" IOSPRxCheck : %s", bits->IOSPRxCheck ? "TRUE": "FALSE" );
567 dbg(" IOSPSetClrBreak : %s", bits->IOSPSetClrBreak ? "TRUE": "FALSE" );
568 dbg(" IOSPWriteMCR : %s", bits->IOSPWriteMCR ? "TRUE": "FALSE" );
569 dbg(" IOSPWriteLCR : %s", bits->IOSPWriteLCR ? "TRUE": "FALSE" );
570 dbg(" IOSPSetBaudRate : %s", bits->IOSPSetBaudRate ? "TRUE": "FALSE" );
571 dbg(" TrueEdgeport : %s", bits->TrueEdgeport ? "TRUE": "FALSE" );
574 return result;
578 /************************************************************************/
579 /************************************************************************/
580 /* U S B C A L L B A C K F U N C T I O N S */
581 /* U S B C A L L B A C K F U N C T I O N S */
582 /************************************************************************/
583 /************************************************************************/
585 /*****************************************************************************
586 * edge_interrupt_callback
587 * this is the callback function for when we have received data on the
588 * interrupt endpoint.
589 *****************************************************************************/
590 static void edge_interrupt_callback (struct urb *urb)
592 struct edgeport_serial *edge_serial = (struct edgeport_serial *)urb->context;
593 struct edgeport_port *edge_port;
594 struct usb_serial_port *port;
595 unsigned char *data = urb->transfer_buffer;
596 int length = urb->actual_length;
597 int bytes_avail;
598 int position;
599 int txCredits;
600 int portNumber;
601 int result;
602 int status = urb->status;
604 dbg("%s", __FUNCTION__);
606 switch (status) {
607 case 0:
608 /* success */
609 break;
610 case -ECONNRESET:
611 case -ENOENT:
612 case -ESHUTDOWN:
613 /* this urb is terminated, clean up */
614 dbg("%s - urb shutting down with status: %d",
615 __FUNCTION__, status);
616 return;
617 default:
618 dbg("%s - nonzero urb status received: %d",
619 __FUNCTION__, status);
620 goto exit;
623 // process this interrupt-read even if there are no ports open
624 if (length) {
625 usb_serial_debug_data(debug, &edge_serial->serial->dev->dev, __FUNCTION__, length, data);
627 if (length > 1) {
628 bytes_avail = data[0] | (data[1] << 8);
629 if (bytes_avail) {
630 spin_lock(&edge_serial->es_lock);
631 edge_serial->rxBytesAvail += bytes_avail;
632 dbg("%s - bytes_avail=%d, rxBytesAvail=%d, read_in_progress=%d", __FUNCTION__, bytes_avail, edge_serial->rxBytesAvail, edge_serial->read_in_progress);
634 if (edge_serial->rxBytesAvail > 0 &&
635 !edge_serial->read_in_progress) {
636 dbg("%s - posting a read", __FUNCTION__);
637 edge_serial->read_in_progress = true;
639 /* we have pending bytes on the bulk in pipe, send a request */
640 edge_serial->read_urb->dev = edge_serial->serial->dev;
641 result = usb_submit_urb(edge_serial->read_urb, GFP_ATOMIC);
642 if (result) {
643 dev_err(&edge_serial->serial->dev->dev, "%s - usb_submit_urb(read bulk) failed with result = %d\n", __FUNCTION__, result);
644 edge_serial->read_in_progress = false;
647 spin_unlock(&edge_serial->es_lock);
650 /* grab the txcredits for the ports if available */
651 position = 2;
652 portNumber = 0;
653 while ((position < length) && (portNumber < edge_serial->serial->num_ports)) {
654 txCredits = data[position] | (data[position+1] << 8);
655 if (txCredits) {
656 port = edge_serial->serial->port[portNumber];
657 edge_port = usb_get_serial_port_data(port);
658 if (edge_port->open) {
659 spin_lock(&edge_port->ep_lock);
660 edge_port->txCredits += txCredits;
661 spin_unlock(&edge_port->ep_lock);
662 dbg("%s - txcredits for port%d = %d", __FUNCTION__, portNumber, edge_port->txCredits);
664 /* tell the tty driver that something has changed */
665 if (edge_port->port->tty)
666 tty_wakeup(edge_port->port->tty);
668 // Since we have more credit, check if more data can be sent
669 send_more_port_data(edge_serial, edge_port);
672 position += 2;
673 ++portNumber;
677 exit:
678 result = usb_submit_urb (urb, GFP_ATOMIC);
679 if (result) {
680 dev_err(&urb->dev->dev, "%s - Error %d submitting control urb\n", __FUNCTION__, result);
685 /*****************************************************************************
686 * edge_bulk_in_callback
687 * this is the callback function for when we have received data on the
688 * bulk in endpoint.
689 *****************************************************************************/
690 static void edge_bulk_in_callback (struct urb *urb)
692 struct edgeport_serial *edge_serial = (struct edgeport_serial *)urb->context;
693 unsigned char *data = urb->transfer_buffer;
694 int retval;
695 __u16 raw_data_length;
696 int status = urb->status;
698 dbg("%s", __FUNCTION__);
700 if (status) {
701 dbg("%s - nonzero read bulk status received: %d",
702 __FUNCTION__, status);
703 edge_serial->read_in_progress = false;
704 return;
707 if (urb->actual_length == 0) {
708 dbg("%s - read bulk callback with no data", __FUNCTION__);
709 edge_serial->read_in_progress = false;
710 return;
713 raw_data_length = urb->actual_length;
715 usb_serial_debug_data(debug, &edge_serial->serial->dev->dev, __FUNCTION__, raw_data_length, data);
717 spin_lock(&edge_serial->es_lock);
719 /* decrement our rxBytes available by the number that we just got */
720 edge_serial->rxBytesAvail -= raw_data_length;
722 dbg("%s - Received = %d, rxBytesAvail %d", __FUNCTION__, raw_data_length, edge_serial->rxBytesAvail);
724 process_rcvd_data (edge_serial, data, urb->actual_length);
726 /* check to see if there's any more data for us to read */
727 if (edge_serial->rxBytesAvail > 0) {
728 dbg("%s - posting a read", __FUNCTION__);
729 edge_serial->read_urb->dev = edge_serial->serial->dev;
730 retval = usb_submit_urb(edge_serial->read_urb, GFP_ATOMIC);
731 if (retval) {
732 dev_err(&urb->dev->dev,
733 "%s - usb_submit_urb(read bulk) failed, "
734 "retval = %d\n", __FUNCTION__, retval);
735 edge_serial->read_in_progress = false;
737 } else {
738 edge_serial->read_in_progress = false;
741 spin_unlock(&edge_serial->es_lock);
745 /*****************************************************************************
746 * edge_bulk_out_data_callback
747 * this is the callback function for when we have finished sending serial data
748 * on the bulk out endpoint.
749 *****************************************************************************/
750 static void edge_bulk_out_data_callback (struct urb *urb)
752 struct edgeport_port *edge_port = (struct edgeport_port *)urb->context;
753 struct tty_struct *tty;
754 int status = urb->status;
756 dbg("%s", __FUNCTION__);
758 if (status) {
759 dbg("%s - nonzero write bulk status received: %d",
760 __FUNCTION__, status);
763 tty = edge_port->port->tty;
765 if (tty && edge_port->open) {
766 /* let the tty driver wakeup if it has a special write_wakeup function */
767 tty_wakeup(tty);
770 // Release the Write URB
771 edge_port->write_in_progress = false;
773 // Check if more data needs to be sent
774 send_more_port_data((struct edgeport_serial *)(usb_get_serial_data(edge_port->port->serial)), edge_port);
778 /*****************************************************************************
779 * BulkOutCmdCallback
780 * this is the callback function for when we have finished sending a command
781 * on the bulk out endpoint.
782 *****************************************************************************/
783 static void edge_bulk_out_cmd_callback (struct urb *urb)
785 struct edgeport_port *edge_port = (struct edgeport_port *)urb->context;
786 struct tty_struct *tty;
787 int status = urb->status;
789 dbg("%s", __FUNCTION__);
791 atomic_dec(&CmdUrbs);
792 dbg("%s - FREE URB %p (outstanding %d)", __FUNCTION__, urb, atomic_read(&CmdUrbs));
795 /* clean up the transfer buffer */
796 kfree(urb->transfer_buffer);
798 /* Free the command urb */
799 usb_free_urb (urb);
801 if (status) {
802 dbg("%s - nonzero write bulk status received: %d", __FUNCTION__, status);
803 return;
806 /* Get pointer to tty */
807 tty = edge_port->port->tty;
809 /* tell the tty driver that something has changed */
810 if (tty && edge_port->open)
811 tty_wakeup(tty);
813 /* we have completed the command */
814 edge_port->commandPending = false;
815 wake_up(&edge_port->wait_command);
819 /*****************************************************************************
820 * Driver tty interface functions
821 *****************************************************************************/
823 /*****************************************************************************
824 * SerialOpen
825 * this function is called by the tty driver when a port is opened
826 * If successful, we return 0
827 * Otherwise we return a negative error number.
828 *****************************************************************************/
829 static int edge_open (struct usb_serial_port *port, struct file * filp)
831 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
832 struct usb_serial *serial;
833 struct edgeport_serial *edge_serial;
834 int response;
836 dbg("%s - port %d", __FUNCTION__, port->number);
838 if (edge_port == NULL)
839 return -ENODEV;
841 if (port->tty)
842 port->tty->low_latency = low_latency;
844 /* see if we've set up our endpoint info yet (can't set it up in edge_startup
845 as the structures were not set up at that time.) */
846 serial = port->serial;
847 edge_serial = usb_get_serial_data(serial);
848 if (edge_serial == NULL) {
849 return -ENODEV;
851 if (edge_serial->interrupt_in_buffer == NULL) {
852 struct usb_serial_port *port0 = serial->port[0];
854 /* not set up yet, so do it now */
855 edge_serial->interrupt_in_buffer = port0->interrupt_in_buffer;
856 edge_serial->interrupt_in_endpoint = port0->interrupt_in_endpointAddress;
857 edge_serial->interrupt_read_urb = port0->interrupt_in_urb;
858 edge_serial->bulk_in_buffer = port0->bulk_in_buffer;
859 edge_serial->bulk_in_endpoint = port0->bulk_in_endpointAddress;
860 edge_serial->read_urb = port0->read_urb;
861 edge_serial->bulk_out_endpoint = port0->bulk_out_endpointAddress;
863 /* set up our interrupt urb */
864 usb_fill_int_urb(edge_serial->interrupt_read_urb,
865 serial->dev,
866 usb_rcvintpipe(serial->dev,
867 port0->interrupt_in_endpointAddress),
868 port0->interrupt_in_buffer,
869 edge_serial->interrupt_read_urb->transfer_buffer_length,
870 edge_interrupt_callback, edge_serial,
871 edge_serial->interrupt_read_urb->interval);
873 /* set up our bulk in urb */
874 usb_fill_bulk_urb(edge_serial->read_urb, serial->dev,
875 usb_rcvbulkpipe(serial->dev,
876 port0->bulk_in_endpointAddress),
877 port0->bulk_in_buffer,
878 edge_serial->read_urb->transfer_buffer_length,
879 edge_bulk_in_callback, edge_serial);
880 edge_serial->read_in_progress = false;
882 /* start interrupt read for this edgeport
883 * this interrupt will continue as long as the edgeport is connected */
884 response = usb_submit_urb (edge_serial->interrupt_read_urb, GFP_KERNEL);
885 if (response) {
886 dev_err(&port->dev, "%s - Error %d submitting control urb\n", __FUNCTION__, response);
890 /* initialize our wait queues */
891 init_waitqueue_head(&edge_port->wait_open);
892 init_waitqueue_head(&edge_port->wait_chase);
893 init_waitqueue_head(&edge_port->delta_msr_wait);
894 init_waitqueue_head(&edge_port->wait_command);
896 /* initialize our icount structure */
897 memset (&(edge_port->icount), 0x00, sizeof(edge_port->icount));
899 /* initialize our port settings */
900 edge_port->txCredits = 0; /* Can't send any data yet */
901 edge_port->shadowMCR = MCR_MASTER_IE; /* Must always set this bit to enable ints! */
902 edge_port->chaseResponsePending = false;
904 /* send a open port command */
905 edge_port->openPending = true;
906 edge_port->open = false;
907 response = send_iosp_ext_cmd (edge_port, IOSP_CMD_OPEN_PORT, 0);
909 if (response < 0) {
910 dev_err(&port->dev, "%s - error sending open port command\n", __FUNCTION__);
911 edge_port->openPending = false;
912 return -ENODEV;
915 /* now wait for the port to be completely opened */
916 wait_event_timeout(edge_port->wait_open, !edge_port->openPending, OPEN_TIMEOUT);
918 if (!edge_port->open) {
919 /* open timed out */
920 dbg("%s - open timedout", __FUNCTION__);
921 edge_port->openPending = false;
922 return -ENODEV;
925 /* create the txfifo */
926 edge_port->txfifo.head = 0;
927 edge_port->txfifo.tail = 0;
928 edge_port->txfifo.count = 0;
929 edge_port->txfifo.size = edge_port->maxTxCredits;
930 edge_port->txfifo.fifo = kmalloc (edge_port->maxTxCredits, GFP_KERNEL);
932 if (!edge_port->txfifo.fifo) {
933 dbg("%s - no memory", __FUNCTION__);
934 edge_close (port, filp);
935 return -ENOMEM;
938 /* Allocate a URB for the write */
939 edge_port->write_urb = usb_alloc_urb (0, GFP_KERNEL);
940 edge_port->write_in_progress = false;
942 if (!edge_port->write_urb) {
943 dbg("%s - no memory", __FUNCTION__);
944 edge_close (port, filp);
945 return -ENOMEM;
948 dbg("%s(%d) - Initialize TX fifo to %d bytes", __FUNCTION__, port->number, edge_port->maxTxCredits);
950 dbg("%s exited", __FUNCTION__);
952 return 0;
956 /************************************************************************
958 * block_until_chase_response
960 * This function will block the close until one of the following:
961 * 1. Response to our Chase comes from Edgeport
962 * 2. A timout of 10 seconds without activity has expired
963 * (1K of Edgeport data @ 2400 baud ==> 4 sec to empty)
965 ************************************************************************/
966 static void block_until_chase_response(struct edgeport_port *edge_port)
968 DEFINE_WAIT(wait);
969 __u16 lastCredits;
970 int timeout = 1*HZ;
971 int loop = 10;
973 while (1) {
974 // Save Last credits
975 lastCredits = edge_port->txCredits;
977 // Did we get our Chase response
978 if (!edge_port->chaseResponsePending) {
979 dbg("%s - Got Chase Response", __FUNCTION__);
981 // did we get all of our credit back?
982 if (edge_port->txCredits == edge_port->maxTxCredits ) {
983 dbg("%s - Got all credits", __FUNCTION__);
984 return;
988 // Block the thread for a while
989 prepare_to_wait(&edge_port->wait_chase, &wait, TASK_UNINTERRUPTIBLE);
990 schedule_timeout(timeout);
991 finish_wait(&edge_port->wait_chase, &wait);
993 if (lastCredits == edge_port->txCredits) {
994 // No activity.. count down.
995 loop--;
996 if (loop == 0) {
997 edge_port->chaseResponsePending = false;
998 dbg("%s - Chase TIMEOUT", __FUNCTION__);
999 return;
1001 } else {
1002 // Reset timout value back to 10 seconds
1003 dbg("%s - Last %d, Current %d", __FUNCTION__, lastCredits, edge_port->txCredits);
1004 loop = 10;
1010 /************************************************************************
1012 * block_until_tx_empty
1014 * This function will block the close until one of the following:
1015 * 1. TX count are 0
1016 * 2. The edgeport has stopped
1017 * 3. A timout of 3 seconds without activity has expired
1019 ************************************************************************/
1020 static void block_until_tx_empty (struct edgeport_port *edge_port)
1022 DEFINE_WAIT(wait);
1023 struct TxFifo *fifo = &edge_port->txfifo;
1024 __u32 lastCount;
1025 int timeout = HZ/10;
1026 int loop = 30;
1028 while (1) {
1029 // Save Last count
1030 lastCount = fifo->count;
1032 // Is the Edgeport Buffer empty?
1033 if (lastCount == 0) {
1034 dbg("%s - TX Buffer Empty", __FUNCTION__);
1035 return;
1038 // Block the thread for a while
1039 prepare_to_wait (&edge_port->wait_chase, &wait, TASK_UNINTERRUPTIBLE);
1040 schedule_timeout(timeout);
1041 finish_wait(&edge_port->wait_chase, &wait);
1043 dbg("%s wait", __FUNCTION__);
1045 if (lastCount == fifo->count) {
1046 // No activity.. count down.
1047 loop--;
1048 if (loop == 0) {
1049 dbg("%s - TIMEOUT", __FUNCTION__);
1050 return;
1052 } else {
1053 // Reset timout value back to seconds
1054 loop = 30;
1060 /*****************************************************************************
1061 * edge_close
1062 * this function is called by the tty driver when a port is closed
1063 *****************************************************************************/
1064 static void edge_close (struct usb_serial_port *port, struct file * filp)
1066 struct edgeport_serial *edge_serial;
1067 struct edgeport_port *edge_port;
1068 int status;
1070 dbg("%s - port %d", __FUNCTION__, port->number);
1072 edge_serial = usb_get_serial_data(port->serial);
1073 edge_port = usb_get_serial_port_data(port);
1074 if ((edge_serial == NULL) || (edge_port == NULL))
1075 return;
1077 // block until tx is empty
1078 block_until_tx_empty(edge_port);
1080 edge_port->closePending = true;
1082 if ((!edge_serial->is_epic) ||
1083 ((edge_serial->is_epic) &&
1084 (edge_serial->epic_descriptor.Supports.IOSPChase))) {
1085 /* flush and chase */
1086 edge_port->chaseResponsePending = true;
1088 dbg("%s - Sending IOSP_CMD_CHASE_PORT", __FUNCTION__);
1089 status = send_iosp_ext_cmd (edge_port, IOSP_CMD_CHASE_PORT, 0);
1090 if (status == 0) {
1091 // block until chase finished
1092 block_until_chase_response(edge_port);
1093 } else {
1094 edge_port->chaseResponsePending = false;
1098 if ((!edge_serial->is_epic) ||
1099 ((edge_serial->is_epic) &&
1100 (edge_serial->epic_descriptor.Supports.IOSPClose))) {
1101 /* close the port */
1102 dbg("%s - Sending IOSP_CMD_CLOSE_PORT", __FUNCTION__);
1103 send_iosp_ext_cmd (edge_port, IOSP_CMD_CLOSE_PORT, 0);
1106 //port->close = true;
1107 edge_port->closePending = false;
1108 edge_port->open = false;
1109 edge_port->openPending = false;
1111 usb_kill_urb(edge_port->write_urb);
1113 if (edge_port->write_urb) {
1114 /* if this urb had a transfer buffer already (old transfer) free it */
1115 kfree(edge_port->write_urb->transfer_buffer);
1116 usb_free_urb(edge_port->write_urb);
1117 edge_port->write_urb = NULL;
1119 kfree(edge_port->txfifo.fifo);
1120 edge_port->txfifo.fifo = NULL;
1122 dbg("%s exited", __FUNCTION__);
1125 /*****************************************************************************
1126 * SerialWrite
1127 * this function is called by the tty driver when data should be written to
1128 * the port.
1129 * If successful, we return the number of bytes written, otherwise we return
1130 * a negative error number.
1131 *****************************************************************************/
1132 static int edge_write (struct usb_serial_port *port, const unsigned char *data, int count)
1134 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1135 struct TxFifo *fifo;
1136 int copySize;
1137 int bytesleft;
1138 int firsthalf;
1139 int secondhalf;
1140 unsigned long flags;
1142 dbg("%s - port %d", __FUNCTION__, port->number);
1144 if (edge_port == NULL)
1145 return -ENODEV;
1147 // get a pointer to the Tx fifo
1148 fifo = &edge_port->txfifo;
1150 spin_lock_irqsave(&edge_port->ep_lock, flags);
1152 // calculate number of bytes to put in fifo
1153 copySize = min ((unsigned int)count, (edge_port->txCredits - fifo->count));
1155 dbg("%s(%d) of %d byte(s) Fifo room %d -- will copy %d bytes", __FUNCTION__,
1156 port->number, count, edge_port->txCredits - fifo->count, copySize);
1158 /* catch writes of 0 bytes which the tty driver likes to give us, and when txCredits is empty */
1159 if (copySize == 0) {
1160 dbg("%s - copySize = Zero", __FUNCTION__);
1161 goto finish_write;
1164 // queue the data
1165 // since we can never overflow the buffer we do not have to check for full condition
1167 // the copy is done is two parts -- first fill to the end of the buffer
1168 // then copy the reset from the start of the buffer
1170 bytesleft = fifo->size - fifo->head;
1171 firsthalf = min (bytesleft, copySize);
1172 dbg("%s - copy %d bytes of %d into fifo ", __FUNCTION__, firsthalf, bytesleft);
1174 /* now copy our data */
1175 memcpy(&fifo->fifo[fifo->head], data, firsthalf);
1176 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, firsthalf, &fifo->fifo[fifo->head]);
1178 // update the index and size
1179 fifo->head += firsthalf;
1180 fifo->count += firsthalf;
1182 // wrap the index
1183 if (fifo->head == fifo->size) {
1184 fifo->head = 0;
1187 secondhalf = copySize-firsthalf;
1189 if (secondhalf) {
1190 dbg("%s - copy rest of data %d", __FUNCTION__, secondhalf);
1191 memcpy(&fifo->fifo[fifo->head], &data[firsthalf], secondhalf);
1192 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, secondhalf, &fifo->fifo[fifo->head]);
1193 // update the index and size
1194 fifo->count += secondhalf;
1195 fifo->head += secondhalf;
1196 // No need to check for wrap since we can not get to end of fifo in this part
1199 finish_write:
1200 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1202 send_more_port_data((struct edgeport_serial *)usb_get_serial_data(port->serial), edge_port);
1204 dbg("%s wrote %d byte(s) TxCredits %d, Fifo %d", __FUNCTION__, copySize, edge_port->txCredits, fifo->count);
1206 return copySize;
1210 /************************************************************************
1212 * send_more_port_data()
1214 * This routine attempts to write additional UART transmit data
1215 * to a port over the USB bulk pipe. It is called (1) when new
1216 * data has been written to a port's TxBuffer from higher layers
1217 * (2) when the peripheral sends us additional TxCredits indicating
1218 * that it can accept more Tx data for a given port; and (3) when
1219 * a bulk write completes successfully and we want to see if we
1220 * can transmit more.
1222 ************************************************************************/
1223 static void send_more_port_data(struct edgeport_serial *edge_serial, struct edgeport_port *edge_port)
1225 struct TxFifo *fifo = &edge_port->txfifo;
1226 struct urb *urb;
1227 unsigned char *buffer;
1228 int status;
1229 int count;
1230 int bytesleft;
1231 int firsthalf;
1232 int secondhalf;
1233 unsigned long flags;
1235 dbg("%s(%d)", __FUNCTION__, edge_port->port->number);
1237 spin_lock_irqsave(&edge_port->ep_lock, flags);
1239 if (edge_port->write_in_progress ||
1240 !edge_port->open ||
1241 (fifo->count == 0)) {
1242 dbg("%s(%d) EXIT - fifo %d, PendingWrite = %d", __FUNCTION__, edge_port->port->number, fifo->count, edge_port->write_in_progress);
1243 goto exit_send;
1246 // since the amount of data in the fifo will always fit into the
1247 // edgeport buffer we do not need to check the write length
1249 // Do we have enough credits for this port to make it worthwhile
1250 // to bother queueing a write. If it's too small, say a few bytes,
1251 // it's better to wait for more credits so we can do a larger
1252 // write.
1253 if (edge_port->txCredits < EDGE_FW_GET_TX_CREDITS_SEND_THRESHOLD(edge_port->maxTxCredits,EDGE_FW_BULK_MAX_PACKET_SIZE)) {
1254 dbg("%s(%d) Not enough credit - fifo %d TxCredit %d", __FUNCTION__, edge_port->port->number, fifo->count, edge_port->txCredits );
1255 goto exit_send;
1258 // lock this write
1259 edge_port->write_in_progress = true;
1261 // get a pointer to the write_urb
1262 urb = edge_port->write_urb;
1264 /* make sure transfer buffer is freed */
1265 kfree(urb->transfer_buffer);
1266 urb->transfer_buffer = NULL;
1268 /* build the data header for the buffer and port that we are about to send out */
1269 count = fifo->count;
1270 buffer = kmalloc (count+2, GFP_ATOMIC);
1271 if (buffer == NULL) {
1272 dev_err(&edge_port->port->dev, "%s - no more kernel memory...\n", __FUNCTION__);
1273 edge_port->write_in_progress = false;
1274 goto exit_send;
1276 buffer[0] = IOSP_BUILD_DATA_HDR1 (edge_port->port->number - edge_port->port->serial->minor, count);
1277 buffer[1] = IOSP_BUILD_DATA_HDR2 (edge_port->port->number - edge_port->port->serial->minor, count);
1279 /* now copy our data */
1280 bytesleft = fifo->size - fifo->tail;
1281 firsthalf = min (bytesleft, count);
1282 memcpy(&buffer[2], &fifo->fifo[fifo->tail], firsthalf);
1283 fifo->tail += firsthalf;
1284 fifo->count -= firsthalf;
1285 if (fifo->tail == fifo->size) {
1286 fifo->tail = 0;
1289 secondhalf = count-firsthalf;
1290 if (secondhalf) {
1291 memcpy(&buffer[2+firsthalf], &fifo->fifo[fifo->tail], secondhalf);
1292 fifo->tail += secondhalf;
1293 fifo->count -= secondhalf;
1296 if (count)
1297 usb_serial_debug_data(debug, &edge_port->port->dev, __FUNCTION__, count, &buffer[2]);
1299 /* fill up the urb with all of our data and submit it */
1300 usb_fill_bulk_urb (urb, edge_serial->serial->dev,
1301 usb_sndbulkpipe(edge_serial->serial->dev, edge_serial->bulk_out_endpoint),
1302 buffer, count+2, edge_bulk_out_data_callback, edge_port);
1304 /* decrement the number of credits we have by the number we just sent */
1305 edge_port->txCredits -= count;
1306 edge_port->icount.tx += count;
1308 urb->dev = edge_serial->serial->dev;
1309 status = usb_submit_urb(urb, GFP_ATOMIC);
1310 if (status) {
1311 /* something went wrong */
1312 dev_err(&edge_port->port->dev, "%s - usb_submit_urb(write bulk) failed, status = %d, data lost\n", __FUNCTION__, status);
1313 edge_port->write_in_progress = false;
1315 /* revert the credits as something bad happened. */
1316 edge_port->txCredits += count;
1317 edge_port->icount.tx -= count;
1319 dbg("%s wrote %d byte(s) TxCredit %d, Fifo %d", __FUNCTION__, count, edge_port->txCredits, fifo->count);
1321 exit_send:
1322 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1326 /*****************************************************************************
1327 * edge_write_room
1328 * this function is called by the tty driver when it wants to know how many
1329 * bytes of data we can accept for a specific port.
1330 * If successful, we return the amount of room that we have for this port
1331 * (the txCredits),
1332 * Otherwise we return a negative error number.
1333 *****************************************************************************/
1334 static int edge_write_room (struct usb_serial_port *port)
1336 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1337 int room;
1338 unsigned long flags;
1340 dbg("%s", __FUNCTION__);
1342 if (edge_port == NULL)
1343 return -ENODEV;
1344 if (edge_port->closePending)
1345 return -ENODEV;
1347 dbg("%s - port %d", __FUNCTION__, port->number);
1349 if (!edge_port->open) {
1350 dbg("%s - port not opened", __FUNCTION__);
1351 return -EINVAL;
1354 // total of both buffers is still txCredit
1355 spin_lock_irqsave(&edge_port->ep_lock, flags);
1356 room = edge_port->txCredits - edge_port->txfifo.count;
1357 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1359 dbg("%s - returns %d", __FUNCTION__, room);
1360 return room;
1364 /*****************************************************************************
1365 * edge_chars_in_buffer
1366 * this function is called by the tty driver when it wants to know how many
1367 * bytes of data we currently have outstanding in the port (data that has
1368 * been written, but hasn't made it out the port yet)
1369 * If successful, we return the number of bytes left to be written in the
1370 * system,
1371 * Otherwise we return a negative error number.
1372 *****************************************************************************/
1373 static int edge_chars_in_buffer (struct usb_serial_port *port)
1375 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1376 int num_chars;
1377 unsigned long flags;
1379 dbg("%s", __FUNCTION__);
1381 if (edge_port == NULL)
1382 return -ENODEV;
1383 if (edge_port->closePending)
1384 return -ENODEV;
1386 if (!edge_port->open) {
1387 dbg("%s - port not opened", __FUNCTION__);
1388 return -EINVAL;
1391 spin_lock_irqsave(&edge_port->ep_lock, flags);
1392 num_chars = edge_port->maxTxCredits - edge_port->txCredits + edge_port->txfifo.count;
1393 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1394 if (num_chars) {
1395 dbg("%s(port %d) - returns %d", __FUNCTION__, port->number, num_chars);
1398 return num_chars;
1402 /*****************************************************************************
1403 * SerialThrottle
1404 * this function is called by the tty driver when it wants to stop the data
1405 * being read from the port.
1406 *****************************************************************************/
1407 static void edge_throttle (struct usb_serial_port *port)
1409 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1410 struct tty_struct *tty;
1411 int status;
1413 dbg("%s - port %d", __FUNCTION__, port->number);
1415 if (edge_port == NULL)
1416 return;
1418 if (!edge_port->open) {
1419 dbg("%s - port not opened", __FUNCTION__);
1420 return;
1423 tty = port->tty;
1424 if (!tty) {
1425 dbg ("%s - no tty available", __FUNCTION__);
1426 return;
1429 /* if we are implementing XON/XOFF, send the stop character */
1430 if (I_IXOFF(tty)) {
1431 unsigned char stop_char = STOP_CHAR(tty);
1432 status = edge_write (port, &stop_char, 1);
1433 if (status <= 0) {
1434 return;
1438 /* if we are implementing RTS/CTS, toggle that line */
1439 if (tty->termios->c_cflag & CRTSCTS) {
1440 edge_port->shadowMCR &= ~MCR_RTS;
1441 status = send_cmd_write_uart_register(edge_port, MCR, edge_port->shadowMCR);
1442 if (status != 0) {
1443 return;
1447 return;
1451 /*****************************************************************************
1452 * edge_unthrottle
1453 * this function is called by the tty driver when it wants to resume the data
1454 * being read from the port (called after SerialThrottle is called)
1455 *****************************************************************************/
1456 static void edge_unthrottle (struct usb_serial_port *port)
1458 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1459 struct tty_struct *tty;
1460 int status;
1462 dbg("%s - port %d", __FUNCTION__, port->number);
1464 if (edge_port == NULL)
1465 return;
1467 if (!edge_port->open) {
1468 dbg("%s - port not opened", __FUNCTION__);
1469 return;
1472 tty = port->tty;
1473 if (!tty) {
1474 dbg ("%s - no tty available", __FUNCTION__);
1475 return;
1478 /* if we are implementing XON/XOFF, send the start character */
1479 if (I_IXOFF(tty)) {
1480 unsigned char start_char = START_CHAR(tty);
1481 status = edge_write (port, &start_char, 1);
1482 if (status <= 0) {
1483 return;
1487 /* if we are implementing RTS/CTS, toggle that line */
1488 if (tty->termios->c_cflag & CRTSCTS) {
1489 edge_port->shadowMCR |= MCR_RTS;
1490 status = send_cmd_write_uart_register(edge_port, MCR, edge_port->shadowMCR);
1491 if (status != 0) {
1492 return;
1496 return;
1500 /*****************************************************************************
1501 * SerialSetTermios
1502 * this function is called by the tty driver when it wants to change the termios structure
1503 *****************************************************************************/
1504 static void edge_set_termios (struct usb_serial_port *port, struct ktermios *old_termios)
1506 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1507 struct tty_struct *tty = port->tty;
1508 unsigned int cflag;
1510 if (!port->tty || !port->tty->termios) {
1511 dbg ("%s - no tty or termios", __FUNCTION__);
1512 return;
1515 cflag = tty->termios->c_cflag;
1516 /* check that they really want us to change something */
1517 if (old_termios) {
1518 if (cflag == old_termios->c_cflag &&
1519 tty->termios->c_iflag == old_termios->c_iflag) {
1520 dbg("%s - nothing to change", __FUNCTION__);
1521 return;
1525 dbg("%s - clfag %08x iflag %08x", __FUNCTION__,
1526 tty->termios->c_cflag, tty->termios->c_iflag);
1527 if (old_termios) {
1528 dbg("%s - old clfag %08x old iflag %08x", __FUNCTION__,
1529 old_termios->c_cflag, old_termios->c_iflag);
1532 dbg("%s - port %d", __FUNCTION__, port->number);
1534 if (edge_port == NULL)
1535 return;
1537 if (!edge_port->open) {
1538 dbg("%s - port not opened", __FUNCTION__);
1539 return;
1542 /* change the port settings to the new ones specified */
1543 change_port_settings (edge_port, old_termios);
1545 return;
1549 /*****************************************************************************
1550 * get_lsr_info - get line status register info
1552 * Purpose: Let user call ioctl() to get info when the UART physically
1553 * is emptied. On bus types like RS485, the transmitter must
1554 * release the bus after transmitting. This must be done when
1555 * the transmit shift register is empty, not be done when the
1556 * transmit holding register is empty. This functionality
1557 * allows an RS485 driver to be written in user space.
1558 *****************************************************************************/
1559 static int get_lsr_info(struct edgeport_port *edge_port, unsigned int __user *value)
1561 unsigned int result = 0;
1562 unsigned long flags;
1564 spin_lock_irqsave(&edge_port->ep_lock, flags);
1565 if (edge_port->maxTxCredits == edge_port->txCredits &&
1566 edge_port->txfifo.count == 0) {
1567 dbg("%s -- Empty", __FUNCTION__);
1568 result = TIOCSER_TEMT;
1570 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1572 if (copy_to_user(value, &result, sizeof(int)))
1573 return -EFAULT;
1574 return 0;
1577 static int get_number_bytes_avail(struct edgeport_port *edge_port, unsigned int __user *value)
1579 unsigned int result = 0;
1580 struct tty_struct *tty = edge_port->port->tty;
1582 if (!tty)
1583 return -ENOIOCTLCMD;
1585 result = tty->read_cnt;
1587 dbg("%s(%d) = %d", __FUNCTION__, edge_port->port->number, result);
1588 if (copy_to_user(value, &result, sizeof(int)))
1589 return -EFAULT;
1590 //return 0;
1591 return -ENOIOCTLCMD;
1594 static int edge_tiocmset (struct usb_serial_port *port, struct file *file, unsigned int set, unsigned int clear)
1596 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1597 unsigned int mcr;
1599 dbg("%s - port %d", __FUNCTION__, port->number);
1601 mcr = edge_port->shadowMCR;
1602 if (set & TIOCM_RTS)
1603 mcr |= MCR_RTS;
1604 if (set & TIOCM_DTR)
1605 mcr |= MCR_DTR;
1606 if (set & TIOCM_LOOP)
1607 mcr |= MCR_LOOPBACK;
1609 if (clear & TIOCM_RTS)
1610 mcr &= ~MCR_RTS;
1611 if (clear & TIOCM_DTR)
1612 mcr &= ~MCR_DTR;
1613 if (clear & TIOCM_LOOP)
1614 mcr &= ~MCR_LOOPBACK;
1616 edge_port->shadowMCR = mcr;
1618 send_cmd_write_uart_register(edge_port, MCR, edge_port->shadowMCR);
1620 return 0;
1623 static int edge_tiocmget(struct usb_serial_port *port, struct file *file)
1625 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1626 unsigned int result = 0;
1627 unsigned int msr;
1628 unsigned int mcr;
1630 dbg("%s - port %d", __FUNCTION__, port->number);
1632 msr = edge_port->shadowMSR;
1633 mcr = edge_port->shadowMCR;
1634 result = ((mcr & MCR_DTR) ? TIOCM_DTR: 0) /* 0x002 */
1635 | ((mcr & MCR_RTS) ? TIOCM_RTS: 0) /* 0x004 */
1636 | ((msr & EDGEPORT_MSR_CTS) ? TIOCM_CTS: 0) /* 0x020 */
1637 | ((msr & EDGEPORT_MSR_CD) ? TIOCM_CAR: 0) /* 0x040 */
1638 | ((msr & EDGEPORT_MSR_RI) ? TIOCM_RI: 0) /* 0x080 */
1639 | ((msr & EDGEPORT_MSR_DSR) ? TIOCM_DSR: 0); /* 0x100 */
1642 dbg("%s -- %x", __FUNCTION__, result);
1644 return result;
1647 static int get_serial_info(struct edgeport_port *edge_port, struct serial_struct __user *retinfo)
1649 struct serial_struct tmp;
1651 if (!retinfo)
1652 return -EFAULT;
1654 memset(&tmp, 0, sizeof(tmp));
1656 tmp.type = PORT_16550A;
1657 tmp.line = edge_port->port->serial->minor;
1658 tmp.port = edge_port->port->number;
1659 tmp.irq = 0;
1660 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
1661 tmp.xmit_fifo_size = edge_port->maxTxCredits;
1662 tmp.baud_base = 9600;
1663 tmp.close_delay = 5*HZ;
1664 tmp.closing_wait = 30*HZ;
1665 // tmp.custom_divisor = state->custom_divisor;
1666 // tmp.hub6 = state->hub6;
1667 // tmp.io_type = state->io_type;
1669 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1670 return -EFAULT;
1671 return 0;
1676 /*****************************************************************************
1677 * SerialIoctl
1678 * this function handles any ioctl calls to the driver
1679 *****************************************************************************/
1680 static int edge_ioctl (struct usb_serial_port *port, struct file *file, unsigned int cmd, unsigned long arg)
1682 DEFINE_WAIT(wait);
1683 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1684 struct async_icount cnow;
1685 struct async_icount cprev;
1686 struct serial_icounter_struct icount;
1688 dbg("%s - port %d, cmd = 0x%x", __FUNCTION__, port->number, cmd);
1690 switch (cmd) {
1691 // return number of bytes available
1692 case TIOCINQ:
1693 dbg("%s (%d) TIOCINQ", __FUNCTION__, port->number);
1694 return get_number_bytes_avail(edge_port, (unsigned int __user *) arg);
1695 break;
1697 case TIOCSERGETLSR:
1698 dbg("%s (%d) TIOCSERGETLSR", __FUNCTION__, port->number);
1699 return get_lsr_info(edge_port, (unsigned int __user *) arg);
1700 return 0;
1702 case TIOCGSERIAL:
1703 dbg("%s (%d) TIOCGSERIAL", __FUNCTION__, port->number);
1704 return get_serial_info(edge_port, (struct serial_struct __user *) arg);
1706 case TIOCSSERIAL:
1707 dbg("%s (%d) TIOCSSERIAL", __FUNCTION__, port->number);
1708 break;
1710 case TIOCMIWAIT:
1711 dbg("%s (%d) TIOCMIWAIT", __FUNCTION__, port->number);
1712 cprev = edge_port->icount;
1713 while (1) {
1714 prepare_to_wait(&edge_port->delta_msr_wait, &wait, TASK_INTERRUPTIBLE);
1715 schedule();
1716 finish_wait(&edge_port->delta_msr_wait, &wait);
1717 /* see if a signal did it */
1718 if (signal_pending(current))
1719 return -ERESTARTSYS;
1720 cnow = edge_port->icount;
1721 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
1722 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
1723 return -EIO; /* no change => error */
1724 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1725 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1726 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1727 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)) ) {
1728 return 0;
1730 cprev = cnow;
1732 /* NOTREACHED */
1733 break;
1735 case TIOCGICOUNT:
1736 cnow = edge_port->icount;
1737 memset(&icount, 0, sizeof(icount));
1738 icount.cts = cnow.cts;
1739 icount.dsr = cnow.dsr;
1740 icount.rng = cnow.rng;
1741 icount.dcd = cnow.dcd;
1742 icount.rx = cnow.rx;
1743 icount.tx = cnow.tx;
1744 icount.frame = cnow.frame;
1745 icount.overrun = cnow.overrun;
1746 icount.parity = cnow.parity;
1747 icount.brk = cnow.brk;
1748 icount.buf_overrun = cnow.buf_overrun;
1750 dbg("%s (%d) TIOCGICOUNT RX=%d, TX=%d", __FUNCTION__, port->number, icount.rx, icount.tx );
1751 if (copy_to_user((void __user *)arg, &icount, sizeof(icount)))
1752 return -EFAULT;
1753 return 0;
1756 return -ENOIOCTLCMD;
1760 /*****************************************************************************
1761 * SerialBreak
1762 * this function sends a break to the port
1763 *****************************************************************************/
1764 static void edge_break (struct usb_serial_port *port, int break_state)
1766 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1767 struct edgeport_serial *edge_serial = usb_get_serial_data(port->serial);
1768 int status;
1770 if ((!edge_serial->is_epic) ||
1771 ((edge_serial->is_epic) &&
1772 (edge_serial->epic_descriptor.Supports.IOSPChase))) {
1773 /* flush and chase */
1774 edge_port->chaseResponsePending = true;
1776 dbg("%s - Sending IOSP_CMD_CHASE_PORT", __FUNCTION__);
1777 status = send_iosp_ext_cmd (edge_port, IOSP_CMD_CHASE_PORT, 0);
1778 if (status == 0) {
1779 // block until chase finished
1780 block_until_chase_response(edge_port);
1781 } else {
1782 edge_port->chaseResponsePending = false;
1786 if ((!edge_serial->is_epic) ||
1787 ((edge_serial->is_epic) &&
1788 (edge_serial->epic_descriptor.Supports.IOSPSetClrBreak))) {
1789 if (break_state == -1) {
1790 dbg("%s - Sending IOSP_CMD_SET_BREAK", __FUNCTION__);
1791 status = send_iosp_ext_cmd (edge_port, IOSP_CMD_SET_BREAK, 0);
1792 } else {
1793 dbg("%s - Sending IOSP_CMD_CLEAR_BREAK", __FUNCTION__);
1794 status = send_iosp_ext_cmd (edge_port, IOSP_CMD_CLEAR_BREAK, 0);
1796 if (status) {
1797 dbg("%s - error sending break set/clear command.", __FUNCTION__);
1801 return;
1805 /*****************************************************************************
1806 * process_rcvd_data
1807 * this function handles the data received on the bulk in pipe.
1808 *****************************************************************************/
1809 static void process_rcvd_data (struct edgeport_serial *edge_serial, unsigned char * buffer, __u16 bufferLength)
1811 struct usb_serial_port *port;
1812 struct edgeport_port *edge_port;
1813 struct tty_struct *tty;
1814 __u16 lastBufferLength;
1815 __u16 rxLen;
1817 dbg("%s", __FUNCTION__);
1819 lastBufferLength = bufferLength + 1;
1821 while (bufferLength > 0) {
1822 /* failsafe incase we get a message that we don't understand */
1823 if (lastBufferLength == bufferLength) {
1824 dbg("%s - stuck in loop, exiting it.", __FUNCTION__);
1825 break;
1827 lastBufferLength = bufferLength;
1829 switch (edge_serial->rxState) {
1830 case EXPECT_HDR1:
1831 edge_serial->rxHeader1 = *buffer;
1832 ++buffer;
1833 --bufferLength;
1835 if (bufferLength == 0) {
1836 edge_serial->rxState = EXPECT_HDR2;
1837 break;
1839 /* otherwise, drop on through */
1841 case EXPECT_HDR2:
1842 edge_serial->rxHeader2 = *buffer;
1843 ++buffer;
1844 --bufferLength;
1846 dbg("%s - Hdr1=%02X Hdr2=%02X", __FUNCTION__, edge_serial->rxHeader1, edge_serial->rxHeader2);
1848 // Process depending on whether this header is
1849 // data or status
1851 if (IS_CMD_STAT_HDR(edge_serial->rxHeader1)) {
1852 // Decode this status header and goto EXPECT_HDR1 (if we
1853 // can process the status with only 2 bytes), or goto
1854 // EXPECT_HDR3 to get the third byte.
1856 edge_serial->rxPort = IOSP_GET_HDR_PORT(edge_serial->rxHeader1);
1857 edge_serial->rxStatusCode = IOSP_GET_STATUS_CODE(edge_serial->rxHeader1);
1859 if (!IOSP_STATUS_IS_2BYTE(edge_serial->rxStatusCode)) {
1860 // This status needs additional bytes. Save what we have
1861 // and then wait for more data.
1862 edge_serial->rxStatusParam = edge_serial->rxHeader2;
1864 edge_serial->rxState = EXPECT_HDR3;
1865 break;
1868 // We have all the header bytes, process the status now
1869 process_rcvd_status (edge_serial, edge_serial->rxHeader2, 0);
1870 edge_serial->rxState = EXPECT_HDR1;
1871 break;
1872 } else {
1873 edge_serial->rxPort = IOSP_GET_HDR_PORT(edge_serial->rxHeader1);
1874 edge_serial->rxBytesRemaining = IOSP_GET_HDR_DATA_LEN(edge_serial->rxHeader1, edge_serial->rxHeader2);
1876 dbg("%s - Data for Port %u Len %u", __FUNCTION__, edge_serial->rxPort, edge_serial->rxBytesRemaining);
1878 //ASSERT( DevExt->RxPort < DevExt->NumPorts );
1879 //ASSERT( DevExt->RxBytesRemaining < IOSP_MAX_DATA_LENGTH );
1881 if (bufferLength == 0 ) {
1882 edge_serial->rxState = EXPECT_DATA;
1883 break;
1885 // Else, drop through
1888 case EXPECT_DATA: // Expect data
1890 if (bufferLength < edge_serial->rxBytesRemaining) {
1891 rxLen = bufferLength;
1892 edge_serial->rxState = EXPECT_DATA; // Expect data to start next buffer
1893 } else {
1894 // BufLen >= RxBytesRemaining
1895 rxLen = edge_serial->rxBytesRemaining;
1896 edge_serial->rxState = EXPECT_HDR1; // Start another header next time
1899 bufferLength -= rxLen;
1900 edge_serial->rxBytesRemaining -= rxLen;
1902 /* spit this data back into the tty driver if this port is open */
1903 if (rxLen) {
1904 port = edge_serial->serial->port[edge_serial->rxPort];
1905 edge_port = usb_get_serial_port_data(port);
1906 if (edge_port->open) {
1907 tty = edge_port->port->tty;
1908 if (tty) {
1909 dbg("%s - Sending %d bytes to TTY for port %d", __FUNCTION__, rxLen, edge_serial->rxPort);
1910 edge_tty_recv(&edge_serial->serial->dev->dev, tty, buffer, rxLen);
1912 edge_port->icount.rx += rxLen;
1914 buffer += rxLen;
1917 break;
1919 case EXPECT_HDR3: // Expect 3rd byte of status header
1920 edge_serial->rxHeader3 = *buffer;
1921 ++buffer;
1922 --bufferLength;
1924 // We have all the header bytes, process the status now
1925 process_rcvd_status (edge_serial, edge_serial->rxStatusParam, edge_serial->rxHeader3);
1926 edge_serial->rxState = EXPECT_HDR1;
1927 break;
1934 /*****************************************************************************
1935 * process_rcvd_status
1936 * this function handles the any status messages received on the bulk in pipe.
1937 *****************************************************************************/
1938 static void process_rcvd_status (struct edgeport_serial *edge_serial, __u8 byte2, __u8 byte3)
1940 struct usb_serial_port *port;
1941 struct edgeport_port *edge_port;
1942 __u8 code = edge_serial->rxStatusCode;
1944 /* switch the port pointer to the one being currently talked about */
1945 port = edge_serial->serial->port[edge_serial->rxPort];
1946 edge_port = usb_get_serial_port_data(port);
1947 if (edge_port == NULL) {
1948 dev_err(&edge_serial->serial->dev->dev, "%s - edge_port == NULL for port %d\n", __FUNCTION__, edge_serial->rxPort);
1949 return;
1952 dbg("%s - port %d", __FUNCTION__, edge_serial->rxPort);
1954 if (code == IOSP_EXT_STATUS) {
1955 switch (byte2) {
1956 case IOSP_EXT_STATUS_CHASE_RSP:
1957 // we want to do EXT status regardless of port open/closed
1958 dbg("%s - Port %u EXT CHASE_RSP Data = %02x", __FUNCTION__, edge_serial->rxPort, byte3 );
1959 // Currently, the only EXT_STATUS is Chase, so process here instead of one more call
1960 // to one more subroutine. If/when more EXT_STATUS, there'll be more work to do.
1961 // Also, we currently clear flag and close the port regardless of content of above's Byte3.
1962 // We could choose to do something else when Byte3 says Timeout on Chase from Edgeport,
1963 // like wait longer in block_until_chase_response, but for now we don't.
1964 edge_port->chaseResponsePending = false;
1965 wake_up (&edge_port->wait_chase);
1966 return;
1968 case IOSP_EXT_STATUS_RX_CHECK_RSP:
1969 dbg("%s ========== Port %u CHECK_RSP Sequence = %02x =============\n", __FUNCTION__, edge_serial->rxPort, byte3 );
1970 //Port->RxCheckRsp = true;
1971 return;
1975 if (code == IOSP_STATUS_OPEN_RSP) {
1976 edge_port->txCredits = GET_TX_BUFFER_SIZE(byte3);
1977 edge_port->maxTxCredits = edge_port->txCredits;
1978 dbg("%s - Port %u Open Response Inital MSR = %02x TxBufferSize = %d", __FUNCTION__, edge_serial->rxPort, byte2, edge_port->txCredits);
1979 handle_new_msr (edge_port, byte2);
1981 /* send the current line settings to the port so we are in sync with any further termios calls */
1982 if (edge_port->port->tty)
1983 change_port_settings (edge_port, edge_port->port->tty->termios);
1985 /* we have completed the open */
1986 edge_port->openPending = false;
1987 edge_port->open = true;
1988 wake_up(&edge_port->wait_open);
1989 return;
1992 // If port is closed, silently discard all rcvd status. We can
1993 // have cases where buffered status is received AFTER the close
1994 // port command is sent to the Edgeport.
1995 if (!edge_port->open || edge_port->closePending) {
1996 return;
1999 switch (code) {
2000 // Not currently sent by Edgeport
2001 case IOSP_STATUS_LSR:
2002 dbg("%s - Port %u LSR Status = %02x", __FUNCTION__, edge_serial->rxPort, byte2);
2003 handle_new_lsr(edge_port, false, byte2, 0);
2004 break;
2006 case IOSP_STATUS_LSR_DATA:
2007 dbg("%s - Port %u LSR Status = %02x, Data = %02x", __FUNCTION__, edge_serial->rxPort, byte2, byte3);
2008 // byte2 is LSR Register
2009 // byte3 is broken data byte
2010 handle_new_lsr(edge_port, true, byte2, byte3);
2011 break;
2013 // case IOSP_EXT_4_STATUS:
2014 // dbg("%s - Port %u LSR Status = %02x Data = %02x", __FUNCTION__, edge_serial->rxPort, byte2, byte3);
2015 // break;
2017 case IOSP_STATUS_MSR:
2018 dbg("%s - Port %u MSR Status = %02x", __FUNCTION__, edge_serial->rxPort, byte2);
2020 // Process this new modem status and generate appropriate
2021 // events, etc, based on the new status. This routine
2022 // also saves the MSR in Port->ShadowMsr.
2023 handle_new_msr(edge_port, byte2);
2024 break;
2026 default:
2027 dbg("%s - Unrecognized IOSP status code %u\n", __FUNCTION__, code);
2028 break;
2031 return;
2035 /*****************************************************************************
2036 * edge_tty_recv
2037 * this function passes data on to the tty flip buffer
2038 *****************************************************************************/
2039 static void edge_tty_recv(struct device *dev, struct tty_struct *tty, unsigned char *data, int length)
2041 int cnt;
2043 do {
2044 cnt = tty_buffer_request_room(tty, length);
2045 if (cnt < length) {
2046 dev_err(dev, "%s - dropping data, %d bytes lost\n",
2047 __FUNCTION__, length - cnt);
2048 if(cnt == 0)
2049 break;
2051 tty_insert_flip_string(tty, data, cnt);
2052 data += cnt;
2053 length -= cnt;
2054 } while (length > 0);
2056 tty_flip_buffer_push(tty);
2060 /*****************************************************************************
2061 * handle_new_msr
2062 * this function handles any change to the msr register for a port.
2063 *****************************************************************************/
2064 static void handle_new_msr(struct edgeport_port *edge_port, __u8 newMsr)
2066 struct async_icount *icount;
2068 dbg("%s %02x", __FUNCTION__, newMsr);
2070 if (newMsr & (EDGEPORT_MSR_DELTA_CTS | EDGEPORT_MSR_DELTA_DSR | EDGEPORT_MSR_DELTA_RI | EDGEPORT_MSR_DELTA_CD)) {
2071 icount = &edge_port->icount;
2073 /* update input line counters */
2074 if (newMsr & EDGEPORT_MSR_DELTA_CTS) {
2075 icount->cts++;
2077 if (newMsr & EDGEPORT_MSR_DELTA_DSR) {
2078 icount->dsr++;
2080 if (newMsr & EDGEPORT_MSR_DELTA_CD) {
2081 icount->dcd++;
2083 if (newMsr & EDGEPORT_MSR_DELTA_RI) {
2084 icount->rng++;
2086 wake_up_interruptible(&edge_port->delta_msr_wait);
2089 /* Save the new modem status */
2090 edge_port->shadowMSR = newMsr & 0xf0;
2092 return;
2096 /*****************************************************************************
2097 * handle_new_lsr
2098 * this function handles any change to the lsr register for a port.
2099 *****************************************************************************/
2100 static void handle_new_lsr(struct edgeport_port *edge_port, __u8 lsrData, __u8 lsr, __u8 data)
2102 __u8 newLsr = (__u8)(lsr & (__u8)(LSR_OVER_ERR | LSR_PAR_ERR | LSR_FRM_ERR | LSR_BREAK));
2103 struct async_icount *icount;
2105 dbg("%s - %02x", __FUNCTION__, newLsr);
2107 edge_port->shadowLSR = lsr;
2109 if (newLsr & LSR_BREAK) {
2111 // Parity and Framing errors only count if they
2112 // occur exclusive of a break being
2113 // received.
2115 newLsr &= (__u8)(LSR_OVER_ERR | LSR_BREAK);
2118 /* Place LSR data byte into Rx buffer */
2119 if (lsrData && edge_port->port->tty)
2120 edge_tty_recv(&edge_port->port->dev, edge_port->port->tty, &data, 1);
2122 /* update input line counters */
2123 icount = &edge_port->icount;
2124 if (newLsr & LSR_BREAK) {
2125 icount->brk++;
2127 if (newLsr & LSR_OVER_ERR) {
2128 icount->overrun++;
2130 if (newLsr & LSR_PAR_ERR) {
2131 icount->parity++;
2133 if (newLsr & LSR_FRM_ERR) {
2134 icount->frame++;
2137 return;
2141 /****************************************************************************
2142 * sram_write
2143 * writes a number of bytes to the Edgeport device's sram starting at the
2144 * given address.
2145 * If successful returns the number of bytes written, otherwise it returns
2146 * a negative error number of the problem.
2147 ****************************************************************************/
2148 static int sram_write (struct usb_serial *serial, __u16 extAddr, __u16 addr, __u16 length, __u8 *data)
2150 int result;
2151 __u16 current_length;
2152 unsigned char *transfer_buffer;
2154 dbg("%s - %x, %x, %d", __FUNCTION__, extAddr, addr, length);
2156 transfer_buffer = kmalloc (64, GFP_KERNEL);
2157 if (!transfer_buffer) {
2158 dev_err(&serial->dev->dev, "%s - kmalloc(%d) failed.\n", __FUNCTION__, 64);
2159 return -ENOMEM;
2162 /* need to split these writes up into 64 byte chunks */
2163 result = 0;
2164 while (length > 0) {
2165 if (length > 64) {
2166 current_length = 64;
2167 } else {
2168 current_length = length;
2170 // dbg("%s - writing %x, %x, %d", __FUNCTION__, extAddr, addr, current_length);
2171 memcpy (transfer_buffer, data, current_length);
2172 result = usb_control_msg (serial->dev, usb_sndctrlpipe(serial->dev, 0), USB_REQUEST_ION_WRITE_RAM,
2173 0x40, addr, extAddr, transfer_buffer, current_length, 300);
2174 if (result < 0)
2175 break;
2176 length -= current_length;
2177 addr += current_length;
2178 data += current_length;
2181 kfree (transfer_buffer);
2182 return result;
2186 /****************************************************************************
2187 * rom_write
2188 * writes a number of bytes to the Edgeport device's ROM starting at the
2189 * given address.
2190 * If successful returns the number of bytes written, otherwise it returns
2191 * a negative error number of the problem.
2192 ****************************************************************************/
2193 static int rom_write (struct usb_serial *serial, __u16 extAddr, __u16 addr, __u16 length, __u8 *data)
2195 int result;
2196 __u16 current_length;
2197 unsigned char *transfer_buffer;
2199 // dbg("%s - %x, %x, %d", __FUNCTION__, extAddr, addr, length);
2201 transfer_buffer = kmalloc (64, GFP_KERNEL);
2202 if (!transfer_buffer) {
2203 dev_err(&serial->dev->dev, "%s - kmalloc(%d) failed.\n", __FUNCTION__, 64);
2204 return -ENOMEM;
2207 /* need to split these writes up into 64 byte chunks */
2208 result = 0;
2209 while (length > 0) {
2210 if (length > 64) {
2211 current_length = 64;
2212 } else {
2213 current_length = length;
2215 // dbg("%s - writing %x, %x, %d", __FUNCTION__, extAddr, addr, current_length);
2216 memcpy (transfer_buffer, data, current_length);
2217 result = usb_control_msg (serial->dev, usb_sndctrlpipe(serial->dev, 0), USB_REQUEST_ION_WRITE_ROM,
2218 0x40, addr, extAddr, transfer_buffer, current_length, 300);
2219 if (result < 0)
2220 break;
2221 length -= current_length;
2222 addr += current_length;
2223 data += current_length;
2226 kfree (transfer_buffer);
2227 return result;
2231 /****************************************************************************
2232 * rom_read
2233 * reads a number of bytes from the Edgeport device starting at the given
2234 * address.
2235 * If successful returns the number of bytes read, otherwise it returns
2236 * a negative error number of the problem.
2237 ****************************************************************************/
2238 static int rom_read (struct usb_serial *serial, __u16 extAddr, __u16 addr, __u16 length, __u8 *data)
2240 int result;
2241 __u16 current_length;
2242 unsigned char *transfer_buffer;
2244 dbg("%s - %x, %x, %d", __FUNCTION__, extAddr, addr, length);
2246 transfer_buffer = kmalloc (64, GFP_KERNEL);
2247 if (!transfer_buffer) {
2248 dev_err(&serial->dev->dev, "%s - kmalloc(%d) failed.\n", __FUNCTION__, 64);
2249 return -ENOMEM;
2252 /* need to split these reads up into 64 byte chunks */
2253 result = 0;
2254 while (length > 0) {
2255 if (length > 64) {
2256 current_length = 64;
2257 } else {
2258 current_length = length;
2260 // dbg("%s - %x, %x, %d", __FUNCTION__, extAddr, addr, current_length);
2261 result = usb_control_msg (serial->dev, usb_rcvctrlpipe(serial->dev, 0), USB_REQUEST_ION_READ_ROM,
2262 0xC0, addr, extAddr, transfer_buffer, current_length, 300);
2263 if (result < 0)
2264 break;
2265 memcpy (data, transfer_buffer, current_length);
2266 length -= current_length;
2267 addr += current_length;
2268 data += current_length;
2271 kfree (transfer_buffer);
2272 return result;
2276 /****************************************************************************
2277 * send_iosp_ext_cmd
2278 * Is used to send a IOSP message to the Edgeport device
2279 ****************************************************************************/
2280 static int send_iosp_ext_cmd (struct edgeport_port *edge_port, __u8 command, __u8 param)
2282 unsigned char *buffer;
2283 unsigned char *currentCommand;
2284 int length = 0;
2285 int status = 0;
2287 dbg("%s - %d, %d", __FUNCTION__, command, param);
2289 buffer = kmalloc (10, GFP_ATOMIC);
2290 if (!buffer) {
2291 dev_err(&edge_port->port->dev, "%s - kmalloc(%d) failed.\n", __FUNCTION__, 10);
2292 return -ENOMEM;
2295 currentCommand = buffer;
2297 MAKE_CMD_EXT_CMD (&currentCommand, &length,
2298 edge_port->port->number - edge_port->port->serial->minor,
2299 command, param);
2301 status = write_cmd_usb (edge_port, buffer, length);
2302 if (status) {
2303 /* something bad happened, let's free up the memory */
2304 kfree(buffer);
2307 return status;
2311 /*****************************************************************************
2312 * write_cmd_usb
2313 * this function writes the given buffer out to the bulk write endpoint.
2314 *****************************************************************************/
2315 static int write_cmd_usb (struct edgeport_port *edge_port, unsigned char *buffer, int length)
2317 struct edgeport_serial *edge_serial = usb_get_serial_data(edge_port->port->serial);
2318 int status = 0;
2319 struct urb *urb;
2320 int timeout;
2322 usb_serial_debug_data(debug, &edge_port->port->dev, __FUNCTION__, length, buffer);
2324 /* Allocate our next urb */
2325 urb = usb_alloc_urb (0, GFP_ATOMIC);
2326 if (!urb)
2327 return -ENOMEM;
2329 atomic_inc(&CmdUrbs);
2330 dbg("%s - ALLOCATE URB %p (outstanding %d)", __FUNCTION__, urb, atomic_read(&CmdUrbs));
2332 usb_fill_bulk_urb (urb, edge_serial->serial->dev,
2333 usb_sndbulkpipe(edge_serial->serial->dev, edge_serial->bulk_out_endpoint),
2334 buffer, length, edge_bulk_out_cmd_callback, edge_port);
2336 edge_port->commandPending = true;
2337 status = usb_submit_urb(urb, GFP_ATOMIC);
2339 if (status) {
2340 /* something went wrong */
2341 dev_err(&edge_port->port->dev, "%s - usb_submit_urb(write command) failed, status = %d\n", __FUNCTION__, status);
2342 usb_kill_urb(urb);
2343 usb_free_urb(urb);
2344 atomic_dec(&CmdUrbs);
2345 return status;
2348 // wait for command to finish
2349 timeout = COMMAND_TIMEOUT;
2350 #if 0
2351 wait_event (&edge_port->wait_command, !edge_port->commandPending);
2353 if (edge_port->commandPending) {
2354 /* command timed out */
2355 dbg("%s - command timed out", __FUNCTION__);
2356 status = -EINVAL;
2358 #endif
2359 return status;
2363 /*****************************************************************************
2364 * send_cmd_write_baud_rate
2365 * this function sends the proper command to change the baud rate of the
2366 * specified port.
2367 *****************************************************************************/
2368 static int send_cmd_write_baud_rate (struct edgeport_port *edge_port, int baudRate)
2370 struct edgeport_serial *edge_serial = usb_get_serial_data(edge_port->port->serial);
2371 unsigned char *cmdBuffer;
2372 unsigned char *currCmd;
2373 int cmdLen = 0;
2374 int divisor;
2375 int status;
2376 unsigned char number = edge_port->port->number - edge_port->port->serial->minor;
2378 if ((!edge_serial->is_epic) ||
2379 ((edge_serial->is_epic) &&
2380 (!edge_serial->epic_descriptor.Supports.IOSPSetBaudRate))) {
2381 dbg("SendCmdWriteBaudRate - NOT Setting baud rate for port = %d, baud = %d",
2382 edge_port->port->number, baudRate);
2383 return 0;
2386 dbg("%s - port = %d, baud = %d", __FUNCTION__, edge_port->port->number, baudRate);
2388 status = calc_baud_rate_divisor (baudRate, &divisor);
2389 if (status) {
2390 dev_err(&edge_port->port->dev, "%s - bad baud rate\n", __FUNCTION__);
2391 return status;
2394 // Alloc memory for the string of commands.
2395 cmdBuffer = kmalloc (0x100, GFP_ATOMIC);
2396 if (!cmdBuffer) {
2397 dev_err(&edge_port->port->dev, "%s - kmalloc(%d) failed.\n", __FUNCTION__, 0x100);
2398 return -ENOMEM;
2400 currCmd = cmdBuffer;
2402 // Enable access to divisor latch
2403 MAKE_CMD_WRITE_REG( &currCmd, &cmdLen, number, LCR, LCR_DL_ENABLE );
2405 // Write the divisor itself
2406 MAKE_CMD_WRITE_REG( &currCmd, &cmdLen, number, DLL, LOW8 (divisor) );
2407 MAKE_CMD_WRITE_REG( &currCmd, &cmdLen, number, DLM, HIGH8(divisor) );
2409 // Restore original value to disable access to divisor latch
2410 MAKE_CMD_WRITE_REG( &currCmd, &cmdLen, number, LCR, edge_port->shadowLCR);
2412 status = write_cmd_usb(edge_port, cmdBuffer, cmdLen );
2413 if (status) {
2414 /* something bad happened, let's free up the memory */
2415 kfree (cmdBuffer);
2418 return status;
2422 /*****************************************************************************
2423 * calc_baud_rate_divisor
2424 * this function calculates the proper baud rate divisor for the specified
2425 * baud rate.
2426 *****************************************************************************/
2427 static int calc_baud_rate_divisor (int baudrate, int *divisor)
2429 int i;
2430 __u16 custom;
2433 dbg("%s - %d", __FUNCTION__, baudrate);
2435 for (i = 0; i < ARRAY_SIZE(divisor_table); i++) {
2436 if ( divisor_table[i].BaudRate == baudrate ) {
2437 *divisor = divisor_table[i].Divisor;
2438 return 0;
2442 // We have tried all of the standard baud rates
2443 // lets try to calculate the divisor for this baud rate
2444 // Make sure the baud rate is reasonable
2445 if (baudrate > 50 && baudrate < 230400) {
2446 // get divisor
2447 custom = (__u16)((230400L + baudrate/2) / baudrate);
2449 *divisor = custom;
2451 dbg("%s - Baud %d = %d\n", __FUNCTION__, baudrate, custom);
2452 return 0;
2455 return -1;
2459 /*****************************************************************************
2460 * send_cmd_write_uart_register
2461 * this function builds up a uart register message and sends to to the device.
2462 *****************************************************************************/
2463 static int send_cmd_write_uart_register (struct edgeport_port *edge_port, __u8 regNum, __u8 regValue)
2465 struct edgeport_serial *edge_serial = usb_get_serial_data(edge_port->port->serial);
2466 unsigned char *cmdBuffer;
2467 unsigned char *currCmd;
2468 unsigned long cmdLen = 0;
2469 int status;
2471 dbg("%s - write to %s register 0x%02x", (regNum == MCR) ? "MCR" : "LCR", __FUNCTION__, regValue);
2473 if ((!edge_serial->is_epic) ||
2474 ((edge_serial->is_epic) &&
2475 (!edge_serial->epic_descriptor.Supports.IOSPWriteMCR) &&
2476 (regNum == MCR))) {
2477 dbg("SendCmdWriteUartReg - Not writing to MCR Register");
2478 return 0;
2481 if ((!edge_serial->is_epic) ||
2482 ((edge_serial->is_epic) &&
2483 (!edge_serial->epic_descriptor.Supports.IOSPWriteLCR) &&
2484 (regNum == LCR))) {
2485 dbg ("SendCmdWriteUartReg - Not writing to LCR Register");
2486 return 0;
2489 // Alloc memory for the string of commands.
2490 cmdBuffer = kmalloc (0x10, GFP_ATOMIC);
2491 if (cmdBuffer == NULL ) {
2492 return -ENOMEM;
2495 currCmd = cmdBuffer;
2497 // Build a cmd in the buffer to write the given register
2498 MAKE_CMD_WRITE_REG (&currCmd, &cmdLen,
2499 edge_port->port->number - edge_port->port->serial->minor,
2500 regNum, regValue);
2502 status = write_cmd_usb(edge_port, cmdBuffer, cmdLen);
2503 if (status) {
2504 /* something bad happened, let's free up the memory */
2505 kfree (cmdBuffer);
2508 return status;
2512 /*****************************************************************************
2513 * change_port_settings
2514 * This routine is called to set the UART on the device to match the specified
2515 * new settings.
2516 *****************************************************************************/
2517 #ifndef CMSPAR
2518 #define CMSPAR 0
2519 #endif
2520 static void change_port_settings (struct edgeport_port *edge_port, struct ktermios *old_termios)
2522 struct edgeport_serial *edge_serial = usb_get_serial_data(edge_port->port->serial);
2523 struct tty_struct *tty;
2524 int baud;
2525 unsigned cflag;
2526 __u8 mask = 0xff;
2527 __u8 lData;
2528 __u8 lParity;
2529 __u8 lStop;
2530 __u8 rxFlow;
2531 __u8 txFlow;
2532 int status;
2534 dbg("%s - port %d", __FUNCTION__, edge_port->port->number);
2536 if (!edge_port->open &&
2537 !edge_port->openPending) {
2538 dbg("%s - port not opened", __FUNCTION__);
2539 return;
2542 tty = edge_port->port->tty;
2543 if ((!tty) ||
2544 (!tty->termios)) {
2545 dbg("%s - no tty structures", __FUNCTION__);
2546 return;
2549 cflag = tty->termios->c_cflag;
2551 switch (cflag & CSIZE) {
2552 case CS5: lData = LCR_BITS_5; mask = 0x1f; dbg("%s - data bits = 5", __FUNCTION__); break;
2553 case CS6: lData = LCR_BITS_6; mask = 0x3f; dbg("%s - data bits = 6", __FUNCTION__); break;
2554 case CS7: lData = LCR_BITS_7; mask = 0x7f; dbg("%s - data bits = 7", __FUNCTION__); break;
2555 default:
2556 case CS8: lData = LCR_BITS_8; dbg("%s - data bits = 8", __FUNCTION__); break;
2559 lParity = LCR_PAR_NONE;
2560 if (cflag & PARENB) {
2561 if (cflag & CMSPAR) {
2562 if (cflag & PARODD) {
2563 lParity = LCR_PAR_MARK;
2564 dbg("%s - parity = mark", __FUNCTION__);
2565 } else {
2566 lParity = LCR_PAR_SPACE;
2567 dbg("%s - parity = space", __FUNCTION__);
2569 } else if (cflag & PARODD) {
2570 lParity = LCR_PAR_ODD;
2571 dbg("%s - parity = odd", __FUNCTION__);
2572 } else {
2573 lParity = LCR_PAR_EVEN;
2574 dbg("%s - parity = even", __FUNCTION__);
2576 } else {
2577 dbg("%s - parity = none", __FUNCTION__);
2580 if (cflag & CSTOPB) {
2581 lStop = LCR_STOP_2;
2582 dbg("%s - stop bits = 2", __FUNCTION__);
2583 } else {
2584 lStop = LCR_STOP_1;
2585 dbg("%s - stop bits = 1", __FUNCTION__);
2588 /* figure out the flow control settings */
2589 rxFlow = txFlow = 0x00;
2590 if (cflag & CRTSCTS) {
2591 rxFlow |= IOSP_RX_FLOW_RTS;
2592 txFlow |= IOSP_TX_FLOW_CTS;
2593 dbg("%s - RTS/CTS is enabled", __FUNCTION__);
2594 } else {
2595 dbg("%s - RTS/CTS is disabled", __FUNCTION__);
2598 /* if we are implementing XON/XOFF, set the start and stop character in the device */
2599 if (I_IXOFF(tty) || I_IXON(tty)) {
2600 unsigned char stop_char = STOP_CHAR(tty);
2601 unsigned char start_char = START_CHAR(tty);
2603 if ((!edge_serial->is_epic) ||
2604 ((edge_serial->is_epic) &&
2605 (edge_serial->epic_descriptor.Supports.IOSPSetXChar))) {
2606 send_iosp_ext_cmd(edge_port, IOSP_CMD_SET_XON_CHAR, start_char);
2607 send_iosp_ext_cmd(edge_port, IOSP_CMD_SET_XOFF_CHAR, stop_char);
2610 /* if we are implementing INBOUND XON/XOFF */
2611 if (I_IXOFF(tty)) {
2612 rxFlow |= IOSP_RX_FLOW_XON_XOFF;
2613 dbg("%s - INBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x", __FUNCTION__, start_char, stop_char);
2614 } else {
2615 dbg("%s - INBOUND XON/XOFF is disabled", __FUNCTION__);
2618 /* if we are implementing OUTBOUND XON/XOFF */
2619 if (I_IXON(tty)) {
2620 txFlow |= IOSP_TX_FLOW_XON_XOFF;
2621 dbg("%s - OUTBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x", __FUNCTION__, start_char, stop_char);
2622 } else {
2623 dbg("%s - OUTBOUND XON/XOFF is disabled", __FUNCTION__);
2627 /* Set flow control to the configured value */
2628 if ((!edge_serial->is_epic) ||
2629 ((edge_serial->is_epic) &&
2630 (edge_serial->epic_descriptor.Supports.IOSPSetRxFlow)))
2631 send_iosp_ext_cmd(edge_port, IOSP_CMD_SET_RX_FLOW, rxFlow);
2632 if ((!edge_serial->is_epic) ||
2633 ((edge_serial->is_epic) &&
2634 (edge_serial->epic_descriptor.Supports.IOSPSetTxFlow)))
2635 send_iosp_ext_cmd(edge_port, IOSP_CMD_SET_TX_FLOW, txFlow);
2638 edge_port->shadowLCR &= ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
2639 edge_port->shadowLCR |= (lData | lParity | lStop);
2641 edge_port->validDataMask = mask;
2643 /* Send the updated LCR value to the EdgePort */
2644 status = send_cmd_write_uart_register(edge_port, LCR, edge_port->shadowLCR);
2645 if (status != 0) {
2646 return;
2649 /* set up the MCR register and send it to the EdgePort */
2650 edge_port->shadowMCR = MCR_MASTER_IE;
2651 if (cflag & CBAUD) {
2652 edge_port->shadowMCR |= (MCR_DTR | MCR_RTS);
2654 status = send_cmd_write_uart_register(edge_port, MCR, edge_port->shadowMCR);
2655 if (status != 0) {
2656 return;
2659 /* Determine divisor based on baud rate */
2660 baud = tty_get_baud_rate(tty);
2661 if (!baud) {
2662 /* pick a default, any default... */
2663 baud = 9600;
2666 dbg("%s - baud rate = %d", __FUNCTION__, baud);
2667 status = send_cmd_write_baud_rate (edge_port, baud);
2669 return;
2673 /****************************************************************************
2674 * unicode_to_ascii
2675 * Turns a string from Unicode into ASCII.
2676 * Doesn't do a good job with any characters that are outside the normal
2677 * ASCII range, but it's only for debugging...
2678 * NOTE: expects the unicode in LE format
2679 ****************************************************************************/
2680 static void unicode_to_ascii(char *string, int buflen, __le16 *unicode, int unicode_size)
2682 int i;
2684 if (buflen <= 0) /* never happens, but... */
2685 return;
2686 --buflen; /* space for nul */
2688 for (i = 0; i < unicode_size; i++) {
2689 if (i >= buflen)
2690 break;
2691 string[i] = (char)(le16_to_cpu(unicode[i]));
2693 string[i] = 0x00;
2697 /****************************************************************************
2698 * get_manufacturing_desc
2699 * reads in the manufacturing descriptor and stores it into the serial
2700 * structure.
2701 ****************************************************************************/
2702 static void get_manufacturing_desc (struct edgeport_serial *edge_serial)
2704 int response;
2706 dbg("getting manufacturer descriptor");
2708 response = rom_read (edge_serial->serial, (EDGE_MANUF_DESC_ADDR & 0xffff0000) >> 16,
2709 (__u16)(EDGE_MANUF_DESC_ADDR & 0x0000ffff), EDGE_MANUF_DESC_LEN,
2710 (__u8 *)(&edge_serial->manuf_descriptor));
2712 if (response < 1) {
2713 dev_err(&edge_serial->serial->dev->dev, "error in getting manufacturer descriptor\n");
2714 } else {
2715 char string[30];
2716 dbg("**Manufacturer Descriptor");
2717 dbg(" RomSize: %dK", edge_serial->manuf_descriptor.RomSize);
2718 dbg(" RamSize: %dK", edge_serial->manuf_descriptor.RamSize);
2719 dbg(" CpuRev: %d", edge_serial->manuf_descriptor.CpuRev);
2720 dbg(" BoardRev: %d", edge_serial->manuf_descriptor.BoardRev);
2721 dbg(" NumPorts: %d", edge_serial->manuf_descriptor.NumPorts);
2722 dbg(" DescDate: %d/%d/%d", edge_serial->manuf_descriptor.DescDate[0], edge_serial->manuf_descriptor.DescDate[1], edge_serial->manuf_descriptor.DescDate[2]+1900);
2723 unicode_to_ascii(string, sizeof(string),
2724 edge_serial->manuf_descriptor.SerialNumber,
2725 edge_serial->manuf_descriptor.SerNumLength/2);
2726 dbg(" SerialNumber: %s", string);
2727 unicode_to_ascii(string, sizeof(string),
2728 edge_serial->manuf_descriptor.AssemblyNumber,
2729 edge_serial->manuf_descriptor.AssemblyNumLength/2);
2730 dbg(" AssemblyNumber: %s", string);
2731 unicode_to_ascii(string, sizeof(string),
2732 edge_serial->manuf_descriptor.OemAssyNumber,
2733 edge_serial->manuf_descriptor.OemAssyNumLength/2);
2734 dbg(" OemAssyNumber: %s", string);
2735 dbg(" UartType: %d", edge_serial->manuf_descriptor.UartType);
2736 dbg(" IonPid: %d", edge_serial->manuf_descriptor.IonPid);
2737 dbg(" IonConfig: %d", edge_serial->manuf_descriptor.IonConfig);
2742 /****************************************************************************
2743 * get_boot_desc
2744 * reads in the bootloader descriptor and stores it into the serial
2745 * structure.
2746 ****************************************************************************/
2747 static void get_boot_desc (struct edgeport_serial *edge_serial)
2749 int response;
2751 dbg("getting boot descriptor");
2753 response = rom_read (edge_serial->serial, (EDGE_BOOT_DESC_ADDR & 0xffff0000) >> 16,
2754 (__u16)(EDGE_BOOT_DESC_ADDR & 0x0000ffff), EDGE_BOOT_DESC_LEN,
2755 (__u8 *)(&edge_serial->boot_descriptor));
2757 if (response < 1) {
2758 dev_err(&edge_serial->serial->dev->dev, "error in getting boot descriptor\n");
2759 } else {
2760 dbg("**Boot Descriptor:");
2761 dbg(" BootCodeLength: %d", le16_to_cpu(edge_serial->boot_descriptor.BootCodeLength));
2762 dbg(" MajorVersion: %d", edge_serial->boot_descriptor.MajorVersion);
2763 dbg(" MinorVersion: %d", edge_serial->boot_descriptor.MinorVersion);
2764 dbg(" BuildNumber: %d", le16_to_cpu(edge_serial->boot_descriptor.BuildNumber));
2765 dbg(" Capabilities: 0x%x", le16_to_cpu(edge_serial->boot_descriptor.Capabilities));
2766 dbg(" UConfig0: %d", edge_serial->boot_descriptor.UConfig0);
2767 dbg(" UConfig1: %d", edge_serial->boot_descriptor.UConfig1);
2772 /****************************************************************************
2773 * load_application_firmware
2774 * This is called to load the application firmware to the device
2775 ****************************************************************************/
2776 static void load_application_firmware (struct edgeport_serial *edge_serial)
2778 struct edge_firmware_image_record *record;
2779 unsigned char *firmware;
2780 unsigned char *FirmwareImage;
2781 int ImageSize;
2782 int response;
2785 switch (edge_serial->product_info.iDownloadFile) {
2786 case EDGE_DOWNLOAD_FILE_I930:
2787 dbg("downloading firmware version (930) %d.%d.%d",
2788 OperationalCodeImageVersion_GEN1.MajorVersion,
2789 OperationalCodeImageVersion_GEN1.MinorVersion,
2790 OperationalCodeImageVersion_GEN1.BuildNumber);
2791 firmware = &OperationalCodeImage_GEN1[0];
2792 FirmwareImage = &OperationalCodeImage_GEN1[0];
2793 ImageSize = sizeof(OperationalCodeImage_GEN1);
2794 break;
2796 case EDGE_DOWNLOAD_FILE_80251:
2797 dbg("downloading firmware version (80251) %d.%d.%d",
2798 OperationalCodeImageVersion_GEN2.MajorVersion,
2799 OperationalCodeImageVersion_GEN2.MinorVersion,
2800 OperationalCodeImageVersion_GEN2.BuildNumber);
2801 firmware = &OperationalCodeImage_GEN2[0];
2802 FirmwareImage = &OperationalCodeImage_GEN2[0];
2803 ImageSize = sizeof(OperationalCodeImage_GEN2);
2804 break;
2806 case EDGE_DOWNLOAD_FILE_NONE:
2807 dbg ("No download file specified, skipping download\n");
2808 return;
2810 default:
2811 return;
2815 for (;;) {
2816 record = (struct edge_firmware_image_record *)firmware;
2817 response = sram_write (edge_serial->serial, le16_to_cpu(record->ExtAddr), le16_to_cpu(record->Addr), le16_to_cpu(record->Len), &record->Data[0]);
2818 if (response < 0) {
2819 dev_err(&edge_serial->serial->dev->dev, "sram_write failed (%x, %x, %d)\n", le16_to_cpu(record->ExtAddr), le16_to_cpu(record->Addr), le16_to_cpu(record->Len));
2820 break;
2822 firmware += sizeof (struct edge_firmware_image_record) + le16_to_cpu(record->Len);
2823 if (firmware >= &FirmwareImage[ImageSize]) {
2824 break;
2828 dbg("sending exec_dl_code");
2829 response = usb_control_msg (edge_serial->serial->dev,
2830 usb_sndctrlpipe(edge_serial->serial->dev, 0),
2831 USB_REQUEST_ION_EXEC_DL_CODE,
2832 0x40, 0x4000, 0x0001, NULL, 0, 3000);
2834 return;
2838 /****************************************************************************
2839 * edge_startup
2840 ****************************************************************************/
2841 static int edge_startup (struct usb_serial *serial)
2843 struct edgeport_serial *edge_serial;
2844 struct edgeport_port *edge_port;
2845 struct usb_device *dev;
2846 int i, j;
2847 int response;
2848 bool interrupt_in_found;
2849 bool bulk_in_found;
2850 bool bulk_out_found;
2851 static __u32 descriptor[3] = { EDGE_COMPATIBILITY_MASK0,
2852 EDGE_COMPATIBILITY_MASK1,
2853 EDGE_COMPATIBILITY_MASK2 };
2855 dev = serial->dev;
2857 /* create our private serial structure */
2858 edge_serial = kzalloc(sizeof(struct edgeport_serial), GFP_KERNEL);
2859 if (edge_serial == NULL) {
2860 dev_err(&serial->dev->dev, "%s - Out of memory\n", __FUNCTION__);
2861 return -ENOMEM;
2863 spin_lock_init(&edge_serial->es_lock);
2864 edge_serial->serial = serial;
2865 usb_set_serial_data(serial, edge_serial);
2867 /* get the name for the device from the device */
2868 i = get_string(dev, dev->descriptor.iManufacturer,
2869 &edge_serial->name[0], MAX_NAME_LEN+1);
2870 edge_serial->name[i++] = ' ';
2871 get_string(dev, dev->descriptor.iProduct,
2872 &edge_serial->name[i], MAX_NAME_LEN+2 - i);
2874 dev_info(&serial->dev->dev, "%s detected\n", edge_serial->name);
2876 /* Read the epic descriptor */
2877 if (get_epic_descriptor(edge_serial) <= 0) {
2878 /* memcpy descriptor to Supports structures */
2879 memcpy(&edge_serial->epic_descriptor.Supports, descriptor,
2880 sizeof(struct edge_compatibility_bits));
2882 /* get the manufacturing descriptor for this device */
2883 get_manufacturing_desc (edge_serial);
2885 /* get the boot descriptor */
2886 get_boot_desc (edge_serial);
2888 get_product_info(edge_serial);
2891 /* set the number of ports from the manufacturing description */
2892 /* serial->num_ports = serial->product_info.NumPorts; */
2893 if ((!edge_serial->is_epic) &&
2894 (edge_serial->product_info.NumPorts != serial->num_ports)) {
2895 dev_warn(&serial->dev->dev, "Device Reported %d serial ports "
2896 "vs. core thinking we have %d ports, email "
2897 "greg@kroah.com this information.",
2898 edge_serial->product_info.NumPorts,
2899 serial->num_ports);
2902 dbg("%s - time 1 %ld", __FUNCTION__, jiffies);
2904 /* If not an EPiC device */
2905 if (!edge_serial->is_epic) {
2906 /* now load the application firmware into this device */
2907 load_application_firmware (edge_serial);
2909 dbg("%s - time 2 %ld", __FUNCTION__, jiffies);
2911 /* Check current Edgeport EEPROM and update if necessary */
2912 update_edgeport_E2PROM (edge_serial);
2914 dbg("%s - time 3 %ld", __FUNCTION__, jiffies);
2916 /* set the configuration to use #1 */
2917 // dbg("set_configuration 1");
2918 // usb_set_configuration (dev, 1);
2921 /* we set up the pointers to the endpoints in the edge_open function,
2922 * as the structures aren't created yet. */
2924 /* set up our port private structures */
2925 for (i = 0; i < serial->num_ports; ++i) {
2926 edge_port = kmalloc (sizeof(struct edgeport_port), GFP_KERNEL);
2927 if (edge_port == NULL) {
2928 dev_err(&serial->dev->dev, "%s - Out of memory\n", __FUNCTION__);
2929 for (j = 0; j < i; ++j) {
2930 kfree (usb_get_serial_port_data(serial->port[j]));
2931 usb_set_serial_port_data(serial->port[j], NULL);
2933 usb_set_serial_data(serial, NULL);
2934 kfree(edge_serial);
2935 return -ENOMEM;
2937 memset (edge_port, 0, sizeof(struct edgeport_port));
2938 spin_lock_init(&edge_port->ep_lock);
2939 edge_port->port = serial->port[i];
2940 usb_set_serial_port_data(serial->port[i], edge_port);
2943 response = 0;
2945 if (edge_serial->is_epic) {
2946 /* EPIC thing, set up our interrupt polling now and our read urb, so
2947 * that the device knows it really is connected. */
2948 interrupt_in_found = bulk_in_found = bulk_out_found = false;
2949 for (i = 0; i < serial->interface->altsetting[0].desc.bNumEndpoints; ++i) {
2950 struct usb_endpoint_descriptor *endpoint;
2951 int buffer_size;
2953 endpoint = &serial->interface->altsetting[0].endpoint[i].desc;
2954 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
2955 if (!interrupt_in_found &&
2956 (usb_endpoint_is_int_in(endpoint))) {
2957 /* we found a interrupt in endpoint */
2958 dbg("found interrupt in");
2960 /* not set up yet, so do it now */
2961 edge_serial->interrupt_read_urb = usb_alloc_urb(0, GFP_KERNEL);
2962 if (!edge_serial->interrupt_read_urb) {
2963 err("out of memory");
2964 return -ENOMEM;
2966 edge_serial->interrupt_in_buffer = kmalloc(buffer_size, GFP_KERNEL);
2967 if (!edge_serial->interrupt_in_buffer) {
2968 err("out of memory");
2969 usb_free_urb(edge_serial->interrupt_read_urb);
2970 return -ENOMEM;
2972 edge_serial->interrupt_in_endpoint = endpoint->bEndpointAddress;
2974 /* set up our interrupt urb */
2975 usb_fill_int_urb(edge_serial->interrupt_read_urb,
2976 dev,
2977 usb_rcvintpipe(dev, endpoint->bEndpointAddress),
2978 edge_serial->interrupt_in_buffer,
2979 buffer_size,
2980 edge_interrupt_callback,
2981 edge_serial,
2982 endpoint->bInterval);
2984 interrupt_in_found = true;
2987 if (!bulk_in_found &&
2988 (usb_endpoint_is_bulk_in(endpoint))) {
2989 /* we found a bulk in endpoint */
2990 dbg("found bulk in");
2992 /* not set up yet, so do it now */
2993 edge_serial->read_urb = usb_alloc_urb(0, GFP_KERNEL);
2994 if (!edge_serial->read_urb) {
2995 err("out of memory");
2996 return -ENOMEM;
2998 edge_serial->bulk_in_buffer = kmalloc(buffer_size, GFP_KERNEL);
2999 if (!edge_serial->bulk_in_buffer) {
3000 err ("out of memory");
3001 usb_free_urb(edge_serial->read_urb);
3002 return -ENOMEM;
3004 edge_serial->bulk_in_endpoint = endpoint->bEndpointAddress;
3006 /* set up our bulk in urb */
3007 usb_fill_bulk_urb(edge_serial->read_urb, dev,
3008 usb_rcvbulkpipe(dev, endpoint->bEndpointAddress),
3009 edge_serial->bulk_in_buffer,
3010 endpoint->wMaxPacketSize,
3011 edge_bulk_in_callback,
3012 edge_serial);
3013 bulk_in_found = true;
3016 if (!bulk_out_found &&
3017 (usb_endpoint_is_bulk_out(endpoint))) {
3018 /* we found a bulk out endpoint */
3019 dbg("found bulk out");
3020 edge_serial->bulk_out_endpoint = endpoint->bEndpointAddress;
3021 bulk_out_found = true;
3025 if (!interrupt_in_found || !bulk_in_found || !bulk_out_found) {
3026 err ("Error - the proper endpoints were not found!");
3027 return -ENODEV;
3030 /* start interrupt read for this edgeport this interrupt will
3031 * continue as long as the edgeport is connected */
3032 response = usb_submit_urb(edge_serial->interrupt_read_urb, GFP_KERNEL);
3033 if (response)
3034 err("%s - Error %d submitting control urb", __FUNCTION__, response);
3036 return response;
3040 /****************************************************************************
3041 * edge_shutdown
3042 * This function is called whenever the device is removed from the usb bus.
3043 ****************************************************************************/
3044 static void edge_shutdown (struct usb_serial *serial)
3046 struct edgeport_serial *edge_serial = usb_get_serial_data(serial);
3047 int i;
3049 dbg("%s", __FUNCTION__);
3051 /* stop reads and writes on all ports */
3052 for (i=0; i < serial->num_ports; ++i) {
3053 kfree (usb_get_serial_port_data(serial->port[i]));
3054 usb_set_serial_port_data(serial->port[i], NULL);
3056 /* free up our endpoint stuff */
3057 if (edge_serial->is_epic) {
3058 usb_kill_urb(edge_serial->interrupt_read_urb);
3059 usb_free_urb(edge_serial->interrupt_read_urb);
3060 kfree(edge_serial->interrupt_in_buffer);
3062 usb_kill_urb(edge_serial->read_urb);
3063 usb_free_urb(edge_serial->read_urb);
3064 kfree(edge_serial->bulk_in_buffer);
3067 kfree(edge_serial);
3068 usb_set_serial_data(serial, NULL);
3072 /****************************************************************************
3073 * edgeport_init
3074 * This is called by the module subsystem, or on startup to initialize us
3075 ****************************************************************************/
3076 static int __init edgeport_init(void)
3078 int retval;
3080 retval = usb_serial_register(&edgeport_2port_device);
3081 if (retval)
3082 goto failed_2port_device_register;
3083 retval = usb_serial_register(&edgeport_4port_device);
3084 if (retval)
3085 goto failed_4port_device_register;
3086 retval = usb_serial_register(&edgeport_8port_device);
3087 if (retval)
3088 goto failed_8port_device_register;
3089 retval = usb_serial_register(&epic_device);
3090 if (retval)
3091 goto failed_epic_device_register;
3092 retval = usb_register(&io_driver);
3093 if (retval)
3094 goto failed_usb_register;
3095 atomic_set(&CmdUrbs, 0);
3096 info(DRIVER_DESC " " DRIVER_VERSION);
3097 return 0;
3099 failed_usb_register:
3100 usb_serial_deregister(&epic_device);
3101 failed_epic_device_register:
3102 usb_serial_deregister(&edgeport_8port_device);
3103 failed_8port_device_register:
3104 usb_serial_deregister(&edgeport_4port_device);
3105 failed_4port_device_register:
3106 usb_serial_deregister(&edgeport_2port_device);
3107 failed_2port_device_register:
3108 return retval;
3112 /****************************************************************************
3113 * edgeport_exit
3114 * Called when the driver is about to be unloaded.
3115 ****************************************************************************/
3116 static void __exit edgeport_exit (void)
3118 usb_deregister (&io_driver);
3119 usb_serial_deregister (&edgeport_2port_device);
3120 usb_serial_deregister (&edgeport_4port_device);
3121 usb_serial_deregister (&edgeport_8port_device);
3122 usb_serial_deregister (&epic_device);
3125 module_init(edgeport_init);
3126 module_exit(edgeport_exit);
3128 /* Module information */
3129 MODULE_AUTHOR( DRIVER_AUTHOR );
3130 MODULE_DESCRIPTION( DRIVER_DESC );
3131 MODULE_LICENSE("GPL");
3133 module_param(debug, bool, S_IRUGO | S_IWUSR);
3134 MODULE_PARM_DESC(debug, "Debug enabled or not");
3136 module_param(low_latency, bool, S_IRUGO | S_IWUSR);
3137 MODULE_PARM_DESC(low_latency, "Low latency enabled or not");