2 * Edgeport USB Serial Converter driver
4 * Copyright (C) 2000 Inside Out Networks, All rights reserved.
5 * Copyright (C) 2001-2002 Greg Kroah-Hartman <greg@kroah.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * Supports the following devices:
26 * For questions or problems with this driver, contact Inside Out
27 * Networks technical support, or Peter Berger <pberger@brimson.com>,
28 * or Al Borchers <alborchers@steinerpoint.com>.
32 #include <linux/kernel.h>
33 #include <linux/jiffies.h>
34 #include <linux/errno.h>
35 #include <linux/init.h>
36 #include <linux/slab.h>
37 #include <linux/tty.h>
38 #include <linux/tty_driver.h>
39 #include <linux/tty_flip.h>
40 #include <linux/module.h>
41 #include <linux/spinlock.h>
42 #include <linux/serial.h>
43 #include <linux/ioctl.h>
44 #include <linux/wait.h>
45 #include <linux/firmware.h>
46 #include <linux/ihex.h>
47 #include <linux/uaccess.h>
48 #include <linux/usb.h>
49 #include <linux/usb/serial.h>
50 #include "io_edgeport.h"
51 #include "io_ionsp.h" /* info for the iosp messages */
52 #include "io_16654.h" /* 16654 UART defines */
54 #define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com> and David Iacovelli"
55 #define DRIVER_DESC "Edgeport USB Serial Driver"
57 #define MAX_NAME_LEN 64
59 #define CHASE_TIMEOUT (5*HZ) /* 5 seconds */
60 #define OPEN_TIMEOUT (5*HZ) /* 5 seconds */
61 #define COMMAND_TIMEOUT (5*HZ) /* 5 seconds */
63 /* receive port state */
65 EXPECT_HDR1
= 0, /* Expect header byte 1 */
66 EXPECT_HDR2
= 1, /* Expect header byte 2 */
67 EXPECT_DATA
= 2, /* Expect 'RxBytesRemaining' data */
68 EXPECT_HDR3
= 3, /* Expect header byte 3 (for status hdrs only) */
73 * This Transmit queue is an extension of the edgeport Rx buffer.
74 * The maximum amount of data buffered in both the edgeport
75 * Rx buffer (maxTxCredits) and this buffer will never exceed maxTxCredits.
78 unsigned int head
; /* index to head pointer (write) */
79 unsigned int tail
; /* index to tail pointer (read) */
80 unsigned int count
; /* Bytes in queue */
81 unsigned int size
; /* Max size of queue (equal to Max number of TxCredits) */
82 unsigned char *fifo
; /* allocated Buffer */
85 /* This structure holds all of the local port information */
86 struct edgeport_port
{
87 __u16 txCredits
; /* our current credits for this port */
88 __u16 maxTxCredits
; /* the max size of the port */
90 struct TxFifo txfifo
; /* transmit fifo -- size will be maxTxCredits */
91 struct urb
*write_urb
; /* write URB for this port */
92 bool write_in_progress
; /* 'true' while a write URB is outstanding */
95 __u8 shadowLCR
; /* last LCR value received */
96 __u8 shadowMCR
; /* last MCR value received */
97 __u8 shadowMSR
; /* last MSR value received */
98 __u8 shadowLSR
; /* last LSR value received */
99 __u8 shadowXonChar
; /* last value set as XON char in Edgeport */
100 __u8 shadowXoffChar
; /* last value set as XOFF char in Edgeport */
108 bool chaseResponsePending
;
110 wait_queue_head_t wait_chase
; /* for handling sleeping while waiting for chase to finish */
111 wait_queue_head_t wait_open
; /* for handling sleeping while waiting for open to finish */
112 wait_queue_head_t wait_command
; /* for handling sleeping while waiting for command to finish */
114 struct async_icount icount
;
115 struct usb_serial_port
*port
; /* loop back to the owner of this object */
119 /* This structure holds all of the individual device information */
120 struct edgeport_serial
{
121 char name
[MAX_NAME_LEN
+2]; /* string name of this device */
123 struct edge_manuf_descriptor manuf_descriptor
; /* the manufacturer descriptor */
124 struct edge_boot_descriptor boot_descriptor
; /* the boot firmware descriptor */
125 struct edgeport_product_info product_info
; /* Product Info */
126 struct edge_compatibility_descriptor epic_descriptor
; /* Edgeport compatible descriptor */
127 int is_epic
; /* flag if EPiC device or not */
129 __u8 interrupt_in_endpoint
; /* the interrupt endpoint handle */
130 unsigned char *interrupt_in_buffer
; /* the buffer we use for the interrupt endpoint */
131 struct urb
*interrupt_read_urb
; /* our interrupt urb */
133 __u8 bulk_in_endpoint
; /* the bulk in endpoint handle */
134 unsigned char *bulk_in_buffer
; /* the buffer we use for the bulk in endpoint */
135 struct urb
*read_urb
; /* our bulk read urb */
136 bool read_in_progress
;
139 __u8 bulk_out_endpoint
; /* the bulk out endpoint handle */
141 __s16 rxBytesAvail
; /* the number of bytes that we need to read from this device */
143 enum RXSTATE rxState
; /* the current state of the bulk receive processor */
144 __u8 rxHeader1
; /* receive header byte 1 */
145 __u8 rxHeader2
; /* receive header byte 2 */
146 __u8 rxHeader3
; /* receive header byte 3 */
147 __u8 rxPort
; /* the port that we are currently receiving data for */
148 __u8 rxStatusCode
; /* the receive status code */
149 __u8 rxStatusParam
; /* the receive status paramater */
150 __s16 rxBytesRemaining
; /* the number of port bytes left to read */
151 struct usb_serial
*serial
; /* loop back to the owner of this object */
154 /* baud rate information */
155 struct divisor_table_entry
{
161 * Define table of divisors for Rev A EdgePort/4 hardware
162 * These assume a 3.6864MHz crystal, the standard /16, and
166 static const struct divisor_table_entry divisor_table
[] = {
169 { 110, 2095}, /* 2094.545455 => 230450 => .0217 % over */
170 { 134, 1713}, /* 1713.011152 => 230398.5 => .00065% under */
188 /* Number of outstanding Command Write Urbs */
189 static atomic_t CmdUrbs
= ATOMIC_INIT(0);
192 /* local function prototypes */
194 /* function prototypes for all URB callbacks */
195 static void edge_interrupt_callback(struct urb
*urb
);
196 static void edge_bulk_in_callback(struct urb
*urb
);
197 static void edge_bulk_out_data_callback(struct urb
*urb
);
198 static void edge_bulk_out_cmd_callback(struct urb
*urb
);
200 /* function prototypes for the usbserial callbacks */
201 static int edge_open(struct tty_struct
*tty
, struct usb_serial_port
*port
);
202 static void edge_close(struct usb_serial_port
*port
);
203 static int edge_write(struct tty_struct
*tty
, struct usb_serial_port
*port
,
204 const unsigned char *buf
, int count
);
205 static int edge_write_room(struct tty_struct
*tty
);
206 static int edge_chars_in_buffer(struct tty_struct
*tty
);
207 static void edge_throttle(struct tty_struct
*tty
);
208 static void edge_unthrottle(struct tty_struct
*tty
);
209 static void edge_set_termios(struct tty_struct
*tty
,
210 struct usb_serial_port
*port
,
211 struct ktermios
*old_termios
);
212 static int edge_ioctl(struct tty_struct
*tty
,
213 unsigned int cmd
, unsigned long arg
);
214 static void edge_break(struct tty_struct
*tty
, int break_state
);
215 static int edge_tiocmget(struct tty_struct
*tty
);
216 static int edge_tiocmset(struct tty_struct
*tty
,
217 unsigned int set
, unsigned int clear
);
218 static int edge_get_icount(struct tty_struct
*tty
,
219 struct serial_icounter_struct
*icount
);
220 static int edge_startup(struct usb_serial
*serial
);
221 static void edge_disconnect(struct usb_serial
*serial
);
222 static void edge_release(struct usb_serial
*serial
);
223 static int edge_port_probe(struct usb_serial_port
*port
);
224 static int edge_port_remove(struct usb_serial_port
*port
);
226 #include "io_tables.h" /* all of the devices that this driver supports */
228 /* function prototypes for all of our local functions */
230 static void process_rcvd_data(struct edgeport_serial
*edge_serial
,
231 unsigned char *buffer
, __u16 bufferLength
);
232 static void process_rcvd_status(struct edgeport_serial
*edge_serial
,
233 __u8 byte2
, __u8 byte3
);
234 static void edge_tty_recv(struct usb_serial_port
*port
, unsigned char *data
,
236 static void handle_new_msr(struct edgeport_port
*edge_port
, __u8 newMsr
);
237 static void handle_new_lsr(struct edgeport_port
*edge_port
, __u8 lsrData
,
238 __u8 lsr
, __u8 data
);
239 static int send_iosp_ext_cmd(struct edgeport_port
*edge_port
, __u8 command
,
241 static int calc_baud_rate_divisor(struct device
*dev
, int baud_rate
, int *divisor
);
242 static int send_cmd_write_baud_rate(struct edgeport_port
*edge_port
,
244 static void change_port_settings(struct tty_struct
*tty
,
245 struct edgeport_port
*edge_port
,
246 struct ktermios
*old_termios
);
247 static int send_cmd_write_uart_register(struct edgeport_port
*edge_port
,
248 __u8 regNum
, __u8 regValue
);
249 static int write_cmd_usb(struct edgeport_port
*edge_port
,
250 unsigned char *buffer
, int writeLength
);
251 static void send_more_port_data(struct edgeport_serial
*edge_serial
,
252 struct edgeport_port
*edge_port
);
254 static int sram_write(struct usb_serial
*serial
, __u16 extAddr
, __u16 addr
,
255 __u16 length
, const __u8
*data
);
256 static int rom_read(struct usb_serial
*serial
, __u16 extAddr
, __u16 addr
,
257 __u16 length
, __u8
*data
);
258 static int rom_write(struct usb_serial
*serial
, __u16 extAddr
, __u16 addr
,
259 __u16 length
, const __u8
*data
);
260 static void get_manufacturing_desc(struct edgeport_serial
*edge_serial
);
261 static void get_boot_desc(struct edgeport_serial
*edge_serial
);
262 static void load_application_firmware(struct edgeport_serial
*edge_serial
);
264 static void unicode_to_ascii(char *string
, int buflen
,
265 __le16
*unicode
, int unicode_size
);
268 /* ************************************************************************ */
269 /* ************************************************************************ */
270 /* ************************************************************************ */
271 /* ************************************************************************ */
273 /************************************************************************
275 * update_edgeport_E2PROM() Compare current versions of *
276 * Boot ROM and Manufacture *
277 * Descriptors with versions *
278 * embedded in this driver *
280 ************************************************************************/
281 static void update_edgeport_E2PROM(struct edgeport_serial
*edge_serial
)
283 struct device
*dev
= &edge_serial
->serial
->dev
->dev
;
286 __u8 BootMajorVersion
;
287 __u8 BootMinorVersion
;
288 __u16 BootBuildNumber
;
290 const struct ihex_binrec
*rec
;
291 const struct firmware
*fw
;
295 switch (edge_serial
->product_info
.iDownloadFile
) {
296 case EDGE_DOWNLOAD_FILE_I930
:
297 fw_name
= "edgeport/boot.fw";
299 case EDGE_DOWNLOAD_FILE_80251
:
300 fw_name
= "edgeport/boot2.fw";
306 response
= request_ihex_firmware(&fw
, fw_name
,
307 &edge_serial
->serial
->dev
->dev
);
309 dev_err(dev
, "Failed to load image \"%s\" err %d\n",
314 rec
= (const struct ihex_binrec
*)fw
->data
;
315 BootMajorVersion
= rec
->data
[0];
316 BootMinorVersion
= rec
->data
[1];
317 BootBuildNumber
= (rec
->data
[2] << 8) | rec
->data
[3];
319 /* Check Boot Image Version */
320 BootCurVer
= (edge_serial
->boot_descriptor
.MajorVersion
<< 24) +
321 (edge_serial
->boot_descriptor
.MinorVersion
<< 16) +
322 le16_to_cpu(edge_serial
->boot_descriptor
.BuildNumber
);
324 BootNewVer
= (BootMajorVersion
<< 24) +
325 (BootMinorVersion
<< 16) +
328 dev_dbg(dev
, "Current Boot Image version %d.%d.%d\n",
329 edge_serial
->boot_descriptor
.MajorVersion
,
330 edge_serial
->boot_descriptor
.MinorVersion
,
331 le16_to_cpu(edge_serial
->boot_descriptor
.BuildNumber
));
334 if (BootNewVer
> BootCurVer
) {
335 dev_dbg(dev
, "**Update Boot Image from %d.%d.%d to %d.%d.%d\n",
336 edge_serial
->boot_descriptor
.MajorVersion
,
337 edge_serial
->boot_descriptor
.MinorVersion
,
338 le16_to_cpu(edge_serial
->boot_descriptor
.BuildNumber
),
339 BootMajorVersion
, BootMinorVersion
, BootBuildNumber
);
341 dev_dbg(dev
, "Downloading new Boot Image\n");
343 for (rec
= ihex_next_binrec(rec
); rec
;
344 rec
= ihex_next_binrec(rec
)) {
345 Bootaddr
= be32_to_cpu(rec
->addr
);
346 response
= rom_write(edge_serial
->serial
,
349 be16_to_cpu(rec
->len
),
352 dev_err(&edge_serial
->serial
->dev
->dev
,
353 "rom_write failed (%x, %x, %d)\n",
354 Bootaddr
>> 16, Bootaddr
& 0xFFFF,
355 be16_to_cpu(rec
->len
));
360 dev_dbg(dev
, "Boot Image -- already up to date\n");
362 release_firmware(fw
);
366 /************************************************************************
368 * Get string descriptor from device
370 ************************************************************************/
371 static int get_string_desc(struct usb_device
*dev
, int Id
,
372 struct usb_string_descriptor
**pRetDesc
)
374 struct usb_string_descriptor StringDesc
;
375 struct usb_string_descriptor
*pStringDesc
;
377 dev_dbg(&dev
->dev
, "%s - USB String ID = %d\n", __func__
, Id
);
379 if (!usb_get_descriptor(dev
, USB_DT_STRING
, Id
, &StringDesc
,
383 pStringDesc
= kmalloc(StringDesc
.bLength
, GFP_KERNEL
);
387 if (!usb_get_descriptor(dev
, USB_DT_STRING
, Id
, pStringDesc
,
388 StringDesc
.bLength
)) {
393 *pRetDesc
= pStringDesc
;
398 static void dump_product_info(struct edgeport_serial
*edge_serial
,
399 struct edgeport_product_info
*product_info
)
401 struct device
*dev
= &edge_serial
->serial
->dev
->dev
;
403 /* Dump Product Info structure */
404 dev_dbg(dev
, "**Product Information:\n");
405 dev_dbg(dev
, " ProductId %x\n", product_info
->ProductId
);
406 dev_dbg(dev
, " NumPorts %d\n", product_info
->NumPorts
);
407 dev_dbg(dev
, " ProdInfoVer %d\n", product_info
->ProdInfoVer
);
408 dev_dbg(dev
, " IsServer %d\n", product_info
->IsServer
);
409 dev_dbg(dev
, " IsRS232 %d\n", product_info
->IsRS232
);
410 dev_dbg(dev
, " IsRS422 %d\n", product_info
->IsRS422
);
411 dev_dbg(dev
, " IsRS485 %d\n", product_info
->IsRS485
);
412 dev_dbg(dev
, " RomSize %d\n", product_info
->RomSize
);
413 dev_dbg(dev
, " RamSize %d\n", product_info
->RamSize
);
414 dev_dbg(dev
, " CpuRev %x\n", product_info
->CpuRev
);
415 dev_dbg(dev
, " BoardRev %x\n", product_info
->BoardRev
);
416 dev_dbg(dev
, " BootMajorVersion %d.%d.%d\n",
417 product_info
->BootMajorVersion
,
418 product_info
->BootMinorVersion
,
419 le16_to_cpu(product_info
->BootBuildNumber
));
420 dev_dbg(dev
, " FirmwareMajorVersion %d.%d.%d\n",
421 product_info
->FirmwareMajorVersion
,
422 product_info
->FirmwareMinorVersion
,
423 le16_to_cpu(product_info
->FirmwareBuildNumber
));
424 dev_dbg(dev
, " ManufactureDescDate %d/%d/%d\n",
425 product_info
->ManufactureDescDate
[0],
426 product_info
->ManufactureDescDate
[1],
427 product_info
->ManufactureDescDate
[2]+1900);
428 dev_dbg(dev
, " iDownloadFile 0x%x\n",
429 product_info
->iDownloadFile
);
430 dev_dbg(dev
, " EpicVer %d\n", product_info
->EpicVer
);
433 static void get_product_info(struct edgeport_serial
*edge_serial
)
435 struct edgeport_product_info
*product_info
= &edge_serial
->product_info
;
437 memset(product_info
, 0, sizeof(struct edgeport_product_info
));
439 product_info
->ProductId
= (__u16
)(le16_to_cpu(edge_serial
->serial
->dev
->descriptor
.idProduct
) & ~ION_DEVICE_ID_80251_NETCHIP
);
440 product_info
->NumPorts
= edge_serial
->manuf_descriptor
.NumPorts
;
441 product_info
->ProdInfoVer
= 0;
443 product_info
->RomSize
= edge_serial
->manuf_descriptor
.RomSize
;
444 product_info
->RamSize
= edge_serial
->manuf_descriptor
.RamSize
;
445 product_info
->CpuRev
= edge_serial
->manuf_descriptor
.CpuRev
;
446 product_info
->BoardRev
= edge_serial
->manuf_descriptor
.BoardRev
;
448 product_info
->BootMajorVersion
=
449 edge_serial
->boot_descriptor
.MajorVersion
;
450 product_info
->BootMinorVersion
=
451 edge_serial
->boot_descriptor
.MinorVersion
;
452 product_info
->BootBuildNumber
=
453 edge_serial
->boot_descriptor
.BuildNumber
;
455 memcpy(product_info
->ManufactureDescDate
,
456 edge_serial
->manuf_descriptor
.DescDate
,
457 sizeof(edge_serial
->manuf_descriptor
.DescDate
));
459 /* check if this is 2nd generation hardware */
460 if (le16_to_cpu(edge_serial
->serial
->dev
->descriptor
.idProduct
)
461 & ION_DEVICE_ID_80251_NETCHIP
)
462 product_info
->iDownloadFile
= EDGE_DOWNLOAD_FILE_80251
;
464 product_info
->iDownloadFile
= EDGE_DOWNLOAD_FILE_I930
;
466 /* Determine Product type and set appropriate flags */
467 switch (DEVICE_ID_FROM_USB_PRODUCT_ID(product_info
->ProductId
)) {
468 case ION_DEVICE_ID_EDGEPORT_COMPATIBLE
:
469 case ION_DEVICE_ID_EDGEPORT_4T
:
470 case ION_DEVICE_ID_EDGEPORT_4
:
471 case ION_DEVICE_ID_EDGEPORT_2
:
472 case ION_DEVICE_ID_EDGEPORT_8_DUAL_CPU
:
473 case ION_DEVICE_ID_EDGEPORT_8
:
474 case ION_DEVICE_ID_EDGEPORT_421
:
475 case ION_DEVICE_ID_EDGEPORT_21
:
476 case ION_DEVICE_ID_EDGEPORT_2_DIN
:
477 case ION_DEVICE_ID_EDGEPORT_4_DIN
:
478 case ION_DEVICE_ID_EDGEPORT_16_DUAL_CPU
:
479 product_info
->IsRS232
= 1;
482 case ION_DEVICE_ID_EDGEPORT_2I
: /* Edgeport/2 RS422/RS485 */
483 product_info
->IsRS422
= 1;
484 product_info
->IsRS485
= 1;
487 case ION_DEVICE_ID_EDGEPORT_8I
: /* Edgeport/4 RS422 */
488 case ION_DEVICE_ID_EDGEPORT_4I
: /* Edgeport/4 RS422 */
489 product_info
->IsRS422
= 1;
493 dump_product_info(edge_serial
, product_info
);
496 static int get_epic_descriptor(struct edgeport_serial
*ep
)
499 struct usb_serial
*serial
= ep
->serial
;
500 struct edgeport_product_info
*product_info
= &ep
->product_info
;
501 struct edge_compatibility_descriptor
*epic
= &ep
->epic_descriptor
;
502 struct edge_compatibility_bits
*bits
;
503 struct device
*dev
= &serial
->dev
->dev
;
506 result
= usb_control_msg(serial
->dev
, usb_rcvctrlpipe(serial
->dev
, 0),
507 USB_REQUEST_ION_GET_EPIC_DESC
,
509 &ep
->epic_descriptor
,
510 sizeof(struct edge_compatibility_descriptor
),
515 memset(product_info
, 0, sizeof(struct edgeport_product_info
));
517 product_info
->NumPorts
= epic
->NumPorts
;
518 product_info
->ProdInfoVer
= 0;
519 product_info
->FirmwareMajorVersion
= epic
->MajorVersion
;
520 product_info
->FirmwareMinorVersion
= epic
->MinorVersion
;
521 product_info
->FirmwareBuildNumber
= epic
->BuildNumber
;
522 product_info
->iDownloadFile
= epic
->iDownloadFile
;
523 product_info
->EpicVer
= epic
->EpicVer
;
524 product_info
->Epic
= epic
->Supports
;
525 product_info
->ProductId
= ION_DEVICE_ID_EDGEPORT_COMPATIBLE
;
526 dump_product_info(ep
, product_info
);
528 bits
= &ep
->epic_descriptor
.Supports
;
529 dev_dbg(dev
, "**EPIC descriptor:\n");
530 dev_dbg(dev
, " VendEnableSuspend: %s\n", bits
->VendEnableSuspend
? "TRUE": "FALSE");
531 dev_dbg(dev
, " IOSPOpen : %s\n", bits
->IOSPOpen
? "TRUE": "FALSE");
532 dev_dbg(dev
, " IOSPClose : %s\n", bits
->IOSPClose
? "TRUE": "FALSE");
533 dev_dbg(dev
, " IOSPChase : %s\n", bits
->IOSPChase
? "TRUE": "FALSE");
534 dev_dbg(dev
, " IOSPSetRxFlow : %s\n", bits
->IOSPSetRxFlow
? "TRUE": "FALSE");
535 dev_dbg(dev
, " IOSPSetTxFlow : %s\n", bits
->IOSPSetTxFlow
? "TRUE": "FALSE");
536 dev_dbg(dev
, " IOSPSetXChar : %s\n", bits
->IOSPSetXChar
? "TRUE": "FALSE");
537 dev_dbg(dev
, " IOSPRxCheck : %s\n", bits
->IOSPRxCheck
? "TRUE": "FALSE");
538 dev_dbg(dev
, " IOSPSetClrBreak : %s\n", bits
->IOSPSetClrBreak
? "TRUE": "FALSE");
539 dev_dbg(dev
, " IOSPWriteMCR : %s\n", bits
->IOSPWriteMCR
? "TRUE": "FALSE");
540 dev_dbg(dev
, " IOSPWriteLCR : %s\n", bits
->IOSPWriteLCR
? "TRUE": "FALSE");
541 dev_dbg(dev
, " IOSPSetBaudRate : %s\n", bits
->IOSPSetBaudRate
? "TRUE": "FALSE");
542 dev_dbg(dev
, " TrueEdgeport : %s\n", bits
->TrueEdgeport
? "TRUE": "FALSE");
549 /************************************************************************/
550 /************************************************************************/
551 /* U S B C A L L B A C K F U N C T I O N S */
552 /* U S B C A L L B A C K F U N C T I O N S */
553 /************************************************************************/
554 /************************************************************************/
556 /*****************************************************************************
557 * edge_interrupt_callback
558 * this is the callback function for when we have received data on the
559 * interrupt endpoint.
560 *****************************************************************************/
561 static void edge_interrupt_callback(struct urb
*urb
)
563 struct edgeport_serial
*edge_serial
= urb
->context
;
565 struct edgeport_port
*edge_port
;
566 struct usb_serial_port
*port
;
567 struct tty_struct
*tty
;
568 unsigned char *data
= urb
->transfer_buffer
;
569 int length
= urb
->actual_length
;
575 int status
= urb
->status
;
584 /* this urb is terminated, clean up */
585 dev_dbg(&urb
->dev
->dev
, "%s - urb shutting down with status: %d\n", __func__
, status
);
588 dev_dbg(&urb
->dev
->dev
, "%s - nonzero urb status received: %d\n", __func__
, status
);
592 dev
= &edge_serial
->serial
->dev
->dev
;
594 /* process this interrupt-read even if there are no ports open */
596 usb_serial_debug_data(dev
, __func__
, length
, data
);
599 bytes_avail
= data
[0] | (data
[1] << 8);
601 spin_lock(&edge_serial
->es_lock
);
602 edge_serial
->rxBytesAvail
+= bytes_avail
;
604 "%s - bytes_avail=%d, rxBytesAvail=%d, read_in_progress=%d\n",
605 __func__
, bytes_avail
,
606 edge_serial
->rxBytesAvail
,
607 edge_serial
->read_in_progress
);
609 if (edge_serial
->rxBytesAvail
> 0 &&
610 !edge_serial
->read_in_progress
) {
611 dev_dbg(dev
, "%s - posting a read\n", __func__
);
612 edge_serial
->read_in_progress
= true;
614 /* we have pending bytes on the
615 bulk in pipe, send a request */
616 result
= usb_submit_urb(edge_serial
->read_urb
, GFP_ATOMIC
);
619 "%s - usb_submit_urb(read bulk) failed with result = %d\n",
621 edge_serial
->read_in_progress
= false;
624 spin_unlock(&edge_serial
->es_lock
);
627 /* grab the txcredits for the ports if available */
630 while ((position
< length
) &&
631 (portNumber
< edge_serial
->serial
->num_ports
)) {
632 txCredits
= data
[position
] | (data
[position
+1] << 8);
634 port
= edge_serial
->serial
->port
[portNumber
];
635 edge_port
= usb_get_serial_port_data(port
);
636 if (edge_port
->open
) {
637 spin_lock(&edge_port
->ep_lock
);
638 edge_port
->txCredits
+= txCredits
;
639 spin_unlock(&edge_port
->ep_lock
);
640 dev_dbg(dev
, "%s - txcredits for port%d = %d\n",
641 __func__
, portNumber
,
642 edge_port
->txCredits
);
644 /* tell the tty driver that something
646 tty
= tty_port_tty_get(
647 &edge_port
->port
->port
);
652 /* Since we have more credit, check
653 if more data can be sent */
654 send_more_port_data(edge_serial
,
664 result
= usb_submit_urb(urb
, GFP_ATOMIC
);
666 dev_err(&urb
->dev
->dev
,
667 "%s - Error %d submitting control urb\n",
672 /*****************************************************************************
673 * edge_bulk_in_callback
674 * this is the callback function for when we have received data on the
676 *****************************************************************************/
677 static void edge_bulk_in_callback(struct urb
*urb
)
679 struct edgeport_serial
*edge_serial
= urb
->context
;
681 unsigned char *data
= urb
->transfer_buffer
;
683 __u16 raw_data_length
;
684 int status
= urb
->status
;
687 dev_dbg(&urb
->dev
->dev
, "%s - nonzero read bulk status received: %d\n",
689 edge_serial
->read_in_progress
= false;
693 if (urb
->actual_length
== 0) {
694 dev_dbg(&urb
->dev
->dev
, "%s - read bulk callback with no data\n", __func__
);
695 edge_serial
->read_in_progress
= false;
699 dev
= &edge_serial
->serial
->dev
->dev
;
700 raw_data_length
= urb
->actual_length
;
702 usb_serial_debug_data(dev
, __func__
, raw_data_length
, data
);
704 spin_lock(&edge_serial
->es_lock
);
706 /* decrement our rxBytes available by the number that we just got */
707 edge_serial
->rxBytesAvail
-= raw_data_length
;
709 dev_dbg(dev
, "%s - Received = %d, rxBytesAvail %d\n", __func__
,
710 raw_data_length
, edge_serial
->rxBytesAvail
);
712 process_rcvd_data(edge_serial
, data
, urb
->actual_length
);
714 /* check to see if there's any more data for us to read */
715 if (edge_serial
->rxBytesAvail
> 0) {
716 dev_dbg(dev
, "%s - posting a read\n", __func__
);
717 retval
= usb_submit_urb(edge_serial
->read_urb
, GFP_ATOMIC
);
720 "%s - usb_submit_urb(read bulk) failed, retval = %d\n",
722 edge_serial
->read_in_progress
= false;
725 edge_serial
->read_in_progress
= false;
728 spin_unlock(&edge_serial
->es_lock
);
732 /*****************************************************************************
733 * edge_bulk_out_data_callback
734 * this is the callback function for when we have finished sending
735 * serial data on the bulk out endpoint.
736 *****************************************************************************/
737 static void edge_bulk_out_data_callback(struct urb
*urb
)
739 struct edgeport_port
*edge_port
= urb
->context
;
740 struct tty_struct
*tty
;
741 int status
= urb
->status
;
744 dev_dbg(&urb
->dev
->dev
,
745 "%s - nonzero write bulk status received: %d\n",
749 tty
= tty_port_tty_get(&edge_port
->port
->port
);
751 if (tty
&& edge_port
->open
) {
752 /* let the tty driver wakeup if it has a special
753 write_wakeup function */
758 /* Release the Write URB */
759 edge_port
->write_in_progress
= false;
761 /* Check if more data needs to be sent */
762 send_more_port_data((struct edgeport_serial
*)
763 (usb_get_serial_data(edge_port
->port
->serial
)), edge_port
);
767 /*****************************************************************************
769 * this is the callback function for when we have finished sending a
770 * command on the bulk out endpoint.
771 *****************************************************************************/
772 static void edge_bulk_out_cmd_callback(struct urb
*urb
)
774 struct edgeport_port
*edge_port
= urb
->context
;
775 struct tty_struct
*tty
;
776 int status
= urb
->status
;
778 atomic_dec(&CmdUrbs
);
779 dev_dbg(&urb
->dev
->dev
, "%s - FREE URB %p (outstanding %d)\n",
780 __func__
, urb
, atomic_read(&CmdUrbs
));
783 /* clean up the transfer buffer */
784 kfree(urb
->transfer_buffer
);
786 /* Free the command urb */
790 dev_dbg(&urb
->dev
->dev
,
791 "%s - nonzero write bulk status received: %d\n",
796 /* Get pointer to tty */
797 tty
= tty_port_tty_get(&edge_port
->port
->port
);
799 /* tell the tty driver that something has changed */
800 if (tty
&& edge_port
->open
)
804 /* we have completed the command */
805 edge_port
->commandPending
= false;
806 wake_up(&edge_port
->wait_command
);
810 /*****************************************************************************
811 * Driver tty interface functions
812 *****************************************************************************/
814 /*****************************************************************************
816 * this function is called by the tty driver when a port is opened
817 * If successful, we return 0
818 * Otherwise we return a negative error number.
819 *****************************************************************************/
820 static int edge_open(struct tty_struct
*tty
, struct usb_serial_port
*port
)
822 struct edgeport_port
*edge_port
= usb_get_serial_port_data(port
);
823 struct device
*dev
= &port
->dev
;
824 struct usb_serial
*serial
;
825 struct edgeport_serial
*edge_serial
;
828 if (edge_port
== NULL
)
831 /* see if we've set up our endpoint info yet (can't set it up
832 in edge_startup as the structures were not set up at that time.) */
833 serial
= port
->serial
;
834 edge_serial
= usb_get_serial_data(serial
);
835 if (edge_serial
== NULL
)
837 if (edge_serial
->interrupt_in_buffer
== NULL
) {
838 struct usb_serial_port
*port0
= serial
->port
[0];
840 /* not set up yet, so do it now */
841 edge_serial
->interrupt_in_buffer
=
842 port0
->interrupt_in_buffer
;
843 edge_serial
->interrupt_in_endpoint
=
844 port0
->interrupt_in_endpointAddress
;
845 edge_serial
->interrupt_read_urb
= port0
->interrupt_in_urb
;
846 edge_serial
->bulk_in_buffer
= port0
->bulk_in_buffer
;
847 edge_serial
->bulk_in_endpoint
=
848 port0
->bulk_in_endpointAddress
;
849 edge_serial
->read_urb
= port0
->read_urb
;
850 edge_serial
->bulk_out_endpoint
=
851 port0
->bulk_out_endpointAddress
;
853 /* set up our interrupt urb */
854 usb_fill_int_urb(edge_serial
->interrupt_read_urb
,
856 usb_rcvintpipe(serial
->dev
,
857 port0
->interrupt_in_endpointAddress
),
858 port0
->interrupt_in_buffer
,
859 edge_serial
->interrupt_read_urb
->transfer_buffer_length
,
860 edge_interrupt_callback
, edge_serial
,
861 edge_serial
->interrupt_read_urb
->interval
);
863 /* set up our bulk in urb */
864 usb_fill_bulk_urb(edge_serial
->read_urb
, serial
->dev
,
865 usb_rcvbulkpipe(serial
->dev
,
866 port0
->bulk_in_endpointAddress
),
867 port0
->bulk_in_buffer
,
868 edge_serial
->read_urb
->transfer_buffer_length
,
869 edge_bulk_in_callback
, edge_serial
);
870 edge_serial
->read_in_progress
= false;
872 /* start interrupt read for this edgeport
873 * this interrupt will continue as long
874 * as the edgeport is connected */
875 response
= usb_submit_urb(edge_serial
->interrupt_read_urb
,
878 dev_err(dev
, "%s - Error %d submitting control urb\n",
883 /* initialize our wait queues */
884 init_waitqueue_head(&edge_port
->wait_open
);
885 init_waitqueue_head(&edge_port
->wait_chase
);
886 init_waitqueue_head(&edge_port
->wait_command
);
888 /* initialize our icount structure */
889 memset(&(edge_port
->icount
), 0x00, sizeof(edge_port
->icount
));
891 /* initialize our port settings */
892 edge_port
->txCredits
= 0; /* Can't send any data yet */
893 /* Must always set this bit to enable ints! */
894 edge_port
->shadowMCR
= MCR_MASTER_IE
;
895 edge_port
->chaseResponsePending
= false;
897 /* send a open port command */
898 edge_port
->openPending
= true;
899 edge_port
->open
= false;
900 response
= send_iosp_ext_cmd(edge_port
, IOSP_CMD_OPEN_PORT
, 0);
903 dev_err(dev
, "%s - error sending open port command\n", __func__
);
904 edge_port
->openPending
= false;
908 /* now wait for the port to be completely opened */
909 wait_event_timeout(edge_port
->wait_open
, !edge_port
->openPending
,
912 if (!edge_port
->open
) {
914 dev_dbg(dev
, "%s - open timedout\n", __func__
);
915 edge_port
->openPending
= false;
919 /* create the txfifo */
920 edge_port
->txfifo
.head
= 0;
921 edge_port
->txfifo
.tail
= 0;
922 edge_port
->txfifo
.count
= 0;
923 edge_port
->txfifo
.size
= edge_port
->maxTxCredits
;
924 edge_port
->txfifo
.fifo
= kmalloc(edge_port
->maxTxCredits
, GFP_KERNEL
);
926 if (!edge_port
->txfifo
.fifo
) {
927 dev_dbg(dev
, "%s - no memory\n", __func__
);
932 /* Allocate a URB for the write */
933 edge_port
->write_urb
= usb_alloc_urb(0, GFP_KERNEL
);
934 edge_port
->write_in_progress
= false;
936 if (!edge_port
->write_urb
) {
937 dev_dbg(dev
, "%s - no memory\n", __func__
);
942 dev_dbg(dev
, "%s(%d) - Initialize TX fifo to %d bytes\n",
943 __func__
, port
->number
, edge_port
->maxTxCredits
);
949 /************************************************************************
951 * block_until_chase_response
953 * This function will block the close until one of the following:
954 * 1. Response to our Chase comes from Edgeport
955 * 2. A timeout of 10 seconds without activity has expired
956 * (1K of Edgeport data @ 2400 baud ==> 4 sec to empty)
958 ************************************************************************/
959 static void block_until_chase_response(struct edgeport_port
*edge_port
)
961 struct device
*dev
= &edge_port
->port
->dev
;
968 /* Save Last credits */
969 lastCredits
= edge_port
->txCredits
;
971 /* Did we get our Chase response */
972 if (!edge_port
->chaseResponsePending
) {
973 dev_dbg(dev
, "%s - Got Chase Response\n", __func__
);
975 /* did we get all of our credit back? */
976 if (edge_port
->txCredits
== edge_port
->maxTxCredits
) {
977 dev_dbg(dev
, "%s - Got all credits\n", __func__
);
982 /* Block the thread for a while */
983 prepare_to_wait(&edge_port
->wait_chase
, &wait
,
984 TASK_UNINTERRUPTIBLE
);
985 schedule_timeout(timeout
);
986 finish_wait(&edge_port
->wait_chase
, &wait
);
988 if (lastCredits
== edge_port
->txCredits
) {
989 /* No activity.. count down. */
992 edge_port
->chaseResponsePending
= false;
993 dev_dbg(dev
, "%s - Chase TIMEOUT\n", __func__
);
997 /* Reset timeout value back to 10 seconds */
998 dev_dbg(dev
, "%s - Last %d, Current %d\n", __func__
,
999 lastCredits
, edge_port
->txCredits
);
1006 /************************************************************************
1008 * block_until_tx_empty
1010 * This function will block the close until one of the following:
1012 * 2. The edgeport has stopped
1013 * 3. A timeout of 3 seconds without activity has expired
1015 ************************************************************************/
1016 static void block_until_tx_empty(struct edgeport_port
*edge_port
)
1018 struct device
*dev
= &edge_port
->port
->dev
;
1020 struct TxFifo
*fifo
= &edge_port
->txfifo
;
1022 int timeout
= HZ
/10;
1026 /* Save Last count */
1027 lastCount
= fifo
->count
;
1029 /* Is the Edgeport Buffer empty? */
1030 if (lastCount
== 0) {
1031 dev_dbg(dev
, "%s - TX Buffer Empty\n", __func__
);
1035 /* Block the thread for a while */
1036 prepare_to_wait(&edge_port
->wait_chase
, &wait
,
1037 TASK_UNINTERRUPTIBLE
);
1038 schedule_timeout(timeout
);
1039 finish_wait(&edge_port
->wait_chase
, &wait
);
1041 dev_dbg(dev
, "%s wait\n", __func__
);
1043 if (lastCount
== fifo
->count
) {
1044 /* No activity.. count down. */
1047 dev_dbg(dev
, "%s - TIMEOUT\n", __func__
);
1051 /* Reset timeout value back to seconds */
1058 /*****************************************************************************
1060 * this function is called by the tty driver when a port is closed
1061 *****************************************************************************/
1062 static void edge_close(struct usb_serial_port
*port
)
1064 struct edgeport_serial
*edge_serial
;
1065 struct edgeport_port
*edge_port
;
1068 edge_serial
= usb_get_serial_data(port
->serial
);
1069 edge_port
= usb_get_serial_port_data(port
);
1070 if (edge_serial
== NULL
|| edge_port
== NULL
)
1073 /* block until tx is empty */
1074 block_until_tx_empty(edge_port
);
1076 edge_port
->closePending
= true;
1078 if ((!edge_serial
->is_epic
) ||
1079 ((edge_serial
->is_epic
) &&
1080 (edge_serial
->epic_descriptor
.Supports
.IOSPChase
))) {
1081 /* flush and chase */
1082 edge_port
->chaseResponsePending
= true;
1084 dev_dbg(&port
->dev
, "%s - Sending IOSP_CMD_CHASE_PORT\n", __func__
);
1085 status
= send_iosp_ext_cmd(edge_port
, IOSP_CMD_CHASE_PORT
, 0);
1087 /* block until chase finished */
1088 block_until_chase_response(edge_port
);
1090 edge_port
->chaseResponsePending
= false;
1093 if ((!edge_serial
->is_epic
) ||
1094 ((edge_serial
->is_epic
) &&
1095 (edge_serial
->epic_descriptor
.Supports
.IOSPClose
))) {
1096 /* close the port */
1097 dev_dbg(&port
->dev
, "%s - Sending IOSP_CMD_CLOSE_PORT\n", __func__
);
1098 send_iosp_ext_cmd(edge_port
, IOSP_CMD_CLOSE_PORT
, 0);
1101 /* port->close = true; */
1102 edge_port
->closePending
= false;
1103 edge_port
->open
= false;
1104 edge_port
->openPending
= false;
1106 usb_kill_urb(edge_port
->write_urb
);
1108 if (edge_port
->write_urb
) {
1109 /* if this urb had a transfer buffer already
1110 (old transfer) free it */
1111 kfree(edge_port
->write_urb
->transfer_buffer
);
1112 usb_free_urb(edge_port
->write_urb
);
1113 edge_port
->write_urb
= NULL
;
1115 kfree(edge_port
->txfifo
.fifo
);
1116 edge_port
->txfifo
.fifo
= NULL
;
1119 /*****************************************************************************
1121 * this function is called by the tty driver when data should be written
1123 * If successful, we return the number of bytes written, otherwise we
1124 * return a negative error number.
1125 *****************************************************************************/
1126 static int edge_write(struct tty_struct
*tty
, struct usb_serial_port
*port
,
1127 const unsigned char *data
, int count
)
1129 struct edgeport_port
*edge_port
= usb_get_serial_port_data(port
);
1130 struct TxFifo
*fifo
;
1135 unsigned long flags
;
1137 if (edge_port
== NULL
)
1140 /* get a pointer to the Tx fifo */
1141 fifo
= &edge_port
->txfifo
;
1143 spin_lock_irqsave(&edge_port
->ep_lock
, flags
);
1145 /* calculate number of bytes to put in fifo */
1146 copySize
= min((unsigned int)count
,
1147 (edge_port
->txCredits
- fifo
->count
));
1149 dev_dbg(&port
->dev
, "%s(%d) of %d byte(s) Fifo room %d -- will copy %d bytes\n",
1150 __func__
, port
->number
, count
,
1151 edge_port
->txCredits
- fifo
->count
, copySize
);
1153 /* catch writes of 0 bytes which the tty driver likes to give us,
1154 and when txCredits is empty */
1155 if (copySize
== 0) {
1156 dev_dbg(&port
->dev
, "%s - copySize = Zero\n", __func__
);
1161 * since we can never overflow the buffer we do not have to check for a
1164 * the copy is done is two parts -- first fill to the end of the buffer
1165 * then copy the reset from the start of the buffer
1167 bytesleft
= fifo
->size
- fifo
->head
;
1168 firsthalf
= min(bytesleft
, copySize
);
1169 dev_dbg(&port
->dev
, "%s - copy %d bytes of %d into fifo \n", __func__
,
1170 firsthalf
, bytesleft
);
1172 /* now copy our data */
1173 memcpy(&fifo
->fifo
[fifo
->head
], data
, firsthalf
);
1174 usb_serial_debug_data(&port
->dev
, __func__
, firsthalf
, &fifo
->fifo
[fifo
->head
]);
1176 /* update the index and size */
1177 fifo
->head
+= firsthalf
;
1178 fifo
->count
+= firsthalf
;
1180 /* wrap the index */
1181 if (fifo
->head
== fifo
->size
)
1184 secondhalf
= copySize
-firsthalf
;
1187 dev_dbg(&port
->dev
, "%s - copy rest of data %d\n", __func__
, secondhalf
);
1188 memcpy(&fifo
->fifo
[fifo
->head
], &data
[firsthalf
], secondhalf
);
1189 usb_serial_debug_data(&port
->dev
, __func__
, secondhalf
, &fifo
->fifo
[fifo
->head
]);
1190 /* update the index and size */
1191 fifo
->count
+= secondhalf
;
1192 fifo
->head
+= secondhalf
;
1193 /* No need to check for wrap since we can not get to end of
1194 * the fifo in this part
1199 spin_unlock_irqrestore(&edge_port
->ep_lock
, flags
);
1201 send_more_port_data((struct edgeport_serial
*)
1202 usb_get_serial_data(port
->serial
), edge_port
);
1204 dev_dbg(&port
->dev
, "%s wrote %d byte(s) TxCredits %d, Fifo %d\n",
1205 __func__
, copySize
, edge_port
->txCredits
, fifo
->count
);
1211 /************************************************************************
1213 * send_more_port_data()
1215 * This routine attempts to write additional UART transmit data
1216 * to a port over the USB bulk pipe. It is called (1) when new
1217 * data has been written to a port's TxBuffer from higher layers
1218 * (2) when the peripheral sends us additional TxCredits indicating
1219 * that it can accept more Tx data for a given port; and (3) when
1220 * a bulk write completes successfully and we want to see if we
1221 * can transmit more.
1223 ************************************************************************/
1224 static void send_more_port_data(struct edgeport_serial
*edge_serial
,
1225 struct edgeport_port
*edge_port
)
1227 struct TxFifo
*fifo
= &edge_port
->txfifo
;
1228 struct device
*dev
= &edge_port
->port
->dev
;
1230 unsigned char *buffer
;
1236 unsigned long flags
;
1238 spin_lock_irqsave(&edge_port
->ep_lock
, flags
);
1240 if (edge_port
->write_in_progress
||
1242 (fifo
->count
== 0)) {
1243 dev_dbg(dev
, "%s(%d) EXIT - fifo %d, PendingWrite = %d\n",
1244 __func__
, edge_port
->port
->number
,
1245 fifo
->count
, edge_port
->write_in_progress
);
1249 /* since the amount of data in the fifo will always fit into the
1250 * edgeport buffer we do not need to check the write length
1252 * Do we have enough credits for this port to make it worthwhile
1253 * to bother queueing a write. If it's too small, say a few bytes,
1254 * it's better to wait for more credits so we can do a larger write.
1256 if (edge_port
->txCredits
< EDGE_FW_GET_TX_CREDITS_SEND_THRESHOLD(edge_port
->maxTxCredits
, EDGE_FW_BULK_MAX_PACKET_SIZE
)) {
1257 dev_dbg(dev
, "%s(%d) Not enough credit - fifo %d TxCredit %d\n",
1258 __func__
, edge_port
->port
->number
, fifo
->count
,
1259 edge_port
->txCredits
);
1263 /* lock this write */
1264 edge_port
->write_in_progress
= true;
1266 /* get a pointer to the write_urb */
1267 urb
= edge_port
->write_urb
;
1269 /* make sure transfer buffer is freed */
1270 kfree(urb
->transfer_buffer
);
1271 urb
->transfer_buffer
= NULL
;
1273 /* build the data header for the buffer and port that we are about
1275 count
= fifo
->count
;
1276 buffer
= kmalloc(count
+2, GFP_ATOMIC
);
1277 if (buffer
== NULL
) {
1278 dev_err_console(edge_port
->port
,
1279 "%s - no more kernel memory...\n", __func__
);
1280 edge_port
->write_in_progress
= false;
1283 buffer
[0] = IOSP_BUILD_DATA_HDR1(edge_port
->port
->number
1284 - edge_port
->port
->serial
->minor
, count
);
1285 buffer
[1] = IOSP_BUILD_DATA_HDR2(edge_port
->port
->number
1286 - edge_port
->port
->serial
->minor
, count
);
1288 /* now copy our data */
1289 bytesleft
= fifo
->size
- fifo
->tail
;
1290 firsthalf
= min(bytesleft
, count
);
1291 memcpy(&buffer
[2], &fifo
->fifo
[fifo
->tail
], firsthalf
);
1292 fifo
->tail
+= firsthalf
;
1293 fifo
->count
-= firsthalf
;
1294 if (fifo
->tail
== fifo
->size
)
1297 secondhalf
= count
-firsthalf
;
1299 memcpy(&buffer
[2+firsthalf
], &fifo
->fifo
[fifo
->tail
],
1301 fifo
->tail
+= secondhalf
;
1302 fifo
->count
-= secondhalf
;
1306 usb_serial_debug_data(&edge_port
->port
->dev
, __func__
, count
, &buffer
[2]);
1308 /* fill up the urb with all of our data and submit it */
1309 usb_fill_bulk_urb(urb
, edge_serial
->serial
->dev
,
1310 usb_sndbulkpipe(edge_serial
->serial
->dev
,
1311 edge_serial
->bulk_out_endpoint
),
1313 edge_bulk_out_data_callback
, edge_port
);
1315 /* decrement the number of credits we have by the number we just sent */
1316 edge_port
->txCredits
-= count
;
1317 edge_port
->icount
.tx
+= count
;
1319 status
= usb_submit_urb(urb
, GFP_ATOMIC
);
1321 /* something went wrong */
1322 dev_err_console(edge_port
->port
,
1323 "%s - usb_submit_urb(write bulk) failed, status = %d, data lost\n",
1325 edge_port
->write_in_progress
= false;
1327 /* revert the credits as something bad happened. */
1328 edge_port
->txCredits
+= count
;
1329 edge_port
->icount
.tx
-= count
;
1331 dev_dbg(dev
, "%s wrote %d byte(s) TxCredit %d, Fifo %d\n",
1332 __func__
, count
, edge_port
->txCredits
, fifo
->count
);
1335 spin_unlock_irqrestore(&edge_port
->ep_lock
, flags
);
1339 /*****************************************************************************
1341 * this function is called by the tty driver when it wants to know how
1342 * many bytes of data we can accept for a specific port. If successful,
1343 * we return the amount of room that we have for this port (the txCredits)
1344 * otherwise we return a negative error number.
1345 *****************************************************************************/
1346 static int edge_write_room(struct tty_struct
*tty
)
1348 struct usb_serial_port
*port
= tty
->driver_data
;
1349 struct edgeport_port
*edge_port
= usb_get_serial_port_data(port
);
1351 unsigned long flags
;
1353 if (edge_port
== NULL
)
1355 if (edge_port
->closePending
)
1358 if (!edge_port
->open
) {
1359 dev_dbg(&port
->dev
, "%s - port not opened\n", __func__
);
1363 /* total of both buffers is still txCredit */
1364 spin_lock_irqsave(&edge_port
->ep_lock
, flags
);
1365 room
= edge_port
->txCredits
- edge_port
->txfifo
.count
;
1366 spin_unlock_irqrestore(&edge_port
->ep_lock
, flags
);
1368 dev_dbg(&port
->dev
, "%s - returns %d\n", __func__
, room
);
1373 /*****************************************************************************
1374 * edge_chars_in_buffer
1375 * this function is called by the tty driver when it wants to know how
1376 * many bytes of data we currently have outstanding in the port (data that
1377 * has been written, but hasn't made it out the port yet)
1378 * If successful, we return the number of bytes left to be written in the
1380 * Otherwise we return a negative error number.
1381 *****************************************************************************/
1382 static int edge_chars_in_buffer(struct tty_struct
*tty
)
1384 struct usb_serial_port
*port
= tty
->driver_data
;
1385 struct edgeport_port
*edge_port
= usb_get_serial_port_data(port
);
1387 unsigned long flags
;
1389 if (edge_port
== NULL
)
1391 if (edge_port
->closePending
)
1394 if (!edge_port
->open
) {
1395 dev_dbg(&port
->dev
, "%s - port not opened\n", __func__
);
1399 spin_lock_irqsave(&edge_port
->ep_lock
, flags
);
1400 num_chars
= edge_port
->maxTxCredits
- edge_port
->txCredits
+
1401 edge_port
->txfifo
.count
;
1402 spin_unlock_irqrestore(&edge_port
->ep_lock
, flags
);
1404 dev_dbg(&port
->dev
, "%s(port %d) - returns %d\n", __func__
,
1405 port
->number
, num_chars
);
1412 /*****************************************************************************
1414 * this function is called by the tty driver when it wants to stop the data
1415 * being read from the port.
1416 *****************************************************************************/
1417 static void edge_throttle(struct tty_struct
*tty
)
1419 struct usb_serial_port
*port
= tty
->driver_data
;
1420 struct edgeport_port
*edge_port
= usb_get_serial_port_data(port
);
1423 if (edge_port
== NULL
)
1426 if (!edge_port
->open
) {
1427 dev_dbg(&port
->dev
, "%s - port not opened\n", __func__
);
1431 /* if we are implementing XON/XOFF, send the stop character */
1433 unsigned char stop_char
= STOP_CHAR(tty
);
1434 status
= edge_write(tty
, port
, &stop_char
, 1);
1439 /* if we are implementing RTS/CTS, toggle that line */
1440 if (tty
->termios
.c_cflag
& CRTSCTS
) {
1441 edge_port
->shadowMCR
&= ~MCR_RTS
;
1442 status
= send_cmd_write_uart_register(edge_port
, MCR
,
1443 edge_port
->shadowMCR
);
1450 /*****************************************************************************
1452 * this function is called by the tty driver when it wants to resume the
1453 * data being read from the port (called after SerialThrottle is called)
1454 *****************************************************************************/
1455 static void edge_unthrottle(struct tty_struct
*tty
)
1457 struct usb_serial_port
*port
= tty
->driver_data
;
1458 struct edgeport_port
*edge_port
= usb_get_serial_port_data(port
);
1461 if (edge_port
== NULL
)
1464 if (!edge_port
->open
) {
1465 dev_dbg(&port
->dev
, "%s - port not opened\n", __func__
);
1469 /* if we are implementing XON/XOFF, send the start character */
1471 unsigned char start_char
= START_CHAR(tty
);
1472 status
= edge_write(tty
, port
, &start_char
, 1);
1476 /* if we are implementing RTS/CTS, toggle that line */
1477 if (tty
->termios
.c_cflag
& CRTSCTS
) {
1478 edge_port
->shadowMCR
|= MCR_RTS
;
1479 send_cmd_write_uart_register(edge_port
, MCR
,
1480 edge_port
->shadowMCR
);
1485 /*****************************************************************************
1487 * this function is called by the tty driver when it wants to change
1488 * the termios structure
1489 *****************************************************************************/
1490 static void edge_set_termios(struct tty_struct
*tty
,
1491 struct usb_serial_port
*port
, struct ktermios
*old_termios
)
1493 struct edgeport_port
*edge_port
= usb_get_serial_port_data(port
);
1496 cflag
= tty
->termios
.c_cflag
;
1497 dev_dbg(&port
->dev
, "%s - clfag %08x iflag %08x\n", __func__
, tty
->termios
.c_cflag
, tty
->termios
.c_iflag
);
1498 dev_dbg(&port
->dev
, "%s - old clfag %08x old iflag %08x\n", __func__
, old_termios
->c_cflag
, old_termios
->c_iflag
);
1500 if (edge_port
== NULL
)
1503 if (!edge_port
->open
) {
1504 dev_dbg(&port
->dev
, "%s - port not opened\n", __func__
);
1508 /* change the port settings to the new ones specified */
1509 change_port_settings(tty
, edge_port
, old_termios
);
1513 /*****************************************************************************
1514 * get_lsr_info - get line status register info
1516 * Purpose: Let user call ioctl() to get info when the UART physically
1517 * is emptied. On bus types like RS485, the transmitter must
1518 * release the bus after transmitting. This must be done when
1519 * the transmit shift register is empty, not be done when the
1520 * transmit holding register is empty. This functionality
1521 * allows an RS485 driver to be written in user space.
1522 *****************************************************************************/
1523 static int get_lsr_info(struct edgeport_port
*edge_port
,
1524 unsigned int __user
*value
)
1526 unsigned int result
= 0;
1527 unsigned long flags
;
1529 spin_lock_irqsave(&edge_port
->ep_lock
, flags
);
1530 if (edge_port
->maxTxCredits
== edge_port
->txCredits
&&
1531 edge_port
->txfifo
.count
== 0) {
1532 dev_dbg(&edge_port
->port
->dev
, "%s -- Empty\n", __func__
);
1533 result
= TIOCSER_TEMT
;
1535 spin_unlock_irqrestore(&edge_port
->ep_lock
, flags
);
1537 if (copy_to_user(value
, &result
, sizeof(int)))
1542 static int edge_tiocmset(struct tty_struct
*tty
,
1543 unsigned int set
, unsigned int clear
)
1545 struct usb_serial_port
*port
= tty
->driver_data
;
1546 struct edgeport_port
*edge_port
= usb_get_serial_port_data(port
);
1549 mcr
= edge_port
->shadowMCR
;
1550 if (set
& TIOCM_RTS
)
1552 if (set
& TIOCM_DTR
)
1554 if (set
& TIOCM_LOOP
)
1555 mcr
|= MCR_LOOPBACK
;
1557 if (clear
& TIOCM_RTS
)
1559 if (clear
& TIOCM_DTR
)
1561 if (clear
& TIOCM_LOOP
)
1562 mcr
&= ~MCR_LOOPBACK
;
1564 edge_port
->shadowMCR
= mcr
;
1566 send_cmd_write_uart_register(edge_port
, MCR
, edge_port
->shadowMCR
);
1571 static int edge_tiocmget(struct tty_struct
*tty
)
1573 struct usb_serial_port
*port
= tty
->driver_data
;
1574 struct edgeport_port
*edge_port
= usb_get_serial_port_data(port
);
1575 unsigned int result
= 0;
1579 msr
= edge_port
->shadowMSR
;
1580 mcr
= edge_port
->shadowMCR
;
1581 result
= ((mcr
& MCR_DTR
) ? TIOCM_DTR
: 0) /* 0x002 */
1582 | ((mcr
& MCR_RTS
) ? TIOCM_RTS
: 0) /* 0x004 */
1583 | ((msr
& EDGEPORT_MSR_CTS
) ? TIOCM_CTS
: 0) /* 0x020 */
1584 | ((msr
& EDGEPORT_MSR_CD
) ? TIOCM_CAR
: 0) /* 0x040 */
1585 | ((msr
& EDGEPORT_MSR_RI
) ? TIOCM_RI
: 0) /* 0x080 */
1586 | ((msr
& EDGEPORT_MSR_DSR
) ? TIOCM_DSR
: 0); /* 0x100 */
1591 static int edge_get_icount(struct tty_struct
*tty
,
1592 struct serial_icounter_struct
*icount
)
1594 struct usb_serial_port
*port
= tty
->driver_data
;
1595 struct edgeport_port
*edge_port
= usb_get_serial_port_data(port
);
1596 struct async_icount cnow
;
1597 cnow
= edge_port
->icount
;
1599 icount
->cts
= cnow
.cts
;
1600 icount
->dsr
= cnow
.dsr
;
1601 icount
->rng
= cnow
.rng
;
1602 icount
->dcd
= cnow
.dcd
;
1603 icount
->rx
= cnow
.rx
;
1604 icount
->tx
= cnow
.tx
;
1605 icount
->frame
= cnow
.frame
;
1606 icount
->overrun
= cnow
.overrun
;
1607 icount
->parity
= cnow
.parity
;
1608 icount
->brk
= cnow
.brk
;
1609 icount
->buf_overrun
= cnow
.buf_overrun
;
1611 dev_dbg(&port
->dev
, "%s (%d) TIOCGICOUNT RX=%d, TX=%d\n", __func__
,
1612 port
->number
, icount
->rx
, icount
->tx
);
1616 static int get_serial_info(struct edgeport_port
*edge_port
,
1617 struct serial_struct __user
*retinfo
)
1619 struct serial_struct tmp
;
1624 memset(&tmp
, 0, sizeof(tmp
));
1626 tmp
.type
= PORT_16550A
;
1627 tmp
.line
= edge_port
->port
->serial
->minor
;
1628 tmp
.port
= edge_port
->port
->number
;
1630 tmp
.flags
= ASYNC_SKIP_TEST
| ASYNC_AUTO_IRQ
;
1631 tmp
.xmit_fifo_size
= edge_port
->maxTxCredits
;
1632 tmp
.baud_base
= 9600;
1633 tmp
.close_delay
= 5*HZ
;
1634 tmp
.closing_wait
= 30*HZ
;
1636 if (copy_to_user(retinfo
, &tmp
, sizeof(*retinfo
)))
1642 /*****************************************************************************
1644 * this function handles any ioctl calls to the driver
1645 *****************************************************************************/
1646 static int edge_ioctl(struct tty_struct
*tty
,
1647 unsigned int cmd
, unsigned long arg
)
1649 struct usb_serial_port
*port
= tty
->driver_data
;
1651 struct edgeport_port
*edge_port
= usb_get_serial_port_data(port
);
1652 struct async_icount cnow
;
1653 struct async_icount cprev
;
1655 dev_dbg(&port
->dev
, "%s - port %d, cmd = 0x%x\n", __func__
, port
->number
, cmd
);
1659 dev_dbg(&port
->dev
, "%s (%d) TIOCSERGETLSR\n", __func__
, port
->number
);
1660 return get_lsr_info(edge_port
, (unsigned int __user
*) arg
);
1663 dev_dbg(&port
->dev
, "%s (%d) TIOCGSERIAL\n", __func__
, port
->number
);
1664 return get_serial_info(edge_port
, (struct serial_struct __user
*) arg
);
1667 dev_dbg(&port
->dev
, "%s (%d) TIOCMIWAIT\n", __func__
, port
->number
);
1668 cprev
= edge_port
->icount
;
1670 prepare_to_wait(&port
->delta_msr_wait
,
1671 &wait
, TASK_INTERRUPTIBLE
);
1673 finish_wait(&port
->delta_msr_wait
, &wait
);
1674 /* see if a signal did it */
1675 if (signal_pending(current
))
1676 return -ERESTARTSYS
;
1678 if (port
->serial
->disconnected
)
1681 cnow
= edge_port
->icount
;
1682 if (cnow
.rng
== cprev
.rng
&& cnow
.dsr
== cprev
.dsr
&&
1683 cnow
.dcd
== cprev
.dcd
&& cnow
.cts
== cprev
.cts
)
1684 return -EIO
; /* no change => error */
1685 if (((arg
& TIOCM_RNG
) && (cnow
.rng
!= cprev
.rng
)) ||
1686 ((arg
& TIOCM_DSR
) && (cnow
.dsr
!= cprev
.dsr
)) ||
1687 ((arg
& TIOCM_CD
) && (cnow
.dcd
!= cprev
.dcd
)) ||
1688 ((arg
& TIOCM_CTS
) && (cnow
.cts
!= cprev
.cts
))) {
1697 return -ENOIOCTLCMD
;
1701 /*****************************************************************************
1703 * this function sends a break to the port
1704 *****************************************************************************/
1705 static void edge_break(struct tty_struct
*tty
, int break_state
)
1707 struct usb_serial_port
*port
= tty
->driver_data
;
1708 struct edgeport_port
*edge_port
= usb_get_serial_port_data(port
);
1709 struct edgeport_serial
*edge_serial
= usb_get_serial_data(port
->serial
);
1712 if ((!edge_serial
->is_epic
) ||
1713 ((edge_serial
->is_epic
) &&
1714 (edge_serial
->epic_descriptor
.Supports
.IOSPChase
))) {
1715 /* flush and chase */
1716 edge_port
->chaseResponsePending
= true;
1718 dev_dbg(&port
->dev
, "%s - Sending IOSP_CMD_CHASE_PORT\n", __func__
);
1719 status
= send_iosp_ext_cmd(edge_port
, IOSP_CMD_CHASE_PORT
, 0);
1721 /* block until chase finished */
1722 block_until_chase_response(edge_port
);
1724 edge_port
->chaseResponsePending
= false;
1728 if ((!edge_serial
->is_epic
) ||
1729 ((edge_serial
->is_epic
) &&
1730 (edge_serial
->epic_descriptor
.Supports
.IOSPSetClrBreak
))) {
1731 if (break_state
== -1) {
1732 dev_dbg(&port
->dev
, "%s - Sending IOSP_CMD_SET_BREAK\n", __func__
);
1733 status
= send_iosp_ext_cmd(edge_port
,
1734 IOSP_CMD_SET_BREAK
, 0);
1736 dev_dbg(&port
->dev
, "%s - Sending IOSP_CMD_CLEAR_BREAK\n", __func__
);
1737 status
= send_iosp_ext_cmd(edge_port
,
1738 IOSP_CMD_CLEAR_BREAK
, 0);
1741 dev_dbg(&port
->dev
, "%s - error sending break set/clear command.\n",
1747 /*****************************************************************************
1749 * this function handles the data received on the bulk in pipe.
1750 *****************************************************************************/
1751 static void process_rcvd_data(struct edgeport_serial
*edge_serial
,
1752 unsigned char *buffer
, __u16 bufferLength
)
1754 struct device
*dev
= &edge_serial
->serial
->dev
->dev
;
1755 struct usb_serial_port
*port
;
1756 struct edgeport_port
*edge_port
;
1757 __u16 lastBufferLength
;
1760 lastBufferLength
= bufferLength
+ 1;
1762 while (bufferLength
> 0) {
1763 /* failsafe incase we get a message that we don't understand */
1764 if (lastBufferLength
== bufferLength
) {
1765 dev_dbg(dev
, "%s - stuck in loop, exiting it.\n", __func__
);
1768 lastBufferLength
= bufferLength
;
1770 switch (edge_serial
->rxState
) {
1772 edge_serial
->rxHeader1
= *buffer
;
1776 if (bufferLength
== 0) {
1777 edge_serial
->rxState
= EXPECT_HDR2
;
1780 /* otherwise, drop on through */
1782 edge_serial
->rxHeader2
= *buffer
;
1786 dev_dbg(dev
, "%s - Hdr1=%02X Hdr2=%02X\n", __func__
,
1787 edge_serial
->rxHeader1
, edge_serial
->rxHeader2
);
1788 /* Process depending on whether this header is
1791 if (IS_CMD_STAT_HDR(edge_serial
->rxHeader1
)) {
1792 /* Decode this status header and go to
1793 * EXPECT_HDR1 (if we can process the status
1794 * with only 2 bytes), or go to EXPECT_HDR3 to
1795 * get the third byte. */
1796 edge_serial
->rxPort
=
1797 IOSP_GET_HDR_PORT(edge_serial
->rxHeader1
);
1798 edge_serial
->rxStatusCode
=
1799 IOSP_GET_STATUS_CODE(
1800 edge_serial
->rxHeader1
);
1802 if (!IOSP_STATUS_IS_2BYTE(
1803 edge_serial
->rxStatusCode
)) {
1804 /* This status needs additional bytes.
1805 * Save what we have and then wait for
1808 edge_serial
->rxStatusParam
1809 = edge_serial
->rxHeader2
;
1810 edge_serial
->rxState
= EXPECT_HDR3
;
1813 /* We have all the header bytes, process the
1815 process_rcvd_status(edge_serial
,
1816 edge_serial
->rxHeader2
, 0);
1817 edge_serial
->rxState
= EXPECT_HDR1
;
1820 edge_serial
->rxPort
=
1821 IOSP_GET_HDR_PORT(edge_serial
->rxHeader1
);
1822 edge_serial
->rxBytesRemaining
=
1823 IOSP_GET_HDR_DATA_LEN(
1824 edge_serial
->rxHeader1
,
1825 edge_serial
->rxHeader2
);
1826 dev_dbg(dev
, "%s - Data for Port %u Len %u\n",
1828 edge_serial
->rxPort
,
1829 edge_serial
->rxBytesRemaining
);
1831 /* ASSERT(DevExt->RxPort < DevExt->NumPorts);
1832 * ASSERT(DevExt->RxBytesRemaining <
1833 * IOSP_MAX_DATA_LENGTH);
1836 if (bufferLength
== 0) {
1837 edge_serial
->rxState
= EXPECT_DATA
;
1840 /* Else, drop through */
1842 case EXPECT_DATA
: /* Expect data */
1843 if (bufferLength
< edge_serial
->rxBytesRemaining
) {
1844 rxLen
= bufferLength
;
1845 /* Expect data to start next buffer */
1846 edge_serial
->rxState
= EXPECT_DATA
;
1848 /* BufLen >= RxBytesRemaining */
1849 rxLen
= edge_serial
->rxBytesRemaining
;
1850 /* Start another header next time */
1851 edge_serial
->rxState
= EXPECT_HDR1
;
1854 bufferLength
-= rxLen
;
1855 edge_serial
->rxBytesRemaining
-= rxLen
;
1857 /* spit this data back into the tty driver if this
1860 port
= edge_serial
->serial
->port
[
1861 edge_serial
->rxPort
];
1862 edge_port
= usb_get_serial_port_data(port
);
1863 if (edge_port
->open
) {
1864 dev_dbg(dev
, "%s - Sending %d bytes to TTY for port %d\n",
1866 edge_serial
->rxPort
);
1867 edge_tty_recv(edge_port
->port
, buffer
,
1869 edge_port
->icount
.rx
+= rxLen
;
1875 case EXPECT_HDR3
: /* Expect 3rd byte of status header */
1876 edge_serial
->rxHeader3
= *buffer
;
1880 /* We have all the header bytes, process the
1882 process_rcvd_status(edge_serial
,
1883 edge_serial
->rxStatusParam
,
1884 edge_serial
->rxHeader3
);
1885 edge_serial
->rxState
= EXPECT_HDR1
;
1892 /*****************************************************************************
1893 * process_rcvd_status
1894 * this function handles the any status messages received on the
1896 *****************************************************************************/
1897 static void process_rcvd_status(struct edgeport_serial
*edge_serial
,
1898 __u8 byte2
, __u8 byte3
)
1900 struct usb_serial_port
*port
;
1901 struct edgeport_port
*edge_port
;
1902 struct tty_struct
*tty
;
1904 __u8 code
= edge_serial
->rxStatusCode
;
1906 /* switch the port pointer to the one being currently talked about */
1907 port
= edge_serial
->serial
->port
[edge_serial
->rxPort
];
1908 edge_port
= usb_get_serial_port_data(port
);
1909 if (edge_port
== NULL
) {
1910 dev_err(&edge_serial
->serial
->dev
->dev
,
1911 "%s - edge_port == NULL for port %d\n",
1912 __func__
, edge_serial
->rxPort
);
1917 if (code
== IOSP_EXT_STATUS
) {
1919 case IOSP_EXT_STATUS_CHASE_RSP
:
1920 /* we want to do EXT status regardless of port
1922 dev_dbg(dev
, "%s - Port %u EXT CHASE_RSP Data = %02x\n",
1923 __func__
, edge_serial
->rxPort
, byte3
);
1924 /* Currently, the only EXT_STATUS is Chase, so process
1925 * here instead of one more call to one more subroutine
1926 * If/when more EXT_STATUS, there'll be more work to do
1927 * Also, we currently clear flag and close the port
1928 * regardless of content of above's Byte3.
1929 * We could choose to do something else when Byte3 says
1930 * Timeout on Chase from Edgeport, like wait longer in
1931 * block_until_chase_response, but for now we don't.
1933 edge_port
->chaseResponsePending
= false;
1934 wake_up(&edge_port
->wait_chase
);
1937 case IOSP_EXT_STATUS_RX_CHECK_RSP
:
1938 dev_dbg(dev
, "%s ========== Port %u CHECK_RSP Sequence = %02x =============\n",
1939 __func__
, edge_serial
->rxPort
, byte3
);
1940 /* Port->RxCheckRsp = true; */
1945 if (code
== IOSP_STATUS_OPEN_RSP
) {
1946 edge_port
->txCredits
= GET_TX_BUFFER_SIZE(byte3
);
1947 edge_port
->maxTxCredits
= edge_port
->txCredits
;
1948 dev_dbg(dev
, "%s - Port %u Open Response Initial MSR = %02x TxBufferSize = %d\n",
1949 __func__
, edge_serial
->rxPort
, byte2
, edge_port
->txCredits
);
1950 handle_new_msr(edge_port
, byte2
);
1952 /* send the current line settings to the port so we are
1953 in sync with any further termios calls */
1954 tty
= tty_port_tty_get(&edge_port
->port
->port
);
1956 change_port_settings(tty
,
1957 edge_port
, &tty
->termios
);
1961 /* we have completed the open */
1962 edge_port
->openPending
= false;
1963 edge_port
->open
= true;
1964 wake_up(&edge_port
->wait_open
);
1968 /* If port is closed, silently discard all rcvd status. We can
1969 * have cases where buffered status is received AFTER the close
1970 * port command is sent to the Edgeport.
1972 if (!edge_port
->open
|| edge_port
->closePending
)
1976 /* Not currently sent by Edgeport */
1977 case IOSP_STATUS_LSR
:
1978 dev_dbg(dev
, "%s - Port %u LSR Status = %02x\n",
1979 __func__
, edge_serial
->rxPort
, byte2
);
1980 handle_new_lsr(edge_port
, false, byte2
, 0);
1983 case IOSP_STATUS_LSR_DATA
:
1984 dev_dbg(dev
, "%s - Port %u LSR Status = %02x, Data = %02x\n",
1985 __func__
, edge_serial
->rxPort
, byte2
, byte3
);
1986 /* byte2 is LSR Register */
1987 /* byte3 is broken data byte */
1988 handle_new_lsr(edge_port
, true, byte2
, byte3
);
1991 * case IOSP_EXT_4_STATUS:
1992 * dev_dbg(dev, "%s - Port %u LSR Status = %02x Data = %02x\n",
1993 * __func__, edge_serial->rxPort, byte2, byte3);
1996 case IOSP_STATUS_MSR
:
1997 dev_dbg(dev
, "%s - Port %u MSR Status = %02x\n",
1998 __func__
, edge_serial
->rxPort
, byte2
);
2000 * Process this new modem status and generate appropriate
2001 * events, etc, based on the new status. This routine
2002 * also saves the MSR in Port->ShadowMsr.
2004 handle_new_msr(edge_port
, byte2
);
2008 dev_dbg(dev
, "%s - Unrecognized IOSP status code %u\n", __func__
, code
);
2014 /*****************************************************************************
2016 * this function passes data on to the tty flip buffer
2017 *****************************************************************************/
2018 static void edge_tty_recv(struct usb_serial_port
*port
, unsigned char *data
,
2023 cnt
= tty_insert_flip_string(&port
->port
, data
, length
);
2025 dev_err(&port
->dev
, "%s - dropping data, %d bytes lost\n",
2026 __func__
, length
- cnt
);
2031 tty_flip_buffer_push(&port
->port
);
2035 /*****************************************************************************
2037 * this function handles any change to the msr register for a port.
2038 *****************************************************************************/
2039 static void handle_new_msr(struct edgeport_port
*edge_port
, __u8 newMsr
)
2041 struct async_icount
*icount
;
2043 if (newMsr
& (EDGEPORT_MSR_DELTA_CTS
| EDGEPORT_MSR_DELTA_DSR
|
2044 EDGEPORT_MSR_DELTA_RI
| EDGEPORT_MSR_DELTA_CD
)) {
2045 icount
= &edge_port
->icount
;
2047 /* update input line counters */
2048 if (newMsr
& EDGEPORT_MSR_DELTA_CTS
)
2050 if (newMsr
& EDGEPORT_MSR_DELTA_DSR
)
2052 if (newMsr
& EDGEPORT_MSR_DELTA_CD
)
2054 if (newMsr
& EDGEPORT_MSR_DELTA_RI
)
2056 wake_up_interruptible(&edge_port
->port
->delta_msr_wait
);
2059 /* Save the new modem status */
2060 edge_port
->shadowMSR
= newMsr
& 0xf0;
2064 /*****************************************************************************
2066 * this function handles any change to the lsr register for a port.
2067 *****************************************************************************/
2068 static void handle_new_lsr(struct edgeport_port
*edge_port
, __u8 lsrData
,
2069 __u8 lsr
, __u8 data
)
2071 __u8 newLsr
= (__u8
) (lsr
& (__u8
)
2072 (LSR_OVER_ERR
| LSR_PAR_ERR
| LSR_FRM_ERR
| LSR_BREAK
));
2073 struct async_icount
*icount
;
2075 edge_port
->shadowLSR
= lsr
;
2077 if (newLsr
& LSR_BREAK
) {
2079 * Parity and Framing errors only count if they
2080 * occur exclusive of a break being
2083 newLsr
&= (__u8
)(LSR_OVER_ERR
| LSR_BREAK
);
2086 /* Place LSR data byte into Rx buffer */
2088 edge_tty_recv(edge_port
->port
, &data
, 1);
2090 /* update input line counters */
2091 icount
= &edge_port
->icount
;
2092 if (newLsr
& LSR_BREAK
)
2094 if (newLsr
& LSR_OVER_ERR
)
2096 if (newLsr
& LSR_PAR_ERR
)
2098 if (newLsr
& LSR_FRM_ERR
)
2103 /****************************************************************************
2105 * writes a number of bytes to the Edgeport device's sram starting at the
2107 * If successful returns the number of bytes written, otherwise it returns
2108 * a negative error number of the problem.
2109 ****************************************************************************/
2110 static int sram_write(struct usb_serial
*serial
, __u16 extAddr
, __u16 addr
,
2111 __u16 length
, const __u8
*data
)
2114 __u16 current_length
;
2115 unsigned char *transfer_buffer
;
2117 dev_dbg(&serial
->dev
->dev
, "%s - %x, %x, %d\n", __func__
, extAddr
, addr
, length
);
2119 transfer_buffer
= kmalloc(64, GFP_KERNEL
);
2120 if (!transfer_buffer
) {
2121 dev_err(&serial
->dev
->dev
, "%s - kmalloc(%d) failed.\n",
2126 /* need to split these writes up into 64 byte chunks */
2128 while (length
> 0) {
2130 current_length
= 64;
2132 current_length
= length
;
2134 /* dev_dbg(&serial->dev->dev, "%s - writing %x, %x, %d\n", __func__, extAddr, addr, current_length); */
2135 memcpy(transfer_buffer
, data
, current_length
);
2136 result
= usb_control_msg(serial
->dev
,
2137 usb_sndctrlpipe(serial
->dev
, 0),
2138 USB_REQUEST_ION_WRITE_RAM
,
2139 0x40, addr
, extAddr
, transfer_buffer
,
2140 current_length
, 300);
2143 length
-= current_length
;
2144 addr
+= current_length
;
2145 data
+= current_length
;
2148 kfree(transfer_buffer
);
2153 /****************************************************************************
2155 * writes a number of bytes to the Edgeport device's ROM starting at the
2157 * If successful returns the number of bytes written, otherwise it returns
2158 * a negative error number of the problem.
2159 ****************************************************************************/
2160 static int rom_write(struct usb_serial
*serial
, __u16 extAddr
, __u16 addr
,
2161 __u16 length
, const __u8
*data
)
2164 __u16 current_length
;
2165 unsigned char *transfer_buffer
;
2167 transfer_buffer
= kmalloc(64, GFP_KERNEL
);
2168 if (!transfer_buffer
) {
2169 dev_err(&serial
->dev
->dev
, "%s - kmalloc(%d) failed.\n",
2174 /* need to split these writes up into 64 byte chunks */
2176 while (length
> 0) {
2178 current_length
= 64;
2180 current_length
= length
;
2181 memcpy(transfer_buffer
, data
, current_length
);
2182 result
= usb_control_msg(serial
->dev
,
2183 usb_sndctrlpipe(serial
->dev
, 0),
2184 USB_REQUEST_ION_WRITE_ROM
, 0x40,
2186 transfer_buffer
, current_length
, 300);
2189 length
-= current_length
;
2190 addr
+= current_length
;
2191 data
+= current_length
;
2194 kfree(transfer_buffer
);
2199 /****************************************************************************
2201 * reads a number of bytes from the Edgeport device starting at the given
2203 * If successful returns the number of bytes read, otherwise it returns
2204 * a negative error number of the problem.
2205 ****************************************************************************/
2206 static int rom_read(struct usb_serial
*serial
, __u16 extAddr
,
2207 __u16 addr
, __u16 length
, __u8
*data
)
2210 __u16 current_length
;
2211 unsigned char *transfer_buffer
;
2213 transfer_buffer
= kmalloc(64, GFP_KERNEL
);
2214 if (!transfer_buffer
) {
2215 dev_err(&serial
->dev
->dev
,
2216 "%s - kmalloc(%d) failed.\n", __func__
, 64);
2220 /* need to split these reads up into 64 byte chunks */
2222 while (length
> 0) {
2224 current_length
= 64;
2226 current_length
= length
;
2227 result
= usb_control_msg(serial
->dev
,
2228 usb_rcvctrlpipe(serial
->dev
, 0),
2229 USB_REQUEST_ION_READ_ROM
,
2230 0xC0, addr
, extAddr
, transfer_buffer
,
2231 current_length
, 300);
2234 memcpy(data
, transfer_buffer
, current_length
);
2235 length
-= current_length
;
2236 addr
+= current_length
;
2237 data
+= current_length
;
2240 kfree(transfer_buffer
);
2245 /****************************************************************************
2247 * Is used to send a IOSP message to the Edgeport device
2248 ****************************************************************************/
2249 static int send_iosp_ext_cmd(struct edgeport_port
*edge_port
,
2250 __u8 command
, __u8 param
)
2252 unsigned char *buffer
;
2253 unsigned char *currentCommand
;
2257 buffer
= kmalloc(10, GFP_ATOMIC
);
2259 dev_err(&edge_port
->port
->dev
,
2260 "%s - kmalloc(%d) failed.\n", __func__
, 10);
2264 currentCommand
= buffer
;
2266 MAKE_CMD_EXT_CMD(¤tCommand
, &length
,
2267 edge_port
->port
->number
- edge_port
->port
->serial
->minor
,
2270 status
= write_cmd_usb(edge_port
, buffer
, length
);
2272 /* something bad happened, let's free up the memory */
2280 /*****************************************************************************
2282 * this function writes the given buffer out to the bulk write endpoint.
2283 *****************************************************************************/
2284 static int write_cmd_usb(struct edgeport_port
*edge_port
,
2285 unsigned char *buffer
, int length
)
2287 struct edgeport_serial
*edge_serial
=
2288 usb_get_serial_data(edge_port
->port
->serial
);
2289 struct device
*dev
= &edge_port
->port
->dev
;
2293 usb_serial_debug_data(dev
, __func__
, length
, buffer
);
2295 /* Allocate our next urb */
2296 urb
= usb_alloc_urb(0, GFP_ATOMIC
);
2300 atomic_inc(&CmdUrbs
);
2301 dev_dbg(dev
, "%s - ALLOCATE URB %p (outstanding %d)\n",
2302 __func__
, urb
, atomic_read(&CmdUrbs
));
2304 usb_fill_bulk_urb(urb
, edge_serial
->serial
->dev
,
2305 usb_sndbulkpipe(edge_serial
->serial
->dev
,
2306 edge_serial
->bulk_out_endpoint
),
2307 buffer
, length
, edge_bulk_out_cmd_callback
, edge_port
);
2309 edge_port
->commandPending
= true;
2310 status
= usb_submit_urb(urb
, GFP_ATOMIC
);
2313 /* something went wrong */
2314 dev_err(dev
, "%s - usb_submit_urb(write command) failed, status = %d\n",
2318 atomic_dec(&CmdUrbs
);
2323 wait_event(&edge_port
->wait_command
, !edge_port
->commandPending
);
2325 if (edge_port
->commandPending
) {
2326 /* command timed out */
2327 dev_dbg(dev
, "%s - command timed out\n", __func__
);
2335 /*****************************************************************************
2336 * send_cmd_write_baud_rate
2337 * this function sends the proper command to change the baud rate of the
2339 *****************************************************************************/
2340 static int send_cmd_write_baud_rate(struct edgeport_port
*edge_port
,
2343 struct edgeport_serial
*edge_serial
=
2344 usb_get_serial_data(edge_port
->port
->serial
);
2345 struct device
*dev
= &edge_port
->port
->dev
;
2346 unsigned char *cmdBuffer
;
2347 unsigned char *currCmd
;
2351 unsigned char number
=
2352 edge_port
->port
->number
- edge_port
->port
->serial
->minor
;
2354 if (edge_serial
->is_epic
&&
2355 !edge_serial
->epic_descriptor
.Supports
.IOSPSetBaudRate
) {
2356 dev_dbg(dev
, "SendCmdWriteBaudRate - NOT Setting baud rate for port = %d, baud = %d\n",
2357 edge_port
->port
->number
, baudRate
);
2361 dev_dbg(dev
, "%s - port = %d, baud = %d\n", __func__
,
2362 edge_port
->port
->number
, baudRate
);
2364 status
= calc_baud_rate_divisor(dev
, baudRate
, &divisor
);
2366 dev_err(dev
, "%s - bad baud rate\n", __func__
);
2370 /* Alloc memory for the string of commands. */
2371 cmdBuffer
= kmalloc(0x100, GFP_ATOMIC
);
2373 dev_err(dev
, "%s - kmalloc(%d) failed.\n", __func__
, 0x100);
2376 currCmd
= cmdBuffer
;
2378 /* Enable access to divisor latch */
2379 MAKE_CMD_WRITE_REG(&currCmd
, &cmdLen
, number
, LCR
, LCR_DL_ENABLE
);
2381 /* Write the divisor itself */
2382 MAKE_CMD_WRITE_REG(&currCmd
, &cmdLen
, number
, DLL
, LOW8(divisor
));
2383 MAKE_CMD_WRITE_REG(&currCmd
, &cmdLen
, number
, DLM
, HIGH8(divisor
));
2385 /* Restore original value to disable access to divisor latch */
2386 MAKE_CMD_WRITE_REG(&currCmd
, &cmdLen
, number
, LCR
,
2387 edge_port
->shadowLCR
);
2389 status
= write_cmd_usb(edge_port
, cmdBuffer
, cmdLen
);
2391 /* something bad happened, let's free up the memory */
2399 /*****************************************************************************
2400 * calc_baud_rate_divisor
2401 * this function calculates the proper baud rate divisor for the specified
2403 *****************************************************************************/
2404 static int calc_baud_rate_divisor(struct device
*dev
, int baudrate
, int *divisor
)
2409 for (i
= 0; i
< ARRAY_SIZE(divisor_table
); i
++) {
2410 if (divisor_table
[i
].BaudRate
== baudrate
) {
2411 *divisor
= divisor_table
[i
].Divisor
;
2416 /* We have tried all of the standard baud rates
2417 * lets try to calculate the divisor for this baud rate
2418 * Make sure the baud rate is reasonable */
2419 if (baudrate
> 50 && baudrate
< 230400) {
2421 custom
= (__u16
)((230400L + baudrate
/2) / baudrate
);
2425 dev_dbg(dev
, "%s - Baud %d = %d\n", __func__
, baudrate
, custom
);
2433 /*****************************************************************************
2434 * send_cmd_write_uart_register
2435 * this function builds up a uart register message and sends to the device.
2436 *****************************************************************************/
2437 static int send_cmd_write_uart_register(struct edgeport_port
*edge_port
,
2438 __u8 regNum
, __u8 regValue
)
2440 struct edgeport_serial
*edge_serial
=
2441 usb_get_serial_data(edge_port
->port
->serial
);
2442 struct device
*dev
= &edge_port
->port
->dev
;
2443 unsigned char *cmdBuffer
;
2444 unsigned char *currCmd
;
2445 unsigned long cmdLen
= 0;
2448 dev_dbg(dev
, "%s - write to %s register 0x%02x\n",
2449 (regNum
== MCR
) ? "MCR" : "LCR", __func__
, regValue
);
2451 if (edge_serial
->is_epic
&&
2452 !edge_serial
->epic_descriptor
.Supports
.IOSPWriteMCR
&&
2454 dev_dbg(dev
, "SendCmdWriteUartReg - Not writing to MCR Register\n");
2458 if (edge_serial
->is_epic
&&
2459 !edge_serial
->epic_descriptor
.Supports
.IOSPWriteLCR
&&
2461 dev_dbg(dev
, "SendCmdWriteUartReg - Not writing to LCR Register\n");
2465 /* Alloc memory for the string of commands. */
2466 cmdBuffer
= kmalloc(0x10, GFP_ATOMIC
);
2467 if (cmdBuffer
== NULL
)
2470 currCmd
= cmdBuffer
;
2472 /* Build a cmd in the buffer to write the given register */
2473 MAKE_CMD_WRITE_REG(&currCmd
, &cmdLen
,
2474 edge_port
->port
->number
- edge_port
->port
->serial
->minor
,
2477 status
= write_cmd_usb(edge_port
, cmdBuffer
, cmdLen
);
2479 /* something bad happened, let's free up the memory */
2487 /*****************************************************************************
2488 * change_port_settings
2489 * This routine is called to set the UART on the device to match the
2490 * specified new settings.
2491 *****************************************************************************/
2493 static void change_port_settings(struct tty_struct
*tty
,
2494 struct edgeport_port
*edge_port
, struct ktermios
*old_termios
)
2496 struct device
*dev
= &edge_port
->port
->dev
;
2497 struct edgeport_serial
*edge_serial
=
2498 usb_get_serial_data(edge_port
->port
->serial
);
2509 dev_dbg(dev
, "%s - port %d\n", __func__
, edge_port
->port
->number
);
2511 if (!edge_port
->open
&&
2512 !edge_port
->openPending
) {
2513 dev_dbg(dev
, "%s - port not opened\n", __func__
);
2517 cflag
= tty
->termios
.c_cflag
;
2519 switch (cflag
& CSIZE
) {
2521 lData
= LCR_BITS_5
; mask
= 0x1f;
2522 dev_dbg(dev
, "%s - data bits = 5\n", __func__
);
2525 lData
= LCR_BITS_6
; mask
= 0x3f;
2526 dev_dbg(dev
, "%s - data bits = 6\n", __func__
);
2529 lData
= LCR_BITS_7
; mask
= 0x7f;
2530 dev_dbg(dev
, "%s - data bits = 7\n", __func__
);
2535 dev_dbg(dev
, "%s - data bits = 8\n", __func__
);
2539 lParity
= LCR_PAR_NONE
;
2540 if (cflag
& PARENB
) {
2541 if (cflag
& CMSPAR
) {
2542 if (cflag
& PARODD
) {
2543 lParity
= LCR_PAR_MARK
;
2544 dev_dbg(dev
, "%s - parity = mark\n", __func__
);
2546 lParity
= LCR_PAR_SPACE
;
2547 dev_dbg(dev
, "%s - parity = space\n", __func__
);
2549 } else if (cflag
& PARODD
) {
2550 lParity
= LCR_PAR_ODD
;
2551 dev_dbg(dev
, "%s - parity = odd\n", __func__
);
2553 lParity
= LCR_PAR_EVEN
;
2554 dev_dbg(dev
, "%s - parity = even\n", __func__
);
2557 dev_dbg(dev
, "%s - parity = none\n", __func__
);
2560 if (cflag
& CSTOPB
) {
2562 dev_dbg(dev
, "%s - stop bits = 2\n", __func__
);
2565 dev_dbg(dev
, "%s - stop bits = 1\n", __func__
);
2568 /* figure out the flow control settings */
2569 rxFlow
= txFlow
= 0x00;
2570 if (cflag
& CRTSCTS
) {
2571 rxFlow
|= IOSP_RX_FLOW_RTS
;
2572 txFlow
|= IOSP_TX_FLOW_CTS
;
2573 dev_dbg(dev
, "%s - RTS/CTS is enabled\n", __func__
);
2575 dev_dbg(dev
, "%s - RTS/CTS is disabled\n", __func__
);
2578 /* if we are implementing XON/XOFF, set the start and stop character
2580 if (I_IXOFF(tty
) || I_IXON(tty
)) {
2581 unsigned char stop_char
= STOP_CHAR(tty
);
2582 unsigned char start_char
= START_CHAR(tty
);
2584 if ((!edge_serial
->is_epic
) ||
2585 ((edge_serial
->is_epic
) &&
2586 (edge_serial
->epic_descriptor
.Supports
.IOSPSetXChar
))) {
2587 send_iosp_ext_cmd(edge_port
,
2588 IOSP_CMD_SET_XON_CHAR
, start_char
);
2589 send_iosp_ext_cmd(edge_port
,
2590 IOSP_CMD_SET_XOFF_CHAR
, stop_char
);
2593 /* if we are implementing INBOUND XON/XOFF */
2595 rxFlow
|= IOSP_RX_FLOW_XON_XOFF
;
2596 dev_dbg(dev
, "%s - INBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x\n",
2597 __func__
, start_char
, stop_char
);
2599 dev_dbg(dev
, "%s - INBOUND XON/XOFF is disabled\n", __func__
);
2602 /* if we are implementing OUTBOUND XON/XOFF */
2604 txFlow
|= IOSP_TX_FLOW_XON_XOFF
;
2605 dev_dbg(dev
, "%s - OUTBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x\n",
2606 __func__
, start_char
, stop_char
);
2608 dev_dbg(dev
, "%s - OUTBOUND XON/XOFF is disabled\n", __func__
);
2612 /* Set flow control to the configured value */
2613 if ((!edge_serial
->is_epic
) ||
2614 ((edge_serial
->is_epic
) &&
2615 (edge_serial
->epic_descriptor
.Supports
.IOSPSetRxFlow
)))
2616 send_iosp_ext_cmd(edge_port
, IOSP_CMD_SET_RX_FLOW
, rxFlow
);
2617 if ((!edge_serial
->is_epic
) ||
2618 ((edge_serial
->is_epic
) &&
2619 (edge_serial
->epic_descriptor
.Supports
.IOSPSetTxFlow
)))
2620 send_iosp_ext_cmd(edge_port
, IOSP_CMD_SET_TX_FLOW
, txFlow
);
2623 edge_port
->shadowLCR
&= ~(LCR_BITS_MASK
| LCR_STOP_MASK
| LCR_PAR_MASK
);
2624 edge_port
->shadowLCR
|= (lData
| lParity
| lStop
);
2626 edge_port
->validDataMask
= mask
;
2628 /* Send the updated LCR value to the EdgePort */
2629 status
= send_cmd_write_uart_register(edge_port
, LCR
,
2630 edge_port
->shadowLCR
);
2634 /* set up the MCR register and send it to the EdgePort */
2635 edge_port
->shadowMCR
= MCR_MASTER_IE
;
2637 edge_port
->shadowMCR
|= (MCR_DTR
| MCR_RTS
);
2639 status
= send_cmd_write_uart_register(edge_port
, MCR
,
2640 edge_port
->shadowMCR
);
2644 /* Determine divisor based on baud rate */
2645 baud
= tty_get_baud_rate(tty
);
2647 /* pick a default, any default... */
2651 dev_dbg(dev
, "%s - baud rate = %d\n", __func__
, baud
);
2652 status
= send_cmd_write_baud_rate(edge_port
, baud
);
2654 /* Speed change was not possible - put back the old speed */
2655 baud
= tty_termios_baud_rate(old_termios
);
2656 tty_encode_baud_rate(tty
, baud
, baud
);
2661 /****************************************************************************
2663 * Turns a string from Unicode into ASCII.
2664 * Doesn't do a good job with any characters that are outside the normal
2665 * ASCII range, but it's only for debugging...
2666 * NOTE: expects the unicode in LE format
2667 ****************************************************************************/
2668 static void unicode_to_ascii(char *string
, int buflen
,
2669 __le16
*unicode
, int unicode_size
)
2673 if (buflen
<= 0) /* never happens, but... */
2675 --buflen
; /* space for nul */
2677 for (i
= 0; i
< unicode_size
; i
++) {
2680 string
[i
] = (char)(le16_to_cpu(unicode
[i
]));
2686 /****************************************************************************
2687 * get_manufacturing_desc
2688 * reads in the manufacturing descriptor and stores it into the serial
2690 ****************************************************************************/
2691 static void get_manufacturing_desc(struct edgeport_serial
*edge_serial
)
2693 struct device
*dev
= &edge_serial
->serial
->dev
->dev
;
2696 dev_dbg(dev
, "getting manufacturer descriptor\n");
2698 response
= rom_read(edge_serial
->serial
,
2699 (EDGE_MANUF_DESC_ADDR
& 0xffff0000) >> 16,
2700 (__u16
)(EDGE_MANUF_DESC_ADDR
& 0x0000ffff),
2701 EDGE_MANUF_DESC_LEN
,
2702 (__u8
*)(&edge_serial
->manuf_descriptor
));
2705 dev_err(dev
, "error in getting manufacturer descriptor\n");
2708 dev_dbg(dev
, "**Manufacturer Descriptor\n");
2709 dev_dbg(dev
, " RomSize: %dK\n",
2710 edge_serial
->manuf_descriptor
.RomSize
);
2711 dev_dbg(dev
, " RamSize: %dK\n",
2712 edge_serial
->manuf_descriptor
.RamSize
);
2713 dev_dbg(dev
, " CpuRev: %d\n",
2714 edge_serial
->manuf_descriptor
.CpuRev
);
2715 dev_dbg(dev
, " BoardRev: %d\n",
2716 edge_serial
->manuf_descriptor
.BoardRev
);
2717 dev_dbg(dev
, " NumPorts: %d\n",
2718 edge_serial
->manuf_descriptor
.NumPorts
);
2719 dev_dbg(dev
, " DescDate: %d/%d/%d\n",
2720 edge_serial
->manuf_descriptor
.DescDate
[0],
2721 edge_serial
->manuf_descriptor
.DescDate
[1],
2722 edge_serial
->manuf_descriptor
.DescDate
[2]+1900);
2723 unicode_to_ascii(string
, sizeof(string
),
2724 edge_serial
->manuf_descriptor
.SerialNumber
,
2725 edge_serial
->manuf_descriptor
.SerNumLength
/2);
2726 dev_dbg(dev
, " SerialNumber: %s\n", string
);
2727 unicode_to_ascii(string
, sizeof(string
),
2728 edge_serial
->manuf_descriptor
.AssemblyNumber
,
2729 edge_serial
->manuf_descriptor
.AssemblyNumLength
/2);
2730 dev_dbg(dev
, " AssemblyNumber: %s\n", string
);
2731 unicode_to_ascii(string
, sizeof(string
),
2732 edge_serial
->manuf_descriptor
.OemAssyNumber
,
2733 edge_serial
->manuf_descriptor
.OemAssyNumLength
/2);
2734 dev_dbg(dev
, " OemAssyNumber: %s\n", string
);
2735 dev_dbg(dev
, " UartType: %d\n",
2736 edge_serial
->manuf_descriptor
.UartType
);
2737 dev_dbg(dev
, " IonPid: %d\n",
2738 edge_serial
->manuf_descriptor
.IonPid
);
2739 dev_dbg(dev
, " IonConfig: %d\n",
2740 edge_serial
->manuf_descriptor
.IonConfig
);
2745 /****************************************************************************
2747 * reads in the bootloader descriptor and stores it into the serial
2749 ****************************************************************************/
2750 static void get_boot_desc(struct edgeport_serial
*edge_serial
)
2752 struct device
*dev
= &edge_serial
->serial
->dev
->dev
;
2755 dev_dbg(dev
, "getting boot descriptor\n");
2757 response
= rom_read(edge_serial
->serial
,
2758 (EDGE_BOOT_DESC_ADDR
& 0xffff0000) >> 16,
2759 (__u16
)(EDGE_BOOT_DESC_ADDR
& 0x0000ffff),
2761 (__u8
*)(&edge_serial
->boot_descriptor
));
2764 dev_err(dev
, "error in getting boot descriptor\n");
2766 dev_dbg(dev
, "**Boot Descriptor:\n");
2767 dev_dbg(dev
, " BootCodeLength: %d\n",
2768 le16_to_cpu(edge_serial
->boot_descriptor
.BootCodeLength
));
2769 dev_dbg(dev
, " MajorVersion: %d\n",
2770 edge_serial
->boot_descriptor
.MajorVersion
);
2771 dev_dbg(dev
, " MinorVersion: %d\n",
2772 edge_serial
->boot_descriptor
.MinorVersion
);
2773 dev_dbg(dev
, " BuildNumber: %d\n",
2774 le16_to_cpu(edge_serial
->boot_descriptor
.BuildNumber
));
2775 dev_dbg(dev
, " Capabilities: 0x%x\n",
2776 le16_to_cpu(edge_serial
->boot_descriptor
.Capabilities
));
2777 dev_dbg(dev
, " UConfig0: %d\n",
2778 edge_serial
->boot_descriptor
.UConfig0
);
2779 dev_dbg(dev
, " UConfig1: %d\n",
2780 edge_serial
->boot_descriptor
.UConfig1
);
2785 /****************************************************************************
2786 * load_application_firmware
2787 * This is called to load the application firmware to the device
2788 ****************************************************************************/
2789 static void load_application_firmware(struct edgeport_serial
*edge_serial
)
2791 struct device
*dev
= &edge_serial
->serial
->dev
->dev
;
2792 const struct ihex_binrec
*rec
;
2793 const struct firmware
*fw
;
2794 const char *fw_name
;
2795 const char *fw_info
;
2800 switch (edge_serial
->product_info
.iDownloadFile
) {
2801 case EDGE_DOWNLOAD_FILE_I930
:
2802 fw_info
= "downloading firmware version (930)";
2803 fw_name
= "edgeport/down.fw";
2806 case EDGE_DOWNLOAD_FILE_80251
:
2807 fw_info
= "downloading firmware version (80251)";
2808 fw_name
= "edgeport/down2.fw";
2811 case EDGE_DOWNLOAD_FILE_NONE
:
2812 dev_dbg(dev
, "No download file specified, skipping download\n");
2819 response
= request_ihex_firmware(&fw
, fw_name
,
2820 &edge_serial
->serial
->dev
->dev
);
2822 dev_err(dev
, "Failed to load image \"%s\" err %d\n",
2827 rec
= (const struct ihex_binrec
*)fw
->data
;
2828 build
= (rec
->data
[2] << 8) | rec
->data
[3];
2830 dev_dbg(dev
, "%s %d.%d.%d\n", fw_info
, rec
->data
[0], rec
->data
[1], build
);
2832 edge_serial
->product_info
.FirmwareMajorVersion
= rec
->data
[0];
2833 edge_serial
->product_info
.FirmwareMinorVersion
= rec
->data
[1];
2834 edge_serial
->product_info
.FirmwareBuildNumber
= cpu_to_le16(build
);
2836 for (rec
= ihex_next_binrec(rec
); rec
;
2837 rec
= ihex_next_binrec(rec
)) {
2838 Operaddr
= be32_to_cpu(rec
->addr
);
2839 response
= sram_write(edge_serial
->serial
,
2842 be16_to_cpu(rec
->len
),
2845 dev_err(&edge_serial
->serial
->dev
->dev
,
2846 "sram_write failed (%x, %x, %d)\n",
2847 Operaddr
>> 16, Operaddr
& 0xFFFF,
2848 be16_to_cpu(rec
->len
));
2853 dev_dbg(dev
, "sending exec_dl_code\n");
2854 response
= usb_control_msg (edge_serial
->serial
->dev
,
2855 usb_sndctrlpipe(edge_serial
->serial
->dev
, 0),
2856 USB_REQUEST_ION_EXEC_DL_CODE
,
2857 0x40, 0x4000, 0x0001, NULL
, 0, 3000);
2859 release_firmware(fw
);
2863 /****************************************************************************
2865 ****************************************************************************/
2866 static int edge_startup(struct usb_serial
*serial
)
2868 struct edgeport_serial
*edge_serial
;
2869 struct usb_device
*dev
;
2870 struct device
*ddev
= &serial
->dev
->dev
;
2873 bool interrupt_in_found
;
2875 bool bulk_out_found
;
2876 static __u32 descriptor
[3] = { EDGE_COMPATIBILITY_MASK0
,
2877 EDGE_COMPATIBILITY_MASK1
,
2878 EDGE_COMPATIBILITY_MASK2
};
2882 /* create our private serial structure */
2883 edge_serial
= kzalloc(sizeof(struct edgeport_serial
), GFP_KERNEL
);
2884 if (edge_serial
== NULL
) {
2885 dev_err(&serial
->dev
->dev
, "%s - Out of memory\n", __func__
);
2888 spin_lock_init(&edge_serial
->es_lock
);
2889 edge_serial
->serial
= serial
;
2890 usb_set_serial_data(serial
, edge_serial
);
2892 /* get the name for the device from the device */
2893 i
= usb_string(dev
, dev
->descriptor
.iManufacturer
,
2894 &edge_serial
->name
[0], MAX_NAME_LEN
+1);
2897 edge_serial
->name
[i
++] = ' ';
2898 usb_string(dev
, dev
->descriptor
.iProduct
,
2899 &edge_serial
->name
[i
], MAX_NAME_LEN
+2 - i
);
2901 dev_info(&serial
->dev
->dev
, "%s detected\n", edge_serial
->name
);
2903 /* Read the epic descriptor */
2904 if (get_epic_descriptor(edge_serial
) <= 0) {
2905 /* memcpy descriptor to Supports structures */
2906 memcpy(&edge_serial
->epic_descriptor
.Supports
, descriptor
,
2907 sizeof(struct edge_compatibility_bits
));
2909 /* get the manufacturing descriptor for this device */
2910 get_manufacturing_desc(edge_serial
);
2912 /* get the boot descriptor */
2913 get_boot_desc(edge_serial
);
2915 get_product_info(edge_serial
);
2918 /* set the number of ports from the manufacturing description */
2919 /* serial->num_ports = serial->product_info.NumPorts; */
2920 if ((!edge_serial
->is_epic
) &&
2921 (edge_serial
->product_info
.NumPorts
!= serial
->num_ports
)) {
2923 "Device Reported %d serial ports vs. core thinking we have %d ports, email greg@kroah.com this information.\n",
2924 edge_serial
->product_info
.NumPorts
,
2928 dev_dbg(ddev
, "%s - time 1 %ld\n", __func__
, jiffies
);
2930 /* If not an EPiC device */
2931 if (!edge_serial
->is_epic
) {
2932 /* now load the application firmware into this device */
2933 load_application_firmware(edge_serial
);
2935 dev_dbg(ddev
, "%s - time 2 %ld\n", __func__
, jiffies
);
2937 /* Check current Edgeport EEPROM and update if necessary */
2938 update_edgeport_E2PROM(edge_serial
);
2940 dev_dbg(ddev
, "%s - time 3 %ld\n", __func__
, jiffies
);
2942 /* set the configuration to use #1 */
2943 /* dev_dbg(ddev, "set_configuration 1\n"); */
2944 /* usb_set_configuration (dev, 1); */
2946 dev_dbg(ddev
, " FirmwareMajorVersion %d.%d.%d\n",
2947 edge_serial
->product_info
.FirmwareMajorVersion
,
2948 edge_serial
->product_info
.FirmwareMinorVersion
,
2949 le16_to_cpu(edge_serial
->product_info
.FirmwareBuildNumber
));
2951 /* we set up the pointers to the endpoints in the edge_open function,
2952 * as the structures aren't created yet. */
2956 if (edge_serial
->is_epic
) {
2957 /* EPIC thing, set up our interrupt polling now and our read
2958 * urb, so that the device knows it really is connected. */
2959 interrupt_in_found
= bulk_in_found
= bulk_out_found
= false;
2960 for (i
= 0; i
< serial
->interface
->altsetting
[0]
2961 .desc
.bNumEndpoints
; ++i
) {
2962 struct usb_endpoint_descriptor
*endpoint
;
2965 endpoint
= &serial
->interface
->altsetting
[0].
2967 buffer_size
= usb_endpoint_maxp(endpoint
);
2968 if (!interrupt_in_found
&&
2969 (usb_endpoint_is_int_in(endpoint
))) {
2970 /* we found a interrupt in endpoint */
2971 dev_dbg(ddev
, "found interrupt in\n");
2973 /* not set up yet, so do it now */
2974 edge_serial
->interrupt_read_urb
=
2975 usb_alloc_urb(0, GFP_KERNEL
);
2976 if (!edge_serial
->interrupt_read_urb
) {
2977 dev_err(ddev
, "out of memory\n");
2980 edge_serial
->interrupt_in_buffer
=
2981 kmalloc(buffer_size
, GFP_KERNEL
);
2982 if (!edge_serial
->interrupt_in_buffer
) {
2983 dev_err(ddev
, "out of memory\n");
2984 usb_free_urb(edge_serial
->interrupt_read_urb
);
2987 edge_serial
->interrupt_in_endpoint
=
2988 endpoint
->bEndpointAddress
;
2990 /* set up our interrupt urb */
2992 edge_serial
->interrupt_read_urb
,
2995 endpoint
->bEndpointAddress
),
2996 edge_serial
->interrupt_in_buffer
,
2998 edge_interrupt_callback
,
3000 endpoint
->bInterval
);
3002 interrupt_in_found
= true;
3005 if (!bulk_in_found
&&
3006 (usb_endpoint_is_bulk_in(endpoint
))) {
3007 /* we found a bulk in endpoint */
3008 dev_dbg(ddev
, "found bulk in\n");
3010 /* not set up yet, so do it now */
3011 edge_serial
->read_urb
=
3012 usb_alloc_urb(0, GFP_KERNEL
);
3013 if (!edge_serial
->read_urb
) {
3014 dev_err(ddev
, "out of memory\n");
3017 edge_serial
->bulk_in_buffer
=
3018 kmalloc(buffer_size
, GFP_KERNEL
);
3019 if (!edge_serial
->bulk_in_buffer
) {
3020 dev_err(&dev
->dev
, "out of memory\n");
3021 usb_free_urb(edge_serial
->read_urb
);
3024 edge_serial
->bulk_in_endpoint
=
3025 endpoint
->bEndpointAddress
;
3027 /* set up our bulk in urb */
3028 usb_fill_bulk_urb(edge_serial
->read_urb
, dev
,
3029 usb_rcvbulkpipe(dev
,
3030 endpoint
->bEndpointAddress
),
3031 edge_serial
->bulk_in_buffer
,
3032 usb_endpoint_maxp(endpoint
),
3033 edge_bulk_in_callback
,
3035 bulk_in_found
= true;
3038 if (!bulk_out_found
&&
3039 (usb_endpoint_is_bulk_out(endpoint
))) {
3040 /* we found a bulk out endpoint */
3041 dev_dbg(ddev
, "found bulk out\n");
3042 edge_serial
->bulk_out_endpoint
=
3043 endpoint
->bEndpointAddress
;
3044 bulk_out_found
= true;
3048 if (!interrupt_in_found
|| !bulk_in_found
|| !bulk_out_found
) {
3049 dev_err(ddev
, "Error - the proper endpoints were not found!\n");
3053 /* start interrupt read for this edgeport this interrupt will
3054 * continue as long as the edgeport is connected */
3055 response
= usb_submit_urb(edge_serial
->interrupt_read_urb
,
3058 dev_err(ddev
, "%s - Error %d submitting control urb\n",
3059 __func__
, response
);
3065 /****************************************************************************
3067 * This function is called whenever the device is removed from the usb bus.
3068 ****************************************************************************/
3069 static void edge_disconnect(struct usb_serial
*serial
)
3071 struct edgeport_serial
*edge_serial
= usb_get_serial_data(serial
);
3073 /* stop reads and writes on all ports */
3074 /* free up our endpoint stuff */
3075 if (edge_serial
->is_epic
) {
3076 usb_kill_urb(edge_serial
->interrupt_read_urb
);
3077 usb_free_urb(edge_serial
->interrupt_read_urb
);
3078 kfree(edge_serial
->interrupt_in_buffer
);
3080 usb_kill_urb(edge_serial
->read_urb
);
3081 usb_free_urb(edge_serial
->read_urb
);
3082 kfree(edge_serial
->bulk_in_buffer
);
3087 /****************************************************************************
3089 * This function is called when the device structure is deallocated.
3090 ****************************************************************************/
3091 static void edge_release(struct usb_serial
*serial
)
3093 struct edgeport_serial
*edge_serial
= usb_get_serial_data(serial
);
3098 static int edge_port_probe(struct usb_serial_port
*port
)
3100 struct edgeport_port
*edge_port
;
3102 edge_port
= kzalloc(sizeof(*edge_port
), GFP_KERNEL
);
3106 spin_lock_init(&edge_port
->ep_lock
);
3107 edge_port
->port
= port
;
3109 usb_set_serial_port_data(port
, edge_port
);
3114 static int edge_port_remove(struct usb_serial_port
*port
)
3116 struct edgeport_port
*edge_port
;
3118 edge_port
= usb_get_serial_port_data(port
);
3124 module_usb_serial_driver(serial_drivers
, id_table_combined
);
3126 MODULE_AUTHOR(DRIVER_AUTHOR
);
3127 MODULE_DESCRIPTION(DRIVER_DESC
);
3128 MODULE_LICENSE("GPL");
3129 MODULE_FIRMWARE("edgeport/boot.fw");
3130 MODULE_FIRMWARE("edgeport/boot2.fw");
3131 MODULE_FIRMWARE("edgeport/down.fw");
3132 MODULE_FIRMWARE("edgeport/down2.fw");