USB: pl2303 usb_serial: implement sysrq handling on break
[linux-2.6/x86.git] / drivers / usb / serial / io_edgeport.c
blob53ef5996e33de377b6290c84e4183f2669ef646e
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 int 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 struct file *filp);
210 static void edge_close(struct usb_serial_port *port);
211 static int edge_write(struct tty_struct *tty, struct usb_serial_port *port,
212 const unsigned char *buf, int count);
213 static int edge_write_room(struct tty_struct *tty);
214 static int edge_chars_in_buffer(struct tty_struct *tty);
215 static void edge_throttle(struct tty_struct *tty);
216 static void edge_unthrottle(struct tty_struct *tty);
217 static void edge_set_termios(struct tty_struct *tty,
218 struct usb_serial_port *port,
219 struct ktermios *old_termios);
220 static int edge_ioctl(struct tty_struct *tty, struct file *file,
221 unsigned int cmd, unsigned long arg);
222 static void edge_break(struct tty_struct *tty, int break_state);
223 static int edge_tiocmget(struct tty_struct *tty, struct file *file);
224 static int edge_tiocmset(struct tty_struct *tty, struct file *file,
225 unsigned int set, unsigned int clear);
226 static int edge_startup(struct usb_serial *serial);
227 static void edge_shutdown(struct usb_serial *serial);
229 #include "io_tables.h" /* all of the devices that this driver supports */
231 /* function prototypes for all of our local functions */
233 static void process_rcvd_data(struct edgeport_serial *edge_serial,
234 unsigned char *buffer, __u16 bufferLength);
235 static void process_rcvd_status(struct edgeport_serial *edge_serial,
236 __u8 byte2, __u8 byte3);
237 static void edge_tty_recv(struct device *dev, struct tty_struct *tty,
238 unsigned char *data, int length);
239 static void handle_new_msr(struct edgeport_port *edge_port, __u8 newMsr);
240 static void handle_new_lsr(struct edgeport_port *edge_port, __u8 lsrData,
241 __u8 lsr, __u8 data);
242 static int send_iosp_ext_cmd(struct edgeport_port *edge_port, __u8 command,
243 __u8 param);
244 static int calc_baud_rate_divisor(int baud_rate, int *divisor);
245 static int send_cmd_write_baud_rate(struct edgeport_port *edge_port,
246 int baudRate);
247 static void change_port_settings(struct tty_struct *tty,
248 struct edgeport_port *edge_port,
249 struct ktermios *old_termios);
250 static int send_cmd_write_uart_register(struct edgeport_port *edge_port,
251 __u8 regNum, __u8 regValue);
252 static int write_cmd_usb(struct edgeport_port *edge_port,
253 unsigned char *buffer, int writeLength);
254 static void send_more_port_data(struct edgeport_serial *edge_serial,
255 struct edgeport_port *edge_port);
257 static int sram_write(struct usb_serial *serial, __u16 extAddr, __u16 addr,
258 __u16 length, const __u8 *data);
259 static int rom_read(struct usb_serial *serial, __u16 extAddr, __u16 addr,
260 __u16 length, __u8 *data);
261 static int rom_write(struct usb_serial *serial, __u16 extAddr, __u16 addr,
262 __u16 length, const __u8 *data);
263 static void get_manufacturing_desc(struct edgeport_serial *edge_serial);
264 static void get_boot_desc(struct edgeport_serial *edge_serial);
265 static void load_application_firmware(struct edgeport_serial *edge_serial);
267 static void unicode_to_ascii(char *string, int buflen,
268 __le16 *unicode, int unicode_size);
271 /* ************************************************************************ */
272 /* ************************************************************************ */
273 /* ************************************************************************ */
274 /* ************************************************************************ */
276 /************************************************************************
278 * update_edgeport_E2PROM() Compare current versions of *
279 * Boot ROM and Manufacture *
280 * Descriptors with versions *
281 * embedded in this driver *
283 ************************************************************************/
284 static void update_edgeport_E2PROM(struct edgeport_serial *edge_serial)
286 __u32 BootCurVer;
287 __u32 BootNewVer;
288 __u8 BootMajorVersion;
289 __u8 BootMinorVersion;
290 __u16 BootBuildNumber;
291 __u32 Bootaddr;
292 const struct ihex_binrec *rec;
293 const struct firmware *fw;
294 const char *fw_name;
295 int response;
297 switch (edge_serial->product_info.iDownloadFile) {
298 case EDGE_DOWNLOAD_FILE_I930:
299 fw_name = "edgeport/boot.fw";
300 break;
301 case EDGE_DOWNLOAD_FILE_80251:
302 fw_name = "edgeport/boot2.fw";
303 break;
304 default:
305 return;
308 response = request_ihex_firmware(&fw, fw_name,
309 &edge_serial->serial->dev->dev);
310 if (response) {
311 printk(KERN_ERR "Failed to load image \"%s\" err %d\n",
312 fw_name, response);
313 return;
316 rec = (const struct ihex_binrec *)fw->data;
317 BootMajorVersion = rec->data[0];
318 BootMinorVersion = rec->data[1];
319 BootBuildNumber = (rec->data[2] << 8) | rec->data[3];
321 /* Check Boot Image Version */
322 BootCurVer = (edge_serial->boot_descriptor.MajorVersion << 24) +
323 (edge_serial->boot_descriptor.MinorVersion << 16) +
324 le16_to_cpu(edge_serial->boot_descriptor.BuildNumber);
326 BootNewVer = (BootMajorVersion << 24) +
327 (BootMinorVersion << 16) +
328 BootBuildNumber;
330 dbg("Current Boot Image version %d.%d.%d",
331 edge_serial->boot_descriptor.MajorVersion,
332 edge_serial->boot_descriptor.MinorVersion,
333 le16_to_cpu(edge_serial->boot_descriptor.BuildNumber));
336 if (BootNewVer > BootCurVer) {
337 dbg("**Update Boot Image from %d.%d.%d to %d.%d.%d",
338 edge_serial->boot_descriptor.MajorVersion,
339 edge_serial->boot_descriptor.MinorVersion,
340 le16_to_cpu(edge_serial->boot_descriptor.BuildNumber),
341 BootMajorVersion, BootMinorVersion, BootBuildNumber);
343 dbg("Downloading new Boot Image");
345 for (rec = ihex_next_binrec(rec); rec;
346 rec = ihex_next_binrec(rec)) {
347 Bootaddr = be32_to_cpu(rec->addr);
348 response = rom_write(edge_serial->serial,
349 Bootaddr >> 16,
350 Bootaddr & 0xFFFF,
351 be16_to_cpu(rec->len),
352 &rec->data[0]);
353 if (response < 0) {
354 dev_err(&edge_serial->serial->dev->dev,
355 "rom_write failed (%x, %x, %d)\n",
356 Bootaddr >> 16, Bootaddr & 0xFFFF,
357 be16_to_cpu(rec->len));
358 break;
361 } else {
362 dbg("Boot Image -- already up to date");
364 release_firmware(fw);
368 /************************************************************************
370 * Get string descriptor from device *
372 ************************************************************************/
373 static int get_string(struct usb_device *dev, int Id, char *string, int buflen)
375 struct usb_string_descriptor StringDesc;
376 struct usb_string_descriptor *pStringDesc;
378 dbg("%s - USB String ID = %d", __func__, Id);
380 if (!usb_get_descriptor(dev, USB_DT_STRING, Id,
381 &StringDesc, sizeof(StringDesc)))
382 return 0;
384 pStringDesc = kmalloc(StringDesc.bLength, GFP_KERNEL);
385 if (!pStringDesc)
386 return 0;
388 if (!usb_get_descriptor(dev, USB_DT_STRING, Id,
389 pStringDesc, StringDesc.bLength)) {
390 kfree(pStringDesc);
391 return 0;
394 unicode_to_ascii(string, buflen,
395 pStringDesc->wData, pStringDesc->bLength/2);
397 kfree(pStringDesc);
398 dbg("%s - USB String %s", __func__, string);
399 return strlen(string);
403 #if 0
404 /************************************************************************
406 * Get string descriptor from device
408 ************************************************************************/
409 static int get_string_desc(struct usb_device *dev, int Id,
410 struct usb_string_descriptor **pRetDesc)
412 struct usb_string_descriptor StringDesc;
413 struct usb_string_descriptor *pStringDesc;
415 dbg("%s - USB String ID = %d", __func__, Id);
417 if (!usb_get_descriptor(dev, USB_DT_STRING, Id, &StringDesc,
418 sizeof(StringDesc)))
419 return 0;
421 pStringDesc = kmalloc(StringDesc.bLength, GFP_KERNEL);
422 if (!pStringDesc)
423 return -1;
425 if (!usb_get_descriptor(dev, USB_DT_STRING, Id, pStringDesc,
426 StringDesc.bLength)) {
427 kfree(pStringDesc);
428 return -1;
431 *pRetDesc = pStringDesc;
432 return 0;
434 #endif
436 static void dump_product_info(struct edgeport_product_info *product_info)
438 /* Dump Product Info structure */
439 dbg("**Product Information:");
440 dbg(" ProductId %x", product_info->ProductId);
441 dbg(" NumPorts %d", product_info->NumPorts);
442 dbg(" ProdInfoVer %d", product_info->ProdInfoVer);
443 dbg(" IsServer %d", product_info->IsServer);
444 dbg(" IsRS232 %d", product_info->IsRS232);
445 dbg(" IsRS422 %d", product_info->IsRS422);
446 dbg(" IsRS485 %d", product_info->IsRS485);
447 dbg(" RomSize %d", product_info->RomSize);
448 dbg(" RamSize %d", product_info->RamSize);
449 dbg(" CpuRev %x", product_info->CpuRev);
450 dbg(" BoardRev %x", product_info->BoardRev);
451 dbg(" BootMajorVersion %d.%d.%d", product_info->BootMajorVersion,
452 product_info->BootMinorVersion,
453 le16_to_cpu(product_info->BootBuildNumber));
454 dbg(" FirmwareMajorVersion %d.%d.%d",
455 product_info->FirmwareMajorVersion,
456 product_info->FirmwareMinorVersion,
457 le16_to_cpu(product_info->FirmwareBuildNumber));
458 dbg(" ManufactureDescDate %d/%d/%d",
459 product_info->ManufactureDescDate[0],
460 product_info->ManufactureDescDate[1],
461 product_info->ManufactureDescDate[2]+1900);
462 dbg(" iDownloadFile 0x%x", product_info->iDownloadFile);
463 dbg(" EpicVer %d", product_info->EpicVer);
466 static void get_product_info(struct edgeport_serial *edge_serial)
468 struct edgeport_product_info *product_info = &edge_serial->product_info;
470 memset(product_info, 0, sizeof(struct edgeport_product_info));
472 product_info->ProductId = (__u16)(le16_to_cpu(edge_serial->serial->dev->descriptor.idProduct) & ~ION_DEVICE_ID_80251_NETCHIP);
473 product_info->NumPorts = edge_serial->manuf_descriptor.NumPorts;
474 product_info->ProdInfoVer = 0;
476 product_info->RomSize = edge_serial->manuf_descriptor.RomSize;
477 product_info->RamSize = edge_serial->manuf_descriptor.RamSize;
478 product_info->CpuRev = edge_serial->manuf_descriptor.CpuRev;
479 product_info->BoardRev = edge_serial->manuf_descriptor.BoardRev;
481 product_info->BootMajorVersion =
482 edge_serial->boot_descriptor.MajorVersion;
483 product_info->BootMinorVersion =
484 edge_serial->boot_descriptor.MinorVersion;
485 product_info->BootBuildNumber =
486 edge_serial->boot_descriptor.BuildNumber;
488 memcpy(product_info->ManufactureDescDate,
489 edge_serial->manuf_descriptor.DescDate,
490 sizeof(edge_serial->manuf_descriptor.DescDate));
492 /* check if this is 2nd generation hardware */
493 if (le16_to_cpu(edge_serial->serial->dev->descriptor.idProduct)
494 & ION_DEVICE_ID_80251_NETCHIP)
495 product_info->iDownloadFile = EDGE_DOWNLOAD_FILE_80251;
496 else
497 product_info->iDownloadFile = EDGE_DOWNLOAD_FILE_I930;
499 /* Determine Product type and set appropriate flags */
500 switch (DEVICE_ID_FROM_USB_PRODUCT_ID(product_info->ProductId)) {
501 case ION_DEVICE_ID_EDGEPORT_COMPATIBLE:
502 case ION_DEVICE_ID_EDGEPORT_4T:
503 case ION_DEVICE_ID_EDGEPORT_4:
504 case ION_DEVICE_ID_EDGEPORT_2:
505 case ION_DEVICE_ID_EDGEPORT_8_DUAL_CPU:
506 case ION_DEVICE_ID_EDGEPORT_8:
507 case ION_DEVICE_ID_EDGEPORT_421:
508 case ION_DEVICE_ID_EDGEPORT_21:
509 case ION_DEVICE_ID_EDGEPORT_2_DIN:
510 case ION_DEVICE_ID_EDGEPORT_4_DIN:
511 case ION_DEVICE_ID_EDGEPORT_16_DUAL_CPU:
512 product_info->IsRS232 = 1;
513 break;
515 case ION_DEVICE_ID_EDGEPORT_2I: /* Edgeport/2 RS422/RS485 */
516 product_info->IsRS422 = 1;
517 product_info->IsRS485 = 1;
518 break;
520 case ION_DEVICE_ID_EDGEPORT_8I: /* Edgeport/4 RS422 */
521 case ION_DEVICE_ID_EDGEPORT_4I: /* Edgeport/4 RS422 */
522 product_info->IsRS422 = 1;
523 break;
526 dump_product_info(product_info);
529 static int get_epic_descriptor(struct edgeport_serial *ep)
531 int result;
532 struct usb_serial *serial = ep->serial;
533 struct edgeport_product_info *product_info = &ep->product_info;
534 struct edge_compatibility_descriptor *epic = &ep->epic_descriptor;
535 struct edge_compatibility_bits *bits;
537 ep->is_epic = 0;
538 result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
539 USB_REQUEST_ION_GET_EPIC_DESC,
540 0xC0, 0x00, 0x00,
541 &ep->epic_descriptor,
542 sizeof(struct edge_compatibility_descriptor),
543 300);
545 dbg("%s result = %d", __func__, result);
547 if (result > 0) {
548 ep->is_epic = 1;
549 memset(product_info, 0, sizeof(struct edgeport_product_info));
551 product_info->NumPorts = epic->NumPorts;
552 product_info->ProdInfoVer = 0;
553 product_info->FirmwareMajorVersion = epic->MajorVersion;
554 product_info->FirmwareMinorVersion = epic->MinorVersion;
555 product_info->FirmwareBuildNumber = epic->BuildNumber;
556 product_info->iDownloadFile = epic->iDownloadFile;
557 product_info->EpicVer = epic->EpicVer;
558 product_info->Epic = epic->Supports;
559 product_info->ProductId = ION_DEVICE_ID_EDGEPORT_COMPATIBLE;
560 dump_product_info(product_info);
562 bits = &ep->epic_descriptor.Supports;
563 dbg("**EPIC descriptor:");
564 dbg(" VendEnableSuspend: %s", bits->VendEnableSuspend ? "TRUE": "FALSE");
565 dbg(" IOSPOpen : %s", bits->IOSPOpen ? "TRUE": "FALSE");
566 dbg(" IOSPClose : %s", bits->IOSPClose ? "TRUE": "FALSE");
567 dbg(" IOSPChase : %s", bits->IOSPChase ? "TRUE": "FALSE");
568 dbg(" IOSPSetRxFlow : %s", bits->IOSPSetRxFlow ? "TRUE": "FALSE");
569 dbg(" IOSPSetTxFlow : %s", bits->IOSPSetTxFlow ? "TRUE": "FALSE");
570 dbg(" IOSPSetXChar : %s", bits->IOSPSetXChar ? "TRUE": "FALSE");
571 dbg(" IOSPRxCheck : %s", bits->IOSPRxCheck ? "TRUE": "FALSE");
572 dbg(" IOSPSetClrBreak : %s", bits->IOSPSetClrBreak ? "TRUE": "FALSE");
573 dbg(" IOSPWriteMCR : %s", bits->IOSPWriteMCR ? "TRUE": "FALSE");
574 dbg(" IOSPWriteLCR : %s", bits->IOSPWriteLCR ? "TRUE": "FALSE");
575 dbg(" IOSPSetBaudRate : %s", bits->IOSPSetBaudRate ? "TRUE": "FALSE");
576 dbg(" TrueEdgeport : %s", bits->TrueEdgeport ? "TRUE": "FALSE");
579 return result;
583 /************************************************************************/
584 /************************************************************************/
585 /* U S B C A L L B A C K F U N C T I O N S */
586 /* U S B C A L L B A C K F U N C T I O N S */
587 /************************************************************************/
588 /************************************************************************/
590 /*****************************************************************************
591 * edge_interrupt_callback
592 * this is the callback function for when we have received data on the
593 * interrupt endpoint.
594 *****************************************************************************/
595 static void edge_interrupt_callback(struct urb *urb)
597 struct edgeport_serial *edge_serial = urb->context;
598 struct edgeport_port *edge_port;
599 struct usb_serial_port *port;
600 struct tty_struct *tty;
601 unsigned char *data = urb->transfer_buffer;
602 int length = urb->actual_length;
603 int bytes_avail;
604 int position;
605 int txCredits;
606 int portNumber;
607 int result;
608 int status = urb->status;
610 dbg("%s", __func__);
612 switch (status) {
613 case 0:
614 /* success */
615 break;
616 case -ECONNRESET:
617 case -ENOENT:
618 case -ESHUTDOWN:
619 /* this urb is terminated, clean up */
620 dbg("%s - urb shutting down with status: %d",
621 __func__, status);
622 return;
623 default:
624 dbg("%s - nonzero urb status received: %d", __func__, status);
625 goto exit;
628 /* process this interrupt-read even if there are no ports open */
629 if (length) {
630 usb_serial_debug_data(debug, &edge_serial->serial->dev->dev,
631 __func__, length, data);
633 if (length > 1) {
634 bytes_avail = data[0] | (data[1] << 8);
635 if (bytes_avail) {
636 spin_lock(&edge_serial->es_lock);
637 edge_serial->rxBytesAvail += bytes_avail;
638 dbg("%s - bytes_avail=%d, rxBytesAvail=%d, read_in_progress=%d", __func__, bytes_avail, edge_serial->rxBytesAvail, edge_serial->read_in_progress);
640 if (edge_serial->rxBytesAvail > 0 &&
641 !edge_serial->read_in_progress) {
642 dbg("%s - posting a read", __func__);
643 edge_serial->read_in_progress = true;
645 /* we have pending bytes on the
646 bulk in pipe, send a request */
647 edge_serial->read_urb->dev = edge_serial->serial->dev;
648 result = usb_submit_urb(edge_serial->read_urb, GFP_ATOMIC);
649 if (result) {
650 dev_err(&edge_serial->serial->dev->dev, "%s - usb_submit_urb(read bulk) failed with result = %d\n", __func__, result);
651 edge_serial->read_in_progress = false;
654 spin_unlock(&edge_serial->es_lock);
657 /* grab the txcredits for the ports if available */
658 position = 2;
659 portNumber = 0;
660 while ((position < length) &&
661 (portNumber < edge_serial->serial->num_ports)) {
662 txCredits = data[position] | (data[position+1] << 8);
663 if (txCredits) {
664 port = edge_serial->serial->port[portNumber];
665 edge_port = usb_get_serial_port_data(port);
666 if (edge_port->open) {
667 spin_lock(&edge_port->ep_lock);
668 edge_port->txCredits += txCredits;
669 spin_unlock(&edge_port->ep_lock);
670 dbg("%s - txcredits for port%d = %d",
671 __func__, portNumber,
672 edge_port->txCredits);
674 /* tell the tty driver that something
675 has changed */
676 tty = tty_port_tty_get(
677 &edge_port->port->port);
678 if (tty) {
679 tty_wakeup(tty);
680 tty_kref_put(tty);
682 /* Since we have more credit, check
683 if more data can be sent */
684 send_more_port_data(edge_serial,
685 edge_port);
688 position += 2;
689 ++portNumber;
693 exit:
694 result = usb_submit_urb(urb, GFP_ATOMIC);
695 if (result)
696 dev_err(&urb->dev->dev,
697 "%s - Error %d submitting control urb\n",
698 __func__, result);
702 /*****************************************************************************
703 * edge_bulk_in_callback
704 * this is the callback function for when we have received data on the
705 * bulk in endpoint.
706 *****************************************************************************/
707 static void edge_bulk_in_callback(struct urb *urb)
709 struct edgeport_serial *edge_serial = urb->context;
710 unsigned char *data = urb->transfer_buffer;
711 int retval;
712 __u16 raw_data_length;
713 int status = urb->status;
715 dbg("%s", __func__);
717 if (status) {
718 dbg("%s - nonzero read bulk status received: %d",
719 __func__, status);
720 edge_serial->read_in_progress = false;
721 return;
724 if (urb->actual_length == 0) {
725 dbg("%s - read bulk callback with no data", __func__);
726 edge_serial->read_in_progress = false;
727 return;
730 raw_data_length = urb->actual_length;
732 usb_serial_debug_data(debug, &edge_serial->serial->dev->dev,
733 __func__, raw_data_length, data);
735 spin_lock(&edge_serial->es_lock);
737 /* decrement our rxBytes available by the number that we just got */
738 edge_serial->rxBytesAvail -= raw_data_length;
740 dbg("%s - Received = %d, rxBytesAvail %d", __func__,
741 raw_data_length, edge_serial->rxBytesAvail);
743 process_rcvd_data(edge_serial, data, urb->actual_length);
745 /* check to see if there's any more data for us to read */
746 if (edge_serial->rxBytesAvail > 0) {
747 dbg("%s - posting a read", __func__);
748 edge_serial->read_urb->dev = edge_serial->serial->dev;
749 retval = usb_submit_urb(edge_serial->read_urb, GFP_ATOMIC);
750 if (retval) {
751 dev_err(&urb->dev->dev,
752 "%s - usb_submit_urb(read bulk) failed, "
753 "retval = %d\n", __func__, retval);
754 edge_serial->read_in_progress = false;
756 } else {
757 edge_serial->read_in_progress = false;
760 spin_unlock(&edge_serial->es_lock);
764 /*****************************************************************************
765 * edge_bulk_out_data_callback
766 * this is the callback function for when we have finished sending
767 * serial data on the bulk out endpoint.
768 *****************************************************************************/
769 static void edge_bulk_out_data_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 if (status) {
778 dbg("%s - nonzero write bulk status received: %d",
779 __func__, status);
782 tty = tty_port_tty_get(&edge_port->port->port);
784 if (tty && edge_port->open) {
785 /* let the tty driver wakeup if it has a special
786 write_wakeup function */
787 tty_wakeup(tty);
789 tty_kref_put(tty);
791 /* Release the Write URB */
792 edge_port->write_in_progress = false;
794 /* Check if more data needs to be sent */
795 send_more_port_data((struct edgeport_serial *)
796 (usb_get_serial_data(edge_port->port->serial)), edge_port);
800 /*****************************************************************************
801 * BulkOutCmdCallback
802 * this is the callback function for when we have finished sending a
803 * command on the bulk out endpoint.
804 *****************************************************************************/
805 static void edge_bulk_out_cmd_callback(struct urb *urb)
807 struct edgeport_port *edge_port = urb->context;
808 struct tty_struct *tty;
809 int status = urb->status;
811 dbg("%s", __func__);
813 atomic_dec(&CmdUrbs);
814 dbg("%s - FREE URB %p (outstanding %d)", __func__,
815 urb, atomic_read(&CmdUrbs));
818 /* clean up the transfer buffer */
819 kfree(urb->transfer_buffer);
821 /* Free the command urb */
822 usb_free_urb(urb);
824 if (status) {
825 dbg("%s - nonzero write bulk status received: %d",
826 __func__, status);
827 return;
830 /* Get pointer to tty */
831 tty = tty_port_tty_get(&edge_port->port->port);
833 /* tell the tty driver that something has changed */
834 if (tty && edge_port->open)
835 tty_wakeup(tty);
836 tty_kref_put(tty);
838 /* we have completed the command */
839 edge_port->commandPending = false;
840 wake_up(&edge_port->wait_command);
844 /*****************************************************************************
845 * Driver tty interface functions
846 *****************************************************************************/
848 /*****************************************************************************
849 * SerialOpen
850 * this function is called by the tty driver when a port is opened
851 * If successful, we return 0
852 * Otherwise we return a negative error number.
853 *****************************************************************************/
854 static int edge_open(struct tty_struct *tty,
855 struct usb_serial_port *port, struct file *filp)
857 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
858 struct usb_serial *serial;
859 struct edgeport_serial *edge_serial;
860 int response;
862 dbg("%s - port %d", __func__, port->number);
864 if (edge_port == NULL)
865 return -ENODEV;
867 /* see if we've set up our endpoint info yet (can't set it up
868 in edge_startup as the structures were not set up at that time.) */
869 serial = port->serial;
870 edge_serial = usb_get_serial_data(serial);
871 if (edge_serial == NULL)
872 return -ENODEV;
873 if (edge_serial->interrupt_in_buffer == NULL) {
874 struct usb_serial_port *port0 = serial->port[0];
876 /* not set up yet, so do it now */
877 edge_serial->interrupt_in_buffer =
878 port0->interrupt_in_buffer;
879 edge_serial->interrupt_in_endpoint =
880 port0->interrupt_in_endpointAddress;
881 edge_serial->interrupt_read_urb = port0->interrupt_in_urb;
882 edge_serial->bulk_in_buffer = port0->bulk_in_buffer;
883 edge_serial->bulk_in_endpoint =
884 port0->bulk_in_endpointAddress;
885 edge_serial->read_urb = port0->read_urb;
886 edge_serial->bulk_out_endpoint =
887 port0->bulk_out_endpointAddress;
889 /* set up our interrupt urb */
890 usb_fill_int_urb(edge_serial->interrupt_read_urb,
891 serial->dev,
892 usb_rcvintpipe(serial->dev,
893 port0->interrupt_in_endpointAddress),
894 port0->interrupt_in_buffer,
895 edge_serial->interrupt_read_urb->transfer_buffer_length,
896 edge_interrupt_callback, edge_serial,
897 edge_serial->interrupt_read_urb->interval);
899 /* set up our bulk in urb */
900 usb_fill_bulk_urb(edge_serial->read_urb, serial->dev,
901 usb_rcvbulkpipe(serial->dev,
902 port0->bulk_in_endpointAddress),
903 port0->bulk_in_buffer,
904 edge_serial->read_urb->transfer_buffer_length,
905 edge_bulk_in_callback, edge_serial);
906 edge_serial->read_in_progress = false;
908 /* start interrupt read for this edgeport
909 * this interrupt will continue as long
910 * as the edgeport is connected */
911 response = usb_submit_urb(edge_serial->interrupt_read_urb,
912 GFP_KERNEL);
913 if (response) {
914 dev_err(&port->dev,
915 "%s - Error %d submitting control urb\n",
916 __func__, response);
920 /* initialize our wait queues */
921 init_waitqueue_head(&edge_port->wait_open);
922 init_waitqueue_head(&edge_port->wait_chase);
923 init_waitqueue_head(&edge_port->delta_msr_wait);
924 init_waitqueue_head(&edge_port->wait_command);
926 /* initialize our icount structure */
927 memset(&(edge_port->icount), 0x00, sizeof(edge_port->icount));
929 /* initialize our port settings */
930 edge_port->txCredits = 0; /* Can't send any data yet */
931 /* Must always set this bit to enable ints! */
932 edge_port->shadowMCR = MCR_MASTER_IE;
933 edge_port->chaseResponsePending = false;
935 /* send a open port command */
936 edge_port->openPending = true;
937 edge_port->open = false;
938 response = send_iosp_ext_cmd(edge_port, IOSP_CMD_OPEN_PORT, 0);
940 if (response < 0) {
941 dev_err(&port->dev, "%s - error sending open port command\n",
942 __func__);
943 edge_port->openPending = false;
944 return -ENODEV;
947 /* now wait for the port to be completely opened */
948 wait_event_timeout(edge_port->wait_open, !edge_port->openPending,
949 OPEN_TIMEOUT);
951 if (!edge_port->open) {
952 /* open timed out */
953 dbg("%s - open timedout", __func__);
954 edge_port->openPending = false;
955 return -ENODEV;
958 /* create the txfifo */
959 edge_port->txfifo.head = 0;
960 edge_port->txfifo.tail = 0;
961 edge_port->txfifo.count = 0;
962 edge_port->txfifo.size = edge_port->maxTxCredits;
963 edge_port->txfifo.fifo = kmalloc(edge_port->maxTxCredits, GFP_KERNEL);
965 if (!edge_port->txfifo.fifo) {
966 dbg("%s - no memory", __func__);
967 edge_close(port);
968 return -ENOMEM;
971 /* Allocate a URB for the write */
972 edge_port->write_urb = usb_alloc_urb(0, GFP_KERNEL);
973 edge_port->write_in_progress = false;
975 if (!edge_port->write_urb) {
976 dbg("%s - no memory", __func__);
977 edge_close(port);
978 return -ENOMEM;
981 dbg("%s(%d) - Initialize TX fifo to %d bytes",
982 __func__, port->number, edge_port->maxTxCredits);
984 dbg("%s exited", __func__);
986 return 0;
990 /************************************************************************
992 * block_until_chase_response
994 * This function will block the close until one of the following:
995 * 1. Response to our Chase comes from Edgeport
996 * 2. A timeout of 10 seconds without activity has expired
997 * (1K of Edgeport data @ 2400 baud ==> 4 sec to empty)
999 ************************************************************************/
1000 static void block_until_chase_response(struct edgeport_port *edge_port)
1002 DEFINE_WAIT(wait);
1003 __u16 lastCredits;
1004 int timeout = 1*HZ;
1005 int loop = 10;
1007 while (1) {
1008 /* Save Last credits */
1009 lastCredits = edge_port->txCredits;
1011 /* Did we get our Chase response */
1012 if (!edge_port->chaseResponsePending) {
1013 dbg("%s - Got Chase Response", __func__);
1015 /* did we get all of our credit back? */
1016 if (edge_port->txCredits == edge_port->maxTxCredits) {
1017 dbg("%s - Got all credits", __func__);
1018 return;
1022 /* Block the thread for a while */
1023 prepare_to_wait(&edge_port->wait_chase, &wait,
1024 TASK_UNINTERRUPTIBLE);
1025 schedule_timeout(timeout);
1026 finish_wait(&edge_port->wait_chase, &wait);
1028 if (lastCredits == edge_port->txCredits) {
1029 /* No activity.. count down. */
1030 loop--;
1031 if (loop == 0) {
1032 edge_port->chaseResponsePending = false;
1033 dbg("%s - Chase TIMEOUT", __func__);
1034 return;
1036 } else {
1037 /* Reset timeout value back to 10 seconds */
1038 dbg("%s - Last %d, Current %d", __func__,
1039 lastCredits, edge_port->txCredits);
1040 loop = 10;
1046 /************************************************************************
1048 * block_until_tx_empty
1050 * This function will block the close until one of the following:
1051 * 1. TX count are 0
1052 * 2. The edgeport has stopped
1053 * 3. A timeout of 3 seconds without activity has expired
1055 ************************************************************************/
1056 static void block_until_tx_empty(struct edgeport_port *edge_port)
1058 DEFINE_WAIT(wait);
1059 struct TxFifo *fifo = &edge_port->txfifo;
1060 __u32 lastCount;
1061 int timeout = HZ/10;
1062 int loop = 30;
1064 while (1) {
1065 /* Save Last count */
1066 lastCount = fifo->count;
1068 /* Is the Edgeport Buffer empty? */
1069 if (lastCount == 0) {
1070 dbg("%s - TX Buffer Empty", __func__);
1071 return;
1074 /* Block the thread for a while */
1075 prepare_to_wait(&edge_port->wait_chase, &wait,
1076 TASK_UNINTERRUPTIBLE);
1077 schedule_timeout(timeout);
1078 finish_wait(&edge_port->wait_chase, &wait);
1080 dbg("%s wait", __func__);
1082 if (lastCount == fifo->count) {
1083 /* No activity.. count down. */
1084 loop--;
1085 if (loop == 0) {
1086 dbg("%s - TIMEOUT", __func__);
1087 return;
1089 } else {
1090 /* Reset timeout value back to seconds */
1091 loop = 30;
1097 /*****************************************************************************
1098 * edge_close
1099 * this function is called by the tty driver when a port is closed
1100 *****************************************************************************/
1101 static void edge_close(struct usb_serial_port *port)
1103 struct edgeport_serial *edge_serial;
1104 struct edgeport_port *edge_port;
1105 int status;
1107 dbg("%s - port %d", __func__, port->number);
1109 edge_serial = usb_get_serial_data(port->serial);
1110 edge_port = usb_get_serial_port_data(port);
1111 if (edge_serial == NULL || edge_port == NULL)
1112 return;
1114 /* block until tx is empty */
1115 block_until_tx_empty(edge_port);
1117 edge_port->closePending = true;
1119 if ((!edge_serial->is_epic) ||
1120 ((edge_serial->is_epic) &&
1121 (edge_serial->epic_descriptor.Supports.IOSPChase))) {
1122 /* flush and chase */
1123 edge_port->chaseResponsePending = true;
1125 dbg("%s - Sending IOSP_CMD_CHASE_PORT", __func__);
1126 status = send_iosp_ext_cmd(edge_port, IOSP_CMD_CHASE_PORT, 0);
1127 if (status == 0)
1128 /* block until chase finished */
1129 block_until_chase_response(edge_port);
1130 else
1131 edge_port->chaseResponsePending = false;
1134 if ((!edge_serial->is_epic) ||
1135 ((edge_serial->is_epic) &&
1136 (edge_serial->epic_descriptor.Supports.IOSPClose))) {
1137 /* close the port */
1138 dbg("%s - Sending IOSP_CMD_CLOSE_PORT", __func__);
1139 send_iosp_ext_cmd(edge_port, IOSP_CMD_CLOSE_PORT, 0);
1142 /* port->close = true; */
1143 edge_port->closePending = false;
1144 edge_port->open = false;
1145 edge_port->openPending = false;
1147 usb_kill_urb(edge_port->write_urb);
1149 if (edge_port->write_urb) {
1150 /* if this urb had a transfer buffer already
1151 (old transfer) free it */
1152 kfree(edge_port->write_urb->transfer_buffer);
1153 usb_free_urb(edge_port->write_urb);
1154 edge_port->write_urb = NULL;
1156 kfree(edge_port->txfifo.fifo);
1157 edge_port->txfifo.fifo = NULL;
1159 dbg("%s exited", __func__);
1162 /*****************************************************************************
1163 * SerialWrite
1164 * this function is called by the tty driver when data should be written
1165 * to the port.
1166 * If successful, we return the number of bytes written, otherwise we
1167 * return a negative error number.
1168 *****************************************************************************/
1169 static int edge_write(struct tty_struct *tty, struct usb_serial_port *port,
1170 const unsigned char *data, int count)
1172 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1173 struct TxFifo *fifo;
1174 int copySize;
1175 int bytesleft;
1176 int firsthalf;
1177 int secondhalf;
1178 unsigned long flags;
1180 dbg("%s - port %d", __func__, port->number);
1182 if (edge_port == NULL)
1183 return -ENODEV;
1185 /* get a pointer to the Tx fifo */
1186 fifo = &edge_port->txfifo;
1188 spin_lock_irqsave(&edge_port->ep_lock, flags);
1190 /* calculate number of bytes to put in fifo */
1191 copySize = min((unsigned int)count,
1192 (edge_port->txCredits - fifo->count));
1194 dbg("%s(%d) of %d byte(s) Fifo room %d -- will copy %d bytes",
1195 __func__, port->number, count,
1196 edge_port->txCredits - fifo->count, copySize);
1198 /* catch writes of 0 bytes which the tty driver likes to give us,
1199 and when txCredits is empty */
1200 if (copySize == 0) {
1201 dbg("%s - copySize = Zero", __func__);
1202 goto finish_write;
1205 /* queue the data
1206 * since we can never overflow the buffer we do not have to check for a
1207 * full condition
1209 * the copy is done is two parts -- first fill to the end of the buffer
1210 * then copy the reset from the start of the buffer
1212 bytesleft = fifo->size - fifo->head;
1213 firsthalf = min(bytesleft, copySize);
1214 dbg("%s - copy %d bytes of %d into fifo ", __func__,
1215 firsthalf, bytesleft);
1217 /* now copy our data */
1218 memcpy(&fifo->fifo[fifo->head], data, firsthalf);
1219 usb_serial_debug_data(debug, &port->dev, __func__,
1220 firsthalf, &fifo->fifo[fifo->head]);
1222 /* update the index and size */
1223 fifo->head += firsthalf;
1224 fifo->count += firsthalf;
1226 /* wrap the index */
1227 if (fifo->head == fifo->size)
1228 fifo->head = 0;
1230 secondhalf = copySize-firsthalf;
1232 if (secondhalf) {
1233 dbg("%s - copy rest of data %d", __func__, secondhalf);
1234 memcpy(&fifo->fifo[fifo->head], &data[firsthalf], secondhalf);
1235 usb_serial_debug_data(debug, &port->dev, __func__,
1236 secondhalf, &fifo->fifo[fifo->head]);
1237 /* update the index and size */
1238 fifo->count += secondhalf;
1239 fifo->head += secondhalf;
1240 /* No need to check for wrap since we can not get to end of
1241 * the fifo in this part
1245 finish_write:
1246 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1248 send_more_port_data((struct edgeport_serial *)
1249 usb_get_serial_data(port->serial), edge_port);
1251 dbg("%s wrote %d byte(s) TxCredits %d, Fifo %d", __func__,
1252 copySize, edge_port->txCredits, fifo->count);
1254 return copySize;
1258 /************************************************************************
1260 * send_more_port_data()
1262 * This routine attempts to write additional UART transmit data
1263 * to a port over the USB bulk pipe. It is called (1) when new
1264 * data has been written to a port's TxBuffer from higher layers
1265 * (2) when the peripheral sends us additional TxCredits indicating
1266 * that it can accept more Tx data for a given port; and (3) when
1267 * a bulk write completes successfully and we want to see if we
1268 * can transmit more.
1270 ************************************************************************/
1271 static void send_more_port_data(struct edgeport_serial *edge_serial,
1272 struct edgeport_port *edge_port)
1274 struct TxFifo *fifo = &edge_port->txfifo;
1275 struct urb *urb;
1276 unsigned char *buffer;
1277 int status;
1278 int count;
1279 int bytesleft;
1280 int firsthalf;
1281 int secondhalf;
1282 unsigned long flags;
1284 dbg("%s(%d)", __func__, edge_port->port->number);
1286 spin_lock_irqsave(&edge_port->ep_lock, flags);
1288 if (edge_port->write_in_progress ||
1289 !edge_port->open ||
1290 (fifo->count == 0)) {
1291 dbg("%s(%d) EXIT - fifo %d, PendingWrite = %d",
1292 __func__, edge_port->port->number,
1293 fifo->count, edge_port->write_in_progress);
1294 goto exit_send;
1297 /* since the amount of data in the fifo will always fit into the
1298 * edgeport buffer we do not need to check the write length
1300 * Do we have enough credits for this port to make it worthwhile
1301 * to bother queueing a write. If it's too small, say a few bytes,
1302 * it's better to wait for more credits so we can do a larger write.
1304 if (edge_port->txCredits < EDGE_FW_GET_TX_CREDITS_SEND_THRESHOLD(edge_port->maxTxCredits, EDGE_FW_BULK_MAX_PACKET_SIZE)) {
1305 dbg("%s(%d) Not enough credit - fifo %d TxCredit %d",
1306 __func__, edge_port->port->number, fifo->count,
1307 edge_port->txCredits);
1308 goto exit_send;
1311 /* lock this write */
1312 edge_port->write_in_progress = true;
1314 /* get a pointer to the write_urb */
1315 urb = edge_port->write_urb;
1317 /* make sure transfer buffer is freed */
1318 kfree(urb->transfer_buffer);
1319 urb->transfer_buffer = NULL;
1321 /* build the data header for the buffer and port that we are about
1322 to send out */
1323 count = fifo->count;
1324 buffer = kmalloc(count+2, GFP_ATOMIC);
1325 if (buffer == NULL) {
1326 dev_err(&edge_port->port->dev,
1327 "%s - no more kernel memory...\n", __func__);
1328 edge_port->write_in_progress = false;
1329 goto exit_send;
1331 buffer[0] = IOSP_BUILD_DATA_HDR1(edge_port->port->number
1332 - edge_port->port->serial->minor, count);
1333 buffer[1] = IOSP_BUILD_DATA_HDR2(edge_port->port->number
1334 - edge_port->port->serial->minor, count);
1336 /* now copy our data */
1337 bytesleft = fifo->size - fifo->tail;
1338 firsthalf = min(bytesleft, count);
1339 memcpy(&buffer[2], &fifo->fifo[fifo->tail], firsthalf);
1340 fifo->tail += firsthalf;
1341 fifo->count -= firsthalf;
1342 if (fifo->tail == fifo->size)
1343 fifo->tail = 0;
1345 secondhalf = count-firsthalf;
1346 if (secondhalf) {
1347 memcpy(&buffer[2+firsthalf], &fifo->fifo[fifo->tail],
1348 secondhalf);
1349 fifo->tail += secondhalf;
1350 fifo->count -= secondhalf;
1353 if (count)
1354 usb_serial_debug_data(debug, &edge_port->port->dev,
1355 __func__, count, &buffer[2]);
1357 /* fill up the urb with all of our data and submit it */
1358 usb_fill_bulk_urb(urb, edge_serial->serial->dev,
1359 usb_sndbulkpipe(edge_serial->serial->dev,
1360 edge_serial->bulk_out_endpoint),
1361 buffer, count+2,
1362 edge_bulk_out_data_callback, edge_port);
1364 /* decrement the number of credits we have by the number we just sent */
1365 edge_port->txCredits -= count;
1366 edge_port->icount.tx += count;
1368 urb->dev = edge_serial->serial->dev;
1369 status = usb_submit_urb(urb, GFP_ATOMIC);
1370 if (status) {
1371 /* something went wrong */
1372 dev_err(&edge_port->port->dev,
1373 "%s - usb_submit_urb(write bulk) failed, status = %d, data lost\n",
1374 __func__, status);
1375 edge_port->write_in_progress = false;
1377 /* revert the credits as something bad happened. */
1378 edge_port->txCredits += count;
1379 edge_port->icount.tx -= count;
1381 dbg("%s wrote %d byte(s) TxCredit %d, Fifo %d",
1382 __func__, count, edge_port->txCredits, fifo->count);
1384 exit_send:
1385 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1389 /*****************************************************************************
1390 * edge_write_room
1391 * this function is called by the tty driver when it wants to know how
1392 * many bytes of data we can accept for a specific port. If successful,
1393 * we return the amount of room that we have for this port (the txCredits)
1394 * otherwise we return a negative error number.
1395 *****************************************************************************/
1396 static int edge_write_room(struct tty_struct *tty)
1398 struct usb_serial_port *port = tty->driver_data;
1399 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1400 int room;
1401 unsigned long flags;
1403 dbg("%s", __func__);
1405 if (edge_port == NULL)
1406 return 0;
1407 if (edge_port->closePending)
1408 return 0;
1410 dbg("%s - port %d", __func__, port->number);
1412 if (!edge_port->open) {
1413 dbg("%s - port not opened", __func__);
1414 return 0;
1417 /* total of both buffers is still txCredit */
1418 spin_lock_irqsave(&edge_port->ep_lock, flags);
1419 room = edge_port->txCredits - edge_port->txfifo.count;
1420 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1422 dbg("%s - returns %d", __func__, room);
1423 return room;
1427 /*****************************************************************************
1428 * edge_chars_in_buffer
1429 * this function is called by the tty driver when it wants to know how
1430 * many bytes of data we currently have outstanding in the port (data that
1431 * has been written, but hasn't made it out the port yet)
1432 * If successful, we return the number of bytes left to be written in the
1433 * system,
1434 * Otherwise we return a negative error number.
1435 *****************************************************************************/
1436 static int edge_chars_in_buffer(struct tty_struct *tty)
1438 struct usb_serial_port *port = tty->driver_data;
1439 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1440 int num_chars;
1441 unsigned long flags;
1443 dbg("%s", __func__);
1445 if (edge_port == NULL)
1446 return 0;
1447 if (edge_port->closePending)
1448 return 0;
1450 if (!edge_port->open) {
1451 dbg("%s - port not opened", __func__);
1452 return 0;
1455 spin_lock_irqsave(&edge_port->ep_lock, flags);
1456 num_chars = edge_port->maxTxCredits - edge_port->txCredits +
1457 edge_port->txfifo.count;
1458 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1459 if (num_chars) {
1460 dbg("%s(port %d) - returns %d", __func__,
1461 port->number, num_chars);
1464 return num_chars;
1468 /*****************************************************************************
1469 * SerialThrottle
1470 * this function is called by the tty driver when it wants to stop the data
1471 * being read from the port.
1472 *****************************************************************************/
1473 static void edge_throttle(struct tty_struct *tty)
1475 struct usb_serial_port *port = tty->driver_data;
1476 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1477 int status;
1479 dbg("%s - port %d", __func__, port->number);
1481 if (edge_port == NULL)
1482 return;
1484 if (!edge_port->open) {
1485 dbg("%s - port not opened", __func__);
1486 return;
1489 /* if we are implementing XON/XOFF, send the stop character */
1490 if (I_IXOFF(tty)) {
1491 unsigned char stop_char = STOP_CHAR(tty);
1492 status = edge_write(tty, port, &stop_char, 1);
1493 if (status <= 0)
1494 return;
1497 /* if we are implementing RTS/CTS, toggle that line */
1498 if (tty->termios->c_cflag & CRTSCTS) {
1499 edge_port->shadowMCR &= ~MCR_RTS;
1500 status = send_cmd_write_uart_register(edge_port, MCR,
1501 edge_port->shadowMCR);
1502 if (status != 0)
1503 return;
1506 return;
1510 /*****************************************************************************
1511 * edge_unthrottle
1512 * this function is called by the tty driver when it wants to resume the
1513 * data being read from the port (called after SerialThrottle is called)
1514 *****************************************************************************/
1515 static void edge_unthrottle(struct tty_struct *tty)
1517 struct usb_serial_port *port = tty->driver_data;
1518 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1519 int status;
1521 dbg("%s - port %d", __func__, port->number);
1523 if (edge_port == NULL)
1524 return;
1526 if (!edge_port->open) {
1527 dbg("%s - port not opened", __func__);
1528 return;
1531 /* if we are implementing XON/XOFF, send the start character */
1532 if (I_IXOFF(tty)) {
1533 unsigned char start_char = START_CHAR(tty);
1534 status = edge_write(tty, port, &start_char, 1);
1535 if (status <= 0)
1536 return;
1538 /* if we are implementing RTS/CTS, toggle that line */
1539 if (tty->termios->c_cflag & CRTSCTS) {
1540 edge_port->shadowMCR |= MCR_RTS;
1541 send_cmd_write_uart_register(edge_port, MCR,
1542 edge_port->shadowMCR);
1547 /*****************************************************************************
1548 * SerialSetTermios
1549 * this function is called by the tty driver when it wants to change
1550 * the termios structure
1551 *****************************************************************************/
1552 static void edge_set_termios(struct tty_struct *tty,
1553 struct usb_serial_port *port, struct ktermios *old_termios)
1555 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1556 unsigned int cflag;
1558 cflag = tty->termios->c_cflag;
1559 dbg("%s - clfag %08x iflag %08x", __func__,
1560 tty->termios->c_cflag, tty->termios->c_iflag);
1561 dbg("%s - old clfag %08x old iflag %08x", __func__,
1562 old_termios->c_cflag, old_termios->c_iflag);
1564 dbg("%s - port %d", __func__, port->number);
1566 if (edge_port == NULL)
1567 return;
1569 if (!edge_port->open) {
1570 dbg("%s - port not opened", __func__);
1571 return;
1574 /* change the port settings to the new ones specified */
1575 change_port_settings(tty, edge_port, old_termios);
1579 /*****************************************************************************
1580 * get_lsr_info - get line status register info
1582 * Purpose: Let user call ioctl() to get info when the UART physically
1583 * is emptied. On bus types like RS485, the transmitter must
1584 * release the bus after transmitting. This must be done when
1585 * the transmit shift register is empty, not be done when the
1586 * transmit holding register is empty. This functionality
1587 * allows an RS485 driver to be written in user space.
1588 *****************************************************************************/
1589 static int get_lsr_info(struct edgeport_port *edge_port,
1590 unsigned int __user *value)
1592 unsigned int result = 0;
1593 unsigned long flags;
1595 spin_lock_irqsave(&edge_port->ep_lock, flags);
1596 if (edge_port->maxTxCredits == edge_port->txCredits &&
1597 edge_port->txfifo.count == 0) {
1598 dbg("%s -- Empty", __func__);
1599 result = TIOCSER_TEMT;
1601 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1603 if (copy_to_user(value, &result, sizeof(int)))
1604 return -EFAULT;
1605 return 0;
1608 static int edge_tiocmset(struct tty_struct *tty, struct file *file,
1609 unsigned int set, unsigned int clear)
1611 struct usb_serial_port *port = tty->driver_data;
1612 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1613 unsigned int mcr;
1615 dbg("%s - port %d", __func__, port->number);
1617 mcr = edge_port->shadowMCR;
1618 if (set & TIOCM_RTS)
1619 mcr |= MCR_RTS;
1620 if (set & TIOCM_DTR)
1621 mcr |= MCR_DTR;
1622 if (set & TIOCM_LOOP)
1623 mcr |= MCR_LOOPBACK;
1625 if (clear & TIOCM_RTS)
1626 mcr &= ~MCR_RTS;
1627 if (clear & TIOCM_DTR)
1628 mcr &= ~MCR_DTR;
1629 if (clear & TIOCM_LOOP)
1630 mcr &= ~MCR_LOOPBACK;
1632 edge_port->shadowMCR = mcr;
1634 send_cmd_write_uart_register(edge_port, MCR, edge_port->shadowMCR);
1636 return 0;
1639 static int edge_tiocmget(struct tty_struct *tty, struct file *file)
1641 struct usb_serial_port *port = tty->driver_data;
1642 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1643 unsigned int result = 0;
1644 unsigned int msr;
1645 unsigned int mcr;
1647 dbg("%s - port %d", __func__, port->number);
1649 msr = edge_port->shadowMSR;
1650 mcr = edge_port->shadowMCR;
1651 result = ((mcr & MCR_DTR) ? TIOCM_DTR: 0) /* 0x002 */
1652 | ((mcr & MCR_RTS) ? TIOCM_RTS: 0) /* 0x004 */
1653 | ((msr & EDGEPORT_MSR_CTS) ? TIOCM_CTS: 0) /* 0x020 */
1654 | ((msr & EDGEPORT_MSR_CD) ? TIOCM_CAR: 0) /* 0x040 */
1655 | ((msr & EDGEPORT_MSR_RI) ? TIOCM_RI: 0) /* 0x080 */
1656 | ((msr & EDGEPORT_MSR_DSR) ? TIOCM_DSR: 0); /* 0x100 */
1659 dbg("%s -- %x", __func__, result);
1661 return result;
1664 static int get_serial_info(struct edgeport_port *edge_port,
1665 struct serial_struct __user *retinfo)
1667 struct serial_struct tmp;
1669 if (!retinfo)
1670 return -EFAULT;
1672 memset(&tmp, 0, sizeof(tmp));
1674 tmp.type = PORT_16550A;
1675 tmp.line = edge_port->port->serial->minor;
1676 tmp.port = edge_port->port->number;
1677 tmp.irq = 0;
1678 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
1679 tmp.xmit_fifo_size = edge_port->maxTxCredits;
1680 tmp.baud_base = 9600;
1681 tmp.close_delay = 5*HZ;
1682 tmp.closing_wait = 30*HZ;
1684 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1685 return -EFAULT;
1686 return 0;
1691 /*****************************************************************************
1692 * SerialIoctl
1693 * this function handles any ioctl calls to the driver
1694 *****************************************************************************/
1695 static int edge_ioctl(struct tty_struct *tty, struct file *file,
1696 unsigned int cmd, unsigned long arg)
1698 struct usb_serial_port *port = tty->driver_data;
1699 DEFINE_WAIT(wait);
1700 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1701 struct async_icount cnow;
1702 struct async_icount cprev;
1703 struct serial_icounter_struct icount;
1705 dbg("%s - port %d, cmd = 0x%x", __func__, port->number, cmd);
1707 switch (cmd) {
1708 case TIOCSERGETLSR:
1709 dbg("%s (%d) TIOCSERGETLSR", __func__, port->number);
1710 return get_lsr_info(edge_port, (unsigned int __user *) arg);
1712 case TIOCGSERIAL:
1713 dbg("%s (%d) TIOCGSERIAL", __func__, port->number);
1714 return get_serial_info(edge_port, (struct serial_struct __user *) arg);
1716 case TIOCMIWAIT:
1717 dbg("%s (%d) TIOCMIWAIT", __func__, port->number);
1718 cprev = edge_port->icount;
1719 while (1) {
1720 prepare_to_wait(&edge_port->delta_msr_wait,
1721 &wait, TASK_INTERRUPTIBLE);
1722 schedule();
1723 finish_wait(&edge_port->delta_msr_wait, &wait);
1724 /* see if a signal did it */
1725 if (signal_pending(current))
1726 return -ERESTARTSYS;
1727 cnow = edge_port->icount;
1728 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
1729 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
1730 return -EIO; /* no change => error */
1731 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1732 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1733 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1734 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
1735 return 0;
1737 cprev = cnow;
1739 /* NOTREACHED */
1740 break;
1742 case TIOCGICOUNT:
1743 cnow = edge_port->icount;
1744 memset(&icount, 0, sizeof(icount));
1745 icount.cts = cnow.cts;
1746 icount.dsr = cnow.dsr;
1747 icount.rng = cnow.rng;
1748 icount.dcd = cnow.dcd;
1749 icount.rx = cnow.rx;
1750 icount.tx = cnow.tx;
1751 icount.frame = cnow.frame;
1752 icount.overrun = cnow.overrun;
1753 icount.parity = cnow.parity;
1754 icount.brk = cnow.brk;
1755 icount.buf_overrun = cnow.buf_overrun;
1757 dbg("%s (%d) TIOCGICOUNT RX=%d, TX=%d",
1758 __func__, port->number, icount.rx, icount.tx);
1759 if (copy_to_user((void __user *)arg, &icount, sizeof(icount)))
1760 return -EFAULT;
1761 return 0;
1763 return -ENOIOCTLCMD;
1767 /*****************************************************************************
1768 * SerialBreak
1769 * this function sends a break to the port
1770 *****************************************************************************/
1771 static void edge_break(struct tty_struct *tty, int break_state)
1773 struct usb_serial_port *port = tty->driver_data;
1774 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1775 struct edgeport_serial *edge_serial = usb_get_serial_data(port->serial);
1776 int status;
1778 if ((!edge_serial->is_epic) ||
1779 ((edge_serial->is_epic) &&
1780 (edge_serial->epic_descriptor.Supports.IOSPChase))) {
1781 /* flush and chase */
1782 edge_port->chaseResponsePending = true;
1784 dbg("%s - Sending IOSP_CMD_CHASE_PORT", __func__);
1785 status = send_iosp_ext_cmd(edge_port, IOSP_CMD_CHASE_PORT, 0);
1786 if (status == 0) {
1787 /* block until chase finished */
1788 block_until_chase_response(edge_port);
1789 } else {
1790 edge_port->chaseResponsePending = false;
1794 if ((!edge_serial->is_epic) ||
1795 ((edge_serial->is_epic) &&
1796 (edge_serial->epic_descriptor.Supports.IOSPSetClrBreak))) {
1797 if (break_state == -1) {
1798 dbg("%s - Sending IOSP_CMD_SET_BREAK", __func__);
1799 status = send_iosp_ext_cmd(edge_port,
1800 IOSP_CMD_SET_BREAK, 0);
1801 } else {
1802 dbg("%s - Sending IOSP_CMD_CLEAR_BREAK", __func__);
1803 status = send_iosp_ext_cmd(edge_port,
1804 IOSP_CMD_CLEAR_BREAK, 0);
1806 if (status)
1807 dbg("%s - error sending break set/clear command.",
1808 __func__);
1811 return;
1815 /*****************************************************************************
1816 * process_rcvd_data
1817 * this function handles the data received on the bulk in pipe.
1818 *****************************************************************************/
1819 static void process_rcvd_data(struct edgeport_serial *edge_serial,
1820 unsigned char *buffer, __u16 bufferLength)
1822 struct usb_serial_port *port;
1823 struct edgeport_port *edge_port;
1824 struct tty_struct *tty;
1825 __u16 lastBufferLength;
1826 __u16 rxLen;
1828 dbg("%s", __func__);
1830 lastBufferLength = bufferLength + 1;
1832 while (bufferLength > 0) {
1833 /* failsafe incase we get a message that we don't understand */
1834 if (lastBufferLength == bufferLength) {
1835 dbg("%s - stuck in loop, exiting it.", __func__);
1836 break;
1838 lastBufferLength = bufferLength;
1840 switch (edge_serial->rxState) {
1841 case EXPECT_HDR1:
1842 edge_serial->rxHeader1 = *buffer;
1843 ++buffer;
1844 --bufferLength;
1846 if (bufferLength == 0) {
1847 edge_serial->rxState = EXPECT_HDR2;
1848 break;
1850 /* otherwise, drop on through */
1851 case EXPECT_HDR2:
1852 edge_serial->rxHeader2 = *buffer;
1853 ++buffer;
1854 --bufferLength;
1856 dbg("%s - Hdr1=%02X Hdr2=%02X", __func__,
1857 edge_serial->rxHeader1, edge_serial->rxHeader2);
1858 /* Process depending on whether this header is
1859 * data or status */
1861 if (IS_CMD_STAT_HDR(edge_serial->rxHeader1)) {
1862 /* Decode this status header and go to
1863 * EXPECT_HDR1 (if we can process the status
1864 * with only 2 bytes), or go to EXPECT_HDR3 to
1865 * get the third byte. */
1866 edge_serial->rxPort =
1867 IOSP_GET_HDR_PORT(edge_serial->rxHeader1);
1868 edge_serial->rxStatusCode =
1869 IOSP_GET_STATUS_CODE(
1870 edge_serial->rxHeader1);
1872 if (!IOSP_STATUS_IS_2BYTE(
1873 edge_serial->rxStatusCode)) {
1874 /* This status needs additional bytes.
1875 * Save what we have and then wait for
1876 * more data.
1878 edge_serial->rxStatusParam
1879 = edge_serial->rxHeader2;
1880 edge_serial->rxState = EXPECT_HDR3;
1881 break;
1883 /* We have all the header bytes, process the
1884 status now */
1885 process_rcvd_status(edge_serial,
1886 edge_serial->rxHeader2, 0);
1887 edge_serial->rxState = EXPECT_HDR1;
1888 break;
1889 } else {
1890 edge_serial->rxPort =
1891 IOSP_GET_HDR_PORT(edge_serial->rxHeader1);
1892 edge_serial->rxBytesRemaining =
1893 IOSP_GET_HDR_DATA_LEN(
1894 edge_serial->rxHeader1,
1895 edge_serial->rxHeader2);
1896 dbg("%s - Data for Port %u Len %u",
1897 __func__,
1898 edge_serial->rxPort,
1899 edge_serial->rxBytesRemaining);
1901 /* ASSERT(DevExt->RxPort < DevExt->NumPorts);
1902 * ASSERT(DevExt->RxBytesRemaining <
1903 * IOSP_MAX_DATA_LENGTH);
1906 if (bufferLength == 0) {
1907 edge_serial->rxState = EXPECT_DATA;
1908 break;
1910 /* Else, drop through */
1912 case EXPECT_DATA: /* Expect data */
1913 if (bufferLength < edge_serial->rxBytesRemaining) {
1914 rxLen = bufferLength;
1915 /* Expect data to start next buffer */
1916 edge_serial->rxState = EXPECT_DATA;
1917 } else {
1918 /* BufLen >= RxBytesRemaining */
1919 rxLen = edge_serial->rxBytesRemaining;
1920 /* Start another header next time */
1921 edge_serial->rxState = EXPECT_HDR1;
1924 bufferLength -= rxLen;
1925 edge_serial->rxBytesRemaining -= rxLen;
1927 /* spit this data back into the tty driver if this
1928 port is open */
1929 if (rxLen) {
1930 port = edge_serial->serial->port[
1931 edge_serial->rxPort];
1932 edge_port = usb_get_serial_port_data(port);
1933 if (edge_port->open) {
1934 tty = tty_port_tty_get(
1935 &edge_port->port->port);
1936 if (tty) {
1937 dbg("%s - Sending %d bytes to TTY for port %d",
1938 __func__, rxLen, edge_serial->rxPort);
1939 edge_tty_recv(&edge_serial->serial->dev->dev, tty, buffer, rxLen);
1940 tty_kref_put(tty);
1942 edge_port->icount.rx += rxLen;
1944 buffer += rxLen;
1946 break;
1948 case EXPECT_HDR3: /* Expect 3rd byte of status header */
1949 edge_serial->rxHeader3 = *buffer;
1950 ++buffer;
1951 --bufferLength;
1953 /* We have all the header bytes, process the
1954 status now */
1955 process_rcvd_status(edge_serial,
1956 edge_serial->rxStatusParam,
1957 edge_serial->rxHeader3);
1958 edge_serial->rxState = EXPECT_HDR1;
1959 break;
1965 /*****************************************************************************
1966 * process_rcvd_status
1967 * this function handles the any status messages received on the
1968 * bulk in pipe.
1969 *****************************************************************************/
1970 static void process_rcvd_status(struct edgeport_serial *edge_serial,
1971 __u8 byte2, __u8 byte3)
1973 struct usb_serial_port *port;
1974 struct edgeport_port *edge_port;
1975 struct tty_struct *tty;
1976 __u8 code = edge_serial->rxStatusCode;
1978 /* switch the port pointer to the one being currently talked about */
1979 port = edge_serial->serial->port[edge_serial->rxPort];
1980 edge_port = usb_get_serial_port_data(port);
1981 if (edge_port == NULL) {
1982 dev_err(&edge_serial->serial->dev->dev,
1983 "%s - edge_port == NULL for port %d\n",
1984 __func__, edge_serial->rxPort);
1985 return;
1988 dbg("%s - port %d", __func__, edge_serial->rxPort);
1990 if (code == IOSP_EXT_STATUS) {
1991 switch (byte2) {
1992 case IOSP_EXT_STATUS_CHASE_RSP:
1993 /* we want to do EXT status regardless of port
1994 * open/closed */
1995 dbg("%s - Port %u EXT CHASE_RSP Data = %02x",
1996 __func__, edge_serial->rxPort, byte3);
1997 /* Currently, the only EXT_STATUS is Chase, so process
1998 * here instead of one more call to one more subroutine
1999 * If/when more EXT_STATUS, there'll be more work to do
2000 * Also, we currently clear flag and close the port
2001 * regardless of content of above's Byte3.
2002 * We could choose to do something else when Byte3 says
2003 * Timeout on Chase from Edgeport, like wait longer in
2004 * block_until_chase_response, but for now we don't.
2006 edge_port->chaseResponsePending = false;
2007 wake_up(&edge_port->wait_chase);
2008 return;
2010 case IOSP_EXT_STATUS_RX_CHECK_RSP:
2011 dbg("%s ========== Port %u CHECK_RSP Sequence = %02x =============\n", __func__, edge_serial->rxPort, byte3);
2012 /* Port->RxCheckRsp = true; */
2013 return;
2017 if (code == IOSP_STATUS_OPEN_RSP) {
2018 edge_port->txCredits = GET_TX_BUFFER_SIZE(byte3);
2019 edge_port->maxTxCredits = edge_port->txCredits;
2020 dbg("%s - Port %u Open Response Inital MSR = %02x TxBufferSize = %d", __func__, edge_serial->rxPort, byte2, edge_port->txCredits);
2021 handle_new_msr(edge_port, byte2);
2023 /* send the current line settings to the port so we are
2024 in sync with any further termios calls */
2025 tty = tty_port_tty_get(&edge_port->port->port);
2026 if (tty) {
2027 change_port_settings(tty,
2028 edge_port, tty->termios);
2029 tty_kref_put(tty);
2032 /* we have completed the open */
2033 edge_port->openPending = false;
2034 edge_port->open = true;
2035 wake_up(&edge_port->wait_open);
2036 return;
2039 /* If port is closed, silently discard all rcvd status. We can
2040 * have cases where buffered status is received AFTER the close
2041 * port command is sent to the Edgeport.
2043 if (!edge_port->open || edge_port->closePending)
2044 return;
2046 switch (code) {
2047 /* Not currently sent by Edgeport */
2048 case IOSP_STATUS_LSR:
2049 dbg("%s - Port %u LSR Status = %02x",
2050 __func__, edge_serial->rxPort, byte2);
2051 handle_new_lsr(edge_port, false, byte2, 0);
2052 break;
2054 case IOSP_STATUS_LSR_DATA:
2055 dbg("%s - Port %u LSR Status = %02x, Data = %02x",
2056 __func__, edge_serial->rxPort, byte2, byte3);
2057 /* byte2 is LSR Register */
2058 /* byte3 is broken data byte */
2059 handle_new_lsr(edge_port, true, byte2, byte3);
2060 break;
2062 * case IOSP_EXT_4_STATUS:
2063 * dbg("%s - Port %u LSR Status = %02x Data = %02x",
2064 * __func__, edge_serial->rxPort, byte2, byte3);
2065 * break;
2067 case IOSP_STATUS_MSR:
2068 dbg("%s - Port %u MSR Status = %02x",
2069 __func__, edge_serial->rxPort, byte2);
2071 * Process this new modem status and generate appropriate
2072 * events, etc, based on the new status. This routine
2073 * also saves the MSR in Port->ShadowMsr.
2075 handle_new_msr(edge_port, byte2);
2076 break;
2078 default:
2079 dbg("%s - Unrecognized IOSP status code %u\n", __func__, code);
2080 break;
2082 return;
2086 /*****************************************************************************
2087 * edge_tty_recv
2088 * this function passes data on to the tty flip buffer
2089 *****************************************************************************/
2090 static void edge_tty_recv(struct device *dev, struct tty_struct *tty,
2091 unsigned char *data, int length)
2093 int cnt;
2095 do {
2096 cnt = tty_buffer_request_room(tty, length);
2097 if (cnt < length) {
2098 dev_err(dev, "%s - dropping data, %d bytes lost\n",
2099 __func__, length - cnt);
2100 if (cnt == 0)
2101 break;
2103 tty_insert_flip_string(tty, data, cnt);
2104 data += cnt;
2105 length -= cnt;
2106 } while (length > 0);
2108 tty_flip_buffer_push(tty);
2112 /*****************************************************************************
2113 * handle_new_msr
2114 * this function handles any change to the msr register for a port.
2115 *****************************************************************************/
2116 static void handle_new_msr(struct edgeport_port *edge_port, __u8 newMsr)
2118 struct async_icount *icount;
2120 dbg("%s %02x", __func__, newMsr);
2122 if (newMsr & (EDGEPORT_MSR_DELTA_CTS | EDGEPORT_MSR_DELTA_DSR |
2123 EDGEPORT_MSR_DELTA_RI | EDGEPORT_MSR_DELTA_CD)) {
2124 icount = &edge_port->icount;
2126 /* update input line counters */
2127 if (newMsr & EDGEPORT_MSR_DELTA_CTS)
2128 icount->cts++;
2129 if (newMsr & EDGEPORT_MSR_DELTA_DSR)
2130 icount->dsr++;
2131 if (newMsr & EDGEPORT_MSR_DELTA_CD)
2132 icount->dcd++;
2133 if (newMsr & EDGEPORT_MSR_DELTA_RI)
2134 icount->rng++;
2135 wake_up_interruptible(&edge_port->delta_msr_wait);
2138 /* Save the new modem status */
2139 edge_port->shadowMSR = newMsr & 0xf0;
2141 return;
2145 /*****************************************************************************
2146 * handle_new_lsr
2147 * this function handles any change to the lsr register for a port.
2148 *****************************************************************************/
2149 static void handle_new_lsr(struct edgeport_port *edge_port, __u8 lsrData,
2150 __u8 lsr, __u8 data)
2152 __u8 newLsr = (__u8) (lsr & (__u8)
2153 (LSR_OVER_ERR | LSR_PAR_ERR | LSR_FRM_ERR | LSR_BREAK));
2154 struct async_icount *icount;
2156 dbg("%s - %02x", __func__, newLsr);
2158 edge_port->shadowLSR = lsr;
2160 if (newLsr & LSR_BREAK) {
2162 * Parity and Framing errors only count if they
2163 * occur exclusive of a break being
2164 * received.
2166 newLsr &= (__u8)(LSR_OVER_ERR | LSR_BREAK);
2169 /* Place LSR data byte into Rx buffer */
2170 if (lsrData) {
2171 struct tty_struct *tty =
2172 tty_port_tty_get(&edge_port->port->port);
2173 if (tty) {
2174 edge_tty_recv(&edge_port->port->dev, tty, &data, 1);
2175 tty_kref_put(tty);
2178 /* update input line counters */
2179 icount = &edge_port->icount;
2180 if (newLsr & LSR_BREAK)
2181 icount->brk++;
2182 if (newLsr & LSR_OVER_ERR)
2183 icount->overrun++;
2184 if (newLsr & LSR_PAR_ERR)
2185 icount->parity++;
2186 if (newLsr & LSR_FRM_ERR)
2187 icount->frame++;
2189 return;
2193 /****************************************************************************
2194 * sram_write
2195 * writes a number of bytes to the Edgeport device's sram starting at the
2196 * given address.
2197 * If successful returns the number of bytes written, otherwise it returns
2198 * a negative error number of the problem.
2199 ****************************************************************************/
2200 static int sram_write(struct usb_serial *serial, __u16 extAddr, __u16 addr,
2201 __u16 length, const __u8 *data)
2203 int result;
2204 __u16 current_length;
2205 unsigned char *transfer_buffer;
2207 dbg("%s - %x, %x, %d", __func__, extAddr, addr, length);
2209 transfer_buffer = kmalloc(64, GFP_KERNEL);
2210 if (!transfer_buffer) {
2211 dev_err(&serial->dev->dev, "%s - kmalloc(%d) failed.\n",
2212 __func__, 64);
2213 return -ENOMEM;
2216 /* need to split these writes up into 64 byte chunks */
2217 result = 0;
2218 while (length > 0) {
2219 if (length > 64)
2220 current_length = 64;
2221 else
2222 current_length = length;
2224 /* dbg("%s - writing %x, %x, %d", __func__,
2225 extAddr, addr, current_length); */
2226 memcpy(transfer_buffer, data, current_length);
2227 result = usb_control_msg(serial->dev,
2228 usb_sndctrlpipe(serial->dev, 0),
2229 USB_REQUEST_ION_WRITE_RAM,
2230 0x40, addr, extAddr, transfer_buffer,
2231 current_length, 300);
2232 if (result < 0)
2233 break;
2234 length -= current_length;
2235 addr += current_length;
2236 data += current_length;
2239 kfree(transfer_buffer);
2240 return result;
2244 /****************************************************************************
2245 * rom_write
2246 * writes a number of bytes to the Edgeport device's ROM starting at the
2247 * given address.
2248 * If successful returns the number of bytes written, otherwise it returns
2249 * a negative error number of the problem.
2250 ****************************************************************************/
2251 static int rom_write(struct usb_serial *serial, __u16 extAddr, __u16 addr,
2252 __u16 length, const __u8 *data)
2254 int result;
2255 __u16 current_length;
2256 unsigned char *transfer_buffer;
2258 /* dbg("%s - %x, %x, %d", __func__, extAddr, addr, length); */
2260 transfer_buffer = kmalloc(64, GFP_KERNEL);
2261 if (!transfer_buffer) {
2262 dev_err(&serial->dev->dev, "%s - kmalloc(%d) failed.\n",
2263 __func__, 64);
2264 return -ENOMEM;
2267 /* need to split these writes up into 64 byte chunks */
2268 result = 0;
2269 while (length > 0) {
2270 if (length > 64)
2271 current_length = 64;
2272 else
2273 current_length = length;
2274 /* dbg("%s - writing %x, %x, %d", __func__,
2275 extAddr, addr, current_length); */
2276 memcpy(transfer_buffer, data, current_length);
2277 result = usb_control_msg(serial->dev,
2278 usb_sndctrlpipe(serial->dev, 0),
2279 USB_REQUEST_ION_WRITE_ROM, 0x40,
2280 addr, extAddr,
2281 transfer_buffer, current_length, 300);
2282 if (result < 0)
2283 break;
2284 length -= current_length;
2285 addr += current_length;
2286 data += current_length;
2289 kfree(transfer_buffer);
2290 return result;
2294 /****************************************************************************
2295 * rom_read
2296 * reads a number of bytes from the Edgeport device starting at the given
2297 * address.
2298 * If successful returns the number of bytes read, otherwise it returns
2299 * a negative error number of the problem.
2300 ****************************************************************************/
2301 static int rom_read(struct usb_serial *serial, __u16 extAddr,
2302 __u16 addr, __u16 length, __u8 *data)
2304 int result;
2305 __u16 current_length;
2306 unsigned char *transfer_buffer;
2308 dbg("%s - %x, %x, %d", __func__, extAddr, addr, length);
2310 transfer_buffer = kmalloc(64, GFP_KERNEL);
2311 if (!transfer_buffer) {
2312 dev_err(&serial->dev->dev,
2313 "%s - kmalloc(%d) failed.\n", __func__, 64);
2314 return -ENOMEM;
2317 /* need to split these reads up into 64 byte chunks */
2318 result = 0;
2319 while (length > 0) {
2320 if (length > 64)
2321 current_length = 64;
2322 else
2323 current_length = length;
2324 /* dbg("%s - %x, %x, %d", __func__,
2325 extAddr, addr, current_length); */
2326 result = usb_control_msg(serial->dev,
2327 usb_rcvctrlpipe(serial->dev, 0),
2328 USB_REQUEST_ION_READ_ROM,
2329 0xC0, addr, extAddr, transfer_buffer,
2330 current_length, 300);
2331 if (result < 0)
2332 break;
2333 memcpy(data, transfer_buffer, current_length);
2334 length -= current_length;
2335 addr += current_length;
2336 data += current_length;
2339 kfree(transfer_buffer);
2340 return result;
2344 /****************************************************************************
2345 * send_iosp_ext_cmd
2346 * Is used to send a IOSP message to the Edgeport device
2347 ****************************************************************************/
2348 static int send_iosp_ext_cmd(struct edgeport_port *edge_port,
2349 __u8 command, __u8 param)
2351 unsigned char *buffer;
2352 unsigned char *currentCommand;
2353 int length = 0;
2354 int status = 0;
2356 dbg("%s - %d, %d", __func__, command, param);
2358 buffer = kmalloc(10, GFP_ATOMIC);
2359 if (!buffer) {
2360 dev_err(&edge_port->port->dev,
2361 "%s - kmalloc(%d) failed.\n", __func__, 10);
2362 return -ENOMEM;
2365 currentCommand = buffer;
2367 MAKE_CMD_EXT_CMD(&currentCommand, &length,
2368 edge_port->port->number - edge_port->port->serial->minor,
2369 command, param);
2371 status = write_cmd_usb(edge_port, buffer, length);
2372 if (status) {
2373 /* something bad happened, let's free up the memory */
2374 kfree(buffer);
2377 return status;
2381 /*****************************************************************************
2382 * write_cmd_usb
2383 * this function writes the given buffer out to the bulk write endpoint.
2384 *****************************************************************************/
2385 static int write_cmd_usb(struct edgeport_port *edge_port,
2386 unsigned char *buffer, int length)
2388 struct edgeport_serial *edge_serial =
2389 usb_get_serial_data(edge_port->port->serial);
2390 int status = 0;
2391 struct urb *urb;
2392 int timeout;
2394 usb_serial_debug_data(debug, &edge_port->port->dev,
2395 __func__, length, buffer);
2397 /* Allocate our next urb */
2398 urb = usb_alloc_urb(0, GFP_ATOMIC);
2399 if (!urb)
2400 return -ENOMEM;
2402 atomic_inc(&CmdUrbs);
2403 dbg("%s - ALLOCATE URB %p (outstanding %d)",
2404 __func__, urb, atomic_read(&CmdUrbs));
2406 usb_fill_bulk_urb(urb, edge_serial->serial->dev,
2407 usb_sndbulkpipe(edge_serial->serial->dev,
2408 edge_serial->bulk_out_endpoint),
2409 buffer, length, edge_bulk_out_cmd_callback, edge_port);
2411 edge_port->commandPending = true;
2412 status = usb_submit_urb(urb, GFP_ATOMIC);
2414 if (status) {
2415 /* something went wrong */
2416 dev_err(&edge_port->port->dev,
2417 "%s - usb_submit_urb(write command) failed, status = %d\n",
2418 __func__, status);
2419 usb_kill_urb(urb);
2420 usb_free_urb(urb);
2421 atomic_dec(&CmdUrbs);
2422 return status;
2425 /* wait for command to finish */
2426 timeout = COMMAND_TIMEOUT;
2427 #if 0
2428 wait_event(&edge_port->wait_command, !edge_port->commandPending);
2430 if (edge_port->commandPending) {
2431 /* command timed out */
2432 dbg("%s - command timed out", __func__);
2433 status = -EINVAL;
2435 #endif
2436 return status;
2440 /*****************************************************************************
2441 * send_cmd_write_baud_rate
2442 * this function sends the proper command to change the baud rate of the
2443 * specified port.
2444 *****************************************************************************/
2445 static int send_cmd_write_baud_rate(struct edgeport_port *edge_port,
2446 int baudRate)
2448 struct edgeport_serial *edge_serial =
2449 usb_get_serial_data(edge_port->port->serial);
2450 unsigned char *cmdBuffer;
2451 unsigned char *currCmd;
2452 int cmdLen = 0;
2453 int divisor;
2454 int status;
2455 unsigned char number =
2456 edge_port->port->number - edge_port->port->serial->minor;
2458 if (edge_serial->is_epic &&
2459 !edge_serial->epic_descriptor.Supports.IOSPSetBaudRate) {
2460 dbg("SendCmdWriteBaudRate - NOT Setting baud rate for port = %d, baud = %d",
2461 edge_port->port->number, baudRate);
2462 return 0;
2465 dbg("%s - port = %d, baud = %d", __func__,
2466 edge_port->port->number, baudRate);
2468 status = calc_baud_rate_divisor(baudRate, &divisor);
2469 if (status) {
2470 dev_err(&edge_port->port->dev, "%s - bad baud rate\n",
2471 __func__);
2472 return status;
2475 /* Alloc memory for the string of commands. */
2476 cmdBuffer = kmalloc(0x100, GFP_ATOMIC);
2477 if (!cmdBuffer) {
2478 dev_err(&edge_port->port->dev,
2479 "%s - kmalloc(%d) failed.\n", __func__, 0x100);
2480 return -ENOMEM;
2482 currCmd = cmdBuffer;
2484 /* Enable access to divisor latch */
2485 MAKE_CMD_WRITE_REG(&currCmd, &cmdLen, number, LCR, LCR_DL_ENABLE);
2487 /* Write the divisor itself */
2488 MAKE_CMD_WRITE_REG(&currCmd, &cmdLen, number, DLL, LOW8(divisor));
2489 MAKE_CMD_WRITE_REG(&currCmd, &cmdLen, number, DLM, HIGH8(divisor));
2491 /* Restore original value to disable access to divisor latch */
2492 MAKE_CMD_WRITE_REG(&currCmd, &cmdLen, number, LCR,
2493 edge_port->shadowLCR);
2495 status = write_cmd_usb(edge_port, cmdBuffer, cmdLen);
2496 if (status) {
2497 /* something bad happened, let's free up the memory */
2498 kfree(cmdBuffer);
2501 return status;
2505 /*****************************************************************************
2506 * calc_baud_rate_divisor
2507 * this function calculates the proper baud rate divisor for the specified
2508 * baud rate.
2509 *****************************************************************************/
2510 static int calc_baud_rate_divisor(int baudrate, int *divisor)
2512 int i;
2513 __u16 custom;
2516 dbg("%s - %d", __func__, baudrate);
2518 for (i = 0; i < ARRAY_SIZE(divisor_table); i++) {
2519 if (divisor_table[i].BaudRate == baudrate) {
2520 *divisor = divisor_table[i].Divisor;
2521 return 0;
2525 /* We have tried all of the standard baud rates
2526 * lets try to calculate the divisor for this baud rate
2527 * Make sure the baud rate is reasonable */
2528 if (baudrate > 50 && baudrate < 230400) {
2529 /* get divisor */
2530 custom = (__u16)((230400L + baudrate/2) / baudrate);
2532 *divisor = custom;
2534 dbg("%s - Baud %d = %d\n", __func__, baudrate, custom);
2535 return 0;
2538 return -1;
2542 /*****************************************************************************
2543 * send_cmd_write_uart_register
2544 * this function builds up a uart register message and sends to to the device.
2545 *****************************************************************************/
2546 static int send_cmd_write_uart_register(struct edgeport_port *edge_port,
2547 __u8 regNum, __u8 regValue)
2549 struct edgeport_serial *edge_serial =
2550 usb_get_serial_data(edge_port->port->serial);
2551 unsigned char *cmdBuffer;
2552 unsigned char *currCmd;
2553 unsigned long cmdLen = 0;
2554 int status;
2556 dbg("%s - write to %s register 0x%02x",
2557 (regNum == MCR) ? "MCR" : "LCR", __func__, regValue);
2559 if (edge_serial->is_epic &&
2560 !edge_serial->epic_descriptor.Supports.IOSPWriteMCR &&
2561 regNum == MCR) {
2562 dbg("SendCmdWriteUartReg - Not writing to MCR Register");
2563 return 0;
2566 if (edge_serial->is_epic &&
2567 !edge_serial->epic_descriptor.Supports.IOSPWriteLCR &&
2568 regNum == LCR) {
2569 dbg("SendCmdWriteUartReg - Not writing to LCR Register");
2570 return 0;
2573 /* Alloc memory for the string of commands. */
2574 cmdBuffer = kmalloc(0x10, GFP_ATOMIC);
2575 if (cmdBuffer == NULL)
2576 return -ENOMEM;
2578 currCmd = cmdBuffer;
2580 /* Build a cmd in the buffer to write the given register */
2581 MAKE_CMD_WRITE_REG(&currCmd, &cmdLen,
2582 edge_port->port->number - edge_port->port->serial->minor,
2583 regNum, regValue);
2585 status = write_cmd_usb(edge_port, cmdBuffer, cmdLen);
2586 if (status) {
2587 /* something bad happened, let's free up the memory */
2588 kfree(cmdBuffer);
2591 return status;
2595 /*****************************************************************************
2596 * change_port_settings
2597 * This routine is called to set the UART on the device to match the
2598 * specified new settings.
2599 *****************************************************************************/
2601 static void change_port_settings(struct tty_struct *tty,
2602 struct edgeport_port *edge_port, struct ktermios *old_termios)
2604 struct edgeport_serial *edge_serial =
2605 usb_get_serial_data(edge_port->port->serial);
2606 int baud;
2607 unsigned cflag;
2608 __u8 mask = 0xff;
2609 __u8 lData;
2610 __u8 lParity;
2611 __u8 lStop;
2612 __u8 rxFlow;
2613 __u8 txFlow;
2614 int status;
2616 dbg("%s - port %d", __func__, edge_port->port->number);
2618 if (!edge_port->open &&
2619 !edge_port->openPending) {
2620 dbg("%s - port not opened", __func__);
2621 return;
2624 cflag = tty->termios->c_cflag;
2626 switch (cflag & CSIZE) {
2627 case CS5:
2628 lData = LCR_BITS_5; mask = 0x1f;
2629 dbg("%s - data bits = 5", __func__);
2630 break;
2631 case CS6:
2632 lData = LCR_BITS_6; mask = 0x3f;
2633 dbg("%s - data bits = 6", __func__);
2634 break;
2635 case CS7:
2636 lData = LCR_BITS_7; mask = 0x7f;
2637 dbg("%s - data bits = 7", __func__);
2638 break;
2639 default:
2640 case CS8:
2641 lData = LCR_BITS_8;
2642 dbg("%s - data bits = 8", __func__);
2643 break;
2646 lParity = LCR_PAR_NONE;
2647 if (cflag & PARENB) {
2648 if (cflag & CMSPAR) {
2649 if (cflag & PARODD) {
2650 lParity = LCR_PAR_MARK;
2651 dbg("%s - parity = mark", __func__);
2652 } else {
2653 lParity = LCR_PAR_SPACE;
2654 dbg("%s - parity = space", __func__);
2656 } else if (cflag & PARODD) {
2657 lParity = LCR_PAR_ODD;
2658 dbg("%s - parity = odd", __func__);
2659 } else {
2660 lParity = LCR_PAR_EVEN;
2661 dbg("%s - parity = even", __func__);
2663 } else {
2664 dbg("%s - parity = none", __func__);
2667 if (cflag & CSTOPB) {
2668 lStop = LCR_STOP_2;
2669 dbg("%s - stop bits = 2", __func__);
2670 } else {
2671 lStop = LCR_STOP_1;
2672 dbg("%s - stop bits = 1", __func__);
2675 /* figure out the flow control settings */
2676 rxFlow = txFlow = 0x00;
2677 if (cflag & CRTSCTS) {
2678 rxFlow |= IOSP_RX_FLOW_RTS;
2679 txFlow |= IOSP_TX_FLOW_CTS;
2680 dbg("%s - RTS/CTS is enabled", __func__);
2681 } else {
2682 dbg("%s - RTS/CTS is disabled", __func__);
2685 /* if we are implementing XON/XOFF, set the start and stop character
2686 in the device */
2687 if (I_IXOFF(tty) || I_IXON(tty)) {
2688 unsigned char stop_char = STOP_CHAR(tty);
2689 unsigned char start_char = START_CHAR(tty);
2691 if ((!edge_serial->is_epic) ||
2692 ((edge_serial->is_epic) &&
2693 (edge_serial->epic_descriptor.Supports.IOSPSetXChar))) {
2694 send_iosp_ext_cmd(edge_port,
2695 IOSP_CMD_SET_XON_CHAR, start_char);
2696 send_iosp_ext_cmd(edge_port,
2697 IOSP_CMD_SET_XOFF_CHAR, stop_char);
2700 /* if we are implementing INBOUND XON/XOFF */
2701 if (I_IXOFF(tty)) {
2702 rxFlow |= IOSP_RX_FLOW_XON_XOFF;
2703 dbg("%s - INBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x",
2704 __func__, start_char, stop_char);
2705 } else {
2706 dbg("%s - INBOUND XON/XOFF is disabled", __func__);
2709 /* if we are implementing OUTBOUND XON/XOFF */
2710 if (I_IXON(tty)) {
2711 txFlow |= IOSP_TX_FLOW_XON_XOFF;
2712 dbg("%s - OUTBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x",
2713 __func__, start_char, stop_char);
2714 } else {
2715 dbg("%s - OUTBOUND XON/XOFF is disabled", __func__);
2719 /* Set flow control to the configured value */
2720 if ((!edge_serial->is_epic) ||
2721 ((edge_serial->is_epic) &&
2722 (edge_serial->epic_descriptor.Supports.IOSPSetRxFlow)))
2723 send_iosp_ext_cmd(edge_port, IOSP_CMD_SET_RX_FLOW, rxFlow);
2724 if ((!edge_serial->is_epic) ||
2725 ((edge_serial->is_epic) &&
2726 (edge_serial->epic_descriptor.Supports.IOSPSetTxFlow)))
2727 send_iosp_ext_cmd(edge_port, IOSP_CMD_SET_TX_FLOW, txFlow);
2730 edge_port->shadowLCR &= ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
2731 edge_port->shadowLCR |= (lData | lParity | lStop);
2733 edge_port->validDataMask = mask;
2735 /* Send the updated LCR value to the EdgePort */
2736 status = send_cmd_write_uart_register(edge_port, LCR,
2737 edge_port->shadowLCR);
2738 if (status != 0)
2739 return;
2741 /* set up the MCR register and send it to the EdgePort */
2742 edge_port->shadowMCR = MCR_MASTER_IE;
2743 if (cflag & CBAUD)
2744 edge_port->shadowMCR |= (MCR_DTR | MCR_RTS);
2746 status = send_cmd_write_uart_register(edge_port, MCR,
2747 edge_port->shadowMCR);
2748 if (status != 0)
2749 return;
2751 /* Determine divisor based on baud rate */
2752 baud = tty_get_baud_rate(tty);
2753 if (!baud) {
2754 /* pick a default, any default... */
2755 baud = 9600;
2758 dbg("%s - baud rate = %d", __func__, baud);
2759 status = send_cmd_write_baud_rate(edge_port, baud);
2760 if (status == -1) {
2761 /* Speed change was not possible - put back the old speed */
2762 baud = tty_termios_baud_rate(old_termios);
2763 tty_encode_baud_rate(tty, baud, baud);
2765 return;
2769 /****************************************************************************
2770 * unicode_to_ascii
2771 * Turns a string from Unicode into ASCII.
2772 * Doesn't do a good job with any characters that are outside the normal
2773 * ASCII range, but it's only for debugging...
2774 * NOTE: expects the unicode in LE format
2775 ****************************************************************************/
2776 static void unicode_to_ascii(char *string, int buflen,
2777 __le16 *unicode, int unicode_size)
2779 int i;
2781 if (buflen <= 0) /* never happens, but... */
2782 return;
2783 --buflen; /* space for nul */
2785 for (i = 0; i < unicode_size; i++) {
2786 if (i >= buflen)
2787 break;
2788 string[i] = (char)(le16_to_cpu(unicode[i]));
2790 string[i] = 0x00;
2794 /****************************************************************************
2795 * get_manufacturing_desc
2796 * reads in the manufacturing descriptor and stores it into the serial
2797 * structure.
2798 ****************************************************************************/
2799 static void get_manufacturing_desc(struct edgeport_serial *edge_serial)
2801 int response;
2803 dbg("getting manufacturer descriptor");
2805 response = rom_read(edge_serial->serial,
2806 (EDGE_MANUF_DESC_ADDR & 0xffff0000) >> 16,
2807 (__u16)(EDGE_MANUF_DESC_ADDR & 0x0000ffff),
2808 EDGE_MANUF_DESC_LEN,
2809 (__u8 *)(&edge_serial->manuf_descriptor));
2811 if (response < 1)
2812 dev_err(&edge_serial->serial->dev->dev,
2813 "error in getting manufacturer descriptor\n");
2814 else {
2815 char string[30];
2816 dbg("**Manufacturer Descriptor");
2817 dbg(" RomSize: %dK",
2818 edge_serial->manuf_descriptor.RomSize);
2819 dbg(" RamSize: %dK",
2820 edge_serial->manuf_descriptor.RamSize);
2821 dbg(" CpuRev: %d",
2822 edge_serial->manuf_descriptor.CpuRev);
2823 dbg(" BoardRev: %d",
2824 edge_serial->manuf_descriptor.BoardRev);
2825 dbg(" NumPorts: %d",
2826 edge_serial->manuf_descriptor.NumPorts);
2827 dbg(" DescDate: %d/%d/%d",
2828 edge_serial->manuf_descriptor.DescDate[0],
2829 edge_serial->manuf_descriptor.DescDate[1],
2830 edge_serial->manuf_descriptor.DescDate[2]+1900);
2831 unicode_to_ascii(string, sizeof(string),
2832 edge_serial->manuf_descriptor.SerialNumber,
2833 edge_serial->manuf_descriptor.SerNumLength/2);
2834 dbg(" SerialNumber: %s", string);
2835 unicode_to_ascii(string, sizeof(string),
2836 edge_serial->manuf_descriptor.AssemblyNumber,
2837 edge_serial->manuf_descriptor.AssemblyNumLength/2);
2838 dbg(" AssemblyNumber: %s", string);
2839 unicode_to_ascii(string, sizeof(string),
2840 edge_serial->manuf_descriptor.OemAssyNumber,
2841 edge_serial->manuf_descriptor.OemAssyNumLength/2);
2842 dbg(" OemAssyNumber: %s", string);
2843 dbg(" UartType: %d",
2844 edge_serial->manuf_descriptor.UartType);
2845 dbg(" IonPid: %d",
2846 edge_serial->manuf_descriptor.IonPid);
2847 dbg(" IonConfig: %d",
2848 edge_serial->manuf_descriptor.IonConfig);
2853 /****************************************************************************
2854 * get_boot_desc
2855 * reads in the bootloader descriptor and stores it into the serial
2856 * structure.
2857 ****************************************************************************/
2858 static void get_boot_desc(struct edgeport_serial *edge_serial)
2860 int response;
2862 dbg("getting boot descriptor");
2864 response = rom_read(edge_serial->serial,
2865 (EDGE_BOOT_DESC_ADDR & 0xffff0000) >> 16,
2866 (__u16)(EDGE_BOOT_DESC_ADDR & 0x0000ffff),
2867 EDGE_BOOT_DESC_LEN,
2868 (__u8 *)(&edge_serial->boot_descriptor));
2870 if (response < 1)
2871 dev_err(&edge_serial->serial->dev->dev,
2872 "error in getting boot descriptor\n");
2873 else {
2874 dbg("**Boot Descriptor:");
2875 dbg(" BootCodeLength: %d",
2876 le16_to_cpu(edge_serial->boot_descriptor.BootCodeLength));
2877 dbg(" MajorVersion: %d",
2878 edge_serial->boot_descriptor.MajorVersion);
2879 dbg(" MinorVersion: %d",
2880 edge_serial->boot_descriptor.MinorVersion);
2881 dbg(" BuildNumber: %d",
2882 le16_to_cpu(edge_serial->boot_descriptor.BuildNumber));
2883 dbg(" Capabilities: 0x%x",
2884 le16_to_cpu(edge_serial->boot_descriptor.Capabilities));
2885 dbg(" UConfig0: %d",
2886 edge_serial->boot_descriptor.UConfig0);
2887 dbg(" UConfig1: %d",
2888 edge_serial->boot_descriptor.UConfig1);
2893 /****************************************************************************
2894 * load_application_firmware
2895 * This is called to load the application firmware to the device
2896 ****************************************************************************/
2897 static void load_application_firmware(struct edgeport_serial *edge_serial)
2899 const struct ihex_binrec *rec;
2900 const struct firmware *fw;
2901 const char *fw_name;
2902 const char *fw_info;
2903 int response;
2904 __u32 Operaddr;
2905 __u16 build;
2907 switch (edge_serial->product_info.iDownloadFile) {
2908 case EDGE_DOWNLOAD_FILE_I930:
2909 fw_info = "downloading firmware version (930)";
2910 fw_name = "edgeport/down.fw";
2911 break;
2913 case EDGE_DOWNLOAD_FILE_80251:
2914 fw_info = "downloading firmware version (80251)";
2915 fw_name = "edgeport/down2.fw";
2916 break;
2918 case EDGE_DOWNLOAD_FILE_NONE:
2919 dbg ("No download file specified, skipping download\n");
2920 return;
2922 default:
2923 return;
2926 response = request_ihex_firmware(&fw, fw_name,
2927 &edge_serial->serial->dev->dev);
2928 if (response) {
2929 printk(KERN_ERR "Failed to load image \"%s\" err %d\n",
2930 fw_name, response);
2931 return;
2934 rec = (const struct ihex_binrec *)fw->data;
2935 build = (rec->data[2] << 8) | rec->data[3];
2937 dbg("%s %d.%d.%d", fw_info, rec->data[0], rec->data[1], build);
2939 edge_serial->product_info.FirmwareMajorVersion = fw->data[0];
2940 edge_serial->product_info.FirmwareMinorVersion = fw->data[1];
2941 edge_serial->product_info.FirmwareBuildNumber = cpu_to_le16(build);
2943 for (rec = ihex_next_binrec(rec); rec;
2944 rec = ihex_next_binrec(rec)) {
2945 Operaddr = be32_to_cpu(rec->addr);
2946 response = sram_write(edge_serial->serial,
2947 Operaddr >> 16,
2948 Operaddr & 0xFFFF,
2949 be16_to_cpu(rec->len),
2950 &rec->data[0]);
2951 if (response < 0) {
2952 dev_err(&edge_serial->serial->dev->dev,
2953 "sram_write failed (%x, %x, %d)\n",
2954 Operaddr >> 16, Operaddr & 0xFFFF,
2955 be16_to_cpu(rec->len));
2956 break;
2960 dbg("sending exec_dl_code");
2961 response = usb_control_msg (edge_serial->serial->dev,
2962 usb_sndctrlpipe(edge_serial->serial->dev, 0),
2963 USB_REQUEST_ION_EXEC_DL_CODE,
2964 0x40, 0x4000, 0x0001, NULL, 0, 3000);
2966 release_firmware(fw);
2967 return;
2971 /****************************************************************************
2972 * edge_startup
2973 ****************************************************************************/
2974 static int edge_startup(struct usb_serial *serial)
2976 struct edgeport_serial *edge_serial;
2977 struct edgeport_port *edge_port;
2978 struct usb_device *dev;
2979 int i, j;
2980 int response;
2981 bool interrupt_in_found;
2982 bool bulk_in_found;
2983 bool bulk_out_found;
2984 static __u32 descriptor[3] = { EDGE_COMPATIBILITY_MASK0,
2985 EDGE_COMPATIBILITY_MASK1,
2986 EDGE_COMPATIBILITY_MASK2 };
2988 dev = serial->dev;
2990 /* create our private serial structure */
2991 edge_serial = kzalloc(sizeof(struct edgeport_serial), GFP_KERNEL);
2992 if (edge_serial == NULL) {
2993 dev_err(&serial->dev->dev, "%s - Out of memory\n", __func__);
2994 return -ENOMEM;
2996 spin_lock_init(&edge_serial->es_lock);
2997 edge_serial->serial = serial;
2998 usb_set_serial_data(serial, edge_serial);
3000 /* get the name for the device from the device */
3001 i = get_string(dev, dev->descriptor.iManufacturer,
3002 &edge_serial->name[0], MAX_NAME_LEN+1);
3003 edge_serial->name[i++] = ' ';
3004 get_string(dev, dev->descriptor.iProduct,
3005 &edge_serial->name[i], MAX_NAME_LEN+2 - i);
3007 dev_info(&serial->dev->dev, "%s detected\n", edge_serial->name);
3009 /* Read the epic descriptor */
3010 if (get_epic_descriptor(edge_serial) <= 0) {
3011 /* memcpy descriptor to Supports structures */
3012 memcpy(&edge_serial->epic_descriptor.Supports, descriptor,
3013 sizeof(struct edge_compatibility_bits));
3015 /* get the manufacturing descriptor for this device */
3016 get_manufacturing_desc(edge_serial);
3018 /* get the boot descriptor */
3019 get_boot_desc(edge_serial);
3021 get_product_info(edge_serial);
3024 /* set the number of ports from the manufacturing description */
3025 /* serial->num_ports = serial->product_info.NumPorts; */
3026 if ((!edge_serial->is_epic) &&
3027 (edge_serial->product_info.NumPorts != serial->num_ports)) {
3028 dev_warn(&serial->dev->dev, "Device Reported %d serial ports "
3029 "vs. core thinking we have %d ports, email "
3030 "greg@kroah.com this information.\n",
3031 edge_serial->product_info.NumPorts,
3032 serial->num_ports);
3035 dbg("%s - time 1 %ld", __func__, jiffies);
3037 /* If not an EPiC device */
3038 if (!edge_serial->is_epic) {
3039 /* now load the application firmware into this device */
3040 load_application_firmware(edge_serial);
3042 dbg("%s - time 2 %ld", __func__, jiffies);
3044 /* Check current Edgeport EEPROM and update if necessary */
3045 update_edgeport_E2PROM(edge_serial);
3047 dbg("%s - time 3 %ld", __func__, jiffies);
3049 /* set the configuration to use #1 */
3050 /* dbg("set_configuration 1"); */
3051 /* usb_set_configuration (dev, 1); */
3053 dbg(" FirmwareMajorVersion %d.%d.%d",
3054 edge_serial->product_info.FirmwareMajorVersion,
3055 edge_serial->product_info.FirmwareMinorVersion,
3056 le16_to_cpu(edge_serial->product_info.FirmwareBuildNumber));
3058 /* we set up the pointers to the endpoints in the edge_open function,
3059 * as the structures aren't created yet. */
3061 /* set up our port private structures */
3062 for (i = 0; i < serial->num_ports; ++i) {
3063 edge_port = kmalloc(sizeof(struct edgeport_port), GFP_KERNEL);
3064 if (edge_port == NULL) {
3065 dev_err(&serial->dev->dev, "%s - Out of memory\n",
3066 __func__);
3067 for (j = 0; j < i; ++j) {
3068 kfree(usb_get_serial_port_data(serial->port[j]));
3069 usb_set_serial_port_data(serial->port[j],
3070 NULL);
3072 usb_set_serial_data(serial, NULL);
3073 kfree(edge_serial);
3074 return -ENOMEM;
3076 memset(edge_port, 0, sizeof(struct edgeport_port));
3077 spin_lock_init(&edge_port->ep_lock);
3078 edge_port->port = serial->port[i];
3079 usb_set_serial_port_data(serial->port[i], edge_port);
3082 response = 0;
3084 if (edge_serial->is_epic) {
3085 /* EPIC thing, set up our interrupt polling now and our read
3086 * urb, so that the device knows it really is connected. */
3087 interrupt_in_found = bulk_in_found = bulk_out_found = false;
3088 for (i = 0; i < serial->interface->altsetting[0]
3089 .desc.bNumEndpoints; ++i) {
3090 struct usb_endpoint_descriptor *endpoint;
3091 int buffer_size;
3093 endpoint = &serial->interface->altsetting[0].
3094 endpoint[i].desc;
3095 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
3096 if (!interrupt_in_found &&
3097 (usb_endpoint_is_int_in(endpoint))) {
3098 /* we found a interrupt in endpoint */
3099 dbg("found interrupt in");
3101 /* not set up yet, so do it now */
3102 edge_serial->interrupt_read_urb =
3103 usb_alloc_urb(0, GFP_KERNEL);
3104 if (!edge_serial->interrupt_read_urb) {
3105 dev_err(&dev->dev, "out of memory\n");
3106 return -ENOMEM;
3108 edge_serial->interrupt_in_buffer =
3109 kmalloc(buffer_size, GFP_KERNEL);
3110 if (!edge_serial->interrupt_in_buffer) {
3111 dev_err(&dev->dev, "out of memory\n");
3112 usb_free_urb(edge_serial->interrupt_read_urb);
3113 return -ENOMEM;
3115 edge_serial->interrupt_in_endpoint =
3116 endpoint->bEndpointAddress;
3118 /* set up our interrupt urb */
3119 usb_fill_int_urb(
3120 edge_serial->interrupt_read_urb,
3121 dev,
3122 usb_rcvintpipe(dev,
3123 endpoint->bEndpointAddress),
3124 edge_serial->interrupt_in_buffer,
3125 buffer_size,
3126 edge_interrupt_callback,
3127 edge_serial,
3128 endpoint->bInterval);
3130 interrupt_in_found = true;
3133 if (!bulk_in_found &&
3134 (usb_endpoint_is_bulk_in(endpoint))) {
3135 /* we found a bulk in endpoint */
3136 dbg("found bulk in");
3138 /* not set up yet, so do it now */
3139 edge_serial->read_urb =
3140 usb_alloc_urb(0, GFP_KERNEL);
3141 if (!edge_serial->read_urb) {
3142 dev_err(&dev->dev, "out of memory\n");
3143 return -ENOMEM;
3145 edge_serial->bulk_in_buffer =
3146 kmalloc(buffer_size, GFP_KERNEL);
3147 if (!edge_serial->bulk_in_buffer) {
3148 dev_err(&dev->dev, "out of memory\n");
3149 usb_free_urb(edge_serial->read_urb);
3150 return -ENOMEM;
3152 edge_serial->bulk_in_endpoint =
3153 endpoint->bEndpointAddress;
3155 /* set up our bulk in urb */
3156 usb_fill_bulk_urb(edge_serial->read_urb, dev,
3157 usb_rcvbulkpipe(dev,
3158 endpoint->bEndpointAddress),
3159 edge_serial->bulk_in_buffer,
3160 le16_to_cpu(endpoint->wMaxPacketSize),
3161 edge_bulk_in_callback,
3162 edge_serial);
3163 bulk_in_found = true;
3166 if (!bulk_out_found &&
3167 (usb_endpoint_is_bulk_out(endpoint))) {
3168 /* we found a bulk out endpoint */
3169 dbg("found bulk out");
3170 edge_serial->bulk_out_endpoint =
3171 endpoint->bEndpointAddress;
3172 bulk_out_found = true;
3176 if (!interrupt_in_found || !bulk_in_found || !bulk_out_found) {
3177 dev_err(&dev->dev, "Error - the proper endpoints "
3178 "were not found!\n");
3179 return -ENODEV;
3182 /* start interrupt read for this edgeport this interrupt will
3183 * continue as long as the edgeport is connected */
3184 response = usb_submit_urb(edge_serial->interrupt_read_urb,
3185 GFP_KERNEL);
3186 if (response)
3187 dev_err(&dev->dev,
3188 "%s - Error %d submitting control urb\n",
3189 __func__, response);
3191 return response;
3195 /****************************************************************************
3196 * edge_shutdown
3197 * This function is called whenever the device is removed from the usb bus.
3198 ****************************************************************************/
3199 static void edge_shutdown(struct usb_serial *serial)
3201 struct edgeport_serial *edge_serial = usb_get_serial_data(serial);
3202 int i;
3204 dbg("%s", __func__);
3206 /* stop reads and writes on all ports */
3207 for (i = 0; i < serial->num_ports; ++i) {
3208 kfree(usb_get_serial_port_data(serial->port[i]));
3209 usb_set_serial_port_data(serial->port[i], NULL);
3211 /* free up our endpoint stuff */
3212 if (edge_serial->is_epic) {
3213 usb_kill_urb(edge_serial->interrupt_read_urb);
3214 usb_free_urb(edge_serial->interrupt_read_urb);
3215 kfree(edge_serial->interrupt_in_buffer);
3217 usb_kill_urb(edge_serial->read_urb);
3218 usb_free_urb(edge_serial->read_urb);
3219 kfree(edge_serial->bulk_in_buffer);
3222 kfree(edge_serial);
3223 usb_set_serial_data(serial, NULL);
3227 /****************************************************************************
3228 * edgeport_init
3229 * This is called by the module subsystem, or on startup to initialize us
3230 ****************************************************************************/
3231 static int __init edgeport_init(void)
3233 int retval;
3235 retval = usb_serial_register(&edgeport_2port_device);
3236 if (retval)
3237 goto failed_2port_device_register;
3238 retval = usb_serial_register(&edgeport_4port_device);
3239 if (retval)
3240 goto failed_4port_device_register;
3241 retval = usb_serial_register(&edgeport_8port_device);
3242 if (retval)
3243 goto failed_8port_device_register;
3244 retval = usb_serial_register(&epic_device);
3245 if (retval)
3246 goto failed_epic_device_register;
3247 retval = usb_register(&io_driver);
3248 if (retval)
3249 goto failed_usb_register;
3250 atomic_set(&CmdUrbs, 0);
3251 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
3252 DRIVER_DESC "\n");
3253 return 0;
3255 failed_usb_register:
3256 usb_serial_deregister(&epic_device);
3257 failed_epic_device_register:
3258 usb_serial_deregister(&edgeport_8port_device);
3259 failed_8port_device_register:
3260 usb_serial_deregister(&edgeport_4port_device);
3261 failed_4port_device_register:
3262 usb_serial_deregister(&edgeport_2port_device);
3263 failed_2port_device_register:
3264 return retval;
3268 /****************************************************************************
3269 * edgeport_exit
3270 * Called when the driver is about to be unloaded.
3271 ****************************************************************************/
3272 static void __exit edgeport_exit (void)
3274 usb_deregister(&io_driver);
3275 usb_serial_deregister(&edgeport_2port_device);
3276 usb_serial_deregister(&edgeport_4port_device);
3277 usb_serial_deregister(&edgeport_8port_device);
3278 usb_serial_deregister(&epic_device);
3281 module_init(edgeport_init);
3282 module_exit(edgeport_exit);
3284 /* Module information */
3285 MODULE_AUTHOR(DRIVER_AUTHOR);
3286 MODULE_DESCRIPTION(DRIVER_DESC);
3287 MODULE_LICENSE("GPL");
3288 MODULE_FIRMWARE("edgeport/boot.fw");
3289 MODULE_FIRMWARE("edgeport/boot2.fw");
3290 MODULE_FIRMWARE("edgeport/down.fw");
3291 MODULE_FIRMWARE("edgeport/down2.fw");
3293 module_param(debug, bool, S_IRUGO | S_IWUSR);
3294 MODULE_PARM_DESC(debug, "Debug enabled or not");