i2c-eg20t: change timeout value 50msec to 1000msec
[zen-stable.git] / drivers / usb / serial / io_edgeport.c
blob0497575e4799fd73fe65903dfea799ffce034518
1 /*
2 * Edgeport USB Serial Converter driver
4 * Copyright (C) 2000 Inside Out Networks, All rights reserved.
5 * Copyright (C) 2001-2002 Greg Kroah-Hartman <greg@kroah.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * Supports the following devices:
13 * Edgeport/4
14 * Edgeport/4t
15 * Edgeport/2
16 * Edgeport/4i
17 * Edgeport/2i
18 * Edgeport/421
19 * Edgeport/21
20 * Rapidport/4
21 * Edgeport/8
22 * Edgeport/2D8
23 * Edgeport/4D8
24 * Edgeport/8i
26 * For questions or problems with this driver, contact Inside Out
27 * Networks technical support, or Peter Berger <pberger@brimson.com>,
28 * or Al Borchers <alborchers@steinerpoint.com>.
32 #include <linux/kernel.h>
33 #include <linux/jiffies.h>
34 #include <linux/errno.h>
35 #include <linux/init.h>
36 #include <linux/slab.h>
37 #include <linux/tty.h>
38 #include <linux/tty_driver.h>
39 #include <linux/tty_flip.h>
40 #include <linux/module.h>
41 #include <linux/spinlock.h>
42 #include <linux/serial.h>
43 #include <linux/ioctl.h>
44 #include <linux/wait.h>
45 #include <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 */
55 * Version Information
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 */
68 enum RXSTATE {
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) */
76 /* Transmit Fifo
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.
81 struct TxFifo {
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 */
97 spinlock_t ep_lock;
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 */
105 __u8 validDataMask;
106 __u32 baudRate;
108 bool open;
109 bool openPending;
110 bool commandPending;
111 bool closePending;
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;
142 spinlock_t es_lock;
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 {
161 __u32 BaudRate;
162 __u16 Divisor;
166 * Define table of divisors for Rev A EdgePort/4 hardware
167 * These assume a 3.6864MHz crystal, the standard /16, and
168 * MCR.7 = 0.
171 static const struct divisor_table_entry divisor_table[] = {
172 { 50, 4608},
173 { 75, 3072},
174 { 110, 2095}, /* 2094.545455 => 230450 => .0217 % over */
175 { 134, 1713}, /* 1713.011152 => 230398.5 => .00065% under */
176 { 150, 1536},
177 { 300, 768},
178 { 600, 384},
179 { 1200, 192},
180 { 1800, 128},
181 { 2400, 96},
182 { 4800, 48},
183 { 7200, 32},
184 { 9600, 24},
185 { 14400, 16},
186 { 19200, 12},
187 { 38400, 6},
188 { 57600, 4},
189 { 115200, 2},
190 { 230400, 1},
193 /* local variables */
194 static bool debug;
196 static atomic_t CmdUrbs; /* Number of outstanding Command Write Urbs */
199 /* local function prototypes */
201 /* function prototypes for all URB callbacks */
202 static void edge_interrupt_callback(struct urb *urb);
203 static void edge_bulk_in_callback(struct urb *urb);
204 static void edge_bulk_out_data_callback(struct urb *urb);
205 static void edge_bulk_out_cmd_callback(struct urb *urb);
207 /* function prototypes for the usbserial callbacks */
208 static int edge_open(struct tty_struct *tty, struct usb_serial_port *port);
209 static void edge_close(struct usb_serial_port *port);
210 static int edge_write(struct tty_struct *tty, struct usb_serial_port *port,
211 const unsigned char *buf, int count);
212 static int edge_write_room(struct tty_struct *tty);
213 static int edge_chars_in_buffer(struct tty_struct *tty);
214 static void edge_throttle(struct tty_struct *tty);
215 static void edge_unthrottle(struct tty_struct *tty);
216 static void edge_set_termios(struct tty_struct *tty,
217 struct usb_serial_port *port,
218 struct ktermios *old_termios);
219 static int edge_ioctl(struct tty_struct *tty,
220 unsigned int cmd, unsigned long arg);
221 static void edge_break(struct tty_struct *tty, int break_state);
222 static int edge_tiocmget(struct tty_struct *tty);
223 static int edge_tiocmset(struct tty_struct *tty,
224 unsigned int set, unsigned int clear);
225 static int edge_get_icount(struct tty_struct *tty,
226 struct serial_icounter_struct *icount);
227 static int edge_startup(struct usb_serial *serial);
228 static void edge_disconnect(struct usb_serial *serial);
229 static void edge_release(struct usb_serial *serial);
231 #include "io_tables.h" /* all of the devices that this driver supports */
233 /* function prototypes for all of our local functions */
235 static void process_rcvd_data(struct edgeport_serial *edge_serial,
236 unsigned char *buffer, __u16 bufferLength);
237 static void process_rcvd_status(struct edgeport_serial *edge_serial,
238 __u8 byte2, __u8 byte3);
239 static void edge_tty_recv(struct device *dev, struct tty_struct *tty,
240 unsigned char *data, int length);
241 static void handle_new_msr(struct edgeport_port *edge_port, __u8 newMsr);
242 static void handle_new_lsr(struct edgeport_port *edge_port, __u8 lsrData,
243 __u8 lsr, __u8 data);
244 static int send_iosp_ext_cmd(struct edgeport_port *edge_port, __u8 command,
245 __u8 param);
246 static int calc_baud_rate_divisor(int baud_rate, int *divisor);
247 static int send_cmd_write_baud_rate(struct edgeport_port *edge_port,
248 int baudRate);
249 static void change_port_settings(struct tty_struct *tty,
250 struct edgeport_port *edge_port,
251 struct ktermios *old_termios);
252 static int send_cmd_write_uart_register(struct edgeport_port *edge_port,
253 __u8 regNum, __u8 regValue);
254 static int write_cmd_usb(struct edgeport_port *edge_port,
255 unsigned char *buffer, int writeLength);
256 static void send_more_port_data(struct edgeport_serial *edge_serial,
257 struct edgeport_port *edge_port);
259 static int sram_write(struct usb_serial *serial, __u16 extAddr, __u16 addr,
260 __u16 length, const __u8 *data);
261 static int rom_read(struct usb_serial *serial, __u16 extAddr, __u16 addr,
262 __u16 length, __u8 *data);
263 static int rom_write(struct usb_serial *serial, __u16 extAddr, __u16 addr,
264 __u16 length, const __u8 *data);
265 static void get_manufacturing_desc(struct edgeport_serial *edge_serial);
266 static void get_boot_desc(struct edgeport_serial *edge_serial);
267 static void load_application_firmware(struct edgeport_serial *edge_serial);
269 static void unicode_to_ascii(char *string, int buflen,
270 __le16 *unicode, int unicode_size);
273 /* ************************************************************************ */
274 /* ************************************************************************ */
275 /* ************************************************************************ */
276 /* ************************************************************************ */
278 /************************************************************************
280 * update_edgeport_E2PROM() Compare current versions of *
281 * Boot ROM and Manufacture *
282 * Descriptors with versions *
283 * embedded in this driver *
285 ************************************************************************/
286 static void update_edgeport_E2PROM(struct edgeport_serial *edge_serial)
288 __u32 BootCurVer;
289 __u32 BootNewVer;
290 __u8 BootMajorVersion;
291 __u8 BootMinorVersion;
292 __u16 BootBuildNumber;
293 __u32 Bootaddr;
294 const struct ihex_binrec *rec;
295 const struct firmware *fw;
296 const char *fw_name;
297 int response;
299 switch (edge_serial->product_info.iDownloadFile) {
300 case EDGE_DOWNLOAD_FILE_I930:
301 fw_name = "edgeport/boot.fw";
302 break;
303 case EDGE_DOWNLOAD_FILE_80251:
304 fw_name = "edgeport/boot2.fw";
305 break;
306 default:
307 return;
310 response = request_ihex_firmware(&fw, fw_name,
311 &edge_serial->serial->dev->dev);
312 if (response) {
313 printk(KERN_ERR "Failed to load image \"%s\" err %d\n",
314 fw_name, response);
315 return;
318 rec = (const struct ihex_binrec *)fw->data;
319 BootMajorVersion = rec->data[0];
320 BootMinorVersion = rec->data[1];
321 BootBuildNumber = (rec->data[2] << 8) | rec->data[3];
323 /* Check Boot Image Version */
324 BootCurVer = (edge_serial->boot_descriptor.MajorVersion << 24) +
325 (edge_serial->boot_descriptor.MinorVersion << 16) +
326 le16_to_cpu(edge_serial->boot_descriptor.BuildNumber);
328 BootNewVer = (BootMajorVersion << 24) +
329 (BootMinorVersion << 16) +
330 BootBuildNumber;
332 dbg("Current Boot Image version %d.%d.%d",
333 edge_serial->boot_descriptor.MajorVersion,
334 edge_serial->boot_descriptor.MinorVersion,
335 le16_to_cpu(edge_serial->boot_descriptor.BuildNumber));
338 if (BootNewVer > BootCurVer) {
339 dbg("**Update Boot Image from %d.%d.%d to %d.%d.%d",
340 edge_serial->boot_descriptor.MajorVersion,
341 edge_serial->boot_descriptor.MinorVersion,
342 le16_to_cpu(edge_serial->boot_descriptor.BuildNumber),
343 BootMajorVersion, BootMinorVersion, BootBuildNumber);
345 dbg("Downloading new Boot Image");
347 for (rec = ihex_next_binrec(rec); rec;
348 rec = ihex_next_binrec(rec)) {
349 Bootaddr = be32_to_cpu(rec->addr);
350 response = rom_write(edge_serial->serial,
351 Bootaddr >> 16,
352 Bootaddr & 0xFFFF,
353 be16_to_cpu(rec->len),
354 &rec->data[0]);
355 if (response < 0) {
356 dev_err(&edge_serial->serial->dev->dev,
357 "rom_write failed (%x, %x, %d)\n",
358 Bootaddr >> 16, Bootaddr & 0xFFFF,
359 be16_to_cpu(rec->len));
360 break;
363 } else {
364 dbg("Boot Image -- already up to date");
366 release_firmware(fw);
369 #if 0
370 /************************************************************************
372 * Get string descriptor from device
374 ************************************************************************/
375 static int get_string_desc(struct usb_device *dev, int Id,
376 struct usb_string_descriptor **pRetDesc)
378 struct usb_string_descriptor StringDesc;
379 struct usb_string_descriptor *pStringDesc;
381 dbg("%s - USB String ID = %d", __func__, Id);
383 if (!usb_get_descriptor(dev, USB_DT_STRING, Id, &StringDesc,
384 sizeof(StringDesc)))
385 return 0;
387 pStringDesc = kmalloc(StringDesc.bLength, GFP_KERNEL);
388 if (!pStringDesc)
389 return -1;
391 if (!usb_get_descriptor(dev, USB_DT_STRING, Id, pStringDesc,
392 StringDesc.bLength)) {
393 kfree(pStringDesc);
394 return -1;
397 *pRetDesc = pStringDesc;
398 return 0;
400 #endif
402 static void dump_product_info(struct edgeport_product_info *product_info)
404 /* Dump Product Info structure */
405 dbg("**Product Information:");
406 dbg(" ProductId %x", product_info->ProductId);
407 dbg(" NumPorts %d", product_info->NumPorts);
408 dbg(" ProdInfoVer %d", product_info->ProdInfoVer);
409 dbg(" IsServer %d", product_info->IsServer);
410 dbg(" IsRS232 %d", product_info->IsRS232);
411 dbg(" IsRS422 %d", product_info->IsRS422);
412 dbg(" IsRS485 %d", product_info->IsRS485);
413 dbg(" RomSize %d", product_info->RomSize);
414 dbg(" RamSize %d", product_info->RamSize);
415 dbg(" CpuRev %x", product_info->CpuRev);
416 dbg(" BoardRev %x", product_info->BoardRev);
417 dbg(" BootMajorVersion %d.%d.%d", product_info->BootMajorVersion,
418 product_info->BootMinorVersion,
419 le16_to_cpu(product_info->BootBuildNumber));
420 dbg(" FirmwareMajorVersion %d.%d.%d",
421 product_info->FirmwareMajorVersion,
422 product_info->FirmwareMinorVersion,
423 le16_to_cpu(product_info->FirmwareBuildNumber));
424 dbg(" ManufactureDescDate %d/%d/%d",
425 product_info->ManufactureDescDate[0],
426 product_info->ManufactureDescDate[1],
427 product_info->ManufactureDescDate[2]+1900);
428 dbg(" iDownloadFile 0x%x", product_info->iDownloadFile);
429 dbg(" EpicVer %d", product_info->EpicVer);
432 static void get_product_info(struct edgeport_serial *edge_serial)
434 struct edgeport_product_info *product_info = &edge_serial->product_info;
436 memset(product_info, 0, sizeof(struct edgeport_product_info));
438 product_info->ProductId = (__u16)(le16_to_cpu(edge_serial->serial->dev->descriptor.idProduct) & ~ION_DEVICE_ID_80251_NETCHIP);
439 product_info->NumPorts = edge_serial->manuf_descriptor.NumPorts;
440 product_info->ProdInfoVer = 0;
442 product_info->RomSize = edge_serial->manuf_descriptor.RomSize;
443 product_info->RamSize = edge_serial->manuf_descriptor.RamSize;
444 product_info->CpuRev = edge_serial->manuf_descriptor.CpuRev;
445 product_info->BoardRev = edge_serial->manuf_descriptor.BoardRev;
447 product_info->BootMajorVersion =
448 edge_serial->boot_descriptor.MajorVersion;
449 product_info->BootMinorVersion =
450 edge_serial->boot_descriptor.MinorVersion;
451 product_info->BootBuildNumber =
452 edge_serial->boot_descriptor.BuildNumber;
454 memcpy(product_info->ManufactureDescDate,
455 edge_serial->manuf_descriptor.DescDate,
456 sizeof(edge_serial->manuf_descriptor.DescDate));
458 /* check if this is 2nd generation hardware */
459 if (le16_to_cpu(edge_serial->serial->dev->descriptor.idProduct)
460 & ION_DEVICE_ID_80251_NETCHIP)
461 product_info->iDownloadFile = EDGE_DOWNLOAD_FILE_80251;
462 else
463 product_info->iDownloadFile = EDGE_DOWNLOAD_FILE_I930;
465 /* Determine Product type and set appropriate flags */
466 switch (DEVICE_ID_FROM_USB_PRODUCT_ID(product_info->ProductId)) {
467 case ION_DEVICE_ID_EDGEPORT_COMPATIBLE:
468 case ION_DEVICE_ID_EDGEPORT_4T:
469 case ION_DEVICE_ID_EDGEPORT_4:
470 case ION_DEVICE_ID_EDGEPORT_2:
471 case ION_DEVICE_ID_EDGEPORT_8_DUAL_CPU:
472 case ION_DEVICE_ID_EDGEPORT_8:
473 case ION_DEVICE_ID_EDGEPORT_421:
474 case ION_DEVICE_ID_EDGEPORT_21:
475 case ION_DEVICE_ID_EDGEPORT_2_DIN:
476 case ION_DEVICE_ID_EDGEPORT_4_DIN:
477 case ION_DEVICE_ID_EDGEPORT_16_DUAL_CPU:
478 product_info->IsRS232 = 1;
479 break;
481 case ION_DEVICE_ID_EDGEPORT_2I: /* Edgeport/2 RS422/RS485 */
482 product_info->IsRS422 = 1;
483 product_info->IsRS485 = 1;
484 break;
486 case ION_DEVICE_ID_EDGEPORT_8I: /* Edgeport/4 RS422 */
487 case ION_DEVICE_ID_EDGEPORT_4I: /* Edgeport/4 RS422 */
488 product_info->IsRS422 = 1;
489 break;
492 dump_product_info(product_info);
495 static int get_epic_descriptor(struct edgeport_serial *ep)
497 int result;
498 struct usb_serial *serial = ep->serial;
499 struct edgeport_product_info *product_info = &ep->product_info;
500 struct edge_compatibility_descriptor *epic = &ep->epic_descriptor;
501 struct edge_compatibility_bits *bits;
503 ep->is_epic = 0;
504 result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
505 USB_REQUEST_ION_GET_EPIC_DESC,
506 0xC0, 0x00, 0x00,
507 &ep->epic_descriptor,
508 sizeof(struct edge_compatibility_descriptor),
509 300);
511 dbg("%s result = %d", __func__, result);
513 if (result > 0) {
514 ep->is_epic = 1;
515 memset(product_info, 0, sizeof(struct edgeport_product_info));
517 product_info->NumPorts = epic->NumPorts;
518 product_info->ProdInfoVer = 0;
519 product_info->FirmwareMajorVersion = epic->MajorVersion;
520 product_info->FirmwareMinorVersion = epic->MinorVersion;
521 product_info->FirmwareBuildNumber = epic->BuildNumber;
522 product_info->iDownloadFile = epic->iDownloadFile;
523 product_info->EpicVer = epic->EpicVer;
524 product_info->Epic = epic->Supports;
525 product_info->ProductId = ION_DEVICE_ID_EDGEPORT_COMPATIBLE;
526 dump_product_info(product_info);
528 bits = &ep->epic_descriptor.Supports;
529 dbg("**EPIC descriptor:");
530 dbg(" VendEnableSuspend: %s", bits->VendEnableSuspend ? "TRUE": "FALSE");
531 dbg(" IOSPOpen : %s", bits->IOSPOpen ? "TRUE": "FALSE");
532 dbg(" IOSPClose : %s", bits->IOSPClose ? "TRUE": "FALSE");
533 dbg(" IOSPChase : %s", bits->IOSPChase ? "TRUE": "FALSE");
534 dbg(" IOSPSetRxFlow : %s", bits->IOSPSetRxFlow ? "TRUE": "FALSE");
535 dbg(" IOSPSetTxFlow : %s", bits->IOSPSetTxFlow ? "TRUE": "FALSE");
536 dbg(" IOSPSetXChar : %s", bits->IOSPSetXChar ? "TRUE": "FALSE");
537 dbg(" IOSPRxCheck : %s", bits->IOSPRxCheck ? "TRUE": "FALSE");
538 dbg(" IOSPSetClrBreak : %s", bits->IOSPSetClrBreak ? "TRUE": "FALSE");
539 dbg(" IOSPWriteMCR : %s", bits->IOSPWriteMCR ? "TRUE": "FALSE");
540 dbg(" IOSPWriteLCR : %s", bits->IOSPWriteLCR ? "TRUE": "FALSE");
541 dbg(" IOSPSetBaudRate : %s", bits->IOSPSetBaudRate ? "TRUE": "FALSE");
542 dbg(" TrueEdgeport : %s", bits->TrueEdgeport ? "TRUE": "FALSE");
545 return result;
549 /************************************************************************/
550 /************************************************************************/
551 /* U S B C A L L B A C K F U N C T I O N S */
552 /* U S B C A L L B A C K F U N C T I O N S */
553 /************************************************************************/
554 /************************************************************************/
556 /*****************************************************************************
557 * edge_interrupt_callback
558 * this is the callback function for when we have received data on the
559 * interrupt endpoint.
560 *****************************************************************************/
561 static void edge_interrupt_callback(struct urb *urb)
563 struct edgeport_serial *edge_serial = urb->context;
564 struct edgeport_port *edge_port;
565 struct usb_serial_port *port;
566 struct tty_struct *tty;
567 unsigned char *data = urb->transfer_buffer;
568 int length = urb->actual_length;
569 int bytes_avail;
570 int position;
571 int txCredits;
572 int portNumber;
573 int result;
574 int status = urb->status;
576 dbg("%s", __func__);
578 switch (status) {
579 case 0:
580 /* success */
581 break;
582 case -ECONNRESET:
583 case -ENOENT:
584 case -ESHUTDOWN:
585 /* this urb is terminated, clean up */
586 dbg("%s - urb shutting down with status: %d",
587 __func__, status);
588 return;
589 default:
590 dbg("%s - nonzero urb status received: %d", __func__, status);
591 goto exit;
594 /* process this interrupt-read even if there are no ports open */
595 if (length) {
596 usb_serial_debug_data(debug, &edge_serial->serial->dev->dev,
597 __func__, length, data);
599 if (length > 1) {
600 bytes_avail = data[0] | (data[1] << 8);
601 if (bytes_avail) {
602 spin_lock(&edge_serial->es_lock);
603 edge_serial->rxBytesAvail += bytes_avail;
604 dbg("%s - bytes_avail=%d, rxBytesAvail=%d, read_in_progress=%d", __func__, bytes_avail, edge_serial->rxBytesAvail, edge_serial->read_in_progress);
606 if (edge_serial->rxBytesAvail > 0 &&
607 !edge_serial->read_in_progress) {
608 dbg("%s - posting a read", __func__);
609 edge_serial->read_in_progress = true;
611 /* we have pending bytes on the
612 bulk in pipe, send a request */
613 result = usb_submit_urb(edge_serial->read_urb, GFP_ATOMIC);
614 if (result) {
615 dev_err(&edge_serial->serial->dev->dev, "%s - usb_submit_urb(read bulk) failed with result = %d\n", __func__, result);
616 edge_serial->read_in_progress = false;
619 spin_unlock(&edge_serial->es_lock);
622 /* grab the txcredits for the ports if available */
623 position = 2;
624 portNumber = 0;
625 while ((position < length) &&
626 (portNumber < edge_serial->serial->num_ports)) {
627 txCredits = data[position] | (data[position+1] << 8);
628 if (txCredits) {
629 port = edge_serial->serial->port[portNumber];
630 edge_port = usb_get_serial_port_data(port);
631 if (edge_port->open) {
632 spin_lock(&edge_port->ep_lock);
633 edge_port->txCredits += txCredits;
634 spin_unlock(&edge_port->ep_lock);
635 dbg("%s - txcredits for port%d = %d",
636 __func__, portNumber,
637 edge_port->txCredits);
639 /* tell the tty driver that something
640 has changed */
641 tty = tty_port_tty_get(
642 &edge_port->port->port);
643 if (tty) {
644 tty_wakeup(tty);
645 tty_kref_put(tty);
647 /* Since we have more credit, check
648 if more data can be sent */
649 send_more_port_data(edge_serial,
650 edge_port);
653 position += 2;
654 ++portNumber;
658 exit:
659 result = usb_submit_urb(urb, GFP_ATOMIC);
660 if (result)
661 dev_err(&urb->dev->dev,
662 "%s - Error %d submitting control urb\n",
663 __func__, result);
667 /*****************************************************************************
668 * edge_bulk_in_callback
669 * this is the callback function for when we have received data on the
670 * bulk in endpoint.
671 *****************************************************************************/
672 static void edge_bulk_in_callback(struct urb *urb)
674 struct edgeport_serial *edge_serial = urb->context;
675 unsigned char *data = urb->transfer_buffer;
676 int retval;
677 __u16 raw_data_length;
678 int status = urb->status;
680 dbg("%s", __func__);
682 if (status) {
683 dbg("%s - nonzero read bulk status received: %d",
684 __func__, status);
685 edge_serial->read_in_progress = false;
686 return;
689 if (urb->actual_length == 0) {
690 dbg("%s - read bulk callback with no data", __func__);
691 edge_serial->read_in_progress = false;
692 return;
695 raw_data_length = urb->actual_length;
697 usb_serial_debug_data(debug, &edge_serial->serial->dev->dev,
698 __func__, raw_data_length, data);
700 spin_lock(&edge_serial->es_lock);
702 /* decrement our rxBytes available by the number that we just got */
703 edge_serial->rxBytesAvail -= raw_data_length;
705 dbg("%s - Received = %d, rxBytesAvail %d", __func__,
706 raw_data_length, edge_serial->rxBytesAvail);
708 process_rcvd_data(edge_serial, data, urb->actual_length);
710 /* check to see if there's any more data for us to read */
711 if (edge_serial->rxBytesAvail > 0) {
712 dbg("%s - posting a read", __func__);
713 retval = usb_submit_urb(edge_serial->read_urb, GFP_ATOMIC);
714 if (retval) {
715 dev_err(&urb->dev->dev,
716 "%s - usb_submit_urb(read bulk) failed, "
717 "retval = %d\n", __func__, retval);
718 edge_serial->read_in_progress = false;
720 } else {
721 edge_serial->read_in_progress = false;
724 spin_unlock(&edge_serial->es_lock);
728 /*****************************************************************************
729 * edge_bulk_out_data_callback
730 * this is the callback function for when we have finished sending
731 * serial data on the bulk out endpoint.
732 *****************************************************************************/
733 static void edge_bulk_out_data_callback(struct urb *urb)
735 struct edgeport_port *edge_port = urb->context;
736 struct tty_struct *tty;
737 int status = urb->status;
739 dbg("%s", __func__);
741 if (status) {
742 dbg("%s - nonzero write bulk status received: %d",
743 __func__, status);
746 tty = tty_port_tty_get(&edge_port->port->port);
748 if (tty && edge_port->open) {
749 /* let the tty driver wakeup if it has a special
750 write_wakeup function */
751 tty_wakeup(tty);
753 tty_kref_put(tty);
755 /* Release the Write URB */
756 edge_port->write_in_progress = false;
758 /* Check if more data needs to be sent */
759 send_more_port_data((struct edgeport_serial *)
760 (usb_get_serial_data(edge_port->port->serial)), edge_port);
764 /*****************************************************************************
765 * BulkOutCmdCallback
766 * this is the callback function for when we have finished sending a
767 * command on the bulk out endpoint.
768 *****************************************************************************/
769 static void edge_bulk_out_cmd_callback(struct urb *urb)
771 struct edgeport_port *edge_port = urb->context;
772 struct tty_struct *tty;
773 int status = urb->status;
775 dbg("%s", __func__);
777 atomic_dec(&CmdUrbs);
778 dbg("%s - FREE URB %p (outstanding %d)", __func__,
779 urb, atomic_read(&CmdUrbs));
782 /* clean up the transfer buffer */
783 kfree(urb->transfer_buffer);
785 /* Free the command urb */
786 usb_free_urb(urb);
788 if (status) {
789 dbg("%s - nonzero write bulk status received: %d",
790 __func__, status);
791 return;
794 /* Get pointer to tty */
795 tty = tty_port_tty_get(&edge_port->port->port);
797 /* tell the tty driver that something has changed */
798 if (tty && edge_port->open)
799 tty_wakeup(tty);
800 tty_kref_put(tty);
802 /* we have completed the command */
803 edge_port->commandPending = false;
804 wake_up(&edge_port->wait_command);
808 /*****************************************************************************
809 * Driver tty interface functions
810 *****************************************************************************/
812 /*****************************************************************************
813 * SerialOpen
814 * this function is called by the tty driver when a port is opened
815 * If successful, we return 0
816 * Otherwise we return a negative error number.
817 *****************************************************************************/
818 static int edge_open(struct tty_struct *tty, struct usb_serial_port *port)
820 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
821 struct usb_serial *serial;
822 struct edgeport_serial *edge_serial;
823 int response;
825 dbg("%s - port %d", __func__, port->number);
827 if (edge_port == NULL)
828 return -ENODEV;
830 /* see if we've set up our endpoint info yet (can't set it up
831 in edge_startup as the structures were not set up at that time.) */
832 serial = port->serial;
833 edge_serial = usb_get_serial_data(serial);
834 if (edge_serial == NULL)
835 return -ENODEV;
836 if (edge_serial->interrupt_in_buffer == NULL) {
837 struct usb_serial_port *port0 = serial->port[0];
839 /* not set up yet, so do it now */
840 edge_serial->interrupt_in_buffer =
841 port0->interrupt_in_buffer;
842 edge_serial->interrupt_in_endpoint =
843 port0->interrupt_in_endpointAddress;
844 edge_serial->interrupt_read_urb = port0->interrupt_in_urb;
845 edge_serial->bulk_in_buffer = port0->bulk_in_buffer;
846 edge_serial->bulk_in_endpoint =
847 port0->bulk_in_endpointAddress;
848 edge_serial->read_urb = port0->read_urb;
849 edge_serial->bulk_out_endpoint =
850 port0->bulk_out_endpointAddress;
852 /* set up our interrupt urb */
853 usb_fill_int_urb(edge_serial->interrupt_read_urb,
854 serial->dev,
855 usb_rcvintpipe(serial->dev,
856 port0->interrupt_in_endpointAddress),
857 port0->interrupt_in_buffer,
858 edge_serial->interrupt_read_urb->transfer_buffer_length,
859 edge_interrupt_callback, edge_serial,
860 edge_serial->interrupt_read_urb->interval);
862 /* set up our bulk in urb */
863 usb_fill_bulk_urb(edge_serial->read_urb, serial->dev,
864 usb_rcvbulkpipe(serial->dev,
865 port0->bulk_in_endpointAddress),
866 port0->bulk_in_buffer,
867 edge_serial->read_urb->transfer_buffer_length,
868 edge_bulk_in_callback, edge_serial);
869 edge_serial->read_in_progress = false;
871 /* start interrupt read for this edgeport
872 * this interrupt will continue as long
873 * as the edgeport is connected */
874 response = usb_submit_urb(edge_serial->interrupt_read_urb,
875 GFP_KERNEL);
876 if (response) {
877 dev_err(&port->dev,
878 "%s - Error %d submitting control urb\n",
879 __func__, response);
883 /* initialize our wait queues */
884 init_waitqueue_head(&edge_port->wait_open);
885 init_waitqueue_head(&edge_port->wait_chase);
886 init_waitqueue_head(&edge_port->delta_msr_wait);
887 init_waitqueue_head(&edge_port->wait_command);
889 /* initialize our icount structure */
890 memset(&(edge_port->icount), 0x00, sizeof(edge_port->icount));
892 /* initialize our port settings */
893 edge_port->txCredits = 0; /* Can't send any data yet */
894 /* Must always set this bit to enable ints! */
895 edge_port->shadowMCR = MCR_MASTER_IE;
896 edge_port->chaseResponsePending = false;
898 /* send a open port command */
899 edge_port->openPending = true;
900 edge_port->open = false;
901 response = send_iosp_ext_cmd(edge_port, IOSP_CMD_OPEN_PORT, 0);
903 if (response < 0) {
904 dev_err(&port->dev, "%s - error sending open port command\n",
905 __func__);
906 edge_port->openPending = false;
907 return -ENODEV;
910 /* now wait for the port to be completely opened */
911 wait_event_timeout(edge_port->wait_open, !edge_port->openPending,
912 OPEN_TIMEOUT);
914 if (!edge_port->open) {
915 /* open timed out */
916 dbg("%s - open timedout", __func__);
917 edge_port->openPending = false;
918 return -ENODEV;
921 /* create the txfifo */
922 edge_port->txfifo.head = 0;
923 edge_port->txfifo.tail = 0;
924 edge_port->txfifo.count = 0;
925 edge_port->txfifo.size = edge_port->maxTxCredits;
926 edge_port->txfifo.fifo = kmalloc(edge_port->maxTxCredits, GFP_KERNEL);
928 if (!edge_port->txfifo.fifo) {
929 dbg("%s - no memory", __func__);
930 edge_close(port);
931 return -ENOMEM;
934 /* Allocate a URB for the write */
935 edge_port->write_urb = usb_alloc_urb(0, GFP_KERNEL);
936 edge_port->write_in_progress = false;
938 if (!edge_port->write_urb) {
939 dbg("%s - no memory", __func__);
940 edge_close(port);
941 return -ENOMEM;
944 dbg("%s(%d) - Initialize TX fifo to %d bytes",
945 __func__, port->number, edge_port->maxTxCredits);
947 dbg("%s exited", __func__);
949 return 0;
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 DEFINE_WAIT(wait);
966 __u16 lastCredits;
967 int timeout = 1*HZ;
968 int loop = 10;
970 while (1) {
971 /* Save Last credits */
972 lastCredits = edge_port->txCredits;
974 /* Did we get our Chase response */
975 if (!edge_port->chaseResponsePending) {
976 dbg("%s - Got Chase Response", __func__);
978 /* did we get all of our credit back? */
979 if (edge_port->txCredits == edge_port->maxTxCredits) {
980 dbg("%s - Got all credits", __func__);
981 return;
985 /* Block the thread for a while */
986 prepare_to_wait(&edge_port->wait_chase, &wait,
987 TASK_UNINTERRUPTIBLE);
988 schedule_timeout(timeout);
989 finish_wait(&edge_port->wait_chase, &wait);
991 if (lastCredits == edge_port->txCredits) {
992 /* No activity.. count down. */
993 loop--;
994 if (loop == 0) {
995 edge_port->chaseResponsePending = false;
996 dbg("%s - Chase TIMEOUT", __func__);
997 return;
999 } else {
1000 /* Reset timeout value back to 10 seconds */
1001 dbg("%s - Last %d, Current %d", __func__,
1002 lastCredits, edge_port->txCredits);
1003 loop = 10;
1009 /************************************************************************
1011 * block_until_tx_empty
1013 * This function will block the close until one of the following:
1014 * 1. TX count are 0
1015 * 2. The edgeport has stopped
1016 * 3. A timeout of 3 seconds without activity has expired
1018 ************************************************************************/
1019 static void block_until_tx_empty(struct edgeport_port *edge_port)
1021 DEFINE_WAIT(wait);
1022 struct TxFifo *fifo = &edge_port->txfifo;
1023 __u32 lastCount;
1024 int timeout = HZ/10;
1025 int loop = 30;
1027 while (1) {
1028 /* Save Last count */
1029 lastCount = fifo->count;
1031 /* Is the Edgeport Buffer empty? */
1032 if (lastCount == 0) {
1033 dbg("%s - TX Buffer Empty", __func__);
1034 return;
1037 /* Block the thread for a while */
1038 prepare_to_wait(&edge_port->wait_chase, &wait,
1039 TASK_UNINTERRUPTIBLE);
1040 schedule_timeout(timeout);
1041 finish_wait(&edge_port->wait_chase, &wait);
1043 dbg("%s wait", __func__);
1045 if (lastCount == fifo->count) {
1046 /* No activity.. count down. */
1047 loop--;
1048 if (loop == 0) {
1049 dbg("%s - TIMEOUT", __func__);
1050 return;
1052 } else {
1053 /* Reset timeout value back to seconds */
1054 loop = 30;
1060 /*****************************************************************************
1061 * edge_close
1062 * this function is called by the tty driver when a port is closed
1063 *****************************************************************************/
1064 static void edge_close(struct usb_serial_port *port)
1066 struct edgeport_serial *edge_serial;
1067 struct edgeport_port *edge_port;
1068 int status;
1070 dbg("%s - port %d", __func__, port->number);
1072 edge_serial = usb_get_serial_data(port->serial);
1073 edge_port = usb_get_serial_port_data(port);
1074 if (edge_serial == NULL || edge_port == NULL)
1075 return;
1077 /* block until tx is empty */
1078 block_until_tx_empty(edge_port);
1080 edge_port->closePending = true;
1082 if ((!edge_serial->is_epic) ||
1083 ((edge_serial->is_epic) &&
1084 (edge_serial->epic_descriptor.Supports.IOSPChase))) {
1085 /* flush and chase */
1086 edge_port->chaseResponsePending = true;
1088 dbg("%s - Sending IOSP_CMD_CHASE_PORT", __func__);
1089 status = send_iosp_ext_cmd(edge_port, IOSP_CMD_CHASE_PORT, 0);
1090 if (status == 0)
1091 /* block until chase finished */
1092 block_until_chase_response(edge_port);
1093 else
1094 edge_port->chaseResponsePending = false;
1097 if ((!edge_serial->is_epic) ||
1098 ((edge_serial->is_epic) &&
1099 (edge_serial->epic_descriptor.Supports.IOSPClose))) {
1100 /* close the port */
1101 dbg("%s - Sending IOSP_CMD_CLOSE_PORT", __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;
1122 dbg("%s exited", __func__);
1125 /*****************************************************************************
1126 * SerialWrite
1127 * this function is called by the tty driver when data should be written
1128 * to the port.
1129 * If successful, we return the number of bytes written, otherwise we
1130 * return a negative error number.
1131 *****************************************************************************/
1132 static int edge_write(struct tty_struct *tty, struct usb_serial_port *port,
1133 const unsigned char *data, int count)
1135 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1136 struct TxFifo *fifo;
1137 int copySize;
1138 int bytesleft;
1139 int firsthalf;
1140 int secondhalf;
1141 unsigned long flags;
1143 dbg("%s - port %d", __func__, port->number);
1145 if (edge_port == NULL)
1146 return -ENODEV;
1148 /* get a pointer to the Tx fifo */
1149 fifo = &edge_port->txfifo;
1151 spin_lock_irqsave(&edge_port->ep_lock, flags);
1153 /* calculate number of bytes to put in fifo */
1154 copySize = min((unsigned int)count,
1155 (edge_port->txCredits - fifo->count));
1157 dbg("%s(%d) of %d byte(s) Fifo room %d -- will copy %d bytes",
1158 __func__, port->number, count,
1159 edge_port->txCredits - fifo->count, copySize);
1161 /* catch writes of 0 bytes which the tty driver likes to give us,
1162 and when txCredits is empty */
1163 if (copySize == 0) {
1164 dbg("%s - copySize = Zero", __func__);
1165 goto finish_write;
1168 /* queue the data
1169 * since we can never overflow the buffer we do not have to check for a
1170 * full condition
1172 * the copy is done is two parts -- first fill to the end of the buffer
1173 * then copy the reset from the start of the buffer
1175 bytesleft = fifo->size - fifo->head;
1176 firsthalf = min(bytesleft, copySize);
1177 dbg("%s - copy %d bytes of %d into fifo ", __func__,
1178 firsthalf, bytesleft);
1180 /* now copy our data */
1181 memcpy(&fifo->fifo[fifo->head], data, firsthalf);
1182 usb_serial_debug_data(debug, &port->dev, __func__,
1183 firsthalf, &fifo->fifo[fifo->head]);
1185 /* update the index and size */
1186 fifo->head += firsthalf;
1187 fifo->count += firsthalf;
1189 /* wrap the index */
1190 if (fifo->head == fifo->size)
1191 fifo->head = 0;
1193 secondhalf = copySize-firsthalf;
1195 if (secondhalf) {
1196 dbg("%s - copy rest of data %d", __func__, secondhalf);
1197 memcpy(&fifo->fifo[fifo->head], &data[firsthalf], secondhalf);
1198 usb_serial_debug_data(debug, &port->dev, __func__,
1199 secondhalf, &fifo->fifo[fifo->head]);
1200 /* update the index and size */
1201 fifo->count += secondhalf;
1202 fifo->head += secondhalf;
1203 /* No need to check for wrap since we can not get to end of
1204 * the fifo in this part
1208 finish_write:
1209 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1211 send_more_port_data((struct edgeport_serial *)
1212 usb_get_serial_data(port->serial), edge_port);
1214 dbg("%s wrote %d byte(s) TxCredits %d, Fifo %d", __func__,
1215 copySize, edge_port->txCredits, fifo->count);
1217 return copySize;
1221 /************************************************************************
1223 * send_more_port_data()
1225 * This routine attempts to write additional UART transmit data
1226 * to a port over the USB bulk pipe. It is called (1) when new
1227 * data has been written to a port's TxBuffer from higher layers
1228 * (2) when the peripheral sends us additional TxCredits indicating
1229 * that it can accept more Tx data for a given port; and (3) when
1230 * a bulk write completes successfully and we want to see if we
1231 * can transmit more.
1233 ************************************************************************/
1234 static void send_more_port_data(struct edgeport_serial *edge_serial,
1235 struct edgeport_port *edge_port)
1237 struct TxFifo *fifo = &edge_port->txfifo;
1238 struct urb *urb;
1239 unsigned char *buffer;
1240 int status;
1241 int count;
1242 int bytesleft;
1243 int firsthalf;
1244 int secondhalf;
1245 unsigned long flags;
1247 dbg("%s(%d)", __func__, edge_port->port->number);
1249 spin_lock_irqsave(&edge_port->ep_lock, flags);
1251 if (edge_port->write_in_progress ||
1252 !edge_port->open ||
1253 (fifo->count == 0)) {
1254 dbg("%s(%d) EXIT - fifo %d, PendingWrite = %d",
1255 __func__, edge_port->port->number,
1256 fifo->count, edge_port->write_in_progress);
1257 goto exit_send;
1260 /* since the amount of data in the fifo will always fit into the
1261 * edgeport buffer we do not need to check the write length
1263 * Do we have enough credits for this port to make it worthwhile
1264 * to bother queueing a write. If it's too small, say a few bytes,
1265 * it's better to wait for more credits so we can do a larger write.
1267 if (edge_port->txCredits < EDGE_FW_GET_TX_CREDITS_SEND_THRESHOLD(edge_port->maxTxCredits, EDGE_FW_BULK_MAX_PACKET_SIZE)) {
1268 dbg("%s(%d) Not enough credit - fifo %d TxCredit %d",
1269 __func__, edge_port->port->number, fifo->count,
1270 edge_port->txCredits);
1271 goto exit_send;
1274 /* lock this write */
1275 edge_port->write_in_progress = true;
1277 /* get a pointer to the write_urb */
1278 urb = edge_port->write_urb;
1280 /* make sure transfer buffer is freed */
1281 kfree(urb->transfer_buffer);
1282 urb->transfer_buffer = NULL;
1284 /* build the data header for the buffer and port that we are about
1285 to send out */
1286 count = fifo->count;
1287 buffer = kmalloc(count+2, GFP_ATOMIC);
1288 if (buffer == NULL) {
1289 dev_err(&edge_port->port->dev,
1290 "%s - no more kernel memory...\n", __func__);
1291 edge_port->write_in_progress = false;
1292 goto exit_send;
1294 buffer[0] = IOSP_BUILD_DATA_HDR1(edge_port->port->number
1295 - edge_port->port->serial->minor, count);
1296 buffer[1] = IOSP_BUILD_DATA_HDR2(edge_port->port->number
1297 - edge_port->port->serial->minor, count);
1299 /* now copy our data */
1300 bytesleft = fifo->size - fifo->tail;
1301 firsthalf = min(bytesleft, count);
1302 memcpy(&buffer[2], &fifo->fifo[fifo->tail], firsthalf);
1303 fifo->tail += firsthalf;
1304 fifo->count -= firsthalf;
1305 if (fifo->tail == fifo->size)
1306 fifo->tail = 0;
1308 secondhalf = count-firsthalf;
1309 if (secondhalf) {
1310 memcpy(&buffer[2+firsthalf], &fifo->fifo[fifo->tail],
1311 secondhalf);
1312 fifo->tail += secondhalf;
1313 fifo->count -= secondhalf;
1316 if (count)
1317 usb_serial_debug_data(debug, &edge_port->port->dev,
1318 __func__, count, &buffer[2]);
1320 /* fill up the urb with all of our data and submit it */
1321 usb_fill_bulk_urb(urb, edge_serial->serial->dev,
1322 usb_sndbulkpipe(edge_serial->serial->dev,
1323 edge_serial->bulk_out_endpoint),
1324 buffer, count+2,
1325 edge_bulk_out_data_callback, edge_port);
1327 /* decrement the number of credits we have by the number we just sent */
1328 edge_port->txCredits -= count;
1329 edge_port->icount.tx += count;
1331 status = usb_submit_urb(urb, GFP_ATOMIC);
1332 if (status) {
1333 /* something went wrong */
1334 dev_err(&edge_port->port->dev,
1335 "%s - usb_submit_urb(write bulk) failed, status = %d, data lost\n",
1336 __func__, status);
1337 edge_port->write_in_progress = false;
1339 /* revert the credits as something bad happened. */
1340 edge_port->txCredits += count;
1341 edge_port->icount.tx -= count;
1343 dbg("%s wrote %d byte(s) TxCredit %d, Fifo %d",
1344 __func__, count, edge_port->txCredits, fifo->count);
1346 exit_send:
1347 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1351 /*****************************************************************************
1352 * edge_write_room
1353 * this function is called by the tty driver when it wants to know how
1354 * many bytes of data we can accept for a specific port. If successful,
1355 * we return the amount of room that we have for this port (the txCredits)
1356 * otherwise we return a negative error number.
1357 *****************************************************************************/
1358 static int edge_write_room(struct tty_struct *tty)
1360 struct usb_serial_port *port = tty->driver_data;
1361 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1362 int room;
1363 unsigned long flags;
1365 dbg("%s", __func__);
1367 if (edge_port == NULL)
1368 return 0;
1369 if (edge_port->closePending)
1370 return 0;
1372 dbg("%s - port %d", __func__, port->number);
1374 if (!edge_port->open) {
1375 dbg("%s - port not opened", __func__);
1376 return 0;
1379 /* total of both buffers is still txCredit */
1380 spin_lock_irqsave(&edge_port->ep_lock, flags);
1381 room = edge_port->txCredits - edge_port->txfifo.count;
1382 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1384 dbg("%s - returns %d", __func__, room);
1385 return room;
1389 /*****************************************************************************
1390 * edge_chars_in_buffer
1391 * this function is called by the tty driver when it wants to know how
1392 * many bytes of data we currently have outstanding in the port (data that
1393 * has been written, but hasn't made it out the port yet)
1394 * If successful, we return the number of bytes left to be written in the
1395 * system,
1396 * Otherwise we return a negative error number.
1397 *****************************************************************************/
1398 static int edge_chars_in_buffer(struct tty_struct *tty)
1400 struct usb_serial_port *port = tty->driver_data;
1401 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1402 int num_chars;
1403 unsigned long flags;
1405 dbg("%s", __func__);
1407 if (edge_port == NULL)
1408 return 0;
1409 if (edge_port->closePending)
1410 return 0;
1412 if (!edge_port->open) {
1413 dbg("%s - port not opened", __func__);
1414 return 0;
1417 spin_lock_irqsave(&edge_port->ep_lock, flags);
1418 num_chars = edge_port->maxTxCredits - edge_port->txCredits +
1419 edge_port->txfifo.count;
1420 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1421 if (num_chars) {
1422 dbg("%s(port %d) - returns %d", __func__,
1423 port->number, num_chars);
1426 return num_chars;
1430 /*****************************************************************************
1431 * SerialThrottle
1432 * this function is called by the tty driver when it wants to stop the data
1433 * being read from the port.
1434 *****************************************************************************/
1435 static void edge_throttle(struct tty_struct *tty)
1437 struct usb_serial_port *port = tty->driver_data;
1438 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1439 int status;
1441 dbg("%s - port %d", __func__, port->number);
1443 if (edge_port == NULL)
1444 return;
1446 if (!edge_port->open) {
1447 dbg("%s - port not opened", __func__);
1448 return;
1451 /* if we are implementing XON/XOFF, send the stop character */
1452 if (I_IXOFF(tty)) {
1453 unsigned char stop_char = STOP_CHAR(tty);
1454 status = edge_write(tty, port, &stop_char, 1);
1455 if (status <= 0)
1456 return;
1459 /* if we are implementing RTS/CTS, toggle that line */
1460 if (tty->termios->c_cflag & CRTSCTS) {
1461 edge_port->shadowMCR &= ~MCR_RTS;
1462 status = send_cmd_write_uart_register(edge_port, MCR,
1463 edge_port->shadowMCR);
1464 if (status != 0)
1465 return;
1470 /*****************************************************************************
1471 * edge_unthrottle
1472 * this function is called by the tty driver when it wants to resume the
1473 * data being read from the port (called after SerialThrottle is called)
1474 *****************************************************************************/
1475 static void edge_unthrottle(struct tty_struct *tty)
1477 struct usb_serial_port *port = tty->driver_data;
1478 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1479 int status;
1481 dbg("%s - port %d", __func__, port->number);
1483 if (edge_port == NULL)
1484 return;
1486 if (!edge_port->open) {
1487 dbg("%s - port not opened", __func__);
1488 return;
1491 /* if we are implementing XON/XOFF, send the start character */
1492 if (I_IXOFF(tty)) {
1493 unsigned char start_char = START_CHAR(tty);
1494 status = edge_write(tty, port, &start_char, 1);
1495 if (status <= 0)
1496 return;
1498 /* if we are implementing RTS/CTS, toggle that line */
1499 if (tty->termios->c_cflag & CRTSCTS) {
1500 edge_port->shadowMCR |= MCR_RTS;
1501 send_cmd_write_uart_register(edge_port, MCR,
1502 edge_port->shadowMCR);
1507 /*****************************************************************************
1508 * SerialSetTermios
1509 * this function is called by the tty driver when it wants to change
1510 * the termios structure
1511 *****************************************************************************/
1512 static void edge_set_termios(struct tty_struct *tty,
1513 struct usb_serial_port *port, struct ktermios *old_termios)
1515 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1516 unsigned int cflag;
1518 cflag = tty->termios->c_cflag;
1519 dbg("%s - clfag %08x iflag %08x", __func__,
1520 tty->termios->c_cflag, tty->termios->c_iflag);
1521 dbg("%s - old clfag %08x old iflag %08x", __func__,
1522 old_termios->c_cflag, old_termios->c_iflag);
1524 dbg("%s - port %d", __func__, port->number);
1526 if (edge_port == NULL)
1527 return;
1529 if (!edge_port->open) {
1530 dbg("%s - port not opened", __func__);
1531 return;
1534 /* change the port settings to the new ones specified */
1535 change_port_settings(tty, edge_port, old_termios);
1539 /*****************************************************************************
1540 * get_lsr_info - get line status register info
1542 * Purpose: Let user call ioctl() to get info when the UART physically
1543 * is emptied. On bus types like RS485, the transmitter must
1544 * release the bus after transmitting. This must be done when
1545 * the transmit shift register is empty, not be done when the
1546 * transmit holding register is empty. This functionality
1547 * allows an RS485 driver to be written in user space.
1548 *****************************************************************************/
1549 static int get_lsr_info(struct edgeport_port *edge_port,
1550 unsigned int __user *value)
1552 unsigned int result = 0;
1553 unsigned long flags;
1555 spin_lock_irqsave(&edge_port->ep_lock, flags);
1556 if (edge_port->maxTxCredits == edge_port->txCredits &&
1557 edge_port->txfifo.count == 0) {
1558 dbg("%s -- Empty", __func__);
1559 result = TIOCSER_TEMT;
1561 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1563 if (copy_to_user(value, &result, sizeof(int)))
1564 return -EFAULT;
1565 return 0;
1568 static int edge_tiocmset(struct tty_struct *tty,
1569 unsigned int set, unsigned int clear)
1571 struct usb_serial_port *port = tty->driver_data;
1572 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1573 unsigned int mcr;
1575 dbg("%s - port %d", __func__, port->number);
1577 mcr = edge_port->shadowMCR;
1578 if (set & TIOCM_RTS)
1579 mcr |= MCR_RTS;
1580 if (set & TIOCM_DTR)
1581 mcr |= MCR_DTR;
1582 if (set & TIOCM_LOOP)
1583 mcr |= MCR_LOOPBACK;
1585 if (clear & TIOCM_RTS)
1586 mcr &= ~MCR_RTS;
1587 if (clear & TIOCM_DTR)
1588 mcr &= ~MCR_DTR;
1589 if (clear & TIOCM_LOOP)
1590 mcr &= ~MCR_LOOPBACK;
1592 edge_port->shadowMCR = mcr;
1594 send_cmd_write_uart_register(edge_port, MCR, edge_port->shadowMCR);
1596 return 0;
1599 static int edge_tiocmget(struct tty_struct *tty)
1601 struct usb_serial_port *port = tty->driver_data;
1602 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1603 unsigned int result = 0;
1604 unsigned int msr;
1605 unsigned int mcr;
1607 dbg("%s - port %d", __func__, port->number);
1609 msr = edge_port->shadowMSR;
1610 mcr = edge_port->shadowMCR;
1611 result = ((mcr & MCR_DTR) ? TIOCM_DTR: 0) /* 0x002 */
1612 | ((mcr & MCR_RTS) ? TIOCM_RTS: 0) /* 0x004 */
1613 | ((msr & EDGEPORT_MSR_CTS) ? TIOCM_CTS: 0) /* 0x020 */
1614 | ((msr & EDGEPORT_MSR_CD) ? TIOCM_CAR: 0) /* 0x040 */
1615 | ((msr & EDGEPORT_MSR_RI) ? TIOCM_RI: 0) /* 0x080 */
1616 | ((msr & EDGEPORT_MSR_DSR) ? TIOCM_DSR: 0); /* 0x100 */
1619 dbg("%s -- %x", __func__, result);
1621 return result;
1624 static int edge_get_icount(struct tty_struct *tty,
1625 struct serial_icounter_struct *icount)
1627 struct usb_serial_port *port = tty->driver_data;
1628 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1629 struct async_icount cnow;
1630 cnow = edge_port->icount;
1632 icount->cts = cnow.cts;
1633 icount->dsr = cnow.dsr;
1634 icount->rng = cnow.rng;
1635 icount->dcd = cnow.dcd;
1636 icount->rx = cnow.rx;
1637 icount->tx = cnow.tx;
1638 icount->frame = cnow.frame;
1639 icount->overrun = cnow.overrun;
1640 icount->parity = cnow.parity;
1641 icount->brk = cnow.brk;
1642 icount->buf_overrun = cnow.buf_overrun;
1644 dbg("%s (%d) TIOCGICOUNT RX=%d, TX=%d",
1645 __func__, port->number, icount->rx, icount->tx);
1646 return 0;
1649 static int get_serial_info(struct edgeport_port *edge_port,
1650 struct serial_struct __user *retinfo)
1652 struct serial_struct tmp;
1654 if (!retinfo)
1655 return -EFAULT;
1657 memset(&tmp, 0, sizeof(tmp));
1659 tmp.type = PORT_16550A;
1660 tmp.line = edge_port->port->serial->minor;
1661 tmp.port = edge_port->port->number;
1662 tmp.irq = 0;
1663 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
1664 tmp.xmit_fifo_size = edge_port->maxTxCredits;
1665 tmp.baud_base = 9600;
1666 tmp.close_delay = 5*HZ;
1667 tmp.closing_wait = 30*HZ;
1669 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1670 return -EFAULT;
1671 return 0;
1675 /*****************************************************************************
1676 * SerialIoctl
1677 * this function handles any ioctl calls to the driver
1678 *****************************************************************************/
1679 static int edge_ioctl(struct tty_struct *tty,
1680 unsigned int cmd, unsigned long arg)
1682 struct usb_serial_port *port = tty->driver_data;
1683 DEFINE_WAIT(wait);
1684 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1685 struct async_icount cnow;
1686 struct async_icount cprev;
1688 dbg("%s - port %d, cmd = 0x%x", __func__, port->number, cmd);
1690 switch (cmd) {
1691 case TIOCSERGETLSR:
1692 dbg("%s (%d) TIOCSERGETLSR", __func__, port->number);
1693 return get_lsr_info(edge_port, (unsigned int __user *) arg);
1695 case TIOCGSERIAL:
1696 dbg("%s (%d) TIOCGSERIAL", __func__, port->number);
1697 return get_serial_info(edge_port, (struct serial_struct __user *) arg);
1699 case TIOCMIWAIT:
1700 dbg("%s (%d) TIOCMIWAIT", __func__, port->number);
1701 cprev = edge_port->icount;
1702 while (1) {
1703 prepare_to_wait(&edge_port->delta_msr_wait,
1704 &wait, TASK_INTERRUPTIBLE);
1705 schedule();
1706 finish_wait(&edge_port->delta_msr_wait, &wait);
1707 /* see if a signal did it */
1708 if (signal_pending(current))
1709 return -ERESTARTSYS;
1710 cnow = edge_port->icount;
1711 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
1712 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
1713 return -EIO; /* no change => error */
1714 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1715 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1716 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1717 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
1718 return 0;
1720 cprev = cnow;
1722 /* NOTREACHED */
1723 break;
1726 return -ENOIOCTLCMD;
1730 /*****************************************************************************
1731 * SerialBreak
1732 * this function sends a break to the port
1733 *****************************************************************************/
1734 static void edge_break(struct tty_struct *tty, int break_state)
1736 struct usb_serial_port *port = tty->driver_data;
1737 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1738 struct edgeport_serial *edge_serial = usb_get_serial_data(port->serial);
1739 int status;
1741 if ((!edge_serial->is_epic) ||
1742 ((edge_serial->is_epic) &&
1743 (edge_serial->epic_descriptor.Supports.IOSPChase))) {
1744 /* flush and chase */
1745 edge_port->chaseResponsePending = true;
1747 dbg("%s - Sending IOSP_CMD_CHASE_PORT", __func__);
1748 status = send_iosp_ext_cmd(edge_port, IOSP_CMD_CHASE_PORT, 0);
1749 if (status == 0) {
1750 /* block until chase finished */
1751 block_until_chase_response(edge_port);
1752 } else {
1753 edge_port->chaseResponsePending = false;
1757 if ((!edge_serial->is_epic) ||
1758 ((edge_serial->is_epic) &&
1759 (edge_serial->epic_descriptor.Supports.IOSPSetClrBreak))) {
1760 if (break_state == -1) {
1761 dbg("%s - Sending IOSP_CMD_SET_BREAK", __func__);
1762 status = send_iosp_ext_cmd(edge_port,
1763 IOSP_CMD_SET_BREAK, 0);
1764 } else {
1765 dbg("%s - Sending IOSP_CMD_CLEAR_BREAK", __func__);
1766 status = send_iosp_ext_cmd(edge_port,
1767 IOSP_CMD_CLEAR_BREAK, 0);
1769 if (status)
1770 dbg("%s - error sending break set/clear command.",
1771 __func__);
1776 /*****************************************************************************
1777 * process_rcvd_data
1778 * this function handles the data received on the bulk in pipe.
1779 *****************************************************************************/
1780 static void process_rcvd_data(struct edgeport_serial *edge_serial,
1781 unsigned char *buffer, __u16 bufferLength)
1783 struct usb_serial_port *port;
1784 struct edgeport_port *edge_port;
1785 struct tty_struct *tty;
1786 __u16 lastBufferLength;
1787 __u16 rxLen;
1789 dbg("%s", __func__);
1791 lastBufferLength = bufferLength + 1;
1793 while (bufferLength > 0) {
1794 /* failsafe incase we get a message that we don't understand */
1795 if (lastBufferLength == bufferLength) {
1796 dbg("%s - stuck in loop, exiting it.", __func__);
1797 break;
1799 lastBufferLength = bufferLength;
1801 switch (edge_serial->rxState) {
1802 case EXPECT_HDR1:
1803 edge_serial->rxHeader1 = *buffer;
1804 ++buffer;
1805 --bufferLength;
1807 if (bufferLength == 0) {
1808 edge_serial->rxState = EXPECT_HDR2;
1809 break;
1811 /* otherwise, drop on through */
1812 case EXPECT_HDR2:
1813 edge_serial->rxHeader2 = *buffer;
1814 ++buffer;
1815 --bufferLength;
1817 dbg("%s - Hdr1=%02X Hdr2=%02X", __func__,
1818 edge_serial->rxHeader1, edge_serial->rxHeader2);
1819 /* Process depending on whether this header is
1820 * data or status */
1822 if (IS_CMD_STAT_HDR(edge_serial->rxHeader1)) {
1823 /* Decode this status header and go to
1824 * EXPECT_HDR1 (if we can process the status
1825 * with only 2 bytes), or go to EXPECT_HDR3 to
1826 * get the third byte. */
1827 edge_serial->rxPort =
1828 IOSP_GET_HDR_PORT(edge_serial->rxHeader1);
1829 edge_serial->rxStatusCode =
1830 IOSP_GET_STATUS_CODE(
1831 edge_serial->rxHeader1);
1833 if (!IOSP_STATUS_IS_2BYTE(
1834 edge_serial->rxStatusCode)) {
1835 /* This status needs additional bytes.
1836 * Save what we have and then wait for
1837 * more data.
1839 edge_serial->rxStatusParam
1840 = edge_serial->rxHeader2;
1841 edge_serial->rxState = EXPECT_HDR3;
1842 break;
1844 /* We have all the header bytes, process the
1845 status now */
1846 process_rcvd_status(edge_serial,
1847 edge_serial->rxHeader2, 0);
1848 edge_serial->rxState = EXPECT_HDR1;
1849 break;
1850 } else {
1851 edge_serial->rxPort =
1852 IOSP_GET_HDR_PORT(edge_serial->rxHeader1);
1853 edge_serial->rxBytesRemaining =
1854 IOSP_GET_HDR_DATA_LEN(
1855 edge_serial->rxHeader1,
1856 edge_serial->rxHeader2);
1857 dbg("%s - Data for Port %u Len %u",
1858 __func__,
1859 edge_serial->rxPort,
1860 edge_serial->rxBytesRemaining);
1862 /* ASSERT(DevExt->RxPort < DevExt->NumPorts);
1863 * ASSERT(DevExt->RxBytesRemaining <
1864 * IOSP_MAX_DATA_LENGTH);
1867 if (bufferLength == 0) {
1868 edge_serial->rxState = EXPECT_DATA;
1869 break;
1871 /* Else, drop through */
1873 case EXPECT_DATA: /* Expect data */
1874 if (bufferLength < edge_serial->rxBytesRemaining) {
1875 rxLen = bufferLength;
1876 /* Expect data to start next buffer */
1877 edge_serial->rxState = EXPECT_DATA;
1878 } else {
1879 /* BufLen >= RxBytesRemaining */
1880 rxLen = edge_serial->rxBytesRemaining;
1881 /* Start another header next time */
1882 edge_serial->rxState = EXPECT_HDR1;
1885 bufferLength -= rxLen;
1886 edge_serial->rxBytesRemaining -= rxLen;
1888 /* spit this data back into the tty driver if this
1889 port is open */
1890 if (rxLen) {
1891 port = edge_serial->serial->port[
1892 edge_serial->rxPort];
1893 edge_port = usb_get_serial_port_data(port);
1894 if (edge_port->open) {
1895 tty = tty_port_tty_get(
1896 &edge_port->port->port);
1897 if (tty) {
1898 dbg("%s - Sending %d bytes to TTY for port %d",
1899 __func__, rxLen, edge_serial->rxPort);
1900 edge_tty_recv(&edge_serial->serial->dev->dev, tty, buffer, rxLen);
1901 tty_kref_put(tty);
1903 edge_port->icount.rx += rxLen;
1905 buffer += rxLen;
1907 break;
1909 case EXPECT_HDR3: /* Expect 3rd byte of status header */
1910 edge_serial->rxHeader3 = *buffer;
1911 ++buffer;
1912 --bufferLength;
1914 /* We have all the header bytes, process the
1915 status now */
1916 process_rcvd_status(edge_serial,
1917 edge_serial->rxStatusParam,
1918 edge_serial->rxHeader3);
1919 edge_serial->rxState = EXPECT_HDR1;
1920 break;
1926 /*****************************************************************************
1927 * process_rcvd_status
1928 * this function handles the any status messages received on the
1929 * bulk in pipe.
1930 *****************************************************************************/
1931 static void process_rcvd_status(struct edgeport_serial *edge_serial,
1932 __u8 byte2, __u8 byte3)
1934 struct usb_serial_port *port;
1935 struct edgeport_port *edge_port;
1936 struct tty_struct *tty;
1937 __u8 code = edge_serial->rxStatusCode;
1939 /* switch the port pointer to the one being currently talked about */
1940 port = edge_serial->serial->port[edge_serial->rxPort];
1941 edge_port = usb_get_serial_port_data(port);
1942 if (edge_port == NULL) {
1943 dev_err(&edge_serial->serial->dev->dev,
1944 "%s - edge_port == NULL for port %d\n",
1945 __func__, edge_serial->rxPort);
1946 return;
1949 dbg("%s - port %d", __func__, edge_serial->rxPort);
1951 if (code == IOSP_EXT_STATUS) {
1952 switch (byte2) {
1953 case IOSP_EXT_STATUS_CHASE_RSP:
1954 /* we want to do EXT status regardless of port
1955 * open/closed */
1956 dbg("%s - Port %u EXT CHASE_RSP Data = %02x",
1957 __func__, edge_serial->rxPort, byte3);
1958 /* Currently, the only EXT_STATUS is Chase, so process
1959 * here instead of one more call to one more subroutine
1960 * If/when more EXT_STATUS, there'll be more work to do
1961 * Also, we currently clear flag and close the port
1962 * regardless of content of above's Byte3.
1963 * We could choose to do something else when Byte3 says
1964 * Timeout on Chase from Edgeport, like wait longer in
1965 * block_until_chase_response, but for now we don't.
1967 edge_port->chaseResponsePending = false;
1968 wake_up(&edge_port->wait_chase);
1969 return;
1971 case IOSP_EXT_STATUS_RX_CHECK_RSP:
1972 dbg("%s ========== Port %u CHECK_RSP Sequence = %02x =============", __func__, edge_serial->rxPort, byte3);
1973 /* Port->RxCheckRsp = true; */
1974 return;
1978 if (code == IOSP_STATUS_OPEN_RSP) {
1979 edge_port->txCredits = GET_TX_BUFFER_SIZE(byte3);
1980 edge_port->maxTxCredits = edge_port->txCredits;
1981 dbg("%s - Port %u Open Response Initial MSR = %02x TxBufferSize = %d", __func__, edge_serial->rxPort, byte2, edge_port->txCredits);
1982 handle_new_msr(edge_port, byte2);
1984 /* send the current line settings to the port so we are
1985 in sync with any further termios calls */
1986 tty = tty_port_tty_get(&edge_port->port->port);
1987 if (tty) {
1988 change_port_settings(tty,
1989 edge_port, tty->termios);
1990 tty_kref_put(tty);
1993 /* we have completed the open */
1994 edge_port->openPending = false;
1995 edge_port->open = true;
1996 wake_up(&edge_port->wait_open);
1997 return;
2000 /* If port is closed, silently discard all rcvd status. We can
2001 * have cases where buffered status is received AFTER the close
2002 * port command is sent to the Edgeport.
2004 if (!edge_port->open || edge_port->closePending)
2005 return;
2007 switch (code) {
2008 /* Not currently sent by Edgeport */
2009 case IOSP_STATUS_LSR:
2010 dbg("%s - Port %u LSR Status = %02x",
2011 __func__, edge_serial->rxPort, byte2);
2012 handle_new_lsr(edge_port, false, byte2, 0);
2013 break;
2015 case IOSP_STATUS_LSR_DATA:
2016 dbg("%s - Port %u LSR Status = %02x, Data = %02x",
2017 __func__, edge_serial->rxPort, byte2, byte3);
2018 /* byte2 is LSR Register */
2019 /* byte3 is broken data byte */
2020 handle_new_lsr(edge_port, true, byte2, byte3);
2021 break;
2023 * case IOSP_EXT_4_STATUS:
2024 * dbg("%s - Port %u LSR Status = %02x Data = %02x",
2025 * __func__, edge_serial->rxPort, byte2, byte3);
2026 * break;
2028 case IOSP_STATUS_MSR:
2029 dbg("%s - Port %u MSR Status = %02x",
2030 __func__, edge_serial->rxPort, byte2);
2032 * Process this new modem status and generate appropriate
2033 * events, etc, based on the new status. This routine
2034 * also saves the MSR in Port->ShadowMsr.
2036 handle_new_msr(edge_port, byte2);
2037 break;
2039 default:
2040 dbg("%s - Unrecognized IOSP status code %u", __func__, code);
2041 break;
2046 /*****************************************************************************
2047 * edge_tty_recv
2048 * this function passes data on to the tty flip buffer
2049 *****************************************************************************/
2050 static void edge_tty_recv(struct device *dev, struct tty_struct *tty,
2051 unsigned char *data, int length)
2053 int cnt;
2055 cnt = tty_insert_flip_string(tty, data, length);
2056 if (cnt < length) {
2057 dev_err(dev, "%s - dropping data, %d bytes lost\n",
2058 __func__, length - cnt);
2060 data += cnt;
2061 length -= cnt;
2063 tty_flip_buffer_push(tty);
2067 /*****************************************************************************
2068 * handle_new_msr
2069 * this function handles any change to the msr register for a port.
2070 *****************************************************************************/
2071 static void handle_new_msr(struct edgeport_port *edge_port, __u8 newMsr)
2073 struct async_icount *icount;
2075 dbg("%s %02x", __func__, newMsr);
2077 if (newMsr & (EDGEPORT_MSR_DELTA_CTS | EDGEPORT_MSR_DELTA_DSR |
2078 EDGEPORT_MSR_DELTA_RI | EDGEPORT_MSR_DELTA_CD)) {
2079 icount = &edge_port->icount;
2081 /* update input line counters */
2082 if (newMsr & EDGEPORT_MSR_DELTA_CTS)
2083 icount->cts++;
2084 if (newMsr & EDGEPORT_MSR_DELTA_DSR)
2085 icount->dsr++;
2086 if (newMsr & EDGEPORT_MSR_DELTA_CD)
2087 icount->dcd++;
2088 if (newMsr & EDGEPORT_MSR_DELTA_RI)
2089 icount->rng++;
2090 wake_up_interruptible(&edge_port->delta_msr_wait);
2093 /* Save the new modem status */
2094 edge_port->shadowMSR = newMsr & 0xf0;
2098 /*****************************************************************************
2099 * handle_new_lsr
2100 * this function handles any change to the lsr register for a port.
2101 *****************************************************************************/
2102 static void handle_new_lsr(struct edgeport_port *edge_port, __u8 lsrData,
2103 __u8 lsr, __u8 data)
2105 __u8 newLsr = (__u8) (lsr & (__u8)
2106 (LSR_OVER_ERR | LSR_PAR_ERR | LSR_FRM_ERR | LSR_BREAK));
2107 struct async_icount *icount;
2109 dbg("%s - %02x", __func__, newLsr);
2111 edge_port->shadowLSR = lsr;
2113 if (newLsr & LSR_BREAK) {
2115 * Parity and Framing errors only count if they
2116 * occur exclusive of a break being
2117 * received.
2119 newLsr &= (__u8)(LSR_OVER_ERR | LSR_BREAK);
2122 /* Place LSR data byte into Rx buffer */
2123 if (lsrData) {
2124 struct tty_struct *tty =
2125 tty_port_tty_get(&edge_port->port->port);
2126 if (tty) {
2127 edge_tty_recv(&edge_port->port->dev, tty, &data, 1);
2128 tty_kref_put(tty);
2131 /* update input line counters */
2132 icount = &edge_port->icount;
2133 if (newLsr & LSR_BREAK)
2134 icount->brk++;
2135 if (newLsr & LSR_OVER_ERR)
2136 icount->overrun++;
2137 if (newLsr & LSR_PAR_ERR)
2138 icount->parity++;
2139 if (newLsr & LSR_FRM_ERR)
2140 icount->frame++;
2144 /****************************************************************************
2145 * sram_write
2146 * writes a number of bytes to the Edgeport device's sram starting at the
2147 * given address.
2148 * If successful returns the number of bytes written, otherwise it returns
2149 * a negative error number of the problem.
2150 ****************************************************************************/
2151 static int sram_write(struct usb_serial *serial, __u16 extAddr, __u16 addr,
2152 __u16 length, const __u8 *data)
2154 int result;
2155 __u16 current_length;
2156 unsigned char *transfer_buffer;
2158 dbg("%s - %x, %x, %d", __func__, extAddr, addr, length);
2160 transfer_buffer = kmalloc(64, GFP_KERNEL);
2161 if (!transfer_buffer) {
2162 dev_err(&serial->dev->dev, "%s - kmalloc(%d) failed.\n",
2163 __func__, 64);
2164 return -ENOMEM;
2167 /* need to split these writes up into 64 byte chunks */
2168 result = 0;
2169 while (length > 0) {
2170 if (length > 64)
2171 current_length = 64;
2172 else
2173 current_length = length;
2175 /* dbg("%s - writing %x, %x, %d", __func__,
2176 extAddr, addr, current_length); */
2177 memcpy(transfer_buffer, data, current_length);
2178 result = usb_control_msg(serial->dev,
2179 usb_sndctrlpipe(serial->dev, 0),
2180 USB_REQUEST_ION_WRITE_RAM,
2181 0x40, addr, extAddr, transfer_buffer,
2182 current_length, 300);
2183 if (result < 0)
2184 break;
2185 length -= current_length;
2186 addr += current_length;
2187 data += current_length;
2190 kfree(transfer_buffer);
2191 return result;
2195 /****************************************************************************
2196 * rom_write
2197 * writes a number of bytes to the Edgeport device's ROM starting at the
2198 * given address.
2199 * If successful returns the number of bytes written, otherwise it returns
2200 * a negative error number of the problem.
2201 ****************************************************************************/
2202 static int rom_write(struct usb_serial *serial, __u16 extAddr, __u16 addr,
2203 __u16 length, const __u8 *data)
2205 int result;
2206 __u16 current_length;
2207 unsigned char *transfer_buffer;
2209 /* dbg("%s - %x, %x, %d", __func__, extAddr, addr, length); */
2211 transfer_buffer = kmalloc(64, GFP_KERNEL);
2212 if (!transfer_buffer) {
2213 dev_err(&serial->dev->dev, "%s - kmalloc(%d) failed.\n",
2214 __func__, 64);
2215 return -ENOMEM;
2218 /* need to split these writes up into 64 byte chunks */
2219 result = 0;
2220 while (length > 0) {
2221 if (length > 64)
2222 current_length = 64;
2223 else
2224 current_length = length;
2225 /* dbg("%s - writing %x, %x, %d", __func__,
2226 extAddr, addr, current_length); */
2227 memcpy(transfer_buffer, data, current_length);
2228 result = usb_control_msg(serial->dev,
2229 usb_sndctrlpipe(serial->dev, 0),
2230 USB_REQUEST_ION_WRITE_ROM, 0x40,
2231 addr, extAddr,
2232 transfer_buffer, current_length, 300);
2233 if (result < 0)
2234 break;
2235 length -= current_length;
2236 addr += current_length;
2237 data += current_length;
2240 kfree(transfer_buffer);
2241 return result;
2245 /****************************************************************************
2246 * rom_read
2247 * reads a number of bytes from the Edgeport device starting at the given
2248 * address.
2249 * If successful returns the number of bytes read, otherwise it returns
2250 * a negative error number of the problem.
2251 ****************************************************************************/
2252 static int rom_read(struct usb_serial *serial, __u16 extAddr,
2253 __u16 addr, __u16 length, __u8 *data)
2255 int result;
2256 __u16 current_length;
2257 unsigned char *transfer_buffer;
2259 dbg("%s - %x, %x, %d", __func__, extAddr, addr, length);
2261 transfer_buffer = kmalloc(64, GFP_KERNEL);
2262 if (!transfer_buffer) {
2263 dev_err(&serial->dev->dev,
2264 "%s - kmalloc(%d) failed.\n", __func__, 64);
2265 return -ENOMEM;
2268 /* need to split these reads up into 64 byte chunks */
2269 result = 0;
2270 while (length > 0) {
2271 if (length > 64)
2272 current_length = 64;
2273 else
2274 current_length = length;
2275 /* dbg("%s - %x, %x, %d", __func__,
2276 extAddr, addr, current_length); */
2277 result = usb_control_msg(serial->dev,
2278 usb_rcvctrlpipe(serial->dev, 0),
2279 USB_REQUEST_ION_READ_ROM,
2280 0xC0, addr, extAddr, transfer_buffer,
2281 current_length, 300);
2282 if (result < 0)
2283 break;
2284 memcpy(data, transfer_buffer, current_length);
2285 length -= current_length;
2286 addr += current_length;
2287 data += current_length;
2290 kfree(transfer_buffer);
2291 return result;
2295 /****************************************************************************
2296 * send_iosp_ext_cmd
2297 * Is used to send a IOSP message to the Edgeport device
2298 ****************************************************************************/
2299 static int send_iosp_ext_cmd(struct edgeport_port *edge_port,
2300 __u8 command, __u8 param)
2302 unsigned char *buffer;
2303 unsigned char *currentCommand;
2304 int length = 0;
2305 int status = 0;
2307 dbg("%s - %d, %d", __func__, command, param);
2309 buffer = kmalloc(10, GFP_ATOMIC);
2310 if (!buffer) {
2311 dev_err(&edge_port->port->dev,
2312 "%s - kmalloc(%d) failed.\n", __func__, 10);
2313 return -ENOMEM;
2316 currentCommand = buffer;
2318 MAKE_CMD_EXT_CMD(&currentCommand, &length,
2319 edge_port->port->number - edge_port->port->serial->minor,
2320 command, param);
2322 status = write_cmd_usb(edge_port, buffer, length);
2323 if (status) {
2324 /* something bad happened, let's free up the memory */
2325 kfree(buffer);
2328 return status;
2332 /*****************************************************************************
2333 * write_cmd_usb
2334 * this function writes the given buffer out to the bulk write endpoint.
2335 *****************************************************************************/
2336 static int write_cmd_usb(struct edgeport_port *edge_port,
2337 unsigned char *buffer, int length)
2339 struct edgeport_serial *edge_serial =
2340 usb_get_serial_data(edge_port->port->serial);
2341 int status = 0;
2342 struct urb *urb;
2344 usb_serial_debug_data(debug, &edge_port->port->dev,
2345 __func__, length, buffer);
2347 /* Allocate our next urb */
2348 urb = usb_alloc_urb(0, GFP_ATOMIC);
2349 if (!urb)
2350 return -ENOMEM;
2352 atomic_inc(&CmdUrbs);
2353 dbg("%s - ALLOCATE URB %p (outstanding %d)",
2354 __func__, urb, atomic_read(&CmdUrbs));
2356 usb_fill_bulk_urb(urb, edge_serial->serial->dev,
2357 usb_sndbulkpipe(edge_serial->serial->dev,
2358 edge_serial->bulk_out_endpoint),
2359 buffer, length, edge_bulk_out_cmd_callback, edge_port);
2361 edge_port->commandPending = true;
2362 status = usb_submit_urb(urb, GFP_ATOMIC);
2364 if (status) {
2365 /* something went wrong */
2366 dev_err(&edge_port->port->dev,
2367 "%s - usb_submit_urb(write command) failed, status = %d\n",
2368 __func__, status);
2369 usb_kill_urb(urb);
2370 usb_free_urb(urb);
2371 atomic_dec(&CmdUrbs);
2372 return status;
2375 #if 0
2376 wait_event(&edge_port->wait_command, !edge_port->commandPending);
2378 if (edge_port->commandPending) {
2379 /* command timed out */
2380 dbg("%s - command timed out", __func__);
2381 status = -EINVAL;
2383 #endif
2384 return status;
2388 /*****************************************************************************
2389 * send_cmd_write_baud_rate
2390 * this function sends the proper command to change the baud rate of the
2391 * specified port.
2392 *****************************************************************************/
2393 static int send_cmd_write_baud_rate(struct edgeport_port *edge_port,
2394 int baudRate)
2396 struct edgeport_serial *edge_serial =
2397 usb_get_serial_data(edge_port->port->serial);
2398 unsigned char *cmdBuffer;
2399 unsigned char *currCmd;
2400 int cmdLen = 0;
2401 int divisor;
2402 int status;
2403 unsigned char number =
2404 edge_port->port->number - edge_port->port->serial->minor;
2406 if (edge_serial->is_epic &&
2407 !edge_serial->epic_descriptor.Supports.IOSPSetBaudRate) {
2408 dbg("SendCmdWriteBaudRate - NOT Setting baud rate for port = %d, baud = %d",
2409 edge_port->port->number, baudRate);
2410 return 0;
2413 dbg("%s - port = %d, baud = %d", __func__,
2414 edge_port->port->number, baudRate);
2416 status = calc_baud_rate_divisor(baudRate, &divisor);
2417 if (status) {
2418 dev_err(&edge_port->port->dev, "%s - bad baud rate\n",
2419 __func__);
2420 return status;
2423 /* Alloc memory for the string of commands. */
2424 cmdBuffer = kmalloc(0x100, GFP_ATOMIC);
2425 if (!cmdBuffer) {
2426 dev_err(&edge_port->port->dev,
2427 "%s - kmalloc(%d) failed.\n", __func__, 0x100);
2428 return -ENOMEM;
2430 currCmd = cmdBuffer;
2432 /* Enable access to divisor latch */
2433 MAKE_CMD_WRITE_REG(&currCmd, &cmdLen, number, LCR, LCR_DL_ENABLE);
2435 /* Write the divisor itself */
2436 MAKE_CMD_WRITE_REG(&currCmd, &cmdLen, number, DLL, LOW8(divisor));
2437 MAKE_CMD_WRITE_REG(&currCmd, &cmdLen, number, DLM, HIGH8(divisor));
2439 /* Restore original value to disable access to divisor latch */
2440 MAKE_CMD_WRITE_REG(&currCmd, &cmdLen, number, LCR,
2441 edge_port->shadowLCR);
2443 status = write_cmd_usb(edge_port, cmdBuffer, cmdLen);
2444 if (status) {
2445 /* something bad happened, let's free up the memory */
2446 kfree(cmdBuffer);
2449 return status;
2453 /*****************************************************************************
2454 * calc_baud_rate_divisor
2455 * this function calculates the proper baud rate divisor for the specified
2456 * baud rate.
2457 *****************************************************************************/
2458 static int calc_baud_rate_divisor(int baudrate, int *divisor)
2460 int i;
2461 __u16 custom;
2464 dbg("%s - %d", __func__, baudrate);
2466 for (i = 0; i < ARRAY_SIZE(divisor_table); i++) {
2467 if (divisor_table[i].BaudRate == baudrate) {
2468 *divisor = divisor_table[i].Divisor;
2469 return 0;
2473 /* We have tried all of the standard baud rates
2474 * lets try to calculate the divisor for this baud rate
2475 * Make sure the baud rate is reasonable */
2476 if (baudrate > 50 && baudrate < 230400) {
2477 /* get divisor */
2478 custom = (__u16)((230400L + baudrate/2) / baudrate);
2480 *divisor = custom;
2482 dbg("%s - Baud %d = %d", __func__, baudrate, custom);
2483 return 0;
2486 return -1;
2490 /*****************************************************************************
2491 * send_cmd_write_uart_register
2492 * this function builds up a uart register message and sends to the device.
2493 *****************************************************************************/
2494 static int send_cmd_write_uart_register(struct edgeport_port *edge_port,
2495 __u8 regNum, __u8 regValue)
2497 struct edgeport_serial *edge_serial =
2498 usb_get_serial_data(edge_port->port->serial);
2499 unsigned char *cmdBuffer;
2500 unsigned char *currCmd;
2501 unsigned long cmdLen = 0;
2502 int status;
2504 dbg("%s - write to %s register 0x%02x",
2505 (regNum == MCR) ? "MCR" : "LCR", __func__, regValue);
2507 if (edge_serial->is_epic &&
2508 !edge_serial->epic_descriptor.Supports.IOSPWriteMCR &&
2509 regNum == MCR) {
2510 dbg("SendCmdWriteUartReg - Not writing to MCR Register");
2511 return 0;
2514 if (edge_serial->is_epic &&
2515 !edge_serial->epic_descriptor.Supports.IOSPWriteLCR &&
2516 regNum == LCR) {
2517 dbg("SendCmdWriteUartReg - Not writing to LCR Register");
2518 return 0;
2521 /* Alloc memory for the string of commands. */
2522 cmdBuffer = kmalloc(0x10, GFP_ATOMIC);
2523 if (cmdBuffer == NULL)
2524 return -ENOMEM;
2526 currCmd = cmdBuffer;
2528 /* Build a cmd in the buffer to write the given register */
2529 MAKE_CMD_WRITE_REG(&currCmd, &cmdLen,
2530 edge_port->port->number - edge_port->port->serial->minor,
2531 regNum, regValue);
2533 status = write_cmd_usb(edge_port, cmdBuffer, cmdLen);
2534 if (status) {
2535 /* something bad happened, let's free up the memory */
2536 kfree(cmdBuffer);
2539 return status;
2543 /*****************************************************************************
2544 * change_port_settings
2545 * This routine is called to set the UART on the device to match the
2546 * specified new settings.
2547 *****************************************************************************/
2549 static void change_port_settings(struct tty_struct *tty,
2550 struct edgeport_port *edge_port, struct ktermios *old_termios)
2552 struct edgeport_serial *edge_serial =
2553 usb_get_serial_data(edge_port->port->serial);
2554 int baud;
2555 unsigned cflag;
2556 __u8 mask = 0xff;
2557 __u8 lData;
2558 __u8 lParity;
2559 __u8 lStop;
2560 __u8 rxFlow;
2561 __u8 txFlow;
2562 int status;
2564 dbg("%s - port %d", __func__, edge_port->port->number);
2566 if (!edge_port->open &&
2567 !edge_port->openPending) {
2568 dbg("%s - port not opened", __func__);
2569 return;
2572 cflag = tty->termios->c_cflag;
2574 switch (cflag & CSIZE) {
2575 case CS5:
2576 lData = LCR_BITS_5; mask = 0x1f;
2577 dbg("%s - data bits = 5", __func__);
2578 break;
2579 case CS6:
2580 lData = LCR_BITS_6; mask = 0x3f;
2581 dbg("%s - data bits = 6", __func__);
2582 break;
2583 case CS7:
2584 lData = LCR_BITS_7; mask = 0x7f;
2585 dbg("%s - data bits = 7", __func__);
2586 break;
2587 default:
2588 case CS8:
2589 lData = LCR_BITS_8;
2590 dbg("%s - data bits = 8", __func__);
2591 break;
2594 lParity = LCR_PAR_NONE;
2595 if (cflag & PARENB) {
2596 if (cflag & CMSPAR) {
2597 if (cflag & PARODD) {
2598 lParity = LCR_PAR_MARK;
2599 dbg("%s - parity = mark", __func__);
2600 } else {
2601 lParity = LCR_PAR_SPACE;
2602 dbg("%s - parity = space", __func__);
2604 } else if (cflag & PARODD) {
2605 lParity = LCR_PAR_ODD;
2606 dbg("%s - parity = odd", __func__);
2607 } else {
2608 lParity = LCR_PAR_EVEN;
2609 dbg("%s - parity = even", __func__);
2611 } else {
2612 dbg("%s - parity = none", __func__);
2615 if (cflag & CSTOPB) {
2616 lStop = LCR_STOP_2;
2617 dbg("%s - stop bits = 2", __func__);
2618 } else {
2619 lStop = LCR_STOP_1;
2620 dbg("%s - stop bits = 1", __func__);
2623 /* figure out the flow control settings */
2624 rxFlow = txFlow = 0x00;
2625 if (cflag & CRTSCTS) {
2626 rxFlow |= IOSP_RX_FLOW_RTS;
2627 txFlow |= IOSP_TX_FLOW_CTS;
2628 dbg("%s - RTS/CTS is enabled", __func__);
2629 } else {
2630 dbg("%s - RTS/CTS is disabled", __func__);
2633 /* if we are implementing XON/XOFF, set the start and stop character
2634 in the device */
2635 if (I_IXOFF(tty) || I_IXON(tty)) {
2636 unsigned char stop_char = STOP_CHAR(tty);
2637 unsigned char start_char = START_CHAR(tty);
2639 if ((!edge_serial->is_epic) ||
2640 ((edge_serial->is_epic) &&
2641 (edge_serial->epic_descriptor.Supports.IOSPSetXChar))) {
2642 send_iosp_ext_cmd(edge_port,
2643 IOSP_CMD_SET_XON_CHAR, start_char);
2644 send_iosp_ext_cmd(edge_port,
2645 IOSP_CMD_SET_XOFF_CHAR, stop_char);
2648 /* if we are implementing INBOUND XON/XOFF */
2649 if (I_IXOFF(tty)) {
2650 rxFlow |= IOSP_RX_FLOW_XON_XOFF;
2651 dbg("%s - INBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x",
2652 __func__, start_char, stop_char);
2653 } else {
2654 dbg("%s - INBOUND XON/XOFF is disabled", __func__);
2657 /* if we are implementing OUTBOUND XON/XOFF */
2658 if (I_IXON(tty)) {
2659 txFlow |= IOSP_TX_FLOW_XON_XOFF;
2660 dbg("%s - OUTBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x",
2661 __func__, start_char, stop_char);
2662 } else {
2663 dbg("%s - OUTBOUND XON/XOFF is disabled", __func__);
2667 /* Set flow control to the configured value */
2668 if ((!edge_serial->is_epic) ||
2669 ((edge_serial->is_epic) &&
2670 (edge_serial->epic_descriptor.Supports.IOSPSetRxFlow)))
2671 send_iosp_ext_cmd(edge_port, IOSP_CMD_SET_RX_FLOW, rxFlow);
2672 if ((!edge_serial->is_epic) ||
2673 ((edge_serial->is_epic) &&
2674 (edge_serial->epic_descriptor.Supports.IOSPSetTxFlow)))
2675 send_iosp_ext_cmd(edge_port, IOSP_CMD_SET_TX_FLOW, txFlow);
2678 edge_port->shadowLCR &= ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
2679 edge_port->shadowLCR |= (lData | lParity | lStop);
2681 edge_port->validDataMask = mask;
2683 /* Send the updated LCR value to the EdgePort */
2684 status = send_cmd_write_uart_register(edge_port, LCR,
2685 edge_port->shadowLCR);
2686 if (status != 0)
2687 return;
2689 /* set up the MCR register and send it to the EdgePort */
2690 edge_port->shadowMCR = MCR_MASTER_IE;
2691 if (cflag & CBAUD)
2692 edge_port->shadowMCR |= (MCR_DTR | MCR_RTS);
2694 status = send_cmd_write_uart_register(edge_port, MCR,
2695 edge_port->shadowMCR);
2696 if (status != 0)
2697 return;
2699 /* Determine divisor based on baud rate */
2700 baud = tty_get_baud_rate(tty);
2701 if (!baud) {
2702 /* pick a default, any default... */
2703 baud = 9600;
2706 dbg("%s - baud rate = %d", __func__, baud);
2707 status = send_cmd_write_baud_rate(edge_port, baud);
2708 if (status == -1) {
2709 /* Speed change was not possible - put back the old speed */
2710 baud = tty_termios_baud_rate(old_termios);
2711 tty_encode_baud_rate(tty, baud, baud);
2716 /****************************************************************************
2717 * unicode_to_ascii
2718 * Turns a string from Unicode into ASCII.
2719 * Doesn't do a good job with any characters that are outside the normal
2720 * ASCII range, but it's only for debugging...
2721 * NOTE: expects the unicode in LE format
2722 ****************************************************************************/
2723 static void unicode_to_ascii(char *string, int buflen,
2724 __le16 *unicode, int unicode_size)
2726 int i;
2728 if (buflen <= 0) /* never happens, but... */
2729 return;
2730 --buflen; /* space for nul */
2732 for (i = 0; i < unicode_size; i++) {
2733 if (i >= buflen)
2734 break;
2735 string[i] = (char)(le16_to_cpu(unicode[i]));
2737 string[i] = 0x00;
2741 /****************************************************************************
2742 * get_manufacturing_desc
2743 * reads in the manufacturing descriptor and stores it into the serial
2744 * structure.
2745 ****************************************************************************/
2746 static void get_manufacturing_desc(struct edgeport_serial *edge_serial)
2748 int response;
2750 dbg("getting manufacturer descriptor");
2752 response = rom_read(edge_serial->serial,
2753 (EDGE_MANUF_DESC_ADDR & 0xffff0000) >> 16,
2754 (__u16)(EDGE_MANUF_DESC_ADDR & 0x0000ffff),
2755 EDGE_MANUF_DESC_LEN,
2756 (__u8 *)(&edge_serial->manuf_descriptor));
2758 if (response < 1)
2759 dev_err(&edge_serial->serial->dev->dev,
2760 "error in getting manufacturer descriptor\n");
2761 else {
2762 char string[30];
2763 dbg("**Manufacturer Descriptor");
2764 dbg(" RomSize: %dK",
2765 edge_serial->manuf_descriptor.RomSize);
2766 dbg(" RamSize: %dK",
2767 edge_serial->manuf_descriptor.RamSize);
2768 dbg(" CpuRev: %d",
2769 edge_serial->manuf_descriptor.CpuRev);
2770 dbg(" BoardRev: %d",
2771 edge_serial->manuf_descriptor.BoardRev);
2772 dbg(" NumPorts: %d",
2773 edge_serial->manuf_descriptor.NumPorts);
2774 dbg(" DescDate: %d/%d/%d",
2775 edge_serial->manuf_descriptor.DescDate[0],
2776 edge_serial->manuf_descriptor.DescDate[1],
2777 edge_serial->manuf_descriptor.DescDate[2]+1900);
2778 unicode_to_ascii(string, sizeof(string),
2779 edge_serial->manuf_descriptor.SerialNumber,
2780 edge_serial->manuf_descriptor.SerNumLength/2);
2781 dbg(" SerialNumber: %s", string);
2782 unicode_to_ascii(string, sizeof(string),
2783 edge_serial->manuf_descriptor.AssemblyNumber,
2784 edge_serial->manuf_descriptor.AssemblyNumLength/2);
2785 dbg(" AssemblyNumber: %s", string);
2786 unicode_to_ascii(string, sizeof(string),
2787 edge_serial->manuf_descriptor.OemAssyNumber,
2788 edge_serial->manuf_descriptor.OemAssyNumLength/2);
2789 dbg(" OemAssyNumber: %s", string);
2790 dbg(" UartType: %d",
2791 edge_serial->manuf_descriptor.UartType);
2792 dbg(" IonPid: %d",
2793 edge_serial->manuf_descriptor.IonPid);
2794 dbg(" IonConfig: %d",
2795 edge_serial->manuf_descriptor.IonConfig);
2800 /****************************************************************************
2801 * get_boot_desc
2802 * reads in the bootloader descriptor and stores it into the serial
2803 * structure.
2804 ****************************************************************************/
2805 static void get_boot_desc(struct edgeport_serial *edge_serial)
2807 int response;
2809 dbg("getting boot descriptor");
2811 response = rom_read(edge_serial->serial,
2812 (EDGE_BOOT_DESC_ADDR & 0xffff0000) >> 16,
2813 (__u16)(EDGE_BOOT_DESC_ADDR & 0x0000ffff),
2814 EDGE_BOOT_DESC_LEN,
2815 (__u8 *)(&edge_serial->boot_descriptor));
2817 if (response < 1)
2818 dev_err(&edge_serial->serial->dev->dev,
2819 "error in getting boot descriptor\n");
2820 else {
2821 dbg("**Boot Descriptor:");
2822 dbg(" BootCodeLength: %d",
2823 le16_to_cpu(edge_serial->boot_descriptor.BootCodeLength));
2824 dbg(" MajorVersion: %d",
2825 edge_serial->boot_descriptor.MajorVersion);
2826 dbg(" MinorVersion: %d",
2827 edge_serial->boot_descriptor.MinorVersion);
2828 dbg(" BuildNumber: %d",
2829 le16_to_cpu(edge_serial->boot_descriptor.BuildNumber));
2830 dbg(" Capabilities: 0x%x",
2831 le16_to_cpu(edge_serial->boot_descriptor.Capabilities));
2832 dbg(" UConfig0: %d",
2833 edge_serial->boot_descriptor.UConfig0);
2834 dbg(" UConfig1: %d",
2835 edge_serial->boot_descriptor.UConfig1);
2840 /****************************************************************************
2841 * load_application_firmware
2842 * This is called to load the application firmware to the device
2843 ****************************************************************************/
2844 static void load_application_firmware(struct edgeport_serial *edge_serial)
2846 const struct ihex_binrec *rec;
2847 const struct firmware *fw;
2848 const char *fw_name;
2849 const char *fw_info;
2850 int response;
2851 __u32 Operaddr;
2852 __u16 build;
2854 switch (edge_serial->product_info.iDownloadFile) {
2855 case EDGE_DOWNLOAD_FILE_I930:
2856 fw_info = "downloading firmware version (930)";
2857 fw_name = "edgeport/down.fw";
2858 break;
2860 case EDGE_DOWNLOAD_FILE_80251:
2861 fw_info = "downloading firmware version (80251)";
2862 fw_name = "edgeport/down2.fw";
2863 break;
2865 case EDGE_DOWNLOAD_FILE_NONE:
2866 dbg("No download file specified, skipping download");
2867 return;
2869 default:
2870 return;
2873 response = request_ihex_firmware(&fw, fw_name,
2874 &edge_serial->serial->dev->dev);
2875 if (response) {
2876 printk(KERN_ERR "Failed to load image \"%s\" err %d\n",
2877 fw_name, response);
2878 return;
2881 rec = (const struct ihex_binrec *)fw->data;
2882 build = (rec->data[2] << 8) | rec->data[3];
2884 dbg("%s %d.%d.%d", fw_info, rec->data[0], rec->data[1], build);
2886 edge_serial->product_info.FirmwareMajorVersion = rec->data[0];
2887 edge_serial->product_info.FirmwareMinorVersion = rec->data[1];
2888 edge_serial->product_info.FirmwareBuildNumber = cpu_to_le16(build);
2890 for (rec = ihex_next_binrec(rec); rec;
2891 rec = ihex_next_binrec(rec)) {
2892 Operaddr = be32_to_cpu(rec->addr);
2893 response = sram_write(edge_serial->serial,
2894 Operaddr >> 16,
2895 Operaddr & 0xFFFF,
2896 be16_to_cpu(rec->len),
2897 &rec->data[0]);
2898 if (response < 0) {
2899 dev_err(&edge_serial->serial->dev->dev,
2900 "sram_write failed (%x, %x, %d)\n",
2901 Operaddr >> 16, Operaddr & 0xFFFF,
2902 be16_to_cpu(rec->len));
2903 break;
2907 dbg("sending exec_dl_code");
2908 response = usb_control_msg (edge_serial->serial->dev,
2909 usb_sndctrlpipe(edge_serial->serial->dev, 0),
2910 USB_REQUEST_ION_EXEC_DL_CODE,
2911 0x40, 0x4000, 0x0001, NULL, 0, 3000);
2913 release_firmware(fw);
2917 /****************************************************************************
2918 * edge_startup
2919 ****************************************************************************/
2920 static int edge_startup(struct usb_serial *serial)
2922 struct edgeport_serial *edge_serial;
2923 struct edgeport_port *edge_port;
2924 struct usb_device *dev;
2925 int i, j;
2926 int response;
2927 bool interrupt_in_found;
2928 bool bulk_in_found;
2929 bool bulk_out_found;
2930 static __u32 descriptor[3] = { EDGE_COMPATIBILITY_MASK0,
2931 EDGE_COMPATIBILITY_MASK1,
2932 EDGE_COMPATIBILITY_MASK2 };
2934 dev = serial->dev;
2936 /* create our private serial structure */
2937 edge_serial = kzalloc(sizeof(struct edgeport_serial), GFP_KERNEL);
2938 if (edge_serial == NULL) {
2939 dev_err(&serial->dev->dev, "%s - Out of memory\n", __func__);
2940 return -ENOMEM;
2942 spin_lock_init(&edge_serial->es_lock);
2943 edge_serial->serial = serial;
2944 usb_set_serial_data(serial, edge_serial);
2946 /* get the name for the device from the device */
2947 i = usb_string(dev, dev->descriptor.iManufacturer,
2948 &edge_serial->name[0], MAX_NAME_LEN+1);
2949 if (i < 0)
2950 i = 0;
2951 edge_serial->name[i++] = ' ';
2952 usb_string(dev, dev->descriptor.iProduct,
2953 &edge_serial->name[i], MAX_NAME_LEN+2 - i);
2955 dev_info(&serial->dev->dev, "%s detected\n", edge_serial->name);
2957 /* Read the epic descriptor */
2958 if (get_epic_descriptor(edge_serial) <= 0) {
2959 /* memcpy descriptor to Supports structures */
2960 memcpy(&edge_serial->epic_descriptor.Supports, descriptor,
2961 sizeof(struct edge_compatibility_bits));
2963 /* get the manufacturing descriptor for this device */
2964 get_manufacturing_desc(edge_serial);
2966 /* get the boot descriptor */
2967 get_boot_desc(edge_serial);
2969 get_product_info(edge_serial);
2972 /* set the number of ports from the manufacturing description */
2973 /* serial->num_ports = serial->product_info.NumPorts; */
2974 if ((!edge_serial->is_epic) &&
2975 (edge_serial->product_info.NumPorts != serial->num_ports)) {
2976 dev_warn(&serial->dev->dev, "Device Reported %d serial ports "
2977 "vs. core thinking we have %d ports, email "
2978 "greg@kroah.com this information.\n",
2979 edge_serial->product_info.NumPorts,
2980 serial->num_ports);
2983 dbg("%s - time 1 %ld", __func__, jiffies);
2985 /* If not an EPiC device */
2986 if (!edge_serial->is_epic) {
2987 /* now load the application firmware into this device */
2988 load_application_firmware(edge_serial);
2990 dbg("%s - time 2 %ld", __func__, jiffies);
2992 /* Check current Edgeport EEPROM and update if necessary */
2993 update_edgeport_E2PROM(edge_serial);
2995 dbg("%s - time 3 %ld", __func__, jiffies);
2997 /* set the configuration to use #1 */
2998 /* dbg("set_configuration 1"); */
2999 /* usb_set_configuration (dev, 1); */
3001 dbg(" FirmwareMajorVersion %d.%d.%d",
3002 edge_serial->product_info.FirmwareMajorVersion,
3003 edge_serial->product_info.FirmwareMinorVersion,
3004 le16_to_cpu(edge_serial->product_info.FirmwareBuildNumber));
3006 /* we set up the pointers to the endpoints in the edge_open function,
3007 * as the structures aren't created yet. */
3009 /* set up our port private structures */
3010 for (i = 0; i < serial->num_ports; ++i) {
3011 edge_port = kzalloc(sizeof(struct edgeport_port), GFP_KERNEL);
3012 if (edge_port == NULL) {
3013 dev_err(&serial->dev->dev, "%s - Out of memory\n",
3014 __func__);
3015 for (j = 0; j < i; ++j) {
3016 kfree(usb_get_serial_port_data(serial->port[j]));
3017 usb_set_serial_port_data(serial->port[j],
3018 NULL);
3020 usb_set_serial_data(serial, NULL);
3021 kfree(edge_serial);
3022 return -ENOMEM;
3024 spin_lock_init(&edge_port->ep_lock);
3025 edge_port->port = serial->port[i];
3026 usb_set_serial_port_data(serial->port[i], edge_port);
3029 response = 0;
3031 if (edge_serial->is_epic) {
3032 /* EPIC thing, set up our interrupt polling now and our read
3033 * urb, so that the device knows it really is connected. */
3034 interrupt_in_found = bulk_in_found = bulk_out_found = false;
3035 for (i = 0; i < serial->interface->altsetting[0]
3036 .desc.bNumEndpoints; ++i) {
3037 struct usb_endpoint_descriptor *endpoint;
3038 int buffer_size;
3040 endpoint = &serial->interface->altsetting[0].
3041 endpoint[i].desc;
3042 buffer_size = usb_endpoint_maxp(endpoint);
3043 if (!interrupt_in_found &&
3044 (usb_endpoint_is_int_in(endpoint))) {
3045 /* we found a interrupt in endpoint */
3046 dbg("found interrupt in");
3048 /* not set up yet, so do it now */
3049 edge_serial->interrupt_read_urb =
3050 usb_alloc_urb(0, GFP_KERNEL);
3051 if (!edge_serial->interrupt_read_urb) {
3052 dev_err(&dev->dev, "out of memory\n");
3053 return -ENOMEM;
3055 edge_serial->interrupt_in_buffer =
3056 kmalloc(buffer_size, GFP_KERNEL);
3057 if (!edge_serial->interrupt_in_buffer) {
3058 dev_err(&dev->dev, "out of memory\n");
3059 usb_free_urb(edge_serial->interrupt_read_urb);
3060 return -ENOMEM;
3062 edge_serial->interrupt_in_endpoint =
3063 endpoint->bEndpointAddress;
3065 /* set up our interrupt urb */
3066 usb_fill_int_urb(
3067 edge_serial->interrupt_read_urb,
3068 dev,
3069 usb_rcvintpipe(dev,
3070 endpoint->bEndpointAddress),
3071 edge_serial->interrupt_in_buffer,
3072 buffer_size,
3073 edge_interrupt_callback,
3074 edge_serial,
3075 endpoint->bInterval);
3077 interrupt_in_found = true;
3080 if (!bulk_in_found &&
3081 (usb_endpoint_is_bulk_in(endpoint))) {
3082 /* we found a bulk in endpoint */
3083 dbg("found bulk in");
3085 /* not set up yet, so do it now */
3086 edge_serial->read_urb =
3087 usb_alloc_urb(0, GFP_KERNEL);
3088 if (!edge_serial->read_urb) {
3089 dev_err(&dev->dev, "out of memory\n");
3090 return -ENOMEM;
3092 edge_serial->bulk_in_buffer =
3093 kmalloc(buffer_size, GFP_KERNEL);
3094 if (!edge_serial->bulk_in_buffer) {
3095 dev_err(&dev->dev, "out of memory\n");
3096 usb_free_urb(edge_serial->read_urb);
3097 return -ENOMEM;
3099 edge_serial->bulk_in_endpoint =
3100 endpoint->bEndpointAddress;
3102 /* set up our bulk in urb */
3103 usb_fill_bulk_urb(edge_serial->read_urb, dev,
3104 usb_rcvbulkpipe(dev,
3105 endpoint->bEndpointAddress),
3106 edge_serial->bulk_in_buffer,
3107 usb_endpoint_maxp(endpoint),
3108 edge_bulk_in_callback,
3109 edge_serial);
3110 bulk_in_found = true;
3113 if (!bulk_out_found &&
3114 (usb_endpoint_is_bulk_out(endpoint))) {
3115 /* we found a bulk out endpoint */
3116 dbg("found bulk out");
3117 edge_serial->bulk_out_endpoint =
3118 endpoint->bEndpointAddress;
3119 bulk_out_found = true;
3123 if (!interrupt_in_found || !bulk_in_found || !bulk_out_found) {
3124 dev_err(&dev->dev, "Error - the proper endpoints "
3125 "were not found!\n");
3126 return -ENODEV;
3129 /* start interrupt read for this edgeport this interrupt will
3130 * continue as long as the edgeport is connected */
3131 response = usb_submit_urb(edge_serial->interrupt_read_urb,
3132 GFP_KERNEL);
3133 if (response)
3134 dev_err(&dev->dev,
3135 "%s - Error %d submitting control urb\n",
3136 __func__, response);
3138 return response;
3142 /****************************************************************************
3143 * edge_disconnect
3144 * This function is called whenever the device is removed from the usb bus.
3145 ****************************************************************************/
3146 static void edge_disconnect(struct usb_serial *serial)
3148 struct edgeport_serial *edge_serial = usb_get_serial_data(serial);
3150 dbg("%s", __func__);
3152 /* stop reads and writes on all ports */
3153 /* free up our endpoint stuff */
3154 if (edge_serial->is_epic) {
3155 usb_kill_urb(edge_serial->interrupt_read_urb);
3156 usb_free_urb(edge_serial->interrupt_read_urb);
3157 kfree(edge_serial->interrupt_in_buffer);
3159 usb_kill_urb(edge_serial->read_urb);
3160 usb_free_urb(edge_serial->read_urb);
3161 kfree(edge_serial->bulk_in_buffer);
3166 /****************************************************************************
3167 * edge_release
3168 * This function is called when the device structure is deallocated.
3169 ****************************************************************************/
3170 static void edge_release(struct usb_serial *serial)
3172 struct edgeport_serial *edge_serial = usb_get_serial_data(serial);
3173 int i;
3175 dbg("%s", __func__);
3177 for (i = 0; i < serial->num_ports; ++i)
3178 kfree(usb_get_serial_port_data(serial->port[i]));
3180 kfree(edge_serial);
3184 /****************************************************************************
3185 * edgeport_init
3186 * This is called by the module subsystem, or on startup to initialize us
3187 ****************************************************************************/
3188 static int __init edgeport_init(void)
3190 int retval;
3192 retval = usb_serial_register(&edgeport_2port_device);
3193 if (retval)
3194 goto failed_2port_device_register;
3195 retval = usb_serial_register(&edgeport_4port_device);
3196 if (retval)
3197 goto failed_4port_device_register;
3198 retval = usb_serial_register(&edgeport_8port_device);
3199 if (retval)
3200 goto failed_8port_device_register;
3201 retval = usb_serial_register(&epic_device);
3202 if (retval)
3203 goto failed_epic_device_register;
3204 retval = usb_register(&io_driver);
3205 if (retval)
3206 goto failed_usb_register;
3207 atomic_set(&CmdUrbs, 0);
3208 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
3209 DRIVER_DESC "\n");
3210 return 0;
3212 failed_usb_register:
3213 usb_serial_deregister(&epic_device);
3214 failed_epic_device_register:
3215 usb_serial_deregister(&edgeport_8port_device);
3216 failed_8port_device_register:
3217 usb_serial_deregister(&edgeport_4port_device);
3218 failed_4port_device_register:
3219 usb_serial_deregister(&edgeport_2port_device);
3220 failed_2port_device_register:
3221 return retval;
3225 /****************************************************************************
3226 * edgeport_exit
3227 * Called when the driver is about to be unloaded.
3228 ****************************************************************************/
3229 static void __exit edgeport_exit (void)
3231 usb_deregister(&io_driver);
3232 usb_serial_deregister(&edgeport_2port_device);
3233 usb_serial_deregister(&edgeport_4port_device);
3234 usb_serial_deregister(&edgeport_8port_device);
3235 usb_serial_deregister(&epic_device);
3238 module_init(edgeport_init);
3239 module_exit(edgeport_exit);
3241 /* Module information */
3242 MODULE_AUTHOR(DRIVER_AUTHOR);
3243 MODULE_DESCRIPTION(DRIVER_DESC);
3244 MODULE_LICENSE("GPL");
3245 MODULE_FIRMWARE("edgeport/boot.fw");
3246 MODULE_FIRMWARE("edgeport/boot2.fw");
3247 MODULE_FIRMWARE("edgeport/down.fw");
3248 MODULE_FIRMWARE("edgeport/down2.fw");
3250 module_param(debug, bool, S_IRUGO | S_IWUSR);
3251 MODULE_PARM_DESC(debug, "Debug enabled or not");