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