2 * Edgeport USB Serial Converter driver
4 * Copyright (C) 2000 Inside Out Networks, All rights reserved.
5 * Copyright (C) 2001-2002 Greg Kroah-Hartman <greg@kroah.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * Supports the following devices:
26 * For questions or problems with this driver, contact Inside Out
27 * Networks technical support, or Peter Berger <pberger@brimson.com>,
28 * or Al Borchers <alborchers@steinerpoint.com>.
32 #include <linux/kernel.h>
33 #include <linux/jiffies.h>
34 #include <linux/errno.h>
35 #include <linux/init.h>
36 #include <linux/slab.h>
37 #include <linux/tty.h>
38 #include <linux/tty_driver.h>
39 #include <linux/tty_flip.h>
40 #include <linux/module.h>
41 #include <linux/spinlock.h>
42 #include <linux/serial.h>
43 #include <linux/ioctl.h>
44 #include <linux/wait.h>
45 #include <linux/firmware.h>
46 #include <linux/ihex.h>
47 #include <linux/uaccess.h>
48 #include <linux/usb.h>
49 #include <linux/usb/serial.h>
50 #include "io_edgeport.h"
51 #include "io_ionsp.h" /* info for the iosp messages */
52 #include "io_16654.h" /* 16654 UART defines */
57 #define DRIVER_VERSION "v2.7"
58 #define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com> and David Iacovelli"
59 #define DRIVER_DESC "Edgeport USB Serial Driver"
61 #define MAX_NAME_LEN 64
63 #define CHASE_TIMEOUT (5*HZ) /* 5 seconds */
64 #define OPEN_TIMEOUT (5*HZ) /* 5 seconds */
65 #define COMMAND_TIMEOUT (5*HZ) /* 5 seconds */
67 /* receive port state */
69 EXPECT_HDR1 = 0, /* Expect header byte 1 */
70 EXPECT_HDR2 = 1, /* Expect header byte 2 */
71 EXPECT_DATA = 2, /* Expect 'RxBytesRemaining' data */
72 EXPECT_HDR3 = 3, /* Expect header byte 3 (for status hdrs only) */
77 * This Transmit queue is an extension of the edgeport Rx buffer.
78 * The maximum amount of data buffered in both the edgeport
79 * Rx buffer (maxTxCredits) and this buffer will never exceed maxTxCredits.
82 unsigned int head; /* index to head pointer (write) */
83 unsigned int tail; /* index to tail pointer (read) */
84 unsigned int count; /* Bytes in queue */
85 unsigned int size; /* Max size of queue (equal to Max number of TxCredits) */
86 unsigned char *fifo; /* allocated Buffer */
89 /* This structure holds all of the local port information */
90 struct edgeport_port {
91 __u16 txCredits; /* our current credits for this port */
92 __u16 maxTxCredits; /* the max size of the port */
94 struct TxFifo txfifo; /* transmit fifo -- size will be maxTxCredits */
95 struct urb *write_urb; /* write URB for this port */
96 bool write_in_progress; /* 'true' while a write URB is outstanding */
99 __u8 shadowLCR; /* last LCR value received */
100 __u8 shadowMCR; /* last MCR value received */
101 __u8 shadowMSR; /* last MSR value received */
102 __u8 shadowLSR; /* last LSR value received */
103 __u8 shadowXonChar; /* last value set as XON char in Edgeport */
104 __u8 shadowXoffChar; /* last value set as XOFF char in Edgeport */
112 bool chaseResponsePending;
114 wait_queue_head_t wait_chase; /* for handling sleeping while waiting for chase to finish */
115 wait_queue_head_t wait_open; /* for handling sleeping while waiting for open to finish */
116 wait_queue_head_t wait_command; /* for handling sleeping while waiting for command to finish */
117 wait_queue_head_t delta_msr_wait; /* for handling sleeping while waiting for msr change to happen */
119 struct async_icount icount;
120 struct usb_serial_port *port; /* loop back to the owner of this object */
124 /* This structure holds all of the individual device information */
125 struct edgeport_serial {
126 char name[MAX_NAME_LEN+2]; /* string name of this device */
128 struct edge_manuf_descriptor manuf_descriptor; /* the manufacturer descriptor */
129 struct edge_boot_descriptor boot_descriptor; /* the boot firmware descriptor */
130 struct edgeport_product_info product_info; /* Product Info */
131 struct edge_compatibility_descriptor epic_descriptor; /* Edgeport compatible descriptor */
132 int is_epic; /* flag if EPiC device or not */
134 __u8 interrupt_in_endpoint; /* the interrupt endpoint handle */
135 unsigned char *interrupt_in_buffer; /* the buffer we use for the interrupt endpoint */
136 struct urb *interrupt_read_urb; /* our interrupt urb */
138 __u8 bulk_in_endpoint; /* the bulk in endpoint handle */
139 unsigned char *bulk_in_buffer; /* the buffer we use for the bulk in endpoint */
140 struct urb *read_urb; /* our bulk read urb */
141 bool read_in_progress;
144 __u8 bulk_out_endpoint; /* the bulk out endpoint handle */
146 __s16 rxBytesAvail; /* the number of bytes that we need to read from this device */
148 enum RXSTATE rxState; /* the current state of the bulk receive processor */
149 __u8 rxHeader1; /* receive header byte 1 */
150 __u8 rxHeader2; /* receive header byte 2 */
151 __u8 rxHeader3; /* receive header byte 3 */
152 __u8 rxPort; /* the port that we are currently receiving data for */
153 __u8 rxStatusCode; /* the receive status code */
154 __u8 rxStatusParam; /* the receive status paramater */
155 __s16 rxBytesRemaining; /* the number of port bytes left to read */
156 struct usb_serial *serial; /* loop back to the owner of this object */
159 /* baud rate information */
160 struct divisor_table_entry {
166 * Define table of divisors for Rev A EdgePort/4 hardware
167 * These assume a 3.6864MHz crystal, the standard /16, and
171 static const struct divisor_table_entry divisor_table[] = {
174 { 110, 2095}, /* 2094.545455 => 230450 => .0217 % over */
175 { 134, 1713}, /* 1713.011152 => 230398.5 => .00065% under */
193 /* local variables */
196 /* Number of outstanding Command Write Urbs */
197 static atomic_t CmdUrbs = ATOMIC_INIT(0);
200 /* local function prototypes */
202 /* function prototypes for all URB callbacks */
203 static void edge_interrupt_callback(struct urb *urb);
204 static void edge_bulk_in_callback(struct urb *urb);
205 static void edge_bulk_out_data_callback(struct urb *urb);
206 static void edge_bulk_out_cmd_callback(struct urb *urb);
208 /* function prototypes for the usbserial callbacks */
209 static int edge_open(struct tty_struct *tty, struct usb_serial_port *port);
210 static void edge_close(struct usb_serial_port *port);
211 static int edge_write(struct tty_struct *tty, struct usb_serial_port *port,
212 const unsigned char *buf, int count);
213 static int edge_write_room(struct tty_struct *tty);
214 static int edge_chars_in_buffer(struct tty_struct *tty);
215 static void edge_throttle(struct tty_struct *tty);
216 static void edge_unthrottle(struct tty_struct *tty);
217 static void edge_set_termios(struct tty_struct *tty,
218 struct usb_serial_port *port,
219 struct ktermios *old_termios);
220 static int edge_ioctl(struct tty_struct *tty,
221 unsigned int cmd, unsigned long arg);
222 static void edge_break(struct tty_struct *tty, int break_state);
223 static int edge_tiocmget(struct tty_struct *tty);
224 static int edge_tiocmset(struct tty_struct *tty,
225 unsigned int set, unsigned int clear);
226 static int edge_get_icount(struct tty_struct *tty,
227 struct serial_icounter_struct *icount);
228 static int edge_startup(struct usb_serial *serial);
229 static void edge_disconnect(struct usb_serial *serial);
230 static void edge_release(struct usb_serial *serial);
232 #include "io_tables.h" /* all of the devices that this driver supports */
234 /* function prototypes for all of our local functions */
236 static void process_rcvd_data(struct edgeport_serial *edge_serial,
237 unsigned char *buffer, __u16 bufferLength);
238 static void process_rcvd_status(struct edgeport_serial *edge_serial,
239 __u8 byte2, __u8 byte3);
240 static void edge_tty_recv(struct device *dev, struct tty_struct *tty,
241 unsigned char *data, int length);
242 static void handle_new_msr(struct edgeport_port *edge_port, __u8 newMsr);
243 static void handle_new_lsr(struct edgeport_port *edge_port, __u8 lsrData,
244 __u8 lsr, __u8 data);
245 static int send_iosp_ext_cmd(struct edgeport_port *edge_port, __u8 command,
247 static int calc_baud_rate_divisor(struct device *dev, int baud_rate, int *divisor);
248 static int send_cmd_write_baud_rate(struct edgeport_port *edge_port,
250 static void change_port_settings(struct tty_struct *tty,
251 struct edgeport_port *edge_port,
252 struct ktermios *old_termios);
253 static int send_cmd_write_uart_register(struct edgeport_port *edge_port,
254 __u8 regNum, __u8 regValue);
255 static int write_cmd_usb(struct edgeport_port *edge_port,
256 unsigned char *buffer, int writeLength);
257 static void send_more_port_data(struct edgeport_serial *edge_serial,
258 struct edgeport_port *edge_port);
260 static int sram_write(struct usb_serial *serial, __u16 extAddr, __u16 addr,
261 __u16 length, const __u8 *data);
262 static int rom_read(struct usb_serial *serial, __u16 extAddr, __u16 addr,
263 __u16 length, __u8 *data);
264 static int rom_write(struct usb_serial *serial, __u16 extAddr, __u16 addr,
265 __u16 length, const __u8 *data);
266 static void get_manufacturing_desc(struct edgeport_serial *edge_serial);
267 static void get_boot_desc(struct edgeport_serial *edge_serial);
268 static void load_application_firmware(struct edgeport_serial *edge_serial);
270 static void unicode_to_ascii(char *string, int buflen,
271 __le16 *unicode, int unicode_size);
274 /* ************************************************************************ */
275 /* ************************************************************************ */
276 /* ************************************************************************ */
277 /* ************************************************************************ */
279 /************************************************************************
281 * update_edgeport_E2PROM() Compare current versions of *
282 * Boot ROM and Manufacture *
283 * Descriptors with versions *
284 * embedded in this driver *
286 ************************************************************************/
287 static void update_edgeport_E2PROM(struct edgeport_serial *edge_serial)
289 struct device *dev = &edge_serial->serial->dev->dev;
292 __u8 BootMajorVersion;
293 __u8 BootMinorVersion;
294 __u16 BootBuildNumber;
296 const struct ihex_binrec *rec;
297 const struct firmware *fw;
301 switch (edge_serial->product_info.iDownloadFile) {
302 case EDGE_DOWNLOAD_FILE_I930:
303 fw_name = "edgeport/boot.fw";
305 case EDGE_DOWNLOAD_FILE_80251:
306 fw_name = "edgeport/boot2.fw";
312 response = request_ihex_firmware(&fw, fw_name,
313 &edge_serial->serial->dev->dev);
315 dev_err(dev, "Failed to load image \"%s\" err %d\n",
320 rec = (const struct ihex_binrec *)fw->data;
321 BootMajorVersion = rec->data[0];
322 BootMinorVersion = rec->data[1];
323 BootBuildNumber = (rec->data[2] << 8) | rec->data[3];
325 /* Check Boot Image Version */
326 BootCurVer = (edge_serial->boot_descriptor.MajorVersion << 24) +
327 (edge_serial->boot_descriptor.MinorVersion << 16) +
328 le16_to_cpu(edge_serial->boot_descriptor.BuildNumber);
330 BootNewVer = (BootMajorVersion << 24) +
331 (BootMinorVersion << 16) +
334 dev_dbg(dev, "Current Boot Image version %d.%d.%d\n",
335 edge_serial->boot_descriptor.MajorVersion,
336 edge_serial->boot_descriptor.MinorVersion,
337 le16_to_cpu(edge_serial->boot_descriptor.BuildNumber));
340 if (BootNewVer > BootCurVer) {
341 dev_dbg(dev, "**Update Boot Image from %d.%d.%d to %d.%d.%d\n",
342 edge_serial->boot_descriptor.MajorVersion,
343 edge_serial->boot_descriptor.MinorVersion,
344 le16_to_cpu(edge_serial->boot_descriptor.BuildNumber),
345 BootMajorVersion, BootMinorVersion, BootBuildNumber);
347 dev_dbg(dev, "Downloading new Boot Image\n");
349 for (rec = ihex_next_binrec(rec); rec;
350 rec = ihex_next_binrec(rec)) {
351 Bootaddr = be32_to_cpu(rec->addr);
352 response = rom_write(edge_serial->serial,
355 be16_to_cpu(rec->len),
358 dev_err(&edge_serial->serial->dev->dev,
359 "rom_write failed (%x, %x, %d)\n",
360 Bootaddr >> 16, Bootaddr & 0xFFFF,
361 be16_to_cpu(rec->len));
366 dev_dbg(dev, "Boot Image -- already up to date\n");
368 release_firmware(fw);
372 /************************************************************************
374 * Get string descriptor from device
376 ************************************************************************/
377 static int get_string_desc(struct usb_device *dev, int Id,
378 struct usb_string_descriptor **pRetDesc)
380 struct usb_string_descriptor StringDesc;
381 struct usb_string_descriptor *pStringDesc;
383 dev_dbg(&dev->dev, "%s - USB String ID = %d\n", __func__, Id);
385 if (!usb_get_descriptor(dev, USB_DT_STRING, Id, &StringDesc,
389 pStringDesc = kmalloc(StringDesc.bLength, GFP_KERNEL);
393 if (!usb_get_descriptor(dev, USB_DT_STRING, Id, pStringDesc,
394 StringDesc.bLength)) {
399 *pRetDesc = pStringDesc;
404 static void dump_product_info(struct edgeport_serial *edge_serial,
405 struct edgeport_product_info *product_info)
407 struct device *dev = &edge_serial->serial->dev->dev;
409 /* Dump Product Info structure */
410 dev_dbg(dev, "**Product Information:\n");
411 dev_dbg(dev, " ProductId %x\n", product_info->ProductId);
412 dev_dbg(dev, " NumPorts %d\n", product_info->NumPorts);
413 dev_dbg(dev, " ProdInfoVer %d\n", product_info->ProdInfoVer);
414 dev_dbg(dev, " IsServer %d\n", product_info->IsServer);
415 dev_dbg(dev, " IsRS232 %d\n", product_info->IsRS232);
416 dev_dbg(dev, " IsRS422 %d\n", product_info->IsRS422);
417 dev_dbg(dev, " IsRS485 %d\n", product_info->IsRS485);
418 dev_dbg(dev, " RomSize %d\n", product_info->RomSize);
419 dev_dbg(dev, " RamSize %d\n", product_info->RamSize);
420 dev_dbg(dev, " CpuRev %x\n", product_info->CpuRev);
421 dev_dbg(dev, " BoardRev %x\n", product_info->BoardRev);
422 dev_dbg(dev, " BootMajorVersion %d.%d.%d\n",
423 product_info->BootMajorVersion,
424 product_info->BootMinorVersion,
425 le16_to_cpu(product_info->BootBuildNumber));
426 dev_dbg(dev, " FirmwareMajorVersion %d.%d.%d\n",
427 product_info->FirmwareMajorVersion,
428 product_info->FirmwareMinorVersion,
429 le16_to_cpu(product_info->FirmwareBuildNumber));
430 dev_dbg(dev, " ManufactureDescDate %d/%d/%d\n",
431 product_info->ManufactureDescDate[0],
432 product_info->ManufactureDescDate[1],
433 product_info->ManufactureDescDate[2]+1900);
434 dev_dbg(dev, " iDownloadFile 0x%x\n",
435 product_info->iDownloadFile);
436 dev_dbg(dev, " EpicVer %d\n", product_info->EpicVer);
439 static void get_product_info(struct edgeport_serial *edge_serial)
441 struct edgeport_product_info *product_info = &edge_serial->product_info;
443 memset(product_info, 0, sizeof(struct edgeport_product_info));
445 product_info->ProductId = (__u16)(le16_to_cpu(edge_serial->serial->dev->descriptor.idProduct) & ~ION_DEVICE_ID_80251_NETCHIP);
446 product_info->NumPorts = edge_serial->manuf_descriptor.NumPorts;
447 product_info->ProdInfoVer = 0;
449 product_info->RomSize = edge_serial->manuf_descriptor.RomSize;
450 product_info->RamSize = edge_serial->manuf_descriptor.RamSize;
451 product_info->CpuRev = edge_serial->manuf_descriptor.CpuRev;
452 product_info->BoardRev = edge_serial->manuf_descriptor.BoardRev;
454 product_info->BootMajorVersion =
455 edge_serial->boot_descriptor.MajorVersion;
456 product_info->BootMinorVersion =
457 edge_serial->boot_descriptor.MinorVersion;
458 product_info->BootBuildNumber =
459 edge_serial->boot_descriptor.BuildNumber;
461 memcpy(product_info->ManufactureDescDate,
462 edge_serial->manuf_descriptor.DescDate,
463 sizeof(edge_serial->manuf_descriptor.DescDate));
465 /* check if this is 2nd generation hardware */
466 if (le16_to_cpu(edge_serial->serial->dev->descriptor.idProduct)
467 & ION_DEVICE_ID_80251_NETCHIP)
468 product_info->iDownloadFile = EDGE_DOWNLOAD_FILE_80251;
470 product_info->iDownloadFile = EDGE_DOWNLOAD_FILE_I930;
472 /* Determine Product type and set appropriate flags */
473 switch (DEVICE_ID_FROM_USB_PRODUCT_ID(product_info->ProductId)) {
474 case ION_DEVICE_ID_EDGEPORT_COMPATIBLE:
475 case ION_DEVICE_ID_EDGEPORT_4T:
476 case ION_DEVICE_ID_EDGEPORT_4:
477 case ION_DEVICE_ID_EDGEPORT_2:
478 case ION_DEVICE_ID_EDGEPORT_8_DUAL_CPU:
479 case ION_DEVICE_ID_EDGEPORT_8:
480 case ION_DEVICE_ID_EDGEPORT_421:
481 case ION_DEVICE_ID_EDGEPORT_21:
482 case ION_DEVICE_ID_EDGEPORT_2_DIN:
483 case ION_DEVICE_ID_EDGEPORT_4_DIN:
484 case ION_DEVICE_ID_EDGEPORT_16_DUAL_CPU:
485 product_info->IsRS232 = 1;
488 case ION_DEVICE_ID_EDGEPORT_2I: /* Edgeport/2 RS422/RS485 */
489 product_info->IsRS422 = 1;
490 product_info->IsRS485 = 1;
493 case ION_DEVICE_ID_EDGEPORT_8I: /* Edgeport/4 RS422 */
494 case ION_DEVICE_ID_EDGEPORT_4I: /* Edgeport/4 RS422 */
495 product_info->IsRS422 = 1;
499 dump_product_info(edge_serial, product_info);
502 static int get_epic_descriptor(struct edgeport_serial *ep)
505 struct usb_serial *serial = ep->serial;
506 struct edgeport_product_info *product_info = &ep->product_info;
507 struct edge_compatibility_descriptor *epic = &ep->epic_descriptor;
508 struct edge_compatibility_bits *bits;
509 struct device *dev = &serial->dev->dev;
512 result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
513 USB_REQUEST_ION_GET_EPIC_DESC,
515 &ep->epic_descriptor,
516 sizeof(struct edge_compatibility_descriptor),
521 memset(product_info, 0, sizeof(struct edgeport_product_info));
523 product_info->NumPorts = epic->NumPorts;
524 product_info->ProdInfoVer = 0;
525 product_info->FirmwareMajorVersion = epic->MajorVersion;
526 product_info->FirmwareMinorVersion = epic->MinorVersion;
527 product_info->FirmwareBuildNumber = epic->BuildNumber;
528 product_info->iDownloadFile = epic->iDownloadFile;
529 product_info->EpicVer = epic->EpicVer;
530 product_info->Epic = epic->Supports;
531 product_info->ProductId = ION_DEVICE_ID_EDGEPORT_COMPATIBLE;
532 dump_product_info(ep, product_info);
534 bits = &ep->epic_descriptor.Supports;
535 dev_dbg(dev, "**EPIC descriptor:\n");
536 dev_dbg(dev, " VendEnableSuspend: %s\n", bits->VendEnableSuspend ? "TRUE": "FALSE");
537 dev_dbg(dev, " IOSPOpen : %s\n", bits->IOSPOpen ? "TRUE": "FALSE");
538 dev_dbg(dev, " IOSPClose : %s\n", bits->IOSPClose ? "TRUE": "FALSE");
539 dev_dbg(dev, " IOSPChase : %s\n", bits->IOSPChase ? "TRUE": "FALSE");
540 dev_dbg(dev, " IOSPSetRxFlow : %s\n", bits->IOSPSetRxFlow ? "TRUE": "FALSE");
541 dev_dbg(dev, " IOSPSetTxFlow : %s\n", bits->IOSPSetTxFlow ? "TRUE": "FALSE");
542 dev_dbg(dev, " IOSPSetXChar : %s\n", bits->IOSPSetXChar ? "TRUE": "FALSE");
543 dev_dbg(dev, " IOSPRxCheck : %s\n", bits->IOSPRxCheck ? "TRUE": "FALSE");
544 dev_dbg(dev, " IOSPSetClrBreak : %s\n", bits->IOSPSetClrBreak ? "TRUE": "FALSE");
545 dev_dbg(dev, " IOSPWriteMCR : %s\n", bits->IOSPWriteMCR ? "TRUE": "FALSE");
546 dev_dbg(dev, " IOSPWriteLCR : %s\n", bits->IOSPWriteLCR ? "TRUE": "FALSE");
547 dev_dbg(dev, " IOSPSetBaudRate : %s\n", bits->IOSPSetBaudRate ? "TRUE": "FALSE");
548 dev_dbg(dev, " TrueEdgeport : %s\n", bits->TrueEdgeport ? "TRUE": "FALSE");
555 /************************************************************************/
556 /************************************************************************/
557 /* U S B C A L L B A C K F U N C T I O N S */
558 /* U S B C A L L B A C K F U N C T I O N S */
559 /************************************************************************/
560 /************************************************************************/
562 /*****************************************************************************
563 * edge_interrupt_callback
564 * this is the callback function for when we have received data on the
565 * interrupt endpoint.
566 *****************************************************************************/
567 static void edge_interrupt_callback(struct urb *urb)
569 struct edgeport_serial *edge_serial = urb->context;
571 struct edgeport_port *edge_port;
572 struct usb_serial_port *port;
573 struct tty_struct *tty;
574 unsigned char *data = urb->transfer_buffer;
575 int length = urb->actual_length;
581 int status = urb->status;
590 /* this urb is terminated, clean up */
591 dev_dbg(&urb->dev->dev, "%s - urb shutting down with status: %d\n", __func__, status);
594 dev_dbg(&urb->dev->dev, "%s - nonzero urb status received: %d\n", __func__, status);
598 dev = &edge_serial->serial->dev->dev;
600 /* process this interrupt-read even if there are no ports open */
602 usb_serial_debug_data(dev, __func__, length, data);
605 bytes_avail = data[0] | (data[1] << 8);
607 spin_lock(&edge_serial->es_lock);
608 edge_serial->rxBytesAvail += bytes_avail;
610 "%s - bytes_avail=%d, rxBytesAvail=%d, read_in_progress=%d\n",
611 __func__, bytes_avail,
612 edge_serial->rxBytesAvail,
613 edge_serial->read_in_progress);
615 if (edge_serial->rxBytesAvail > 0 &&
616 !edge_serial->read_in_progress) {
617 dev_dbg(dev, "%s - posting a read\n", __func__);
618 edge_serial->read_in_progress = true;
620 /* we have pending bytes on the
621 bulk in pipe, send a request */
622 result = usb_submit_urb(edge_serial->read_urb, GFP_ATOMIC);
625 "%s - usb_submit_urb(read bulk) failed with result = %d\n",
627 edge_serial->read_in_progress = false;
630 spin_unlock(&edge_serial->es_lock);
633 /* grab the txcredits for the ports if available */
636 while ((position < length) &&
637 (portNumber < edge_serial->serial->num_ports)) {
638 txCredits = data[position] | (data[position+1] << 8);
640 port = edge_serial->serial->port[portNumber];
641 edge_port = usb_get_serial_port_data(port);
642 if (edge_port->open) {
643 spin_lock(&edge_port->ep_lock);
644 edge_port->txCredits += txCredits;
645 spin_unlock(&edge_port->ep_lock);
646 dev_dbg(dev, "%s - txcredits for port%d = %d\n",
647 __func__, portNumber,
648 edge_port->txCredits);
650 /* tell the tty driver that something
652 tty = tty_port_tty_get(
653 &edge_port->port->port);
658 /* Since we have more credit, check
659 if more data can be sent */
660 send_more_port_data(edge_serial,
670 result = usb_submit_urb(urb, GFP_ATOMIC);
672 dev_err(&urb->dev->dev,
673 "%s - Error %d submitting control urb\n",
678 /*****************************************************************************
679 * edge_bulk_in_callback
680 * this is the callback function for when we have received data on the
682 *****************************************************************************/
683 static void edge_bulk_in_callback(struct urb *urb)
685 struct edgeport_serial *edge_serial = urb->context;
687 unsigned char *data = urb->transfer_buffer;
689 __u16 raw_data_length;
690 int status = urb->status;
693 dev_dbg(&urb->dev->dev, "%s - nonzero read bulk status received: %d\n",
695 edge_serial->read_in_progress = false;
699 if (urb->actual_length == 0) {
700 dev_dbg(&urb->dev->dev, "%s - read bulk callback with no data\n", __func__);
701 edge_serial->read_in_progress = false;
705 dev = &edge_serial->serial->dev->dev;
706 raw_data_length = urb->actual_length;
708 usb_serial_debug_data(dev, __func__, raw_data_length, data);
710 spin_lock(&edge_serial->es_lock);
712 /* decrement our rxBytes available by the number that we just got */
713 edge_serial->rxBytesAvail -= raw_data_length;
715 dev_dbg(dev, "%s - Received = %d, rxBytesAvail %d\n", __func__,
716 raw_data_length, edge_serial->rxBytesAvail);
718 process_rcvd_data(edge_serial, data, urb->actual_length);
720 /* check to see if there's any more data for us to read */
721 if (edge_serial->rxBytesAvail > 0) {
722 dev_dbg(dev, "%s - posting a read\n", __func__);
723 retval = usb_submit_urb(edge_serial->read_urb, GFP_ATOMIC);
726 "%s - usb_submit_urb(read bulk) failed, retval = %d\n",
728 edge_serial->read_in_progress = false;
731 edge_serial->read_in_progress = false;
734 spin_unlock(&edge_serial->es_lock);
738 /*****************************************************************************
739 * edge_bulk_out_data_callback
740 * this is the callback function for when we have finished sending
741 * serial data on the bulk out endpoint.
742 *****************************************************************************/
743 static void edge_bulk_out_data_callback(struct urb *urb)
745 struct edgeport_port *edge_port = urb->context;
746 struct tty_struct *tty;
747 int status = urb->status;
750 dev_dbg(&urb->dev->dev,
751 "%s - nonzero write bulk status received: %d\n",
755 tty = tty_port_tty_get(&edge_port->port->port);
757 if (tty && edge_port->open) {
758 /* let the tty driver wakeup if it has a special
759 write_wakeup function */
764 /* Release the Write URB */
765 edge_port->write_in_progress = false;
767 /* Check if more data needs to be sent */
768 send_more_port_data((struct edgeport_serial *)
769 (usb_get_serial_data(edge_port->port->serial)), edge_port);
773 /*****************************************************************************
775 * this is the callback function for when we have finished sending a
776 * command on the bulk out endpoint.
777 *****************************************************************************/
778 static void edge_bulk_out_cmd_callback(struct urb *urb)
780 struct edgeport_port *edge_port = urb->context;
781 struct tty_struct *tty;
782 int status = urb->status;
784 atomic_dec(&CmdUrbs);
785 dev_dbg(&urb->dev->dev, "%s - FREE URB %p (outstanding %d)\n",
786 __func__, urb, atomic_read(&CmdUrbs));
789 /* clean up the transfer buffer */
790 kfree(urb->transfer_buffer);
792 /* Free the command urb */
796 dev_dbg(&urb->dev->dev,
797 "%s - nonzero write bulk status received: %d\n",
802 /* Get pointer to tty */
803 tty = tty_port_tty_get(&edge_port->port->port);
805 /* tell the tty driver that something has changed */
806 if (tty && edge_port->open)
810 /* we have completed the command */
811 edge_port->commandPending = false;
812 wake_up(&edge_port->wait_command);
816 /*****************************************************************************
817 * Driver tty interface functions
818 *****************************************************************************/
820 /*****************************************************************************
822 * this function is called by the tty driver when a port is opened
823 * If successful, we return 0
824 * Otherwise we return a negative error number.
825 *****************************************************************************/
826 static int edge_open(struct tty_struct *tty, struct usb_serial_port *port)
828 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
829 struct device *dev = &port->dev;
830 struct usb_serial *serial;
831 struct edgeport_serial *edge_serial;
834 if (edge_port == NULL)
837 /* see if we've set up our endpoint info yet (can't set it up
838 in edge_startup as the structures were not set up at that time.) */
839 serial = port->serial;
840 edge_serial = usb_get_serial_data(serial);
841 if (edge_serial == NULL)
843 if (edge_serial->interrupt_in_buffer == NULL) {
844 struct usb_serial_port *port0 = serial->port[0];
846 /* not set up yet, so do it now */
847 edge_serial->interrupt_in_buffer =
848 port0->interrupt_in_buffer;
849 edge_serial->interrupt_in_endpoint =
850 port0->interrupt_in_endpointAddress;
851 edge_serial->interrupt_read_urb = port0->interrupt_in_urb;
852 edge_serial->bulk_in_buffer = port0->bulk_in_buffer;
853 edge_serial->bulk_in_endpoint =
854 port0->bulk_in_endpointAddress;
855 edge_serial->read_urb = port0->read_urb;
856 edge_serial->bulk_out_endpoint =
857 port0->bulk_out_endpointAddress;
859 /* set up our interrupt urb */
860 usb_fill_int_urb(edge_serial->interrupt_read_urb,
862 usb_rcvintpipe(serial->dev,
863 port0->interrupt_in_endpointAddress),
864 port0->interrupt_in_buffer,
865 edge_serial->interrupt_read_urb->transfer_buffer_length,
866 edge_interrupt_callback, edge_serial,
867 edge_serial->interrupt_read_urb->interval);
869 /* set up our bulk in urb */
870 usb_fill_bulk_urb(edge_serial->read_urb, serial->dev,
871 usb_rcvbulkpipe(serial->dev,
872 port0->bulk_in_endpointAddress),
873 port0->bulk_in_buffer,
874 edge_serial->read_urb->transfer_buffer_length,
875 edge_bulk_in_callback, edge_serial);
876 edge_serial->read_in_progress = false;
878 /* start interrupt read for this edgeport
879 * this interrupt will continue as long
880 * as the edgeport is connected */
881 response = usb_submit_urb(edge_serial->interrupt_read_urb,
884 dev_err(dev, "%s - Error %d submitting control urb\n",
889 /* initialize our wait queues */
890 init_waitqueue_head(&edge_port->wait_open);
891 init_waitqueue_head(&edge_port->wait_chase);
892 init_waitqueue_head(&edge_port->delta_msr_wait);
893 init_waitqueue_head(&edge_port->wait_command);
895 /* initialize our icount structure */
896 memset(&(edge_port->icount), 0x00, sizeof(edge_port->icount));
898 /* initialize our port settings */
899 edge_port->txCredits = 0; /* Can't send any data yet */
900 /* Must always set this bit to enable ints! */
901 edge_port->shadowMCR = MCR_MASTER_IE;
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);
910 dev_err(dev, "%s - error sending open port command\n", __func__);
911 edge_port->openPending = false;
915 /* now wait for the port to be completely opened */
916 wait_event_timeout(edge_port->wait_open, !edge_port->openPending,
919 if (!edge_port->open) {
921 dev_dbg(dev, "%s - open timedout\n", __func__);
922 edge_port->openPending = false;
926 /* create the txfifo */
927 edge_port->txfifo.head = 0;
928 edge_port->txfifo.tail = 0;
929 edge_port->txfifo.count = 0;
930 edge_port->txfifo.size = edge_port->maxTxCredits;
931 edge_port->txfifo.fifo = kmalloc(edge_port->maxTxCredits, GFP_KERNEL);
933 if (!edge_port->txfifo.fifo) {
934 dev_dbg(dev, "%s - no memory\n", __func__);
939 /* Allocate a URB for the write */
940 edge_port->write_urb = usb_alloc_urb(0, GFP_KERNEL);
941 edge_port->write_in_progress = false;
943 if (!edge_port->write_urb) {
944 dev_dbg(dev, "%s - no memory\n", __func__);
949 dev_dbg(dev, "%s(%d) - Initialize TX fifo to %d bytes\n",
950 __func__, port->number, edge_port->maxTxCredits);
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 timeout 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 struct device *dev = &edge_port->port->dev;
975 /* Save Last credits */
976 lastCredits = edge_port->txCredits;
978 /* Did we get our Chase response */
979 if (!edge_port->chaseResponsePending) {
980 dev_dbg(dev, "%s - Got Chase Response\n", __func__);
982 /* did we get all of our credit back? */
983 if (edge_port->txCredits == edge_port->maxTxCredits) {
984 dev_dbg(dev, "%s - Got all credits\n", __func__);
989 /* Block the thread for a while */
990 prepare_to_wait(&edge_port->wait_chase, &wait,
991 TASK_UNINTERRUPTIBLE);
992 schedule_timeout(timeout);
993 finish_wait(&edge_port->wait_chase, &wait);
995 if (lastCredits == edge_port->txCredits) {
996 /* No activity.. count down. */
999 edge_port->chaseResponsePending = false;
1000 dev_dbg(dev, "%s - Chase TIMEOUT\n", __func__);
1004 /* Reset timeout value back to 10 seconds */
1005 dev_dbg(dev, "%s - Last %d, Current %d\n", __func__,
1006 lastCredits, edge_port->txCredits);
1013 /************************************************************************
1015 * block_until_tx_empty
1017 * This function will block the close until one of the following:
1019 * 2. The edgeport has stopped
1020 * 3. A timeout of 3 seconds without activity has expired
1022 ************************************************************************/
1023 static void block_until_tx_empty(struct edgeport_port *edge_port)
1025 struct device *dev = &edge_port->port->dev;
1027 struct TxFifo *fifo = &edge_port->txfifo;
1029 int timeout = HZ/10;
1033 /* Save Last count */
1034 lastCount = fifo->count;
1036 /* Is the Edgeport Buffer empty? */
1037 if (lastCount == 0) {
1038 dev_dbg(dev, "%s - TX Buffer Empty\n", __func__);
1042 /* Block the thread for a while */
1043 prepare_to_wait(&edge_port->wait_chase, &wait,
1044 TASK_UNINTERRUPTIBLE);
1045 schedule_timeout(timeout);
1046 finish_wait(&edge_port->wait_chase, &wait);
1048 dev_dbg(dev, "%s wait\n", __func__);
1050 if (lastCount == fifo->count) {
1051 /* No activity.. count down. */
1054 dev_dbg(dev, "%s - TIMEOUT\n", __func__);
1058 /* Reset timeout value back to seconds */
1065 /*****************************************************************************
1067 * this function is called by the tty driver when a port is closed
1068 *****************************************************************************/
1069 static void edge_close(struct usb_serial_port *port)
1071 struct edgeport_serial *edge_serial;
1072 struct edgeport_port *edge_port;
1075 edge_serial = usb_get_serial_data(port->serial);
1076 edge_port = usb_get_serial_port_data(port);
1077 if (edge_serial == NULL || edge_port == NULL)
1080 /* block until tx is empty */
1081 block_until_tx_empty(edge_port);
1083 edge_port->closePending = true;
1085 if ((!edge_serial->is_epic) ||
1086 ((edge_serial->is_epic) &&
1087 (edge_serial->epic_descriptor.Supports.IOSPChase))) {
1088 /* flush and chase */
1089 edge_port->chaseResponsePending = true;
1091 dev_dbg(&port->dev, "%s - Sending IOSP_CMD_CHASE_PORT\n", __func__);
1092 status = send_iosp_ext_cmd(edge_port, IOSP_CMD_CHASE_PORT, 0);
1094 /* block until chase finished */
1095 block_until_chase_response(edge_port);
1097 edge_port->chaseResponsePending = false;
1100 if ((!edge_serial->is_epic) ||
1101 ((edge_serial->is_epic) &&
1102 (edge_serial->epic_descriptor.Supports.IOSPClose))) {
1103 /* close the port */
1104 dev_dbg(&port->dev, "%s - Sending IOSP_CMD_CLOSE_PORT\n", __func__);
1105 send_iosp_ext_cmd(edge_port, IOSP_CMD_CLOSE_PORT, 0);
1108 /* port->close = true; */
1109 edge_port->closePending = false;
1110 edge_port->open = false;
1111 edge_port->openPending = false;
1113 usb_kill_urb(edge_port->write_urb);
1115 if (edge_port->write_urb) {
1116 /* if this urb had a transfer buffer already
1117 (old transfer) free it */
1118 kfree(edge_port->write_urb->transfer_buffer);
1119 usb_free_urb(edge_port->write_urb);
1120 edge_port->write_urb = NULL;
1122 kfree(edge_port->txfifo.fifo);
1123 edge_port->txfifo.fifo = NULL;
1126 /*****************************************************************************
1128 * this function is called by the tty driver when data should be written
1130 * If successful, we return the number of bytes written, otherwise we
1131 * return a negative error number.
1132 *****************************************************************************/
1133 static int edge_write(struct tty_struct *tty, struct usb_serial_port *port,
1134 const unsigned char *data, int count)
1136 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1137 struct TxFifo *fifo;
1142 unsigned long flags;
1144 if (edge_port == NULL)
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,
1154 (edge_port->txCredits - fifo->count));
1156 dev_dbg(&port->dev, "%s(%d) of %d byte(s) Fifo room %d -- will copy %d bytes\n",
1157 __func__, port->number, count,
1158 edge_port->txCredits - fifo->count, copySize);
1160 /* catch writes of 0 bytes which the tty driver likes to give us,
1161 and when txCredits is empty */
1162 if (copySize == 0) {
1163 dev_dbg(&port->dev, "%s - copySize = Zero\n", __func__);
1168 * since we can never overflow the buffer we do not have to check for a
1171 * the copy is done is two parts -- first fill to the end of the buffer
1172 * then copy the reset from the start of the buffer
1174 bytesleft = fifo->size - fifo->head;
1175 firsthalf = min(bytesleft, copySize);
1176 dev_dbg(&port->dev, "%s - copy %d bytes of %d into fifo \n", __func__,
1177 firsthalf, bytesleft);
1179 /* now copy our data */
1180 memcpy(&fifo->fifo[fifo->head], data, firsthalf);
1181 usb_serial_debug_data(&port->dev, __func__, firsthalf, &fifo->fifo[fifo->head]);
1183 /* update the index and size */
1184 fifo->head += firsthalf;
1185 fifo->count += firsthalf;
1187 /* wrap the index */
1188 if (fifo->head == fifo->size)
1191 secondhalf = copySize-firsthalf;
1194 dev_dbg(&port->dev, "%s - copy rest of data %d\n", __func__, secondhalf);
1195 memcpy(&fifo->fifo[fifo->head], &data[firsthalf], secondhalf);
1196 usb_serial_debug_data(&port->dev, __func__, secondhalf, &fifo->fifo[fifo->head]);
1197 /* update the index and size */
1198 fifo->count += secondhalf;
1199 fifo->head += secondhalf;
1200 /* No need to check for wrap since we can not get to end of
1201 * the fifo in this part
1206 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1208 send_more_port_data((struct edgeport_serial *)
1209 usb_get_serial_data(port->serial), edge_port);
1211 dev_dbg(&port->dev, "%s wrote %d byte(s) TxCredits %d, Fifo %d\n",
1212 __func__, copySize, edge_port->txCredits, fifo->count);
1218 /************************************************************************
1220 * send_more_port_data()
1222 * This routine attempts to write additional UART transmit data
1223 * to a port over the USB bulk pipe. It is called (1) when new
1224 * data has been written to a port's TxBuffer from higher layers
1225 * (2) when the peripheral sends us additional TxCredits indicating
1226 * that it can accept more Tx data for a given port; and (3) when
1227 * a bulk write completes successfully and we want to see if we
1228 * can transmit more.
1230 ************************************************************************/
1231 static void send_more_port_data(struct edgeport_serial *edge_serial,
1232 struct edgeport_port *edge_port)
1234 struct TxFifo *fifo = &edge_port->txfifo;
1235 struct device *dev = &edge_port->port->dev;
1237 unsigned char *buffer;
1243 unsigned long flags;
1245 spin_lock_irqsave(&edge_port->ep_lock, flags);
1247 if (edge_port->write_in_progress ||
1249 (fifo->count == 0)) {
1250 dev_dbg(dev, "%s(%d) EXIT - fifo %d, PendingWrite = %d\n",
1251 __func__, edge_port->port->number,
1252 fifo->count, edge_port->write_in_progress);
1256 /* since the amount of data in the fifo will always fit into the
1257 * edgeport buffer we do not need to check the write length
1259 * Do we have enough credits for this port to make it worthwhile
1260 * to bother queueing a write. If it's too small, say a few bytes,
1261 * it's better to wait for more credits so we can do a larger write.
1263 if (edge_port->txCredits < EDGE_FW_GET_TX_CREDITS_SEND_THRESHOLD(edge_port->maxTxCredits, EDGE_FW_BULK_MAX_PACKET_SIZE)) {
1264 dev_dbg(dev, "%s(%d) Not enough credit - fifo %d TxCredit %d\n",
1265 __func__, edge_port->port->number, fifo->count,
1266 edge_port->txCredits);
1270 /* lock this write */
1271 edge_port->write_in_progress = true;
1273 /* get a pointer to the write_urb */
1274 urb = edge_port->write_urb;
1276 /* make sure transfer buffer is freed */
1277 kfree(urb->transfer_buffer);
1278 urb->transfer_buffer = NULL;
1280 /* build the data header for the buffer and port that we are about
1282 count = fifo->count;
1283 buffer = kmalloc(count+2, GFP_ATOMIC);
1284 if (buffer == NULL) {
1285 dev_err_console(edge_port->port,
1286 "%s - no more kernel memory...\n", __func__);
1287 edge_port->write_in_progress = false;
1290 buffer[0] = IOSP_BUILD_DATA_HDR1(edge_port->port->number
1291 - edge_port->port->serial->minor, count);
1292 buffer[1] = IOSP_BUILD_DATA_HDR2(edge_port->port->number
1293 - edge_port->port->serial->minor, count);
1295 /* now copy our data */
1296 bytesleft = fifo->size - fifo->tail;
1297 firsthalf = min(bytesleft, count);
1298 memcpy(&buffer[2], &fifo->fifo[fifo->tail], firsthalf);
1299 fifo->tail += firsthalf;
1300 fifo->count -= firsthalf;
1301 if (fifo->tail == fifo->size)
1304 secondhalf = count-firsthalf;
1306 memcpy(&buffer[2+firsthalf], &fifo->fifo[fifo->tail],
1308 fifo->tail += secondhalf;
1309 fifo->count -= secondhalf;
1313 usb_serial_debug_data(&edge_port->port->dev, __func__, count, &buffer[2]);
1315 /* fill up the urb with all of our data and submit it */
1316 usb_fill_bulk_urb(urb, edge_serial->serial->dev,
1317 usb_sndbulkpipe(edge_serial->serial->dev,
1318 edge_serial->bulk_out_endpoint),
1320 edge_bulk_out_data_callback, edge_port);
1322 /* decrement the number of credits we have by the number we just sent */
1323 edge_port->txCredits -= count;
1324 edge_port->icount.tx += count;
1326 status = usb_submit_urb(urb, GFP_ATOMIC);
1328 /* something went wrong */
1329 dev_err_console(edge_port->port,
1330 "%s - usb_submit_urb(write bulk) failed, status = %d, data lost\n",
1332 edge_port->write_in_progress = false;
1334 /* revert the credits as something bad happened. */
1335 edge_port->txCredits += count;
1336 edge_port->icount.tx -= count;
1338 dev_dbg(dev, "%s wrote %d byte(s) TxCredit %d, Fifo %d\n",
1339 __func__, count, edge_port->txCredits, fifo->count);
1342 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1346 /*****************************************************************************
1348 * this function is called by the tty driver when it wants to know how
1349 * many bytes of data we can accept for a specific port. If successful,
1350 * we return the amount of room that we have for this port (the txCredits)
1351 * otherwise we return a negative error number.
1352 *****************************************************************************/
1353 static int edge_write_room(struct tty_struct *tty)
1355 struct usb_serial_port *port = tty->driver_data;
1356 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1358 unsigned long flags;
1360 if (edge_port == NULL)
1362 if (edge_port->closePending)
1365 if (!edge_port->open) {
1366 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
1370 /* total of both buffers is still txCredit */
1371 spin_lock_irqsave(&edge_port->ep_lock, flags);
1372 room = edge_port->txCredits - edge_port->txfifo.count;
1373 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1375 dev_dbg(&port->dev, "%s - returns %d\n", __func__, room);
1380 /*****************************************************************************
1381 * edge_chars_in_buffer
1382 * this function is called by the tty driver when it wants to know how
1383 * many bytes of data we currently have outstanding in the port (data that
1384 * has been written, but hasn't made it out the port yet)
1385 * If successful, we return the number of bytes left to be written in the
1387 * Otherwise we return a negative error number.
1388 *****************************************************************************/
1389 static int edge_chars_in_buffer(struct tty_struct *tty)
1391 struct usb_serial_port *port = tty->driver_data;
1392 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1394 unsigned long flags;
1396 if (edge_port == NULL)
1398 if (edge_port->closePending)
1401 if (!edge_port->open) {
1402 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
1406 spin_lock_irqsave(&edge_port->ep_lock, flags);
1407 num_chars = edge_port->maxTxCredits - edge_port->txCredits +
1408 edge_port->txfifo.count;
1409 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1411 dev_dbg(&port->dev, "%s(port %d) - returns %d\n", __func__,
1412 port->number, num_chars);
1419 /*****************************************************************************
1421 * this function is called by the tty driver when it wants to stop the data
1422 * being read from the port.
1423 *****************************************************************************/
1424 static void edge_throttle(struct tty_struct *tty)
1426 struct usb_serial_port *port = tty->driver_data;
1427 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1430 if (edge_port == NULL)
1433 if (!edge_port->open) {
1434 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
1438 /* if we are implementing XON/XOFF, send the stop character */
1440 unsigned char stop_char = STOP_CHAR(tty);
1441 status = edge_write(tty, port, &stop_char, 1);
1446 /* if we are implementing RTS/CTS, toggle that line */
1447 if (tty->termios->c_cflag & CRTSCTS) {
1448 edge_port->shadowMCR &= ~MCR_RTS;
1449 status = send_cmd_write_uart_register(edge_port, MCR,
1450 edge_port->shadowMCR);
1457 /*****************************************************************************
1459 * this function is called by the tty driver when it wants to resume the
1460 * data being read from the port (called after SerialThrottle is called)
1461 *****************************************************************************/
1462 static void edge_unthrottle(struct tty_struct *tty)
1464 struct usb_serial_port *port = tty->driver_data;
1465 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1468 if (edge_port == NULL)
1471 if (!edge_port->open) {
1472 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
1476 /* if we are implementing XON/XOFF, send the start character */
1478 unsigned char start_char = START_CHAR(tty);
1479 status = edge_write(tty, port, &start_char, 1);
1483 /* if we are implementing RTS/CTS, toggle that line */
1484 if (tty->termios->c_cflag & CRTSCTS) {
1485 edge_port->shadowMCR |= MCR_RTS;
1486 send_cmd_write_uart_register(edge_port, MCR,
1487 edge_port->shadowMCR);
1492 /*****************************************************************************
1494 * this function is called by the tty driver when it wants to change
1495 * the termios structure
1496 *****************************************************************************/
1497 static void edge_set_termios(struct tty_struct *tty,
1498 struct usb_serial_port *port, struct ktermios *old_termios)
1500 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1503 cflag = tty->termios->c_cflag;
1504 dev_dbg(&port->dev, "%s - clfag %08x iflag %08x\n", __func__, tty->termios->c_cflag, tty->termios->c_iflag);
1505 dev_dbg(&port->dev, "%s - old clfag %08x old iflag %08x\n", __func__, old_termios->c_cflag, old_termios->c_iflag);
1507 if (edge_port == NULL)
1510 if (!edge_port->open) {
1511 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
1515 /* change the port settings to the new ones specified */
1516 change_port_settings(tty, edge_port, old_termios);
1520 /*****************************************************************************
1521 * get_lsr_info - get line status register info
1523 * Purpose: Let user call ioctl() to get info when the UART physically
1524 * is emptied. On bus types like RS485, the transmitter must
1525 * release the bus after transmitting. This must be done when
1526 * the transmit shift register is empty, not be done when the
1527 * transmit holding register is empty. This functionality
1528 * allows an RS485 driver to be written in user space.
1529 *****************************************************************************/
1530 static int get_lsr_info(struct edgeport_port *edge_port,
1531 unsigned int __user *value)
1533 unsigned int result = 0;
1534 unsigned long flags;
1536 spin_lock_irqsave(&edge_port->ep_lock, flags);
1537 if (edge_port->maxTxCredits == edge_port->txCredits &&
1538 edge_port->txfifo.count == 0) {
1539 dev_dbg(&edge_port->port->dev, "%s -- Empty\n", __func__);
1540 result = TIOCSER_TEMT;
1542 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1544 if (copy_to_user(value, &result, sizeof(int)))
1549 static int edge_tiocmset(struct tty_struct *tty,
1550 unsigned int set, unsigned int clear)
1552 struct usb_serial_port *port = tty->driver_data;
1553 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1556 mcr = edge_port->shadowMCR;
1557 if (set & TIOCM_RTS)
1559 if (set & TIOCM_DTR)
1561 if (set & TIOCM_LOOP)
1562 mcr |= MCR_LOOPBACK;
1564 if (clear & TIOCM_RTS)
1566 if (clear & TIOCM_DTR)
1568 if (clear & TIOCM_LOOP)
1569 mcr &= ~MCR_LOOPBACK;
1571 edge_port->shadowMCR = mcr;
1573 send_cmd_write_uart_register(edge_port, MCR, edge_port->shadowMCR);
1578 static int edge_tiocmget(struct tty_struct *tty)
1580 struct usb_serial_port *port = tty->driver_data;
1581 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1582 unsigned int result = 0;
1586 msr = edge_port->shadowMSR;
1587 mcr = edge_port->shadowMCR;
1588 result = ((mcr & MCR_DTR) ? TIOCM_DTR: 0) /* 0x002 */
1589 | ((mcr & MCR_RTS) ? TIOCM_RTS: 0) /* 0x004 */
1590 | ((msr & EDGEPORT_MSR_CTS) ? TIOCM_CTS: 0) /* 0x020 */
1591 | ((msr & EDGEPORT_MSR_CD) ? TIOCM_CAR: 0) /* 0x040 */
1592 | ((msr & EDGEPORT_MSR_RI) ? TIOCM_RI: 0) /* 0x080 */
1593 | ((msr & EDGEPORT_MSR_DSR) ? TIOCM_DSR: 0); /* 0x100 */
1598 static int edge_get_icount(struct tty_struct *tty,
1599 struct serial_icounter_struct *icount)
1601 struct usb_serial_port *port = tty->driver_data;
1602 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1603 struct async_icount cnow;
1604 cnow = edge_port->icount;
1606 icount->cts = cnow.cts;
1607 icount->dsr = cnow.dsr;
1608 icount->rng = cnow.rng;
1609 icount->dcd = cnow.dcd;
1610 icount->rx = cnow.rx;
1611 icount->tx = cnow.tx;
1612 icount->frame = cnow.frame;
1613 icount->overrun = cnow.overrun;
1614 icount->parity = cnow.parity;
1615 icount->brk = cnow.brk;
1616 icount->buf_overrun = cnow.buf_overrun;
1618 dev_dbg(&port->dev, "%s (%d) TIOCGICOUNT RX=%d, TX=%d\n", __func__,
1619 port->number, icount->rx, icount->tx);
1623 static int get_serial_info(struct edgeport_port *edge_port,
1624 struct serial_struct __user *retinfo)
1626 struct serial_struct tmp;
1631 memset(&tmp, 0, sizeof(tmp));
1633 tmp.type = PORT_16550A;
1634 tmp.line = edge_port->port->serial->minor;
1635 tmp.port = edge_port->port->number;
1637 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
1638 tmp.xmit_fifo_size = edge_port->maxTxCredits;
1639 tmp.baud_base = 9600;
1640 tmp.close_delay = 5*HZ;
1641 tmp.closing_wait = 30*HZ;
1643 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1649 /*****************************************************************************
1651 * this function handles any ioctl calls to the driver
1652 *****************************************************************************/
1653 static int edge_ioctl(struct tty_struct *tty,
1654 unsigned int cmd, unsigned long arg)
1656 struct usb_serial_port *port = tty->driver_data;
1658 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1659 struct async_icount cnow;
1660 struct async_icount cprev;
1662 dev_dbg(&port->dev, "%s - port %d, cmd = 0x%x\n", __func__, port->number, cmd);
1666 dev_dbg(&port->dev, "%s (%d) TIOCSERGETLSR\n", __func__, port->number);
1667 return get_lsr_info(edge_port, (unsigned int __user *) arg);
1670 dev_dbg(&port->dev, "%s (%d) TIOCGSERIAL\n", __func__, port->number);
1671 return get_serial_info(edge_port, (struct serial_struct __user *) arg);
1674 dev_dbg(&port->dev, "%s (%d) TIOCMIWAIT\n", __func__, port->number);
1675 cprev = edge_port->icount;
1677 prepare_to_wait(&edge_port->delta_msr_wait,
1678 &wait, TASK_INTERRUPTIBLE);
1680 finish_wait(&edge_port->delta_msr_wait, &wait);
1681 /* see if a signal did it */
1682 if (signal_pending(current))
1683 return -ERESTARTSYS;
1684 cnow = edge_port->icount;
1685 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
1686 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
1687 return -EIO; /* no change => error */
1688 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1689 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1690 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1691 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
1700 return -ENOIOCTLCMD;
1704 /*****************************************************************************
1706 * this function sends a break to the port
1707 *****************************************************************************/
1708 static void edge_break(struct tty_struct *tty, int break_state)
1710 struct usb_serial_port *port = tty->driver_data;
1711 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1712 struct edgeport_serial *edge_serial = usb_get_serial_data(port->serial);
1715 if ((!edge_serial->is_epic) ||
1716 ((edge_serial->is_epic) &&
1717 (edge_serial->epic_descriptor.Supports.IOSPChase))) {
1718 /* flush and chase */
1719 edge_port->chaseResponsePending = true;
1721 dev_dbg(&port->dev, "%s - Sending IOSP_CMD_CHASE_PORT\n", __func__);
1722 status = send_iosp_ext_cmd(edge_port, IOSP_CMD_CHASE_PORT, 0);
1724 /* block until chase finished */
1725 block_until_chase_response(edge_port);
1727 edge_port->chaseResponsePending = false;
1731 if ((!edge_serial->is_epic) ||
1732 ((edge_serial->is_epic) &&
1733 (edge_serial->epic_descriptor.Supports.IOSPSetClrBreak))) {
1734 if (break_state == -1) {
1735 dev_dbg(&port->dev, "%s - Sending IOSP_CMD_SET_BREAK\n", __func__);
1736 status = send_iosp_ext_cmd(edge_port,
1737 IOSP_CMD_SET_BREAK, 0);
1739 dev_dbg(&port->dev, "%s - Sending IOSP_CMD_CLEAR_BREAK\n", __func__);
1740 status = send_iosp_ext_cmd(edge_port,
1741 IOSP_CMD_CLEAR_BREAK, 0);
1744 dev_dbg(&port->dev, "%s - error sending break set/clear command.\n",
1750 /*****************************************************************************
1752 * this function handles the data received on the bulk in pipe.
1753 *****************************************************************************/
1754 static void process_rcvd_data(struct edgeport_serial *edge_serial,
1755 unsigned char *buffer, __u16 bufferLength)
1757 struct device *dev = &edge_serial->serial->dev->dev;
1758 struct usb_serial_port *port;
1759 struct edgeport_port *edge_port;
1760 struct tty_struct *tty;
1761 __u16 lastBufferLength;
1764 lastBufferLength = bufferLength + 1;
1766 while (bufferLength > 0) {
1767 /* failsafe incase we get a message that we don't understand */
1768 if (lastBufferLength == bufferLength) {
1769 dev_dbg(dev, "%s - stuck in loop, exiting it.\n", __func__);
1772 lastBufferLength = bufferLength;
1774 switch (edge_serial->rxState) {
1776 edge_serial->rxHeader1 = *buffer;
1780 if (bufferLength == 0) {
1781 edge_serial->rxState = EXPECT_HDR2;
1784 /* otherwise, drop on through */
1786 edge_serial->rxHeader2 = *buffer;
1790 dev_dbg(dev, "%s - Hdr1=%02X Hdr2=%02X\n", __func__,
1791 edge_serial->rxHeader1, edge_serial->rxHeader2);
1792 /* Process depending on whether this header is
1795 if (IS_CMD_STAT_HDR(edge_serial->rxHeader1)) {
1796 /* Decode this status header and go to
1797 * EXPECT_HDR1 (if we can process the status
1798 * with only 2 bytes), or go to EXPECT_HDR3 to
1799 * get the third byte. */
1800 edge_serial->rxPort =
1801 IOSP_GET_HDR_PORT(edge_serial->rxHeader1);
1802 edge_serial->rxStatusCode =
1803 IOSP_GET_STATUS_CODE(
1804 edge_serial->rxHeader1);
1806 if (!IOSP_STATUS_IS_2BYTE(
1807 edge_serial->rxStatusCode)) {
1808 /* This status needs additional bytes.
1809 * Save what we have and then wait for
1812 edge_serial->rxStatusParam
1813 = edge_serial->rxHeader2;
1814 edge_serial->rxState = EXPECT_HDR3;
1817 /* We have all the header bytes, process the
1819 process_rcvd_status(edge_serial,
1820 edge_serial->rxHeader2, 0);
1821 edge_serial->rxState = EXPECT_HDR1;
1824 edge_serial->rxPort =
1825 IOSP_GET_HDR_PORT(edge_serial->rxHeader1);
1826 edge_serial->rxBytesRemaining =
1827 IOSP_GET_HDR_DATA_LEN(
1828 edge_serial->rxHeader1,
1829 edge_serial->rxHeader2);
1830 dev_dbg(dev, "%s - Data for Port %u Len %u\n",
1832 edge_serial->rxPort,
1833 edge_serial->rxBytesRemaining);
1835 /* ASSERT(DevExt->RxPort < DevExt->NumPorts);
1836 * ASSERT(DevExt->RxBytesRemaining <
1837 * IOSP_MAX_DATA_LENGTH);
1840 if (bufferLength == 0) {
1841 edge_serial->rxState = EXPECT_DATA;
1844 /* Else, drop through */
1846 case EXPECT_DATA: /* Expect data */
1847 if (bufferLength < edge_serial->rxBytesRemaining) {
1848 rxLen = bufferLength;
1849 /* Expect data to start next buffer */
1850 edge_serial->rxState = EXPECT_DATA;
1852 /* BufLen >= RxBytesRemaining */
1853 rxLen = edge_serial->rxBytesRemaining;
1854 /* Start another header next time */
1855 edge_serial->rxState = EXPECT_HDR1;
1858 bufferLength -= rxLen;
1859 edge_serial->rxBytesRemaining -= rxLen;
1861 /* spit this data back into the tty driver if this
1864 port = edge_serial->serial->port[
1865 edge_serial->rxPort];
1866 edge_port = usb_get_serial_port_data(port);
1867 if (edge_port->open) {
1868 tty = tty_port_tty_get(
1869 &edge_port->port->port);
1871 dev_dbg(dev, "%s - Sending %d bytes to TTY for port %d\n",
1872 __func__, rxLen, edge_serial->rxPort);
1873 edge_tty_recv(&edge_serial->serial->dev->dev, tty, buffer, rxLen);
1876 edge_port->icount.rx += rxLen;
1882 case EXPECT_HDR3: /* Expect 3rd byte of status header */
1883 edge_serial->rxHeader3 = *buffer;
1887 /* We have all the header bytes, process the
1889 process_rcvd_status(edge_serial,
1890 edge_serial->rxStatusParam,
1891 edge_serial->rxHeader3);
1892 edge_serial->rxState = EXPECT_HDR1;
1899 /*****************************************************************************
1900 * process_rcvd_status
1901 * this function handles the any status messages received on the
1903 *****************************************************************************/
1904 static void process_rcvd_status(struct edgeport_serial *edge_serial,
1905 __u8 byte2, __u8 byte3)
1907 struct usb_serial_port *port;
1908 struct edgeport_port *edge_port;
1909 struct tty_struct *tty;
1911 __u8 code = edge_serial->rxStatusCode;
1913 /* switch the port pointer to the one being currently talked about */
1914 port = edge_serial->serial->port[edge_serial->rxPort];
1915 edge_port = usb_get_serial_port_data(port);
1916 if (edge_port == NULL) {
1917 dev_err(&edge_serial->serial->dev->dev,
1918 "%s - edge_port == NULL for port %d\n",
1919 __func__, edge_serial->rxPort);
1924 if (code == IOSP_EXT_STATUS) {
1926 case IOSP_EXT_STATUS_CHASE_RSP:
1927 /* we want to do EXT status regardless of port
1929 dev_dbg(dev, "%s - Port %u EXT CHASE_RSP Data = %02x\n",
1930 __func__, edge_serial->rxPort, byte3);
1931 /* Currently, the only EXT_STATUS is Chase, so process
1932 * here instead of one more call to one more subroutine
1933 * If/when more EXT_STATUS, there'll be more work to do
1934 * Also, we currently clear flag and close the port
1935 * regardless of content of above's Byte3.
1936 * We could choose to do something else when Byte3 says
1937 * Timeout on Chase from Edgeport, like wait longer in
1938 * block_until_chase_response, but for now we don't.
1940 edge_port->chaseResponsePending = false;
1941 wake_up(&edge_port->wait_chase);
1944 case IOSP_EXT_STATUS_RX_CHECK_RSP:
1945 dev_dbg(dev, "%s ========== Port %u CHECK_RSP Sequence = %02x =============\n",
1946 __func__, edge_serial->rxPort, byte3);
1947 /* Port->RxCheckRsp = true; */
1952 if (code == IOSP_STATUS_OPEN_RSP) {
1953 edge_port->txCredits = GET_TX_BUFFER_SIZE(byte3);
1954 edge_port->maxTxCredits = edge_port->txCredits;
1955 dev_dbg(dev, "%s - Port %u Open Response Initial MSR = %02x TxBufferSize = %d\n",
1956 __func__, edge_serial->rxPort, byte2, edge_port->txCredits);
1957 handle_new_msr(edge_port, byte2);
1959 /* send the current line settings to the port so we are
1960 in sync with any further termios calls */
1961 tty = tty_port_tty_get(&edge_port->port->port);
1963 change_port_settings(tty,
1964 edge_port, tty->termios);
1968 /* we have completed the open */
1969 edge_port->openPending = false;
1970 edge_port->open = true;
1971 wake_up(&edge_port->wait_open);
1975 /* If port is closed, silently discard all rcvd status. We can
1976 * have cases where buffered status is received AFTER the close
1977 * port command is sent to the Edgeport.
1979 if (!edge_port->open || edge_port->closePending)
1983 /* Not currently sent by Edgeport */
1984 case IOSP_STATUS_LSR:
1985 dev_dbg(dev, "%s - Port %u LSR Status = %02x\n",
1986 __func__, edge_serial->rxPort, byte2);
1987 handle_new_lsr(edge_port, false, byte2, 0);
1990 case IOSP_STATUS_LSR_DATA:
1991 dev_dbg(dev, "%s - Port %u LSR Status = %02x, Data = %02x\n",
1992 __func__, edge_serial->rxPort, byte2, byte3);
1993 /* byte2 is LSR Register */
1994 /* byte3 is broken data byte */
1995 handle_new_lsr(edge_port, true, byte2, byte3);
1998 * case IOSP_EXT_4_STATUS:
1999 * dev_dbg(dev, "%s - Port %u LSR Status = %02x Data = %02x\n",
2000 * __func__, edge_serial->rxPort, byte2, byte3);
2003 case IOSP_STATUS_MSR:
2004 dev_dbg(dev, "%s - Port %u MSR Status = %02x\n",
2005 __func__, edge_serial->rxPort, byte2);
2007 * Process this new modem status and generate appropriate
2008 * events, etc, based on the new status. This routine
2009 * also saves the MSR in Port->ShadowMsr.
2011 handle_new_msr(edge_port, byte2);
2015 dev_dbg(dev, "%s - Unrecognized IOSP status code %u\n", __func__, code);
2021 /*****************************************************************************
2023 * this function passes data on to the tty flip buffer
2024 *****************************************************************************/
2025 static void edge_tty_recv(struct device *dev, struct tty_struct *tty,
2026 unsigned char *data, int length)
2030 cnt = tty_insert_flip_string(tty, data, length);
2032 dev_err(dev, "%s - dropping data, %d bytes lost\n",
2033 __func__, length - cnt);
2038 tty_flip_buffer_push(tty);
2042 /*****************************************************************************
2044 * this function handles any change to the msr register for a port.
2045 *****************************************************************************/
2046 static void handle_new_msr(struct edgeport_port *edge_port, __u8 newMsr)
2048 struct async_icount *icount;
2050 if (newMsr & (EDGEPORT_MSR_DELTA_CTS | EDGEPORT_MSR_DELTA_DSR |
2051 EDGEPORT_MSR_DELTA_RI | EDGEPORT_MSR_DELTA_CD)) {
2052 icount = &edge_port->icount;
2054 /* update input line counters */
2055 if (newMsr & EDGEPORT_MSR_DELTA_CTS)
2057 if (newMsr & EDGEPORT_MSR_DELTA_DSR)
2059 if (newMsr & EDGEPORT_MSR_DELTA_CD)
2061 if (newMsr & EDGEPORT_MSR_DELTA_RI)
2063 wake_up_interruptible(&edge_port->delta_msr_wait);
2066 /* Save the new modem status */
2067 edge_port->shadowMSR = newMsr & 0xf0;
2071 /*****************************************************************************
2073 * this function handles any change to the lsr register for a port.
2074 *****************************************************************************/
2075 static void handle_new_lsr(struct edgeport_port *edge_port, __u8 lsrData,
2076 __u8 lsr, __u8 data)
2078 __u8 newLsr = (__u8) (lsr & (__u8)
2079 (LSR_OVER_ERR | LSR_PAR_ERR | LSR_FRM_ERR | LSR_BREAK));
2080 struct async_icount *icount;
2082 edge_port->shadowLSR = lsr;
2084 if (newLsr & LSR_BREAK) {
2086 * Parity and Framing errors only count if they
2087 * occur exclusive of a break being
2090 newLsr &= (__u8)(LSR_OVER_ERR | LSR_BREAK);
2093 /* Place LSR data byte into Rx buffer */
2095 struct tty_struct *tty =
2096 tty_port_tty_get(&edge_port->port->port);
2098 edge_tty_recv(&edge_port->port->dev, tty, &data, 1);
2102 /* update input line counters */
2103 icount = &edge_port->icount;
2104 if (newLsr & LSR_BREAK)
2106 if (newLsr & LSR_OVER_ERR)
2108 if (newLsr & LSR_PAR_ERR)
2110 if (newLsr & LSR_FRM_ERR)
2115 /****************************************************************************
2117 * writes a number of bytes to the Edgeport device's sram starting at the
2119 * If successful returns the number of bytes written, otherwise it returns
2120 * a negative error number of the problem.
2121 ****************************************************************************/
2122 static int sram_write(struct usb_serial *serial, __u16 extAddr, __u16 addr,
2123 __u16 length, const __u8 *data)
2126 __u16 current_length;
2127 unsigned char *transfer_buffer;
2129 dev_dbg(&serial->dev->dev, "%s - %x, %x, %d\n", __func__, extAddr, addr, length);
2131 transfer_buffer = kmalloc(64, GFP_KERNEL);
2132 if (!transfer_buffer) {
2133 dev_err(&serial->dev->dev, "%s - kmalloc(%d) failed.\n",
2138 /* need to split these writes up into 64 byte chunks */
2140 while (length > 0) {
2142 current_length = 64;
2144 current_length = length;
2146 /* dev_dbg(&serial->dev->dev, "%s - writing %x, %x, %d\n", __func__, extAddr, addr, current_length); */
2147 memcpy(transfer_buffer, data, current_length);
2148 result = usb_control_msg(serial->dev,
2149 usb_sndctrlpipe(serial->dev, 0),
2150 USB_REQUEST_ION_WRITE_RAM,
2151 0x40, addr, extAddr, transfer_buffer,
2152 current_length, 300);
2155 length -= current_length;
2156 addr += current_length;
2157 data += current_length;
2160 kfree(transfer_buffer);
2165 /****************************************************************************
2167 * writes a number of bytes to the Edgeport device's ROM starting at the
2169 * If successful returns the number of bytes written, otherwise it returns
2170 * a negative error number of the problem.
2171 ****************************************************************************/
2172 static int rom_write(struct usb_serial *serial, __u16 extAddr, __u16 addr,
2173 __u16 length, const __u8 *data)
2176 __u16 current_length;
2177 unsigned char *transfer_buffer;
2179 transfer_buffer = kmalloc(64, GFP_KERNEL);
2180 if (!transfer_buffer) {
2181 dev_err(&serial->dev->dev, "%s - kmalloc(%d) failed.\n",
2186 /* need to split these writes up into 64 byte chunks */
2188 while (length > 0) {
2190 current_length = 64;
2192 current_length = length;
2193 memcpy(transfer_buffer, data, current_length);
2194 result = usb_control_msg(serial->dev,
2195 usb_sndctrlpipe(serial->dev, 0),
2196 USB_REQUEST_ION_WRITE_ROM, 0x40,
2198 transfer_buffer, current_length, 300);
2201 length -= current_length;
2202 addr += current_length;
2203 data += current_length;
2206 kfree(transfer_buffer);
2211 /****************************************************************************
2213 * reads a number of bytes from the Edgeport device starting at the given
2215 * If successful returns the number of bytes read, otherwise it returns
2216 * a negative error number of the problem.
2217 ****************************************************************************/
2218 static int rom_read(struct usb_serial *serial, __u16 extAddr,
2219 __u16 addr, __u16 length, __u8 *data)
2222 __u16 current_length;
2223 unsigned char *transfer_buffer;
2225 transfer_buffer = kmalloc(64, GFP_KERNEL);
2226 if (!transfer_buffer) {
2227 dev_err(&serial->dev->dev,
2228 "%s - kmalloc(%d) failed.\n", __func__, 64);
2232 /* need to split these reads up into 64 byte chunks */
2234 while (length > 0) {
2236 current_length = 64;
2238 current_length = length;
2239 result = usb_control_msg(serial->dev,
2240 usb_rcvctrlpipe(serial->dev, 0),
2241 USB_REQUEST_ION_READ_ROM,
2242 0xC0, addr, extAddr, transfer_buffer,
2243 current_length, 300);
2246 memcpy(data, transfer_buffer, current_length);
2247 length -= current_length;
2248 addr += current_length;
2249 data += current_length;
2252 kfree(transfer_buffer);
2257 /****************************************************************************
2259 * Is used to send a IOSP message to the Edgeport device
2260 ****************************************************************************/
2261 static int send_iosp_ext_cmd(struct edgeport_port *edge_port,
2262 __u8 command, __u8 param)
2264 unsigned char *buffer;
2265 unsigned char *currentCommand;
2269 buffer = kmalloc(10, GFP_ATOMIC);
2271 dev_err(&edge_port->port->dev,
2272 "%s - kmalloc(%d) failed.\n", __func__, 10);
2276 currentCommand = buffer;
2278 MAKE_CMD_EXT_CMD(¤tCommand, &length,
2279 edge_port->port->number - edge_port->port->serial->minor,
2282 status = write_cmd_usb(edge_port, buffer, length);
2284 /* something bad happened, let's free up the memory */
2292 /*****************************************************************************
2294 * this function writes the given buffer out to the bulk write endpoint.
2295 *****************************************************************************/
2296 static int write_cmd_usb(struct edgeport_port *edge_port,
2297 unsigned char *buffer, int length)
2299 struct edgeport_serial *edge_serial =
2300 usb_get_serial_data(edge_port->port->serial);
2301 struct device *dev = &edge_port->port->dev;
2305 usb_serial_debug_data(dev, __func__, length, buffer);
2307 /* Allocate our next urb */
2308 urb = usb_alloc_urb(0, GFP_ATOMIC);
2312 atomic_inc(&CmdUrbs);
2313 dev_dbg(dev, "%s - ALLOCATE URB %p (outstanding %d)\n",
2314 __func__, urb, atomic_read(&CmdUrbs));
2316 usb_fill_bulk_urb(urb, edge_serial->serial->dev,
2317 usb_sndbulkpipe(edge_serial->serial->dev,
2318 edge_serial->bulk_out_endpoint),
2319 buffer, length, edge_bulk_out_cmd_callback, edge_port);
2321 edge_port->commandPending = true;
2322 status = usb_submit_urb(urb, GFP_ATOMIC);
2325 /* something went wrong */
2326 dev_err(dev, "%s - usb_submit_urb(write command) failed, status = %d\n",
2330 atomic_dec(&CmdUrbs);
2335 wait_event(&edge_port->wait_command, !edge_port->commandPending);
2337 if (edge_port->commandPending) {
2338 /* command timed out */
2339 dev_dbg(dev, "%s - command timed out\n", __func__);
2347 /*****************************************************************************
2348 * send_cmd_write_baud_rate
2349 * this function sends the proper command to change the baud rate of the
2351 *****************************************************************************/
2352 static int send_cmd_write_baud_rate(struct edgeport_port *edge_port,
2355 struct edgeport_serial *edge_serial =
2356 usb_get_serial_data(edge_port->port->serial);
2357 struct device *dev = &edge_port->port->dev;
2358 unsigned char *cmdBuffer;
2359 unsigned char *currCmd;
2363 unsigned char number =
2364 edge_port->port->number - edge_port->port->serial->minor;
2366 if (edge_serial->is_epic &&
2367 !edge_serial->epic_descriptor.Supports.IOSPSetBaudRate) {
2368 dev_dbg(dev, "SendCmdWriteBaudRate - NOT Setting baud rate for port = %d, baud = %d\n",
2369 edge_port->port->number, baudRate);
2373 dev_dbg(dev, "%s - port = %d, baud = %d\n", __func__,
2374 edge_port->port->number, baudRate);
2376 status = calc_baud_rate_divisor(dev, baudRate, &divisor);
2378 dev_err(dev, "%s - bad baud rate\n", __func__);
2382 /* Alloc memory for the string of commands. */
2383 cmdBuffer = kmalloc(0x100, GFP_ATOMIC);
2385 dev_err(dev, "%s - kmalloc(%d) failed.\n", __func__, 0x100);
2388 currCmd = cmdBuffer;
2390 /* Enable access to divisor latch */
2391 MAKE_CMD_WRITE_REG(&currCmd, &cmdLen, number, LCR, LCR_DL_ENABLE);
2393 /* Write the divisor itself */
2394 MAKE_CMD_WRITE_REG(&currCmd, &cmdLen, number, DLL, LOW8(divisor));
2395 MAKE_CMD_WRITE_REG(&currCmd, &cmdLen, number, DLM, HIGH8(divisor));
2397 /* Restore original value to disable access to divisor latch */
2398 MAKE_CMD_WRITE_REG(&currCmd, &cmdLen, number, LCR,
2399 edge_port->shadowLCR);
2401 status = write_cmd_usb(edge_port, cmdBuffer, cmdLen);
2403 /* something bad happened, let's free up the memory */
2411 /*****************************************************************************
2412 * calc_baud_rate_divisor
2413 * this function calculates the proper baud rate divisor for the specified
2415 *****************************************************************************/
2416 static int calc_baud_rate_divisor(struct device *dev, int baudrate, int *divisor)
2421 for (i = 0; i < ARRAY_SIZE(divisor_table); i++) {
2422 if (divisor_table[i].BaudRate == baudrate) {
2423 *divisor = divisor_table[i].Divisor;
2428 /* We have tried all of the standard baud rates
2429 * lets try to calculate the divisor for this baud rate
2430 * Make sure the baud rate is reasonable */
2431 if (baudrate > 50 && baudrate < 230400) {
2433 custom = (__u16)((230400L + baudrate/2) / baudrate);
2437 dev_dbg(dev, "%s - Baud %d = %d\n", __func__, baudrate, custom);
2445 /*****************************************************************************
2446 * send_cmd_write_uart_register
2447 * this function builds up a uart register message and sends to the device.
2448 *****************************************************************************/
2449 static int send_cmd_write_uart_register(struct edgeport_port *edge_port,
2450 __u8 regNum, __u8 regValue)
2452 struct edgeport_serial *edge_serial =
2453 usb_get_serial_data(edge_port->port->serial);
2454 struct device *dev = &edge_port->port->dev;
2455 unsigned char *cmdBuffer;
2456 unsigned char *currCmd;
2457 unsigned long cmdLen = 0;
2460 dev_dbg(dev, "%s - write to %s register 0x%02x\n",
2461 (regNum == MCR) ? "MCR" : "LCR", __func__, regValue);
2463 if (edge_serial->is_epic &&
2464 !edge_serial->epic_descriptor.Supports.IOSPWriteMCR &&
2466 dev_dbg(dev, "SendCmdWriteUartReg - Not writing to MCR Register\n");
2470 if (edge_serial->is_epic &&
2471 !edge_serial->epic_descriptor.Supports.IOSPWriteLCR &&
2473 dev_dbg(dev, "SendCmdWriteUartReg - Not writing to LCR Register\n");
2477 /* Alloc memory for the string of commands. */
2478 cmdBuffer = kmalloc(0x10, GFP_ATOMIC);
2479 if (cmdBuffer == NULL)
2482 currCmd = cmdBuffer;
2484 /* Build a cmd in the buffer to write the given register */
2485 MAKE_CMD_WRITE_REG(&currCmd, &cmdLen,
2486 edge_port->port->number - edge_port->port->serial->minor,
2489 status = write_cmd_usb(edge_port, cmdBuffer, cmdLen);
2491 /* something bad happened, let's free up the memory */
2499 /*****************************************************************************
2500 * change_port_settings
2501 * This routine is called to set the UART on the device to match the
2502 * specified new settings.
2503 *****************************************************************************/
2505 static void change_port_settings(struct tty_struct *tty,
2506 struct edgeport_port *edge_port, struct ktermios *old_termios)
2508 struct device *dev = &edge_port->port->dev;
2509 struct edgeport_serial *edge_serial =
2510 usb_get_serial_data(edge_port->port->serial);
2521 dev_dbg(dev, "%s - port %d\n", __func__, edge_port->port->number);
2523 if (!edge_port->open &&
2524 !edge_port->openPending) {
2525 dev_dbg(dev, "%s - port not opened\n", __func__);
2529 cflag = tty->termios->c_cflag;
2531 switch (cflag & CSIZE) {
2533 lData = LCR_BITS_5; mask = 0x1f;
2534 dev_dbg(dev, "%s - data bits = 5\n", __func__);
2537 lData = LCR_BITS_6; mask = 0x3f;
2538 dev_dbg(dev, "%s - data bits = 6\n", __func__);
2541 lData = LCR_BITS_7; mask = 0x7f;
2542 dev_dbg(dev, "%s - data bits = 7\n", __func__);
2547 dev_dbg(dev, "%s - data bits = 8\n", __func__);
2551 lParity = LCR_PAR_NONE;
2552 if (cflag & PARENB) {
2553 if (cflag & CMSPAR) {
2554 if (cflag & PARODD) {
2555 lParity = LCR_PAR_MARK;
2556 dev_dbg(dev, "%s - parity = mark\n", __func__);
2558 lParity = LCR_PAR_SPACE;
2559 dev_dbg(dev, "%s - parity = space\n", __func__);
2561 } else if (cflag & PARODD) {
2562 lParity = LCR_PAR_ODD;
2563 dev_dbg(dev, "%s - parity = odd\n", __func__);
2565 lParity = LCR_PAR_EVEN;
2566 dev_dbg(dev, "%s - parity = even\n", __func__);
2569 dev_dbg(dev, "%s - parity = none\n", __func__);
2572 if (cflag & CSTOPB) {
2574 dev_dbg(dev, "%s - stop bits = 2\n", __func__);
2577 dev_dbg(dev, "%s - stop bits = 1\n", __func__);
2580 /* figure out the flow control settings */
2581 rxFlow = txFlow = 0x00;
2582 if (cflag & CRTSCTS) {
2583 rxFlow |= IOSP_RX_FLOW_RTS;
2584 txFlow |= IOSP_TX_FLOW_CTS;
2585 dev_dbg(dev, "%s - RTS/CTS is enabled\n", __func__);
2587 dev_dbg(dev, "%s - RTS/CTS is disabled\n", __func__);
2590 /* if we are implementing XON/XOFF, set the start and stop character
2592 if (I_IXOFF(tty) || I_IXON(tty)) {
2593 unsigned char stop_char = STOP_CHAR(tty);
2594 unsigned char start_char = START_CHAR(tty);
2596 if ((!edge_serial->is_epic) ||
2597 ((edge_serial->is_epic) &&
2598 (edge_serial->epic_descriptor.Supports.IOSPSetXChar))) {
2599 send_iosp_ext_cmd(edge_port,
2600 IOSP_CMD_SET_XON_CHAR, start_char);
2601 send_iosp_ext_cmd(edge_port,
2602 IOSP_CMD_SET_XOFF_CHAR, stop_char);
2605 /* if we are implementing INBOUND XON/XOFF */
2607 rxFlow |= IOSP_RX_FLOW_XON_XOFF;
2608 dev_dbg(dev, "%s - INBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x\n",
2609 __func__, start_char, stop_char);
2611 dev_dbg(dev, "%s - INBOUND XON/XOFF is disabled\n", __func__);
2614 /* if we are implementing OUTBOUND XON/XOFF */
2616 txFlow |= IOSP_TX_FLOW_XON_XOFF;
2617 dev_dbg(dev, "%s - OUTBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x\n",
2618 __func__, start_char, stop_char);
2620 dev_dbg(dev, "%s - OUTBOUND XON/XOFF is disabled\n", __func__);
2624 /* Set flow control to the configured value */
2625 if ((!edge_serial->is_epic) ||
2626 ((edge_serial->is_epic) &&
2627 (edge_serial->epic_descriptor.Supports.IOSPSetRxFlow)))
2628 send_iosp_ext_cmd(edge_port, IOSP_CMD_SET_RX_FLOW, rxFlow);
2629 if ((!edge_serial->is_epic) ||
2630 ((edge_serial->is_epic) &&
2631 (edge_serial->epic_descriptor.Supports.IOSPSetTxFlow)))
2632 send_iosp_ext_cmd(edge_port, IOSP_CMD_SET_TX_FLOW, txFlow);
2635 edge_port->shadowLCR &= ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
2636 edge_port->shadowLCR |= (lData | lParity | lStop);
2638 edge_port->validDataMask = mask;
2640 /* Send the updated LCR value to the EdgePort */
2641 status = send_cmd_write_uart_register(edge_port, LCR,
2642 edge_port->shadowLCR);
2646 /* set up the MCR register and send it to the EdgePort */
2647 edge_port->shadowMCR = MCR_MASTER_IE;
2649 edge_port->shadowMCR |= (MCR_DTR | MCR_RTS);
2651 status = send_cmd_write_uart_register(edge_port, MCR,
2652 edge_port->shadowMCR);
2656 /* Determine divisor based on baud rate */
2657 baud = tty_get_baud_rate(tty);
2659 /* pick a default, any default... */
2663 dev_dbg(dev, "%s - baud rate = %d\n", __func__, baud);
2664 status = send_cmd_write_baud_rate(edge_port, baud);
2666 /* Speed change was not possible - put back the old speed */
2667 baud = tty_termios_baud_rate(old_termios);
2668 tty_encode_baud_rate(tty, baud, baud);
2673 /****************************************************************************
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,
2681 __le16 *unicode, int unicode_size)
2685 if (buflen <= 0) /* never happens, but... */
2687 --buflen; /* space for nul */
2689 for (i = 0; i < unicode_size; i++) {
2692 string[i] = (char)(le16_to_cpu(unicode[i]));
2698 /****************************************************************************
2699 * get_manufacturing_desc
2700 * reads in the manufacturing descriptor and stores it into the serial
2702 ****************************************************************************/
2703 static void get_manufacturing_desc(struct edgeport_serial *edge_serial)
2705 struct device *dev = &edge_serial->serial->dev->dev;
2708 dev_dbg(dev, "getting manufacturer descriptor\n");
2710 response = rom_read(edge_serial->serial,
2711 (EDGE_MANUF_DESC_ADDR & 0xffff0000) >> 16,
2712 (__u16)(EDGE_MANUF_DESC_ADDR & 0x0000ffff),
2713 EDGE_MANUF_DESC_LEN,
2714 (__u8 *)(&edge_serial->manuf_descriptor));
2717 dev_err(dev, "error in getting manufacturer descriptor\n");
2720 dev_dbg(dev, "**Manufacturer Descriptor\n");
2721 dev_dbg(dev, " RomSize: %dK\n",
2722 edge_serial->manuf_descriptor.RomSize);
2723 dev_dbg(dev, " RamSize: %dK\n",
2724 edge_serial->manuf_descriptor.RamSize);
2725 dev_dbg(dev, " CpuRev: %d\n",
2726 edge_serial->manuf_descriptor.CpuRev);
2727 dev_dbg(dev, " BoardRev: %d\n",
2728 edge_serial->manuf_descriptor.BoardRev);
2729 dev_dbg(dev, " NumPorts: %d\n",
2730 edge_serial->manuf_descriptor.NumPorts);
2731 dev_dbg(dev, " DescDate: %d/%d/%d\n",
2732 edge_serial->manuf_descriptor.DescDate[0],
2733 edge_serial->manuf_descriptor.DescDate[1],
2734 edge_serial->manuf_descriptor.DescDate[2]+1900);
2735 unicode_to_ascii(string, sizeof(string),
2736 edge_serial->manuf_descriptor.SerialNumber,
2737 edge_serial->manuf_descriptor.SerNumLength/2);
2738 dev_dbg(dev, " SerialNumber: %s\n", string);
2739 unicode_to_ascii(string, sizeof(string),
2740 edge_serial->manuf_descriptor.AssemblyNumber,
2741 edge_serial->manuf_descriptor.AssemblyNumLength/2);
2742 dev_dbg(dev, " AssemblyNumber: %s\n", string);
2743 unicode_to_ascii(string, sizeof(string),
2744 edge_serial->manuf_descriptor.OemAssyNumber,
2745 edge_serial->manuf_descriptor.OemAssyNumLength/2);
2746 dev_dbg(dev, " OemAssyNumber: %s\n", string);
2747 dev_dbg(dev, " UartType: %d\n",
2748 edge_serial->manuf_descriptor.UartType);
2749 dev_dbg(dev, " IonPid: %d\n",
2750 edge_serial->manuf_descriptor.IonPid);
2751 dev_dbg(dev, " IonConfig: %d\n",
2752 edge_serial->manuf_descriptor.IonConfig);
2757 /****************************************************************************
2759 * reads in the bootloader descriptor and stores it into the serial
2761 ****************************************************************************/
2762 static void get_boot_desc(struct edgeport_serial *edge_serial)
2764 struct device *dev = &edge_serial->serial->dev->dev;
2767 dev_dbg(dev, "getting boot descriptor\n");
2769 response = rom_read(edge_serial->serial,
2770 (EDGE_BOOT_DESC_ADDR & 0xffff0000) >> 16,
2771 (__u16)(EDGE_BOOT_DESC_ADDR & 0x0000ffff),
2773 (__u8 *)(&edge_serial->boot_descriptor));
2776 dev_err(dev, "error in getting boot descriptor\n");
2778 dev_dbg(dev, "**Boot Descriptor:\n");
2779 dev_dbg(dev, " BootCodeLength: %d\n",
2780 le16_to_cpu(edge_serial->boot_descriptor.BootCodeLength));
2781 dev_dbg(dev, " MajorVersion: %d\n",
2782 edge_serial->boot_descriptor.MajorVersion);
2783 dev_dbg(dev, " MinorVersion: %d\n",
2784 edge_serial->boot_descriptor.MinorVersion);
2785 dev_dbg(dev, " BuildNumber: %d\n",
2786 le16_to_cpu(edge_serial->boot_descriptor.BuildNumber));
2787 dev_dbg(dev, " Capabilities: 0x%x\n",
2788 le16_to_cpu(edge_serial->boot_descriptor.Capabilities));
2789 dev_dbg(dev, " UConfig0: %d\n",
2790 edge_serial->boot_descriptor.UConfig0);
2791 dev_dbg(dev, " UConfig1: %d\n",
2792 edge_serial->boot_descriptor.UConfig1);
2797 /****************************************************************************
2798 * load_application_firmware
2799 * This is called to load the application firmware to the device
2800 ****************************************************************************/
2801 static void load_application_firmware(struct edgeport_serial *edge_serial)
2803 struct device *dev = &edge_serial->serial->dev->dev;
2804 const struct ihex_binrec *rec;
2805 const struct firmware *fw;
2806 const char *fw_name;
2807 const char *fw_info;
2812 switch (edge_serial->product_info.iDownloadFile) {
2813 case EDGE_DOWNLOAD_FILE_I930:
2814 fw_info = "downloading firmware version (930)";
2815 fw_name = "edgeport/down.fw";
2818 case EDGE_DOWNLOAD_FILE_80251:
2819 fw_info = "downloading firmware version (80251)";
2820 fw_name = "edgeport/down2.fw";
2823 case EDGE_DOWNLOAD_FILE_NONE:
2824 dev_dbg(dev, "No download file specified, skipping download\n");
2831 response = request_ihex_firmware(&fw, fw_name,
2832 &edge_serial->serial->dev->dev);
2834 dev_err(dev, "Failed to load image \"%s\" err %d\n",
2839 rec = (const struct ihex_binrec *)fw->data;
2840 build = (rec->data[2] << 8) | rec->data[3];
2842 dev_dbg(dev, "%s %d.%d.%d\n", fw_info, rec->data[0], rec->data[1], build);
2844 edge_serial->product_info.FirmwareMajorVersion = rec->data[0];
2845 edge_serial->product_info.FirmwareMinorVersion = rec->data[1];
2846 edge_serial->product_info.FirmwareBuildNumber = cpu_to_le16(build);
2848 for (rec = ihex_next_binrec(rec); rec;
2849 rec = ihex_next_binrec(rec)) {
2850 Operaddr = be32_to_cpu(rec->addr);
2851 response = sram_write(edge_serial->serial,
2854 be16_to_cpu(rec->len),
2857 dev_err(&edge_serial->serial->dev->dev,
2858 "sram_write failed (%x, %x, %d)\n",
2859 Operaddr >> 16, Operaddr & 0xFFFF,
2860 be16_to_cpu(rec->len));
2865 dev_dbg(dev, "sending exec_dl_code\n");
2866 response = usb_control_msg (edge_serial->serial->dev,
2867 usb_sndctrlpipe(edge_serial->serial->dev, 0),
2868 USB_REQUEST_ION_EXEC_DL_CODE,
2869 0x40, 0x4000, 0x0001, NULL, 0, 3000);
2871 release_firmware(fw);
2875 /****************************************************************************
2877 ****************************************************************************/
2878 static int edge_startup(struct usb_serial *serial)
2880 struct edgeport_serial *edge_serial;
2881 struct edgeport_port *edge_port;
2882 struct usb_device *dev;
2883 struct device *ddev = &serial->dev->dev;
2886 bool interrupt_in_found;
2888 bool bulk_out_found;
2889 static __u32 descriptor[3] = { EDGE_COMPATIBILITY_MASK0,
2890 EDGE_COMPATIBILITY_MASK1,
2891 EDGE_COMPATIBILITY_MASK2 };
2895 /* create our private serial structure */
2896 edge_serial = kzalloc(sizeof(struct edgeport_serial), GFP_KERNEL);
2897 if (edge_serial == NULL) {
2898 dev_err(&serial->dev->dev, "%s - Out of memory\n", __func__);
2901 spin_lock_init(&edge_serial->es_lock);
2902 edge_serial->serial = serial;
2903 usb_set_serial_data(serial, edge_serial);
2905 /* get the name for the device from the device */
2906 i = usb_string(dev, dev->descriptor.iManufacturer,
2907 &edge_serial->name[0], MAX_NAME_LEN+1);
2910 edge_serial->name[i++] = ' ';
2911 usb_string(dev, dev->descriptor.iProduct,
2912 &edge_serial->name[i], MAX_NAME_LEN+2 - i);
2914 dev_info(&serial->dev->dev, "%s detected\n", edge_serial->name);
2916 /* Read the epic descriptor */
2917 if (get_epic_descriptor(edge_serial) <= 0) {
2918 /* memcpy descriptor to Supports structures */
2919 memcpy(&edge_serial->epic_descriptor.Supports, descriptor,
2920 sizeof(struct edge_compatibility_bits));
2922 /* get the manufacturing descriptor for this device */
2923 get_manufacturing_desc(edge_serial);
2925 /* get the boot descriptor */
2926 get_boot_desc(edge_serial);
2928 get_product_info(edge_serial);
2931 /* set the number of ports from the manufacturing description */
2932 /* serial->num_ports = serial->product_info.NumPorts; */
2933 if ((!edge_serial->is_epic) &&
2934 (edge_serial->product_info.NumPorts != serial->num_ports)) {
2936 "Device Reported %d serial ports vs. core thinking we have %d ports, email greg@kroah.com this information.\n",
2937 edge_serial->product_info.NumPorts,
2941 dev_dbg(ddev, "%s - time 1 %ld\n", __func__, jiffies);
2943 /* If not an EPiC device */
2944 if (!edge_serial->is_epic) {
2945 /* now load the application firmware into this device */
2946 load_application_firmware(edge_serial);
2948 dev_dbg(ddev, "%s - time 2 %ld\n", __func__, jiffies);
2950 /* Check current Edgeport EEPROM and update if necessary */
2951 update_edgeport_E2PROM(edge_serial);
2953 dev_dbg(ddev, "%s - time 3 %ld\n", __func__, jiffies);
2955 /* set the configuration to use #1 */
2956 /* dev_dbg(ddev, "set_configuration 1\n"); */
2957 /* usb_set_configuration (dev, 1); */
2959 dev_dbg(ddev, " FirmwareMajorVersion %d.%d.%d\n",
2960 edge_serial->product_info.FirmwareMajorVersion,
2961 edge_serial->product_info.FirmwareMinorVersion,
2962 le16_to_cpu(edge_serial->product_info.FirmwareBuildNumber));
2964 /* we set up the pointers to the endpoints in the edge_open function,
2965 * as the structures aren't created yet. */
2967 /* set up our port private structures */
2968 for (i = 0; i < serial->num_ports; ++i) {
2969 edge_port = kzalloc(sizeof(struct edgeport_port), GFP_KERNEL);
2970 if (edge_port == NULL) {
2971 dev_err(ddev, "%s - Out of memory\n", __func__);
2972 for (j = 0; j < i; ++j) {
2973 kfree(usb_get_serial_port_data(serial->port[j]));
2974 usb_set_serial_port_data(serial->port[j],
2977 usb_set_serial_data(serial, NULL);
2981 spin_lock_init(&edge_port->ep_lock);
2982 edge_port->port = serial->port[i];
2983 usb_set_serial_port_data(serial->port[i], edge_port);
2988 if (edge_serial->is_epic) {
2989 /* EPIC thing, set up our interrupt polling now and our read
2990 * urb, so that the device knows it really is connected. */
2991 interrupt_in_found = bulk_in_found = bulk_out_found = false;
2992 for (i = 0; i < serial->interface->altsetting[0]
2993 .desc.bNumEndpoints; ++i) {
2994 struct usb_endpoint_descriptor *endpoint;
2997 endpoint = &serial->interface->altsetting[0].
2999 buffer_size = usb_endpoint_maxp(endpoint);
3000 if (!interrupt_in_found &&
3001 (usb_endpoint_is_int_in(endpoint))) {
3002 /* we found a interrupt in endpoint */
3003 dev_dbg(ddev, "found interrupt in\n");
3005 /* not set up yet, so do it now */
3006 edge_serial->interrupt_read_urb =
3007 usb_alloc_urb(0, GFP_KERNEL);
3008 if (!edge_serial->interrupt_read_urb) {
3009 dev_err(ddev, "out of memory\n");
3012 edge_serial->interrupt_in_buffer =
3013 kmalloc(buffer_size, GFP_KERNEL);
3014 if (!edge_serial->interrupt_in_buffer) {
3015 dev_err(ddev, "out of memory\n");
3016 usb_free_urb(edge_serial->interrupt_read_urb);
3019 edge_serial->interrupt_in_endpoint =
3020 endpoint->bEndpointAddress;
3022 /* set up our interrupt urb */
3024 edge_serial->interrupt_read_urb,
3027 endpoint->bEndpointAddress),
3028 edge_serial->interrupt_in_buffer,
3030 edge_interrupt_callback,
3032 endpoint->bInterval);
3034 interrupt_in_found = true;
3037 if (!bulk_in_found &&
3038 (usb_endpoint_is_bulk_in(endpoint))) {
3039 /* we found a bulk in endpoint */
3040 dev_dbg(ddev, "found bulk in\n");
3042 /* not set up yet, so do it now */
3043 edge_serial->read_urb =
3044 usb_alloc_urb(0, GFP_KERNEL);
3045 if (!edge_serial->read_urb) {
3046 dev_err(ddev, "out of memory\n");
3049 edge_serial->bulk_in_buffer =
3050 kmalloc(buffer_size, GFP_KERNEL);
3051 if (!edge_serial->bulk_in_buffer) {
3052 dev_err(&dev->dev, "out of memory\n");
3053 usb_free_urb(edge_serial->read_urb);
3056 edge_serial->bulk_in_endpoint =
3057 endpoint->bEndpointAddress;
3059 /* set up our bulk in urb */
3060 usb_fill_bulk_urb(edge_serial->read_urb, dev,
3061 usb_rcvbulkpipe(dev,
3062 endpoint->bEndpointAddress),
3063 edge_serial->bulk_in_buffer,
3064 usb_endpoint_maxp(endpoint),
3065 edge_bulk_in_callback,
3067 bulk_in_found = true;
3070 if (!bulk_out_found &&
3071 (usb_endpoint_is_bulk_out(endpoint))) {
3072 /* we found a bulk out endpoint */
3073 dev_dbg(ddev, "found bulk out\n");
3074 edge_serial->bulk_out_endpoint =
3075 endpoint->bEndpointAddress;
3076 bulk_out_found = true;
3080 if (!interrupt_in_found || !bulk_in_found || !bulk_out_found) {
3081 dev_err(ddev, "Error - the proper endpoints were not found!\n");
3085 /* start interrupt read for this edgeport this interrupt will
3086 * continue as long as the edgeport is connected */
3087 response = usb_submit_urb(edge_serial->interrupt_read_urb,
3090 dev_err(ddev, "%s - Error %d submitting control urb\n",
3091 __func__, response);
3097 /****************************************************************************
3099 * This function is called whenever the device is removed from the usb bus.
3100 ****************************************************************************/
3101 static void edge_disconnect(struct usb_serial *serial)
3103 struct edgeport_serial *edge_serial = usb_get_serial_data(serial);
3105 /* stop reads and writes on all ports */
3106 /* free up our endpoint stuff */
3107 if (edge_serial->is_epic) {
3108 usb_kill_urb(edge_serial->interrupt_read_urb);
3109 usb_free_urb(edge_serial->interrupt_read_urb);
3110 kfree(edge_serial->interrupt_in_buffer);
3112 usb_kill_urb(edge_serial->read_urb);
3113 usb_free_urb(edge_serial->read_urb);
3114 kfree(edge_serial->bulk_in_buffer);
3119 /****************************************************************************
3121 * This function is called when the device structure is deallocated.
3122 ****************************************************************************/
3123 static void edge_release(struct usb_serial *serial)
3125 struct edgeport_serial *edge_serial = usb_get_serial_data(serial);
3128 for (i = 0; i < serial->num_ports; ++i)
3129 kfree(usb_get_serial_port_data(serial->port[i]));
3134 module_usb_serial_driver(serial_drivers, id_table_combined);
3136 MODULE_AUTHOR(DRIVER_AUTHOR);
3137 MODULE_DESCRIPTION(DRIVER_DESC);
3138 MODULE_LICENSE("GPL");
3139 MODULE_FIRMWARE("edgeport/boot.fw");
3140 MODULE_FIRMWARE("edgeport/boot2.fw");
3141 MODULE_FIRMWARE("edgeport/down.fw");
3142 MODULE_FIRMWARE("edgeport/down2.fw");
3144 module_param(debug, bool, S_IRUGO | S_IWUSR);
3145 MODULE_PARM_DESC(debug, "Debug enabled or not");