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 */
54 #define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com> and David Iacovelli"
55 #define DRIVER_DESC "Edgeport USB Serial Driver"
57 #define MAX_NAME_LEN 64
59 #define CHASE_TIMEOUT (5*HZ) /* 5 seconds */
60 #define OPEN_TIMEOUT (5*HZ) /* 5 seconds */
61 #define COMMAND_TIMEOUT (5*HZ) /* 5 seconds */
63 /* receive port state */
65 EXPECT_HDR1 = 0, /* Expect header byte 1 */
66 EXPECT_HDR2 = 1, /* Expect header byte 2 */
67 EXPECT_DATA = 2, /* Expect 'RxBytesRemaining' data */
68 EXPECT_HDR3 = 3, /* Expect header byte 3 (for status hdrs only) */
73 * This Transmit queue is an extension of the edgeport Rx buffer.
74 * The maximum amount of data buffered in both the edgeport
75 * Rx buffer (maxTxCredits) and this buffer will never exceed maxTxCredits.
78 unsigned int head; /* index to head pointer (write) */
79 unsigned int tail; /* index to tail pointer (read) */
80 unsigned int count; /* Bytes in queue */
81 unsigned int size; /* Max size of queue (equal to Max number of TxCredits) */
82 unsigned char *fifo; /* allocated Buffer */
85 /* This structure holds all of the local port information */
86 struct edgeport_port {
87 __u16 txCredits; /* our current credits for this port */
88 __u16 maxTxCredits; /* the max size of the port */
90 struct TxFifo txfifo; /* transmit fifo -- size will be maxTxCredits */
91 struct urb *write_urb; /* write URB for this port */
92 bool write_in_progress; /* 'true' while a write URB is outstanding */
95 __u8 shadowLCR; /* last LCR value received */
96 __u8 shadowMCR; /* last MCR value received */
97 __u8 shadowMSR; /* last MSR value received */
98 __u8 shadowLSR; /* last LSR value received */
99 __u8 shadowXonChar; /* last value set as XON char in Edgeport */
100 __u8 shadowXoffChar; /* last value set as XOFF char in Edgeport */
108 bool chaseResponsePending;
110 wait_queue_head_t wait_chase; /* for handling sleeping while waiting for chase to finish */
111 wait_queue_head_t wait_open; /* for handling sleeping while waiting for open to finish */
112 wait_queue_head_t wait_command; /* for handling sleeping while waiting for command to finish */
113 wait_queue_head_t delta_msr_wait; /* for handling sleeping while waiting for msr change to happen */
115 struct async_icount icount;
116 struct usb_serial_port *port; /* loop back to the owner of this object */
120 /* This structure holds all of the individual device information */
121 struct edgeport_serial {
122 char name[MAX_NAME_LEN+2]; /* string name of this device */
124 struct edge_manuf_descriptor manuf_descriptor; /* the manufacturer descriptor */
125 struct edge_boot_descriptor boot_descriptor; /* the boot firmware descriptor */
126 struct edgeport_product_info product_info; /* Product Info */
127 struct edge_compatibility_descriptor epic_descriptor; /* Edgeport compatible descriptor */
128 int is_epic; /* flag if EPiC device or not */
130 __u8 interrupt_in_endpoint; /* the interrupt endpoint handle */
131 unsigned char *interrupt_in_buffer; /* the buffer we use for the interrupt endpoint */
132 struct urb *interrupt_read_urb; /* our interrupt urb */
134 __u8 bulk_in_endpoint; /* the bulk in endpoint handle */
135 unsigned char *bulk_in_buffer; /* the buffer we use for the bulk in endpoint */
136 struct urb *read_urb; /* our bulk read urb */
137 bool read_in_progress;
140 __u8 bulk_out_endpoint; /* the bulk out endpoint handle */
142 __s16 rxBytesAvail; /* the number of bytes that we need to read from this device */
144 enum RXSTATE rxState; /* the current state of the bulk receive processor */
145 __u8 rxHeader1; /* receive header byte 1 */
146 __u8 rxHeader2; /* receive header byte 2 */
147 __u8 rxHeader3; /* receive header byte 3 */
148 __u8 rxPort; /* the port that we are currently receiving data for */
149 __u8 rxStatusCode; /* the receive status code */
150 __u8 rxStatusParam; /* the receive status paramater */
151 __s16 rxBytesRemaining; /* the number of port bytes left to read */
152 struct usb_serial *serial; /* loop back to the owner of this object */
155 /* baud rate information */
156 struct divisor_table_entry {
162 * Define table of divisors for Rev A EdgePort/4 hardware
163 * These assume a 3.6864MHz crystal, the standard /16, and
167 static const struct divisor_table_entry divisor_table[] = {
170 { 110, 2095}, /* 2094.545455 => 230450 => .0217 % over */
171 { 134, 1713}, /* 1713.011152 => 230398.5 => .00065% under */
189 /* Number of outstanding Command Write Urbs */
190 static atomic_t CmdUrbs = ATOMIC_INIT(0);
193 /* local function prototypes */
195 /* function prototypes for all URB callbacks */
196 static void edge_interrupt_callback(struct urb *urb);
197 static void edge_bulk_in_callback(struct urb *urb);
198 static void edge_bulk_out_data_callback(struct urb *urb);
199 static void edge_bulk_out_cmd_callback(struct urb *urb);
201 /* function prototypes for the usbserial callbacks */
202 static int edge_open(struct tty_struct *tty, struct usb_serial_port *port);
203 static void edge_close(struct usb_serial_port *port);
204 static int edge_write(struct tty_struct *tty, struct usb_serial_port *port,
205 const unsigned char *buf, int count);
206 static int edge_write_room(struct tty_struct *tty);
207 static int edge_chars_in_buffer(struct tty_struct *tty);
208 static void edge_throttle(struct tty_struct *tty);
209 static void edge_unthrottle(struct tty_struct *tty);
210 static void edge_set_termios(struct tty_struct *tty,
211 struct usb_serial_port *port,
212 struct ktermios *old_termios);
213 static int edge_ioctl(struct tty_struct *tty,
214 unsigned int cmd, unsigned long arg);
215 static void edge_break(struct tty_struct *tty, int break_state);
216 static int edge_tiocmget(struct tty_struct *tty);
217 static int edge_tiocmset(struct tty_struct *tty,
218 unsigned int set, unsigned int clear);
219 static int edge_get_icount(struct tty_struct *tty,
220 struct serial_icounter_struct *icount);
221 static int edge_startup(struct usb_serial *serial);
222 static void edge_disconnect(struct usb_serial *serial);
223 static void edge_release(struct usb_serial *serial);
224 static int edge_port_probe(struct usb_serial_port *port);
225 static int edge_port_remove(struct usb_serial_port *port);
227 #include "io_tables.h" /* all of the devices that this driver supports */
229 /* function prototypes for all of our local functions */
231 static void process_rcvd_data(struct edgeport_serial *edge_serial,
232 unsigned char *buffer, __u16 bufferLength);
233 static void process_rcvd_status(struct edgeport_serial *edge_serial,
234 __u8 byte2, __u8 byte3);
235 static void edge_tty_recv(struct usb_serial_port *port, unsigned char *data,
237 static void handle_new_msr(struct edgeport_port *edge_port, __u8 newMsr);
238 static void handle_new_lsr(struct edgeport_port *edge_port, __u8 lsrData,
239 __u8 lsr, __u8 data);
240 static int send_iosp_ext_cmd(struct edgeport_port *edge_port, __u8 command,
242 static int calc_baud_rate_divisor(struct device *dev, int baud_rate, int *divisor);
243 static int send_cmd_write_baud_rate(struct edgeport_port *edge_port,
245 static void change_port_settings(struct tty_struct *tty,
246 struct edgeport_port *edge_port,
247 struct ktermios *old_termios);
248 static int send_cmd_write_uart_register(struct edgeport_port *edge_port,
249 __u8 regNum, __u8 regValue);
250 static int write_cmd_usb(struct edgeport_port *edge_port,
251 unsigned char *buffer, int writeLength);
252 static void send_more_port_data(struct edgeport_serial *edge_serial,
253 struct edgeport_port *edge_port);
255 static int sram_write(struct usb_serial *serial, __u16 extAddr, __u16 addr,
256 __u16 length, const __u8 *data);
257 static int rom_read(struct usb_serial *serial, __u16 extAddr, __u16 addr,
258 __u16 length, __u8 *data);
259 static int rom_write(struct usb_serial *serial, __u16 extAddr, __u16 addr,
260 __u16 length, const __u8 *data);
261 static void get_manufacturing_desc(struct edgeport_serial *edge_serial);
262 static void get_boot_desc(struct edgeport_serial *edge_serial);
263 static void load_application_firmware(struct edgeport_serial *edge_serial);
265 static void unicode_to_ascii(char *string, int buflen,
266 __le16 *unicode, int unicode_size);
269 /* ************************************************************************ */
270 /* ************************************************************************ */
271 /* ************************************************************************ */
272 /* ************************************************************************ */
274 /************************************************************************
276 * update_edgeport_E2PROM() Compare current versions of *
277 * Boot ROM and Manufacture *
278 * Descriptors with versions *
279 * embedded in this driver *
281 ************************************************************************/
282 static void update_edgeport_E2PROM(struct edgeport_serial *edge_serial)
284 struct device *dev = &edge_serial->serial->dev->dev;
287 __u8 BootMajorVersion;
288 __u8 BootMinorVersion;
289 __u16 BootBuildNumber;
291 const struct ihex_binrec *rec;
292 const struct firmware *fw;
296 switch (edge_serial->product_info.iDownloadFile) {
297 case EDGE_DOWNLOAD_FILE_I930:
298 fw_name = "edgeport/boot.fw";
300 case EDGE_DOWNLOAD_FILE_80251:
301 fw_name = "edgeport/boot2.fw";
307 response = request_ihex_firmware(&fw, fw_name,
308 &edge_serial->serial->dev->dev);
310 dev_err(dev, "Failed to load image \"%s\" err %d\n",
315 rec = (const struct ihex_binrec *)fw->data;
316 BootMajorVersion = rec->data[0];
317 BootMinorVersion = rec->data[1];
318 BootBuildNumber = (rec->data[2] << 8) | rec->data[3];
320 /* Check Boot Image Version */
321 BootCurVer = (edge_serial->boot_descriptor.MajorVersion << 24) +
322 (edge_serial->boot_descriptor.MinorVersion << 16) +
323 le16_to_cpu(edge_serial->boot_descriptor.BuildNumber);
325 BootNewVer = (BootMajorVersion << 24) +
326 (BootMinorVersion << 16) +
329 dev_dbg(dev, "Current Boot Image version %d.%d.%d\n",
330 edge_serial->boot_descriptor.MajorVersion,
331 edge_serial->boot_descriptor.MinorVersion,
332 le16_to_cpu(edge_serial->boot_descriptor.BuildNumber));
335 if (BootNewVer > BootCurVer) {
336 dev_dbg(dev, "**Update Boot Image from %d.%d.%d to %d.%d.%d\n",
337 edge_serial->boot_descriptor.MajorVersion,
338 edge_serial->boot_descriptor.MinorVersion,
339 le16_to_cpu(edge_serial->boot_descriptor.BuildNumber),
340 BootMajorVersion, BootMinorVersion, BootBuildNumber);
342 dev_dbg(dev, "Downloading new Boot Image\n");
344 for (rec = ihex_next_binrec(rec); rec;
345 rec = ihex_next_binrec(rec)) {
346 Bootaddr = be32_to_cpu(rec->addr);
347 response = rom_write(edge_serial->serial,
350 be16_to_cpu(rec->len),
353 dev_err(&edge_serial->serial->dev->dev,
354 "rom_write failed (%x, %x, %d)\n",
355 Bootaddr >> 16, Bootaddr & 0xFFFF,
356 be16_to_cpu(rec->len));
361 dev_dbg(dev, "Boot Image -- already up to date\n");
363 release_firmware(fw);
367 /************************************************************************
369 * Get string descriptor from device
371 ************************************************************************/
372 static int get_string_desc(struct usb_device *dev, int Id,
373 struct usb_string_descriptor **pRetDesc)
375 struct usb_string_descriptor StringDesc;
376 struct usb_string_descriptor *pStringDesc;
378 dev_dbg(&dev->dev, "%s - USB String ID = %d\n", __func__, Id);
380 if (!usb_get_descriptor(dev, USB_DT_STRING, Id, &StringDesc,
384 pStringDesc = kmalloc(StringDesc.bLength, GFP_KERNEL);
388 if (!usb_get_descriptor(dev, USB_DT_STRING, Id, pStringDesc,
389 StringDesc.bLength)) {
394 *pRetDesc = pStringDesc;
399 static void dump_product_info(struct edgeport_serial *edge_serial,
400 struct edgeport_product_info *product_info)
402 struct device *dev = &edge_serial->serial->dev->dev;
404 /* Dump Product Info structure */
405 dev_dbg(dev, "**Product Information:\n");
406 dev_dbg(dev, " ProductId %x\n", product_info->ProductId);
407 dev_dbg(dev, " NumPorts %d\n", product_info->NumPorts);
408 dev_dbg(dev, " ProdInfoVer %d\n", product_info->ProdInfoVer);
409 dev_dbg(dev, " IsServer %d\n", product_info->IsServer);
410 dev_dbg(dev, " IsRS232 %d\n", product_info->IsRS232);
411 dev_dbg(dev, " IsRS422 %d\n", product_info->IsRS422);
412 dev_dbg(dev, " IsRS485 %d\n", product_info->IsRS485);
413 dev_dbg(dev, " RomSize %d\n", product_info->RomSize);
414 dev_dbg(dev, " RamSize %d\n", product_info->RamSize);
415 dev_dbg(dev, " CpuRev %x\n", product_info->CpuRev);
416 dev_dbg(dev, " BoardRev %x\n", product_info->BoardRev);
417 dev_dbg(dev, " BootMajorVersion %d.%d.%d\n",
418 product_info->BootMajorVersion,
419 product_info->BootMinorVersion,
420 le16_to_cpu(product_info->BootBuildNumber));
421 dev_dbg(dev, " FirmwareMajorVersion %d.%d.%d\n",
422 product_info->FirmwareMajorVersion,
423 product_info->FirmwareMinorVersion,
424 le16_to_cpu(product_info->FirmwareBuildNumber));
425 dev_dbg(dev, " ManufactureDescDate %d/%d/%d\n",
426 product_info->ManufactureDescDate[0],
427 product_info->ManufactureDescDate[1],
428 product_info->ManufactureDescDate[2]+1900);
429 dev_dbg(dev, " iDownloadFile 0x%x\n",
430 product_info->iDownloadFile);
431 dev_dbg(dev, " EpicVer %d\n", product_info->EpicVer);
434 static void get_product_info(struct edgeport_serial *edge_serial)
436 struct edgeport_product_info *product_info = &edge_serial->product_info;
438 memset(product_info, 0, sizeof(struct edgeport_product_info));
440 product_info->ProductId = (__u16)(le16_to_cpu(edge_serial->serial->dev->descriptor.idProduct) & ~ION_DEVICE_ID_80251_NETCHIP);
441 product_info->NumPorts = edge_serial->manuf_descriptor.NumPorts;
442 product_info->ProdInfoVer = 0;
444 product_info->RomSize = edge_serial->manuf_descriptor.RomSize;
445 product_info->RamSize = edge_serial->manuf_descriptor.RamSize;
446 product_info->CpuRev = edge_serial->manuf_descriptor.CpuRev;
447 product_info->BoardRev = edge_serial->manuf_descriptor.BoardRev;
449 product_info->BootMajorVersion =
450 edge_serial->boot_descriptor.MajorVersion;
451 product_info->BootMinorVersion =
452 edge_serial->boot_descriptor.MinorVersion;
453 product_info->BootBuildNumber =
454 edge_serial->boot_descriptor.BuildNumber;
456 memcpy(product_info->ManufactureDescDate,
457 edge_serial->manuf_descriptor.DescDate,
458 sizeof(edge_serial->manuf_descriptor.DescDate));
460 /* check if this is 2nd generation hardware */
461 if (le16_to_cpu(edge_serial->serial->dev->descriptor.idProduct)
462 & ION_DEVICE_ID_80251_NETCHIP)
463 product_info->iDownloadFile = EDGE_DOWNLOAD_FILE_80251;
465 product_info->iDownloadFile = EDGE_DOWNLOAD_FILE_I930;
467 /* Determine Product type and set appropriate flags */
468 switch (DEVICE_ID_FROM_USB_PRODUCT_ID(product_info->ProductId)) {
469 case ION_DEVICE_ID_EDGEPORT_COMPATIBLE:
470 case ION_DEVICE_ID_EDGEPORT_4T:
471 case ION_DEVICE_ID_EDGEPORT_4:
472 case ION_DEVICE_ID_EDGEPORT_2:
473 case ION_DEVICE_ID_EDGEPORT_8_DUAL_CPU:
474 case ION_DEVICE_ID_EDGEPORT_8:
475 case ION_DEVICE_ID_EDGEPORT_421:
476 case ION_DEVICE_ID_EDGEPORT_21:
477 case ION_DEVICE_ID_EDGEPORT_2_DIN:
478 case ION_DEVICE_ID_EDGEPORT_4_DIN:
479 case ION_DEVICE_ID_EDGEPORT_16_DUAL_CPU:
480 product_info->IsRS232 = 1;
483 case ION_DEVICE_ID_EDGEPORT_2I: /* Edgeport/2 RS422/RS485 */
484 product_info->IsRS422 = 1;
485 product_info->IsRS485 = 1;
488 case ION_DEVICE_ID_EDGEPORT_8I: /* Edgeport/4 RS422 */
489 case ION_DEVICE_ID_EDGEPORT_4I: /* Edgeport/4 RS422 */
490 product_info->IsRS422 = 1;
494 dump_product_info(edge_serial, product_info);
497 static int get_epic_descriptor(struct edgeport_serial *ep)
500 struct usb_serial *serial = ep->serial;
501 struct edgeport_product_info *product_info = &ep->product_info;
502 struct edge_compatibility_descriptor *epic = &ep->epic_descriptor;
503 struct edge_compatibility_bits *bits;
504 struct device *dev = &serial->dev->dev;
507 result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
508 USB_REQUEST_ION_GET_EPIC_DESC,
510 &ep->epic_descriptor,
511 sizeof(struct edge_compatibility_descriptor),
516 memset(product_info, 0, sizeof(struct edgeport_product_info));
518 product_info->NumPorts = epic->NumPorts;
519 product_info->ProdInfoVer = 0;
520 product_info->FirmwareMajorVersion = epic->MajorVersion;
521 product_info->FirmwareMinorVersion = epic->MinorVersion;
522 product_info->FirmwareBuildNumber = epic->BuildNumber;
523 product_info->iDownloadFile = epic->iDownloadFile;
524 product_info->EpicVer = epic->EpicVer;
525 product_info->Epic = epic->Supports;
526 product_info->ProductId = ION_DEVICE_ID_EDGEPORT_COMPATIBLE;
527 dump_product_info(ep, product_info);
529 bits = &ep->epic_descriptor.Supports;
530 dev_dbg(dev, "**EPIC descriptor:\n");
531 dev_dbg(dev, " VendEnableSuspend: %s\n", bits->VendEnableSuspend ? "TRUE": "FALSE");
532 dev_dbg(dev, " IOSPOpen : %s\n", bits->IOSPOpen ? "TRUE": "FALSE");
533 dev_dbg(dev, " IOSPClose : %s\n", bits->IOSPClose ? "TRUE": "FALSE");
534 dev_dbg(dev, " IOSPChase : %s\n", bits->IOSPChase ? "TRUE": "FALSE");
535 dev_dbg(dev, " IOSPSetRxFlow : %s\n", bits->IOSPSetRxFlow ? "TRUE": "FALSE");
536 dev_dbg(dev, " IOSPSetTxFlow : %s\n", bits->IOSPSetTxFlow ? "TRUE": "FALSE");
537 dev_dbg(dev, " IOSPSetXChar : %s\n", bits->IOSPSetXChar ? "TRUE": "FALSE");
538 dev_dbg(dev, " IOSPRxCheck : %s\n", bits->IOSPRxCheck ? "TRUE": "FALSE");
539 dev_dbg(dev, " IOSPSetClrBreak : %s\n", bits->IOSPSetClrBreak ? "TRUE": "FALSE");
540 dev_dbg(dev, " IOSPWriteMCR : %s\n", bits->IOSPWriteMCR ? "TRUE": "FALSE");
541 dev_dbg(dev, " IOSPWriteLCR : %s\n", bits->IOSPWriteLCR ? "TRUE": "FALSE");
542 dev_dbg(dev, " IOSPSetBaudRate : %s\n", bits->IOSPSetBaudRate ? "TRUE": "FALSE");
543 dev_dbg(dev, " TrueEdgeport : %s\n", bits->TrueEdgeport ? "TRUE": "FALSE");
550 /************************************************************************/
551 /************************************************************************/
552 /* U S B C A L L B A C K F U N C T I O N S */
553 /* U S B C A L L B A C K F U N C T I O N S */
554 /************************************************************************/
555 /************************************************************************/
557 /*****************************************************************************
558 * edge_interrupt_callback
559 * this is the callback function for when we have received data on the
560 * interrupt endpoint.
561 *****************************************************************************/
562 static void edge_interrupt_callback(struct urb *urb)
564 struct edgeport_serial *edge_serial = urb->context;
566 struct edgeport_port *edge_port;
567 struct usb_serial_port *port;
568 unsigned char *data = urb->transfer_buffer;
569 int length = urb->actual_length;
575 int status = urb->status;
584 /* this urb is terminated, clean up */
585 dev_dbg(&urb->dev->dev, "%s - urb shutting down with status: %d\n", __func__, status);
588 dev_dbg(&urb->dev->dev, "%s - nonzero urb status received: %d\n", __func__, status);
592 dev = &edge_serial->serial->dev->dev;
594 /* process this interrupt-read even if there are no ports open */
596 usb_serial_debug_data(dev, __func__, length, data);
599 bytes_avail = data[0] | (data[1] << 8);
601 spin_lock(&edge_serial->es_lock);
602 edge_serial->rxBytesAvail += bytes_avail;
604 "%s - bytes_avail=%d, rxBytesAvail=%d, read_in_progress=%d\n",
605 __func__, bytes_avail,
606 edge_serial->rxBytesAvail,
607 edge_serial->read_in_progress);
609 if (edge_serial->rxBytesAvail > 0 &&
610 !edge_serial->read_in_progress) {
611 dev_dbg(dev, "%s - posting a read\n", __func__);
612 edge_serial->read_in_progress = true;
614 /* we have pending bytes on the
615 bulk in pipe, send a request */
616 result = usb_submit_urb(edge_serial->read_urb, GFP_ATOMIC);
619 "%s - usb_submit_urb(read bulk) failed with result = %d\n",
621 edge_serial->read_in_progress = false;
624 spin_unlock(&edge_serial->es_lock);
627 /* grab the txcredits for the ports if available */
630 while ((position < length) &&
631 (portNumber < edge_serial->serial->num_ports)) {
632 txCredits = data[position] | (data[position+1] << 8);
634 port = edge_serial->serial->port[portNumber];
635 edge_port = usb_get_serial_port_data(port);
636 if (edge_port->open) {
637 spin_lock(&edge_port->ep_lock);
638 edge_port->txCredits += txCredits;
639 spin_unlock(&edge_port->ep_lock);
640 dev_dbg(dev, "%s - txcredits for port%d = %d\n",
641 __func__, portNumber,
642 edge_port->txCredits);
644 /* tell the tty driver that something
646 tty_port_tty_wakeup(&edge_port->port->port);
647 /* Since we have more credit, check
648 if more data can be sent */
649 send_more_port_data(edge_serial,
659 result = usb_submit_urb(urb, GFP_ATOMIC);
661 dev_err(&urb->dev->dev,
662 "%s - Error %d submitting control urb\n",
667 /*****************************************************************************
668 * edge_bulk_in_callback
669 * this is the callback function for when we have received data on the
671 *****************************************************************************/
672 static void edge_bulk_in_callback(struct urb *urb)
674 struct edgeport_serial *edge_serial = urb->context;
676 unsigned char *data = urb->transfer_buffer;
678 __u16 raw_data_length;
679 int status = urb->status;
682 dev_dbg(&urb->dev->dev, "%s - nonzero read bulk status received: %d\n",
684 edge_serial->read_in_progress = false;
688 if (urb->actual_length == 0) {
689 dev_dbg(&urb->dev->dev, "%s - read bulk callback with no data\n", __func__);
690 edge_serial->read_in_progress = false;
694 dev = &edge_serial->serial->dev->dev;
695 raw_data_length = urb->actual_length;
697 usb_serial_debug_data(dev, __func__, raw_data_length, data);
699 spin_lock(&edge_serial->es_lock);
701 /* decrement our rxBytes available by the number that we just got */
702 edge_serial->rxBytesAvail -= raw_data_length;
704 dev_dbg(dev, "%s - Received = %d, rxBytesAvail %d\n", __func__,
705 raw_data_length, edge_serial->rxBytesAvail);
707 process_rcvd_data(edge_serial, data, urb->actual_length);
709 /* check to see if there's any more data for us to read */
710 if (edge_serial->rxBytesAvail > 0) {
711 dev_dbg(dev, "%s - posting a read\n", __func__);
712 retval = usb_submit_urb(edge_serial->read_urb, GFP_ATOMIC);
715 "%s - usb_submit_urb(read bulk) failed, retval = %d\n",
717 edge_serial->read_in_progress = false;
720 edge_serial->read_in_progress = false;
723 spin_unlock(&edge_serial->es_lock);
727 /*****************************************************************************
728 * edge_bulk_out_data_callback
729 * this is the callback function for when we have finished sending
730 * serial data on the bulk out endpoint.
731 *****************************************************************************/
732 static void edge_bulk_out_data_callback(struct urb *urb)
734 struct edgeport_port *edge_port = urb->context;
735 int status = urb->status;
738 dev_dbg(&urb->dev->dev,
739 "%s - nonzero write bulk status received: %d\n",
744 tty_port_tty_wakeup(&edge_port->port->port);
746 /* Release the Write URB */
747 edge_port->write_in_progress = false;
749 /* Check if more data needs to be sent */
750 send_more_port_data((struct edgeport_serial *)
751 (usb_get_serial_data(edge_port->port->serial)), edge_port);
755 /*****************************************************************************
757 * this is the callback function for when we have finished sending a
758 * command on the bulk out endpoint.
759 *****************************************************************************/
760 static void edge_bulk_out_cmd_callback(struct urb *urb)
762 struct edgeport_port *edge_port = urb->context;
763 int status = urb->status;
765 atomic_dec(&CmdUrbs);
766 dev_dbg(&urb->dev->dev, "%s - FREE URB %p (outstanding %d)\n",
767 __func__, urb, atomic_read(&CmdUrbs));
770 /* clean up the transfer buffer */
771 kfree(urb->transfer_buffer);
773 /* Free the command urb */
777 dev_dbg(&urb->dev->dev,
778 "%s - nonzero write bulk status received: %d\n",
783 /* tell the tty driver that something has changed */
785 tty_port_tty_wakeup(&edge_port->port->port);
787 /* we have completed the command */
788 edge_port->commandPending = false;
789 wake_up(&edge_port->wait_command);
793 /*****************************************************************************
794 * Driver tty interface functions
795 *****************************************************************************/
797 /*****************************************************************************
799 * this function is called by the tty driver when a port is opened
800 * If successful, we return 0
801 * Otherwise we return a negative error number.
802 *****************************************************************************/
803 static int edge_open(struct tty_struct *tty, struct usb_serial_port *port)
805 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
806 struct device *dev = &port->dev;
807 struct usb_serial *serial;
808 struct edgeport_serial *edge_serial;
811 if (edge_port == NULL)
814 /* see if we've set up our endpoint info yet (can't set it up
815 in edge_startup as the structures were not set up at that time.) */
816 serial = port->serial;
817 edge_serial = usb_get_serial_data(serial);
818 if (edge_serial == NULL)
820 if (edge_serial->interrupt_in_buffer == NULL) {
821 struct usb_serial_port *port0 = serial->port[0];
823 /* not set up yet, so do it now */
824 edge_serial->interrupt_in_buffer =
825 port0->interrupt_in_buffer;
826 edge_serial->interrupt_in_endpoint =
827 port0->interrupt_in_endpointAddress;
828 edge_serial->interrupt_read_urb = port0->interrupt_in_urb;
829 edge_serial->bulk_in_buffer = port0->bulk_in_buffer;
830 edge_serial->bulk_in_endpoint =
831 port0->bulk_in_endpointAddress;
832 edge_serial->read_urb = port0->read_urb;
833 edge_serial->bulk_out_endpoint =
834 port0->bulk_out_endpointAddress;
836 /* set up our interrupt urb */
837 usb_fill_int_urb(edge_serial->interrupt_read_urb,
839 usb_rcvintpipe(serial->dev,
840 port0->interrupt_in_endpointAddress),
841 port0->interrupt_in_buffer,
842 edge_serial->interrupt_read_urb->transfer_buffer_length,
843 edge_interrupt_callback, edge_serial,
844 edge_serial->interrupt_read_urb->interval);
846 /* set up our bulk in urb */
847 usb_fill_bulk_urb(edge_serial->read_urb, serial->dev,
848 usb_rcvbulkpipe(serial->dev,
849 port0->bulk_in_endpointAddress),
850 port0->bulk_in_buffer,
851 edge_serial->read_urb->transfer_buffer_length,
852 edge_bulk_in_callback, edge_serial);
853 edge_serial->read_in_progress = false;
855 /* start interrupt read for this edgeport
856 * this interrupt will continue as long
857 * as the edgeport is connected */
858 response = usb_submit_urb(edge_serial->interrupt_read_urb,
861 dev_err(dev, "%s - Error %d submitting control urb\n",
866 /* initialize our wait queues */
867 init_waitqueue_head(&edge_port->wait_open);
868 init_waitqueue_head(&edge_port->wait_chase);
869 init_waitqueue_head(&edge_port->delta_msr_wait);
870 init_waitqueue_head(&edge_port->wait_command);
872 /* initialize our icount structure */
873 memset(&(edge_port->icount), 0x00, sizeof(edge_port->icount));
875 /* initialize our port settings */
876 edge_port->txCredits = 0; /* Can't send any data yet */
877 /* Must always set this bit to enable ints! */
878 edge_port->shadowMCR = MCR_MASTER_IE;
879 edge_port->chaseResponsePending = false;
881 /* send a open port command */
882 edge_port->openPending = true;
883 edge_port->open = false;
884 response = send_iosp_ext_cmd(edge_port, IOSP_CMD_OPEN_PORT, 0);
887 dev_err(dev, "%s - error sending open port command\n", __func__);
888 edge_port->openPending = false;
892 /* now wait for the port to be completely opened */
893 wait_event_timeout(edge_port->wait_open, !edge_port->openPending,
896 if (!edge_port->open) {
898 dev_dbg(dev, "%s - open timedout\n", __func__);
899 edge_port->openPending = false;
903 /* create the txfifo */
904 edge_port->txfifo.head = 0;
905 edge_port->txfifo.tail = 0;
906 edge_port->txfifo.count = 0;
907 edge_port->txfifo.size = edge_port->maxTxCredits;
908 edge_port->txfifo.fifo = kmalloc(edge_port->maxTxCredits, GFP_KERNEL);
910 if (!edge_port->txfifo.fifo) {
911 dev_dbg(dev, "%s - no memory\n", __func__);
916 /* Allocate a URB for the write */
917 edge_port->write_urb = usb_alloc_urb(0, GFP_KERNEL);
918 edge_port->write_in_progress = false;
920 if (!edge_port->write_urb) {
921 dev_dbg(dev, "%s - no memory\n", __func__);
926 dev_dbg(dev, "%s(%d) - Initialize TX fifo to %d bytes\n",
927 __func__, port->number, edge_port->maxTxCredits);
933 /************************************************************************
935 * block_until_chase_response
937 * This function will block the close until one of the following:
938 * 1. Response to our Chase comes from Edgeport
939 * 2. A timeout of 10 seconds without activity has expired
940 * (1K of Edgeport data @ 2400 baud ==> 4 sec to empty)
942 ************************************************************************/
943 static void block_until_chase_response(struct edgeport_port *edge_port)
945 struct device *dev = &edge_port->port->dev;
952 /* Save Last credits */
953 lastCredits = edge_port->txCredits;
955 /* Did we get our Chase response */
956 if (!edge_port->chaseResponsePending) {
957 dev_dbg(dev, "%s - Got Chase Response\n", __func__);
959 /* did we get all of our credit back? */
960 if (edge_port->txCredits == edge_port->maxTxCredits) {
961 dev_dbg(dev, "%s - Got all credits\n", __func__);
966 /* Block the thread for a while */
967 prepare_to_wait(&edge_port->wait_chase, &wait,
968 TASK_UNINTERRUPTIBLE);
969 schedule_timeout(timeout);
970 finish_wait(&edge_port->wait_chase, &wait);
972 if (lastCredits == edge_port->txCredits) {
973 /* No activity.. count down. */
976 edge_port->chaseResponsePending = false;
977 dev_dbg(dev, "%s - Chase TIMEOUT\n", __func__);
981 /* Reset timeout value back to 10 seconds */
982 dev_dbg(dev, "%s - Last %d, Current %d\n", __func__,
983 lastCredits, edge_port->txCredits);
990 /************************************************************************
992 * block_until_tx_empty
994 * This function will block the close until one of the following:
996 * 2. The edgeport has stopped
997 * 3. A timeout of 3 seconds without activity has expired
999 ************************************************************************/
1000 static void block_until_tx_empty(struct edgeport_port *edge_port)
1002 struct device *dev = &edge_port->port->dev;
1004 struct TxFifo *fifo = &edge_port->txfifo;
1006 int timeout = HZ/10;
1010 /* Save Last count */
1011 lastCount = fifo->count;
1013 /* Is the Edgeport Buffer empty? */
1014 if (lastCount == 0) {
1015 dev_dbg(dev, "%s - TX Buffer Empty\n", __func__);
1019 /* Block the thread for a while */
1020 prepare_to_wait(&edge_port->wait_chase, &wait,
1021 TASK_UNINTERRUPTIBLE);
1022 schedule_timeout(timeout);
1023 finish_wait(&edge_port->wait_chase, &wait);
1025 dev_dbg(dev, "%s wait\n", __func__);
1027 if (lastCount == fifo->count) {
1028 /* No activity.. count down. */
1031 dev_dbg(dev, "%s - TIMEOUT\n", __func__);
1035 /* Reset timeout value back to seconds */
1042 /*****************************************************************************
1044 * this function is called by the tty driver when a port is closed
1045 *****************************************************************************/
1046 static void edge_close(struct usb_serial_port *port)
1048 struct edgeport_serial *edge_serial;
1049 struct edgeport_port *edge_port;
1052 edge_serial = usb_get_serial_data(port->serial);
1053 edge_port = usb_get_serial_port_data(port);
1054 if (edge_serial == NULL || edge_port == NULL)
1057 /* block until tx is empty */
1058 block_until_tx_empty(edge_port);
1060 edge_port->closePending = true;
1062 if ((!edge_serial->is_epic) ||
1063 ((edge_serial->is_epic) &&
1064 (edge_serial->epic_descriptor.Supports.IOSPChase))) {
1065 /* flush and chase */
1066 edge_port->chaseResponsePending = true;
1068 dev_dbg(&port->dev, "%s - Sending IOSP_CMD_CHASE_PORT\n", __func__);
1069 status = send_iosp_ext_cmd(edge_port, IOSP_CMD_CHASE_PORT, 0);
1071 /* block until chase finished */
1072 block_until_chase_response(edge_port);
1074 edge_port->chaseResponsePending = false;
1077 if ((!edge_serial->is_epic) ||
1078 ((edge_serial->is_epic) &&
1079 (edge_serial->epic_descriptor.Supports.IOSPClose))) {
1080 /* close the port */
1081 dev_dbg(&port->dev, "%s - Sending IOSP_CMD_CLOSE_PORT\n", __func__);
1082 send_iosp_ext_cmd(edge_port, IOSP_CMD_CLOSE_PORT, 0);
1085 /* port->close = true; */
1086 edge_port->closePending = false;
1087 edge_port->open = false;
1088 edge_port->openPending = false;
1090 usb_kill_urb(edge_port->write_urb);
1092 if (edge_port->write_urb) {
1093 /* if this urb had a transfer buffer already
1094 (old transfer) free it */
1095 kfree(edge_port->write_urb->transfer_buffer);
1096 usb_free_urb(edge_port->write_urb);
1097 edge_port->write_urb = NULL;
1099 kfree(edge_port->txfifo.fifo);
1100 edge_port->txfifo.fifo = NULL;
1103 /*****************************************************************************
1105 * this function is called by the tty driver when data should be written
1107 * If successful, we return the number of bytes written, otherwise we
1108 * return a negative error number.
1109 *****************************************************************************/
1110 static int edge_write(struct tty_struct *tty, struct usb_serial_port *port,
1111 const unsigned char *data, int count)
1113 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1114 struct TxFifo *fifo;
1119 unsigned long flags;
1121 if (edge_port == NULL)
1124 /* get a pointer to the Tx fifo */
1125 fifo = &edge_port->txfifo;
1127 spin_lock_irqsave(&edge_port->ep_lock, flags);
1129 /* calculate number of bytes to put in fifo */
1130 copySize = min((unsigned int)count,
1131 (edge_port->txCredits - fifo->count));
1133 dev_dbg(&port->dev, "%s(%d) of %d byte(s) Fifo room %d -- will copy %d bytes\n",
1134 __func__, port->number, count,
1135 edge_port->txCredits - fifo->count, copySize);
1137 /* catch writes of 0 bytes which the tty driver likes to give us,
1138 and when txCredits is empty */
1139 if (copySize == 0) {
1140 dev_dbg(&port->dev, "%s - copySize = Zero\n", __func__);
1145 * since we can never overflow the buffer we do not have to check for a
1148 * the copy is done is two parts -- first fill to the end of the buffer
1149 * then copy the reset from the start of the buffer
1151 bytesleft = fifo->size - fifo->head;
1152 firsthalf = min(bytesleft, copySize);
1153 dev_dbg(&port->dev, "%s - copy %d bytes of %d into fifo \n", __func__,
1154 firsthalf, bytesleft);
1156 /* now copy our data */
1157 memcpy(&fifo->fifo[fifo->head], data, firsthalf);
1158 usb_serial_debug_data(&port->dev, __func__, firsthalf, &fifo->fifo[fifo->head]);
1160 /* update the index and size */
1161 fifo->head += firsthalf;
1162 fifo->count += firsthalf;
1164 /* wrap the index */
1165 if (fifo->head == fifo->size)
1168 secondhalf = copySize-firsthalf;
1171 dev_dbg(&port->dev, "%s - copy rest of data %d\n", __func__, secondhalf);
1172 memcpy(&fifo->fifo[fifo->head], &data[firsthalf], secondhalf);
1173 usb_serial_debug_data(&port->dev, __func__, secondhalf, &fifo->fifo[fifo->head]);
1174 /* update the index and size */
1175 fifo->count += secondhalf;
1176 fifo->head += secondhalf;
1177 /* No need to check for wrap since we can not get to end of
1178 * the fifo in this part
1183 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1185 send_more_port_data((struct edgeport_serial *)
1186 usb_get_serial_data(port->serial), edge_port);
1188 dev_dbg(&port->dev, "%s wrote %d byte(s) TxCredits %d, Fifo %d\n",
1189 __func__, copySize, edge_port->txCredits, fifo->count);
1195 /************************************************************************
1197 * send_more_port_data()
1199 * This routine attempts to write additional UART transmit data
1200 * to a port over the USB bulk pipe. It is called (1) when new
1201 * data has been written to a port's TxBuffer from higher layers
1202 * (2) when the peripheral sends us additional TxCredits indicating
1203 * that it can accept more Tx data for a given port; and (3) when
1204 * a bulk write completes successfully and we want to see if we
1205 * can transmit more.
1207 ************************************************************************/
1208 static void send_more_port_data(struct edgeport_serial *edge_serial,
1209 struct edgeport_port *edge_port)
1211 struct TxFifo *fifo = &edge_port->txfifo;
1212 struct device *dev = &edge_port->port->dev;
1214 unsigned char *buffer;
1220 unsigned long flags;
1222 spin_lock_irqsave(&edge_port->ep_lock, flags);
1224 if (edge_port->write_in_progress ||
1226 (fifo->count == 0)) {
1227 dev_dbg(dev, "%s(%d) EXIT - fifo %d, PendingWrite = %d\n",
1228 __func__, edge_port->port->number,
1229 fifo->count, edge_port->write_in_progress);
1233 /* since the amount of data in the fifo will always fit into the
1234 * edgeport buffer we do not need to check the write length
1236 * Do we have enough credits for this port to make it worthwhile
1237 * to bother queueing a write. If it's too small, say a few bytes,
1238 * it's better to wait for more credits so we can do a larger write.
1240 if (edge_port->txCredits < EDGE_FW_GET_TX_CREDITS_SEND_THRESHOLD(edge_port->maxTxCredits, EDGE_FW_BULK_MAX_PACKET_SIZE)) {
1241 dev_dbg(dev, "%s(%d) Not enough credit - fifo %d TxCredit %d\n",
1242 __func__, edge_port->port->number, fifo->count,
1243 edge_port->txCredits);
1247 /* lock this write */
1248 edge_port->write_in_progress = true;
1250 /* get a pointer to the write_urb */
1251 urb = edge_port->write_urb;
1253 /* make sure transfer buffer is freed */
1254 kfree(urb->transfer_buffer);
1255 urb->transfer_buffer = NULL;
1257 /* build the data header for the buffer and port that we are about
1259 count = fifo->count;
1260 buffer = kmalloc(count+2, GFP_ATOMIC);
1261 if (buffer == NULL) {
1262 dev_err_console(edge_port->port,
1263 "%s - no more kernel memory...\n", __func__);
1264 edge_port->write_in_progress = false;
1267 buffer[0] = IOSP_BUILD_DATA_HDR1(edge_port->port->number
1268 - edge_port->port->serial->minor, count);
1269 buffer[1] = IOSP_BUILD_DATA_HDR2(edge_port->port->number
1270 - edge_port->port->serial->minor, count);
1272 /* now copy our data */
1273 bytesleft = fifo->size - fifo->tail;
1274 firsthalf = min(bytesleft, count);
1275 memcpy(&buffer[2], &fifo->fifo[fifo->tail], firsthalf);
1276 fifo->tail += firsthalf;
1277 fifo->count -= firsthalf;
1278 if (fifo->tail == fifo->size)
1281 secondhalf = count-firsthalf;
1283 memcpy(&buffer[2+firsthalf], &fifo->fifo[fifo->tail],
1285 fifo->tail += secondhalf;
1286 fifo->count -= secondhalf;
1290 usb_serial_debug_data(&edge_port->port->dev, __func__, count, &buffer[2]);
1292 /* fill up the urb with all of our data and submit it */
1293 usb_fill_bulk_urb(urb, edge_serial->serial->dev,
1294 usb_sndbulkpipe(edge_serial->serial->dev,
1295 edge_serial->bulk_out_endpoint),
1297 edge_bulk_out_data_callback, edge_port);
1299 /* decrement the number of credits we have by the number we just sent */
1300 edge_port->txCredits -= count;
1301 edge_port->icount.tx += count;
1303 status = usb_submit_urb(urb, GFP_ATOMIC);
1305 /* something went wrong */
1306 dev_err_console(edge_port->port,
1307 "%s - usb_submit_urb(write bulk) failed, status = %d, data lost\n",
1309 edge_port->write_in_progress = false;
1311 /* revert the credits as something bad happened. */
1312 edge_port->txCredits += count;
1313 edge_port->icount.tx -= count;
1315 dev_dbg(dev, "%s wrote %d byte(s) TxCredit %d, Fifo %d\n",
1316 __func__, count, edge_port->txCredits, fifo->count);
1319 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1323 /*****************************************************************************
1325 * this function is called by the tty driver when it wants to know how
1326 * many bytes of data we can accept for a specific port. If successful,
1327 * we return the amount of room that we have for this port (the txCredits)
1328 * otherwise we return a negative error number.
1329 *****************************************************************************/
1330 static int edge_write_room(struct tty_struct *tty)
1332 struct usb_serial_port *port = tty->driver_data;
1333 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1335 unsigned long flags;
1337 if (edge_port == NULL)
1339 if (edge_port->closePending)
1342 if (!edge_port->open) {
1343 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
1347 /* total of both buffers is still txCredit */
1348 spin_lock_irqsave(&edge_port->ep_lock, flags);
1349 room = edge_port->txCredits - edge_port->txfifo.count;
1350 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1352 dev_dbg(&port->dev, "%s - returns %d\n", __func__, room);
1357 /*****************************************************************************
1358 * edge_chars_in_buffer
1359 * this function is called by the tty driver when it wants to know how
1360 * many bytes of data we currently have outstanding in the port (data that
1361 * has been written, but hasn't made it out the port yet)
1362 * If successful, we return the number of bytes left to be written in the
1364 * Otherwise we return a negative error number.
1365 *****************************************************************************/
1366 static int edge_chars_in_buffer(struct tty_struct *tty)
1368 struct usb_serial_port *port = tty->driver_data;
1369 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1371 unsigned long flags;
1373 if (edge_port == NULL)
1375 if (edge_port->closePending)
1378 if (!edge_port->open) {
1379 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
1383 spin_lock_irqsave(&edge_port->ep_lock, flags);
1384 num_chars = edge_port->maxTxCredits - edge_port->txCredits +
1385 edge_port->txfifo.count;
1386 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1388 dev_dbg(&port->dev, "%s(port %d) - returns %d\n", __func__,
1389 port->number, num_chars);
1396 /*****************************************************************************
1398 * this function is called by the tty driver when it wants to stop the data
1399 * being read from the port.
1400 *****************************************************************************/
1401 static void edge_throttle(struct tty_struct *tty)
1403 struct usb_serial_port *port = tty->driver_data;
1404 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1407 if (edge_port == NULL)
1410 if (!edge_port->open) {
1411 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
1415 /* if we are implementing XON/XOFF, send the stop character */
1417 unsigned char stop_char = STOP_CHAR(tty);
1418 status = edge_write(tty, port, &stop_char, 1);
1423 /* if we are implementing RTS/CTS, toggle that line */
1424 if (tty->termios.c_cflag & CRTSCTS) {
1425 edge_port->shadowMCR &= ~MCR_RTS;
1426 status = send_cmd_write_uart_register(edge_port, MCR,
1427 edge_port->shadowMCR);
1434 /*****************************************************************************
1436 * this function is called by the tty driver when it wants to resume the
1437 * data being read from the port (called after SerialThrottle is called)
1438 *****************************************************************************/
1439 static void edge_unthrottle(struct tty_struct *tty)
1441 struct usb_serial_port *port = tty->driver_data;
1442 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1445 if (edge_port == NULL)
1448 if (!edge_port->open) {
1449 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
1453 /* if we are implementing XON/XOFF, send the start character */
1455 unsigned char start_char = START_CHAR(tty);
1456 status = edge_write(tty, port, &start_char, 1);
1460 /* if we are implementing RTS/CTS, toggle that line */
1461 if (tty->termios.c_cflag & CRTSCTS) {
1462 edge_port->shadowMCR |= MCR_RTS;
1463 send_cmd_write_uart_register(edge_port, MCR,
1464 edge_port->shadowMCR);
1469 /*****************************************************************************
1471 * this function is called by the tty driver when it wants to change
1472 * the termios structure
1473 *****************************************************************************/
1474 static void edge_set_termios(struct tty_struct *tty,
1475 struct usb_serial_port *port, struct ktermios *old_termios)
1477 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1480 cflag = tty->termios.c_cflag;
1481 dev_dbg(&port->dev, "%s - clfag %08x iflag %08x\n", __func__, tty->termios.c_cflag, tty->termios.c_iflag);
1482 dev_dbg(&port->dev, "%s - old clfag %08x old iflag %08x\n", __func__, old_termios->c_cflag, old_termios->c_iflag);
1484 if (edge_port == NULL)
1487 if (!edge_port->open) {
1488 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
1492 /* change the port settings to the new ones specified */
1493 change_port_settings(tty, edge_port, old_termios);
1497 /*****************************************************************************
1498 * get_lsr_info - get line status register info
1500 * Purpose: Let user call ioctl() to get info when the UART physically
1501 * is emptied. On bus types like RS485, the transmitter must
1502 * release the bus after transmitting. This must be done when
1503 * the transmit shift register is empty, not be done when the
1504 * transmit holding register is empty. This functionality
1505 * allows an RS485 driver to be written in user space.
1506 *****************************************************************************/
1507 static int get_lsr_info(struct edgeport_port *edge_port,
1508 unsigned int __user *value)
1510 unsigned int result = 0;
1511 unsigned long flags;
1513 spin_lock_irqsave(&edge_port->ep_lock, flags);
1514 if (edge_port->maxTxCredits == edge_port->txCredits &&
1515 edge_port->txfifo.count == 0) {
1516 dev_dbg(&edge_port->port->dev, "%s -- Empty\n", __func__);
1517 result = TIOCSER_TEMT;
1519 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1521 if (copy_to_user(value, &result, sizeof(int)))
1526 static int edge_tiocmset(struct tty_struct *tty,
1527 unsigned int set, unsigned int clear)
1529 struct usb_serial_port *port = tty->driver_data;
1530 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1533 mcr = edge_port->shadowMCR;
1534 if (set & TIOCM_RTS)
1536 if (set & TIOCM_DTR)
1538 if (set & TIOCM_LOOP)
1539 mcr |= MCR_LOOPBACK;
1541 if (clear & TIOCM_RTS)
1543 if (clear & TIOCM_DTR)
1545 if (clear & TIOCM_LOOP)
1546 mcr &= ~MCR_LOOPBACK;
1548 edge_port->shadowMCR = mcr;
1550 send_cmd_write_uart_register(edge_port, MCR, edge_port->shadowMCR);
1555 static int edge_tiocmget(struct tty_struct *tty)
1557 struct usb_serial_port *port = tty->driver_data;
1558 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1559 unsigned int result = 0;
1563 msr = edge_port->shadowMSR;
1564 mcr = edge_port->shadowMCR;
1565 result = ((mcr & MCR_DTR) ? TIOCM_DTR: 0) /* 0x002 */
1566 | ((mcr & MCR_RTS) ? TIOCM_RTS: 0) /* 0x004 */
1567 | ((msr & EDGEPORT_MSR_CTS) ? TIOCM_CTS: 0) /* 0x020 */
1568 | ((msr & EDGEPORT_MSR_CD) ? TIOCM_CAR: 0) /* 0x040 */
1569 | ((msr & EDGEPORT_MSR_RI) ? TIOCM_RI: 0) /* 0x080 */
1570 | ((msr & EDGEPORT_MSR_DSR) ? TIOCM_DSR: 0); /* 0x100 */
1575 static int edge_get_icount(struct tty_struct *tty,
1576 struct serial_icounter_struct *icount)
1578 struct usb_serial_port *port = tty->driver_data;
1579 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1580 struct async_icount cnow;
1581 cnow = edge_port->icount;
1583 icount->cts = cnow.cts;
1584 icount->dsr = cnow.dsr;
1585 icount->rng = cnow.rng;
1586 icount->dcd = cnow.dcd;
1587 icount->rx = cnow.rx;
1588 icount->tx = cnow.tx;
1589 icount->frame = cnow.frame;
1590 icount->overrun = cnow.overrun;
1591 icount->parity = cnow.parity;
1592 icount->brk = cnow.brk;
1593 icount->buf_overrun = cnow.buf_overrun;
1595 dev_dbg(&port->dev, "%s (%d) TIOCGICOUNT RX=%d, TX=%d\n", __func__,
1596 port->number, icount->rx, icount->tx);
1600 static int get_serial_info(struct edgeport_port *edge_port,
1601 struct serial_struct __user *retinfo)
1603 struct serial_struct tmp;
1608 memset(&tmp, 0, sizeof(tmp));
1610 tmp.type = PORT_16550A;
1611 tmp.line = edge_port->port->serial->minor;
1612 tmp.port = edge_port->port->number;
1614 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
1615 tmp.xmit_fifo_size = edge_port->maxTxCredits;
1616 tmp.baud_base = 9600;
1617 tmp.close_delay = 5*HZ;
1618 tmp.closing_wait = 30*HZ;
1620 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1626 /*****************************************************************************
1628 * this function handles any ioctl calls to the driver
1629 *****************************************************************************/
1630 static int edge_ioctl(struct tty_struct *tty,
1631 unsigned int cmd, unsigned long arg)
1633 struct usb_serial_port *port = tty->driver_data;
1635 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1636 struct async_icount cnow;
1637 struct async_icount cprev;
1639 dev_dbg(&port->dev, "%s - port %d, cmd = 0x%x\n", __func__, port->number, cmd);
1643 dev_dbg(&port->dev, "%s (%d) TIOCSERGETLSR\n", __func__, port->number);
1644 return get_lsr_info(edge_port, (unsigned int __user *) arg);
1647 dev_dbg(&port->dev, "%s (%d) TIOCGSERIAL\n", __func__, port->number);
1648 return get_serial_info(edge_port, (struct serial_struct __user *) arg);
1651 dev_dbg(&port->dev, "%s (%d) TIOCMIWAIT\n", __func__, port->number);
1652 cprev = edge_port->icount;
1654 prepare_to_wait(&edge_port->delta_msr_wait,
1655 &wait, TASK_INTERRUPTIBLE);
1657 finish_wait(&edge_port->delta_msr_wait, &wait);
1658 /* see if a signal did it */
1659 if (signal_pending(current))
1660 return -ERESTARTSYS;
1661 cnow = edge_port->icount;
1662 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
1663 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
1664 return -EIO; /* no change => error */
1665 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1666 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1667 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1668 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
1677 return -ENOIOCTLCMD;
1681 /*****************************************************************************
1683 * this function sends a break to the port
1684 *****************************************************************************/
1685 static void edge_break(struct tty_struct *tty, int break_state)
1687 struct usb_serial_port *port = tty->driver_data;
1688 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1689 struct edgeport_serial *edge_serial = usb_get_serial_data(port->serial);
1692 if ((!edge_serial->is_epic) ||
1693 ((edge_serial->is_epic) &&
1694 (edge_serial->epic_descriptor.Supports.IOSPChase))) {
1695 /* flush and chase */
1696 edge_port->chaseResponsePending = true;
1698 dev_dbg(&port->dev, "%s - Sending IOSP_CMD_CHASE_PORT\n", __func__);
1699 status = send_iosp_ext_cmd(edge_port, IOSP_CMD_CHASE_PORT, 0);
1701 /* block until chase finished */
1702 block_until_chase_response(edge_port);
1704 edge_port->chaseResponsePending = false;
1708 if ((!edge_serial->is_epic) ||
1709 ((edge_serial->is_epic) &&
1710 (edge_serial->epic_descriptor.Supports.IOSPSetClrBreak))) {
1711 if (break_state == -1) {
1712 dev_dbg(&port->dev, "%s - Sending IOSP_CMD_SET_BREAK\n", __func__);
1713 status = send_iosp_ext_cmd(edge_port,
1714 IOSP_CMD_SET_BREAK, 0);
1716 dev_dbg(&port->dev, "%s - Sending IOSP_CMD_CLEAR_BREAK\n", __func__);
1717 status = send_iosp_ext_cmd(edge_port,
1718 IOSP_CMD_CLEAR_BREAK, 0);
1721 dev_dbg(&port->dev, "%s - error sending break set/clear command.\n",
1727 /*****************************************************************************
1729 * this function handles the data received on the bulk in pipe.
1730 *****************************************************************************/
1731 static void process_rcvd_data(struct edgeport_serial *edge_serial,
1732 unsigned char *buffer, __u16 bufferLength)
1734 struct device *dev = &edge_serial->serial->dev->dev;
1735 struct usb_serial_port *port;
1736 struct edgeport_port *edge_port;
1737 __u16 lastBufferLength;
1740 lastBufferLength = bufferLength + 1;
1742 while (bufferLength > 0) {
1743 /* failsafe incase we get a message that we don't understand */
1744 if (lastBufferLength == bufferLength) {
1745 dev_dbg(dev, "%s - stuck in loop, exiting it.\n", __func__);
1748 lastBufferLength = bufferLength;
1750 switch (edge_serial->rxState) {
1752 edge_serial->rxHeader1 = *buffer;
1756 if (bufferLength == 0) {
1757 edge_serial->rxState = EXPECT_HDR2;
1760 /* otherwise, drop on through */
1762 edge_serial->rxHeader2 = *buffer;
1766 dev_dbg(dev, "%s - Hdr1=%02X Hdr2=%02X\n", __func__,
1767 edge_serial->rxHeader1, edge_serial->rxHeader2);
1768 /* Process depending on whether this header is
1771 if (IS_CMD_STAT_HDR(edge_serial->rxHeader1)) {
1772 /* Decode this status header and go to
1773 * EXPECT_HDR1 (if we can process the status
1774 * with only 2 bytes), or go to EXPECT_HDR3 to
1775 * get the third byte. */
1776 edge_serial->rxPort =
1777 IOSP_GET_HDR_PORT(edge_serial->rxHeader1);
1778 edge_serial->rxStatusCode =
1779 IOSP_GET_STATUS_CODE(
1780 edge_serial->rxHeader1);
1782 if (!IOSP_STATUS_IS_2BYTE(
1783 edge_serial->rxStatusCode)) {
1784 /* This status needs additional bytes.
1785 * Save what we have and then wait for
1788 edge_serial->rxStatusParam
1789 = edge_serial->rxHeader2;
1790 edge_serial->rxState = EXPECT_HDR3;
1793 /* We have all the header bytes, process the
1795 process_rcvd_status(edge_serial,
1796 edge_serial->rxHeader2, 0);
1797 edge_serial->rxState = EXPECT_HDR1;
1800 edge_serial->rxPort =
1801 IOSP_GET_HDR_PORT(edge_serial->rxHeader1);
1802 edge_serial->rxBytesRemaining =
1803 IOSP_GET_HDR_DATA_LEN(
1804 edge_serial->rxHeader1,
1805 edge_serial->rxHeader2);
1806 dev_dbg(dev, "%s - Data for Port %u Len %u\n",
1808 edge_serial->rxPort,
1809 edge_serial->rxBytesRemaining);
1811 /* ASSERT(DevExt->RxPort < DevExt->NumPorts);
1812 * ASSERT(DevExt->RxBytesRemaining <
1813 * IOSP_MAX_DATA_LENGTH);
1816 if (bufferLength == 0) {
1817 edge_serial->rxState = EXPECT_DATA;
1820 /* Else, drop through */
1822 case EXPECT_DATA: /* Expect data */
1823 if (bufferLength < edge_serial->rxBytesRemaining) {
1824 rxLen = bufferLength;
1825 /* Expect data to start next buffer */
1826 edge_serial->rxState = EXPECT_DATA;
1828 /* BufLen >= RxBytesRemaining */
1829 rxLen = edge_serial->rxBytesRemaining;
1830 /* Start another header next time */
1831 edge_serial->rxState = EXPECT_HDR1;
1834 bufferLength -= rxLen;
1835 edge_serial->rxBytesRemaining -= rxLen;
1837 /* spit this data back into the tty driver if this
1840 port = edge_serial->serial->port[
1841 edge_serial->rxPort];
1842 edge_port = usb_get_serial_port_data(port);
1843 if (edge_port->open) {
1844 dev_dbg(dev, "%s - Sending %d bytes to TTY for port %d\n",
1846 edge_serial->rxPort);
1847 edge_tty_recv(edge_port->port, buffer,
1849 edge_port->icount.rx += rxLen;
1855 case EXPECT_HDR3: /* Expect 3rd byte of status header */
1856 edge_serial->rxHeader3 = *buffer;
1860 /* We have all the header bytes, process the
1862 process_rcvd_status(edge_serial,
1863 edge_serial->rxStatusParam,
1864 edge_serial->rxHeader3);
1865 edge_serial->rxState = EXPECT_HDR1;
1872 /*****************************************************************************
1873 * process_rcvd_status
1874 * this function handles the any status messages received on the
1876 *****************************************************************************/
1877 static void process_rcvd_status(struct edgeport_serial *edge_serial,
1878 __u8 byte2, __u8 byte3)
1880 struct usb_serial_port *port;
1881 struct edgeport_port *edge_port;
1882 struct tty_struct *tty;
1884 __u8 code = edge_serial->rxStatusCode;
1886 /* switch the port pointer to the one being currently talked about */
1887 port = edge_serial->serial->port[edge_serial->rxPort];
1888 edge_port = usb_get_serial_port_data(port);
1889 if (edge_port == NULL) {
1890 dev_err(&edge_serial->serial->dev->dev,
1891 "%s - edge_port == NULL for port %d\n",
1892 __func__, edge_serial->rxPort);
1897 if (code == IOSP_EXT_STATUS) {
1899 case IOSP_EXT_STATUS_CHASE_RSP:
1900 /* we want to do EXT status regardless of port
1902 dev_dbg(dev, "%s - Port %u EXT CHASE_RSP Data = %02x\n",
1903 __func__, edge_serial->rxPort, byte3);
1904 /* Currently, the only EXT_STATUS is Chase, so process
1905 * here instead of one more call to one more subroutine
1906 * If/when more EXT_STATUS, there'll be more work to do
1907 * Also, we currently clear flag and close the port
1908 * regardless of content of above's Byte3.
1909 * We could choose to do something else when Byte3 says
1910 * Timeout on Chase from Edgeport, like wait longer in
1911 * block_until_chase_response, but for now we don't.
1913 edge_port->chaseResponsePending = false;
1914 wake_up(&edge_port->wait_chase);
1917 case IOSP_EXT_STATUS_RX_CHECK_RSP:
1918 dev_dbg(dev, "%s ========== Port %u CHECK_RSP Sequence = %02x =============\n",
1919 __func__, edge_serial->rxPort, byte3);
1920 /* Port->RxCheckRsp = true; */
1925 if (code == IOSP_STATUS_OPEN_RSP) {
1926 edge_port->txCredits = GET_TX_BUFFER_SIZE(byte3);
1927 edge_port->maxTxCredits = edge_port->txCredits;
1928 dev_dbg(dev, "%s - Port %u Open Response Initial MSR = %02x TxBufferSize = %d\n",
1929 __func__, edge_serial->rxPort, byte2, edge_port->txCredits);
1930 handle_new_msr(edge_port, byte2);
1932 /* send the current line settings to the port so we are
1933 in sync with any further termios calls */
1934 tty = tty_port_tty_get(&edge_port->port->port);
1936 change_port_settings(tty,
1937 edge_port, &tty->termios);
1941 /* we have completed the open */
1942 edge_port->openPending = false;
1943 edge_port->open = true;
1944 wake_up(&edge_port->wait_open);
1948 /* If port is closed, silently discard all rcvd status. We can
1949 * have cases where buffered status is received AFTER the close
1950 * port command is sent to the Edgeport.
1952 if (!edge_port->open || edge_port->closePending)
1956 /* Not currently sent by Edgeport */
1957 case IOSP_STATUS_LSR:
1958 dev_dbg(dev, "%s - Port %u LSR Status = %02x\n",
1959 __func__, edge_serial->rxPort, byte2);
1960 handle_new_lsr(edge_port, false, byte2, 0);
1963 case IOSP_STATUS_LSR_DATA:
1964 dev_dbg(dev, "%s - Port %u LSR Status = %02x, Data = %02x\n",
1965 __func__, edge_serial->rxPort, byte2, byte3);
1966 /* byte2 is LSR Register */
1967 /* byte3 is broken data byte */
1968 handle_new_lsr(edge_port, true, byte2, byte3);
1971 * case IOSP_EXT_4_STATUS:
1972 * dev_dbg(dev, "%s - Port %u LSR Status = %02x Data = %02x\n",
1973 * __func__, edge_serial->rxPort, byte2, byte3);
1976 case IOSP_STATUS_MSR:
1977 dev_dbg(dev, "%s - Port %u MSR Status = %02x\n",
1978 __func__, edge_serial->rxPort, byte2);
1980 * Process this new modem status and generate appropriate
1981 * events, etc, based on the new status. This routine
1982 * also saves the MSR in Port->ShadowMsr.
1984 handle_new_msr(edge_port, byte2);
1988 dev_dbg(dev, "%s - Unrecognized IOSP status code %u\n", __func__, code);
1994 /*****************************************************************************
1996 * this function passes data on to the tty flip buffer
1997 *****************************************************************************/
1998 static void edge_tty_recv(struct usb_serial_port *port, unsigned char *data,
2003 cnt = tty_insert_flip_string(&port->port, data, length);
2005 dev_err(&port->dev, "%s - dropping data, %d bytes lost\n",
2006 __func__, length - cnt);
2011 tty_flip_buffer_push(&port->port);
2015 /*****************************************************************************
2017 * this function handles any change to the msr register for a port.
2018 *****************************************************************************/
2019 static void handle_new_msr(struct edgeport_port *edge_port, __u8 newMsr)
2021 struct async_icount *icount;
2023 if (newMsr & (EDGEPORT_MSR_DELTA_CTS | EDGEPORT_MSR_DELTA_DSR |
2024 EDGEPORT_MSR_DELTA_RI | EDGEPORT_MSR_DELTA_CD)) {
2025 icount = &edge_port->icount;
2027 /* update input line counters */
2028 if (newMsr & EDGEPORT_MSR_DELTA_CTS)
2030 if (newMsr & EDGEPORT_MSR_DELTA_DSR)
2032 if (newMsr & EDGEPORT_MSR_DELTA_CD)
2034 if (newMsr & EDGEPORT_MSR_DELTA_RI)
2036 wake_up_interruptible(&edge_port->delta_msr_wait);
2039 /* Save the new modem status */
2040 edge_port->shadowMSR = newMsr & 0xf0;
2044 /*****************************************************************************
2046 * this function handles any change to the lsr register for a port.
2047 *****************************************************************************/
2048 static void handle_new_lsr(struct edgeport_port *edge_port, __u8 lsrData,
2049 __u8 lsr, __u8 data)
2051 __u8 newLsr = (__u8) (lsr & (__u8)
2052 (LSR_OVER_ERR | LSR_PAR_ERR | LSR_FRM_ERR | LSR_BREAK));
2053 struct async_icount *icount;
2055 edge_port->shadowLSR = lsr;
2057 if (newLsr & LSR_BREAK) {
2059 * Parity and Framing errors only count if they
2060 * occur exclusive of a break being
2063 newLsr &= (__u8)(LSR_OVER_ERR | LSR_BREAK);
2066 /* Place LSR data byte into Rx buffer */
2068 edge_tty_recv(edge_port->port, &data, 1);
2070 /* update input line counters */
2071 icount = &edge_port->icount;
2072 if (newLsr & LSR_BREAK)
2074 if (newLsr & LSR_OVER_ERR)
2076 if (newLsr & LSR_PAR_ERR)
2078 if (newLsr & LSR_FRM_ERR)
2083 /****************************************************************************
2085 * writes a number of bytes to the Edgeport device's sram starting at the
2087 * If successful returns the number of bytes written, otherwise it returns
2088 * a negative error number of the problem.
2089 ****************************************************************************/
2090 static int sram_write(struct usb_serial *serial, __u16 extAddr, __u16 addr,
2091 __u16 length, const __u8 *data)
2094 __u16 current_length;
2095 unsigned char *transfer_buffer;
2097 dev_dbg(&serial->dev->dev, "%s - %x, %x, %d\n", __func__, extAddr, addr, length);
2099 transfer_buffer = kmalloc(64, GFP_KERNEL);
2100 if (!transfer_buffer) {
2101 dev_err(&serial->dev->dev, "%s - kmalloc(%d) failed.\n",
2106 /* need to split these writes up into 64 byte chunks */
2108 while (length > 0) {
2110 current_length = 64;
2112 current_length = length;
2114 /* dev_dbg(&serial->dev->dev, "%s - writing %x, %x, %d\n", __func__, extAddr, addr, current_length); */
2115 memcpy(transfer_buffer, data, current_length);
2116 result = usb_control_msg(serial->dev,
2117 usb_sndctrlpipe(serial->dev, 0),
2118 USB_REQUEST_ION_WRITE_RAM,
2119 0x40, addr, extAddr, transfer_buffer,
2120 current_length, 300);
2123 length -= current_length;
2124 addr += current_length;
2125 data += current_length;
2128 kfree(transfer_buffer);
2133 /****************************************************************************
2135 * writes a number of bytes to the Edgeport device's ROM starting at the
2137 * If successful returns the number of bytes written, otherwise it returns
2138 * a negative error number of the problem.
2139 ****************************************************************************/
2140 static int rom_write(struct usb_serial *serial, __u16 extAddr, __u16 addr,
2141 __u16 length, const __u8 *data)
2144 __u16 current_length;
2145 unsigned char *transfer_buffer;
2147 transfer_buffer = kmalloc(64, GFP_KERNEL);
2148 if (!transfer_buffer) {
2149 dev_err(&serial->dev->dev, "%s - kmalloc(%d) failed.\n",
2154 /* need to split these writes up into 64 byte chunks */
2156 while (length > 0) {
2158 current_length = 64;
2160 current_length = length;
2161 memcpy(transfer_buffer, data, current_length);
2162 result = usb_control_msg(serial->dev,
2163 usb_sndctrlpipe(serial->dev, 0),
2164 USB_REQUEST_ION_WRITE_ROM, 0x40,
2166 transfer_buffer, current_length, 300);
2169 length -= current_length;
2170 addr += current_length;
2171 data += current_length;
2174 kfree(transfer_buffer);
2179 /****************************************************************************
2181 * reads a number of bytes from the Edgeport device starting at the given
2183 * If successful returns the number of bytes read, otherwise it returns
2184 * a negative error number of the problem.
2185 ****************************************************************************/
2186 static int rom_read(struct usb_serial *serial, __u16 extAddr,
2187 __u16 addr, __u16 length, __u8 *data)
2190 __u16 current_length;
2191 unsigned char *transfer_buffer;
2193 transfer_buffer = kmalloc(64, GFP_KERNEL);
2194 if (!transfer_buffer) {
2195 dev_err(&serial->dev->dev,
2196 "%s - kmalloc(%d) failed.\n", __func__, 64);
2200 /* need to split these reads up into 64 byte chunks */
2202 while (length > 0) {
2204 current_length = 64;
2206 current_length = length;
2207 result = usb_control_msg(serial->dev,
2208 usb_rcvctrlpipe(serial->dev, 0),
2209 USB_REQUEST_ION_READ_ROM,
2210 0xC0, addr, extAddr, transfer_buffer,
2211 current_length, 300);
2214 memcpy(data, transfer_buffer, current_length);
2215 length -= current_length;
2216 addr += current_length;
2217 data += current_length;
2220 kfree(transfer_buffer);
2225 /****************************************************************************
2227 * Is used to send a IOSP message to the Edgeport device
2228 ****************************************************************************/
2229 static int send_iosp_ext_cmd(struct edgeport_port *edge_port,
2230 __u8 command, __u8 param)
2232 unsigned char *buffer;
2233 unsigned char *currentCommand;
2237 buffer = kmalloc(10, GFP_ATOMIC);
2239 dev_err(&edge_port->port->dev,
2240 "%s - kmalloc(%d) failed.\n", __func__, 10);
2244 currentCommand = buffer;
2246 MAKE_CMD_EXT_CMD(¤tCommand, &length,
2247 edge_port->port->number - edge_port->port->serial->minor,
2250 status = write_cmd_usb(edge_port, buffer, length);
2252 /* something bad happened, let's free up the memory */
2260 /*****************************************************************************
2262 * this function writes the given buffer out to the bulk write endpoint.
2263 *****************************************************************************/
2264 static int write_cmd_usb(struct edgeport_port *edge_port,
2265 unsigned char *buffer, int length)
2267 struct edgeport_serial *edge_serial =
2268 usb_get_serial_data(edge_port->port->serial);
2269 struct device *dev = &edge_port->port->dev;
2273 usb_serial_debug_data(dev, __func__, length, buffer);
2275 /* Allocate our next urb */
2276 urb = usb_alloc_urb(0, GFP_ATOMIC);
2280 atomic_inc(&CmdUrbs);
2281 dev_dbg(dev, "%s - ALLOCATE URB %p (outstanding %d)\n",
2282 __func__, urb, atomic_read(&CmdUrbs));
2284 usb_fill_bulk_urb(urb, edge_serial->serial->dev,
2285 usb_sndbulkpipe(edge_serial->serial->dev,
2286 edge_serial->bulk_out_endpoint),
2287 buffer, length, edge_bulk_out_cmd_callback, edge_port);
2289 edge_port->commandPending = true;
2290 status = usb_submit_urb(urb, GFP_ATOMIC);
2293 /* something went wrong */
2294 dev_err(dev, "%s - usb_submit_urb(write command) failed, status = %d\n",
2298 atomic_dec(&CmdUrbs);
2303 wait_event(&edge_port->wait_command, !edge_port->commandPending);
2305 if (edge_port->commandPending) {
2306 /* command timed out */
2307 dev_dbg(dev, "%s - command timed out\n", __func__);
2315 /*****************************************************************************
2316 * send_cmd_write_baud_rate
2317 * this function sends the proper command to change the baud rate of the
2319 *****************************************************************************/
2320 static int send_cmd_write_baud_rate(struct edgeport_port *edge_port,
2323 struct edgeport_serial *edge_serial =
2324 usb_get_serial_data(edge_port->port->serial);
2325 struct device *dev = &edge_port->port->dev;
2326 unsigned char *cmdBuffer;
2327 unsigned char *currCmd;
2331 unsigned char number =
2332 edge_port->port->number - edge_port->port->serial->minor;
2334 if (edge_serial->is_epic &&
2335 !edge_serial->epic_descriptor.Supports.IOSPSetBaudRate) {
2336 dev_dbg(dev, "SendCmdWriteBaudRate - NOT Setting baud rate for port = %d, baud = %d\n",
2337 edge_port->port->number, baudRate);
2341 dev_dbg(dev, "%s - port = %d, baud = %d\n", __func__,
2342 edge_port->port->number, baudRate);
2344 status = calc_baud_rate_divisor(dev, baudRate, &divisor);
2346 dev_err(dev, "%s - bad baud rate\n", __func__);
2350 /* Alloc memory for the string of commands. */
2351 cmdBuffer = kmalloc(0x100, GFP_ATOMIC);
2353 dev_err(dev, "%s - kmalloc(%d) failed.\n", __func__, 0x100);
2356 currCmd = cmdBuffer;
2358 /* Enable access to divisor latch */
2359 MAKE_CMD_WRITE_REG(&currCmd, &cmdLen, number, LCR, LCR_DL_ENABLE);
2361 /* Write the divisor itself */
2362 MAKE_CMD_WRITE_REG(&currCmd, &cmdLen, number, DLL, LOW8(divisor));
2363 MAKE_CMD_WRITE_REG(&currCmd, &cmdLen, number, DLM, HIGH8(divisor));
2365 /* Restore original value to disable access to divisor latch */
2366 MAKE_CMD_WRITE_REG(&currCmd, &cmdLen, number, LCR,
2367 edge_port->shadowLCR);
2369 status = write_cmd_usb(edge_port, cmdBuffer, cmdLen);
2371 /* something bad happened, let's free up the memory */
2379 /*****************************************************************************
2380 * calc_baud_rate_divisor
2381 * this function calculates the proper baud rate divisor for the specified
2383 *****************************************************************************/
2384 static int calc_baud_rate_divisor(struct device *dev, int baudrate, int *divisor)
2389 for (i = 0; i < ARRAY_SIZE(divisor_table); i++) {
2390 if (divisor_table[i].BaudRate == baudrate) {
2391 *divisor = divisor_table[i].Divisor;
2396 /* We have tried all of the standard baud rates
2397 * lets try to calculate the divisor for this baud rate
2398 * Make sure the baud rate is reasonable */
2399 if (baudrate > 50 && baudrate < 230400) {
2401 custom = (__u16)((230400L + baudrate/2) / baudrate);
2405 dev_dbg(dev, "%s - Baud %d = %d\n", __func__, baudrate, custom);
2413 /*****************************************************************************
2414 * send_cmd_write_uart_register
2415 * this function builds up a uart register message and sends to the device.
2416 *****************************************************************************/
2417 static int send_cmd_write_uart_register(struct edgeport_port *edge_port,
2418 __u8 regNum, __u8 regValue)
2420 struct edgeport_serial *edge_serial =
2421 usb_get_serial_data(edge_port->port->serial);
2422 struct device *dev = &edge_port->port->dev;
2423 unsigned char *cmdBuffer;
2424 unsigned char *currCmd;
2425 unsigned long cmdLen = 0;
2428 dev_dbg(dev, "%s - write to %s register 0x%02x\n",
2429 (regNum == MCR) ? "MCR" : "LCR", __func__, regValue);
2431 if (edge_serial->is_epic &&
2432 !edge_serial->epic_descriptor.Supports.IOSPWriteMCR &&
2434 dev_dbg(dev, "SendCmdWriteUartReg - Not writing to MCR Register\n");
2438 if (edge_serial->is_epic &&
2439 !edge_serial->epic_descriptor.Supports.IOSPWriteLCR &&
2441 dev_dbg(dev, "SendCmdWriteUartReg - Not writing to LCR Register\n");
2445 /* Alloc memory for the string of commands. */
2446 cmdBuffer = kmalloc(0x10, GFP_ATOMIC);
2447 if (cmdBuffer == NULL)
2450 currCmd = cmdBuffer;
2452 /* Build a cmd in the buffer to write the given register */
2453 MAKE_CMD_WRITE_REG(&currCmd, &cmdLen,
2454 edge_port->port->number - edge_port->port->serial->minor,
2457 status = write_cmd_usb(edge_port, cmdBuffer, cmdLen);
2459 /* something bad happened, let's free up the memory */
2467 /*****************************************************************************
2468 * change_port_settings
2469 * This routine is called to set the UART on the device to match the
2470 * specified new settings.
2471 *****************************************************************************/
2473 static void change_port_settings(struct tty_struct *tty,
2474 struct edgeport_port *edge_port, struct ktermios *old_termios)
2476 struct device *dev = &edge_port->port->dev;
2477 struct edgeport_serial *edge_serial =
2478 usb_get_serial_data(edge_port->port->serial);
2489 dev_dbg(dev, "%s - port %d\n", __func__, edge_port->port->number);
2491 if (!edge_port->open &&
2492 !edge_port->openPending) {
2493 dev_dbg(dev, "%s - port not opened\n", __func__);
2497 cflag = tty->termios.c_cflag;
2499 switch (cflag & CSIZE) {
2501 lData = LCR_BITS_5; mask = 0x1f;
2502 dev_dbg(dev, "%s - data bits = 5\n", __func__);
2505 lData = LCR_BITS_6; mask = 0x3f;
2506 dev_dbg(dev, "%s - data bits = 6\n", __func__);
2509 lData = LCR_BITS_7; mask = 0x7f;
2510 dev_dbg(dev, "%s - data bits = 7\n", __func__);
2515 dev_dbg(dev, "%s - data bits = 8\n", __func__);
2519 lParity = LCR_PAR_NONE;
2520 if (cflag & PARENB) {
2521 if (cflag & CMSPAR) {
2522 if (cflag & PARODD) {
2523 lParity = LCR_PAR_MARK;
2524 dev_dbg(dev, "%s - parity = mark\n", __func__);
2526 lParity = LCR_PAR_SPACE;
2527 dev_dbg(dev, "%s - parity = space\n", __func__);
2529 } else if (cflag & PARODD) {
2530 lParity = LCR_PAR_ODD;
2531 dev_dbg(dev, "%s - parity = odd\n", __func__);
2533 lParity = LCR_PAR_EVEN;
2534 dev_dbg(dev, "%s - parity = even\n", __func__);
2537 dev_dbg(dev, "%s - parity = none\n", __func__);
2540 if (cflag & CSTOPB) {
2542 dev_dbg(dev, "%s - stop bits = 2\n", __func__);
2545 dev_dbg(dev, "%s - stop bits = 1\n", __func__);
2548 /* figure out the flow control settings */
2549 rxFlow = txFlow = 0x00;
2550 if (cflag & CRTSCTS) {
2551 rxFlow |= IOSP_RX_FLOW_RTS;
2552 txFlow |= IOSP_TX_FLOW_CTS;
2553 dev_dbg(dev, "%s - RTS/CTS is enabled\n", __func__);
2555 dev_dbg(dev, "%s - RTS/CTS is disabled\n", __func__);
2558 /* if we are implementing XON/XOFF, set the start and stop character
2560 if (I_IXOFF(tty) || I_IXON(tty)) {
2561 unsigned char stop_char = STOP_CHAR(tty);
2562 unsigned char start_char = START_CHAR(tty);
2564 if ((!edge_serial->is_epic) ||
2565 ((edge_serial->is_epic) &&
2566 (edge_serial->epic_descriptor.Supports.IOSPSetXChar))) {
2567 send_iosp_ext_cmd(edge_port,
2568 IOSP_CMD_SET_XON_CHAR, start_char);
2569 send_iosp_ext_cmd(edge_port,
2570 IOSP_CMD_SET_XOFF_CHAR, stop_char);
2573 /* if we are implementing INBOUND XON/XOFF */
2575 rxFlow |= IOSP_RX_FLOW_XON_XOFF;
2576 dev_dbg(dev, "%s - INBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x\n",
2577 __func__, start_char, stop_char);
2579 dev_dbg(dev, "%s - INBOUND XON/XOFF is disabled\n", __func__);
2582 /* if we are implementing OUTBOUND XON/XOFF */
2584 txFlow |= IOSP_TX_FLOW_XON_XOFF;
2585 dev_dbg(dev, "%s - OUTBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x\n",
2586 __func__, start_char, stop_char);
2588 dev_dbg(dev, "%s - OUTBOUND XON/XOFF is disabled\n", __func__);
2592 /* Set flow control to the configured value */
2593 if ((!edge_serial->is_epic) ||
2594 ((edge_serial->is_epic) &&
2595 (edge_serial->epic_descriptor.Supports.IOSPSetRxFlow)))
2596 send_iosp_ext_cmd(edge_port, IOSP_CMD_SET_RX_FLOW, rxFlow);
2597 if ((!edge_serial->is_epic) ||
2598 ((edge_serial->is_epic) &&
2599 (edge_serial->epic_descriptor.Supports.IOSPSetTxFlow)))
2600 send_iosp_ext_cmd(edge_port, IOSP_CMD_SET_TX_FLOW, txFlow);
2603 edge_port->shadowLCR &= ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
2604 edge_port->shadowLCR |= (lData | lParity | lStop);
2606 edge_port->validDataMask = mask;
2608 /* Send the updated LCR value to the EdgePort */
2609 status = send_cmd_write_uart_register(edge_port, LCR,
2610 edge_port->shadowLCR);
2614 /* set up the MCR register and send it to the EdgePort */
2615 edge_port->shadowMCR = MCR_MASTER_IE;
2617 edge_port->shadowMCR |= (MCR_DTR | MCR_RTS);
2619 status = send_cmd_write_uart_register(edge_port, MCR,
2620 edge_port->shadowMCR);
2624 /* Determine divisor based on baud rate */
2625 baud = tty_get_baud_rate(tty);
2627 /* pick a default, any default... */
2631 dev_dbg(dev, "%s - baud rate = %d\n", __func__, baud);
2632 status = send_cmd_write_baud_rate(edge_port, baud);
2634 /* Speed change was not possible - put back the old speed */
2635 baud = tty_termios_baud_rate(old_termios);
2636 tty_encode_baud_rate(tty, baud, baud);
2641 /****************************************************************************
2643 * Turns a string from Unicode into ASCII.
2644 * Doesn't do a good job with any characters that are outside the normal
2645 * ASCII range, but it's only for debugging...
2646 * NOTE: expects the unicode in LE format
2647 ****************************************************************************/
2648 static void unicode_to_ascii(char *string, int buflen,
2649 __le16 *unicode, int unicode_size)
2653 if (buflen <= 0) /* never happens, but... */
2655 --buflen; /* space for nul */
2657 for (i = 0; i < unicode_size; i++) {
2660 string[i] = (char)(le16_to_cpu(unicode[i]));
2666 /****************************************************************************
2667 * get_manufacturing_desc
2668 * reads in the manufacturing descriptor and stores it into the serial
2670 ****************************************************************************/
2671 static void get_manufacturing_desc(struct edgeport_serial *edge_serial)
2673 struct device *dev = &edge_serial->serial->dev->dev;
2676 dev_dbg(dev, "getting manufacturer descriptor\n");
2678 response = rom_read(edge_serial->serial,
2679 (EDGE_MANUF_DESC_ADDR & 0xffff0000) >> 16,
2680 (__u16)(EDGE_MANUF_DESC_ADDR & 0x0000ffff),
2681 EDGE_MANUF_DESC_LEN,
2682 (__u8 *)(&edge_serial->manuf_descriptor));
2685 dev_err(dev, "error in getting manufacturer descriptor\n");
2688 dev_dbg(dev, "**Manufacturer Descriptor\n");
2689 dev_dbg(dev, " RomSize: %dK\n",
2690 edge_serial->manuf_descriptor.RomSize);
2691 dev_dbg(dev, " RamSize: %dK\n",
2692 edge_serial->manuf_descriptor.RamSize);
2693 dev_dbg(dev, " CpuRev: %d\n",
2694 edge_serial->manuf_descriptor.CpuRev);
2695 dev_dbg(dev, " BoardRev: %d\n",
2696 edge_serial->manuf_descriptor.BoardRev);
2697 dev_dbg(dev, " NumPorts: %d\n",
2698 edge_serial->manuf_descriptor.NumPorts);
2699 dev_dbg(dev, " DescDate: %d/%d/%d\n",
2700 edge_serial->manuf_descriptor.DescDate[0],
2701 edge_serial->manuf_descriptor.DescDate[1],
2702 edge_serial->manuf_descriptor.DescDate[2]+1900);
2703 unicode_to_ascii(string, sizeof(string),
2704 edge_serial->manuf_descriptor.SerialNumber,
2705 edge_serial->manuf_descriptor.SerNumLength/2);
2706 dev_dbg(dev, " SerialNumber: %s\n", string);
2707 unicode_to_ascii(string, sizeof(string),
2708 edge_serial->manuf_descriptor.AssemblyNumber,
2709 edge_serial->manuf_descriptor.AssemblyNumLength/2);
2710 dev_dbg(dev, " AssemblyNumber: %s\n", string);
2711 unicode_to_ascii(string, sizeof(string),
2712 edge_serial->manuf_descriptor.OemAssyNumber,
2713 edge_serial->manuf_descriptor.OemAssyNumLength/2);
2714 dev_dbg(dev, " OemAssyNumber: %s\n", string);
2715 dev_dbg(dev, " UartType: %d\n",
2716 edge_serial->manuf_descriptor.UartType);
2717 dev_dbg(dev, " IonPid: %d\n",
2718 edge_serial->manuf_descriptor.IonPid);
2719 dev_dbg(dev, " IonConfig: %d\n",
2720 edge_serial->manuf_descriptor.IonConfig);
2725 /****************************************************************************
2727 * reads in the bootloader descriptor and stores it into the serial
2729 ****************************************************************************/
2730 static void get_boot_desc(struct edgeport_serial *edge_serial)
2732 struct device *dev = &edge_serial->serial->dev->dev;
2735 dev_dbg(dev, "getting boot descriptor\n");
2737 response = rom_read(edge_serial->serial,
2738 (EDGE_BOOT_DESC_ADDR & 0xffff0000) >> 16,
2739 (__u16)(EDGE_BOOT_DESC_ADDR & 0x0000ffff),
2741 (__u8 *)(&edge_serial->boot_descriptor));
2744 dev_err(dev, "error in getting boot descriptor\n");
2746 dev_dbg(dev, "**Boot Descriptor:\n");
2747 dev_dbg(dev, " BootCodeLength: %d\n",
2748 le16_to_cpu(edge_serial->boot_descriptor.BootCodeLength));
2749 dev_dbg(dev, " MajorVersion: %d\n",
2750 edge_serial->boot_descriptor.MajorVersion);
2751 dev_dbg(dev, " MinorVersion: %d\n",
2752 edge_serial->boot_descriptor.MinorVersion);
2753 dev_dbg(dev, " BuildNumber: %d\n",
2754 le16_to_cpu(edge_serial->boot_descriptor.BuildNumber));
2755 dev_dbg(dev, " Capabilities: 0x%x\n",
2756 le16_to_cpu(edge_serial->boot_descriptor.Capabilities));
2757 dev_dbg(dev, " UConfig0: %d\n",
2758 edge_serial->boot_descriptor.UConfig0);
2759 dev_dbg(dev, " UConfig1: %d\n",
2760 edge_serial->boot_descriptor.UConfig1);
2765 /****************************************************************************
2766 * load_application_firmware
2767 * This is called to load the application firmware to the device
2768 ****************************************************************************/
2769 static void load_application_firmware(struct edgeport_serial *edge_serial)
2771 struct device *dev = &edge_serial->serial->dev->dev;
2772 const struct ihex_binrec *rec;
2773 const struct firmware *fw;
2774 const char *fw_name;
2775 const char *fw_info;
2780 switch (edge_serial->product_info.iDownloadFile) {
2781 case EDGE_DOWNLOAD_FILE_I930:
2782 fw_info = "downloading firmware version (930)";
2783 fw_name = "edgeport/down.fw";
2786 case EDGE_DOWNLOAD_FILE_80251:
2787 fw_info = "downloading firmware version (80251)";
2788 fw_name = "edgeport/down2.fw";
2791 case EDGE_DOWNLOAD_FILE_NONE:
2792 dev_dbg(dev, "No download file specified, skipping download\n");
2799 response = request_ihex_firmware(&fw, fw_name,
2800 &edge_serial->serial->dev->dev);
2802 dev_err(dev, "Failed to load image \"%s\" err %d\n",
2807 rec = (const struct ihex_binrec *)fw->data;
2808 build = (rec->data[2] << 8) | rec->data[3];
2810 dev_dbg(dev, "%s %d.%d.%d\n", fw_info, rec->data[0], rec->data[1], build);
2812 edge_serial->product_info.FirmwareMajorVersion = rec->data[0];
2813 edge_serial->product_info.FirmwareMinorVersion = rec->data[1];
2814 edge_serial->product_info.FirmwareBuildNumber = cpu_to_le16(build);
2816 for (rec = ihex_next_binrec(rec); rec;
2817 rec = ihex_next_binrec(rec)) {
2818 Operaddr = be32_to_cpu(rec->addr);
2819 response = sram_write(edge_serial->serial,
2822 be16_to_cpu(rec->len),
2825 dev_err(&edge_serial->serial->dev->dev,
2826 "sram_write failed (%x, %x, %d)\n",
2827 Operaddr >> 16, Operaddr & 0xFFFF,
2828 be16_to_cpu(rec->len));
2833 dev_dbg(dev, "sending exec_dl_code\n");
2834 response = usb_control_msg (edge_serial->serial->dev,
2835 usb_sndctrlpipe(edge_serial->serial->dev, 0),
2836 USB_REQUEST_ION_EXEC_DL_CODE,
2837 0x40, 0x4000, 0x0001, NULL, 0, 3000);
2839 release_firmware(fw);
2843 /****************************************************************************
2845 ****************************************************************************/
2846 static int edge_startup(struct usb_serial *serial)
2848 struct edgeport_serial *edge_serial;
2849 struct usb_device *dev;
2850 struct device *ddev = &serial->dev->dev;
2853 bool interrupt_in_found;
2855 bool bulk_out_found;
2856 static __u32 descriptor[3] = { EDGE_COMPATIBILITY_MASK0,
2857 EDGE_COMPATIBILITY_MASK1,
2858 EDGE_COMPATIBILITY_MASK2 };
2862 /* create our private serial structure */
2863 edge_serial = kzalloc(sizeof(struct edgeport_serial), GFP_KERNEL);
2864 if (edge_serial == NULL) {
2865 dev_err(&serial->dev->dev, "%s - Out of memory\n", __func__);
2868 spin_lock_init(&edge_serial->es_lock);
2869 edge_serial->serial = serial;
2870 usb_set_serial_data(serial, edge_serial);
2872 /* get the name for the device from the device */
2873 i = usb_string(dev, dev->descriptor.iManufacturer,
2874 &edge_serial->name[0], MAX_NAME_LEN+1);
2877 edge_serial->name[i++] = ' ';
2878 usb_string(dev, dev->descriptor.iProduct,
2879 &edge_serial->name[i], MAX_NAME_LEN+2 - i);
2881 dev_info(&serial->dev->dev, "%s detected\n", edge_serial->name);
2883 /* Read the epic descriptor */
2884 if (get_epic_descriptor(edge_serial) <= 0) {
2885 /* memcpy descriptor to Supports structures */
2886 memcpy(&edge_serial->epic_descriptor.Supports, descriptor,
2887 sizeof(struct edge_compatibility_bits));
2889 /* get the manufacturing descriptor for this device */
2890 get_manufacturing_desc(edge_serial);
2892 /* get the boot descriptor */
2893 get_boot_desc(edge_serial);
2895 get_product_info(edge_serial);
2898 /* set the number of ports from the manufacturing description */
2899 /* serial->num_ports = serial->product_info.NumPorts; */
2900 if ((!edge_serial->is_epic) &&
2901 (edge_serial->product_info.NumPorts != serial->num_ports)) {
2903 "Device Reported %d serial ports vs. core thinking we have %d ports, email greg@kroah.com this information.\n",
2904 edge_serial->product_info.NumPorts,
2908 dev_dbg(ddev, "%s - time 1 %ld\n", __func__, jiffies);
2910 /* If not an EPiC device */
2911 if (!edge_serial->is_epic) {
2912 /* now load the application firmware into this device */
2913 load_application_firmware(edge_serial);
2915 dev_dbg(ddev, "%s - time 2 %ld\n", __func__, jiffies);
2917 /* Check current Edgeport EEPROM and update if necessary */
2918 update_edgeport_E2PROM(edge_serial);
2920 dev_dbg(ddev, "%s - time 3 %ld\n", __func__, jiffies);
2922 /* set the configuration to use #1 */
2923 /* dev_dbg(ddev, "set_configuration 1\n"); */
2924 /* usb_set_configuration (dev, 1); */
2926 dev_dbg(ddev, " FirmwareMajorVersion %d.%d.%d\n",
2927 edge_serial->product_info.FirmwareMajorVersion,
2928 edge_serial->product_info.FirmwareMinorVersion,
2929 le16_to_cpu(edge_serial->product_info.FirmwareBuildNumber));
2931 /* we set up the pointers to the endpoints in the edge_open function,
2932 * as the structures aren't created yet. */
2936 if (edge_serial->is_epic) {
2937 /* EPIC thing, set up our interrupt polling now and our read
2938 * urb, so that the device knows it really is connected. */
2939 interrupt_in_found = bulk_in_found = bulk_out_found = false;
2940 for (i = 0; i < serial->interface->altsetting[0]
2941 .desc.bNumEndpoints; ++i) {
2942 struct usb_endpoint_descriptor *endpoint;
2945 endpoint = &serial->interface->altsetting[0].
2947 buffer_size = usb_endpoint_maxp(endpoint);
2948 if (!interrupt_in_found &&
2949 (usb_endpoint_is_int_in(endpoint))) {
2950 /* we found a interrupt in endpoint */
2951 dev_dbg(ddev, "found interrupt in\n");
2953 /* not set up yet, so do it now */
2954 edge_serial->interrupt_read_urb =
2955 usb_alloc_urb(0, GFP_KERNEL);
2956 if (!edge_serial->interrupt_read_urb) {
2957 dev_err(ddev, "out of memory\n");
2960 edge_serial->interrupt_in_buffer =
2961 kmalloc(buffer_size, GFP_KERNEL);
2962 if (!edge_serial->interrupt_in_buffer) {
2963 dev_err(ddev, "out of memory\n");
2964 usb_free_urb(edge_serial->interrupt_read_urb);
2967 edge_serial->interrupt_in_endpoint =
2968 endpoint->bEndpointAddress;
2970 /* set up our interrupt urb */
2972 edge_serial->interrupt_read_urb,
2975 endpoint->bEndpointAddress),
2976 edge_serial->interrupt_in_buffer,
2978 edge_interrupt_callback,
2980 endpoint->bInterval);
2982 interrupt_in_found = true;
2985 if (!bulk_in_found &&
2986 (usb_endpoint_is_bulk_in(endpoint))) {
2987 /* we found a bulk in endpoint */
2988 dev_dbg(ddev, "found bulk in\n");
2990 /* not set up yet, so do it now */
2991 edge_serial->read_urb =
2992 usb_alloc_urb(0, GFP_KERNEL);
2993 if (!edge_serial->read_urb) {
2994 dev_err(ddev, "out of memory\n");
2997 edge_serial->bulk_in_buffer =
2998 kmalloc(buffer_size, GFP_KERNEL);
2999 if (!edge_serial->bulk_in_buffer) {
3000 dev_err(&dev->dev, "out of memory\n");
3001 usb_free_urb(edge_serial->read_urb);
3004 edge_serial->bulk_in_endpoint =
3005 endpoint->bEndpointAddress;
3007 /* set up our bulk in urb */
3008 usb_fill_bulk_urb(edge_serial->read_urb, dev,
3009 usb_rcvbulkpipe(dev,
3010 endpoint->bEndpointAddress),
3011 edge_serial->bulk_in_buffer,
3012 usb_endpoint_maxp(endpoint),
3013 edge_bulk_in_callback,
3015 bulk_in_found = true;
3018 if (!bulk_out_found &&
3019 (usb_endpoint_is_bulk_out(endpoint))) {
3020 /* we found a bulk out endpoint */
3021 dev_dbg(ddev, "found bulk out\n");
3022 edge_serial->bulk_out_endpoint =
3023 endpoint->bEndpointAddress;
3024 bulk_out_found = true;
3028 if (!interrupt_in_found || !bulk_in_found || !bulk_out_found) {
3029 dev_err(ddev, "Error - the proper endpoints were not found!\n");
3033 /* start interrupt read for this edgeport this interrupt will
3034 * continue as long as the edgeport is connected */
3035 response = usb_submit_urb(edge_serial->interrupt_read_urb,
3038 dev_err(ddev, "%s - Error %d submitting control urb\n",
3039 __func__, response);
3045 /****************************************************************************
3047 * This function is called whenever the device is removed from the usb bus.
3048 ****************************************************************************/
3049 static void edge_disconnect(struct usb_serial *serial)
3051 struct edgeport_serial *edge_serial = usb_get_serial_data(serial);
3053 /* stop reads and writes on all ports */
3054 /* free up our endpoint stuff */
3055 if (edge_serial->is_epic) {
3056 usb_kill_urb(edge_serial->interrupt_read_urb);
3057 usb_free_urb(edge_serial->interrupt_read_urb);
3058 kfree(edge_serial->interrupt_in_buffer);
3060 usb_kill_urb(edge_serial->read_urb);
3061 usb_free_urb(edge_serial->read_urb);
3062 kfree(edge_serial->bulk_in_buffer);
3067 /****************************************************************************
3069 * This function is called when the device structure is deallocated.
3070 ****************************************************************************/
3071 static void edge_release(struct usb_serial *serial)
3073 struct edgeport_serial *edge_serial = usb_get_serial_data(serial);
3078 static int edge_port_probe(struct usb_serial_port *port)
3080 struct edgeport_port *edge_port;
3082 edge_port = kzalloc(sizeof(*edge_port), GFP_KERNEL);
3086 spin_lock_init(&edge_port->ep_lock);
3087 edge_port->port = port;
3089 usb_set_serial_port_data(port, edge_port);
3094 static int edge_port_remove(struct usb_serial_port *port)
3096 struct edgeport_port *edge_port;
3098 edge_port = usb_get_serial_port_data(port);
3104 module_usb_serial_driver(serial_drivers, id_table_combined);
3106 MODULE_AUTHOR(DRIVER_AUTHOR);
3107 MODULE_DESCRIPTION(DRIVER_DESC);
3108 MODULE_LICENSE("GPL");
3109 MODULE_FIRMWARE("edgeport/boot.fw");
3110 MODULE_FIRMWARE("edgeport/boot2.fw");
3111 MODULE_FIRMWARE("edgeport/down.fw");
3112 MODULE_FIRMWARE("edgeport/down2.fw");