2 * Edgeport USB Serial Converter driver
4 * Copyright (C) 2000 Inside Out Networks, All rights reserved.
5 * Copyright (C) 2001-2002 Greg Kroah-Hartman <greg@kroah.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * Supports the following devices:
26 * For questions or problems with this driver, contact Inside Out
27 * Networks technical support, or Peter Berger <pberger@brimson.com>,
28 * or Al Borchers <alborchers@steinerpoint.com>.
32 #include <linux/kernel.h>
33 #include <linux/jiffies.h>
34 #include <linux/errno.h>
35 #include <linux/init.h>
36 #include <linux/slab.h>
37 #include <linux/tty.h>
38 #include <linux/tty_driver.h>
39 #include <linux/tty_flip.h>
40 #include <linux/module.h>
41 #include <linux/spinlock.h>
42 #include <linux/serial.h>
43 #include <linux/ioctl.h>
44 #include <linux/wait.h>
45 #include <linux/firmware.h>
46 #include <linux/ihex.h>
47 #include <linux/uaccess.h>
48 #include <linux/usb.h>
49 #include <linux/usb/serial.h>
50 #include "io_edgeport.h"
51 #include "io_ionsp.h" /* info for the iosp messages */
52 #include "io_16654.h" /* 16654 UART defines */
57 #define DRIVER_VERSION "v2.7"
58 #define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com> and David Iacovelli"
59 #define DRIVER_DESC "Edgeport USB Serial Driver"
61 #define MAX_NAME_LEN 64
63 #define CHASE_TIMEOUT (5*HZ) /* 5 seconds */
64 #define OPEN_TIMEOUT (5*HZ) /* 5 seconds */
65 #define COMMAND_TIMEOUT (5*HZ) /* 5 seconds */
67 /* receive port state */
69 EXPECT_HDR1
= 0, /* Expect header byte 1 */
70 EXPECT_HDR2
= 1, /* Expect header byte 2 */
71 EXPECT_DATA
= 2, /* Expect 'RxBytesRemaining' data */
72 EXPECT_HDR3
= 3, /* Expect header byte 3 (for status hdrs only) */
77 * This Transmit queue is an extension of the edgeport Rx buffer.
78 * The maximum amount of data buffered in both the edgeport
79 * Rx buffer (maxTxCredits) and this buffer will never exceed maxTxCredits.
82 unsigned int head
; /* index to head pointer (write) */
83 unsigned int tail
; /* index to tail pointer (read) */
84 unsigned int count
; /* Bytes in queue */
85 unsigned int size
; /* Max size of queue (equal to Max number of TxCredits) */
86 unsigned char *fifo
; /* allocated Buffer */
89 /* This structure holds all of the local port information */
90 struct edgeport_port
{
91 __u16 txCredits
; /* our current credits for this port */
92 __u16 maxTxCredits
; /* the max size of the port */
94 struct TxFifo txfifo
; /* transmit fifo -- size will be maxTxCredits */
95 struct urb
*write_urb
; /* write URB for this port */
96 bool write_in_progress
; /* 'true' while a write URB is outstanding */
99 __u8 shadowLCR
; /* last LCR value received */
100 __u8 shadowMCR
; /* last MCR value received */
101 __u8 shadowMSR
; /* last MSR value received */
102 __u8 shadowLSR
; /* last LSR value received */
103 __u8 shadowXonChar
; /* last value set as XON char in Edgeport */
104 __u8 shadowXoffChar
; /* last value set as XOFF char in Edgeport */
112 bool chaseResponsePending
;
114 wait_queue_head_t wait_chase
; /* for handling sleeping while waiting for chase to finish */
115 wait_queue_head_t wait_open
; /* for handling sleeping while waiting for open to finish */
116 wait_queue_head_t wait_command
; /* for handling sleeping while waiting for command to finish */
117 wait_queue_head_t delta_msr_wait
; /* for handling sleeping while waiting for msr change to happen */
119 struct async_icount icount
;
120 struct usb_serial_port
*port
; /* loop back to the owner of this object */
124 /* This structure holds all of the individual device information */
125 struct edgeport_serial
{
126 char name
[MAX_NAME_LEN
+2]; /* string name of this device */
128 struct edge_manuf_descriptor manuf_descriptor
; /* the manufacturer descriptor */
129 struct edge_boot_descriptor boot_descriptor
; /* the boot firmware descriptor */
130 struct edgeport_product_info product_info
; /* Product Info */
131 struct edge_compatibility_descriptor epic_descriptor
; /* Edgeport compatible descriptor */
132 int is_epic
; /* flag if EPiC device or not */
134 __u8 interrupt_in_endpoint
; /* the interrupt endpoint handle */
135 unsigned char *interrupt_in_buffer
; /* the buffer we use for the interrupt endpoint */
136 struct urb
*interrupt_read_urb
; /* our interrupt urb */
138 __u8 bulk_in_endpoint
; /* the bulk in endpoint handle */
139 unsigned char *bulk_in_buffer
; /* the buffer we use for the bulk in endpoint */
140 struct urb
*read_urb
; /* our bulk read urb */
141 bool read_in_progress
;
144 __u8 bulk_out_endpoint
; /* the bulk out endpoint handle */
146 __s16 rxBytesAvail
; /* the number of bytes that we need to read from this device */
148 enum RXSTATE rxState
; /* the current state of the bulk receive processor */
149 __u8 rxHeader1
; /* receive header byte 1 */
150 __u8 rxHeader2
; /* receive header byte 2 */
151 __u8 rxHeader3
; /* receive header byte 3 */
152 __u8 rxPort
; /* the port that we are currently receiving data for */
153 __u8 rxStatusCode
; /* the receive status code */
154 __u8 rxStatusParam
; /* the receive status paramater */
155 __s16 rxBytesRemaining
; /* the number of port bytes left to read */
156 struct usb_serial
*serial
; /* loop back to the owner of this object */
159 /* baud rate information */
160 struct divisor_table_entry
{
166 * Define table of divisors for Rev A EdgePort/4 hardware
167 * These assume a 3.6864MHz crystal, the standard /16, and
171 static const struct divisor_table_entry divisor_table
[] = {
174 { 110, 2095}, /* 2094.545455 => 230450 => .0217 % over */
175 { 134, 1713}, /* 1713.011152 => 230398.5 => .00065% under */
193 /* Number of outstanding Command Write Urbs */
194 static atomic_t CmdUrbs
= ATOMIC_INIT(0);
197 /* local function prototypes */
199 /* function prototypes for all URB callbacks */
200 static void edge_interrupt_callback(struct urb
*urb
);
201 static void edge_bulk_in_callback(struct urb
*urb
);
202 static void edge_bulk_out_data_callback(struct urb
*urb
);
203 static void edge_bulk_out_cmd_callback(struct urb
*urb
);
205 /* function prototypes for the usbserial callbacks */
206 static int edge_open(struct tty_struct
*tty
, struct usb_serial_port
*port
);
207 static void edge_close(struct usb_serial_port
*port
);
208 static int edge_write(struct tty_struct
*tty
, struct usb_serial_port
*port
,
209 const unsigned char *buf
, int count
);
210 static int edge_write_room(struct tty_struct
*tty
);
211 static int edge_chars_in_buffer(struct tty_struct
*tty
);
212 static void edge_throttle(struct tty_struct
*tty
);
213 static void edge_unthrottle(struct tty_struct
*tty
);
214 static void edge_set_termios(struct tty_struct
*tty
,
215 struct usb_serial_port
*port
,
216 struct ktermios
*old_termios
);
217 static int edge_ioctl(struct tty_struct
*tty
,
218 unsigned int cmd
, unsigned long arg
);
219 static void edge_break(struct tty_struct
*tty
, int break_state
);
220 static int edge_tiocmget(struct tty_struct
*tty
);
221 static int edge_tiocmset(struct tty_struct
*tty
,
222 unsigned int set
, unsigned int clear
);
223 static int edge_get_icount(struct tty_struct
*tty
,
224 struct serial_icounter_struct
*icount
);
225 static int edge_startup(struct usb_serial
*serial
);
226 static void edge_disconnect(struct usb_serial
*serial
);
227 static void edge_release(struct usb_serial
*serial
);
228 static int edge_port_probe(struct usb_serial_port
*port
);
229 static int edge_port_remove(struct usb_serial_port
*port
);
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(struct device
*dev
, 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
)
288 struct device
*dev
= &edge_serial
->serial
->dev
->dev
;
291 __u8 BootMajorVersion
;
292 __u8 BootMinorVersion
;
293 __u16 BootBuildNumber
;
295 const struct ihex_binrec
*rec
;
296 const struct firmware
*fw
;
300 switch (edge_serial
->product_info
.iDownloadFile
) {
301 case EDGE_DOWNLOAD_FILE_I930
:
302 fw_name
= "edgeport/boot.fw";
304 case EDGE_DOWNLOAD_FILE_80251
:
305 fw_name
= "edgeport/boot2.fw";
311 response
= request_ihex_firmware(&fw
, fw_name
,
312 &edge_serial
->serial
->dev
->dev
);
314 dev_err(dev
, "Failed to load image \"%s\" err %d\n",
319 rec
= (const struct ihex_binrec
*)fw
->data
;
320 BootMajorVersion
= rec
->data
[0];
321 BootMinorVersion
= rec
->data
[1];
322 BootBuildNumber
= (rec
->data
[2] << 8) | rec
->data
[3];
324 /* Check Boot Image Version */
325 BootCurVer
= (edge_serial
->boot_descriptor
.MajorVersion
<< 24) +
326 (edge_serial
->boot_descriptor
.MinorVersion
<< 16) +
327 le16_to_cpu(edge_serial
->boot_descriptor
.BuildNumber
);
329 BootNewVer
= (BootMajorVersion
<< 24) +
330 (BootMinorVersion
<< 16) +
333 dev_dbg(dev
, "Current Boot Image version %d.%d.%d\n",
334 edge_serial
->boot_descriptor
.MajorVersion
,
335 edge_serial
->boot_descriptor
.MinorVersion
,
336 le16_to_cpu(edge_serial
->boot_descriptor
.BuildNumber
));
339 if (BootNewVer
> BootCurVer
) {
340 dev_dbg(dev
, "**Update Boot Image from %d.%d.%d to %d.%d.%d\n",
341 edge_serial
->boot_descriptor
.MajorVersion
,
342 edge_serial
->boot_descriptor
.MinorVersion
,
343 le16_to_cpu(edge_serial
->boot_descriptor
.BuildNumber
),
344 BootMajorVersion
, BootMinorVersion
, BootBuildNumber
);
346 dev_dbg(dev
, "Downloading new Boot Image\n");
348 for (rec
= ihex_next_binrec(rec
); rec
;
349 rec
= ihex_next_binrec(rec
)) {
350 Bootaddr
= be32_to_cpu(rec
->addr
);
351 response
= rom_write(edge_serial
->serial
,
354 be16_to_cpu(rec
->len
),
357 dev_err(&edge_serial
->serial
->dev
->dev
,
358 "rom_write failed (%x, %x, %d)\n",
359 Bootaddr
>> 16, Bootaddr
& 0xFFFF,
360 be16_to_cpu(rec
->len
));
365 dev_dbg(dev
, "Boot Image -- already up to date\n");
367 release_firmware(fw
);
371 /************************************************************************
373 * Get string descriptor from device
375 ************************************************************************/
376 static int get_string_desc(struct usb_device
*dev
, int Id
,
377 struct usb_string_descriptor
**pRetDesc
)
379 struct usb_string_descriptor StringDesc
;
380 struct usb_string_descriptor
*pStringDesc
;
382 dev_dbg(&dev
->dev
, "%s - USB String ID = %d\n", __func__
, Id
);
384 if (!usb_get_descriptor(dev
, USB_DT_STRING
, Id
, &StringDesc
,
388 pStringDesc
= kmalloc(StringDesc
.bLength
, GFP_KERNEL
);
392 if (!usb_get_descriptor(dev
, USB_DT_STRING
, Id
, pStringDesc
,
393 StringDesc
.bLength
)) {
398 *pRetDesc
= pStringDesc
;
403 static void dump_product_info(struct edgeport_serial
*edge_serial
,
404 struct edgeport_product_info
*product_info
)
406 struct device
*dev
= &edge_serial
->serial
->dev
->dev
;
408 /* Dump Product Info structure */
409 dev_dbg(dev
, "**Product Information:\n");
410 dev_dbg(dev
, " ProductId %x\n", product_info
->ProductId
);
411 dev_dbg(dev
, " NumPorts %d\n", product_info
->NumPorts
);
412 dev_dbg(dev
, " ProdInfoVer %d\n", product_info
->ProdInfoVer
);
413 dev_dbg(dev
, " IsServer %d\n", product_info
->IsServer
);
414 dev_dbg(dev
, " IsRS232 %d\n", product_info
->IsRS232
);
415 dev_dbg(dev
, " IsRS422 %d\n", product_info
->IsRS422
);
416 dev_dbg(dev
, " IsRS485 %d\n", product_info
->IsRS485
);
417 dev_dbg(dev
, " RomSize %d\n", product_info
->RomSize
);
418 dev_dbg(dev
, " RamSize %d\n", product_info
->RamSize
);
419 dev_dbg(dev
, " CpuRev %x\n", product_info
->CpuRev
);
420 dev_dbg(dev
, " BoardRev %x\n", product_info
->BoardRev
);
421 dev_dbg(dev
, " BootMajorVersion %d.%d.%d\n",
422 product_info
->BootMajorVersion
,
423 product_info
->BootMinorVersion
,
424 le16_to_cpu(product_info
->BootBuildNumber
));
425 dev_dbg(dev
, " FirmwareMajorVersion %d.%d.%d\n",
426 product_info
->FirmwareMajorVersion
,
427 product_info
->FirmwareMinorVersion
,
428 le16_to_cpu(product_info
->FirmwareBuildNumber
));
429 dev_dbg(dev
, " ManufactureDescDate %d/%d/%d\n",
430 product_info
->ManufactureDescDate
[0],
431 product_info
->ManufactureDescDate
[1],
432 product_info
->ManufactureDescDate
[2]+1900);
433 dev_dbg(dev
, " iDownloadFile 0x%x\n",
434 product_info
->iDownloadFile
);
435 dev_dbg(dev
, " EpicVer %d\n", product_info
->EpicVer
);
438 static void get_product_info(struct edgeport_serial
*edge_serial
)
440 struct edgeport_product_info
*product_info
= &edge_serial
->product_info
;
442 memset(product_info
, 0, sizeof(struct edgeport_product_info
));
444 product_info
->ProductId
= (__u16
)(le16_to_cpu(edge_serial
->serial
->dev
->descriptor
.idProduct
) & ~ION_DEVICE_ID_80251_NETCHIP
);
445 product_info
->NumPorts
= edge_serial
->manuf_descriptor
.NumPorts
;
446 product_info
->ProdInfoVer
= 0;
448 product_info
->RomSize
= edge_serial
->manuf_descriptor
.RomSize
;
449 product_info
->RamSize
= edge_serial
->manuf_descriptor
.RamSize
;
450 product_info
->CpuRev
= edge_serial
->manuf_descriptor
.CpuRev
;
451 product_info
->BoardRev
= edge_serial
->manuf_descriptor
.BoardRev
;
453 product_info
->BootMajorVersion
=
454 edge_serial
->boot_descriptor
.MajorVersion
;
455 product_info
->BootMinorVersion
=
456 edge_serial
->boot_descriptor
.MinorVersion
;
457 product_info
->BootBuildNumber
=
458 edge_serial
->boot_descriptor
.BuildNumber
;
460 memcpy(product_info
->ManufactureDescDate
,
461 edge_serial
->manuf_descriptor
.DescDate
,
462 sizeof(edge_serial
->manuf_descriptor
.DescDate
));
464 /* check if this is 2nd generation hardware */
465 if (le16_to_cpu(edge_serial
->serial
->dev
->descriptor
.idProduct
)
466 & ION_DEVICE_ID_80251_NETCHIP
)
467 product_info
->iDownloadFile
= EDGE_DOWNLOAD_FILE_80251
;
469 product_info
->iDownloadFile
= EDGE_DOWNLOAD_FILE_I930
;
471 /* Determine Product type and set appropriate flags */
472 switch (DEVICE_ID_FROM_USB_PRODUCT_ID(product_info
->ProductId
)) {
473 case ION_DEVICE_ID_EDGEPORT_COMPATIBLE
:
474 case ION_DEVICE_ID_EDGEPORT_4T
:
475 case ION_DEVICE_ID_EDGEPORT_4
:
476 case ION_DEVICE_ID_EDGEPORT_2
:
477 case ION_DEVICE_ID_EDGEPORT_8_DUAL_CPU
:
478 case ION_DEVICE_ID_EDGEPORT_8
:
479 case ION_DEVICE_ID_EDGEPORT_421
:
480 case ION_DEVICE_ID_EDGEPORT_21
:
481 case ION_DEVICE_ID_EDGEPORT_2_DIN
:
482 case ION_DEVICE_ID_EDGEPORT_4_DIN
:
483 case ION_DEVICE_ID_EDGEPORT_16_DUAL_CPU
:
484 product_info
->IsRS232
= 1;
487 case ION_DEVICE_ID_EDGEPORT_2I
: /* Edgeport/2 RS422/RS485 */
488 product_info
->IsRS422
= 1;
489 product_info
->IsRS485
= 1;
492 case ION_DEVICE_ID_EDGEPORT_8I
: /* Edgeport/4 RS422 */
493 case ION_DEVICE_ID_EDGEPORT_4I
: /* Edgeport/4 RS422 */
494 product_info
->IsRS422
= 1;
498 dump_product_info(edge_serial
, product_info
);
501 static int get_epic_descriptor(struct edgeport_serial
*ep
)
504 struct usb_serial
*serial
= ep
->serial
;
505 struct edgeport_product_info
*product_info
= &ep
->product_info
;
506 struct edge_compatibility_descriptor
*epic
= &ep
->epic_descriptor
;
507 struct edge_compatibility_bits
*bits
;
508 struct device
*dev
= &serial
->dev
->dev
;
511 result
= usb_control_msg(serial
->dev
, usb_rcvctrlpipe(serial
->dev
, 0),
512 USB_REQUEST_ION_GET_EPIC_DESC
,
514 &ep
->epic_descriptor
,
515 sizeof(struct edge_compatibility_descriptor
),
520 memset(product_info
, 0, sizeof(struct edgeport_product_info
));
522 product_info
->NumPorts
= epic
->NumPorts
;
523 product_info
->ProdInfoVer
= 0;
524 product_info
->FirmwareMajorVersion
= epic
->MajorVersion
;
525 product_info
->FirmwareMinorVersion
= epic
->MinorVersion
;
526 product_info
->FirmwareBuildNumber
= epic
->BuildNumber
;
527 product_info
->iDownloadFile
= epic
->iDownloadFile
;
528 product_info
->EpicVer
= epic
->EpicVer
;
529 product_info
->Epic
= epic
->Supports
;
530 product_info
->ProductId
= ION_DEVICE_ID_EDGEPORT_COMPATIBLE
;
531 dump_product_info(ep
, product_info
);
533 bits
= &ep
->epic_descriptor
.Supports
;
534 dev_dbg(dev
, "**EPIC descriptor:\n");
535 dev_dbg(dev
, " VendEnableSuspend: %s\n", bits
->VendEnableSuspend
? "TRUE": "FALSE");
536 dev_dbg(dev
, " IOSPOpen : %s\n", bits
->IOSPOpen
? "TRUE": "FALSE");
537 dev_dbg(dev
, " IOSPClose : %s\n", bits
->IOSPClose
? "TRUE": "FALSE");
538 dev_dbg(dev
, " IOSPChase : %s\n", bits
->IOSPChase
? "TRUE": "FALSE");
539 dev_dbg(dev
, " IOSPSetRxFlow : %s\n", bits
->IOSPSetRxFlow
? "TRUE": "FALSE");
540 dev_dbg(dev
, " IOSPSetTxFlow : %s\n", bits
->IOSPSetTxFlow
? "TRUE": "FALSE");
541 dev_dbg(dev
, " IOSPSetXChar : %s\n", bits
->IOSPSetXChar
? "TRUE": "FALSE");
542 dev_dbg(dev
, " IOSPRxCheck : %s\n", bits
->IOSPRxCheck
? "TRUE": "FALSE");
543 dev_dbg(dev
, " IOSPSetClrBreak : %s\n", bits
->IOSPSetClrBreak
? "TRUE": "FALSE");
544 dev_dbg(dev
, " IOSPWriteMCR : %s\n", bits
->IOSPWriteMCR
? "TRUE": "FALSE");
545 dev_dbg(dev
, " IOSPWriteLCR : %s\n", bits
->IOSPWriteLCR
? "TRUE": "FALSE");
546 dev_dbg(dev
, " IOSPSetBaudRate : %s\n", bits
->IOSPSetBaudRate
? "TRUE": "FALSE");
547 dev_dbg(dev
, " TrueEdgeport : %s\n", bits
->TrueEdgeport
? "TRUE": "FALSE");
554 /************************************************************************/
555 /************************************************************************/
556 /* U S B C A L L B A C K F U N C T I O N S */
557 /* U S B C A L L B A C K F U N C T I O N S */
558 /************************************************************************/
559 /************************************************************************/
561 /*****************************************************************************
562 * edge_interrupt_callback
563 * this is the callback function for when we have received data on the
564 * interrupt endpoint.
565 *****************************************************************************/
566 static void edge_interrupt_callback(struct urb
*urb
)
568 struct edgeport_serial
*edge_serial
= urb
->context
;
570 struct edgeport_port
*edge_port
;
571 struct usb_serial_port
*port
;
572 struct tty_struct
*tty
;
573 unsigned char *data
= urb
->transfer_buffer
;
574 int length
= urb
->actual_length
;
580 int status
= urb
->status
;
589 /* this urb is terminated, clean up */
590 dev_dbg(&urb
->dev
->dev
, "%s - urb shutting down with status: %d\n", __func__
, status
);
593 dev_dbg(&urb
->dev
->dev
, "%s - nonzero urb status received: %d\n", __func__
, status
);
597 dev
= &edge_serial
->serial
->dev
->dev
;
599 /* process this interrupt-read even if there are no ports open */
601 usb_serial_debug_data(dev
, __func__
, length
, data
);
604 bytes_avail
= data
[0] | (data
[1] << 8);
606 spin_lock(&edge_serial
->es_lock
);
607 edge_serial
->rxBytesAvail
+= bytes_avail
;
609 "%s - bytes_avail=%d, rxBytesAvail=%d, read_in_progress=%d\n",
610 __func__
, bytes_avail
,
611 edge_serial
->rxBytesAvail
,
612 edge_serial
->read_in_progress
);
614 if (edge_serial
->rxBytesAvail
> 0 &&
615 !edge_serial
->read_in_progress
) {
616 dev_dbg(dev
, "%s - posting a read\n", __func__
);
617 edge_serial
->read_in_progress
= true;
619 /* we have pending bytes on the
620 bulk in pipe, send a request */
621 result
= usb_submit_urb(edge_serial
->read_urb
, GFP_ATOMIC
);
624 "%s - usb_submit_urb(read bulk) failed with result = %d\n",
626 edge_serial
->read_in_progress
= false;
629 spin_unlock(&edge_serial
->es_lock
);
632 /* grab the txcredits for the ports if available */
635 while ((position
< length
) &&
636 (portNumber
< edge_serial
->serial
->num_ports
)) {
637 txCredits
= data
[position
] | (data
[position
+1] << 8);
639 port
= edge_serial
->serial
->port
[portNumber
];
640 edge_port
= usb_get_serial_port_data(port
);
641 if (edge_port
->open
) {
642 spin_lock(&edge_port
->ep_lock
);
643 edge_port
->txCredits
+= txCredits
;
644 spin_unlock(&edge_port
->ep_lock
);
645 dev_dbg(dev
, "%s - txcredits for port%d = %d\n",
646 __func__
, portNumber
,
647 edge_port
->txCredits
);
649 /* tell the tty driver that something
651 tty
= tty_port_tty_get(
652 &edge_port
->port
->port
);
657 /* Since we have more credit, check
658 if more data can be sent */
659 send_more_port_data(edge_serial
,
669 result
= usb_submit_urb(urb
, GFP_ATOMIC
);
671 dev_err(&urb
->dev
->dev
,
672 "%s - Error %d submitting control urb\n",
677 /*****************************************************************************
678 * edge_bulk_in_callback
679 * this is the callback function for when we have received data on the
681 *****************************************************************************/
682 static void edge_bulk_in_callback(struct urb
*urb
)
684 struct edgeport_serial
*edge_serial
= urb
->context
;
686 unsigned char *data
= urb
->transfer_buffer
;
688 __u16 raw_data_length
;
689 int status
= urb
->status
;
692 dev_dbg(&urb
->dev
->dev
, "%s - nonzero read bulk status received: %d\n",
694 edge_serial
->read_in_progress
= false;
698 if (urb
->actual_length
== 0) {
699 dev_dbg(&urb
->dev
->dev
, "%s - read bulk callback with no data\n", __func__
);
700 edge_serial
->read_in_progress
= false;
704 dev
= &edge_serial
->serial
->dev
->dev
;
705 raw_data_length
= urb
->actual_length
;
707 usb_serial_debug_data(dev
, __func__
, raw_data_length
, data
);
709 spin_lock(&edge_serial
->es_lock
);
711 /* decrement our rxBytes available by the number that we just got */
712 edge_serial
->rxBytesAvail
-= raw_data_length
;
714 dev_dbg(dev
, "%s - Received = %d, rxBytesAvail %d\n", __func__
,
715 raw_data_length
, edge_serial
->rxBytesAvail
);
717 process_rcvd_data(edge_serial
, data
, urb
->actual_length
);
719 /* check to see if there's any more data for us to read */
720 if (edge_serial
->rxBytesAvail
> 0) {
721 dev_dbg(dev
, "%s - posting a read\n", __func__
);
722 retval
= usb_submit_urb(edge_serial
->read_urb
, GFP_ATOMIC
);
725 "%s - usb_submit_urb(read bulk) failed, retval = %d\n",
727 edge_serial
->read_in_progress
= false;
730 edge_serial
->read_in_progress
= false;
733 spin_unlock(&edge_serial
->es_lock
);
737 /*****************************************************************************
738 * edge_bulk_out_data_callback
739 * this is the callback function for when we have finished sending
740 * serial data on the bulk out endpoint.
741 *****************************************************************************/
742 static void edge_bulk_out_data_callback(struct urb
*urb
)
744 struct edgeport_port
*edge_port
= urb
->context
;
745 struct tty_struct
*tty
;
746 int status
= urb
->status
;
749 dev_dbg(&urb
->dev
->dev
,
750 "%s - nonzero write bulk status received: %d\n",
754 tty
= tty_port_tty_get(&edge_port
->port
->port
);
756 if (tty
&& edge_port
->open
) {
757 /* let the tty driver wakeup if it has a special
758 write_wakeup function */
763 /* Release the Write URB */
764 edge_port
->write_in_progress
= false;
766 /* Check if more data needs to be sent */
767 send_more_port_data((struct edgeport_serial
*)
768 (usb_get_serial_data(edge_port
->port
->serial
)), edge_port
);
772 /*****************************************************************************
774 * this is the callback function for when we have finished sending a
775 * command on the bulk out endpoint.
776 *****************************************************************************/
777 static void edge_bulk_out_cmd_callback(struct urb
*urb
)
779 struct edgeport_port
*edge_port
= urb
->context
;
780 struct tty_struct
*tty
;
781 int status
= urb
->status
;
783 atomic_dec(&CmdUrbs
);
784 dev_dbg(&urb
->dev
->dev
, "%s - FREE URB %p (outstanding %d)\n",
785 __func__
, urb
, atomic_read(&CmdUrbs
));
788 /* clean up the transfer buffer */
789 kfree(urb
->transfer_buffer
);
791 /* Free the command urb */
795 dev_dbg(&urb
->dev
->dev
,
796 "%s - nonzero write bulk status received: %d\n",
801 /* Get pointer to tty */
802 tty
= tty_port_tty_get(&edge_port
->port
->port
);
804 /* tell the tty driver that something has changed */
805 if (tty
&& edge_port
->open
)
809 /* we have completed the command */
810 edge_port
->commandPending
= false;
811 wake_up(&edge_port
->wait_command
);
815 /*****************************************************************************
816 * Driver tty interface functions
817 *****************************************************************************/
819 /*****************************************************************************
821 * this function is called by the tty driver when a port is opened
822 * If successful, we return 0
823 * Otherwise we return a negative error number.
824 *****************************************************************************/
825 static int edge_open(struct tty_struct
*tty
, struct usb_serial_port
*port
)
827 struct edgeport_port
*edge_port
= usb_get_serial_port_data(port
);
828 struct device
*dev
= &port
->dev
;
829 struct usb_serial
*serial
;
830 struct edgeport_serial
*edge_serial
;
833 if (edge_port
== NULL
)
836 /* see if we've set up our endpoint info yet (can't set it up
837 in edge_startup as the structures were not set up at that time.) */
838 serial
= port
->serial
;
839 edge_serial
= usb_get_serial_data(serial
);
840 if (edge_serial
== NULL
)
842 if (edge_serial
->interrupt_in_buffer
== NULL
) {
843 struct usb_serial_port
*port0
= serial
->port
[0];
845 /* not set up yet, so do it now */
846 edge_serial
->interrupt_in_buffer
=
847 port0
->interrupt_in_buffer
;
848 edge_serial
->interrupt_in_endpoint
=
849 port0
->interrupt_in_endpointAddress
;
850 edge_serial
->interrupt_read_urb
= port0
->interrupt_in_urb
;
851 edge_serial
->bulk_in_buffer
= port0
->bulk_in_buffer
;
852 edge_serial
->bulk_in_endpoint
=
853 port0
->bulk_in_endpointAddress
;
854 edge_serial
->read_urb
= port0
->read_urb
;
855 edge_serial
->bulk_out_endpoint
=
856 port0
->bulk_out_endpointAddress
;
858 /* set up our interrupt urb */
859 usb_fill_int_urb(edge_serial
->interrupt_read_urb
,
861 usb_rcvintpipe(serial
->dev
,
862 port0
->interrupt_in_endpointAddress
),
863 port0
->interrupt_in_buffer
,
864 edge_serial
->interrupt_read_urb
->transfer_buffer_length
,
865 edge_interrupt_callback
, edge_serial
,
866 edge_serial
->interrupt_read_urb
->interval
);
868 /* set up our bulk in urb */
869 usb_fill_bulk_urb(edge_serial
->read_urb
, serial
->dev
,
870 usb_rcvbulkpipe(serial
->dev
,
871 port0
->bulk_in_endpointAddress
),
872 port0
->bulk_in_buffer
,
873 edge_serial
->read_urb
->transfer_buffer_length
,
874 edge_bulk_in_callback
, edge_serial
);
875 edge_serial
->read_in_progress
= false;
877 /* start interrupt read for this edgeport
878 * this interrupt will continue as long
879 * as the edgeport is connected */
880 response
= usb_submit_urb(edge_serial
->interrupt_read_urb
,
883 dev_err(dev
, "%s - Error %d submitting control urb\n",
888 /* initialize our wait queues */
889 init_waitqueue_head(&edge_port
->wait_open
);
890 init_waitqueue_head(&edge_port
->wait_chase
);
891 init_waitqueue_head(&edge_port
->delta_msr_wait
);
892 init_waitqueue_head(&edge_port
->wait_command
);
894 /* initialize our icount structure */
895 memset(&(edge_port
->icount
), 0x00, sizeof(edge_port
->icount
));
897 /* initialize our port settings */
898 edge_port
->txCredits
= 0; /* Can't send any data yet */
899 /* Must always set this bit to enable ints! */
900 edge_port
->shadowMCR
= MCR_MASTER_IE
;
901 edge_port
->chaseResponsePending
= false;
903 /* send a open port command */
904 edge_port
->openPending
= true;
905 edge_port
->open
= false;
906 response
= send_iosp_ext_cmd(edge_port
, IOSP_CMD_OPEN_PORT
, 0);
909 dev_err(dev
, "%s - error sending open port command\n", __func__
);
910 edge_port
->openPending
= false;
914 /* now wait for the port to be completely opened */
915 wait_event_timeout(edge_port
->wait_open
, !edge_port
->openPending
,
918 if (!edge_port
->open
) {
920 dev_dbg(dev
, "%s - open timedout\n", __func__
);
921 edge_port
->openPending
= false;
925 /* create the txfifo */
926 edge_port
->txfifo
.head
= 0;
927 edge_port
->txfifo
.tail
= 0;
928 edge_port
->txfifo
.count
= 0;
929 edge_port
->txfifo
.size
= edge_port
->maxTxCredits
;
930 edge_port
->txfifo
.fifo
= kmalloc(edge_port
->maxTxCredits
, GFP_KERNEL
);
932 if (!edge_port
->txfifo
.fifo
) {
933 dev_dbg(dev
, "%s - no memory\n", __func__
);
938 /* Allocate a URB for the write */
939 edge_port
->write_urb
= usb_alloc_urb(0, GFP_KERNEL
);
940 edge_port
->write_in_progress
= false;
942 if (!edge_port
->write_urb
) {
943 dev_dbg(dev
, "%s - no memory\n", __func__
);
948 dev_dbg(dev
, "%s(%d) - Initialize TX fifo to %d bytes\n",
949 __func__
, port
->number
, edge_port
->maxTxCredits
);
955 /************************************************************************
957 * block_until_chase_response
959 * This function will block the close until one of the following:
960 * 1. Response to our Chase comes from Edgeport
961 * 2. A timeout of 10 seconds without activity has expired
962 * (1K of Edgeport data @ 2400 baud ==> 4 sec to empty)
964 ************************************************************************/
965 static void block_until_chase_response(struct edgeport_port
*edge_port
)
967 struct device
*dev
= &edge_port
->port
->dev
;
974 /* Save Last credits */
975 lastCredits
= edge_port
->txCredits
;
977 /* Did we get our Chase response */
978 if (!edge_port
->chaseResponsePending
) {
979 dev_dbg(dev
, "%s - Got Chase Response\n", __func__
);
981 /* did we get all of our credit back? */
982 if (edge_port
->txCredits
== edge_port
->maxTxCredits
) {
983 dev_dbg(dev
, "%s - Got all credits\n", __func__
);
988 /* Block the thread for a while */
989 prepare_to_wait(&edge_port
->wait_chase
, &wait
,
990 TASK_UNINTERRUPTIBLE
);
991 schedule_timeout(timeout
);
992 finish_wait(&edge_port
->wait_chase
, &wait
);
994 if (lastCredits
== edge_port
->txCredits
) {
995 /* No activity.. count down. */
998 edge_port
->chaseResponsePending
= false;
999 dev_dbg(dev
, "%s - Chase TIMEOUT\n", __func__
);
1003 /* Reset timeout value back to 10 seconds */
1004 dev_dbg(dev
, "%s - Last %d, Current %d\n", __func__
,
1005 lastCredits
, edge_port
->txCredits
);
1012 /************************************************************************
1014 * block_until_tx_empty
1016 * This function will block the close until one of the following:
1018 * 2. The edgeport has stopped
1019 * 3. A timeout of 3 seconds without activity has expired
1021 ************************************************************************/
1022 static void block_until_tx_empty(struct edgeport_port
*edge_port
)
1024 struct device
*dev
= &edge_port
->port
->dev
;
1026 struct TxFifo
*fifo
= &edge_port
->txfifo
;
1028 int timeout
= HZ
/10;
1032 /* Save Last count */
1033 lastCount
= fifo
->count
;
1035 /* Is the Edgeport Buffer empty? */
1036 if (lastCount
== 0) {
1037 dev_dbg(dev
, "%s - TX Buffer Empty\n", __func__
);
1041 /* Block the thread for a while */
1042 prepare_to_wait(&edge_port
->wait_chase
, &wait
,
1043 TASK_UNINTERRUPTIBLE
);
1044 schedule_timeout(timeout
);
1045 finish_wait(&edge_port
->wait_chase
, &wait
);
1047 dev_dbg(dev
, "%s wait\n", __func__
);
1049 if (lastCount
== fifo
->count
) {
1050 /* No activity.. count down. */
1053 dev_dbg(dev
, "%s - TIMEOUT\n", __func__
);
1057 /* Reset timeout value back to seconds */
1064 /*****************************************************************************
1066 * this function is called by the tty driver when a port is closed
1067 *****************************************************************************/
1068 static void edge_close(struct usb_serial_port
*port
)
1070 struct edgeport_serial
*edge_serial
;
1071 struct edgeport_port
*edge_port
;
1074 edge_serial
= usb_get_serial_data(port
->serial
);
1075 edge_port
= usb_get_serial_port_data(port
);
1076 if (edge_serial
== NULL
|| edge_port
== NULL
)
1079 /* block until tx is empty */
1080 block_until_tx_empty(edge_port
);
1082 edge_port
->closePending
= true;
1084 if ((!edge_serial
->is_epic
) ||
1085 ((edge_serial
->is_epic
) &&
1086 (edge_serial
->epic_descriptor
.Supports
.IOSPChase
))) {
1087 /* flush and chase */
1088 edge_port
->chaseResponsePending
= true;
1090 dev_dbg(&port
->dev
, "%s - Sending IOSP_CMD_CHASE_PORT\n", __func__
);
1091 status
= send_iosp_ext_cmd(edge_port
, IOSP_CMD_CHASE_PORT
, 0);
1093 /* block until chase finished */
1094 block_until_chase_response(edge_port
);
1096 edge_port
->chaseResponsePending
= false;
1099 if ((!edge_serial
->is_epic
) ||
1100 ((edge_serial
->is_epic
) &&
1101 (edge_serial
->epic_descriptor
.Supports
.IOSPClose
))) {
1102 /* close the port */
1103 dev_dbg(&port
->dev
, "%s - Sending IOSP_CMD_CLOSE_PORT\n", __func__
);
1104 send_iosp_ext_cmd(edge_port
, IOSP_CMD_CLOSE_PORT
, 0);
1107 /* port->close = true; */
1108 edge_port
->closePending
= false;
1109 edge_port
->open
= false;
1110 edge_port
->openPending
= false;
1112 usb_kill_urb(edge_port
->write_urb
);
1114 if (edge_port
->write_urb
) {
1115 /* if this urb had a transfer buffer already
1116 (old transfer) free it */
1117 kfree(edge_port
->write_urb
->transfer_buffer
);
1118 usb_free_urb(edge_port
->write_urb
);
1119 edge_port
->write_urb
= NULL
;
1121 kfree(edge_port
->txfifo
.fifo
);
1122 edge_port
->txfifo
.fifo
= NULL
;
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 if (edge_port
== NULL
)
1146 /* get a pointer to the Tx fifo */
1147 fifo
= &edge_port
->txfifo
;
1149 spin_lock_irqsave(&edge_port
->ep_lock
, flags
);
1151 /* calculate number of bytes to put in fifo */
1152 copySize
= min((unsigned int)count
,
1153 (edge_port
->txCredits
- fifo
->count
));
1155 dev_dbg(&port
->dev
, "%s(%d) of %d byte(s) Fifo room %d -- will copy %d bytes\n",
1156 __func__
, port
->number
, count
,
1157 edge_port
->txCredits
- fifo
->count
, copySize
);
1159 /* catch writes of 0 bytes which the tty driver likes to give us,
1160 and when txCredits is empty */
1161 if (copySize
== 0) {
1162 dev_dbg(&port
->dev
, "%s - copySize = Zero\n", __func__
);
1167 * since we can never overflow the buffer we do not have to check for a
1170 * the copy is done is two parts -- first fill to the end of the buffer
1171 * then copy the reset from the start of the buffer
1173 bytesleft
= fifo
->size
- fifo
->head
;
1174 firsthalf
= min(bytesleft
, copySize
);
1175 dev_dbg(&port
->dev
, "%s - copy %d bytes of %d into fifo \n", __func__
,
1176 firsthalf
, bytesleft
);
1178 /* now copy our data */
1179 memcpy(&fifo
->fifo
[fifo
->head
], data
, firsthalf
);
1180 usb_serial_debug_data(&port
->dev
, __func__
, firsthalf
, &fifo
->fifo
[fifo
->head
]);
1182 /* update the index and size */
1183 fifo
->head
+= firsthalf
;
1184 fifo
->count
+= firsthalf
;
1186 /* wrap the index */
1187 if (fifo
->head
== fifo
->size
)
1190 secondhalf
= copySize
-firsthalf
;
1193 dev_dbg(&port
->dev
, "%s - copy rest of data %d\n", __func__
, secondhalf
);
1194 memcpy(&fifo
->fifo
[fifo
->head
], &data
[firsthalf
], secondhalf
);
1195 usb_serial_debug_data(&port
->dev
, __func__
, secondhalf
, &fifo
->fifo
[fifo
->head
]);
1196 /* update the index and size */
1197 fifo
->count
+= secondhalf
;
1198 fifo
->head
+= secondhalf
;
1199 /* No need to check for wrap since we can not get to end of
1200 * the fifo in this part
1205 spin_unlock_irqrestore(&edge_port
->ep_lock
, flags
);
1207 send_more_port_data((struct edgeport_serial
*)
1208 usb_get_serial_data(port
->serial
), edge_port
);
1210 dev_dbg(&port
->dev
, "%s wrote %d byte(s) TxCredits %d, Fifo %d\n",
1211 __func__
, copySize
, edge_port
->txCredits
, fifo
->count
);
1217 /************************************************************************
1219 * send_more_port_data()
1221 * This routine attempts to write additional UART transmit data
1222 * to a port over the USB bulk pipe. It is called (1) when new
1223 * data has been written to a port's TxBuffer from higher layers
1224 * (2) when the peripheral sends us additional TxCredits indicating
1225 * that it can accept more Tx data for a given port; and (3) when
1226 * a bulk write completes successfully and we want to see if we
1227 * can transmit more.
1229 ************************************************************************/
1230 static void send_more_port_data(struct edgeport_serial
*edge_serial
,
1231 struct edgeport_port
*edge_port
)
1233 struct TxFifo
*fifo
= &edge_port
->txfifo
;
1234 struct device
*dev
= &edge_port
->port
->dev
;
1236 unsigned char *buffer
;
1242 unsigned long flags
;
1244 spin_lock_irqsave(&edge_port
->ep_lock
, flags
);
1246 if (edge_port
->write_in_progress
||
1248 (fifo
->count
== 0)) {
1249 dev_dbg(dev
, "%s(%d) EXIT - fifo %d, PendingWrite = %d\n",
1250 __func__
, edge_port
->port
->number
,
1251 fifo
->count
, edge_port
->write_in_progress
);
1255 /* since the amount of data in the fifo will always fit into the
1256 * edgeport buffer we do not need to check the write length
1258 * Do we have enough credits for this port to make it worthwhile
1259 * to bother queueing a write. If it's too small, say a few bytes,
1260 * it's better to wait for more credits so we can do a larger write.
1262 if (edge_port
->txCredits
< EDGE_FW_GET_TX_CREDITS_SEND_THRESHOLD(edge_port
->maxTxCredits
, EDGE_FW_BULK_MAX_PACKET_SIZE
)) {
1263 dev_dbg(dev
, "%s(%d) Not enough credit - fifo %d TxCredit %d\n",
1264 __func__
, edge_port
->port
->number
, fifo
->count
,
1265 edge_port
->txCredits
);
1269 /* lock this write */
1270 edge_port
->write_in_progress
= true;
1272 /* get a pointer to the write_urb */
1273 urb
= edge_port
->write_urb
;
1275 /* make sure transfer buffer is freed */
1276 kfree(urb
->transfer_buffer
);
1277 urb
->transfer_buffer
= NULL
;
1279 /* build the data header for the buffer and port that we are about
1281 count
= fifo
->count
;
1282 buffer
= kmalloc(count
+2, GFP_ATOMIC
);
1283 if (buffer
== NULL
) {
1284 dev_err_console(edge_port
->port
,
1285 "%s - no more kernel memory...\n", __func__
);
1286 edge_port
->write_in_progress
= false;
1289 buffer
[0] = IOSP_BUILD_DATA_HDR1(edge_port
->port
->number
1290 - edge_port
->port
->serial
->minor
, count
);
1291 buffer
[1] = IOSP_BUILD_DATA_HDR2(edge_port
->port
->number
1292 - edge_port
->port
->serial
->minor
, count
);
1294 /* now copy our data */
1295 bytesleft
= fifo
->size
- fifo
->tail
;
1296 firsthalf
= min(bytesleft
, count
);
1297 memcpy(&buffer
[2], &fifo
->fifo
[fifo
->tail
], firsthalf
);
1298 fifo
->tail
+= firsthalf
;
1299 fifo
->count
-= firsthalf
;
1300 if (fifo
->tail
== fifo
->size
)
1303 secondhalf
= count
-firsthalf
;
1305 memcpy(&buffer
[2+firsthalf
], &fifo
->fifo
[fifo
->tail
],
1307 fifo
->tail
+= secondhalf
;
1308 fifo
->count
-= secondhalf
;
1312 usb_serial_debug_data(&edge_port
->port
->dev
, __func__
, count
, &buffer
[2]);
1314 /* fill up the urb with all of our data and submit it */
1315 usb_fill_bulk_urb(urb
, edge_serial
->serial
->dev
,
1316 usb_sndbulkpipe(edge_serial
->serial
->dev
,
1317 edge_serial
->bulk_out_endpoint
),
1319 edge_bulk_out_data_callback
, edge_port
);
1321 /* decrement the number of credits we have by the number we just sent */
1322 edge_port
->txCredits
-= count
;
1323 edge_port
->icount
.tx
+= count
;
1325 status
= usb_submit_urb(urb
, GFP_ATOMIC
);
1327 /* something went wrong */
1328 dev_err_console(edge_port
->port
,
1329 "%s - usb_submit_urb(write bulk) failed, status = %d, data lost\n",
1331 edge_port
->write_in_progress
= false;
1333 /* revert the credits as something bad happened. */
1334 edge_port
->txCredits
+= count
;
1335 edge_port
->icount
.tx
-= count
;
1337 dev_dbg(dev
, "%s wrote %d byte(s) TxCredit %d, Fifo %d\n",
1338 __func__
, count
, edge_port
->txCredits
, fifo
->count
);
1341 spin_unlock_irqrestore(&edge_port
->ep_lock
, flags
);
1345 /*****************************************************************************
1347 * this function is called by the tty driver when it wants to know how
1348 * many bytes of data we can accept for a specific port. If successful,
1349 * we return the amount of room that we have for this port (the txCredits)
1350 * otherwise we return a negative error number.
1351 *****************************************************************************/
1352 static int edge_write_room(struct tty_struct
*tty
)
1354 struct usb_serial_port
*port
= tty
->driver_data
;
1355 struct edgeport_port
*edge_port
= usb_get_serial_port_data(port
);
1357 unsigned long flags
;
1359 if (edge_port
== NULL
)
1361 if (edge_port
->closePending
)
1364 if (!edge_port
->open
) {
1365 dev_dbg(&port
->dev
, "%s - port not opened\n", __func__
);
1369 /* total of both buffers is still txCredit */
1370 spin_lock_irqsave(&edge_port
->ep_lock
, flags
);
1371 room
= edge_port
->txCredits
- edge_port
->txfifo
.count
;
1372 spin_unlock_irqrestore(&edge_port
->ep_lock
, flags
);
1374 dev_dbg(&port
->dev
, "%s - returns %d\n", __func__
, room
);
1379 /*****************************************************************************
1380 * edge_chars_in_buffer
1381 * this function is called by the tty driver when it wants to know how
1382 * many bytes of data we currently have outstanding in the port (data that
1383 * has been written, but hasn't made it out the port yet)
1384 * If successful, we return the number of bytes left to be written in the
1386 * Otherwise we return a negative error number.
1387 *****************************************************************************/
1388 static int edge_chars_in_buffer(struct tty_struct
*tty
)
1390 struct usb_serial_port
*port
= tty
->driver_data
;
1391 struct edgeport_port
*edge_port
= usb_get_serial_port_data(port
);
1393 unsigned long flags
;
1395 if (edge_port
== NULL
)
1397 if (edge_port
->closePending
)
1400 if (!edge_port
->open
) {
1401 dev_dbg(&port
->dev
, "%s - port not opened\n", __func__
);
1405 spin_lock_irqsave(&edge_port
->ep_lock
, flags
);
1406 num_chars
= edge_port
->maxTxCredits
- edge_port
->txCredits
+
1407 edge_port
->txfifo
.count
;
1408 spin_unlock_irqrestore(&edge_port
->ep_lock
, flags
);
1410 dev_dbg(&port
->dev
, "%s(port %d) - returns %d\n", __func__
,
1411 port
->number
, num_chars
);
1418 /*****************************************************************************
1420 * this function is called by the tty driver when it wants to stop the data
1421 * being read from the port.
1422 *****************************************************************************/
1423 static void edge_throttle(struct tty_struct
*tty
)
1425 struct usb_serial_port
*port
= tty
->driver_data
;
1426 struct edgeport_port
*edge_port
= usb_get_serial_port_data(port
);
1429 if (edge_port
== NULL
)
1432 if (!edge_port
->open
) {
1433 dev_dbg(&port
->dev
, "%s - port not opened\n", __func__
);
1437 /* if we are implementing XON/XOFF, send the stop character */
1439 unsigned char stop_char
= STOP_CHAR(tty
);
1440 status
= edge_write(tty
, port
, &stop_char
, 1);
1445 /* if we are implementing RTS/CTS, toggle that line */
1446 if (tty
->termios
.c_cflag
& CRTSCTS
) {
1447 edge_port
->shadowMCR
&= ~MCR_RTS
;
1448 status
= send_cmd_write_uart_register(edge_port
, MCR
,
1449 edge_port
->shadowMCR
);
1456 /*****************************************************************************
1458 * this function is called by the tty driver when it wants to resume the
1459 * data being read from the port (called after SerialThrottle is called)
1460 *****************************************************************************/
1461 static void edge_unthrottle(struct tty_struct
*tty
)
1463 struct usb_serial_port
*port
= tty
->driver_data
;
1464 struct edgeport_port
*edge_port
= usb_get_serial_port_data(port
);
1467 if (edge_port
== NULL
)
1470 if (!edge_port
->open
) {
1471 dev_dbg(&port
->dev
, "%s - port not opened\n", __func__
);
1475 /* if we are implementing XON/XOFF, send the start character */
1477 unsigned char start_char
= START_CHAR(tty
);
1478 status
= edge_write(tty
, port
, &start_char
, 1);
1482 /* if we are implementing RTS/CTS, toggle that line */
1483 if (tty
->termios
.c_cflag
& CRTSCTS
) {
1484 edge_port
->shadowMCR
|= MCR_RTS
;
1485 send_cmd_write_uart_register(edge_port
, MCR
,
1486 edge_port
->shadowMCR
);
1491 /*****************************************************************************
1493 * this function is called by the tty driver when it wants to change
1494 * the termios structure
1495 *****************************************************************************/
1496 static void edge_set_termios(struct tty_struct
*tty
,
1497 struct usb_serial_port
*port
, struct ktermios
*old_termios
)
1499 struct edgeport_port
*edge_port
= usb_get_serial_port_data(port
);
1502 cflag
= tty
->termios
.c_cflag
;
1503 dev_dbg(&port
->dev
, "%s - clfag %08x iflag %08x\n", __func__
, tty
->termios
.c_cflag
, tty
->termios
.c_iflag
);
1504 dev_dbg(&port
->dev
, "%s - old clfag %08x old iflag %08x\n", __func__
, old_termios
->c_cflag
, old_termios
->c_iflag
);
1506 if (edge_port
== NULL
)
1509 if (!edge_port
->open
) {
1510 dev_dbg(&port
->dev
, "%s - port not opened\n", __func__
);
1514 /* change the port settings to the new ones specified */
1515 change_port_settings(tty
, edge_port
, old_termios
);
1519 /*****************************************************************************
1520 * get_lsr_info - get line status register info
1522 * Purpose: Let user call ioctl() to get info when the UART physically
1523 * is emptied. On bus types like RS485, the transmitter must
1524 * release the bus after transmitting. This must be done when
1525 * the transmit shift register is empty, not be done when the
1526 * transmit holding register is empty. This functionality
1527 * allows an RS485 driver to be written in user space.
1528 *****************************************************************************/
1529 static int get_lsr_info(struct edgeport_port
*edge_port
,
1530 unsigned int __user
*value
)
1532 unsigned int result
= 0;
1533 unsigned long flags
;
1535 spin_lock_irqsave(&edge_port
->ep_lock
, flags
);
1536 if (edge_port
->maxTxCredits
== edge_port
->txCredits
&&
1537 edge_port
->txfifo
.count
== 0) {
1538 dev_dbg(&edge_port
->port
->dev
, "%s -- Empty\n", __func__
);
1539 result
= TIOCSER_TEMT
;
1541 spin_unlock_irqrestore(&edge_port
->ep_lock
, flags
);
1543 if (copy_to_user(value
, &result
, sizeof(int)))
1548 static int edge_tiocmset(struct tty_struct
*tty
,
1549 unsigned int set
, unsigned int clear
)
1551 struct usb_serial_port
*port
= tty
->driver_data
;
1552 struct edgeport_port
*edge_port
= usb_get_serial_port_data(port
);
1555 mcr
= edge_port
->shadowMCR
;
1556 if (set
& TIOCM_RTS
)
1558 if (set
& TIOCM_DTR
)
1560 if (set
& TIOCM_LOOP
)
1561 mcr
|= MCR_LOOPBACK
;
1563 if (clear
& TIOCM_RTS
)
1565 if (clear
& TIOCM_DTR
)
1567 if (clear
& TIOCM_LOOP
)
1568 mcr
&= ~MCR_LOOPBACK
;
1570 edge_port
->shadowMCR
= mcr
;
1572 send_cmd_write_uart_register(edge_port
, MCR
, edge_port
->shadowMCR
);
1577 static int edge_tiocmget(struct tty_struct
*tty
)
1579 struct usb_serial_port
*port
= tty
->driver_data
;
1580 struct edgeport_port
*edge_port
= usb_get_serial_port_data(port
);
1581 unsigned int result
= 0;
1585 msr
= edge_port
->shadowMSR
;
1586 mcr
= edge_port
->shadowMCR
;
1587 result
= ((mcr
& MCR_DTR
) ? TIOCM_DTR
: 0) /* 0x002 */
1588 | ((mcr
& MCR_RTS
) ? TIOCM_RTS
: 0) /* 0x004 */
1589 | ((msr
& EDGEPORT_MSR_CTS
) ? TIOCM_CTS
: 0) /* 0x020 */
1590 | ((msr
& EDGEPORT_MSR_CD
) ? TIOCM_CAR
: 0) /* 0x040 */
1591 | ((msr
& EDGEPORT_MSR_RI
) ? TIOCM_RI
: 0) /* 0x080 */
1592 | ((msr
& EDGEPORT_MSR_DSR
) ? TIOCM_DSR
: 0); /* 0x100 */
1597 static int edge_get_icount(struct tty_struct
*tty
,
1598 struct serial_icounter_struct
*icount
)
1600 struct usb_serial_port
*port
= tty
->driver_data
;
1601 struct edgeport_port
*edge_port
= usb_get_serial_port_data(port
);
1602 struct async_icount cnow
;
1603 cnow
= edge_port
->icount
;
1605 icount
->cts
= cnow
.cts
;
1606 icount
->dsr
= cnow
.dsr
;
1607 icount
->rng
= cnow
.rng
;
1608 icount
->dcd
= cnow
.dcd
;
1609 icount
->rx
= cnow
.rx
;
1610 icount
->tx
= cnow
.tx
;
1611 icount
->frame
= cnow
.frame
;
1612 icount
->overrun
= cnow
.overrun
;
1613 icount
->parity
= cnow
.parity
;
1614 icount
->brk
= cnow
.brk
;
1615 icount
->buf_overrun
= cnow
.buf_overrun
;
1617 dev_dbg(&port
->dev
, "%s (%d) TIOCGICOUNT RX=%d, TX=%d\n", __func__
,
1618 port
->number
, icount
->rx
, icount
->tx
);
1622 static int get_serial_info(struct edgeport_port
*edge_port
,
1623 struct serial_struct __user
*retinfo
)
1625 struct serial_struct tmp
;
1630 memset(&tmp
, 0, sizeof(tmp
));
1632 tmp
.type
= PORT_16550A
;
1633 tmp
.line
= edge_port
->port
->serial
->minor
;
1634 tmp
.port
= edge_port
->port
->number
;
1636 tmp
.flags
= ASYNC_SKIP_TEST
| ASYNC_AUTO_IRQ
;
1637 tmp
.xmit_fifo_size
= edge_port
->maxTxCredits
;
1638 tmp
.baud_base
= 9600;
1639 tmp
.close_delay
= 5*HZ
;
1640 tmp
.closing_wait
= 30*HZ
;
1642 if (copy_to_user(retinfo
, &tmp
, sizeof(*retinfo
)))
1648 /*****************************************************************************
1650 * this function handles any ioctl calls to the driver
1651 *****************************************************************************/
1652 static int edge_ioctl(struct tty_struct
*tty
,
1653 unsigned int cmd
, unsigned long arg
)
1655 struct usb_serial_port
*port
= tty
->driver_data
;
1657 struct edgeport_port
*edge_port
= usb_get_serial_port_data(port
);
1658 struct async_icount cnow
;
1659 struct async_icount cprev
;
1661 dev_dbg(&port
->dev
, "%s - port %d, cmd = 0x%x\n", __func__
, port
->number
, cmd
);
1665 dev_dbg(&port
->dev
, "%s (%d) TIOCSERGETLSR\n", __func__
, port
->number
);
1666 return get_lsr_info(edge_port
, (unsigned int __user
*) arg
);
1669 dev_dbg(&port
->dev
, "%s (%d) TIOCGSERIAL\n", __func__
, port
->number
);
1670 return get_serial_info(edge_port
, (struct serial_struct __user
*) arg
);
1673 dev_dbg(&port
->dev
, "%s (%d) TIOCMIWAIT\n", __func__
, port
->number
);
1674 cprev
= edge_port
->icount
;
1676 prepare_to_wait(&edge_port
->delta_msr_wait
,
1677 &wait
, TASK_INTERRUPTIBLE
);
1679 finish_wait(&edge_port
->delta_msr_wait
, &wait
);
1680 /* see if a signal did it */
1681 if (signal_pending(current
))
1682 return -ERESTARTSYS
;
1683 cnow
= edge_port
->icount
;
1684 if (cnow
.rng
== cprev
.rng
&& cnow
.dsr
== cprev
.dsr
&&
1685 cnow
.dcd
== cprev
.dcd
&& cnow
.cts
== cprev
.cts
)
1686 return -EIO
; /* no change => error */
1687 if (((arg
& TIOCM_RNG
) && (cnow
.rng
!= cprev
.rng
)) ||
1688 ((arg
& TIOCM_DSR
) && (cnow
.dsr
!= cprev
.dsr
)) ||
1689 ((arg
& TIOCM_CD
) && (cnow
.dcd
!= cprev
.dcd
)) ||
1690 ((arg
& TIOCM_CTS
) && (cnow
.cts
!= cprev
.cts
))) {
1699 return -ENOIOCTLCMD
;
1703 /*****************************************************************************
1705 * this function sends a break to the port
1706 *****************************************************************************/
1707 static void edge_break(struct tty_struct
*tty
, int break_state
)
1709 struct usb_serial_port
*port
= tty
->driver_data
;
1710 struct edgeport_port
*edge_port
= usb_get_serial_port_data(port
);
1711 struct edgeport_serial
*edge_serial
= usb_get_serial_data(port
->serial
);
1714 if ((!edge_serial
->is_epic
) ||
1715 ((edge_serial
->is_epic
) &&
1716 (edge_serial
->epic_descriptor
.Supports
.IOSPChase
))) {
1717 /* flush and chase */
1718 edge_port
->chaseResponsePending
= true;
1720 dev_dbg(&port
->dev
, "%s - Sending IOSP_CMD_CHASE_PORT\n", __func__
);
1721 status
= send_iosp_ext_cmd(edge_port
, IOSP_CMD_CHASE_PORT
, 0);
1723 /* block until chase finished */
1724 block_until_chase_response(edge_port
);
1726 edge_port
->chaseResponsePending
= false;
1730 if ((!edge_serial
->is_epic
) ||
1731 ((edge_serial
->is_epic
) &&
1732 (edge_serial
->epic_descriptor
.Supports
.IOSPSetClrBreak
))) {
1733 if (break_state
== -1) {
1734 dev_dbg(&port
->dev
, "%s - Sending IOSP_CMD_SET_BREAK\n", __func__
);
1735 status
= send_iosp_ext_cmd(edge_port
,
1736 IOSP_CMD_SET_BREAK
, 0);
1738 dev_dbg(&port
->dev
, "%s - Sending IOSP_CMD_CLEAR_BREAK\n", __func__
);
1739 status
= send_iosp_ext_cmd(edge_port
,
1740 IOSP_CMD_CLEAR_BREAK
, 0);
1743 dev_dbg(&port
->dev
, "%s - error sending break set/clear command.\n",
1749 /*****************************************************************************
1751 * this function handles the data received on the bulk in pipe.
1752 *****************************************************************************/
1753 static void process_rcvd_data(struct edgeport_serial
*edge_serial
,
1754 unsigned char *buffer
, __u16 bufferLength
)
1756 struct device
*dev
= &edge_serial
->serial
->dev
->dev
;
1757 struct usb_serial_port
*port
;
1758 struct edgeport_port
*edge_port
;
1759 struct tty_struct
*tty
;
1760 __u16 lastBufferLength
;
1763 lastBufferLength
= bufferLength
+ 1;
1765 while (bufferLength
> 0) {
1766 /* failsafe incase we get a message that we don't understand */
1767 if (lastBufferLength
== bufferLength
) {
1768 dev_dbg(dev
, "%s - stuck in loop, exiting it.\n", __func__
);
1771 lastBufferLength
= bufferLength
;
1773 switch (edge_serial
->rxState
) {
1775 edge_serial
->rxHeader1
= *buffer
;
1779 if (bufferLength
== 0) {
1780 edge_serial
->rxState
= EXPECT_HDR2
;
1783 /* otherwise, drop on through */
1785 edge_serial
->rxHeader2
= *buffer
;
1789 dev_dbg(dev
, "%s - Hdr1=%02X Hdr2=%02X\n", __func__
,
1790 edge_serial
->rxHeader1
, edge_serial
->rxHeader2
);
1791 /* Process depending on whether this header is
1794 if (IS_CMD_STAT_HDR(edge_serial
->rxHeader1
)) {
1795 /* Decode this status header and go to
1796 * EXPECT_HDR1 (if we can process the status
1797 * with only 2 bytes), or go to EXPECT_HDR3 to
1798 * get the third byte. */
1799 edge_serial
->rxPort
=
1800 IOSP_GET_HDR_PORT(edge_serial
->rxHeader1
);
1801 edge_serial
->rxStatusCode
=
1802 IOSP_GET_STATUS_CODE(
1803 edge_serial
->rxHeader1
);
1805 if (!IOSP_STATUS_IS_2BYTE(
1806 edge_serial
->rxStatusCode
)) {
1807 /* This status needs additional bytes.
1808 * Save what we have and then wait for
1811 edge_serial
->rxStatusParam
1812 = edge_serial
->rxHeader2
;
1813 edge_serial
->rxState
= EXPECT_HDR3
;
1816 /* We have all the header bytes, process the
1818 process_rcvd_status(edge_serial
,
1819 edge_serial
->rxHeader2
, 0);
1820 edge_serial
->rxState
= EXPECT_HDR1
;
1823 edge_serial
->rxPort
=
1824 IOSP_GET_HDR_PORT(edge_serial
->rxHeader1
);
1825 edge_serial
->rxBytesRemaining
=
1826 IOSP_GET_HDR_DATA_LEN(
1827 edge_serial
->rxHeader1
,
1828 edge_serial
->rxHeader2
);
1829 dev_dbg(dev
, "%s - Data for Port %u Len %u\n",
1831 edge_serial
->rxPort
,
1832 edge_serial
->rxBytesRemaining
);
1834 /* ASSERT(DevExt->RxPort < DevExt->NumPorts);
1835 * ASSERT(DevExt->RxBytesRemaining <
1836 * IOSP_MAX_DATA_LENGTH);
1839 if (bufferLength
== 0) {
1840 edge_serial
->rxState
= EXPECT_DATA
;
1843 /* Else, drop through */
1845 case EXPECT_DATA
: /* Expect data */
1846 if (bufferLength
< edge_serial
->rxBytesRemaining
) {
1847 rxLen
= bufferLength
;
1848 /* Expect data to start next buffer */
1849 edge_serial
->rxState
= EXPECT_DATA
;
1851 /* BufLen >= RxBytesRemaining */
1852 rxLen
= edge_serial
->rxBytesRemaining
;
1853 /* Start another header next time */
1854 edge_serial
->rxState
= EXPECT_HDR1
;
1857 bufferLength
-= rxLen
;
1858 edge_serial
->rxBytesRemaining
-= rxLen
;
1860 /* spit this data back into the tty driver if this
1863 port
= edge_serial
->serial
->port
[
1864 edge_serial
->rxPort
];
1865 edge_port
= usb_get_serial_port_data(port
);
1866 if (edge_port
->open
) {
1867 tty
= tty_port_tty_get(
1868 &edge_port
->port
->port
);
1870 dev_dbg(dev
, "%s - Sending %d bytes to TTY for port %d\n",
1871 __func__
, rxLen
, edge_serial
->rxPort
);
1872 edge_tty_recv(&edge_serial
->serial
->dev
->dev
, tty
, buffer
, rxLen
);
1875 edge_port
->icount
.rx
+= rxLen
;
1881 case EXPECT_HDR3
: /* Expect 3rd byte of status header */
1882 edge_serial
->rxHeader3
= *buffer
;
1886 /* We have all the header bytes, process the
1888 process_rcvd_status(edge_serial
,
1889 edge_serial
->rxStatusParam
,
1890 edge_serial
->rxHeader3
);
1891 edge_serial
->rxState
= EXPECT_HDR1
;
1898 /*****************************************************************************
1899 * process_rcvd_status
1900 * this function handles the any status messages received on the
1902 *****************************************************************************/
1903 static void process_rcvd_status(struct edgeport_serial
*edge_serial
,
1904 __u8 byte2
, __u8 byte3
)
1906 struct usb_serial_port
*port
;
1907 struct edgeport_port
*edge_port
;
1908 struct tty_struct
*tty
;
1910 __u8 code
= edge_serial
->rxStatusCode
;
1912 /* switch the port pointer to the one being currently talked about */
1913 port
= edge_serial
->serial
->port
[edge_serial
->rxPort
];
1914 edge_port
= usb_get_serial_port_data(port
);
1915 if (edge_port
== NULL
) {
1916 dev_err(&edge_serial
->serial
->dev
->dev
,
1917 "%s - edge_port == NULL for port %d\n",
1918 __func__
, edge_serial
->rxPort
);
1923 if (code
== IOSP_EXT_STATUS
) {
1925 case IOSP_EXT_STATUS_CHASE_RSP
:
1926 /* we want to do EXT status regardless of port
1928 dev_dbg(dev
, "%s - Port %u EXT CHASE_RSP Data = %02x\n",
1929 __func__
, edge_serial
->rxPort
, byte3
);
1930 /* Currently, the only EXT_STATUS is Chase, so process
1931 * here instead of one more call to one more subroutine
1932 * If/when more EXT_STATUS, there'll be more work to do
1933 * Also, we currently clear flag and close the port
1934 * regardless of content of above's Byte3.
1935 * We could choose to do something else when Byte3 says
1936 * Timeout on Chase from Edgeport, like wait longer in
1937 * block_until_chase_response, but for now we don't.
1939 edge_port
->chaseResponsePending
= false;
1940 wake_up(&edge_port
->wait_chase
);
1943 case IOSP_EXT_STATUS_RX_CHECK_RSP
:
1944 dev_dbg(dev
, "%s ========== Port %u CHECK_RSP Sequence = %02x =============\n",
1945 __func__
, edge_serial
->rxPort
, byte3
);
1946 /* Port->RxCheckRsp = true; */
1951 if (code
== IOSP_STATUS_OPEN_RSP
) {
1952 edge_port
->txCredits
= GET_TX_BUFFER_SIZE(byte3
);
1953 edge_port
->maxTxCredits
= edge_port
->txCredits
;
1954 dev_dbg(dev
, "%s - Port %u Open Response Initial MSR = %02x TxBufferSize = %d\n",
1955 __func__
, edge_serial
->rxPort
, byte2
, edge_port
->txCredits
);
1956 handle_new_msr(edge_port
, byte2
);
1958 /* send the current line settings to the port so we are
1959 in sync with any further termios calls */
1960 tty
= tty_port_tty_get(&edge_port
->port
->port
);
1962 change_port_settings(tty
,
1963 edge_port
, &tty
->termios
);
1967 /* we have completed the open */
1968 edge_port
->openPending
= false;
1969 edge_port
->open
= true;
1970 wake_up(&edge_port
->wait_open
);
1974 /* If port is closed, silently discard all rcvd status. We can
1975 * have cases where buffered status is received AFTER the close
1976 * port command is sent to the Edgeport.
1978 if (!edge_port
->open
|| edge_port
->closePending
)
1982 /* Not currently sent by Edgeport */
1983 case IOSP_STATUS_LSR
:
1984 dev_dbg(dev
, "%s - Port %u LSR Status = %02x\n",
1985 __func__
, edge_serial
->rxPort
, byte2
);
1986 handle_new_lsr(edge_port
, false, byte2
, 0);
1989 case IOSP_STATUS_LSR_DATA
:
1990 dev_dbg(dev
, "%s - Port %u LSR Status = %02x, Data = %02x\n",
1991 __func__
, edge_serial
->rxPort
, byte2
, byte3
);
1992 /* byte2 is LSR Register */
1993 /* byte3 is broken data byte */
1994 handle_new_lsr(edge_port
, true, byte2
, byte3
);
1997 * case IOSP_EXT_4_STATUS:
1998 * dev_dbg(dev, "%s - Port %u LSR Status = %02x Data = %02x\n",
1999 * __func__, edge_serial->rxPort, byte2, byte3);
2002 case IOSP_STATUS_MSR
:
2003 dev_dbg(dev
, "%s - Port %u MSR Status = %02x\n",
2004 __func__
, edge_serial
->rxPort
, byte2
);
2006 * Process this new modem status and generate appropriate
2007 * events, etc, based on the new status. This routine
2008 * also saves the MSR in Port->ShadowMsr.
2010 handle_new_msr(edge_port
, byte2
);
2014 dev_dbg(dev
, "%s - Unrecognized IOSP status code %u\n", __func__
, code
);
2020 /*****************************************************************************
2022 * this function passes data on to the tty flip buffer
2023 *****************************************************************************/
2024 static void edge_tty_recv(struct device
*dev
, struct tty_struct
*tty
,
2025 unsigned char *data
, int length
)
2029 cnt
= tty_insert_flip_string(tty
, data
, length
);
2031 dev_err(dev
, "%s - dropping data, %d bytes lost\n",
2032 __func__
, length
- cnt
);
2037 tty_flip_buffer_push(tty
);
2041 /*****************************************************************************
2043 * this function handles any change to the msr register for a port.
2044 *****************************************************************************/
2045 static void handle_new_msr(struct edgeport_port
*edge_port
, __u8 newMsr
)
2047 struct async_icount
*icount
;
2049 if (newMsr
& (EDGEPORT_MSR_DELTA_CTS
| EDGEPORT_MSR_DELTA_DSR
|
2050 EDGEPORT_MSR_DELTA_RI
| EDGEPORT_MSR_DELTA_CD
)) {
2051 icount
= &edge_port
->icount
;
2053 /* update input line counters */
2054 if (newMsr
& EDGEPORT_MSR_DELTA_CTS
)
2056 if (newMsr
& EDGEPORT_MSR_DELTA_DSR
)
2058 if (newMsr
& EDGEPORT_MSR_DELTA_CD
)
2060 if (newMsr
& EDGEPORT_MSR_DELTA_RI
)
2062 wake_up_interruptible(&edge_port
->delta_msr_wait
);
2065 /* Save the new modem status */
2066 edge_port
->shadowMSR
= newMsr
& 0xf0;
2070 /*****************************************************************************
2072 * this function handles any change to the lsr register for a port.
2073 *****************************************************************************/
2074 static void handle_new_lsr(struct edgeport_port
*edge_port
, __u8 lsrData
,
2075 __u8 lsr
, __u8 data
)
2077 __u8 newLsr
= (__u8
) (lsr
& (__u8
)
2078 (LSR_OVER_ERR
| LSR_PAR_ERR
| LSR_FRM_ERR
| LSR_BREAK
));
2079 struct async_icount
*icount
;
2081 edge_port
->shadowLSR
= lsr
;
2083 if (newLsr
& LSR_BREAK
) {
2085 * Parity and Framing errors only count if they
2086 * occur exclusive of a break being
2089 newLsr
&= (__u8
)(LSR_OVER_ERR
| LSR_BREAK
);
2092 /* Place LSR data byte into Rx buffer */
2094 struct tty_struct
*tty
=
2095 tty_port_tty_get(&edge_port
->port
->port
);
2097 edge_tty_recv(&edge_port
->port
->dev
, tty
, &data
, 1);
2101 /* update input line counters */
2102 icount
= &edge_port
->icount
;
2103 if (newLsr
& LSR_BREAK
)
2105 if (newLsr
& LSR_OVER_ERR
)
2107 if (newLsr
& LSR_PAR_ERR
)
2109 if (newLsr
& LSR_FRM_ERR
)
2114 /****************************************************************************
2116 * writes a number of bytes to the Edgeport device's sram starting at the
2118 * If successful returns the number of bytes written, otherwise it returns
2119 * a negative error number of the problem.
2120 ****************************************************************************/
2121 static int sram_write(struct usb_serial
*serial
, __u16 extAddr
, __u16 addr
,
2122 __u16 length
, const __u8
*data
)
2125 __u16 current_length
;
2126 unsigned char *transfer_buffer
;
2128 dev_dbg(&serial
->dev
->dev
, "%s - %x, %x, %d\n", __func__
, extAddr
, addr
, length
);
2130 transfer_buffer
= kmalloc(64, GFP_KERNEL
);
2131 if (!transfer_buffer
) {
2132 dev_err(&serial
->dev
->dev
, "%s - kmalloc(%d) failed.\n",
2137 /* need to split these writes up into 64 byte chunks */
2139 while (length
> 0) {
2141 current_length
= 64;
2143 current_length
= length
;
2145 /* dev_dbg(&serial->dev->dev, "%s - writing %x, %x, %d\n", __func__, extAddr, addr, current_length); */
2146 memcpy(transfer_buffer
, data
, current_length
);
2147 result
= usb_control_msg(serial
->dev
,
2148 usb_sndctrlpipe(serial
->dev
, 0),
2149 USB_REQUEST_ION_WRITE_RAM
,
2150 0x40, addr
, extAddr
, transfer_buffer
,
2151 current_length
, 300);
2154 length
-= current_length
;
2155 addr
+= current_length
;
2156 data
+= current_length
;
2159 kfree(transfer_buffer
);
2164 /****************************************************************************
2166 * writes a number of bytes to the Edgeport device's ROM starting at the
2168 * If successful returns the number of bytes written, otherwise it returns
2169 * a negative error number of the problem.
2170 ****************************************************************************/
2171 static int rom_write(struct usb_serial
*serial
, __u16 extAddr
, __u16 addr
,
2172 __u16 length
, const __u8
*data
)
2175 __u16 current_length
;
2176 unsigned char *transfer_buffer
;
2178 transfer_buffer
= kmalloc(64, GFP_KERNEL
);
2179 if (!transfer_buffer
) {
2180 dev_err(&serial
->dev
->dev
, "%s - kmalloc(%d) failed.\n",
2185 /* need to split these writes up into 64 byte chunks */
2187 while (length
> 0) {
2189 current_length
= 64;
2191 current_length
= length
;
2192 memcpy(transfer_buffer
, data
, current_length
);
2193 result
= usb_control_msg(serial
->dev
,
2194 usb_sndctrlpipe(serial
->dev
, 0),
2195 USB_REQUEST_ION_WRITE_ROM
, 0x40,
2197 transfer_buffer
, current_length
, 300);
2200 length
-= current_length
;
2201 addr
+= current_length
;
2202 data
+= current_length
;
2205 kfree(transfer_buffer
);
2210 /****************************************************************************
2212 * reads a number of bytes from the Edgeport device starting at the given
2214 * If successful returns the number of bytes read, otherwise it returns
2215 * a negative error number of the problem.
2216 ****************************************************************************/
2217 static int rom_read(struct usb_serial
*serial
, __u16 extAddr
,
2218 __u16 addr
, __u16 length
, __u8
*data
)
2221 __u16 current_length
;
2222 unsigned char *transfer_buffer
;
2224 transfer_buffer
= kmalloc(64, GFP_KERNEL
);
2225 if (!transfer_buffer
) {
2226 dev_err(&serial
->dev
->dev
,
2227 "%s - kmalloc(%d) failed.\n", __func__
, 64);
2231 /* need to split these reads up into 64 byte chunks */
2233 while (length
> 0) {
2235 current_length
= 64;
2237 current_length
= length
;
2238 result
= usb_control_msg(serial
->dev
,
2239 usb_rcvctrlpipe(serial
->dev
, 0),
2240 USB_REQUEST_ION_READ_ROM
,
2241 0xC0, addr
, extAddr
, transfer_buffer
,
2242 current_length
, 300);
2245 memcpy(data
, transfer_buffer
, current_length
);
2246 length
-= current_length
;
2247 addr
+= current_length
;
2248 data
+= current_length
;
2251 kfree(transfer_buffer
);
2256 /****************************************************************************
2258 * Is used to send a IOSP message to the Edgeport device
2259 ****************************************************************************/
2260 static int send_iosp_ext_cmd(struct edgeport_port
*edge_port
,
2261 __u8 command
, __u8 param
)
2263 unsigned char *buffer
;
2264 unsigned char *currentCommand
;
2268 buffer
= kmalloc(10, GFP_ATOMIC
);
2270 dev_err(&edge_port
->port
->dev
,
2271 "%s - kmalloc(%d) failed.\n", __func__
, 10);
2275 currentCommand
= buffer
;
2277 MAKE_CMD_EXT_CMD(¤tCommand
, &length
,
2278 edge_port
->port
->number
- edge_port
->port
->serial
->minor
,
2281 status
= write_cmd_usb(edge_port
, buffer
, length
);
2283 /* something bad happened, let's free up the memory */
2291 /*****************************************************************************
2293 * this function writes the given buffer out to the bulk write endpoint.
2294 *****************************************************************************/
2295 static int write_cmd_usb(struct edgeport_port
*edge_port
,
2296 unsigned char *buffer
, int length
)
2298 struct edgeport_serial
*edge_serial
=
2299 usb_get_serial_data(edge_port
->port
->serial
);
2300 struct device
*dev
= &edge_port
->port
->dev
;
2304 usb_serial_debug_data(dev
, __func__
, length
, buffer
);
2306 /* Allocate our next urb */
2307 urb
= usb_alloc_urb(0, GFP_ATOMIC
);
2311 atomic_inc(&CmdUrbs
);
2312 dev_dbg(dev
, "%s - ALLOCATE URB %p (outstanding %d)\n",
2313 __func__
, urb
, atomic_read(&CmdUrbs
));
2315 usb_fill_bulk_urb(urb
, edge_serial
->serial
->dev
,
2316 usb_sndbulkpipe(edge_serial
->serial
->dev
,
2317 edge_serial
->bulk_out_endpoint
),
2318 buffer
, length
, edge_bulk_out_cmd_callback
, edge_port
);
2320 edge_port
->commandPending
= true;
2321 status
= usb_submit_urb(urb
, GFP_ATOMIC
);
2324 /* something went wrong */
2325 dev_err(dev
, "%s - usb_submit_urb(write command) failed, status = %d\n",
2329 atomic_dec(&CmdUrbs
);
2334 wait_event(&edge_port
->wait_command
, !edge_port
->commandPending
);
2336 if (edge_port
->commandPending
) {
2337 /* command timed out */
2338 dev_dbg(dev
, "%s - command timed out\n", __func__
);
2346 /*****************************************************************************
2347 * send_cmd_write_baud_rate
2348 * this function sends the proper command to change the baud rate of the
2350 *****************************************************************************/
2351 static int send_cmd_write_baud_rate(struct edgeport_port
*edge_port
,
2354 struct edgeport_serial
*edge_serial
=
2355 usb_get_serial_data(edge_port
->port
->serial
);
2356 struct device
*dev
= &edge_port
->port
->dev
;
2357 unsigned char *cmdBuffer
;
2358 unsigned char *currCmd
;
2362 unsigned char number
=
2363 edge_port
->port
->number
- edge_port
->port
->serial
->minor
;
2365 if (edge_serial
->is_epic
&&
2366 !edge_serial
->epic_descriptor
.Supports
.IOSPSetBaudRate
) {
2367 dev_dbg(dev
, "SendCmdWriteBaudRate - NOT Setting baud rate for port = %d, baud = %d\n",
2368 edge_port
->port
->number
, baudRate
);
2372 dev_dbg(dev
, "%s - port = %d, baud = %d\n", __func__
,
2373 edge_port
->port
->number
, baudRate
);
2375 status
= calc_baud_rate_divisor(dev
, baudRate
, &divisor
);
2377 dev_err(dev
, "%s - bad baud rate\n", __func__
);
2381 /* Alloc memory for the string of commands. */
2382 cmdBuffer
= kmalloc(0x100, GFP_ATOMIC
);
2384 dev_err(dev
, "%s - kmalloc(%d) failed.\n", __func__
, 0x100);
2387 currCmd
= cmdBuffer
;
2389 /* Enable access to divisor latch */
2390 MAKE_CMD_WRITE_REG(&currCmd
, &cmdLen
, number
, LCR
, LCR_DL_ENABLE
);
2392 /* Write the divisor itself */
2393 MAKE_CMD_WRITE_REG(&currCmd
, &cmdLen
, number
, DLL
, LOW8(divisor
));
2394 MAKE_CMD_WRITE_REG(&currCmd
, &cmdLen
, number
, DLM
, HIGH8(divisor
));
2396 /* Restore original value to disable access to divisor latch */
2397 MAKE_CMD_WRITE_REG(&currCmd
, &cmdLen
, number
, LCR
,
2398 edge_port
->shadowLCR
);
2400 status
= write_cmd_usb(edge_port
, cmdBuffer
, cmdLen
);
2402 /* something bad happened, let's free up the memory */
2410 /*****************************************************************************
2411 * calc_baud_rate_divisor
2412 * this function calculates the proper baud rate divisor for the specified
2414 *****************************************************************************/
2415 static int calc_baud_rate_divisor(struct device
*dev
, int baudrate
, int *divisor
)
2420 for (i
= 0; i
< ARRAY_SIZE(divisor_table
); i
++) {
2421 if (divisor_table
[i
].BaudRate
== baudrate
) {
2422 *divisor
= divisor_table
[i
].Divisor
;
2427 /* We have tried all of the standard baud rates
2428 * lets try to calculate the divisor for this baud rate
2429 * Make sure the baud rate is reasonable */
2430 if (baudrate
> 50 && baudrate
< 230400) {
2432 custom
= (__u16
)((230400L + baudrate
/2) / baudrate
);
2436 dev_dbg(dev
, "%s - Baud %d = %d\n", __func__
, baudrate
, custom
);
2444 /*****************************************************************************
2445 * send_cmd_write_uart_register
2446 * this function builds up a uart register message and sends to the device.
2447 *****************************************************************************/
2448 static int send_cmd_write_uart_register(struct edgeport_port
*edge_port
,
2449 __u8 regNum
, __u8 regValue
)
2451 struct edgeport_serial
*edge_serial
=
2452 usb_get_serial_data(edge_port
->port
->serial
);
2453 struct device
*dev
= &edge_port
->port
->dev
;
2454 unsigned char *cmdBuffer
;
2455 unsigned char *currCmd
;
2456 unsigned long cmdLen
= 0;
2459 dev_dbg(dev
, "%s - write to %s register 0x%02x\n",
2460 (regNum
== MCR
) ? "MCR" : "LCR", __func__
, regValue
);
2462 if (edge_serial
->is_epic
&&
2463 !edge_serial
->epic_descriptor
.Supports
.IOSPWriteMCR
&&
2465 dev_dbg(dev
, "SendCmdWriteUartReg - Not writing to MCR Register\n");
2469 if (edge_serial
->is_epic
&&
2470 !edge_serial
->epic_descriptor
.Supports
.IOSPWriteLCR
&&
2472 dev_dbg(dev
, "SendCmdWriteUartReg - Not writing to LCR Register\n");
2476 /* Alloc memory for the string of commands. */
2477 cmdBuffer
= kmalloc(0x10, GFP_ATOMIC
);
2478 if (cmdBuffer
== NULL
)
2481 currCmd
= cmdBuffer
;
2483 /* Build a cmd in the buffer to write the given register */
2484 MAKE_CMD_WRITE_REG(&currCmd
, &cmdLen
,
2485 edge_port
->port
->number
- edge_port
->port
->serial
->minor
,
2488 status
= write_cmd_usb(edge_port
, cmdBuffer
, cmdLen
);
2490 /* something bad happened, let's free up the memory */
2498 /*****************************************************************************
2499 * change_port_settings
2500 * This routine is called to set the UART on the device to match the
2501 * specified new settings.
2502 *****************************************************************************/
2504 static void change_port_settings(struct tty_struct
*tty
,
2505 struct edgeport_port
*edge_port
, struct ktermios
*old_termios
)
2507 struct device
*dev
= &edge_port
->port
->dev
;
2508 struct edgeport_serial
*edge_serial
=
2509 usb_get_serial_data(edge_port
->port
->serial
);
2520 dev_dbg(dev
, "%s - port %d\n", __func__
, edge_port
->port
->number
);
2522 if (!edge_port
->open
&&
2523 !edge_port
->openPending
) {
2524 dev_dbg(dev
, "%s - port not opened\n", __func__
);
2528 cflag
= tty
->termios
.c_cflag
;
2530 switch (cflag
& CSIZE
) {
2532 lData
= LCR_BITS_5
; mask
= 0x1f;
2533 dev_dbg(dev
, "%s - data bits = 5\n", __func__
);
2536 lData
= LCR_BITS_6
; mask
= 0x3f;
2537 dev_dbg(dev
, "%s - data bits = 6\n", __func__
);
2540 lData
= LCR_BITS_7
; mask
= 0x7f;
2541 dev_dbg(dev
, "%s - data bits = 7\n", __func__
);
2546 dev_dbg(dev
, "%s - data bits = 8\n", __func__
);
2550 lParity
= LCR_PAR_NONE
;
2551 if (cflag
& PARENB
) {
2552 if (cflag
& CMSPAR
) {
2553 if (cflag
& PARODD
) {
2554 lParity
= LCR_PAR_MARK
;
2555 dev_dbg(dev
, "%s - parity = mark\n", __func__
);
2557 lParity
= LCR_PAR_SPACE
;
2558 dev_dbg(dev
, "%s - parity = space\n", __func__
);
2560 } else if (cflag
& PARODD
) {
2561 lParity
= LCR_PAR_ODD
;
2562 dev_dbg(dev
, "%s - parity = odd\n", __func__
);
2564 lParity
= LCR_PAR_EVEN
;
2565 dev_dbg(dev
, "%s - parity = even\n", __func__
);
2568 dev_dbg(dev
, "%s - parity = none\n", __func__
);
2571 if (cflag
& CSTOPB
) {
2573 dev_dbg(dev
, "%s - stop bits = 2\n", __func__
);
2576 dev_dbg(dev
, "%s - stop bits = 1\n", __func__
);
2579 /* figure out the flow control settings */
2580 rxFlow
= txFlow
= 0x00;
2581 if (cflag
& CRTSCTS
) {
2582 rxFlow
|= IOSP_RX_FLOW_RTS
;
2583 txFlow
|= IOSP_TX_FLOW_CTS
;
2584 dev_dbg(dev
, "%s - RTS/CTS is enabled\n", __func__
);
2586 dev_dbg(dev
, "%s - RTS/CTS is disabled\n", __func__
);
2589 /* if we are implementing XON/XOFF, set the start and stop character
2591 if (I_IXOFF(tty
) || I_IXON(tty
)) {
2592 unsigned char stop_char
= STOP_CHAR(tty
);
2593 unsigned char start_char
= START_CHAR(tty
);
2595 if ((!edge_serial
->is_epic
) ||
2596 ((edge_serial
->is_epic
) &&
2597 (edge_serial
->epic_descriptor
.Supports
.IOSPSetXChar
))) {
2598 send_iosp_ext_cmd(edge_port
,
2599 IOSP_CMD_SET_XON_CHAR
, start_char
);
2600 send_iosp_ext_cmd(edge_port
,
2601 IOSP_CMD_SET_XOFF_CHAR
, stop_char
);
2604 /* if we are implementing INBOUND XON/XOFF */
2606 rxFlow
|= IOSP_RX_FLOW_XON_XOFF
;
2607 dev_dbg(dev
, "%s - INBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x\n",
2608 __func__
, start_char
, stop_char
);
2610 dev_dbg(dev
, "%s - INBOUND XON/XOFF is disabled\n", __func__
);
2613 /* if we are implementing OUTBOUND XON/XOFF */
2615 txFlow
|= IOSP_TX_FLOW_XON_XOFF
;
2616 dev_dbg(dev
, "%s - OUTBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x\n",
2617 __func__
, start_char
, stop_char
);
2619 dev_dbg(dev
, "%s - OUTBOUND XON/XOFF is disabled\n", __func__
);
2623 /* Set flow control to the configured value */
2624 if ((!edge_serial
->is_epic
) ||
2625 ((edge_serial
->is_epic
) &&
2626 (edge_serial
->epic_descriptor
.Supports
.IOSPSetRxFlow
)))
2627 send_iosp_ext_cmd(edge_port
, IOSP_CMD_SET_RX_FLOW
, rxFlow
);
2628 if ((!edge_serial
->is_epic
) ||
2629 ((edge_serial
->is_epic
) &&
2630 (edge_serial
->epic_descriptor
.Supports
.IOSPSetTxFlow
)))
2631 send_iosp_ext_cmd(edge_port
, IOSP_CMD_SET_TX_FLOW
, txFlow
);
2634 edge_port
->shadowLCR
&= ~(LCR_BITS_MASK
| LCR_STOP_MASK
| LCR_PAR_MASK
);
2635 edge_port
->shadowLCR
|= (lData
| lParity
| lStop
);
2637 edge_port
->validDataMask
= mask
;
2639 /* Send the updated LCR value to the EdgePort */
2640 status
= send_cmd_write_uart_register(edge_port
, LCR
,
2641 edge_port
->shadowLCR
);
2645 /* set up the MCR register and send it to the EdgePort */
2646 edge_port
->shadowMCR
= MCR_MASTER_IE
;
2648 edge_port
->shadowMCR
|= (MCR_DTR
| MCR_RTS
);
2650 status
= send_cmd_write_uart_register(edge_port
, MCR
,
2651 edge_port
->shadowMCR
);
2655 /* Determine divisor based on baud rate */
2656 baud
= tty_get_baud_rate(tty
);
2658 /* pick a default, any default... */
2662 dev_dbg(dev
, "%s - baud rate = %d\n", __func__
, baud
);
2663 status
= send_cmd_write_baud_rate(edge_port
, baud
);
2665 /* Speed change was not possible - put back the old speed */
2666 baud
= tty_termios_baud_rate(old_termios
);
2667 tty_encode_baud_rate(tty
, baud
, baud
);
2672 /****************************************************************************
2674 * Turns a string from Unicode into ASCII.
2675 * Doesn't do a good job with any characters that are outside the normal
2676 * ASCII range, but it's only for debugging...
2677 * NOTE: expects the unicode in LE format
2678 ****************************************************************************/
2679 static void unicode_to_ascii(char *string
, int buflen
,
2680 __le16
*unicode
, int unicode_size
)
2684 if (buflen
<= 0) /* never happens, but... */
2686 --buflen
; /* space for nul */
2688 for (i
= 0; i
< unicode_size
; i
++) {
2691 string
[i
] = (char)(le16_to_cpu(unicode
[i
]));
2697 /****************************************************************************
2698 * get_manufacturing_desc
2699 * reads in the manufacturing descriptor and stores it into the serial
2701 ****************************************************************************/
2702 static void get_manufacturing_desc(struct edgeport_serial
*edge_serial
)
2704 struct device
*dev
= &edge_serial
->serial
->dev
->dev
;
2707 dev_dbg(dev
, "getting manufacturer descriptor\n");
2709 response
= rom_read(edge_serial
->serial
,
2710 (EDGE_MANUF_DESC_ADDR
& 0xffff0000) >> 16,
2711 (__u16
)(EDGE_MANUF_DESC_ADDR
& 0x0000ffff),
2712 EDGE_MANUF_DESC_LEN
,
2713 (__u8
*)(&edge_serial
->manuf_descriptor
));
2716 dev_err(dev
, "error in getting manufacturer descriptor\n");
2719 dev_dbg(dev
, "**Manufacturer Descriptor\n");
2720 dev_dbg(dev
, " RomSize: %dK\n",
2721 edge_serial
->manuf_descriptor
.RomSize
);
2722 dev_dbg(dev
, " RamSize: %dK\n",
2723 edge_serial
->manuf_descriptor
.RamSize
);
2724 dev_dbg(dev
, " CpuRev: %d\n",
2725 edge_serial
->manuf_descriptor
.CpuRev
);
2726 dev_dbg(dev
, " BoardRev: %d\n",
2727 edge_serial
->manuf_descriptor
.BoardRev
);
2728 dev_dbg(dev
, " NumPorts: %d\n",
2729 edge_serial
->manuf_descriptor
.NumPorts
);
2730 dev_dbg(dev
, " DescDate: %d/%d/%d\n",
2731 edge_serial
->manuf_descriptor
.DescDate
[0],
2732 edge_serial
->manuf_descriptor
.DescDate
[1],
2733 edge_serial
->manuf_descriptor
.DescDate
[2]+1900);
2734 unicode_to_ascii(string
, sizeof(string
),
2735 edge_serial
->manuf_descriptor
.SerialNumber
,
2736 edge_serial
->manuf_descriptor
.SerNumLength
/2);
2737 dev_dbg(dev
, " SerialNumber: %s\n", string
);
2738 unicode_to_ascii(string
, sizeof(string
),
2739 edge_serial
->manuf_descriptor
.AssemblyNumber
,
2740 edge_serial
->manuf_descriptor
.AssemblyNumLength
/2);
2741 dev_dbg(dev
, " AssemblyNumber: %s\n", string
);
2742 unicode_to_ascii(string
, sizeof(string
),
2743 edge_serial
->manuf_descriptor
.OemAssyNumber
,
2744 edge_serial
->manuf_descriptor
.OemAssyNumLength
/2);
2745 dev_dbg(dev
, " OemAssyNumber: %s\n", string
);
2746 dev_dbg(dev
, " UartType: %d\n",
2747 edge_serial
->manuf_descriptor
.UartType
);
2748 dev_dbg(dev
, " IonPid: %d\n",
2749 edge_serial
->manuf_descriptor
.IonPid
);
2750 dev_dbg(dev
, " IonConfig: %d\n",
2751 edge_serial
->manuf_descriptor
.IonConfig
);
2756 /****************************************************************************
2758 * reads in the bootloader descriptor and stores it into the serial
2760 ****************************************************************************/
2761 static void get_boot_desc(struct edgeport_serial
*edge_serial
)
2763 struct device
*dev
= &edge_serial
->serial
->dev
->dev
;
2766 dev_dbg(dev
, "getting boot descriptor\n");
2768 response
= rom_read(edge_serial
->serial
,
2769 (EDGE_BOOT_DESC_ADDR
& 0xffff0000) >> 16,
2770 (__u16
)(EDGE_BOOT_DESC_ADDR
& 0x0000ffff),
2772 (__u8
*)(&edge_serial
->boot_descriptor
));
2775 dev_err(dev
, "error in getting boot descriptor\n");
2777 dev_dbg(dev
, "**Boot Descriptor:\n");
2778 dev_dbg(dev
, " BootCodeLength: %d\n",
2779 le16_to_cpu(edge_serial
->boot_descriptor
.BootCodeLength
));
2780 dev_dbg(dev
, " MajorVersion: %d\n",
2781 edge_serial
->boot_descriptor
.MajorVersion
);
2782 dev_dbg(dev
, " MinorVersion: %d\n",
2783 edge_serial
->boot_descriptor
.MinorVersion
);
2784 dev_dbg(dev
, " BuildNumber: %d\n",
2785 le16_to_cpu(edge_serial
->boot_descriptor
.BuildNumber
));
2786 dev_dbg(dev
, " Capabilities: 0x%x\n",
2787 le16_to_cpu(edge_serial
->boot_descriptor
.Capabilities
));
2788 dev_dbg(dev
, " UConfig0: %d\n",
2789 edge_serial
->boot_descriptor
.UConfig0
);
2790 dev_dbg(dev
, " UConfig1: %d\n",
2791 edge_serial
->boot_descriptor
.UConfig1
);
2796 /****************************************************************************
2797 * load_application_firmware
2798 * This is called to load the application firmware to the device
2799 ****************************************************************************/
2800 static void load_application_firmware(struct edgeport_serial
*edge_serial
)
2802 struct device
*dev
= &edge_serial
->serial
->dev
->dev
;
2803 const struct ihex_binrec
*rec
;
2804 const struct firmware
*fw
;
2805 const char *fw_name
;
2806 const char *fw_info
;
2811 switch (edge_serial
->product_info
.iDownloadFile
) {
2812 case EDGE_DOWNLOAD_FILE_I930
:
2813 fw_info
= "downloading firmware version (930)";
2814 fw_name
= "edgeport/down.fw";
2817 case EDGE_DOWNLOAD_FILE_80251
:
2818 fw_info
= "downloading firmware version (80251)";
2819 fw_name
= "edgeport/down2.fw";
2822 case EDGE_DOWNLOAD_FILE_NONE
:
2823 dev_dbg(dev
, "No download file specified, skipping download\n");
2830 response
= request_ihex_firmware(&fw
, fw_name
,
2831 &edge_serial
->serial
->dev
->dev
);
2833 dev_err(dev
, "Failed to load image \"%s\" err %d\n",
2838 rec
= (const struct ihex_binrec
*)fw
->data
;
2839 build
= (rec
->data
[2] << 8) | rec
->data
[3];
2841 dev_dbg(dev
, "%s %d.%d.%d\n", fw_info
, rec
->data
[0], rec
->data
[1], build
);
2843 edge_serial
->product_info
.FirmwareMajorVersion
= rec
->data
[0];
2844 edge_serial
->product_info
.FirmwareMinorVersion
= rec
->data
[1];
2845 edge_serial
->product_info
.FirmwareBuildNumber
= cpu_to_le16(build
);
2847 for (rec
= ihex_next_binrec(rec
); rec
;
2848 rec
= ihex_next_binrec(rec
)) {
2849 Operaddr
= be32_to_cpu(rec
->addr
);
2850 response
= sram_write(edge_serial
->serial
,
2853 be16_to_cpu(rec
->len
),
2856 dev_err(&edge_serial
->serial
->dev
->dev
,
2857 "sram_write failed (%x, %x, %d)\n",
2858 Operaddr
>> 16, Operaddr
& 0xFFFF,
2859 be16_to_cpu(rec
->len
));
2864 dev_dbg(dev
, "sending exec_dl_code\n");
2865 response
= usb_control_msg (edge_serial
->serial
->dev
,
2866 usb_sndctrlpipe(edge_serial
->serial
->dev
, 0),
2867 USB_REQUEST_ION_EXEC_DL_CODE
,
2868 0x40, 0x4000, 0x0001, NULL
, 0, 3000);
2870 release_firmware(fw
);
2874 /****************************************************************************
2876 ****************************************************************************/
2877 static int edge_startup(struct usb_serial
*serial
)
2879 struct edgeport_serial
*edge_serial
;
2880 struct usb_device
*dev
;
2881 struct device
*ddev
= &serial
->dev
->dev
;
2884 bool interrupt_in_found
;
2886 bool bulk_out_found
;
2887 static __u32 descriptor
[3] = { EDGE_COMPATIBILITY_MASK0
,
2888 EDGE_COMPATIBILITY_MASK1
,
2889 EDGE_COMPATIBILITY_MASK2
};
2893 /* create our private serial structure */
2894 edge_serial
= kzalloc(sizeof(struct edgeport_serial
), GFP_KERNEL
);
2895 if (edge_serial
== NULL
) {
2896 dev_err(&serial
->dev
->dev
, "%s - Out of memory\n", __func__
);
2899 spin_lock_init(&edge_serial
->es_lock
);
2900 edge_serial
->serial
= serial
;
2901 usb_set_serial_data(serial
, edge_serial
);
2903 /* get the name for the device from the device */
2904 i
= usb_string(dev
, dev
->descriptor
.iManufacturer
,
2905 &edge_serial
->name
[0], MAX_NAME_LEN
+1);
2908 edge_serial
->name
[i
++] = ' ';
2909 usb_string(dev
, dev
->descriptor
.iProduct
,
2910 &edge_serial
->name
[i
], MAX_NAME_LEN
+2 - i
);
2912 dev_info(&serial
->dev
->dev
, "%s detected\n", edge_serial
->name
);
2914 /* Read the epic descriptor */
2915 if (get_epic_descriptor(edge_serial
) <= 0) {
2916 /* memcpy descriptor to Supports structures */
2917 memcpy(&edge_serial
->epic_descriptor
.Supports
, descriptor
,
2918 sizeof(struct edge_compatibility_bits
));
2920 /* get the manufacturing descriptor for this device */
2921 get_manufacturing_desc(edge_serial
);
2923 /* get the boot descriptor */
2924 get_boot_desc(edge_serial
);
2926 get_product_info(edge_serial
);
2929 /* set the number of ports from the manufacturing description */
2930 /* serial->num_ports = serial->product_info.NumPorts; */
2931 if ((!edge_serial
->is_epic
) &&
2932 (edge_serial
->product_info
.NumPorts
!= serial
->num_ports
)) {
2934 "Device Reported %d serial ports vs. core thinking we have %d ports, email greg@kroah.com this information.\n",
2935 edge_serial
->product_info
.NumPorts
,
2939 dev_dbg(ddev
, "%s - time 1 %ld\n", __func__
, jiffies
);
2941 /* If not an EPiC device */
2942 if (!edge_serial
->is_epic
) {
2943 /* now load the application firmware into this device */
2944 load_application_firmware(edge_serial
);
2946 dev_dbg(ddev
, "%s - time 2 %ld\n", __func__
, jiffies
);
2948 /* Check current Edgeport EEPROM and update if necessary */
2949 update_edgeport_E2PROM(edge_serial
);
2951 dev_dbg(ddev
, "%s - time 3 %ld\n", __func__
, jiffies
);
2953 /* set the configuration to use #1 */
2954 /* dev_dbg(ddev, "set_configuration 1\n"); */
2955 /* usb_set_configuration (dev, 1); */
2957 dev_dbg(ddev
, " FirmwareMajorVersion %d.%d.%d\n",
2958 edge_serial
->product_info
.FirmwareMajorVersion
,
2959 edge_serial
->product_info
.FirmwareMinorVersion
,
2960 le16_to_cpu(edge_serial
->product_info
.FirmwareBuildNumber
));
2962 /* we set up the pointers to the endpoints in the edge_open function,
2963 * as the structures aren't created yet. */
2967 if (edge_serial
->is_epic
) {
2968 /* EPIC thing, set up our interrupt polling now and our read
2969 * urb, so that the device knows it really is connected. */
2970 interrupt_in_found
= bulk_in_found
= bulk_out_found
= false;
2971 for (i
= 0; i
< serial
->interface
->altsetting
[0]
2972 .desc
.bNumEndpoints
; ++i
) {
2973 struct usb_endpoint_descriptor
*endpoint
;
2976 endpoint
= &serial
->interface
->altsetting
[0].
2978 buffer_size
= usb_endpoint_maxp(endpoint
);
2979 if (!interrupt_in_found
&&
2980 (usb_endpoint_is_int_in(endpoint
))) {
2981 /* we found a interrupt in endpoint */
2982 dev_dbg(ddev
, "found interrupt in\n");
2984 /* not set up yet, so do it now */
2985 edge_serial
->interrupt_read_urb
=
2986 usb_alloc_urb(0, GFP_KERNEL
);
2987 if (!edge_serial
->interrupt_read_urb
) {
2988 dev_err(ddev
, "out of memory\n");
2991 edge_serial
->interrupt_in_buffer
=
2992 kmalloc(buffer_size
, GFP_KERNEL
);
2993 if (!edge_serial
->interrupt_in_buffer
) {
2994 dev_err(ddev
, "out of memory\n");
2995 usb_free_urb(edge_serial
->interrupt_read_urb
);
2998 edge_serial
->interrupt_in_endpoint
=
2999 endpoint
->bEndpointAddress
;
3001 /* set up our interrupt urb */
3003 edge_serial
->interrupt_read_urb
,
3006 endpoint
->bEndpointAddress
),
3007 edge_serial
->interrupt_in_buffer
,
3009 edge_interrupt_callback
,
3011 endpoint
->bInterval
);
3013 interrupt_in_found
= true;
3016 if (!bulk_in_found
&&
3017 (usb_endpoint_is_bulk_in(endpoint
))) {
3018 /* we found a bulk in endpoint */
3019 dev_dbg(ddev
, "found bulk in\n");
3021 /* not set up yet, so do it now */
3022 edge_serial
->read_urb
=
3023 usb_alloc_urb(0, GFP_KERNEL
);
3024 if (!edge_serial
->read_urb
) {
3025 dev_err(ddev
, "out of memory\n");
3028 edge_serial
->bulk_in_buffer
=
3029 kmalloc(buffer_size
, GFP_KERNEL
);
3030 if (!edge_serial
->bulk_in_buffer
) {
3031 dev_err(&dev
->dev
, "out of memory\n");
3032 usb_free_urb(edge_serial
->read_urb
);
3035 edge_serial
->bulk_in_endpoint
=
3036 endpoint
->bEndpointAddress
;
3038 /* set up our bulk in urb */
3039 usb_fill_bulk_urb(edge_serial
->read_urb
, dev
,
3040 usb_rcvbulkpipe(dev
,
3041 endpoint
->bEndpointAddress
),
3042 edge_serial
->bulk_in_buffer
,
3043 usb_endpoint_maxp(endpoint
),
3044 edge_bulk_in_callback
,
3046 bulk_in_found
= true;
3049 if (!bulk_out_found
&&
3050 (usb_endpoint_is_bulk_out(endpoint
))) {
3051 /* we found a bulk out endpoint */
3052 dev_dbg(ddev
, "found bulk out\n");
3053 edge_serial
->bulk_out_endpoint
=
3054 endpoint
->bEndpointAddress
;
3055 bulk_out_found
= true;
3059 if (!interrupt_in_found
|| !bulk_in_found
|| !bulk_out_found
) {
3060 dev_err(ddev
, "Error - the proper endpoints were not found!\n");
3064 /* start interrupt read for this edgeport this interrupt will
3065 * continue as long as the edgeport is connected */
3066 response
= usb_submit_urb(edge_serial
->interrupt_read_urb
,
3069 dev_err(ddev
, "%s - Error %d submitting control urb\n",
3070 __func__
, response
);
3076 /****************************************************************************
3078 * This function is called whenever the device is removed from the usb bus.
3079 ****************************************************************************/
3080 static void edge_disconnect(struct usb_serial
*serial
)
3082 struct edgeport_serial
*edge_serial
= usb_get_serial_data(serial
);
3084 /* stop reads and writes on all ports */
3085 /* free up our endpoint stuff */
3086 if (edge_serial
->is_epic
) {
3087 usb_kill_urb(edge_serial
->interrupt_read_urb
);
3088 usb_free_urb(edge_serial
->interrupt_read_urb
);
3089 kfree(edge_serial
->interrupt_in_buffer
);
3091 usb_kill_urb(edge_serial
->read_urb
);
3092 usb_free_urb(edge_serial
->read_urb
);
3093 kfree(edge_serial
->bulk_in_buffer
);
3098 /****************************************************************************
3100 * This function is called when the device structure is deallocated.
3101 ****************************************************************************/
3102 static void edge_release(struct usb_serial
*serial
)
3104 struct edgeport_serial
*edge_serial
= usb_get_serial_data(serial
);
3109 static int edge_port_probe(struct usb_serial_port
*port
)
3111 struct edgeport_port
*edge_port
;
3113 edge_port
= kzalloc(sizeof(*edge_port
), GFP_KERNEL
);
3117 spin_lock_init(&edge_port
->ep_lock
);
3118 edge_port
->port
= port
;
3120 usb_set_serial_port_data(port
, edge_port
);
3125 static int edge_port_remove(struct usb_serial_port
*port
)
3127 struct edgeport_port
*edge_port
;
3129 edge_port
= usb_get_serial_port_data(port
);
3135 module_usb_serial_driver(serial_drivers
, id_table_combined
);
3137 MODULE_AUTHOR(DRIVER_AUTHOR
);
3138 MODULE_DESCRIPTION(DRIVER_DESC
);
3139 MODULE_LICENSE("GPL");
3140 MODULE_FIRMWARE("edgeport/boot.fw");
3141 MODULE_FIRMWARE("edgeport/boot2.fw");
3142 MODULE_FIRMWARE("edgeport/down.fw");
3143 MODULE_FIRMWARE("edgeport/down2.fw");