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 /* Number of outstanding Command Write Urbs */
194 static atomic_t CmdUrbs = ATOMIC_INIT(0);
197 /* local function prototypes */
199 /* function prototypes for all URB callbacks */
200 static void edge_interrupt_callback(struct urb *urb);
201 static void edge_bulk_in_callback(struct urb *urb);
202 static void edge_bulk_out_data_callback(struct urb *urb);
203 static void edge_bulk_out_cmd_callback(struct urb *urb);
205 /* function prototypes for the usbserial callbacks */
206 static int edge_open(struct tty_struct *tty, struct usb_serial_port *port);
207 static void edge_close(struct usb_serial_port *port);
208 static int edge_write(struct tty_struct *tty, struct usb_serial_port *port,
209 const unsigned char *buf, int count);
210 static int edge_write_room(struct tty_struct *tty);
211 static int edge_chars_in_buffer(struct tty_struct *tty);
212 static void edge_throttle(struct tty_struct *tty);
213 static void edge_unthrottle(struct tty_struct *tty);
214 static void edge_set_termios(struct tty_struct *tty,
215 struct usb_serial_port *port,
216 struct ktermios *old_termios);
217 static int edge_ioctl(struct tty_struct *tty,
218 unsigned int cmd, unsigned long arg);
219 static void edge_break(struct tty_struct *tty, int break_state);
220 static int edge_tiocmget(struct tty_struct *tty);
221 static int edge_tiocmset(struct tty_struct *tty,
222 unsigned int set, unsigned int clear);
223 static int edge_get_icount(struct tty_struct *tty,
224 struct serial_icounter_struct *icount);
225 static int edge_startup(struct usb_serial *serial);
226 static void edge_disconnect(struct usb_serial *serial);
227 static void edge_release(struct usb_serial *serial);
229 #include "io_tables.h" /* all of the devices that this driver supports */
231 /* function prototypes for all of our local functions */
233 static void process_rcvd_data(struct edgeport_serial *edge_serial,
234 unsigned char *buffer, __u16 bufferLength);
235 static void process_rcvd_status(struct edgeport_serial *edge_serial,
236 __u8 byte2, __u8 byte3);
237 static void edge_tty_recv(struct device *dev, struct tty_struct *tty,
238 unsigned char *data, int length);
239 static void handle_new_msr(struct edgeport_port *edge_port, __u8 newMsr);
240 static void handle_new_lsr(struct edgeport_port *edge_port, __u8 lsrData,
241 __u8 lsr, __u8 data);
242 static int send_iosp_ext_cmd(struct edgeport_port *edge_port, __u8 command,
244 static int calc_baud_rate_divisor(struct device *dev, int baud_rate, int *divisor);
245 static int send_cmd_write_baud_rate(struct edgeport_port *edge_port,
247 static void change_port_settings(struct tty_struct *tty,
248 struct edgeport_port *edge_port,
249 struct ktermios *old_termios);
250 static int send_cmd_write_uart_register(struct edgeport_port *edge_port,
251 __u8 regNum, __u8 regValue);
252 static int write_cmd_usb(struct edgeport_port *edge_port,
253 unsigned char *buffer, int writeLength);
254 static void send_more_port_data(struct edgeport_serial *edge_serial,
255 struct edgeport_port *edge_port);
257 static int sram_write(struct usb_serial *serial, __u16 extAddr, __u16 addr,
258 __u16 length, const __u8 *data);
259 static int rom_read(struct usb_serial *serial, __u16 extAddr, __u16 addr,
260 __u16 length, __u8 *data);
261 static int rom_write(struct usb_serial *serial, __u16 extAddr, __u16 addr,
262 __u16 length, const __u8 *data);
263 static void get_manufacturing_desc(struct edgeport_serial *edge_serial);
264 static void get_boot_desc(struct edgeport_serial *edge_serial);
265 static void load_application_firmware(struct edgeport_serial *edge_serial);
267 static void unicode_to_ascii(char *string, int buflen,
268 __le16 *unicode, int unicode_size);
271 /* ************************************************************************ */
272 /* ************************************************************************ */
273 /* ************************************************************************ */
274 /* ************************************************************************ */
276 /************************************************************************
278 * update_edgeport_E2PROM() Compare current versions of *
279 * Boot ROM and Manufacture *
280 * Descriptors with versions *
281 * embedded in this driver *
283 ************************************************************************/
284 static void update_edgeport_E2PROM(struct edgeport_serial *edge_serial)
286 struct device *dev = &edge_serial->serial->dev->dev;
289 __u8 BootMajorVersion;
290 __u8 BootMinorVersion;
291 __u16 BootBuildNumber;
293 const struct ihex_binrec *rec;
294 const struct firmware *fw;
298 switch (edge_serial->product_info.iDownloadFile) {
299 case EDGE_DOWNLOAD_FILE_I930:
300 fw_name = "edgeport/boot.fw";
302 case EDGE_DOWNLOAD_FILE_80251:
303 fw_name = "edgeport/boot2.fw";
309 response = request_ihex_firmware(&fw, fw_name,
310 &edge_serial->serial->dev->dev);
312 dev_err(dev, "Failed to load image \"%s\" err %d\n",
317 rec = (const struct ihex_binrec *)fw->data;
318 BootMajorVersion = rec->data[0];
319 BootMinorVersion = rec->data[1];
320 BootBuildNumber = (rec->data[2] << 8) | rec->data[3];
322 /* Check Boot Image Version */
323 BootCurVer = (edge_serial->boot_descriptor.MajorVersion << 24) +
324 (edge_serial->boot_descriptor.MinorVersion << 16) +
325 le16_to_cpu(edge_serial->boot_descriptor.BuildNumber);
327 BootNewVer = (BootMajorVersion << 24) +
328 (BootMinorVersion << 16) +
331 dev_dbg(dev, "Current Boot Image version %d.%d.%d\n",
332 edge_serial->boot_descriptor.MajorVersion,
333 edge_serial->boot_descriptor.MinorVersion,
334 le16_to_cpu(edge_serial->boot_descriptor.BuildNumber));
337 if (BootNewVer > BootCurVer) {
338 dev_dbg(dev, "**Update Boot Image from %d.%d.%d to %d.%d.%d\n",
339 edge_serial->boot_descriptor.MajorVersion,
340 edge_serial->boot_descriptor.MinorVersion,
341 le16_to_cpu(edge_serial->boot_descriptor.BuildNumber),
342 BootMajorVersion, BootMinorVersion, BootBuildNumber);
344 dev_dbg(dev, "Downloading new Boot Image\n");
346 for (rec = ihex_next_binrec(rec); rec;
347 rec = ihex_next_binrec(rec)) {
348 Bootaddr = be32_to_cpu(rec->addr);
349 response = rom_write(edge_serial->serial,
352 be16_to_cpu(rec->len),
355 dev_err(&edge_serial->serial->dev->dev,
356 "rom_write failed (%x, %x, %d)\n",
357 Bootaddr >> 16, Bootaddr & 0xFFFF,
358 be16_to_cpu(rec->len));
363 dev_dbg(dev, "Boot Image -- already up to date\n");
365 release_firmware(fw);
369 /************************************************************************
371 * Get string descriptor from device
373 ************************************************************************/
374 static int get_string_desc(struct usb_device *dev, int Id,
375 struct usb_string_descriptor **pRetDesc)
377 struct usb_string_descriptor StringDesc;
378 struct usb_string_descriptor *pStringDesc;
380 dev_dbg(&dev->dev, "%s - USB String ID = %d\n", __func__, Id);
382 if (!usb_get_descriptor(dev, USB_DT_STRING, Id, &StringDesc,
386 pStringDesc = kmalloc(StringDesc.bLength, GFP_KERNEL);
390 if (!usb_get_descriptor(dev, USB_DT_STRING, Id, pStringDesc,
391 StringDesc.bLength)) {
396 *pRetDesc = pStringDesc;
401 static void dump_product_info(struct edgeport_serial *edge_serial,
402 struct edgeport_product_info *product_info)
404 struct device *dev = &edge_serial->serial->dev->dev;
406 /* Dump Product Info structure */
407 dev_dbg(dev, "**Product Information:\n");
408 dev_dbg(dev, " ProductId %x\n", product_info->ProductId);
409 dev_dbg(dev, " NumPorts %d\n", product_info->NumPorts);
410 dev_dbg(dev, " ProdInfoVer %d\n", product_info->ProdInfoVer);
411 dev_dbg(dev, " IsServer %d\n", product_info->IsServer);
412 dev_dbg(dev, " IsRS232 %d\n", product_info->IsRS232);
413 dev_dbg(dev, " IsRS422 %d\n", product_info->IsRS422);
414 dev_dbg(dev, " IsRS485 %d\n", product_info->IsRS485);
415 dev_dbg(dev, " RomSize %d\n", product_info->RomSize);
416 dev_dbg(dev, " RamSize %d\n", product_info->RamSize);
417 dev_dbg(dev, " CpuRev %x\n", product_info->CpuRev);
418 dev_dbg(dev, " BoardRev %x\n", product_info->BoardRev);
419 dev_dbg(dev, " BootMajorVersion %d.%d.%d\n",
420 product_info->BootMajorVersion,
421 product_info->BootMinorVersion,
422 le16_to_cpu(product_info->BootBuildNumber));
423 dev_dbg(dev, " FirmwareMajorVersion %d.%d.%d\n",
424 product_info->FirmwareMajorVersion,
425 product_info->FirmwareMinorVersion,
426 le16_to_cpu(product_info->FirmwareBuildNumber));
427 dev_dbg(dev, " ManufactureDescDate %d/%d/%d\n",
428 product_info->ManufactureDescDate[0],
429 product_info->ManufactureDescDate[1],
430 product_info->ManufactureDescDate[2]+1900);
431 dev_dbg(dev, " iDownloadFile 0x%x\n",
432 product_info->iDownloadFile);
433 dev_dbg(dev, " EpicVer %d\n", product_info->EpicVer);
436 static void get_product_info(struct edgeport_serial *edge_serial)
438 struct edgeport_product_info *product_info = &edge_serial->product_info;
440 memset(product_info, 0, sizeof(struct edgeport_product_info));
442 product_info->ProductId = (__u16)(le16_to_cpu(edge_serial->serial->dev->descriptor.idProduct) & ~ION_DEVICE_ID_80251_NETCHIP);
443 product_info->NumPorts = edge_serial->manuf_descriptor.NumPorts;
444 product_info->ProdInfoVer = 0;
446 product_info->RomSize = edge_serial->manuf_descriptor.RomSize;
447 product_info->RamSize = edge_serial->manuf_descriptor.RamSize;
448 product_info->CpuRev = edge_serial->manuf_descriptor.CpuRev;
449 product_info->BoardRev = edge_serial->manuf_descriptor.BoardRev;
451 product_info->BootMajorVersion =
452 edge_serial->boot_descriptor.MajorVersion;
453 product_info->BootMinorVersion =
454 edge_serial->boot_descriptor.MinorVersion;
455 product_info->BootBuildNumber =
456 edge_serial->boot_descriptor.BuildNumber;
458 memcpy(product_info->ManufactureDescDate,
459 edge_serial->manuf_descriptor.DescDate,
460 sizeof(edge_serial->manuf_descriptor.DescDate));
462 /* check if this is 2nd generation hardware */
463 if (le16_to_cpu(edge_serial->serial->dev->descriptor.idProduct)
464 & ION_DEVICE_ID_80251_NETCHIP)
465 product_info->iDownloadFile = EDGE_DOWNLOAD_FILE_80251;
467 product_info->iDownloadFile = EDGE_DOWNLOAD_FILE_I930;
469 /* Determine Product type and set appropriate flags */
470 switch (DEVICE_ID_FROM_USB_PRODUCT_ID(product_info->ProductId)) {
471 case ION_DEVICE_ID_EDGEPORT_COMPATIBLE:
472 case ION_DEVICE_ID_EDGEPORT_4T:
473 case ION_DEVICE_ID_EDGEPORT_4:
474 case ION_DEVICE_ID_EDGEPORT_2:
475 case ION_DEVICE_ID_EDGEPORT_8_DUAL_CPU:
476 case ION_DEVICE_ID_EDGEPORT_8:
477 case ION_DEVICE_ID_EDGEPORT_421:
478 case ION_DEVICE_ID_EDGEPORT_21:
479 case ION_DEVICE_ID_EDGEPORT_2_DIN:
480 case ION_DEVICE_ID_EDGEPORT_4_DIN:
481 case ION_DEVICE_ID_EDGEPORT_16_DUAL_CPU:
482 product_info->IsRS232 = 1;
485 case ION_DEVICE_ID_EDGEPORT_2I: /* Edgeport/2 RS422/RS485 */
486 product_info->IsRS422 = 1;
487 product_info->IsRS485 = 1;
490 case ION_DEVICE_ID_EDGEPORT_8I: /* Edgeport/4 RS422 */
491 case ION_DEVICE_ID_EDGEPORT_4I: /* Edgeport/4 RS422 */
492 product_info->IsRS422 = 1;
496 dump_product_info(edge_serial, product_info);
499 static int get_epic_descriptor(struct edgeport_serial *ep)
502 struct usb_serial *serial = ep->serial;
503 struct edgeport_product_info *product_info = &ep->product_info;
504 struct edge_compatibility_descriptor *epic = &ep->epic_descriptor;
505 struct edge_compatibility_bits *bits;
506 struct device *dev = &serial->dev->dev;
509 result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
510 USB_REQUEST_ION_GET_EPIC_DESC,
512 &ep->epic_descriptor,
513 sizeof(struct edge_compatibility_descriptor),
518 memset(product_info, 0, sizeof(struct edgeport_product_info));
520 product_info->NumPorts = epic->NumPorts;
521 product_info->ProdInfoVer = 0;
522 product_info->FirmwareMajorVersion = epic->MajorVersion;
523 product_info->FirmwareMinorVersion = epic->MinorVersion;
524 product_info->FirmwareBuildNumber = epic->BuildNumber;
525 product_info->iDownloadFile = epic->iDownloadFile;
526 product_info->EpicVer = epic->EpicVer;
527 product_info->Epic = epic->Supports;
528 product_info->ProductId = ION_DEVICE_ID_EDGEPORT_COMPATIBLE;
529 dump_product_info(ep, product_info);
531 bits = &ep->epic_descriptor.Supports;
532 dev_dbg(dev, "**EPIC descriptor:\n");
533 dev_dbg(dev, " VendEnableSuspend: %s\n", bits->VendEnableSuspend ? "TRUE": "FALSE");
534 dev_dbg(dev, " IOSPOpen : %s\n", bits->IOSPOpen ? "TRUE": "FALSE");
535 dev_dbg(dev, " IOSPClose : %s\n", bits->IOSPClose ? "TRUE": "FALSE");
536 dev_dbg(dev, " IOSPChase : %s\n", bits->IOSPChase ? "TRUE": "FALSE");
537 dev_dbg(dev, " IOSPSetRxFlow : %s\n", bits->IOSPSetRxFlow ? "TRUE": "FALSE");
538 dev_dbg(dev, " IOSPSetTxFlow : %s\n", bits->IOSPSetTxFlow ? "TRUE": "FALSE");
539 dev_dbg(dev, " IOSPSetXChar : %s\n", bits->IOSPSetXChar ? "TRUE": "FALSE");
540 dev_dbg(dev, " IOSPRxCheck : %s\n", bits->IOSPRxCheck ? "TRUE": "FALSE");
541 dev_dbg(dev, " IOSPSetClrBreak : %s\n", bits->IOSPSetClrBreak ? "TRUE": "FALSE");
542 dev_dbg(dev, " IOSPWriteMCR : %s\n", bits->IOSPWriteMCR ? "TRUE": "FALSE");
543 dev_dbg(dev, " IOSPWriteLCR : %s\n", bits->IOSPWriteLCR ? "TRUE": "FALSE");
544 dev_dbg(dev, " IOSPSetBaudRate : %s\n", bits->IOSPSetBaudRate ? "TRUE": "FALSE");
545 dev_dbg(dev, " TrueEdgeport : %s\n", bits->TrueEdgeport ? "TRUE": "FALSE");
552 /************************************************************************/
553 /************************************************************************/
554 /* U S B C A L L B A C K F U N C T I O N S */
555 /* U S B C A L L B A C K F U N C T I O N S */
556 /************************************************************************/
557 /************************************************************************/
559 /*****************************************************************************
560 * edge_interrupt_callback
561 * this is the callback function for when we have received data on the
562 * interrupt endpoint.
563 *****************************************************************************/
564 static void edge_interrupt_callback(struct urb *urb)
566 struct edgeport_serial *edge_serial = urb->context;
568 struct edgeport_port *edge_port;
569 struct usb_serial_port *port;
570 struct tty_struct *tty;
571 unsigned char *data = urb->transfer_buffer;
572 int length = urb->actual_length;
578 int status = urb->status;
587 /* this urb is terminated, clean up */
588 dev_dbg(&urb->dev->dev, "%s - urb shutting down with status: %d\n", __func__, status);
591 dev_dbg(&urb->dev->dev, "%s - nonzero urb status received: %d\n", __func__, status);
595 dev = &edge_serial->serial->dev->dev;
597 /* process this interrupt-read even if there are no ports open */
599 usb_serial_debug_data(dev, __func__, length, data);
602 bytes_avail = data[0] | (data[1] << 8);
604 spin_lock(&edge_serial->es_lock);
605 edge_serial->rxBytesAvail += bytes_avail;
607 "%s - bytes_avail=%d, rxBytesAvail=%d, read_in_progress=%d\n",
608 __func__, bytes_avail,
609 edge_serial->rxBytesAvail,
610 edge_serial->read_in_progress);
612 if (edge_serial->rxBytesAvail > 0 &&
613 !edge_serial->read_in_progress) {
614 dev_dbg(dev, "%s - posting a read\n", __func__);
615 edge_serial->read_in_progress = true;
617 /* we have pending bytes on the
618 bulk in pipe, send a request */
619 result = usb_submit_urb(edge_serial->read_urb, GFP_ATOMIC);
622 "%s - usb_submit_urb(read bulk) failed with result = %d\n",
624 edge_serial->read_in_progress = false;
627 spin_unlock(&edge_serial->es_lock);
630 /* grab the txcredits for the ports if available */
633 while ((position < length) &&
634 (portNumber < edge_serial->serial->num_ports)) {
635 txCredits = data[position] | (data[position+1] << 8);
637 port = edge_serial->serial->port[portNumber];
638 edge_port = usb_get_serial_port_data(port);
639 if (edge_port->open) {
640 spin_lock(&edge_port->ep_lock);
641 edge_port->txCredits += txCredits;
642 spin_unlock(&edge_port->ep_lock);
643 dev_dbg(dev, "%s - txcredits for port%d = %d\n",
644 __func__, portNumber,
645 edge_port->txCredits);
647 /* tell the tty driver that something
649 tty = tty_port_tty_get(
650 &edge_port->port->port);
655 /* Since we have more credit, check
656 if more data can be sent */
657 send_more_port_data(edge_serial,
667 result = usb_submit_urb(urb, GFP_ATOMIC);
669 dev_err(&urb->dev->dev,
670 "%s - Error %d submitting control urb\n",
675 /*****************************************************************************
676 * edge_bulk_in_callback
677 * this is the callback function for when we have received data on the
679 *****************************************************************************/
680 static void edge_bulk_in_callback(struct urb *urb)
682 struct edgeport_serial *edge_serial = urb->context;
684 unsigned char *data = urb->transfer_buffer;
686 __u16 raw_data_length;
687 int status = urb->status;
690 dev_dbg(&urb->dev->dev, "%s - nonzero read bulk status received: %d\n",
692 edge_serial->read_in_progress = false;
696 if (urb->actual_length == 0) {
697 dev_dbg(&urb->dev->dev, "%s - read bulk callback with no data\n", __func__);
698 edge_serial->read_in_progress = false;
702 dev = &edge_serial->serial->dev->dev;
703 raw_data_length = urb->actual_length;
705 usb_serial_debug_data(dev, __func__, raw_data_length, data);
707 spin_lock(&edge_serial->es_lock);
709 /* decrement our rxBytes available by the number that we just got */
710 edge_serial->rxBytesAvail -= raw_data_length;
712 dev_dbg(dev, "%s - Received = %d, rxBytesAvail %d\n", __func__,
713 raw_data_length, edge_serial->rxBytesAvail);
715 process_rcvd_data(edge_serial, data, urb->actual_length);
717 /* check to see if there's any more data for us to read */
718 if (edge_serial->rxBytesAvail > 0) {
719 dev_dbg(dev, "%s - posting a read\n", __func__);
720 retval = usb_submit_urb(edge_serial->read_urb, GFP_ATOMIC);
723 "%s - usb_submit_urb(read bulk) failed, retval = %d\n",
725 edge_serial->read_in_progress = false;
728 edge_serial->read_in_progress = false;
731 spin_unlock(&edge_serial->es_lock);
735 /*****************************************************************************
736 * edge_bulk_out_data_callback
737 * this is the callback function for when we have finished sending
738 * serial data on the bulk out endpoint.
739 *****************************************************************************/
740 static void edge_bulk_out_data_callback(struct urb *urb)
742 struct edgeport_port *edge_port = urb->context;
743 struct tty_struct *tty;
744 int status = urb->status;
747 dev_dbg(&urb->dev->dev,
748 "%s - nonzero write bulk status received: %d\n",
752 tty = tty_port_tty_get(&edge_port->port->port);
754 if (tty && edge_port->open) {
755 /* let the tty driver wakeup if it has a special
756 write_wakeup function */
761 /* Release the Write URB */
762 edge_port->write_in_progress = false;
764 /* Check if more data needs to be sent */
765 send_more_port_data((struct edgeport_serial *)
766 (usb_get_serial_data(edge_port->port->serial)), edge_port);
770 /*****************************************************************************
772 * this is the callback function for when we have finished sending a
773 * command on the bulk out endpoint.
774 *****************************************************************************/
775 static void edge_bulk_out_cmd_callback(struct urb *urb)
777 struct edgeport_port *edge_port = urb->context;
778 struct tty_struct *tty;
779 int status = urb->status;
781 atomic_dec(&CmdUrbs);
782 dev_dbg(&urb->dev->dev, "%s - FREE URB %p (outstanding %d)\n",
783 __func__, urb, atomic_read(&CmdUrbs));
786 /* clean up the transfer buffer */
787 kfree(urb->transfer_buffer);
789 /* Free the command urb */
793 dev_dbg(&urb->dev->dev,
794 "%s - nonzero write bulk status received: %d\n",
799 /* Get pointer to tty */
800 tty = tty_port_tty_get(&edge_port->port->port);
802 /* tell the tty driver that something has changed */
803 if (tty && edge_port->open)
807 /* we have completed the command */
808 edge_port->commandPending = false;
809 wake_up(&edge_port->wait_command);
813 /*****************************************************************************
814 * Driver tty interface functions
815 *****************************************************************************/
817 /*****************************************************************************
819 * this function is called by the tty driver when a port is opened
820 * If successful, we return 0
821 * Otherwise we return a negative error number.
822 *****************************************************************************/
823 static int edge_open(struct tty_struct *tty, struct usb_serial_port *port)
825 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
826 struct device *dev = &port->dev;
827 struct usb_serial *serial;
828 struct edgeport_serial *edge_serial;
831 if (edge_port == NULL)
834 /* see if we've set up our endpoint info yet (can't set it up
835 in edge_startup as the structures were not set up at that time.) */
836 serial = port->serial;
837 edge_serial = usb_get_serial_data(serial);
838 if (edge_serial == NULL)
840 if (edge_serial->interrupt_in_buffer == NULL) {
841 struct usb_serial_port *port0 = serial->port[0];
843 /* not set up yet, so do it now */
844 edge_serial->interrupt_in_buffer =
845 port0->interrupt_in_buffer;
846 edge_serial->interrupt_in_endpoint =
847 port0->interrupt_in_endpointAddress;
848 edge_serial->interrupt_read_urb = port0->interrupt_in_urb;
849 edge_serial->bulk_in_buffer = port0->bulk_in_buffer;
850 edge_serial->bulk_in_endpoint =
851 port0->bulk_in_endpointAddress;
852 edge_serial->read_urb = port0->read_urb;
853 edge_serial->bulk_out_endpoint =
854 port0->bulk_out_endpointAddress;
856 /* set up our interrupt urb */
857 usb_fill_int_urb(edge_serial->interrupt_read_urb,
859 usb_rcvintpipe(serial->dev,
860 port0->interrupt_in_endpointAddress),
861 port0->interrupt_in_buffer,
862 edge_serial->interrupt_read_urb->transfer_buffer_length,
863 edge_interrupt_callback, edge_serial,
864 edge_serial->interrupt_read_urb->interval);
866 /* set up our bulk in urb */
867 usb_fill_bulk_urb(edge_serial->read_urb, serial->dev,
868 usb_rcvbulkpipe(serial->dev,
869 port0->bulk_in_endpointAddress),
870 port0->bulk_in_buffer,
871 edge_serial->read_urb->transfer_buffer_length,
872 edge_bulk_in_callback, edge_serial);
873 edge_serial->read_in_progress = false;
875 /* start interrupt read for this edgeport
876 * this interrupt will continue as long
877 * as the edgeport is connected */
878 response = usb_submit_urb(edge_serial->interrupt_read_urb,
881 dev_err(dev, "%s - Error %d submitting control urb\n",
886 /* initialize our wait queues */
887 init_waitqueue_head(&edge_port->wait_open);
888 init_waitqueue_head(&edge_port->wait_chase);
889 init_waitqueue_head(&edge_port->delta_msr_wait);
890 init_waitqueue_head(&edge_port->wait_command);
892 /* initialize our icount structure */
893 memset(&(edge_port->icount), 0x00, sizeof(edge_port->icount));
895 /* initialize our port settings */
896 edge_port->txCredits = 0; /* Can't send any data yet */
897 /* Must always set this bit to enable ints! */
898 edge_port->shadowMCR = MCR_MASTER_IE;
899 edge_port->chaseResponsePending = false;
901 /* send a open port command */
902 edge_port->openPending = true;
903 edge_port->open = false;
904 response = send_iosp_ext_cmd(edge_port, IOSP_CMD_OPEN_PORT, 0);
907 dev_err(dev, "%s - error sending open port command\n", __func__);
908 edge_port->openPending = false;
912 /* now wait for the port to be completely opened */
913 wait_event_timeout(edge_port->wait_open, !edge_port->openPending,
916 if (!edge_port->open) {
918 dev_dbg(dev, "%s - open timedout\n", __func__);
919 edge_port->openPending = false;
923 /* create the txfifo */
924 edge_port->txfifo.head = 0;
925 edge_port->txfifo.tail = 0;
926 edge_port->txfifo.count = 0;
927 edge_port->txfifo.size = edge_port->maxTxCredits;
928 edge_port->txfifo.fifo = kmalloc(edge_port->maxTxCredits, GFP_KERNEL);
930 if (!edge_port->txfifo.fifo) {
931 dev_dbg(dev, "%s - no memory\n", __func__);
936 /* Allocate a URB for the write */
937 edge_port->write_urb = usb_alloc_urb(0, GFP_KERNEL);
938 edge_port->write_in_progress = false;
940 if (!edge_port->write_urb) {
941 dev_dbg(dev, "%s - no memory\n", __func__);
946 dev_dbg(dev, "%s(%d) - Initialize TX fifo to %d bytes\n",
947 __func__, port->number, edge_port->maxTxCredits);
953 /************************************************************************
955 * block_until_chase_response
957 * This function will block the close until one of the following:
958 * 1. Response to our Chase comes from Edgeport
959 * 2. A timeout of 10 seconds without activity has expired
960 * (1K of Edgeport data @ 2400 baud ==> 4 sec to empty)
962 ************************************************************************/
963 static void block_until_chase_response(struct edgeport_port *edge_port)
965 struct device *dev = &edge_port->port->dev;
972 /* Save Last credits */
973 lastCredits = edge_port->txCredits;
975 /* Did we get our Chase response */
976 if (!edge_port->chaseResponsePending) {
977 dev_dbg(dev, "%s - Got Chase Response\n", __func__);
979 /* did we get all of our credit back? */
980 if (edge_port->txCredits == edge_port->maxTxCredits) {
981 dev_dbg(dev, "%s - Got all credits\n", __func__);
986 /* Block the thread for a while */
987 prepare_to_wait(&edge_port->wait_chase, &wait,
988 TASK_UNINTERRUPTIBLE);
989 schedule_timeout(timeout);
990 finish_wait(&edge_port->wait_chase, &wait);
992 if (lastCredits == edge_port->txCredits) {
993 /* No activity.. count down. */
996 edge_port->chaseResponsePending = false;
997 dev_dbg(dev, "%s - Chase TIMEOUT\n", __func__);
1001 /* Reset timeout value back to 10 seconds */
1002 dev_dbg(dev, "%s - Last %d, Current %d\n", __func__,
1003 lastCredits, edge_port->txCredits);
1010 /************************************************************************
1012 * block_until_tx_empty
1014 * This function will block the close until one of the following:
1016 * 2. The edgeport has stopped
1017 * 3. A timeout of 3 seconds without activity has expired
1019 ************************************************************************/
1020 static void block_until_tx_empty(struct edgeport_port *edge_port)
1022 struct device *dev = &edge_port->port->dev;
1024 struct TxFifo *fifo = &edge_port->txfifo;
1026 int timeout = HZ/10;
1030 /* Save Last count */
1031 lastCount = fifo->count;
1033 /* Is the Edgeport Buffer empty? */
1034 if (lastCount == 0) {
1035 dev_dbg(dev, "%s - TX Buffer Empty\n", __func__);
1039 /* Block the thread for a while */
1040 prepare_to_wait(&edge_port->wait_chase, &wait,
1041 TASK_UNINTERRUPTIBLE);
1042 schedule_timeout(timeout);
1043 finish_wait(&edge_port->wait_chase, &wait);
1045 dev_dbg(dev, "%s wait\n", __func__);
1047 if (lastCount == fifo->count) {
1048 /* No activity.. count down. */
1051 dev_dbg(dev, "%s - TIMEOUT\n", __func__);
1055 /* Reset timeout value back to seconds */
1062 /*****************************************************************************
1064 * this function is called by the tty driver when a port is closed
1065 *****************************************************************************/
1066 static void edge_close(struct usb_serial_port *port)
1068 struct edgeport_serial *edge_serial;
1069 struct edgeport_port *edge_port;
1072 edge_serial = usb_get_serial_data(port->serial);
1073 edge_port = usb_get_serial_port_data(port);
1074 if (edge_serial == NULL || edge_port == NULL)
1077 /* block until tx is empty */
1078 block_until_tx_empty(edge_port);
1080 edge_port->closePending = true;
1082 if ((!edge_serial->is_epic) ||
1083 ((edge_serial->is_epic) &&
1084 (edge_serial->epic_descriptor.Supports.IOSPChase))) {
1085 /* flush and chase */
1086 edge_port->chaseResponsePending = true;
1088 dev_dbg(&port->dev, "%s - Sending IOSP_CMD_CHASE_PORT\n", __func__);
1089 status = send_iosp_ext_cmd(edge_port, IOSP_CMD_CHASE_PORT, 0);
1091 /* block until chase finished */
1092 block_until_chase_response(edge_port);
1094 edge_port->chaseResponsePending = false;
1097 if ((!edge_serial->is_epic) ||
1098 ((edge_serial->is_epic) &&
1099 (edge_serial->epic_descriptor.Supports.IOSPClose))) {
1100 /* close the port */
1101 dev_dbg(&port->dev, "%s - Sending IOSP_CMD_CLOSE_PORT\n", __func__);
1102 send_iosp_ext_cmd(edge_port, IOSP_CMD_CLOSE_PORT, 0);
1105 /* port->close = true; */
1106 edge_port->closePending = false;
1107 edge_port->open = false;
1108 edge_port->openPending = false;
1110 usb_kill_urb(edge_port->write_urb);
1112 if (edge_port->write_urb) {
1113 /* if this urb had a transfer buffer already
1114 (old transfer) free it */
1115 kfree(edge_port->write_urb->transfer_buffer);
1116 usb_free_urb(edge_port->write_urb);
1117 edge_port->write_urb = NULL;
1119 kfree(edge_port->txfifo.fifo);
1120 edge_port->txfifo.fifo = NULL;
1123 /*****************************************************************************
1125 * this function is called by the tty driver when data should be written
1127 * If successful, we return the number of bytes written, otherwise we
1128 * return a negative error number.
1129 *****************************************************************************/
1130 static int edge_write(struct tty_struct *tty, struct usb_serial_port *port,
1131 const unsigned char *data, int count)
1133 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1134 struct TxFifo *fifo;
1139 unsigned long flags;
1141 if (edge_port == NULL)
1144 /* get a pointer to the Tx fifo */
1145 fifo = &edge_port->txfifo;
1147 spin_lock_irqsave(&edge_port->ep_lock, flags);
1149 /* calculate number of bytes to put in fifo */
1150 copySize = min((unsigned int)count,
1151 (edge_port->txCredits - fifo->count));
1153 dev_dbg(&port->dev, "%s(%d) of %d byte(s) Fifo room %d -- will copy %d bytes\n",
1154 __func__, port->number, count,
1155 edge_port->txCredits - fifo->count, copySize);
1157 /* catch writes of 0 bytes which the tty driver likes to give us,
1158 and when txCredits is empty */
1159 if (copySize == 0) {
1160 dev_dbg(&port->dev, "%s - copySize = Zero\n", __func__);
1165 * since we can never overflow the buffer we do not have to check for a
1168 * the copy is done is two parts -- first fill to the end of the buffer
1169 * then copy the reset from the start of the buffer
1171 bytesleft = fifo->size - fifo->head;
1172 firsthalf = min(bytesleft, copySize);
1173 dev_dbg(&port->dev, "%s - copy %d bytes of %d into fifo \n", __func__,
1174 firsthalf, bytesleft);
1176 /* now copy our data */
1177 memcpy(&fifo->fifo[fifo->head], data, firsthalf);
1178 usb_serial_debug_data(&port->dev, __func__, firsthalf, &fifo->fifo[fifo->head]);
1180 /* update the index and size */
1181 fifo->head += firsthalf;
1182 fifo->count += firsthalf;
1184 /* wrap the index */
1185 if (fifo->head == fifo->size)
1188 secondhalf = copySize-firsthalf;
1191 dev_dbg(&port->dev, "%s - copy rest of data %d\n", __func__, secondhalf);
1192 memcpy(&fifo->fifo[fifo->head], &data[firsthalf], secondhalf);
1193 usb_serial_debug_data(&port->dev, __func__, secondhalf, &fifo->fifo[fifo->head]);
1194 /* update the index and size */
1195 fifo->count += secondhalf;
1196 fifo->head += secondhalf;
1197 /* No need to check for wrap since we can not get to end of
1198 * the fifo in this part
1203 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1205 send_more_port_data((struct edgeport_serial *)
1206 usb_get_serial_data(port->serial), edge_port);
1208 dev_dbg(&port->dev, "%s wrote %d byte(s) TxCredits %d, Fifo %d\n",
1209 __func__, copySize, edge_port->txCredits, fifo->count);
1215 /************************************************************************
1217 * send_more_port_data()
1219 * This routine attempts to write additional UART transmit data
1220 * to a port over the USB bulk pipe. It is called (1) when new
1221 * data has been written to a port's TxBuffer from higher layers
1222 * (2) when the peripheral sends us additional TxCredits indicating
1223 * that it can accept more Tx data for a given port; and (3) when
1224 * a bulk write completes successfully and we want to see if we
1225 * can transmit more.
1227 ************************************************************************/
1228 static void send_more_port_data(struct edgeport_serial *edge_serial,
1229 struct edgeport_port *edge_port)
1231 struct TxFifo *fifo = &edge_port->txfifo;
1232 struct device *dev = &edge_port->port->dev;
1234 unsigned char *buffer;
1240 unsigned long flags;
1242 spin_lock_irqsave(&edge_port->ep_lock, flags);
1244 if (edge_port->write_in_progress ||
1246 (fifo->count == 0)) {
1247 dev_dbg(dev, "%s(%d) EXIT - fifo %d, PendingWrite = %d\n",
1248 __func__, edge_port->port->number,
1249 fifo->count, edge_port->write_in_progress);
1253 /* since the amount of data in the fifo will always fit into the
1254 * edgeport buffer we do not need to check the write length
1256 * Do we have enough credits for this port to make it worthwhile
1257 * to bother queueing a write. If it's too small, say a few bytes,
1258 * it's better to wait for more credits so we can do a larger write.
1260 if (edge_port->txCredits < EDGE_FW_GET_TX_CREDITS_SEND_THRESHOLD(edge_port->maxTxCredits, EDGE_FW_BULK_MAX_PACKET_SIZE)) {
1261 dev_dbg(dev, "%s(%d) Not enough credit - fifo %d TxCredit %d\n",
1262 __func__, edge_port->port->number, fifo->count,
1263 edge_port->txCredits);
1267 /* lock this write */
1268 edge_port->write_in_progress = true;
1270 /* get a pointer to the write_urb */
1271 urb = edge_port->write_urb;
1273 /* make sure transfer buffer is freed */
1274 kfree(urb->transfer_buffer);
1275 urb->transfer_buffer = NULL;
1277 /* build the data header for the buffer and port that we are about
1279 count = fifo->count;
1280 buffer = kmalloc(count+2, GFP_ATOMIC);
1281 if (buffer == NULL) {
1282 dev_err_console(edge_port->port,
1283 "%s - no more kernel memory...\n", __func__);
1284 edge_port->write_in_progress = false;
1287 buffer[0] = IOSP_BUILD_DATA_HDR1(edge_port->port->number
1288 - edge_port->port->serial->minor, count);
1289 buffer[1] = IOSP_BUILD_DATA_HDR2(edge_port->port->number
1290 - edge_port->port->serial->minor, count);
1292 /* now copy our data */
1293 bytesleft = fifo->size - fifo->tail;
1294 firsthalf = min(bytesleft, count);
1295 memcpy(&buffer[2], &fifo->fifo[fifo->tail], firsthalf);
1296 fifo->tail += firsthalf;
1297 fifo->count -= firsthalf;
1298 if (fifo->tail == fifo->size)
1301 secondhalf = count-firsthalf;
1303 memcpy(&buffer[2+firsthalf], &fifo->fifo[fifo->tail],
1305 fifo->tail += secondhalf;
1306 fifo->count -= secondhalf;
1310 usb_serial_debug_data(&edge_port->port->dev, __func__, count, &buffer[2]);
1312 /* fill up the urb with all of our data and submit it */
1313 usb_fill_bulk_urb(urb, edge_serial->serial->dev,
1314 usb_sndbulkpipe(edge_serial->serial->dev,
1315 edge_serial->bulk_out_endpoint),
1317 edge_bulk_out_data_callback, edge_port);
1319 /* decrement the number of credits we have by the number we just sent */
1320 edge_port->txCredits -= count;
1321 edge_port->icount.tx += count;
1323 status = usb_submit_urb(urb, GFP_ATOMIC);
1325 /* something went wrong */
1326 dev_err_console(edge_port->port,
1327 "%s - usb_submit_urb(write bulk) failed, status = %d, data lost\n",
1329 edge_port->write_in_progress = false;
1331 /* revert the credits as something bad happened. */
1332 edge_port->txCredits += count;
1333 edge_port->icount.tx -= count;
1335 dev_dbg(dev, "%s wrote %d byte(s) TxCredit %d, Fifo %d\n",
1336 __func__, count, edge_port->txCredits, fifo->count);
1339 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1343 /*****************************************************************************
1345 * this function is called by the tty driver when it wants to know how
1346 * many bytes of data we can accept for a specific port. If successful,
1347 * we return the amount of room that we have for this port (the txCredits)
1348 * otherwise we return a negative error number.
1349 *****************************************************************************/
1350 static int edge_write_room(struct tty_struct *tty)
1352 struct usb_serial_port *port = tty->driver_data;
1353 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1355 unsigned long flags;
1357 if (edge_port == NULL)
1359 if (edge_port->closePending)
1362 if (!edge_port->open) {
1363 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
1367 /* total of both buffers is still txCredit */
1368 spin_lock_irqsave(&edge_port->ep_lock, flags);
1369 room = edge_port->txCredits - edge_port->txfifo.count;
1370 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1372 dev_dbg(&port->dev, "%s - returns %d\n", __func__, room);
1377 /*****************************************************************************
1378 * edge_chars_in_buffer
1379 * this function is called by the tty driver when it wants to know how
1380 * many bytes of data we currently have outstanding in the port (data that
1381 * has been written, but hasn't made it out the port yet)
1382 * If successful, we return the number of bytes left to be written in the
1384 * Otherwise we return a negative error number.
1385 *****************************************************************************/
1386 static int edge_chars_in_buffer(struct tty_struct *tty)
1388 struct usb_serial_port *port = tty->driver_data;
1389 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1391 unsigned long flags;
1393 if (edge_port == NULL)
1395 if (edge_port->closePending)
1398 if (!edge_port->open) {
1399 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
1403 spin_lock_irqsave(&edge_port->ep_lock, flags);
1404 num_chars = edge_port->maxTxCredits - edge_port->txCredits +
1405 edge_port->txfifo.count;
1406 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1408 dev_dbg(&port->dev, "%s(port %d) - returns %d\n", __func__,
1409 port->number, num_chars);
1416 /*****************************************************************************
1418 * this function is called by the tty driver when it wants to stop the data
1419 * being read from the port.
1420 *****************************************************************************/
1421 static void edge_throttle(struct tty_struct *tty)
1423 struct usb_serial_port *port = tty->driver_data;
1424 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1427 if (edge_port == NULL)
1430 if (!edge_port->open) {
1431 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
1435 /* if we are implementing XON/XOFF, send the stop character */
1437 unsigned char stop_char = STOP_CHAR(tty);
1438 status = edge_write(tty, port, &stop_char, 1);
1443 /* if we are implementing RTS/CTS, toggle that line */
1444 if (tty->termios.c_cflag & CRTSCTS) {
1445 edge_port->shadowMCR &= ~MCR_RTS;
1446 status = send_cmd_write_uart_register(edge_port, MCR,
1447 edge_port->shadowMCR);
1454 /*****************************************************************************
1456 * this function is called by the tty driver when it wants to resume the
1457 * data being read from the port (called after SerialThrottle is called)
1458 *****************************************************************************/
1459 static void edge_unthrottle(struct tty_struct *tty)
1461 struct usb_serial_port *port = tty->driver_data;
1462 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1465 if (edge_port == NULL)
1468 if (!edge_port->open) {
1469 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
1473 /* if we are implementing XON/XOFF, send the start character */
1475 unsigned char start_char = START_CHAR(tty);
1476 status = edge_write(tty, port, &start_char, 1);
1480 /* if we are implementing RTS/CTS, toggle that line */
1481 if (tty->termios.c_cflag & CRTSCTS) {
1482 edge_port->shadowMCR |= MCR_RTS;
1483 send_cmd_write_uart_register(edge_port, MCR,
1484 edge_port->shadowMCR);
1489 /*****************************************************************************
1491 * this function is called by the tty driver when it wants to change
1492 * the termios structure
1493 *****************************************************************************/
1494 static void edge_set_termios(struct tty_struct *tty,
1495 struct usb_serial_port *port, struct ktermios *old_termios)
1497 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1500 cflag = tty->termios.c_cflag;
1501 dev_dbg(&port->dev, "%s - clfag %08x iflag %08x\n", __func__, tty->termios.c_cflag, tty->termios.c_iflag);
1502 dev_dbg(&port->dev, "%s - old clfag %08x old iflag %08x\n", __func__, old_termios->c_cflag, old_termios->c_iflag);
1504 if (edge_port == NULL)
1507 if (!edge_port->open) {
1508 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
1512 /* change the port settings to the new ones specified */
1513 change_port_settings(tty, edge_port, old_termios);
1517 /*****************************************************************************
1518 * get_lsr_info - get line status register info
1520 * Purpose: Let user call ioctl() to get info when the UART physically
1521 * is emptied. On bus types like RS485, the transmitter must
1522 * release the bus after transmitting. This must be done when
1523 * the transmit shift register is empty, not be done when the
1524 * transmit holding register is empty. This functionality
1525 * allows an RS485 driver to be written in user space.
1526 *****************************************************************************/
1527 static int get_lsr_info(struct edgeport_port *edge_port,
1528 unsigned int __user *value)
1530 unsigned int result = 0;
1531 unsigned long flags;
1533 spin_lock_irqsave(&edge_port->ep_lock, flags);
1534 if (edge_port->maxTxCredits == edge_port->txCredits &&
1535 edge_port->txfifo.count == 0) {
1536 dev_dbg(&edge_port->port->dev, "%s -- Empty\n", __func__);
1537 result = TIOCSER_TEMT;
1539 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1541 if (copy_to_user(value, &result, sizeof(int)))
1546 static int edge_tiocmset(struct tty_struct *tty,
1547 unsigned int set, unsigned int clear)
1549 struct usb_serial_port *port = tty->driver_data;
1550 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1553 mcr = edge_port->shadowMCR;
1554 if (set & TIOCM_RTS)
1556 if (set & TIOCM_DTR)
1558 if (set & TIOCM_LOOP)
1559 mcr |= MCR_LOOPBACK;
1561 if (clear & TIOCM_RTS)
1563 if (clear & TIOCM_DTR)
1565 if (clear & TIOCM_LOOP)
1566 mcr &= ~MCR_LOOPBACK;
1568 edge_port->shadowMCR = mcr;
1570 send_cmd_write_uart_register(edge_port, MCR, edge_port->shadowMCR);
1575 static int edge_tiocmget(struct tty_struct *tty)
1577 struct usb_serial_port *port = tty->driver_data;
1578 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1579 unsigned int result = 0;
1583 msr = edge_port->shadowMSR;
1584 mcr = edge_port->shadowMCR;
1585 result = ((mcr & MCR_DTR) ? TIOCM_DTR: 0) /* 0x002 */
1586 | ((mcr & MCR_RTS) ? TIOCM_RTS: 0) /* 0x004 */
1587 | ((msr & EDGEPORT_MSR_CTS) ? TIOCM_CTS: 0) /* 0x020 */
1588 | ((msr & EDGEPORT_MSR_CD) ? TIOCM_CAR: 0) /* 0x040 */
1589 | ((msr & EDGEPORT_MSR_RI) ? TIOCM_RI: 0) /* 0x080 */
1590 | ((msr & EDGEPORT_MSR_DSR) ? TIOCM_DSR: 0); /* 0x100 */
1595 static int edge_get_icount(struct tty_struct *tty,
1596 struct serial_icounter_struct *icount)
1598 struct usb_serial_port *port = tty->driver_data;
1599 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1600 struct async_icount cnow;
1601 cnow = edge_port->icount;
1603 icount->cts = cnow.cts;
1604 icount->dsr = cnow.dsr;
1605 icount->rng = cnow.rng;
1606 icount->dcd = cnow.dcd;
1607 icount->rx = cnow.rx;
1608 icount->tx = cnow.tx;
1609 icount->frame = cnow.frame;
1610 icount->overrun = cnow.overrun;
1611 icount->parity = cnow.parity;
1612 icount->brk = cnow.brk;
1613 icount->buf_overrun = cnow.buf_overrun;
1615 dev_dbg(&port->dev, "%s (%d) TIOCGICOUNT RX=%d, TX=%d\n", __func__,
1616 port->number, icount->rx, icount->tx);
1620 static int get_serial_info(struct edgeport_port *edge_port,
1621 struct serial_struct __user *retinfo)
1623 struct serial_struct tmp;
1628 memset(&tmp, 0, sizeof(tmp));
1630 tmp.type = PORT_16550A;
1631 tmp.line = edge_port->port->serial->minor;
1632 tmp.port = edge_port->port->number;
1634 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
1635 tmp.xmit_fifo_size = edge_port->maxTxCredits;
1636 tmp.baud_base = 9600;
1637 tmp.close_delay = 5*HZ;
1638 tmp.closing_wait = 30*HZ;
1640 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1646 /*****************************************************************************
1648 * this function handles any ioctl calls to the driver
1649 *****************************************************************************/
1650 static int edge_ioctl(struct tty_struct *tty,
1651 unsigned int cmd, unsigned long arg)
1653 struct usb_serial_port *port = tty->driver_data;
1655 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1656 struct async_icount cnow;
1657 struct async_icount cprev;
1659 dev_dbg(&port->dev, "%s - port %d, cmd = 0x%x\n", __func__, port->number, cmd);
1663 dev_dbg(&port->dev, "%s (%d) TIOCSERGETLSR\n", __func__, port->number);
1664 return get_lsr_info(edge_port, (unsigned int __user *) arg);
1667 dev_dbg(&port->dev, "%s (%d) TIOCGSERIAL\n", __func__, port->number);
1668 return get_serial_info(edge_port, (struct serial_struct __user *) arg);
1671 dev_dbg(&port->dev, "%s (%d) TIOCMIWAIT\n", __func__, port->number);
1672 cprev = edge_port->icount;
1674 prepare_to_wait(&edge_port->delta_msr_wait,
1675 &wait, TASK_INTERRUPTIBLE);
1677 finish_wait(&edge_port->delta_msr_wait, &wait);
1678 /* see if a signal did it */
1679 if (signal_pending(current))
1680 return -ERESTARTSYS;
1681 cnow = edge_port->icount;
1682 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
1683 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
1684 return -EIO; /* no change => error */
1685 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1686 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1687 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1688 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
1697 return -ENOIOCTLCMD;
1701 /*****************************************************************************
1703 * this function sends a break to the port
1704 *****************************************************************************/
1705 static void edge_break(struct tty_struct *tty, int break_state)
1707 struct usb_serial_port *port = tty->driver_data;
1708 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1709 struct edgeport_serial *edge_serial = usb_get_serial_data(port->serial);
1712 if ((!edge_serial->is_epic) ||
1713 ((edge_serial->is_epic) &&
1714 (edge_serial->epic_descriptor.Supports.IOSPChase))) {
1715 /* flush and chase */
1716 edge_port->chaseResponsePending = true;
1718 dev_dbg(&port->dev, "%s - Sending IOSP_CMD_CHASE_PORT\n", __func__);
1719 status = send_iosp_ext_cmd(edge_port, IOSP_CMD_CHASE_PORT, 0);
1721 /* block until chase finished */
1722 block_until_chase_response(edge_port);
1724 edge_port->chaseResponsePending = false;
1728 if ((!edge_serial->is_epic) ||
1729 ((edge_serial->is_epic) &&
1730 (edge_serial->epic_descriptor.Supports.IOSPSetClrBreak))) {
1731 if (break_state == -1) {
1732 dev_dbg(&port->dev, "%s - Sending IOSP_CMD_SET_BREAK\n", __func__);
1733 status = send_iosp_ext_cmd(edge_port,
1734 IOSP_CMD_SET_BREAK, 0);
1736 dev_dbg(&port->dev, "%s - Sending IOSP_CMD_CLEAR_BREAK\n", __func__);
1737 status = send_iosp_ext_cmd(edge_port,
1738 IOSP_CMD_CLEAR_BREAK, 0);
1741 dev_dbg(&port->dev, "%s - error sending break set/clear command.\n",
1747 /*****************************************************************************
1749 * this function handles the data received on the bulk in pipe.
1750 *****************************************************************************/
1751 static void process_rcvd_data(struct edgeport_serial *edge_serial,
1752 unsigned char *buffer, __u16 bufferLength)
1754 struct device *dev = &edge_serial->serial->dev->dev;
1755 struct usb_serial_port *port;
1756 struct edgeport_port *edge_port;
1757 struct tty_struct *tty;
1758 __u16 lastBufferLength;
1761 lastBufferLength = bufferLength + 1;
1763 while (bufferLength > 0) {
1764 /* failsafe incase we get a message that we don't understand */
1765 if (lastBufferLength == bufferLength) {
1766 dev_dbg(dev, "%s - stuck in loop, exiting it.\n", __func__);
1769 lastBufferLength = bufferLength;
1771 switch (edge_serial->rxState) {
1773 edge_serial->rxHeader1 = *buffer;
1777 if (bufferLength == 0) {
1778 edge_serial->rxState = EXPECT_HDR2;
1781 /* otherwise, drop on through */
1783 edge_serial->rxHeader2 = *buffer;
1787 dev_dbg(dev, "%s - Hdr1=%02X Hdr2=%02X\n", __func__,
1788 edge_serial->rxHeader1, edge_serial->rxHeader2);
1789 /* Process depending on whether this header is
1792 if (IS_CMD_STAT_HDR(edge_serial->rxHeader1)) {
1793 /* Decode this status header and go to
1794 * EXPECT_HDR1 (if we can process the status
1795 * with only 2 bytes), or go to EXPECT_HDR3 to
1796 * get the third byte. */
1797 edge_serial->rxPort =
1798 IOSP_GET_HDR_PORT(edge_serial->rxHeader1);
1799 edge_serial->rxStatusCode =
1800 IOSP_GET_STATUS_CODE(
1801 edge_serial->rxHeader1);
1803 if (!IOSP_STATUS_IS_2BYTE(
1804 edge_serial->rxStatusCode)) {
1805 /* This status needs additional bytes.
1806 * Save what we have and then wait for
1809 edge_serial->rxStatusParam
1810 = edge_serial->rxHeader2;
1811 edge_serial->rxState = EXPECT_HDR3;
1814 /* We have all the header bytes, process the
1816 process_rcvd_status(edge_serial,
1817 edge_serial->rxHeader2, 0);
1818 edge_serial->rxState = EXPECT_HDR1;
1821 edge_serial->rxPort =
1822 IOSP_GET_HDR_PORT(edge_serial->rxHeader1);
1823 edge_serial->rxBytesRemaining =
1824 IOSP_GET_HDR_DATA_LEN(
1825 edge_serial->rxHeader1,
1826 edge_serial->rxHeader2);
1827 dev_dbg(dev, "%s - Data for Port %u Len %u\n",
1829 edge_serial->rxPort,
1830 edge_serial->rxBytesRemaining);
1832 /* ASSERT(DevExt->RxPort < DevExt->NumPorts);
1833 * ASSERT(DevExt->RxBytesRemaining <
1834 * IOSP_MAX_DATA_LENGTH);
1837 if (bufferLength == 0) {
1838 edge_serial->rxState = EXPECT_DATA;
1841 /* Else, drop through */
1843 case EXPECT_DATA: /* Expect data */
1844 if (bufferLength < edge_serial->rxBytesRemaining) {
1845 rxLen = bufferLength;
1846 /* Expect data to start next buffer */
1847 edge_serial->rxState = EXPECT_DATA;
1849 /* BufLen >= RxBytesRemaining */
1850 rxLen = edge_serial->rxBytesRemaining;
1851 /* Start another header next time */
1852 edge_serial->rxState = EXPECT_HDR1;
1855 bufferLength -= rxLen;
1856 edge_serial->rxBytesRemaining -= rxLen;
1858 /* spit this data back into the tty driver if this
1861 port = edge_serial->serial->port[
1862 edge_serial->rxPort];
1863 edge_port = usb_get_serial_port_data(port);
1864 if (edge_port->open) {
1865 tty = tty_port_tty_get(
1866 &edge_port->port->port);
1868 dev_dbg(dev, "%s - Sending %d bytes to TTY for port %d\n",
1869 __func__, rxLen, edge_serial->rxPort);
1870 edge_tty_recv(&edge_serial->serial->dev->dev, tty, buffer, rxLen);
1873 edge_port->icount.rx += rxLen;
1879 case EXPECT_HDR3: /* Expect 3rd byte of status header */
1880 edge_serial->rxHeader3 = *buffer;
1884 /* We have all the header bytes, process the
1886 process_rcvd_status(edge_serial,
1887 edge_serial->rxStatusParam,
1888 edge_serial->rxHeader3);
1889 edge_serial->rxState = EXPECT_HDR1;
1896 /*****************************************************************************
1897 * process_rcvd_status
1898 * this function handles the any status messages received on the
1900 *****************************************************************************/
1901 static void process_rcvd_status(struct edgeport_serial *edge_serial,
1902 __u8 byte2, __u8 byte3)
1904 struct usb_serial_port *port;
1905 struct edgeport_port *edge_port;
1906 struct tty_struct *tty;
1908 __u8 code = edge_serial->rxStatusCode;
1910 /* switch the port pointer to the one being currently talked about */
1911 port = edge_serial->serial->port[edge_serial->rxPort];
1912 edge_port = usb_get_serial_port_data(port);
1913 if (edge_port == NULL) {
1914 dev_err(&edge_serial->serial->dev->dev,
1915 "%s - edge_port == NULL for port %d\n",
1916 __func__, edge_serial->rxPort);
1921 if (code == IOSP_EXT_STATUS) {
1923 case IOSP_EXT_STATUS_CHASE_RSP:
1924 /* we want to do EXT status regardless of port
1926 dev_dbg(dev, "%s - Port %u EXT CHASE_RSP Data = %02x\n",
1927 __func__, edge_serial->rxPort, byte3);
1928 /* Currently, the only EXT_STATUS is Chase, so process
1929 * here instead of one more call to one more subroutine
1930 * If/when more EXT_STATUS, there'll be more work to do
1931 * Also, we currently clear flag and close the port
1932 * regardless of content of above's Byte3.
1933 * We could choose to do something else when Byte3 says
1934 * Timeout on Chase from Edgeport, like wait longer in
1935 * block_until_chase_response, but for now we don't.
1937 edge_port->chaseResponsePending = false;
1938 wake_up(&edge_port->wait_chase);
1941 case IOSP_EXT_STATUS_RX_CHECK_RSP:
1942 dev_dbg(dev, "%s ========== Port %u CHECK_RSP Sequence = %02x =============\n",
1943 __func__, edge_serial->rxPort, byte3);
1944 /* Port->RxCheckRsp = true; */
1949 if (code == IOSP_STATUS_OPEN_RSP) {
1950 edge_port->txCredits = GET_TX_BUFFER_SIZE(byte3);
1951 edge_port->maxTxCredits = edge_port->txCredits;
1952 dev_dbg(dev, "%s - Port %u Open Response Initial MSR = %02x TxBufferSize = %d\n",
1953 __func__, edge_serial->rxPort, byte2, edge_port->txCredits);
1954 handle_new_msr(edge_port, byte2);
1956 /* send the current line settings to the port so we are
1957 in sync with any further termios calls */
1958 tty = tty_port_tty_get(&edge_port->port->port);
1960 change_port_settings(tty,
1961 edge_port, &tty->termios);
1965 /* we have completed the open */
1966 edge_port->openPending = false;
1967 edge_port->open = true;
1968 wake_up(&edge_port->wait_open);
1972 /* If port is closed, silently discard all rcvd status. We can
1973 * have cases where buffered status is received AFTER the close
1974 * port command is sent to the Edgeport.
1976 if (!edge_port->open || edge_port->closePending)
1980 /* Not currently sent by Edgeport */
1981 case IOSP_STATUS_LSR:
1982 dev_dbg(dev, "%s - Port %u LSR Status = %02x\n",
1983 __func__, edge_serial->rxPort, byte2);
1984 handle_new_lsr(edge_port, false, byte2, 0);
1987 case IOSP_STATUS_LSR_DATA:
1988 dev_dbg(dev, "%s - Port %u LSR Status = %02x, Data = %02x\n",
1989 __func__, edge_serial->rxPort, byte2, byte3);
1990 /* byte2 is LSR Register */
1991 /* byte3 is broken data byte */
1992 handle_new_lsr(edge_port, true, byte2, byte3);
1995 * case IOSP_EXT_4_STATUS:
1996 * dev_dbg(dev, "%s - Port %u LSR Status = %02x Data = %02x\n",
1997 * __func__, edge_serial->rxPort, byte2, byte3);
2000 case IOSP_STATUS_MSR:
2001 dev_dbg(dev, "%s - Port %u MSR Status = %02x\n",
2002 __func__, edge_serial->rxPort, byte2);
2004 * Process this new modem status and generate appropriate
2005 * events, etc, based on the new status. This routine
2006 * also saves the MSR in Port->ShadowMsr.
2008 handle_new_msr(edge_port, byte2);
2012 dev_dbg(dev, "%s - Unrecognized IOSP status code %u\n", __func__, code);
2018 /*****************************************************************************
2020 * this function passes data on to the tty flip buffer
2021 *****************************************************************************/
2022 static void edge_tty_recv(struct device *dev, struct tty_struct *tty,
2023 unsigned char *data, int length)
2027 cnt = tty_insert_flip_string(tty, data, length);
2029 dev_err(dev, "%s - dropping data, %d bytes lost\n",
2030 __func__, length - cnt);
2035 tty_flip_buffer_push(tty);
2039 /*****************************************************************************
2041 * this function handles any change to the msr register for a port.
2042 *****************************************************************************/
2043 static void handle_new_msr(struct edgeport_port *edge_port, __u8 newMsr)
2045 struct async_icount *icount;
2047 if (newMsr & (EDGEPORT_MSR_DELTA_CTS | EDGEPORT_MSR_DELTA_DSR |
2048 EDGEPORT_MSR_DELTA_RI | EDGEPORT_MSR_DELTA_CD)) {
2049 icount = &edge_port->icount;
2051 /* update input line counters */
2052 if (newMsr & EDGEPORT_MSR_DELTA_CTS)
2054 if (newMsr & EDGEPORT_MSR_DELTA_DSR)
2056 if (newMsr & EDGEPORT_MSR_DELTA_CD)
2058 if (newMsr & EDGEPORT_MSR_DELTA_RI)
2060 wake_up_interruptible(&edge_port->delta_msr_wait);
2063 /* Save the new modem status */
2064 edge_port->shadowMSR = newMsr & 0xf0;
2068 /*****************************************************************************
2070 * this function handles any change to the lsr register for a port.
2071 *****************************************************************************/
2072 static void handle_new_lsr(struct edgeport_port *edge_port, __u8 lsrData,
2073 __u8 lsr, __u8 data)
2075 __u8 newLsr = (__u8) (lsr & (__u8)
2076 (LSR_OVER_ERR | LSR_PAR_ERR | LSR_FRM_ERR | LSR_BREAK));
2077 struct async_icount *icount;
2079 edge_port->shadowLSR = lsr;
2081 if (newLsr & LSR_BREAK) {
2083 * Parity and Framing errors only count if they
2084 * occur exclusive of a break being
2087 newLsr &= (__u8)(LSR_OVER_ERR | LSR_BREAK);
2090 /* Place LSR data byte into Rx buffer */
2092 struct tty_struct *tty =
2093 tty_port_tty_get(&edge_port->port->port);
2095 edge_tty_recv(&edge_port->port->dev, tty, &data, 1);
2099 /* update input line counters */
2100 icount = &edge_port->icount;
2101 if (newLsr & LSR_BREAK)
2103 if (newLsr & LSR_OVER_ERR)
2105 if (newLsr & LSR_PAR_ERR)
2107 if (newLsr & LSR_FRM_ERR)
2112 /****************************************************************************
2114 * writes a number of bytes to the Edgeport device's sram starting at the
2116 * If successful returns the number of bytes written, otherwise it returns
2117 * a negative error number of the problem.
2118 ****************************************************************************/
2119 static int sram_write(struct usb_serial *serial, __u16 extAddr, __u16 addr,
2120 __u16 length, const __u8 *data)
2123 __u16 current_length;
2124 unsigned char *transfer_buffer;
2126 dev_dbg(&serial->dev->dev, "%s - %x, %x, %d\n", __func__, extAddr, addr, length);
2128 transfer_buffer = kmalloc(64, GFP_KERNEL);
2129 if (!transfer_buffer) {
2130 dev_err(&serial->dev->dev, "%s - kmalloc(%d) failed.\n",
2135 /* need to split these writes up into 64 byte chunks */
2137 while (length > 0) {
2139 current_length = 64;
2141 current_length = length;
2143 /* dev_dbg(&serial->dev->dev, "%s - writing %x, %x, %d\n", __func__, extAddr, addr, current_length); */
2144 memcpy(transfer_buffer, data, current_length);
2145 result = usb_control_msg(serial->dev,
2146 usb_sndctrlpipe(serial->dev, 0),
2147 USB_REQUEST_ION_WRITE_RAM,
2148 0x40, addr, extAddr, transfer_buffer,
2149 current_length, 300);
2152 length -= current_length;
2153 addr += current_length;
2154 data += current_length;
2157 kfree(transfer_buffer);
2162 /****************************************************************************
2164 * writes a number of bytes to the Edgeport device's ROM starting at the
2166 * If successful returns the number of bytes written, otherwise it returns
2167 * a negative error number of the problem.
2168 ****************************************************************************/
2169 static int rom_write(struct usb_serial *serial, __u16 extAddr, __u16 addr,
2170 __u16 length, const __u8 *data)
2173 __u16 current_length;
2174 unsigned char *transfer_buffer;
2176 transfer_buffer = kmalloc(64, GFP_KERNEL);
2177 if (!transfer_buffer) {
2178 dev_err(&serial->dev->dev, "%s - kmalloc(%d) failed.\n",
2183 /* need to split these writes up into 64 byte chunks */
2185 while (length > 0) {
2187 current_length = 64;
2189 current_length = length;
2190 memcpy(transfer_buffer, data, current_length);
2191 result = usb_control_msg(serial->dev,
2192 usb_sndctrlpipe(serial->dev, 0),
2193 USB_REQUEST_ION_WRITE_ROM, 0x40,
2195 transfer_buffer, current_length, 300);
2198 length -= current_length;
2199 addr += current_length;
2200 data += current_length;
2203 kfree(transfer_buffer);
2208 /****************************************************************************
2210 * reads a number of bytes from the Edgeport device starting at the given
2212 * If successful returns the number of bytes read, otherwise it returns
2213 * a negative error number of the problem.
2214 ****************************************************************************/
2215 static int rom_read(struct usb_serial *serial, __u16 extAddr,
2216 __u16 addr, __u16 length, __u8 *data)
2219 __u16 current_length;
2220 unsigned char *transfer_buffer;
2222 transfer_buffer = kmalloc(64, GFP_KERNEL);
2223 if (!transfer_buffer) {
2224 dev_err(&serial->dev->dev,
2225 "%s - kmalloc(%d) failed.\n", __func__, 64);
2229 /* need to split these reads up into 64 byte chunks */
2231 while (length > 0) {
2233 current_length = 64;
2235 current_length = length;
2236 result = usb_control_msg(serial->dev,
2237 usb_rcvctrlpipe(serial->dev, 0),
2238 USB_REQUEST_ION_READ_ROM,
2239 0xC0, addr, extAddr, transfer_buffer,
2240 current_length, 300);
2243 memcpy(data, transfer_buffer, current_length);
2244 length -= current_length;
2245 addr += current_length;
2246 data += current_length;
2249 kfree(transfer_buffer);
2254 /****************************************************************************
2256 * Is used to send a IOSP message to the Edgeport device
2257 ****************************************************************************/
2258 static int send_iosp_ext_cmd(struct edgeport_port *edge_port,
2259 __u8 command, __u8 param)
2261 unsigned char *buffer;
2262 unsigned char *currentCommand;
2266 buffer = kmalloc(10, GFP_ATOMIC);
2268 dev_err(&edge_port->port->dev,
2269 "%s - kmalloc(%d) failed.\n", __func__, 10);
2273 currentCommand = buffer;
2275 MAKE_CMD_EXT_CMD(¤tCommand, &length,
2276 edge_port->port->number - edge_port->port->serial->minor,
2279 status = write_cmd_usb(edge_port, buffer, length);
2281 /* something bad happened, let's free up the memory */
2289 /*****************************************************************************
2291 * this function writes the given buffer out to the bulk write endpoint.
2292 *****************************************************************************/
2293 static int write_cmd_usb(struct edgeport_port *edge_port,
2294 unsigned char *buffer, int length)
2296 struct edgeport_serial *edge_serial =
2297 usb_get_serial_data(edge_port->port->serial);
2298 struct device *dev = &edge_port->port->dev;
2302 usb_serial_debug_data(dev, __func__, length, buffer);
2304 /* Allocate our next urb */
2305 urb = usb_alloc_urb(0, GFP_ATOMIC);
2309 atomic_inc(&CmdUrbs);
2310 dev_dbg(dev, "%s - ALLOCATE URB %p (outstanding %d)\n",
2311 __func__, urb, atomic_read(&CmdUrbs));
2313 usb_fill_bulk_urb(urb, edge_serial->serial->dev,
2314 usb_sndbulkpipe(edge_serial->serial->dev,
2315 edge_serial->bulk_out_endpoint),
2316 buffer, length, edge_bulk_out_cmd_callback, edge_port);
2318 edge_port->commandPending = true;
2319 status = usb_submit_urb(urb, GFP_ATOMIC);
2322 /* something went wrong */
2323 dev_err(dev, "%s - usb_submit_urb(write command) failed, status = %d\n",
2327 atomic_dec(&CmdUrbs);
2332 wait_event(&edge_port->wait_command, !edge_port->commandPending);
2334 if (edge_port->commandPending) {
2335 /* command timed out */
2336 dev_dbg(dev, "%s - command timed out\n", __func__);
2344 /*****************************************************************************
2345 * send_cmd_write_baud_rate
2346 * this function sends the proper command to change the baud rate of the
2348 *****************************************************************************/
2349 static int send_cmd_write_baud_rate(struct edgeport_port *edge_port,
2352 struct edgeport_serial *edge_serial =
2353 usb_get_serial_data(edge_port->port->serial);
2354 struct device *dev = &edge_port->port->dev;
2355 unsigned char *cmdBuffer;
2356 unsigned char *currCmd;
2360 unsigned char number =
2361 edge_port->port->number - edge_port->port->serial->minor;
2363 if (edge_serial->is_epic &&
2364 !edge_serial->epic_descriptor.Supports.IOSPSetBaudRate) {
2365 dev_dbg(dev, "SendCmdWriteBaudRate - NOT Setting baud rate for port = %d, baud = %d\n",
2366 edge_port->port->number, baudRate);
2370 dev_dbg(dev, "%s - port = %d, baud = %d\n", __func__,
2371 edge_port->port->number, baudRate);
2373 status = calc_baud_rate_divisor(dev, baudRate, &divisor);
2375 dev_err(dev, "%s - bad baud rate\n", __func__);
2379 /* Alloc memory for the string of commands. */
2380 cmdBuffer = kmalloc(0x100, GFP_ATOMIC);
2382 dev_err(dev, "%s - kmalloc(%d) failed.\n", __func__, 0x100);
2385 currCmd = cmdBuffer;
2387 /* Enable access to divisor latch */
2388 MAKE_CMD_WRITE_REG(&currCmd, &cmdLen, number, LCR, LCR_DL_ENABLE);
2390 /* Write the divisor itself */
2391 MAKE_CMD_WRITE_REG(&currCmd, &cmdLen, number, DLL, LOW8(divisor));
2392 MAKE_CMD_WRITE_REG(&currCmd, &cmdLen, number, DLM, HIGH8(divisor));
2394 /* Restore original value to disable access to divisor latch */
2395 MAKE_CMD_WRITE_REG(&currCmd, &cmdLen, number, LCR,
2396 edge_port->shadowLCR);
2398 status = write_cmd_usb(edge_port, cmdBuffer, cmdLen);
2400 /* something bad happened, let's free up the memory */
2408 /*****************************************************************************
2409 * calc_baud_rate_divisor
2410 * this function calculates the proper baud rate divisor for the specified
2412 *****************************************************************************/
2413 static int calc_baud_rate_divisor(struct device *dev, int baudrate, int *divisor)
2418 for (i = 0; i < ARRAY_SIZE(divisor_table); i++) {
2419 if (divisor_table[i].BaudRate == baudrate) {
2420 *divisor = divisor_table[i].Divisor;
2425 /* We have tried all of the standard baud rates
2426 * lets try to calculate the divisor for this baud rate
2427 * Make sure the baud rate is reasonable */
2428 if (baudrate > 50 && baudrate < 230400) {
2430 custom = (__u16)((230400L + baudrate/2) / baudrate);
2434 dev_dbg(dev, "%s - Baud %d = %d\n", __func__, baudrate, custom);
2442 /*****************************************************************************
2443 * send_cmd_write_uart_register
2444 * this function builds up a uart register message and sends to the device.
2445 *****************************************************************************/
2446 static int send_cmd_write_uart_register(struct edgeport_port *edge_port,
2447 __u8 regNum, __u8 regValue)
2449 struct edgeport_serial *edge_serial =
2450 usb_get_serial_data(edge_port->port->serial);
2451 struct device *dev = &edge_port->port->dev;
2452 unsigned char *cmdBuffer;
2453 unsigned char *currCmd;
2454 unsigned long cmdLen = 0;
2457 dev_dbg(dev, "%s - write to %s register 0x%02x\n",
2458 (regNum == MCR) ? "MCR" : "LCR", __func__, regValue);
2460 if (edge_serial->is_epic &&
2461 !edge_serial->epic_descriptor.Supports.IOSPWriteMCR &&
2463 dev_dbg(dev, "SendCmdWriteUartReg - Not writing to MCR Register\n");
2467 if (edge_serial->is_epic &&
2468 !edge_serial->epic_descriptor.Supports.IOSPWriteLCR &&
2470 dev_dbg(dev, "SendCmdWriteUartReg - Not writing to LCR Register\n");
2474 /* Alloc memory for the string of commands. */
2475 cmdBuffer = kmalloc(0x10, GFP_ATOMIC);
2476 if (cmdBuffer == NULL)
2479 currCmd = cmdBuffer;
2481 /* Build a cmd in the buffer to write the given register */
2482 MAKE_CMD_WRITE_REG(&currCmd, &cmdLen,
2483 edge_port->port->number - edge_port->port->serial->minor,
2486 status = write_cmd_usb(edge_port, cmdBuffer, cmdLen);
2488 /* something bad happened, let's free up the memory */
2496 /*****************************************************************************
2497 * change_port_settings
2498 * This routine is called to set the UART on the device to match the
2499 * specified new settings.
2500 *****************************************************************************/
2502 static void change_port_settings(struct tty_struct *tty,
2503 struct edgeport_port *edge_port, struct ktermios *old_termios)
2505 struct device *dev = &edge_port->port->dev;
2506 struct edgeport_serial *edge_serial =
2507 usb_get_serial_data(edge_port->port->serial);
2518 dev_dbg(dev, "%s - port %d\n", __func__, edge_port->port->number);
2520 if (!edge_port->open &&
2521 !edge_port->openPending) {
2522 dev_dbg(dev, "%s - port not opened\n", __func__);
2526 cflag = tty->termios.c_cflag;
2528 switch (cflag & CSIZE) {
2530 lData = LCR_BITS_5; mask = 0x1f;
2531 dev_dbg(dev, "%s - data bits = 5\n", __func__);
2534 lData = LCR_BITS_6; mask = 0x3f;
2535 dev_dbg(dev, "%s - data bits = 6\n", __func__);
2538 lData = LCR_BITS_7; mask = 0x7f;
2539 dev_dbg(dev, "%s - data bits = 7\n", __func__);
2544 dev_dbg(dev, "%s - data bits = 8\n", __func__);
2548 lParity = LCR_PAR_NONE;
2549 if (cflag & PARENB) {
2550 if (cflag & CMSPAR) {
2551 if (cflag & PARODD) {
2552 lParity = LCR_PAR_MARK;
2553 dev_dbg(dev, "%s - parity = mark\n", __func__);
2555 lParity = LCR_PAR_SPACE;
2556 dev_dbg(dev, "%s - parity = space\n", __func__);
2558 } else if (cflag & PARODD) {
2559 lParity = LCR_PAR_ODD;
2560 dev_dbg(dev, "%s - parity = odd\n", __func__);
2562 lParity = LCR_PAR_EVEN;
2563 dev_dbg(dev, "%s - parity = even\n", __func__);
2566 dev_dbg(dev, "%s - parity = none\n", __func__);
2569 if (cflag & CSTOPB) {
2571 dev_dbg(dev, "%s - stop bits = 2\n", __func__);
2574 dev_dbg(dev, "%s - stop bits = 1\n", __func__);
2577 /* figure out the flow control settings */
2578 rxFlow = txFlow = 0x00;
2579 if (cflag & CRTSCTS) {
2580 rxFlow |= IOSP_RX_FLOW_RTS;
2581 txFlow |= IOSP_TX_FLOW_CTS;
2582 dev_dbg(dev, "%s - RTS/CTS is enabled\n", __func__);
2584 dev_dbg(dev, "%s - RTS/CTS is disabled\n", __func__);
2587 /* if we are implementing XON/XOFF, set the start and stop character
2589 if (I_IXOFF(tty) || I_IXON(tty)) {
2590 unsigned char stop_char = STOP_CHAR(tty);
2591 unsigned char start_char = START_CHAR(tty);
2593 if ((!edge_serial->is_epic) ||
2594 ((edge_serial->is_epic) &&
2595 (edge_serial->epic_descriptor.Supports.IOSPSetXChar))) {
2596 send_iosp_ext_cmd(edge_port,
2597 IOSP_CMD_SET_XON_CHAR, start_char);
2598 send_iosp_ext_cmd(edge_port,
2599 IOSP_CMD_SET_XOFF_CHAR, stop_char);
2602 /* if we are implementing INBOUND XON/XOFF */
2604 rxFlow |= IOSP_RX_FLOW_XON_XOFF;
2605 dev_dbg(dev, "%s - INBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x\n",
2606 __func__, start_char, stop_char);
2608 dev_dbg(dev, "%s - INBOUND XON/XOFF is disabled\n", __func__);
2611 /* if we are implementing OUTBOUND XON/XOFF */
2613 txFlow |= IOSP_TX_FLOW_XON_XOFF;
2614 dev_dbg(dev, "%s - OUTBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x\n",
2615 __func__, start_char, stop_char);
2617 dev_dbg(dev, "%s - OUTBOUND XON/XOFF is disabled\n", __func__);
2621 /* Set flow control to the configured value */
2622 if ((!edge_serial->is_epic) ||
2623 ((edge_serial->is_epic) &&
2624 (edge_serial->epic_descriptor.Supports.IOSPSetRxFlow)))
2625 send_iosp_ext_cmd(edge_port, IOSP_CMD_SET_RX_FLOW, rxFlow);
2626 if ((!edge_serial->is_epic) ||
2627 ((edge_serial->is_epic) &&
2628 (edge_serial->epic_descriptor.Supports.IOSPSetTxFlow)))
2629 send_iosp_ext_cmd(edge_port, IOSP_CMD_SET_TX_FLOW, txFlow);
2632 edge_port->shadowLCR &= ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
2633 edge_port->shadowLCR |= (lData | lParity | lStop);
2635 edge_port->validDataMask = mask;
2637 /* Send the updated LCR value to the EdgePort */
2638 status = send_cmd_write_uart_register(edge_port, LCR,
2639 edge_port->shadowLCR);
2643 /* set up the MCR register and send it to the EdgePort */
2644 edge_port->shadowMCR = MCR_MASTER_IE;
2646 edge_port->shadowMCR |= (MCR_DTR | MCR_RTS);
2648 status = send_cmd_write_uart_register(edge_port, MCR,
2649 edge_port->shadowMCR);
2653 /* Determine divisor based on baud rate */
2654 baud = tty_get_baud_rate(tty);
2656 /* pick a default, any default... */
2660 dev_dbg(dev, "%s - baud rate = %d\n", __func__, baud);
2661 status = send_cmd_write_baud_rate(edge_port, baud);
2663 /* Speed change was not possible - put back the old speed */
2664 baud = tty_termios_baud_rate(old_termios);
2665 tty_encode_baud_rate(tty, baud, baud);
2670 /****************************************************************************
2672 * Turns a string from Unicode into ASCII.
2673 * Doesn't do a good job with any characters that are outside the normal
2674 * ASCII range, but it's only for debugging...
2675 * NOTE: expects the unicode in LE format
2676 ****************************************************************************/
2677 static void unicode_to_ascii(char *string, int buflen,
2678 __le16 *unicode, int unicode_size)
2682 if (buflen <= 0) /* never happens, but... */
2684 --buflen; /* space for nul */
2686 for (i = 0; i < unicode_size; i++) {
2689 string[i] = (char)(le16_to_cpu(unicode[i]));
2695 /****************************************************************************
2696 * get_manufacturing_desc
2697 * reads in the manufacturing descriptor and stores it into the serial
2699 ****************************************************************************/
2700 static void get_manufacturing_desc(struct edgeport_serial *edge_serial)
2702 struct device *dev = &edge_serial->serial->dev->dev;
2705 dev_dbg(dev, "getting manufacturer descriptor\n");
2707 response = rom_read(edge_serial->serial,
2708 (EDGE_MANUF_DESC_ADDR & 0xffff0000) >> 16,
2709 (__u16)(EDGE_MANUF_DESC_ADDR & 0x0000ffff),
2710 EDGE_MANUF_DESC_LEN,
2711 (__u8 *)(&edge_serial->manuf_descriptor));
2714 dev_err(dev, "error in getting manufacturer descriptor\n");
2717 dev_dbg(dev, "**Manufacturer Descriptor\n");
2718 dev_dbg(dev, " RomSize: %dK\n",
2719 edge_serial->manuf_descriptor.RomSize);
2720 dev_dbg(dev, " RamSize: %dK\n",
2721 edge_serial->manuf_descriptor.RamSize);
2722 dev_dbg(dev, " CpuRev: %d\n",
2723 edge_serial->manuf_descriptor.CpuRev);
2724 dev_dbg(dev, " BoardRev: %d\n",
2725 edge_serial->manuf_descriptor.BoardRev);
2726 dev_dbg(dev, " NumPorts: %d\n",
2727 edge_serial->manuf_descriptor.NumPorts);
2728 dev_dbg(dev, " DescDate: %d/%d/%d\n",
2729 edge_serial->manuf_descriptor.DescDate[0],
2730 edge_serial->manuf_descriptor.DescDate[1],
2731 edge_serial->manuf_descriptor.DescDate[2]+1900);
2732 unicode_to_ascii(string, sizeof(string),
2733 edge_serial->manuf_descriptor.SerialNumber,
2734 edge_serial->manuf_descriptor.SerNumLength/2);
2735 dev_dbg(dev, " SerialNumber: %s\n", string);
2736 unicode_to_ascii(string, sizeof(string),
2737 edge_serial->manuf_descriptor.AssemblyNumber,
2738 edge_serial->manuf_descriptor.AssemblyNumLength/2);
2739 dev_dbg(dev, " AssemblyNumber: %s\n", string);
2740 unicode_to_ascii(string, sizeof(string),
2741 edge_serial->manuf_descriptor.OemAssyNumber,
2742 edge_serial->manuf_descriptor.OemAssyNumLength/2);
2743 dev_dbg(dev, " OemAssyNumber: %s\n", string);
2744 dev_dbg(dev, " UartType: %d\n",
2745 edge_serial->manuf_descriptor.UartType);
2746 dev_dbg(dev, " IonPid: %d\n",
2747 edge_serial->manuf_descriptor.IonPid);
2748 dev_dbg(dev, " IonConfig: %d\n",
2749 edge_serial->manuf_descriptor.IonConfig);
2754 /****************************************************************************
2756 * reads in the bootloader descriptor and stores it into the serial
2758 ****************************************************************************/
2759 static void get_boot_desc(struct edgeport_serial *edge_serial)
2761 struct device *dev = &edge_serial->serial->dev->dev;
2764 dev_dbg(dev, "getting boot descriptor\n");
2766 response = rom_read(edge_serial->serial,
2767 (EDGE_BOOT_DESC_ADDR & 0xffff0000) >> 16,
2768 (__u16)(EDGE_BOOT_DESC_ADDR & 0x0000ffff),
2770 (__u8 *)(&edge_serial->boot_descriptor));
2773 dev_err(dev, "error in getting boot descriptor\n");
2775 dev_dbg(dev, "**Boot Descriptor:\n");
2776 dev_dbg(dev, " BootCodeLength: %d\n",
2777 le16_to_cpu(edge_serial->boot_descriptor.BootCodeLength));
2778 dev_dbg(dev, " MajorVersion: %d\n",
2779 edge_serial->boot_descriptor.MajorVersion);
2780 dev_dbg(dev, " MinorVersion: %d\n",
2781 edge_serial->boot_descriptor.MinorVersion);
2782 dev_dbg(dev, " BuildNumber: %d\n",
2783 le16_to_cpu(edge_serial->boot_descriptor.BuildNumber));
2784 dev_dbg(dev, " Capabilities: 0x%x\n",
2785 le16_to_cpu(edge_serial->boot_descriptor.Capabilities));
2786 dev_dbg(dev, " UConfig0: %d\n",
2787 edge_serial->boot_descriptor.UConfig0);
2788 dev_dbg(dev, " UConfig1: %d\n",
2789 edge_serial->boot_descriptor.UConfig1);
2794 /****************************************************************************
2795 * load_application_firmware
2796 * This is called to load the application firmware to the device
2797 ****************************************************************************/
2798 static void load_application_firmware(struct edgeport_serial *edge_serial)
2800 struct device *dev = &edge_serial->serial->dev->dev;
2801 const struct ihex_binrec *rec;
2802 const struct firmware *fw;
2803 const char *fw_name;
2804 const char *fw_info;
2809 switch (edge_serial->product_info.iDownloadFile) {
2810 case EDGE_DOWNLOAD_FILE_I930:
2811 fw_info = "downloading firmware version (930)";
2812 fw_name = "edgeport/down.fw";
2815 case EDGE_DOWNLOAD_FILE_80251:
2816 fw_info = "downloading firmware version (80251)";
2817 fw_name = "edgeport/down2.fw";
2820 case EDGE_DOWNLOAD_FILE_NONE:
2821 dev_dbg(dev, "No download file specified, skipping download\n");
2828 response = request_ihex_firmware(&fw, fw_name,
2829 &edge_serial->serial->dev->dev);
2831 dev_err(dev, "Failed to load image \"%s\" err %d\n",
2836 rec = (const struct ihex_binrec *)fw->data;
2837 build = (rec->data[2] << 8) | rec->data[3];
2839 dev_dbg(dev, "%s %d.%d.%d\n", fw_info, rec->data[0], rec->data[1], build);
2841 edge_serial->product_info.FirmwareMajorVersion = rec->data[0];
2842 edge_serial->product_info.FirmwareMinorVersion = rec->data[1];
2843 edge_serial->product_info.FirmwareBuildNumber = cpu_to_le16(build);
2845 for (rec = ihex_next_binrec(rec); rec;
2846 rec = ihex_next_binrec(rec)) {
2847 Operaddr = be32_to_cpu(rec->addr);
2848 response = sram_write(edge_serial->serial,
2851 be16_to_cpu(rec->len),
2854 dev_err(&edge_serial->serial->dev->dev,
2855 "sram_write failed (%x, %x, %d)\n",
2856 Operaddr >> 16, Operaddr & 0xFFFF,
2857 be16_to_cpu(rec->len));
2862 dev_dbg(dev, "sending exec_dl_code\n");
2863 response = usb_control_msg (edge_serial->serial->dev,
2864 usb_sndctrlpipe(edge_serial->serial->dev, 0),
2865 USB_REQUEST_ION_EXEC_DL_CODE,
2866 0x40, 0x4000, 0x0001, NULL, 0, 3000);
2868 release_firmware(fw);
2872 /****************************************************************************
2874 ****************************************************************************/
2875 static int edge_startup(struct usb_serial *serial)
2877 struct edgeport_serial *edge_serial;
2878 struct edgeport_port *edge_port;
2879 struct usb_device *dev;
2880 struct device *ddev = &serial->dev->dev;
2883 bool interrupt_in_found;
2885 bool bulk_out_found;
2886 static __u32 descriptor[3] = { EDGE_COMPATIBILITY_MASK0,
2887 EDGE_COMPATIBILITY_MASK1,
2888 EDGE_COMPATIBILITY_MASK2 };
2892 /* create our private serial structure */
2893 edge_serial = kzalloc(sizeof(struct edgeport_serial), GFP_KERNEL);
2894 if (edge_serial == NULL) {
2895 dev_err(&serial->dev->dev, "%s - Out of memory\n", __func__);
2898 spin_lock_init(&edge_serial->es_lock);
2899 edge_serial->serial = serial;
2900 usb_set_serial_data(serial, edge_serial);
2902 /* get the name for the device from the device */
2903 i = usb_string(dev, dev->descriptor.iManufacturer,
2904 &edge_serial->name[0], MAX_NAME_LEN+1);
2907 edge_serial->name[i++] = ' ';
2908 usb_string(dev, dev->descriptor.iProduct,
2909 &edge_serial->name[i], MAX_NAME_LEN+2 - i);
2911 dev_info(&serial->dev->dev, "%s detected\n", edge_serial->name);
2913 /* Read the epic descriptor */
2914 if (get_epic_descriptor(edge_serial) <= 0) {
2915 /* memcpy descriptor to Supports structures */
2916 memcpy(&edge_serial->epic_descriptor.Supports, descriptor,
2917 sizeof(struct edge_compatibility_bits));
2919 /* get the manufacturing descriptor for this device */
2920 get_manufacturing_desc(edge_serial);
2922 /* get the boot descriptor */
2923 get_boot_desc(edge_serial);
2925 get_product_info(edge_serial);
2928 /* set the number of ports from the manufacturing description */
2929 /* serial->num_ports = serial->product_info.NumPorts; */
2930 if ((!edge_serial->is_epic) &&
2931 (edge_serial->product_info.NumPorts != serial->num_ports)) {
2933 "Device Reported %d serial ports vs. core thinking we have %d ports, email greg@kroah.com this information.\n",
2934 edge_serial->product_info.NumPorts,
2938 dev_dbg(ddev, "%s - time 1 %ld\n", __func__, jiffies);
2940 /* If not an EPiC device */
2941 if (!edge_serial->is_epic) {
2942 /* now load the application firmware into this device */
2943 load_application_firmware(edge_serial);
2945 dev_dbg(ddev, "%s - time 2 %ld\n", __func__, jiffies);
2947 /* Check current Edgeport EEPROM and update if necessary */
2948 update_edgeport_E2PROM(edge_serial);
2950 dev_dbg(ddev, "%s - time 3 %ld\n", __func__, jiffies);
2952 /* set the configuration to use #1 */
2953 /* dev_dbg(ddev, "set_configuration 1\n"); */
2954 /* usb_set_configuration (dev, 1); */
2956 dev_dbg(ddev, " FirmwareMajorVersion %d.%d.%d\n",
2957 edge_serial->product_info.FirmwareMajorVersion,
2958 edge_serial->product_info.FirmwareMinorVersion,
2959 le16_to_cpu(edge_serial->product_info.FirmwareBuildNumber));
2961 /* we set up the pointers to the endpoints in the edge_open function,
2962 * as the structures aren't created yet. */
2964 /* set up our port private structures */
2965 for (i = 0; i < serial->num_ports; ++i) {
2966 edge_port = kzalloc(sizeof(struct edgeport_port), GFP_KERNEL);
2967 if (edge_port == NULL) {
2968 dev_err(ddev, "%s - Out of memory\n", __func__);
2969 for (j = 0; j < i; ++j) {
2970 kfree(usb_get_serial_port_data(serial->port[j]));
2971 usb_set_serial_port_data(serial->port[j],
2974 usb_set_serial_data(serial, NULL);
2978 spin_lock_init(&edge_port->ep_lock);
2979 edge_port->port = serial->port[i];
2980 usb_set_serial_port_data(serial->port[i], edge_port);
2985 if (edge_serial->is_epic) {
2986 /* EPIC thing, set up our interrupt polling now and our read
2987 * urb, so that the device knows it really is connected. */
2988 interrupt_in_found = bulk_in_found = bulk_out_found = false;
2989 for (i = 0; i < serial->interface->altsetting[0]
2990 .desc.bNumEndpoints; ++i) {
2991 struct usb_endpoint_descriptor *endpoint;
2994 endpoint = &serial->interface->altsetting[0].
2996 buffer_size = usb_endpoint_maxp(endpoint);
2997 if (!interrupt_in_found &&
2998 (usb_endpoint_is_int_in(endpoint))) {
2999 /* we found a interrupt in endpoint */
3000 dev_dbg(ddev, "found interrupt in\n");
3002 /* not set up yet, so do it now */
3003 edge_serial->interrupt_read_urb =
3004 usb_alloc_urb(0, GFP_KERNEL);
3005 if (!edge_serial->interrupt_read_urb) {
3006 dev_err(ddev, "out of memory\n");
3009 edge_serial->interrupt_in_buffer =
3010 kmalloc(buffer_size, GFP_KERNEL);
3011 if (!edge_serial->interrupt_in_buffer) {
3012 dev_err(ddev, "out of memory\n");
3013 usb_free_urb(edge_serial->interrupt_read_urb);
3016 edge_serial->interrupt_in_endpoint =
3017 endpoint->bEndpointAddress;
3019 /* set up our interrupt urb */
3021 edge_serial->interrupt_read_urb,
3024 endpoint->bEndpointAddress),
3025 edge_serial->interrupt_in_buffer,
3027 edge_interrupt_callback,
3029 endpoint->bInterval);
3031 interrupt_in_found = true;
3034 if (!bulk_in_found &&
3035 (usb_endpoint_is_bulk_in(endpoint))) {
3036 /* we found a bulk in endpoint */
3037 dev_dbg(ddev, "found bulk in\n");
3039 /* not set up yet, so do it now */
3040 edge_serial->read_urb =
3041 usb_alloc_urb(0, GFP_KERNEL);
3042 if (!edge_serial->read_urb) {
3043 dev_err(ddev, "out of memory\n");
3046 edge_serial->bulk_in_buffer =
3047 kmalloc(buffer_size, GFP_KERNEL);
3048 if (!edge_serial->bulk_in_buffer) {
3049 dev_err(&dev->dev, "out of memory\n");
3050 usb_free_urb(edge_serial->read_urb);
3053 edge_serial->bulk_in_endpoint =
3054 endpoint->bEndpointAddress;
3056 /* set up our bulk in urb */
3057 usb_fill_bulk_urb(edge_serial->read_urb, dev,
3058 usb_rcvbulkpipe(dev,
3059 endpoint->bEndpointAddress),
3060 edge_serial->bulk_in_buffer,
3061 usb_endpoint_maxp(endpoint),
3062 edge_bulk_in_callback,
3064 bulk_in_found = true;
3067 if (!bulk_out_found &&
3068 (usb_endpoint_is_bulk_out(endpoint))) {
3069 /* we found a bulk out endpoint */
3070 dev_dbg(ddev, "found bulk out\n");
3071 edge_serial->bulk_out_endpoint =
3072 endpoint->bEndpointAddress;
3073 bulk_out_found = true;
3077 if (!interrupt_in_found || !bulk_in_found || !bulk_out_found) {
3078 dev_err(ddev, "Error - the proper endpoints were not found!\n");
3082 /* start interrupt read for this edgeport this interrupt will
3083 * continue as long as the edgeport is connected */
3084 response = usb_submit_urb(edge_serial->interrupt_read_urb,
3087 dev_err(ddev, "%s - Error %d submitting control urb\n",
3088 __func__, response);
3094 /****************************************************************************
3096 * This function is called whenever the device is removed from the usb bus.
3097 ****************************************************************************/
3098 static void edge_disconnect(struct usb_serial *serial)
3100 struct edgeport_serial *edge_serial = usb_get_serial_data(serial);
3102 /* stop reads and writes on all ports */
3103 /* free up our endpoint stuff */
3104 if (edge_serial->is_epic) {
3105 usb_kill_urb(edge_serial->interrupt_read_urb);
3106 usb_free_urb(edge_serial->interrupt_read_urb);
3107 kfree(edge_serial->interrupt_in_buffer);
3109 usb_kill_urb(edge_serial->read_urb);
3110 usb_free_urb(edge_serial->read_urb);
3111 kfree(edge_serial->bulk_in_buffer);
3116 /****************************************************************************
3118 * This function is called when the device structure is deallocated.
3119 ****************************************************************************/
3120 static void edge_release(struct usb_serial *serial)
3122 struct edgeport_serial *edge_serial = usb_get_serial_data(serial);
3125 for (i = 0; i < serial->num_ports; ++i)
3126 kfree(usb_get_serial_port_data(serial->port[i]));
3131 module_usb_serial_driver(serial_drivers, id_table_combined);
3133 MODULE_AUTHOR(DRIVER_AUTHOR);
3134 MODULE_DESCRIPTION(DRIVER_DESC);
3135 MODULE_LICENSE("GPL");
3136 MODULE_FIRMWARE("edgeport/boot.fw");
3137 MODULE_FIRMWARE("edgeport/boot2.fw");
3138 MODULE_FIRMWARE("edgeport/down.fw");
3139 MODULE_FIRMWARE("edgeport/down2.fw");