]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/usb/serial/io_edgeport.c
TTY: add tty_port_tty_wakeup helper
[mirror_ubuntu-bionic-kernel.git] / drivers / usb / serial / io_edgeport.c
CommitLineData
1da177e4
LT
1/*
2 * Edgeport USB Serial Converter driver
3 *
4 * Copyright (C) 2000 Inside Out Networks, All rights reserved.
5 * Copyright (C) 2001-2002 Greg Kroah-Hartman <greg@kroah.com>
6 *
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.
11 *
12 * Supports the following devices:
13 * Edgeport/4
14 * Edgeport/4t
15 * Edgeport/2
16 * Edgeport/4i
17 * Edgeport/2i
18 * Edgeport/421
19 * Edgeport/21
20 * Rapidport/4
21 * Edgeport/8
22 * Edgeport/2D8
23 * Edgeport/4D8
24 * Edgeport/8i
25 *
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>.
29 *
1da177e4
LT
30 */
31
1da177e4
LT
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>
5b9ea932
JS
45#include <linux/firmware.h>
46#include <linux/ihex.h>
03f0dbf7 47#include <linux/uaccess.h>
1da177e4 48#include <linux/usb.h>
a969888c 49#include <linux/usb/serial.h>
1da177e4
LT
50#include "io_edgeport.h"
51#include "io_ionsp.h" /* info for the iosp messages */
52#include "io_16654.h" /* 16654 UART defines */
53
1da177e4
LT
54#define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com> and David Iacovelli"
55#define DRIVER_DESC "Edgeport USB Serial Driver"
56
1da177e4
LT
57#define MAX_NAME_LEN 64
58
59#define CHASE_TIMEOUT (5*HZ) /* 5 seconds */
60#define OPEN_TIMEOUT (5*HZ) /* 5 seconds */
61#define COMMAND_TIMEOUT (5*HZ) /* 5 seconds */
62
63/* receive port state */
64enum RXSTATE {
03f0dbf7
AC
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) */
1da177e4
LT
69};
70
71
03f0dbf7
AC
72/* Transmit Fifo
73 * This Transmit queue is an extension of the edgeport Rx buffer.
74 * The maximum amount of data buffered in both the edgeport
1da177e4
LT
75 * Rx buffer (maxTxCredits) and this buffer will never exceed maxTxCredits.
76 */
77struct TxFifo {
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 */
83};
84
85/* This structure holds all of the local port information */
86struct edgeport_port {
87 __u16 txCredits; /* our current credits for this port */
88 __u16 maxTxCredits; /* the max size of the port */
89
90 struct TxFifo txfifo; /* transmit fifo -- size will be maxTxCredits */
91 struct urb *write_urb; /* write URB for this port */
cb8eaa8b 92 bool write_in_progress; /* 'true' while a write URB is outstanding */
1da177e4
LT
93 spinlock_t ep_lock;
94
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 */
101 __u8 validDataMask;
102 __u32 baudRate;
103
cb8eaa8b
RK
104 bool open;
105 bool openPending;
106 bool commandPending;
107 bool closePending;
108 bool chaseResponsePending;
1da177e4
LT
109
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 */
113 wait_queue_head_t delta_msr_wait; /* for handling sleeping while waiting for msr change to happen */
114
115 struct async_icount icount;
116 struct usb_serial_port *port; /* loop back to the owner of this object */
117};
118
119
120/* This structure holds all of the individual device information */
121struct edgeport_serial {
ad93375a 122 char name[MAX_NAME_LEN+2]; /* string name of this device */
1da177e4
LT
123
124 struct edge_manuf_descriptor manuf_descriptor; /* the manufacturer descriptor */
125 struct edge_boot_descriptor boot_descriptor; /* the boot firmware descriptor */
126 struct edgeport_product_info product_info; /* Product Info */
6e8cf775
GKH
127 struct edge_compatibility_descriptor epic_descriptor; /* Edgeport compatible descriptor */
128 int is_epic; /* flag if EPiC device or not */
1da177e4
LT
129
130 __u8 interrupt_in_endpoint; /* the interrupt endpoint handle */
03f0dbf7
AC
131 unsigned char *interrupt_in_buffer; /* the buffer we use for the interrupt endpoint */
132 struct urb *interrupt_read_urb; /* our interrupt urb */
1da177e4
LT
133
134 __u8 bulk_in_endpoint; /* the bulk in endpoint handle */
03f0dbf7
AC
135 unsigned char *bulk_in_buffer; /* the buffer we use for the bulk in endpoint */
136 struct urb *read_urb; /* our bulk read urb */
cb8eaa8b 137 bool read_in_progress;
1da177e4
LT
138 spinlock_t es_lock;
139
140 __u8 bulk_out_endpoint; /* the bulk out endpoint handle */
141
142 __s16 rxBytesAvail; /* the number of bytes that we need to read from this device */
143
144 enum RXSTATE rxState; /* the current state of the bulk receive processor */
145 __u8 rxHeader1; /* receive header byte 1 */
146 __u8 rxHeader2; /* receive header byte 2 */
147 __u8 rxHeader3; /* receive header byte 3 */
148 __u8 rxPort; /* the port that we are currently receiving data for */
149 __u8 rxStatusCode; /* the receive status code */
150 __u8 rxStatusParam; /* the receive status paramater */
151 __s16 rxBytesRemaining; /* the number of port bytes left to read */
152 struct usb_serial *serial; /* loop back to the owner of this object */
153};
154
155/* baud rate information */
156struct divisor_table_entry {
157 __u32 BaudRate;
158 __u16 Divisor;
159};
160
03f0dbf7
AC
161/*
162 * Define table of divisors for Rev A EdgePort/4 hardware
163 * These assume a 3.6864MHz crystal, the standard /16, and
164 * MCR.7 = 0.
165 */
166
4c4c9432 167static const struct divisor_table_entry divisor_table[] = {
03f0dbf7
AC
168 { 50, 4608},
169 { 75, 3072},
170 { 110, 2095}, /* 2094.545455 => 230450 => .0217 % over */
171 { 134, 1713}, /* 1713.011152 => 230398.5 => .00065% under */
1da177e4
LT
172 { 150, 1536},
173 { 300, 768},
174 { 600, 384},
175 { 1200, 192},
176 { 1800, 128},
177 { 2400, 96},
178 { 4800, 48},
179 { 7200, 32},
180 { 9600, 24},
181 { 14400, 16},
182 { 19200, 12},
183 { 38400, 6},
184 { 57600, 4},
185 { 115200, 2},
186 { 230400, 1},
187};
188
36ccce49
GKH
189/* Number of outstanding Command Write Urbs */
190static atomic_t CmdUrbs = ATOMIC_INIT(0);
1da177e4
LT
191
192
193/* local function prototypes */
194
195/* function prototypes for all URB callbacks */
03f0dbf7
AC
196static void edge_interrupt_callback(struct urb *urb);
197static void edge_bulk_in_callback(struct urb *urb);
198static void edge_bulk_out_data_callback(struct urb *urb);
199static void edge_bulk_out_cmd_callback(struct urb *urb);
1da177e4
LT
200
201/* function prototypes for the usbserial callbacks */
a509a7e4 202static int edge_open(struct tty_struct *tty, struct usb_serial_port *port);
335f8514 203static void edge_close(struct usb_serial_port *port);
03f0dbf7
AC
204static int edge_write(struct tty_struct *tty, struct usb_serial_port *port,
205 const unsigned char *buf, int count);
206static int edge_write_room(struct tty_struct *tty);
207static int edge_chars_in_buffer(struct tty_struct *tty);
208static void edge_throttle(struct tty_struct *tty);
209static void edge_unthrottle(struct tty_struct *tty);
210static void edge_set_termios(struct tty_struct *tty,
211 struct usb_serial_port *port,
212 struct ktermios *old_termios);
00a0d0d6 213static int edge_ioctl(struct tty_struct *tty,
03f0dbf7
AC
214 unsigned int cmd, unsigned long arg);
215static void edge_break(struct tty_struct *tty, int break_state);
60b33c13 216static int edge_tiocmget(struct tty_struct *tty);
20b9d177 217static int edge_tiocmset(struct tty_struct *tty,
03f0dbf7 218 unsigned int set, unsigned int clear);
0bca1b91
AC
219static int edge_get_icount(struct tty_struct *tty,
220 struct serial_icounter_struct *icount);
03f0dbf7 221static int edge_startup(struct usb_serial *serial);
f9c99bb8
AS
222static void edge_disconnect(struct usb_serial *serial);
223static void edge_release(struct usb_serial *serial);
c27f3efc
JH
224static int edge_port_probe(struct usb_serial_port *port);
225static int edge_port_remove(struct usb_serial_port *port);
1da177e4
LT
226
227#include "io_tables.h" /* all of the devices that this driver supports */
228
1da177e4 229/* function prototypes for all of our local functions */
03f0dbf7
AC
230
231static void process_rcvd_data(struct edgeport_serial *edge_serial,
232 unsigned char *buffer, __u16 bufferLength);
233static void process_rcvd_status(struct edgeport_serial *edge_serial,
234 __u8 byte2, __u8 byte3);
2e124b4a
JS
235static void edge_tty_recv(struct usb_serial_port *port, unsigned char *data,
236 int length);
03f0dbf7
AC
237static void handle_new_msr(struct edgeport_port *edge_port, __u8 newMsr);
238static void handle_new_lsr(struct edgeport_port *edge_port, __u8 lsrData,
239 __u8 lsr, __u8 data);
240static int send_iosp_ext_cmd(struct edgeport_port *edge_port, __u8 command,
241 __u8 param);
984f6868 242static int calc_baud_rate_divisor(struct device *dev, int baud_rate, int *divisor);
03f0dbf7
AC
243static int send_cmd_write_baud_rate(struct edgeport_port *edge_port,
244 int baudRate);
245static void change_port_settings(struct tty_struct *tty,
246 struct edgeport_port *edge_port,
247 struct ktermios *old_termios);
248static int send_cmd_write_uart_register(struct edgeport_port *edge_port,
249 __u8 regNum, __u8 regValue);
250static int write_cmd_usb(struct edgeport_port *edge_port,
251 unsigned char *buffer, int writeLength);
252static void send_more_port_data(struct edgeport_serial *edge_serial,
253 struct edgeport_port *edge_port);
254
255static int sram_write(struct usb_serial *serial, __u16 extAddr, __u16 addr,
256 __u16 length, const __u8 *data);
257static int rom_read(struct usb_serial *serial, __u16 extAddr, __u16 addr,
258 __u16 length, __u8 *data);
259static int rom_write(struct usb_serial *serial, __u16 extAddr, __u16 addr,
260 __u16 length, const __u8 *data);
261static void get_manufacturing_desc(struct edgeport_serial *edge_serial);
262static void get_boot_desc(struct edgeport_serial *edge_serial);
263static void load_application_firmware(struct edgeport_serial *edge_serial);
264
265static void unicode_to_ascii(char *string, int buflen,
266 __le16 *unicode, int unicode_size);
267
268
269/* ************************************************************************ */
270/* ************************************************************************ */
271/* ************************************************************************ */
272/* ************************************************************************ */
1da177e4
LT
273
274/************************************************************************
275 * *
276 * update_edgeport_E2PROM() Compare current versions of *
277 * Boot ROM and Manufacture *
278 * Descriptors with versions *
279 * embedded in this driver *
280 * *
281 ************************************************************************/
03f0dbf7 282static void update_edgeport_E2PROM(struct edgeport_serial *edge_serial)
1da177e4 283{
984f6868 284 struct device *dev = &edge_serial->serial->dev->dev;
1da177e4
LT
285 __u32 BootCurVer;
286 __u32 BootNewVer;
5b9ea932
JS
287 __u8 BootMajorVersion;
288 __u8 BootMinorVersion;
289 __u16 BootBuildNumber;
290 __u32 Bootaddr;
291 const struct ihex_binrec *rec;
292 const struct firmware *fw;
293 const char *fw_name;
1da177e4
LT
294 int response;
295
1da177e4 296 switch (edge_serial->product_info.iDownloadFile) {
03f0dbf7
AC
297 case EDGE_DOWNLOAD_FILE_I930:
298 fw_name = "edgeport/boot.fw";
299 break;
300 case EDGE_DOWNLOAD_FILE_80251:
301 fw_name = "edgeport/boot2.fw";
302 break;
303 default:
304 return;
1da177e4
LT
305 }
306
5b9ea932
JS
307 response = request_ihex_firmware(&fw, fw_name,
308 &edge_serial->serial->dev->dev);
309 if (response) {
984f6868 310 dev_err(dev, "Failed to load image \"%s\" err %d\n",
5b9ea932
JS
311 fw_name, response);
312 return;
313 }
314
315 rec = (const struct ihex_binrec *)fw->data;
316 BootMajorVersion = rec->data[0];
317 BootMinorVersion = rec->data[1];
318 BootBuildNumber = (rec->data[2] << 8) | rec->data[3];
319
03f0dbf7 320 /* Check Boot Image Version */
1da177e4
LT
321 BootCurVer = (edge_serial->boot_descriptor.MajorVersion << 24) +
322 (edge_serial->boot_descriptor.MinorVersion << 16) +
323 le16_to_cpu(edge_serial->boot_descriptor.BuildNumber);
324
325 BootNewVer = (BootMajorVersion << 24) +
326 (BootMinorVersion << 16) +
5b9ea932 327 BootBuildNumber;
1da177e4 328
984f6868 329 dev_dbg(dev, "Current Boot Image version %d.%d.%d\n",
1da177e4
LT
330 edge_serial->boot_descriptor.MajorVersion,
331 edge_serial->boot_descriptor.MinorVersion,
332 le16_to_cpu(edge_serial->boot_descriptor.BuildNumber));
333
334
335 if (BootNewVer > BootCurVer) {
984f6868 336 dev_dbg(dev, "**Update Boot Image from %d.%d.%d to %d.%d.%d\n",
1da177e4
LT
337 edge_serial->boot_descriptor.MajorVersion,
338 edge_serial->boot_descriptor.MinorVersion,
339 le16_to_cpu(edge_serial->boot_descriptor.BuildNumber),
5b9ea932 340 BootMajorVersion, BootMinorVersion, BootBuildNumber);
1da177e4 341
984f6868 342 dev_dbg(dev, "Downloading new Boot Image\n");
1da177e4 343
5b9ea932
JS
344 for (rec = ihex_next_binrec(rec); rec;
345 rec = ihex_next_binrec(rec)) {
346 Bootaddr = be32_to_cpu(rec->addr);
347 response = rom_write(edge_serial->serial,
348 Bootaddr >> 16,
349 Bootaddr & 0xFFFF,
350 be16_to_cpu(rec->len),
351 &rec->data[0]);
1da177e4 352 if (response < 0) {
5b9ea932
JS
353 dev_err(&edge_serial->serial->dev->dev,
354 "rom_write failed (%x, %x, %d)\n",
355 Bootaddr >> 16, Bootaddr & 0xFFFF,
356 be16_to_cpu(rec->len));
1da177e4
LT
357 break;
358 }
359 }
360 } else {
984f6868 361 dev_dbg(dev, "Boot Image -- already up to date\n");
1da177e4 362 }
5b9ea932 363 release_firmware(fw);
1da177e4
LT
364}
365
1da177e4
LT
366#if 0
367/************************************************************************
368 *
369 * Get string descriptor from device
370 *
371 ************************************************************************/
03f0dbf7
AC
372static int get_string_desc(struct usb_device *dev, int Id,
373 struct usb_string_descriptor **pRetDesc)
1da177e4
LT
374{
375 struct usb_string_descriptor StringDesc;
376 struct usb_string_descriptor *pStringDesc;
377
984f6868 378 dev_dbg(&dev->dev, "%s - USB String ID = %d\n", __func__, Id);
1da177e4 379
03f0dbf7
AC
380 if (!usb_get_descriptor(dev, USB_DT_STRING, Id, &StringDesc,
381 sizeof(StringDesc)))
1da177e4 382 return 0;
1da177e4 383
03f0dbf7
AC
384 pStringDesc = kmalloc(StringDesc.bLength, GFP_KERNEL);
385 if (!pStringDesc)
1da177e4 386 return -1;
1da177e4 387
03f0dbf7
AC
388 if (!usb_get_descriptor(dev, USB_DT_STRING, Id, pStringDesc,
389 StringDesc.bLength)) {
1da177e4
LT
390 kfree(pStringDesc);
391 return -1;
392 }
393
394 *pRetDesc = pStringDesc;
395 return 0;
396}
397#endif
398
984f6868
GKH
399static void dump_product_info(struct edgeport_serial *edge_serial,
400 struct edgeport_product_info *product_info)
6e8cf775 401{
984f6868
GKH
402 struct device *dev = &edge_serial->serial->dev->dev;
403
03f0dbf7 404 /* Dump Product Info structure */
984f6868
GKH
405 dev_dbg(dev, "**Product Information:\n");
406 dev_dbg(dev, " ProductId %x\n", product_info->ProductId);
407 dev_dbg(dev, " NumPorts %d\n", product_info->NumPorts);
408 dev_dbg(dev, " ProdInfoVer %d\n", product_info->ProdInfoVer);
409 dev_dbg(dev, " IsServer %d\n", product_info->IsServer);
410 dev_dbg(dev, " IsRS232 %d\n", product_info->IsRS232);
411 dev_dbg(dev, " IsRS422 %d\n", product_info->IsRS422);
412 dev_dbg(dev, " IsRS485 %d\n", product_info->IsRS485);
413 dev_dbg(dev, " RomSize %d\n", product_info->RomSize);
414 dev_dbg(dev, " RamSize %d\n", product_info->RamSize);
415 dev_dbg(dev, " CpuRev %x\n", product_info->CpuRev);
416 dev_dbg(dev, " BoardRev %x\n", product_info->BoardRev);
417 dev_dbg(dev, " BootMajorVersion %d.%d.%d\n",
418 product_info->BootMajorVersion,
419 product_info->BootMinorVersion,
420 le16_to_cpu(product_info->BootBuildNumber));
421 dev_dbg(dev, " FirmwareMajorVersion %d.%d.%d\n",
422 product_info->FirmwareMajorVersion,
423 product_info->FirmwareMinorVersion,
424 le16_to_cpu(product_info->FirmwareBuildNumber));
425 dev_dbg(dev, " ManufactureDescDate %d/%d/%d\n",
426 product_info->ManufactureDescDate[0],
427 product_info->ManufactureDescDate[1],
428 product_info->ManufactureDescDate[2]+1900);
429 dev_dbg(dev, " iDownloadFile 0x%x\n",
430 product_info->iDownloadFile);
431 dev_dbg(dev, " EpicVer %d\n", product_info->EpicVer);
6e8cf775
GKH
432}
433
1da177e4
LT
434static void get_product_info(struct edgeport_serial *edge_serial)
435{
436 struct edgeport_product_info *product_info = &edge_serial->product_info;
437
03f0dbf7
AC
438 memset(product_info, 0, sizeof(struct edgeport_product_info));
439
440 product_info->ProductId = (__u16)(le16_to_cpu(edge_serial->serial->dev->descriptor.idProduct) & ~ION_DEVICE_ID_80251_NETCHIP);
441 product_info->NumPorts = edge_serial->manuf_descriptor.NumPorts;
442 product_info->ProdInfoVer = 0;
443
444 product_info->RomSize = edge_serial->manuf_descriptor.RomSize;
445 product_info->RamSize = edge_serial->manuf_descriptor.RamSize;
446 product_info->CpuRev = edge_serial->manuf_descriptor.CpuRev;
447 product_info->BoardRev = edge_serial->manuf_descriptor.BoardRev;
448
449 product_info->BootMajorVersion =
450 edge_serial->boot_descriptor.MajorVersion;
451 product_info->BootMinorVersion =
452 edge_serial->boot_descriptor.MinorVersion;
453 product_info->BootBuildNumber =
454 edge_serial->boot_descriptor.BuildNumber;
455
456 memcpy(product_info->ManufactureDescDate,
457 edge_serial->manuf_descriptor.DescDate,
458 sizeof(edge_serial->manuf_descriptor.DescDate));
459
460 /* check if this is 2nd generation hardware */
461 if (le16_to_cpu(edge_serial->serial->dev->descriptor.idProduct)
462 & ION_DEVICE_ID_80251_NETCHIP)
463 product_info->iDownloadFile = EDGE_DOWNLOAD_FILE_80251;
464 else
465 product_info->iDownloadFile = EDGE_DOWNLOAD_FILE_I930;
984f6868 466
03f0dbf7 467 /* Determine Product type and set appropriate flags */
1da177e4 468 switch (DEVICE_ID_FROM_USB_PRODUCT_ID(product_info->ProductId)) {
03f0dbf7
AC
469 case ION_DEVICE_ID_EDGEPORT_COMPATIBLE:
470 case ION_DEVICE_ID_EDGEPORT_4T:
471 case ION_DEVICE_ID_EDGEPORT_4:
472 case ION_DEVICE_ID_EDGEPORT_2:
473 case ION_DEVICE_ID_EDGEPORT_8_DUAL_CPU:
474 case ION_DEVICE_ID_EDGEPORT_8:
475 case ION_DEVICE_ID_EDGEPORT_421:
476 case ION_DEVICE_ID_EDGEPORT_21:
477 case ION_DEVICE_ID_EDGEPORT_2_DIN:
478 case ION_DEVICE_ID_EDGEPORT_4_DIN:
479 case ION_DEVICE_ID_EDGEPORT_16_DUAL_CPU:
480 product_info->IsRS232 = 1;
481 break;
1da177e4 482
03f0dbf7
AC
483 case ION_DEVICE_ID_EDGEPORT_2I: /* Edgeport/2 RS422/RS485 */
484 product_info->IsRS422 = 1;
485 product_info->IsRS485 = 1;
486 break;
1da177e4 487
03f0dbf7
AC
488 case ION_DEVICE_ID_EDGEPORT_8I: /* Edgeport/4 RS422 */
489 case ION_DEVICE_ID_EDGEPORT_4I: /* Edgeport/4 RS422 */
490 product_info->IsRS422 = 1;
491 break;
1da177e4
LT
492 }
493
984f6868 494 dump_product_info(edge_serial, product_info);
6e8cf775 495}
1da177e4 496
6e8cf775
GKH
497static int get_epic_descriptor(struct edgeport_serial *ep)
498{
499 int result;
500 struct usb_serial *serial = ep->serial;
501 struct edgeport_product_info *product_info = &ep->product_info;
502 struct edge_compatibility_descriptor *epic = &ep->epic_descriptor;
503 struct edge_compatibility_bits *bits;
984f6868 504 struct device *dev = &serial->dev->dev;
6e8cf775
GKH
505
506 ep->is_epic = 0;
507 result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
508 USB_REQUEST_ION_GET_EPIC_DESC,
509 0xC0, 0x00, 0x00,
510 &ep->epic_descriptor,
511 sizeof(struct edge_compatibility_descriptor),
512 300);
513
6e8cf775
GKH
514 if (result > 0) {
515 ep->is_epic = 1;
516 memset(product_info, 0, sizeof(struct edgeport_product_info));
517
03f0dbf7
AC
518 product_info->NumPorts = epic->NumPorts;
519 product_info->ProdInfoVer = 0;
520 product_info->FirmwareMajorVersion = epic->MajorVersion;
521 product_info->FirmwareMinorVersion = epic->MinorVersion;
522 product_info->FirmwareBuildNumber = epic->BuildNumber;
523 product_info->iDownloadFile = epic->iDownloadFile;
524 product_info->EpicVer = epic->EpicVer;
525 product_info->Epic = epic->Supports;
526 product_info->ProductId = ION_DEVICE_ID_EDGEPORT_COMPATIBLE;
984f6868 527 dump_product_info(ep, product_info);
6e8cf775
GKH
528
529 bits = &ep->epic_descriptor.Supports;
984f6868
GKH
530 dev_dbg(dev, "**EPIC descriptor:\n");
531 dev_dbg(dev, " VendEnableSuspend: %s\n", bits->VendEnableSuspend ? "TRUE": "FALSE");
532 dev_dbg(dev, " IOSPOpen : %s\n", bits->IOSPOpen ? "TRUE": "FALSE");
533 dev_dbg(dev, " IOSPClose : %s\n", bits->IOSPClose ? "TRUE": "FALSE");
534 dev_dbg(dev, " IOSPChase : %s\n", bits->IOSPChase ? "TRUE": "FALSE");
535 dev_dbg(dev, " IOSPSetRxFlow : %s\n", bits->IOSPSetRxFlow ? "TRUE": "FALSE");
536 dev_dbg(dev, " IOSPSetTxFlow : %s\n", bits->IOSPSetTxFlow ? "TRUE": "FALSE");
537 dev_dbg(dev, " IOSPSetXChar : %s\n", bits->IOSPSetXChar ? "TRUE": "FALSE");
538 dev_dbg(dev, " IOSPRxCheck : %s\n", bits->IOSPRxCheck ? "TRUE": "FALSE");
539 dev_dbg(dev, " IOSPSetClrBreak : %s\n", bits->IOSPSetClrBreak ? "TRUE": "FALSE");
540 dev_dbg(dev, " IOSPWriteMCR : %s\n", bits->IOSPWriteMCR ? "TRUE": "FALSE");
541 dev_dbg(dev, " IOSPWriteLCR : %s\n", bits->IOSPWriteLCR ? "TRUE": "FALSE");
542 dev_dbg(dev, " IOSPSetBaudRate : %s\n", bits->IOSPSetBaudRate ? "TRUE": "FALSE");
543 dev_dbg(dev, " TrueEdgeport : %s\n", bits->TrueEdgeport ? "TRUE": "FALSE");
6e8cf775
GKH
544 }
545
546 return result;
1da177e4
LT
547}
548
549
550/************************************************************************/
551/************************************************************************/
552/* U S B C A L L B A C K F U N C T I O N S */
553/* U S B C A L L B A C K F U N C T I O N S */
554/************************************************************************/
555/************************************************************************/
556
557/*****************************************************************************
558 * edge_interrupt_callback
03f0dbf7 559 * this is the callback function for when we have received data on the
1da177e4
LT
560 * interrupt endpoint.
561 *****************************************************************************/
03f0dbf7 562static void edge_interrupt_callback(struct urb *urb)
1da177e4 563{
984f6868
GKH
564 struct edgeport_serial *edge_serial = urb->context;
565 struct device *dev;
1da177e4
LT
566 struct edgeport_port *edge_port;
567 struct usb_serial_port *port;
568 unsigned char *data = urb->transfer_buffer;
569 int length = urb->actual_length;
570 int bytes_avail;
571 int position;
572 int txCredits;
573 int portNumber;
574 int result;
2a393f5f 575 int status = urb->status;
1da177e4 576
2a393f5f 577 switch (status) {
1da177e4
LT
578 case 0:
579 /* success */
580 break;
581 case -ECONNRESET:
582 case -ENOENT:
583 case -ESHUTDOWN:
584 /* this urb is terminated, clean up */
984f6868 585 dev_dbg(&urb->dev->dev, "%s - urb shutting down with status: %d\n", __func__, status);
1da177e4
LT
586 return;
587 default:
984f6868 588 dev_dbg(&urb->dev->dev, "%s - nonzero urb status received: %d\n", __func__, status);
1da177e4
LT
589 goto exit;
590 }
591
984f6868
GKH
592 dev = &edge_serial->serial->dev->dev;
593
03f0dbf7 594 /* process this interrupt-read even if there are no ports open */
1da177e4 595 if (length) {
59d33f2f 596 usb_serial_debug_data(dev, __func__, length, data);
1da177e4
LT
597
598 if (length > 1) {
599 bytes_avail = data[0] | (data[1] << 8);
600 if (bytes_avail) {
601 spin_lock(&edge_serial->es_lock);
602 edge_serial->rxBytesAvail += bytes_avail;
984f6868
GKH
603 dev_dbg(dev,
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);
1da177e4
LT
608
609 if (edge_serial->rxBytesAvail > 0 &&
610 !edge_serial->read_in_progress) {
984f6868 611 dev_dbg(dev, "%s - posting a read\n", __func__);
cb8eaa8b 612 edge_serial->read_in_progress = true;
1da177e4 613
03f0dbf7
AC
614 /* we have pending bytes on the
615 bulk in pipe, send a request */
1da177e4
LT
616 result = usb_submit_urb(edge_serial->read_urb, GFP_ATOMIC);
617 if (result) {
984f6868
GKH
618 dev_err(dev,
619 "%s - usb_submit_urb(read bulk) failed with result = %d\n",
620 __func__, result);
cb8eaa8b 621 edge_serial->read_in_progress = false;
1da177e4
LT
622 }
623 }
624 spin_unlock(&edge_serial->es_lock);
625 }
626 }
627 /* grab the txcredits for the ports if available */
628 position = 2;
629 portNumber = 0;
03f0dbf7
AC
630 while ((position < length) &&
631 (portNumber < edge_serial->serial->num_ports)) {
1da177e4
LT
632 txCredits = data[position] | (data[position+1] << 8);
633 if (txCredits) {
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);
984f6868
GKH
640 dev_dbg(dev, "%s - txcredits for port%d = %d\n",
641 __func__, portNumber,
642 edge_port->txCredits);
1da177e4 643
03f0dbf7
AC
644 /* tell the tty driver that something
645 has changed */
6aad04f2 646 tty_port_tty_wakeup(&edge_port->port->port);
03f0dbf7
AC
647 /* Since we have more credit, check
648 if more data can be sent */
649 send_more_port_data(edge_serial,
650 edge_port);
1da177e4
LT
651 }
652 }
653 position += 2;
654 ++portNumber;
655 }
656 }
657
658exit:
03f0dbf7
AC
659 result = usb_submit_urb(urb, GFP_ATOMIC);
660 if (result)
661 dev_err(&urb->dev->dev,
662 "%s - Error %d submitting control urb\n",
663 __func__, result);
1da177e4
LT
664}
665
666
667/*****************************************************************************
668 * edge_bulk_in_callback
03f0dbf7 669 * this is the callback function for when we have received data on the
1da177e4
LT
670 * bulk in endpoint.
671 *****************************************************************************/
03f0dbf7 672static void edge_bulk_in_callback(struct urb *urb)
1da177e4 673{
cdc97792 674 struct edgeport_serial *edge_serial = urb->context;
984f6868 675 struct device *dev;
1da177e4 676 unsigned char *data = urb->transfer_buffer;
2a393f5f 677 int retval;
1da177e4 678 __u16 raw_data_length;
2a393f5f 679 int status = urb->status;
1da177e4 680
2a393f5f 681 if (status) {
984f6868
GKH
682 dev_dbg(&urb->dev->dev, "%s - nonzero read bulk status received: %d\n",
683 __func__, status);
cb8eaa8b 684 edge_serial->read_in_progress = false;
1da177e4
LT
685 return;
686 }
687
688 if (urb->actual_length == 0) {
984f6868 689 dev_dbg(&urb->dev->dev, "%s - read bulk callback with no data\n", __func__);
cb8eaa8b 690 edge_serial->read_in_progress = false;
1da177e4
LT
691 return;
692 }
693
984f6868 694 dev = &edge_serial->serial->dev->dev;
1da177e4
LT
695 raw_data_length = urb->actual_length;
696
59d33f2f 697 usb_serial_debug_data(dev, __func__, raw_data_length, data);
1da177e4
LT
698
699 spin_lock(&edge_serial->es_lock);
700
701 /* decrement our rxBytes available by the number that we just got */
702 edge_serial->rxBytesAvail -= raw_data_length;
703
984f6868
GKH
704 dev_dbg(dev, "%s - Received = %d, rxBytesAvail %d\n", __func__,
705 raw_data_length, edge_serial->rxBytesAvail);
1da177e4 706
03f0dbf7 707 process_rcvd_data(edge_serial, data, urb->actual_length);
1da177e4
LT
708
709 /* check to see if there's any more data for us to read */
710 if (edge_serial->rxBytesAvail > 0) {
984f6868 711 dev_dbg(dev, "%s - posting a read\n", __func__);
2a393f5f
GKH
712 retval = usb_submit_urb(edge_serial->read_urb, GFP_ATOMIC);
713 if (retval) {
984f6868
GKH
714 dev_err(dev,
715 "%s - usb_submit_urb(read bulk) failed, retval = %d\n",
716 __func__, retval);
cb8eaa8b 717 edge_serial->read_in_progress = false;
1da177e4
LT
718 }
719 } else {
cb8eaa8b 720 edge_serial->read_in_progress = false;
1da177e4
LT
721 }
722
723 spin_unlock(&edge_serial->es_lock);
724}
725
726
727/*****************************************************************************
728 * edge_bulk_out_data_callback
03f0dbf7
AC
729 * this is the callback function for when we have finished sending
730 * serial data on the bulk out endpoint.
1da177e4 731 *****************************************************************************/
03f0dbf7 732static void edge_bulk_out_data_callback(struct urb *urb)
1da177e4 733{
cdc97792 734 struct edgeport_port *edge_port = urb->context;
2a393f5f 735 int status = urb->status;
1da177e4 736
2a393f5f 737 if (status) {
984f6868
GKH
738 dev_dbg(&urb->dev->dev,
739 "%s - nonzero write bulk status received: %d\n",
740 __func__, status);
1da177e4
LT
741 }
742
6aad04f2
JS
743 if (edge_port->open)
744 tty_port_tty_wakeup(&edge_port->port->port);
1da177e4 745
03f0dbf7 746 /* Release the Write URB */
cb8eaa8b 747 edge_port->write_in_progress = false;
1da177e4 748
03f0dbf7
AC
749 /* Check if more data needs to be sent */
750 send_more_port_data((struct edgeport_serial *)
751 (usb_get_serial_data(edge_port->port->serial)), edge_port);
1da177e4
LT
752}
753
754
755/*****************************************************************************
756 * BulkOutCmdCallback
03f0dbf7
AC
757 * this is the callback function for when we have finished sending a
758 * command on the bulk out endpoint.
1da177e4 759 *****************************************************************************/
03f0dbf7 760static void edge_bulk_out_cmd_callback(struct urb *urb)
1da177e4 761{
cdc97792 762 struct edgeport_port *edge_port = urb->context;
1da177e4
LT
763 int status = urb->status;
764
96c706ed 765 atomic_dec(&CmdUrbs);
984f6868
GKH
766 dev_dbg(&urb->dev->dev, "%s - FREE URB %p (outstanding %d)\n",
767 __func__, urb, atomic_read(&CmdUrbs));
1da177e4
LT
768
769
770 /* clean up the transfer buffer */
1bc3c9e1 771 kfree(urb->transfer_buffer);
1da177e4
LT
772
773 /* Free the command urb */
03f0dbf7 774 usb_free_urb(urb);
1da177e4
LT
775
776 if (status) {
984f6868
GKH
777 dev_dbg(&urb->dev->dev,
778 "%s - nonzero write bulk status received: %d\n",
779 __func__, status);
1da177e4
LT
780 return;
781 }
782
1da177e4 783 /* tell the tty driver that something has changed */
6aad04f2
JS
784 if (edge_port->open)
785 tty_port_tty_wakeup(&edge_port->port->port);
1da177e4
LT
786
787 /* we have completed the command */
cb8eaa8b 788 edge_port->commandPending = false;
1da177e4
LT
789 wake_up(&edge_port->wait_command);
790}
791
792
793/*****************************************************************************
794 * Driver tty interface functions
795 *****************************************************************************/
796
797/*****************************************************************************
798 * SerialOpen
799 * this function is called by the tty driver when a port is opened
800 * If successful, we return 0
801 * Otherwise we return a negative error number.
802 *****************************************************************************/
a509a7e4 803static int edge_open(struct tty_struct *tty, struct usb_serial_port *port)
1da177e4
LT
804{
805 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
984f6868 806 struct device *dev = &port->dev;
1da177e4
LT
807 struct usb_serial *serial;
808 struct edgeport_serial *edge_serial;
809 int response;
810
1da177e4
LT
811 if (edge_port == NULL)
812 return -ENODEV;
813
03f0dbf7
AC
814 /* see if we've set up our endpoint info yet (can't set it up
815 in edge_startup as the structures were not set up at that time.) */
1da177e4
LT
816 serial = port->serial;
817 edge_serial = usb_get_serial_data(serial);
95da310e 818 if (edge_serial == NULL)
1da177e4 819 return -ENODEV;
1da177e4
LT
820 if (edge_serial->interrupt_in_buffer == NULL) {
821 struct usb_serial_port *port0 = serial->port[0];
03f0dbf7 822
1da177e4 823 /* not set up yet, so do it now */
03f0dbf7
AC
824 edge_serial->interrupt_in_buffer =
825 port0->interrupt_in_buffer;
826 edge_serial->interrupt_in_endpoint =
827 port0->interrupt_in_endpointAddress;
1da177e4
LT
828 edge_serial->interrupt_read_urb = port0->interrupt_in_urb;
829 edge_serial->bulk_in_buffer = port0->bulk_in_buffer;
03f0dbf7
AC
830 edge_serial->bulk_in_endpoint =
831 port0->bulk_in_endpointAddress;
1da177e4 832 edge_serial->read_urb = port0->read_urb;
03f0dbf7
AC
833 edge_serial->bulk_out_endpoint =
834 port0->bulk_out_endpointAddress;
835
1da177e4
LT
836 /* set up our interrupt urb */
837 usb_fill_int_urb(edge_serial->interrupt_read_urb,
03f0dbf7
AC
838 serial->dev,
839 usb_rcvintpipe(serial->dev,
840 port0->interrupt_in_endpointAddress),
841 port0->interrupt_in_buffer,
842 edge_serial->interrupt_read_urb->transfer_buffer_length,
843 edge_interrupt_callback, edge_serial,
844 edge_serial->interrupt_read_urb->interval);
845
1da177e4
LT
846 /* set up our bulk in urb */
847 usb_fill_bulk_urb(edge_serial->read_urb, serial->dev,
03f0dbf7
AC
848 usb_rcvbulkpipe(serial->dev,
849 port0->bulk_in_endpointAddress),
850 port0->bulk_in_buffer,
851 edge_serial->read_urb->transfer_buffer_length,
852 edge_bulk_in_callback, edge_serial);
cb8eaa8b 853 edge_serial->read_in_progress = false;
1da177e4
LT
854
855 /* start interrupt read for this edgeport
03f0dbf7
AC
856 * this interrupt will continue as long
857 * as the edgeport is connected */
858 response = usb_submit_urb(edge_serial->interrupt_read_urb,
859 GFP_KERNEL);
1da177e4 860 if (response) {
984f6868
GKH
861 dev_err(dev, "%s - Error %d submitting control urb\n",
862 __func__, response);
1da177e4
LT
863 }
864 }
03f0dbf7 865
1da177e4
LT
866 /* initialize our wait queues */
867 init_waitqueue_head(&edge_port->wait_open);
868 init_waitqueue_head(&edge_port->wait_chase);
869 init_waitqueue_head(&edge_port->delta_msr_wait);
870 init_waitqueue_head(&edge_port->wait_command);
871
872 /* initialize our icount structure */
03f0dbf7 873 memset(&(edge_port->icount), 0x00, sizeof(edge_port->icount));
1da177e4
LT
874
875 /* initialize our port settings */
03f0dbf7
AC
876 edge_port->txCredits = 0; /* Can't send any data yet */
877 /* Must always set this bit to enable ints! */
878 edge_port->shadowMCR = MCR_MASTER_IE;
cb8eaa8b 879 edge_port->chaseResponsePending = false;
1da177e4
LT
880
881 /* send a open port command */
cb8eaa8b
RK
882 edge_port->openPending = true;
883 edge_port->open = false;
03f0dbf7 884 response = send_iosp_ext_cmd(edge_port, IOSP_CMD_OPEN_PORT, 0);
1da177e4
LT
885
886 if (response < 0) {
984f6868 887 dev_err(dev, "%s - error sending open port command\n", __func__);
cb8eaa8b 888 edge_port->openPending = false;
1da177e4
LT
889 return -ENODEV;
890 }
891
892 /* now wait for the port to be completely opened */
03f0dbf7
AC
893 wait_event_timeout(edge_port->wait_open, !edge_port->openPending,
894 OPEN_TIMEOUT);
1da177e4 895
cb8eaa8b 896 if (!edge_port->open) {
1da177e4 897 /* open timed out */
984f6868 898 dev_dbg(dev, "%s - open timedout\n", __func__);
cb8eaa8b 899 edge_port->openPending = false;
1da177e4
LT
900 return -ENODEV;
901 }
902
903 /* create the txfifo */
904 edge_port->txfifo.head = 0;
905 edge_port->txfifo.tail = 0;
906 edge_port->txfifo.count = 0;
907 edge_port->txfifo.size = edge_port->maxTxCredits;
03f0dbf7 908 edge_port->txfifo.fifo = kmalloc(edge_port->maxTxCredits, GFP_KERNEL);
1da177e4
LT
909
910 if (!edge_port->txfifo.fifo) {
984f6868 911 dev_dbg(dev, "%s - no memory\n", __func__);
335f8514 912 edge_close(port);
1da177e4
LT
913 return -ENOMEM;
914 }
915
916 /* Allocate a URB for the write */
03f0dbf7 917 edge_port->write_urb = usb_alloc_urb(0, GFP_KERNEL);
cb8eaa8b 918 edge_port->write_in_progress = false;
1da177e4
LT
919
920 if (!edge_port->write_urb) {
984f6868 921 dev_dbg(dev, "%s - no memory\n", __func__);
335f8514 922 edge_close(port);
1da177e4
LT
923 return -ENOMEM;
924 }
925
984f6868
GKH
926 dev_dbg(dev, "%s(%d) - Initialize TX fifo to %d bytes\n",
927 __func__, port->number, edge_port->maxTxCredits);
1da177e4
LT
928
929 return 0;
930}
931
932
933/************************************************************************
934 *
935 * block_until_chase_response
936 *
937 * This function will block the close until one of the following:
938 * 1. Response to our Chase comes from Edgeport
dc0d5c1e 939 * 2. A timeout of 10 seconds without activity has expired
1da177e4
LT
940 * (1K of Edgeport data @ 2400 baud ==> 4 sec to empty)
941 *
942 ************************************************************************/
943static void block_until_chase_response(struct edgeport_port *edge_port)
944{
984f6868 945 struct device *dev = &edge_port->port->dev;
1da177e4
LT
946 DEFINE_WAIT(wait);
947 __u16 lastCredits;
948 int timeout = 1*HZ;
949 int loop = 10;
950
951 while (1) {
03f0dbf7 952 /* Save Last credits */
1da177e4
LT
953 lastCredits = edge_port->txCredits;
954
03f0dbf7 955 /* Did we get our Chase response */
cb8eaa8b 956 if (!edge_port->chaseResponsePending) {
984f6868 957 dev_dbg(dev, "%s - Got Chase Response\n", __func__);
1da177e4 958
03f0dbf7
AC
959 /* did we get all of our credit back? */
960 if (edge_port->txCredits == edge_port->maxTxCredits) {
984f6868 961 dev_dbg(dev, "%s - Got all credits\n", __func__);
1da177e4
LT
962 return;
963 }
964 }
965
03f0dbf7
AC
966 /* Block the thread for a while */
967 prepare_to_wait(&edge_port->wait_chase, &wait,
968 TASK_UNINTERRUPTIBLE);
1da177e4
LT
969 schedule_timeout(timeout);
970 finish_wait(&edge_port->wait_chase, &wait);
971
972 if (lastCredits == edge_port->txCredits) {
03f0dbf7 973 /* No activity.. count down. */
1da177e4
LT
974 loop--;
975 if (loop == 0) {
cb8eaa8b 976 edge_port->chaseResponsePending = false;
984f6868 977 dev_dbg(dev, "%s - Chase TIMEOUT\n", __func__);
1da177e4
LT
978 return;
979 }
980 } else {
03f0dbf7 981 /* Reset timeout value back to 10 seconds */
984f6868 982 dev_dbg(dev, "%s - Last %d, Current %d\n", __func__,
03f0dbf7 983 lastCredits, edge_port->txCredits);
1da177e4
LT
984 loop = 10;
985 }
986 }
987}
988
989
990/************************************************************************
991 *
992 * block_until_tx_empty
993 *
994 * This function will block the close until one of the following:
995 * 1. TX count are 0
996 * 2. The edgeport has stopped
dc0d5c1e 997 * 3. A timeout of 3 seconds without activity has expired
1da177e4
LT
998 *
999 ************************************************************************/
03f0dbf7 1000static void block_until_tx_empty(struct edgeport_port *edge_port)
1da177e4 1001{
984f6868 1002 struct device *dev = &edge_port->port->dev;
1da177e4
LT
1003 DEFINE_WAIT(wait);
1004 struct TxFifo *fifo = &edge_port->txfifo;
1005 __u32 lastCount;
1006 int timeout = HZ/10;
1007 int loop = 30;
1008
1009 while (1) {
03f0dbf7 1010 /* Save Last count */
1da177e4
LT
1011 lastCount = fifo->count;
1012
03f0dbf7 1013 /* Is the Edgeport Buffer empty? */
1da177e4 1014 if (lastCount == 0) {
984f6868 1015 dev_dbg(dev, "%s - TX Buffer Empty\n", __func__);
1da177e4
LT
1016 return;
1017 }
1018
03f0dbf7
AC
1019 /* Block the thread for a while */
1020 prepare_to_wait(&edge_port->wait_chase, &wait,
1021 TASK_UNINTERRUPTIBLE);
1da177e4
LT
1022 schedule_timeout(timeout);
1023 finish_wait(&edge_port->wait_chase, &wait);
1024
984f6868 1025 dev_dbg(dev, "%s wait\n", __func__);
1da177e4
LT
1026
1027 if (lastCount == fifo->count) {
03f0dbf7 1028 /* No activity.. count down. */
1da177e4
LT
1029 loop--;
1030 if (loop == 0) {
984f6868 1031 dev_dbg(dev, "%s - TIMEOUT\n", __func__);
1da177e4
LT
1032 return;
1033 }
1034 } else {
03f0dbf7 1035 /* Reset timeout value back to seconds */
1da177e4
LT
1036 loop = 30;
1037 }
1038 }
1039}
1040
1041
1042/*****************************************************************************
1043 * edge_close
1044 * this function is called by the tty driver when a port is closed
1045 *****************************************************************************/
335f8514 1046static void edge_close(struct usb_serial_port *port)
1da177e4
LT
1047{
1048 struct edgeport_serial *edge_serial;
1049 struct edgeport_port *edge_port;
1050 int status;
1051
1da177e4
LT
1052 edge_serial = usb_get_serial_data(port->serial);
1053 edge_port = usb_get_serial_port_data(port);
03f0dbf7 1054 if (edge_serial == NULL || edge_port == NULL)
1da177e4 1055 return;
03f0dbf7
AC
1056
1057 /* block until tx is empty */
1da177e4
LT
1058 block_until_tx_empty(edge_port);
1059
cb8eaa8b 1060 edge_port->closePending = true;
1da177e4 1061
6e8cf775
GKH
1062 if ((!edge_serial->is_epic) ||
1063 ((edge_serial->is_epic) &&
1064 (edge_serial->epic_descriptor.Supports.IOSPChase))) {
1065 /* flush and chase */
cb8eaa8b 1066 edge_port->chaseResponsePending = true;
6e8cf775 1067
984f6868 1068 dev_dbg(&port->dev, "%s - Sending IOSP_CMD_CHASE_PORT\n", __func__);
03f0dbf7
AC
1069 status = send_iosp_ext_cmd(edge_port, IOSP_CMD_CHASE_PORT, 0);
1070 if (status == 0)
1071 /* block until chase finished */
6e8cf775 1072 block_until_chase_response(edge_port);
03f0dbf7 1073 else
cb8eaa8b 1074 edge_port->chaseResponsePending = false;
1da177e4
LT
1075 }
1076
6e8cf775
GKH
1077 if ((!edge_serial->is_epic) ||
1078 ((edge_serial->is_epic) &&
1079 (edge_serial->epic_descriptor.Supports.IOSPClose))) {
1080 /* close the port */
984f6868 1081 dev_dbg(&port->dev, "%s - Sending IOSP_CMD_CLOSE_PORT\n", __func__);
03f0dbf7 1082 send_iosp_ext_cmd(edge_port, IOSP_CMD_CLOSE_PORT, 0);
6e8cf775 1083 }
1da177e4 1084
03f0dbf7 1085 /* port->close = true; */
cb8eaa8b
RK
1086 edge_port->closePending = false;
1087 edge_port->open = false;
1088 edge_port->openPending = false;
1da177e4 1089
9a25f44f 1090 usb_kill_urb(edge_port->write_urb);
1da177e4
LT
1091
1092 if (edge_port->write_urb) {
03f0dbf7
AC
1093 /* if this urb had a transfer buffer already
1094 (old transfer) free it */
1bc3c9e1
JJ
1095 kfree(edge_port->write_urb->transfer_buffer);
1096 usb_free_urb(edge_port->write_urb);
1da177e4
LT
1097 edge_port->write_urb = NULL;
1098 }
1bc3c9e1
JJ
1099 kfree(edge_port->txfifo.fifo);
1100 edge_port->txfifo.fifo = NULL;
03f0dbf7 1101}
1da177e4
LT
1102
1103/*****************************************************************************
1104 * SerialWrite
03f0dbf7
AC
1105 * this function is called by the tty driver when data should be written
1106 * to the port.
1107 * If successful, we return the number of bytes written, otherwise we
1108 * return a negative error number.
1da177e4 1109 *****************************************************************************/
95da310e
AC
1110static int edge_write(struct tty_struct *tty, struct usb_serial_port *port,
1111 const unsigned char *data, int count)
1da177e4
LT
1112{
1113 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1114 struct TxFifo *fifo;
1115 int copySize;
1116 int bytesleft;
1117 int firsthalf;
1118 int secondhalf;
1119 unsigned long flags;
1120
1da177e4
LT
1121 if (edge_port == NULL)
1122 return -ENODEV;
1123
03f0dbf7 1124 /* get a pointer to the Tx fifo */
1da177e4
LT
1125 fifo = &edge_port->txfifo;
1126
1127 spin_lock_irqsave(&edge_port->ep_lock, flags);
1128
03f0dbf7
AC
1129 /* calculate number of bytes to put in fifo */
1130 copySize = min((unsigned int)count,
1131 (edge_port->txCredits - fifo->count));
1da177e4 1132
984f6868
GKH
1133 dev_dbg(&port->dev, "%s(%d) of %d byte(s) Fifo room %d -- will copy %d bytes\n",
1134 __func__, port->number, count,
03f0dbf7 1135 edge_port->txCredits - fifo->count, copySize);
1da177e4 1136
03f0dbf7
AC
1137 /* catch writes of 0 bytes which the tty driver likes to give us,
1138 and when txCredits is empty */
1da177e4 1139 if (copySize == 0) {
984f6868 1140 dev_dbg(&port->dev, "%s - copySize = Zero\n", __func__);
1da177e4
LT
1141 goto finish_write;
1142 }
1143
03f0dbf7
AC
1144 /* queue the data
1145 * since we can never overflow the buffer we do not have to check for a
1146 * full condition
1147 *
1148 * the copy is done is two parts -- first fill to the end of the buffer
1149 * then copy the reset from the start of the buffer
1150 */
1da177e4 1151 bytesleft = fifo->size - fifo->head;
03f0dbf7 1152 firsthalf = min(bytesleft, copySize);
984f6868
GKH
1153 dev_dbg(&port->dev, "%s - copy %d bytes of %d into fifo \n", __func__,
1154 firsthalf, bytesleft);
1da177e4
LT
1155
1156 /* now copy our data */
1157 memcpy(&fifo->fifo[fifo->head], data, firsthalf);
59d33f2f 1158 usb_serial_debug_data(&port->dev, __func__, firsthalf, &fifo->fifo[fifo->head]);
1da177e4 1159
03f0dbf7 1160 /* update the index and size */
1da177e4
LT
1161 fifo->head += firsthalf;
1162 fifo->count += firsthalf;
1163
03f0dbf7
AC
1164 /* wrap the index */
1165 if (fifo->head == fifo->size)
1da177e4 1166 fifo->head = 0;
1da177e4
LT
1167
1168 secondhalf = copySize-firsthalf;
1169
1170 if (secondhalf) {
984f6868 1171 dev_dbg(&port->dev, "%s - copy rest of data %d\n", __func__, secondhalf);
1da177e4 1172 memcpy(&fifo->fifo[fifo->head], &data[firsthalf], secondhalf);
59d33f2f 1173 usb_serial_debug_data(&port->dev, __func__, secondhalf, &fifo->fifo[fifo->head]);
03f0dbf7 1174 /* update the index and size */
1da177e4
LT
1175 fifo->count += secondhalf;
1176 fifo->head += secondhalf;
03f0dbf7
AC
1177 /* No need to check for wrap since we can not get to end of
1178 * the fifo in this part
1179 */
1da177e4
LT
1180 }
1181
1182finish_write:
1183 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1184
03f0dbf7
AC
1185 send_more_port_data((struct edgeport_serial *)
1186 usb_get_serial_data(port->serial), edge_port);
1da177e4 1187
984f6868
GKH
1188 dev_dbg(&port->dev, "%s wrote %d byte(s) TxCredits %d, Fifo %d\n",
1189 __func__, copySize, edge_port->txCredits, fifo->count);
1da177e4 1190
03f0dbf7 1191 return copySize;
1da177e4
LT
1192}
1193
1194
1195/************************************************************************
1196 *
1197 * send_more_port_data()
1198 *
1199 * This routine attempts to write additional UART transmit data
1200 * to a port over the USB bulk pipe. It is called (1) when new
1201 * data has been written to a port's TxBuffer from higher layers
1202 * (2) when the peripheral sends us additional TxCredits indicating
1203 * that it can accept more Tx data for a given port; and (3) when
1204 * a bulk write completes successfully and we want to see if we
1205 * can transmit more.
1206 *
1207 ************************************************************************/
03f0dbf7
AC
1208static void send_more_port_data(struct edgeport_serial *edge_serial,
1209 struct edgeport_port *edge_port)
1da177e4
LT
1210{
1211 struct TxFifo *fifo = &edge_port->txfifo;
984f6868 1212 struct device *dev = &edge_port->port->dev;
1da177e4
LT
1213 struct urb *urb;
1214 unsigned char *buffer;
1215 int status;
1216 int count;
1217 int bytesleft;
1218 int firsthalf;
1219 int secondhalf;
1220 unsigned long flags;
1221
1da177e4
LT
1222 spin_lock_irqsave(&edge_port->ep_lock, flags);
1223
1224 if (edge_port->write_in_progress ||
1225 !edge_port->open ||
1226 (fifo->count == 0)) {
984f6868
GKH
1227 dev_dbg(dev, "%s(%d) EXIT - fifo %d, PendingWrite = %d\n",
1228 __func__, edge_port->port->number,
1229 fifo->count, edge_port->write_in_progress);
1da177e4
LT
1230 goto exit_send;
1231 }
1232
03f0dbf7
AC
1233 /* since the amount of data in the fifo will always fit into the
1234 * edgeport buffer we do not need to check the write length
1235 *
1236 * Do we have enough credits for this port to make it worthwhile
1237 * to bother queueing a write. If it's too small, say a few bytes,
1238 * it's better to wait for more credits so we can do a larger write.
1239 */
1240 if (edge_port->txCredits < EDGE_FW_GET_TX_CREDITS_SEND_THRESHOLD(edge_port->maxTxCredits, EDGE_FW_BULK_MAX_PACKET_SIZE)) {
984f6868 1241 dev_dbg(dev, "%s(%d) Not enough credit - fifo %d TxCredit %d\n",
03f0dbf7
AC
1242 __func__, edge_port->port->number, fifo->count,
1243 edge_port->txCredits);
1da177e4
LT
1244 goto exit_send;
1245 }
1246
03f0dbf7 1247 /* lock this write */
cb8eaa8b 1248 edge_port->write_in_progress = true;
1da177e4 1249
03f0dbf7 1250 /* get a pointer to the write_urb */
1da177e4
LT
1251 urb = edge_port->write_urb;
1252
1bc3c9e1
JJ
1253 /* make sure transfer buffer is freed */
1254 kfree(urb->transfer_buffer);
1255 urb->transfer_buffer = NULL;
1da177e4 1256
03f0dbf7
AC
1257 /* build the data header for the buffer and port that we are about
1258 to send out */
1da177e4 1259 count = fifo->count;
03f0dbf7 1260 buffer = kmalloc(count+2, GFP_ATOMIC);
1da177e4 1261 if (buffer == NULL) {
22a416c4 1262 dev_err_console(edge_port->port,
03f0dbf7 1263 "%s - no more kernel memory...\n", __func__);
cb8eaa8b 1264 edge_port->write_in_progress = false;
1da177e4
LT
1265 goto exit_send;
1266 }
03f0dbf7
AC
1267 buffer[0] = IOSP_BUILD_DATA_HDR1(edge_port->port->number
1268 - edge_port->port->serial->minor, count);
1269 buffer[1] = IOSP_BUILD_DATA_HDR2(edge_port->port->number
1270 - edge_port->port->serial->minor, count);
1da177e4
LT
1271
1272 /* now copy our data */
1273 bytesleft = fifo->size - fifo->tail;
03f0dbf7 1274 firsthalf = min(bytesleft, count);
1da177e4
LT
1275 memcpy(&buffer[2], &fifo->fifo[fifo->tail], firsthalf);
1276 fifo->tail += firsthalf;
1277 fifo->count -= firsthalf;
03f0dbf7 1278 if (fifo->tail == fifo->size)
1da177e4 1279 fifo->tail = 0;
1da177e4
LT
1280
1281 secondhalf = count-firsthalf;
1282 if (secondhalf) {
03f0dbf7
AC
1283 memcpy(&buffer[2+firsthalf], &fifo->fifo[fifo->tail],
1284 secondhalf);
1da177e4
LT
1285 fifo->tail += secondhalf;
1286 fifo->count -= secondhalf;
1287 }
1288
1289 if (count)
59d33f2f 1290 usb_serial_debug_data(&edge_port->port->dev, __func__, count, &buffer[2]);
1da177e4
LT
1291
1292 /* fill up the urb with all of our data and submit it */
03f0dbf7
AC
1293 usb_fill_bulk_urb(urb, edge_serial->serial->dev,
1294 usb_sndbulkpipe(edge_serial->serial->dev,
1295 edge_serial->bulk_out_endpoint),
1296 buffer, count+2,
1297 edge_bulk_out_data_callback, edge_port);
1da177e4
LT
1298
1299 /* decrement the number of credits we have by the number we just sent */
1300 edge_port->txCredits -= count;
1301 edge_port->icount.tx += count;
1302
1da177e4
LT
1303 status = usb_submit_urb(urb, GFP_ATOMIC);
1304 if (status) {
1305 /* something went wrong */
22a416c4 1306 dev_err_console(edge_port->port,
03f0dbf7
AC
1307 "%s - usb_submit_urb(write bulk) failed, status = %d, data lost\n",
1308 __func__, status);
cb8eaa8b 1309 edge_port->write_in_progress = false;
1da177e4
LT
1310
1311 /* revert the credits as something bad happened. */
1312 edge_port->txCredits += count;
1313 edge_port->icount.tx -= count;
1314 }
984f6868
GKH
1315 dev_dbg(dev, "%s wrote %d byte(s) TxCredit %d, Fifo %d\n",
1316 __func__, count, edge_port->txCredits, fifo->count);
1da177e4
LT
1317
1318exit_send:
1319 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1320}
1321
1322
1323/*****************************************************************************
1324 * edge_write_room
03f0dbf7
AC
1325 * this function is called by the tty driver when it wants to know how
1326 * many bytes of data we can accept for a specific port. If successful,
1327 * we return the amount of room that we have for this port (the txCredits)
1328 * otherwise we return a negative error number.
1da177e4 1329 *****************************************************************************/
95da310e 1330static int edge_write_room(struct tty_struct *tty)
1da177e4 1331{
95da310e 1332 struct usb_serial_port *port = tty->driver_data;
1da177e4
LT
1333 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1334 int room;
1335 unsigned long flags;
1336
1da177e4 1337 if (edge_port == NULL)
d76f2f44 1338 return 0;
cb8eaa8b 1339 if (edge_port->closePending)
d76f2f44 1340 return 0;
1da177e4 1341
1da177e4 1342 if (!edge_port->open) {
984f6868 1343 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
d76f2f44 1344 return 0;
1da177e4
LT
1345 }
1346
03f0dbf7 1347 /* total of both buffers is still txCredit */
1da177e4
LT
1348 spin_lock_irqsave(&edge_port->ep_lock, flags);
1349 room = edge_port->txCredits - edge_port->txfifo.count;
1350 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1351
984f6868 1352 dev_dbg(&port->dev, "%s - returns %d\n", __func__, room);
1da177e4
LT
1353 return room;
1354}
1355
1356
1357/*****************************************************************************
1358 * edge_chars_in_buffer
03f0dbf7
AC
1359 * this function is called by the tty driver when it wants to know how
1360 * many bytes of data we currently have outstanding in the port (data that
1361 * has been written, but hasn't made it out the port yet)
1362 * If successful, we return the number of bytes left to be written in the
1363 * system,
1da177e4
LT
1364 * Otherwise we return a negative error number.
1365 *****************************************************************************/
95da310e 1366static int edge_chars_in_buffer(struct tty_struct *tty)
1da177e4 1367{
95da310e 1368 struct usb_serial_port *port = tty->driver_data;
1da177e4
LT
1369 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1370 int num_chars;
1371 unsigned long flags;
1372
1da177e4 1373 if (edge_port == NULL)
d76f2f44 1374 return 0;
cb8eaa8b 1375 if (edge_port->closePending)
d76f2f44 1376 return 0;
1da177e4
LT
1377
1378 if (!edge_port->open) {
984f6868 1379 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
d76f2f44 1380 return 0;
1da177e4
LT
1381 }
1382
1383 spin_lock_irqsave(&edge_port->ep_lock, flags);
03f0dbf7
AC
1384 num_chars = edge_port->maxTxCredits - edge_port->txCredits +
1385 edge_port->txfifo.count;
1da177e4
LT
1386 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1387 if (num_chars) {
984f6868
GKH
1388 dev_dbg(&port->dev, "%s(port %d) - returns %d\n", __func__,
1389 port->number, num_chars);
1da177e4
LT
1390 }
1391
1392 return num_chars;
1393}
1394
1395
1396/*****************************************************************************
1397 * SerialThrottle
1398 * this function is called by the tty driver when it wants to stop the data
1399 * being read from the port.
1400 *****************************************************************************/
95da310e 1401static void edge_throttle(struct tty_struct *tty)
1da177e4 1402{
95da310e 1403 struct usb_serial_port *port = tty->driver_data;
1da177e4 1404 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1da177e4
LT
1405 int status;
1406
1da177e4
LT
1407 if (edge_port == NULL)
1408 return;
1409
1410 if (!edge_port->open) {
984f6868 1411 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
1da177e4
LT
1412 return;
1413 }
1414
1da177e4
LT
1415 /* if we are implementing XON/XOFF, send the stop character */
1416 if (I_IXOFF(tty)) {
1417 unsigned char stop_char = STOP_CHAR(tty);
03f0dbf7
AC
1418 status = edge_write(tty, port, &stop_char, 1);
1419 if (status <= 0)
1da177e4 1420 return;
1da177e4
LT
1421 }
1422
1423 /* if we are implementing RTS/CTS, toggle that line */
adc8d746 1424 if (tty->termios.c_cflag & CRTSCTS) {
1da177e4 1425 edge_port->shadowMCR &= ~MCR_RTS;
03f0dbf7
AC
1426 status = send_cmd_write_uart_register(edge_port, MCR,
1427 edge_port->shadowMCR);
1428 if (status != 0)
1da177e4 1429 return;
1da177e4 1430 }
1da177e4
LT
1431}
1432
1433
1434/*****************************************************************************
1435 * edge_unthrottle
03f0dbf7
AC
1436 * this function is called by the tty driver when it wants to resume the
1437 * data being read from the port (called after SerialThrottle is called)
1da177e4 1438 *****************************************************************************/
95da310e 1439static void edge_unthrottle(struct tty_struct *tty)
1da177e4 1440{
95da310e 1441 struct usb_serial_port *port = tty->driver_data;
1da177e4 1442 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1da177e4
LT
1443 int status;
1444
1da177e4
LT
1445 if (edge_port == NULL)
1446 return;
1447
1448 if (!edge_port->open) {
984f6868 1449 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
1da177e4
LT
1450 return;
1451 }
1452
1da177e4
LT
1453 /* if we are implementing XON/XOFF, send the start character */
1454 if (I_IXOFF(tty)) {
1455 unsigned char start_char = START_CHAR(tty);
95da310e
AC
1456 status = edge_write(tty, port, &start_char, 1);
1457 if (status <= 0)
1da177e4 1458 return;
1da177e4 1459 }
1da177e4 1460 /* if we are implementing RTS/CTS, toggle that line */
adc8d746 1461 if (tty->termios.c_cflag & CRTSCTS) {
1da177e4 1462 edge_port->shadowMCR |= MCR_RTS;
03f0dbf7
AC
1463 send_cmd_write_uart_register(edge_port, MCR,
1464 edge_port->shadowMCR);
1da177e4 1465 }
1da177e4
LT
1466}
1467
1468
1469/*****************************************************************************
1470 * SerialSetTermios
03f0dbf7
AC
1471 * this function is called by the tty driver when it wants to change
1472 * the termios structure
1da177e4 1473 *****************************************************************************/
95da310e
AC
1474static void edge_set_termios(struct tty_struct *tty,
1475 struct usb_serial_port *port, struct ktermios *old_termios)
1da177e4
LT
1476{
1477 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1da177e4
LT
1478 unsigned int cflag;
1479
adc8d746 1480 cflag = tty->termios.c_cflag;
d9a80746 1481 dev_dbg(&port->dev, "%s - clfag %08x iflag %08x\n", __func__, tty->termios.c_cflag, tty->termios.c_iflag);
984f6868 1482 dev_dbg(&port->dev, "%s - old clfag %08x old iflag %08x\n", __func__, old_termios->c_cflag, old_termios->c_iflag);
1da177e4
LT
1483
1484 if (edge_port == NULL)
1485 return;
1486
1487 if (!edge_port->open) {
984f6868 1488 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
1da177e4
LT
1489 return;
1490 }
1491
1492 /* change the port settings to the new ones specified */
95da310e 1493 change_port_settings(tty, edge_port, old_termios);
1da177e4
LT
1494}
1495
1496
1497/*****************************************************************************
1498 * get_lsr_info - get line status register info
1499 *
1500 * Purpose: Let user call ioctl() to get info when the UART physically
1501 * is emptied. On bus types like RS485, the transmitter must
1502 * release the bus after transmitting. This must be done when
1503 * the transmit shift register is empty, not be done when the
1504 * transmit holding register is empty. This functionality
03f0dbf7 1505 * allows an RS485 driver to be written in user space.
1da177e4 1506 *****************************************************************************/
03f0dbf7
AC
1507static int get_lsr_info(struct edgeport_port *edge_port,
1508 unsigned int __user *value)
1da177e4
LT
1509{
1510 unsigned int result = 0;
1511 unsigned long flags;
1512
1513 spin_lock_irqsave(&edge_port->ep_lock, flags);
1514 if (edge_port->maxTxCredits == edge_port->txCredits &&
1515 edge_port->txfifo.count == 0) {
984f6868 1516 dev_dbg(&edge_port->port->dev, "%s -- Empty\n", __func__);
1da177e4
LT
1517 result = TIOCSER_TEMT;
1518 }
1519 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1520
1521 if (copy_to_user(value, &result, sizeof(int)))
1522 return -EFAULT;
1523 return 0;
1524}
1525
20b9d177 1526static int edge_tiocmset(struct tty_struct *tty,
03f0dbf7 1527 unsigned int set, unsigned int clear)
1da177e4 1528{
95da310e 1529 struct usb_serial_port *port = tty->driver_data;
1da177e4
LT
1530 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1531 unsigned int mcr;
1532
1da177e4
LT
1533 mcr = edge_port->shadowMCR;
1534 if (set & TIOCM_RTS)
1535 mcr |= MCR_RTS;
1536 if (set & TIOCM_DTR)
1537 mcr |= MCR_DTR;
1538 if (set & TIOCM_LOOP)
1539 mcr |= MCR_LOOPBACK;
1540
1541 if (clear & TIOCM_RTS)
1542 mcr &= ~MCR_RTS;
1543 if (clear & TIOCM_DTR)
1544 mcr &= ~MCR_DTR;
1545 if (clear & TIOCM_LOOP)
1546 mcr &= ~MCR_LOOPBACK;
1547
1548 edge_port->shadowMCR = mcr;
1549
1550 send_cmd_write_uart_register(edge_port, MCR, edge_port->shadowMCR);
1551
1552 return 0;
1553}
1554
60b33c13 1555static int edge_tiocmget(struct tty_struct *tty)
1da177e4 1556{
95da310e 1557 struct usb_serial_port *port = tty->driver_data;
1da177e4
LT
1558 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1559 unsigned int result = 0;
1560 unsigned int msr;
1561 unsigned int mcr;
1562
1da177e4
LT
1563 msr = edge_port->shadowMSR;
1564 mcr = edge_port->shadowMCR;
1565 result = ((mcr & MCR_DTR) ? TIOCM_DTR: 0) /* 0x002 */
1566 | ((mcr & MCR_RTS) ? TIOCM_RTS: 0) /* 0x004 */
1567 | ((msr & EDGEPORT_MSR_CTS) ? TIOCM_CTS: 0) /* 0x020 */
1568 | ((msr & EDGEPORT_MSR_CD) ? TIOCM_CAR: 0) /* 0x040 */
1569 | ((msr & EDGEPORT_MSR_RI) ? TIOCM_RI: 0) /* 0x080 */
1570 | ((msr & EDGEPORT_MSR_DSR) ? TIOCM_DSR: 0); /* 0x100 */
1571
1da177e4
LT
1572 return result;
1573}
1574
0bca1b91
AC
1575static int edge_get_icount(struct tty_struct *tty,
1576 struct serial_icounter_struct *icount)
1577{
1578 struct usb_serial_port *port = tty->driver_data;
1579 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1580 struct async_icount cnow;
1581 cnow = edge_port->icount;
1582
1583 icount->cts = cnow.cts;
1584 icount->dsr = cnow.dsr;
1585 icount->rng = cnow.rng;
1586 icount->dcd = cnow.dcd;
1587 icount->rx = cnow.rx;
1588 icount->tx = cnow.tx;
1589 icount->frame = cnow.frame;
1590 icount->overrun = cnow.overrun;
1591 icount->parity = cnow.parity;
1592 icount->brk = cnow.brk;
1593 icount->buf_overrun = cnow.buf_overrun;
1594
984f6868
GKH
1595 dev_dbg(&port->dev, "%s (%d) TIOCGICOUNT RX=%d, TX=%d\n", __func__,
1596 port->number, icount->rx, icount->tx);
0bca1b91
AC
1597 return 0;
1598}
1599
03f0dbf7
AC
1600static int get_serial_info(struct edgeport_port *edge_port,
1601 struct serial_struct __user *retinfo)
1da177e4
LT
1602{
1603 struct serial_struct tmp;
1604
1605 if (!retinfo)
1606 return -EFAULT;
1607
1608 memset(&tmp, 0, sizeof(tmp));
1609
1610 tmp.type = PORT_16550A;
1611 tmp.line = edge_port->port->serial->minor;
1612 tmp.port = edge_port->port->number;
1613 tmp.irq = 0;
1614 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
1615 tmp.xmit_fifo_size = edge_port->maxTxCredits;
1616 tmp.baud_base = 9600;
1617 tmp.close_delay = 5*HZ;
1618 tmp.closing_wait = 30*HZ;
1da177e4
LT
1619
1620 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1621 return -EFAULT;
1622 return 0;
1623}
1624
1625
1da177e4
LT
1626/*****************************************************************************
1627 * SerialIoctl
1628 * this function handles any ioctl calls to the driver
1629 *****************************************************************************/
00a0d0d6 1630static int edge_ioctl(struct tty_struct *tty,
95da310e 1631 unsigned int cmd, unsigned long arg)
1da177e4 1632{
95da310e 1633 struct usb_serial_port *port = tty->driver_data;
1da177e4
LT
1634 DEFINE_WAIT(wait);
1635 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1636 struct async_icount cnow;
1637 struct async_icount cprev;
1da177e4 1638
984f6868 1639 dev_dbg(&port->dev, "%s - port %d, cmd = 0x%x\n", __func__, port->number, cmd);
1da177e4
LT
1640
1641 switch (cmd) {
03f0dbf7 1642 case TIOCSERGETLSR:
984f6868 1643 dev_dbg(&port->dev, "%s (%d) TIOCSERGETLSR\n", __func__, port->number);
03f0dbf7
AC
1644 return get_lsr_info(edge_port, (unsigned int __user *) arg);
1645
1646 case TIOCGSERIAL:
984f6868 1647 dev_dbg(&port->dev, "%s (%d) TIOCGSERIAL\n", __func__, port->number);
03f0dbf7
AC
1648 return get_serial_info(edge_port, (struct serial_struct __user *) arg);
1649
1650 case TIOCMIWAIT:
984f6868 1651 dev_dbg(&port->dev, "%s (%d) TIOCMIWAIT\n", __func__, port->number);
03f0dbf7
AC
1652 cprev = edge_port->icount;
1653 while (1) {
1654 prepare_to_wait(&edge_port->delta_msr_wait,
1655 &wait, TASK_INTERRUPTIBLE);
1656 schedule();
1657 finish_wait(&edge_port->delta_msr_wait, &wait);
1658 /* see if a signal did it */
1659 if (signal_pending(current))
1660 return -ERESTARTSYS;
1661 cnow = edge_port->icount;
1662 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
1663 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
1664 return -EIO; /* no change => error */
1665 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1666 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1667 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1668 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
1669 return 0;
1da177e4 1670 }
03f0dbf7
AC
1671 cprev = cnow;
1672 }
1673 /* NOTREACHED */
1674 break;
1da177e4 1675
1da177e4 1676 }
1da177e4
LT
1677 return -ENOIOCTLCMD;
1678}
1679
1680
1681/*****************************************************************************
1682 * SerialBreak
1683 * this function sends a break to the port
1684 *****************************************************************************/
03f0dbf7 1685static void edge_break(struct tty_struct *tty, int break_state)
1da177e4 1686{
95da310e 1687 struct usb_serial_port *port = tty->driver_data;
1da177e4 1688 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
6e8cf775 1689 struct edgeport_serial *edge_serial = usb_get_serial_data(port->serial);
1da177e4
LT
1690 int status;
1691
6e8cf775
GKH
1692 if ((!edge_serial->is_epic) ||
1693 ((edge_serial->is_epic) &&
1694 (edge_serial->epic_descriptor.Supports.IOSPChase))) {
1695 /* flush and chase */
cb8eaa8b 1696 edge_port->chaseResponsePending = true;
6e8cf775 1697
984f6868 1698 dev_dbg(&port->dev, "%s - Sending IOSP_CMD_CHASE_PORT\n", __func__);
03f0dbf7 1699 status = send_iosp_ext_cmd(edge_port, IOSP_CMD_CHASE_PORT, 0);
6e8cf775 1700 if (status == 0) {
03f0dbf7 1701 /* block until chase finished */
6e8cf775
GKH
1702 block_until_chase_response(edge_port);
1703 } else {
cb8eaa8b 1704 edge_port->chaseResponsePending = false;
6e8cf775 1705 }
1da177e4
LT
1706 }
1707
6e8cf775
GKH
1708 if ((!edge_serial->is_epic) ||
1709 ((edge_serial->is_epic) &&
1710 (edge_serial->epic_descriptor.Supports.IOSPSetClrBreak))) {
1711 if (break_state == -1) {
984f6868 1712 dev_dbg(&port->dev, "%s - Sending IOSP_CMD_SET_BREAK\n", __func__);
03f0dbf7
AC
1713 status = send_iosp_ext_cmd(edge_port,
1714 IOSP_CMD_SET_BREAK, 0);
6e8cf775 1715 } else {
984f6868 1716 dev_dbg(&port->dev, "%s - Sending IOSP_CMD_CLEAR_BREAK\n", __func__);
03f0dbf7
AC
1717 status = send_iosp_ext_cmd(edge_port,
1718 IOSP_CMD_CLEAR_BREAK, 0);
6e8cf775 1719 }
03f0dbf7 1720 if (status)
984f6868 1721 dev_dbg(&port->dev, "%s - error sending break set/clear command.\n",
03f0dbf7 1722 __func__);
1da177e4 1723 }
1da177e4
LT
1724}
1725
1726
1727/*****************************************************************************
1728 * process_rcvd_data
1729 * this function handles the data received on the bulk in pipe.
1730 *****************************************************************************/
03f0dbf7
AC
1731static void process_rcvd_data(struct edgeport_serial *edge_serial,
1732 unsigned char *buffer, __u16 bufferLength)
1da177e4 1733{
984f6868 1734 struct device *dev = &edge_serial->serial->dev->dev;
1da177e4
LT
1735 struct usb_serial_port *port;
1736 struct edgeport_port *edge_port;
1da177e4
LT
1737 __u16 lastBufferLength;
1738 __u16 rxLen;
1739
1da177e4
LT
1740 lastBufferLength = bufferLength + 1;
1741
1742 while (bufferLength > 0) {
1743 /* failsafe incase we get a message that we don't understand */
1744 if (lastBufferLength == bufferLength) {
984f6868 1745 dev_dbg(dev, "%s - stuck in loop, exiting it.\n", __func__);
1da177e4
LT
1746 break;
1747 }
1748 lastBufferLength = bufferLength;
1749
1750 switch (edge_serial->rxState) {
03f0dbf7
AC
1751 case EXPECT_HDR1:
1752 edge_serial->rxHeader1 = *buffer;
1753 ++buffer;
1754 --bufferLength;
1da177e4 1755
03f0dbf7
AC
1756 if (bufferLength == 0) {
1757 edge_serial->rxState = EXPECT_HDR2;
1758 break;
1759 }
1760 /* otherwise, drop on through */
1761 case EXPECT_HDR2:
1762 edge_serial->rxHeader2 = *buffer;
1763 ++buffer;
1764 --bufferLength;
1765
984f6868
GKH
1766 dev_dbg(dev, "%s - Hdr1=%02X Hdr2=%02X\n", __func__,
1767 edge_serial->rxHeader1, edge_serial->rxHeader2);
03f0dbf7
AC
1768 /* Process depending on whether this header is
1769 * data or status */
1770
1771 if (IS_CMD_STAT_HDR(edge_serial->rxHeader1)) {
1772 /* Decode this status header and go to
1773 * EXPECT_HDR1 (if we can process the status
1774 * with only 2 bytes), or go to EXPECT_HDR3 to
1775 * get the third byte. */
1776 edge_serial->rxPort =
1777 IOSP_GET_HDR_PORT(edge_serial->rxHeader1);
1778 edge_serial->rxStatusCode =
1779 IOSP_GET_STATUS_CODE(
1780 edge_serial->rxHeader1);
1781
1782 if (!IOSP_STATUS_IS_2BYTE(
1783 edge_serial->rxStatusCode)) {
1784 /* This status needs additional bytes.
1785 * Save what we have and then wait for
1786 * more data.
1787 */
1788 edge_serial->rxStatusParam
1789 = edge_serial->rxHeader2;
1790 edge_serial->rxState = EXPECT_HDR3;
1da177e4
LT
1791 break;
1792 }
03f0dbf7
AC
1793 /* We have all the header bytes, process the
1794 status now */
1795 process_rcvd_status(edge_serial,
1796 edge_serial->rxHeader2, 0);
1797 edge_serial->rxState = EXPECT_HDR1;
1798 break;
1799 } else {
1800 edge_serial->rxPort =
1801 IOSP_GET_HDR_PORT(edge_serial->rxHeader1);
1802 edge_serial->rxBytesRemaining =
1803 IOSP_GET_HDR_DATA_LEN(
1804 edge_serial->rxHeader1,
1805 edge_serial->rxHeader2);
984f6868
GKH
1806 dev_dbg(dev, "%s - Data for Port %u Len %u\n",
1807 __func__,
1808 edge_serial->rxPort,
1809 edge_serial->rxBytesRemaining);
03f0dbf7
AC
1810
1811 /* ASSERT(DevExt->RxPort < DevExt->NumPorts);
1812 * ASSERT(DevExt->RxBytesRemaining <
1813 * IOSP_MAX_DATA_LENGTH);
1814 */
1da177e4 1815
03f0dbf7
AC
1816 if (bufferLength == 0) {
1817 edge_serial->rxState = EXPECT_DATA;
1da177e4 1818 break;
1da177e4 1819 }
03f0dbf7
AC
1820 /* Else, drop through */
1821 }
1822 case EXPECT_DATA: /* Expect data */
1823 if (bufferLength < edge_serial->rxBytesRemaining) {
1824 rxLen = bufferLength;
1825 /* Expect data to start next buffer */
1826 edge_serial->rxState = EXPECT_DATA;
1827 } else {
1828 /* BufLen >= RxBytesRemaining */
1829 rxLen = edge_serial->rxBytesRemaining;
1830 /* Start another header next time */
1831 edge_serial->rxState = EXPECT_HDR1;
1832 }
1da177e4 1833
03f0dbf7
AC
1834 bufferLength -= rxLen;
1835 edge_serial->rxBytesRemaining -= rxLen;
1da177e4 1836
03f0dbf7
AC
1837 /* spit this data back into the tty driver if this
1838 port is open */
1839 if (rxLen) {
1840 port = edge_serial->serial->port[
1841 edge_serial->rxPort];
1842 edge_port = usb_get_serial_port_data(port);
1843 if (edge_port->open) {
2e124b4a
JS
1844 dev_dbg(dev, "%s - Sending %d bytes to TTY for port %d\n",
1845 __func__, rxLen,
1846 edge_serial->rxPort);
1847 edge_tty_recv(edge_port->port, buffer,
1848 rxLen);
03f0dbf7 1849 edge_port->icount.rx += rxLen;
1da177e4 1850 }
03f0dbf7
AC
1851 buffer += rxLen;
1852 }
1853 break;
1da177e4 1854
03f0dbf7
AC
1855 case EXPECT_HDR3: /* Expect 3rd byte of status header */
1856 edge_serial->rxHeader3 = *buffer;
1857 ++buffer;
1858 --bufferLength;
1859
1860 /* We have all the header bytes, process the
1861 status now */
1862 process_rcvd_status(edge_serial,
1863 edge_serial->rxStatusParam,
1864 edge_serial->rxHeader3);
1865 edge_serial->rxState = EXPECT_HDR1;
1866 break;
1da177e4
LT
1867 }
1868 }
1869}
1870
1871
1872/*****************************************************************************
1873 * process_rcvd_status
03f0dbf7
AC
1874 * this function handles the any status messages received on the
1875 * bulk in pipe.
1da177e4 1876 *****************************************************************************/
03f0dbf7
AC
1877static void process_rcvd_status(struct edgeport_serial *edge_serial,
1878 __u8 byte2, __u8 byte3)
1da177e4
LT
1879{
1880 struct usb_serial_port *port;
1881 struct edgeport_port *edge_port;
4a90f09b 1882 struct tty_struct *tty;
984f6868 1883 struct device *dev;
1da177e4
LT
1884 __u8 code = edge_serial->rxStatusCode;
1885
1886 /* switch the port pointer to the one being currently talked about */
1887 port = edge_serial->serial->port[edge_serial->rxPort];
1888 edge_port = usb_get_serial_port_data(port);
1889 if (edge_port == NULL) {
03f0dbf7
AC
1890 dev_err(&edge_serial->serial->dev->dev,
1891 "%s - edge_port == NULL for port %d\n",
1892 __func__, edge_serial->rxPort);
1da177e4
LT
1893 return;
1894 }
984f6868 1895 dev = &port->dev;
1da177e4
LT
1896
1897 if (code == IOSP_EXT_STATUS) {
1898 switch (byte2) {
03f0dbf7
AC
1899 case IOSP_EXT_STATUS_CHASE_RSP:
1900 /* we want to do EXT status regardless of port
1901 * open/closed */
984f6868
GKH
1902 dev_dbg(dev, "%s - Port %u EXT CHASE_RSP Data = %02x\n",
1903 __func__, edge_serial->rxPort, byte3);
03f0dbf7
AC
1904 /* Currently, the only EXT_STATUS is Chase, so process
1905 * here instead of one more call to one more subroutine
1906 * If/when more EXT_STATUS, there'll be more work to do
1907 * Also, we currently clear flag and close the port
1908 * regardless of content of above's Byte3.
1909 * We could choose to do something else when Byte3 says
1910 * Timeout on Chase from Edgeport, like wait longer in
1911 * block_until_chase_response, but for now we don't.
1912 */
1913 edge_port->chaseResponsePending = false;
1914 wake_up(&edge_port->wait_chase);
1915 return;
1da177e4 1916
03f0dbf7 1917 case IOSP_EXT_STATUS_RX_CHECK_RSP:
984f6868
GKH
1918 dev_dbg(dev, "%s ========== Port %u CHECK_RSP Sequence = %02x =============\n",
1919 __func__, edge_serial->rxPort, byte3);
03f0dbf7
AC
1920 /* Port->RxCheckRsp = true; */
1921 return;
1da177e4
LT
1922 }
1923 }
1924
1925 if (code == IOSP_STATUS_OPEN_RSP) {
1926 edge_port->txCredits = GET_TX_BUFFER_SIZE(byte3);
1927 edge_port->maxTxCredits = edge_port->txCredits;
984f6868
GKH
1928 dev_dbg(dev, "%s - Port %u Open Response Initial MSR = %02x TxBufferSize = %d\n",
1929 __func__, edge_serial->rxPort, byte2, edge_port->txCredits);
03f0dbf7 1930 handle_new_msr(edge_port, byte2);
1da177e4 1931
03f0dbf7
AC
1932 /* send the current line settings to the port so we are
1933 in sync with any further termios calls */
4a90f09b
AC
1934 tty = tty_port_tty_get(&edge_port->port->port);
1935 if (tty) {
1936 change_port_settings(tty,
adc8d746 1937 edge_port, &tty->termios);
4a90f09b
AC
1938 tty_kref_put(tty);
1939 }
1da177e4
LT
1940
1941 /* we have completed the open */
cb8eaa8b
RK
1942 edge_port->openPending = false;
1943 edge_port->open = true;
1da177e4
LT
1944 wake_up(&edge_port->wait_open);
1945 return;
1946 }
1947
03f0dbf7
AC
1948 /* If port is closed, silently discard all rcvd status. We can
1949 * have cases where buffered status is received AFTER the close
1950 * port command is sent to the Edgeport.
1951 */
1952 if (!edge_port->open || edge_port->closePending)
1da177e4 1953 return;
1da177e4
LT
1954
1955 switch (code) {
03f0dbf7
AC
1956 /* Not currently sent by Edgeport */
1957 case IOSP_STATUS_LSR:
984f6868
GKH
1958 dev_dbg(dev, "%s - Port %u LSR Status = %02x\n",
1959 __func__, edge_serial->rxPort, byte2);
03f0dbf7
AC
1960 handle_new_lsr(edge_port, false, byte2, 0);
1961 break;
1da177e4 1962
03f0dbf7 1963 case IOSP_STATUS_LSR_DATA:
984f6868
GKH
1964 dev_dbg(dev, "%s - Port %u LSR Status = %02x, Data = %02x\n",
1965 __func__, edge_serial->rxPort, byte2, byte3);
03f0dbf7
AC
1966 /* byte2 is LSR Register */
1967 /* byte3 is broken data byte */
1968 handle_new_lsr(edge_port, true, byte2, byte3);
1969 break;
1970 /*
1971 * case IOSP_EXT_4_STATUS:
984f6868 1972 * dev_dbg(dev, "%s - Port %u LSR Status = %02x Data = %02x\n",
03f0dbf7
AC
1973 * __func__, edge_serial->rxPort, byte2, byte3);
1974 * break;
1975 */
1976 case IOSP_STATUS_MSR:
984f6868
GKH
1977 dev_dbg(dev, "%s - Port %u MSR Status = %02x\n",
1978 __func__, edge_serial->rxPort, byte2);
03f0dbf7
AC
1979 /*
1980 * Process this new modem status and generate appropriate
1981 * events, etc, based on the new status. This routine
1982 * also saves the MSR in Port->ShadowMsr.
1983 */
1984 handle_new_msr(edge_port, byte2);
1985 break;
1da177e4 1986
03f0dbf7 1987 default:
984f6868 1988 dev_dbg(dev, "%s - Unrecognized IOSP status code %u\n", __func__, code);
03f0dbf7 1989 break;
1da177e4 1990 }
1da177e4
LT
1991}
1992
1993
1994/*****************************************************************************
1995 * edge_tty_recv
1996 * this function passes data on to the tty flip buffer
1997 *****************************************************************************/
2e124b4a
JS
1998static void edge_tty_recv(struct usb_serial_port *port, unsigned char *data,
1999 int length)
1da177e4
LT
2000{
2001 int cnt;
2002
05c7cd39 2003 cnt = tty_insert_flip_string(&port->port, data, length);
a108bfcb 2004 if (cnt < length) {
05c7cd39 2005 dev_err(&port->dev, "%s - dropping data, %d bytes lost\n",
a108bfcb
AC
2006 __func__, length - cnt);
2007 }
2008 data += cnt;
2009 length -= cnt;
1da177e4 2010
2e124b4a 2011 tty_flip_buffer_push(&port->port);
1da177e4
LT
2012}
2013
2014
2015/*****************************************************************************
2016 * handle_new_msr
2017 * this function handles any change to the msr register for a port.
2018 *****************************************************************************/
2019static void handle_new_msr(struct edgeport_port *edge_port, __u8 newMsr)
2020{
2021 struct async_icount *icount;
2022
03f0dbf7
AC
2023 if (newMsr & (EDGEPORT_MSR_DELTA_CTS | EDGEPORT_MSR_DELTA_DSR |
2024 EDGEPORT_MSR_DELTA_RI | EDGEPORT_MSR_DELTA_CD)) {
1da177e4
LT
2025 icount = &edge_port->icount;
2026
2027 /* update input line counters */
03f0dbf7 2028 if (newMsr & EDGEPORT_MSR_DELTA_CTS)
1da177e4 2029 icount->cts++;
03f0dbf7 2030 if (newMsr & EDGEPORT_MSR_DELTA_DSR)
1da177e4 2031 icount->dsr++;
03f0dbf7 2032 if (newMsr & EDGEPORT_MSR_DELTA_CD)
1da177e4 2033 icount->dcd++;
03f0dbf7 2034 if (newMsr & EDGEPORT_MSR_DELTA_RI)
1da177e4 2035 icount->rng++;
1da177e4
LT
2036 wake_up_interruptible(&edge_port->delta_msr_wait);
2037 }
2038
2039 /* Save the new modem status */
2040 edge_port->shadowMSR = newMsr & 0xf0;
1da177e4
LT
2041}
2042
2043
2044/*****************************************************************************
2045 * handle_new_lsr
2046 * this function handles any change to the lsr register for a port.
2047 *****************************************************************************/
03f0dbf7
AC
2048static void handle_new_lsr(struct edgeport_port *edge_port, __u8 lsrData,
2049 __u8 lsr, __u8 data)
1da177e4 2050{
03f0dbf7
AC
2051 __u8 newLsr = (__u8) (lsr & (__u8)
2052 (LSR_OVER_ERR | LSR_PAR_ERR | LSR_FRM_ERR | LSR_BREAK));
2053 struct async_icount *icount;
1da177e4 2054
1da177e4
LT
2055 edge_port->shadowLSR = lsr;
2056
2057 if (newLsr & LSR_BREAK) {
03f0dbf7
AC
2058 /*
2059 * Parity and Framing errors only count if they
2060 * occur exclusive of a break being
2061 * received.
2062 */
1da177e4
LT
2063 newLsr &= (__u8)(LSR_OVER_ERR | LSR_BREAK);
2064 }
2065
2066 /* Place LSR data byte into Rx buffer */
2e124b4a
JS
2067 if (lsrData)
2068 edge_tty_recv(edge_port->port, &data, 1);
2069
1da177e4
LT
2070 /* update input line counters */
2071 icount = &edge_port->icount;
03f0dbf7 2072 if (newLsr & LSR_BREAK)
1da177e4 2073 icount->brk++;
03f0dbf7 2074 if (newLsr & LSR_OVER_ERR)
1da177e4 2075 icount->overrun++;
03f0dbf7 2076 if (newLsr & LSR_PAR_ERR)
1da177e4 2077 icount->parity++;
03f0dbf7 2078 if (newLsr & LSR_FRM_ERR)
1da177e4 2079 icount->frame++;
1da177e4
LT
2080}
2081
2082
2083/****************************************************************************
2084 * sram_write
03f0dbf7 2085 * writes a number of bytes to the Edgeport device's sram starting at the
1da177e4
LT
2086 * given address.
2087 * If successful returns the number of bytes written, otherwise it returns
2088 * a negative error number of the problem.
2089 ****************************************************************************/
03f0dbf7
AC
2090static int sram_write(struct usb_serial *serial, __u16 extAddr, __u16 addr,
2091 __u16 length, const __u8 *data)
1da177e4
LT
2092{
2093 int result;
2094 __u16 current_length;
2095 unsigned char *transfer_buffer;
2096
984f6868 2097 dev_dbg(&serial->dev->dev, "%s - %x, %x, %d\n", __func__, extAddr, addr, length);
1da177e4 2098
03f0dbf7 2099 transfer_buffer = kmalloc(64, GFP_KERNEL);
1da177e4 2100 if (!transfer_buffer) {
03f0dbf7
AC
2101 dev_err(&serial->dev->dev, "%s - kmalloc(%d) failed.\n",
2102 __func__, 64);
1da177e4
LT
2103 return -ENOMEM;
2104 }
2105
2106 /* need to split these writes up into 64 byte chunks */
2107 result = 0;
2108 while (length > 0) {
03f0dbf7 2109 if (length > 64)
1da177e4 2110 current_length = 64;
03f0dbf7 2111 else
1da177e4 2112 current_length = length;
03f0dbf7 2113
984f6868 2114/* dev_dbg(&serial->dev->dev, "%s - writing %x, %x, %d\n", __func__, extAddr, addr, current_length); */
03f0dbf7
AC
2115 memcpy(transfer_buffer, data, current_length);
2116 result = usb_control_msg(serial->dev,
2117 usb_sndctrlpipe(serial->dev, 0),
2118 USB_REQUEST_ION_WRITE_RAM,
2119 0x40, addr, extAddr, transfer_buffer,
2120 current_length, 300);
1da177e4
LT
2121 if (result < 0)
2122 break;
2123 length -= current_length;
2124 addr += current_length;
2125 data += current_length;
03f0dbf7 2126 }
1da177e4 2127
03f0dbf7 2128 kfree(transfer_buffer);
1da177e4
LT
2129 return result;
2130}
2131
2132
2133/****************************************************************************
2134 * rom_write
2135 * writes a number of bytes to the Edgeport device's ROM starting at the
2136 * given address.
2137 * If successful returns the number of bytes written, otherwise it returns
2138 * a negative error number of the problem.
2139 ****************************************************************************/
03f0dbf7
AC
2140static int rom_write(struct usb_serial *serial, __u16 extAddr, __u16 addr,
2141 __u16 length, const __u8 *data)
1da177e4
LT
2142{
2143 int result;
2144 __u16 current_length;
2145 unsigned char *transfer_buffer;
2146
03f0dbf7 2147 transfer_buffer = kmalloc(64, GFP_KERNEL);
1da177e4 2148 if (!transfer_buffer) {
03f0dbf7
AC
2149 dev_err(&serial->dev->dev, "%s - kmalloc(%d) failed.\n",
2150 __func__, 64);
1da177e4
LT
2151 return -ENOMEM;
2152 }
2153
2154 /* need to split these writes up into 64 byte chunks */
2155 result = 0;
2156 while (length > 0) {
03f0dbf7 2157 if (length > 64)
1da177e4 2158 current_length = 64;
03f0dbf7 2159 else
1da177e4 2160 current_length = length;
03f0dbf7
AC
2161 memcpy(transfer_buffer, data, current_length);
2162 result = usb_control_msg(serial->dev,
2163 usb_sndctrlpipe(serial->dev, 0),
2164 USB_REQUEST_ION_WRITE_ROM, 0x40,
2165 addr, extAddr,
2166 transfer_buffer, current_length, 300);
1da177e4
LT
2167 if (result < 0)
2168 break;
2169 length -= current_length;
2170 addr += current_length;
2171 data += current_length;
03f0dbf7 2172 }
1da177e4 2173
03f0dbf7 2174 kfree(transfer_buffer);
1da177e4
LT
2175 return result;
2176}
2177
2178
2179/****************************************************************************
2180 * rom_read
2181 * reads a number of bytes from the Edgeport device starting at the given
2182 * address.
2183 * If successful returns the number of bytes read, otherwise it returns
2184 * a negative error number of the problem.
2185 ****************************************************************************/
03f0dbf7
AC
2186static int rom_read(struct usb_serial *serial, __u16 extAddr,
2187 __u16 addr, __u16 length, __u8 *data)
1da177e4
LT
2188{
2189 int result;
2190 __u16 current_length;
2191 unsigned char *transfer_buffer;
2192
03f0dbf7 2193 transfer_buffer = kmalloc(64, GFP_KERNEL);
1da177e4 2194 if (!transfer_buffer) {
03f0dbf7
AC
2195 dev_err(&serial->dev->dev,
2196 "%s - kmalloc(%d) failed.\n", __func__, 64);
1da177e4
LT
2197 return -ENOMEM;
2198 }
2199
2200 /* need to split these reads up into 64 byte chunks */
2201 result = 0;
2202 while (length > 0) {
03f0dbf7 2203 if (length > 64)
1da177e4 2204 current_length = 64;
03f0dbf7 2205 else
1da177e4 2206 current_length = length;
03f0dbf7
AC
2207 result = usb_control_msg(serial->dev,
2208 usb_rcvctrlpipe(serial->dev, 0),
2209 USB_REQUEST_ION_READ_ROM,
2210 0xC0, addr, extAddr, transfer_buffer,
2211 current_length, 300);
1da177e4
LT
2212 if (result < 0)
2213 break;
03f0dbf7 2214 memcpy(data, transfer_buffer, current_length);
1da177e4
LT
2215 length -= current_length;
2216 addr += current_length;
2217 data += current_length;
03f0dbf7 2218 }
1da177e4 2219
03f0dbf7 2220 kfree(transfer_buffer);
1da177e4
LT
2221 return result;
2222}
2223
2224
2225/****************************************************************************
2226 * send_iosp_ext_cmd
2227 * Is used to send a IOSP message to the Edgeport device
2228 ****************************************************************************/
03f0dbf7
AC
2229static int send_iosp_ext_cmd(struct edgeport_port *edge_port,
2230 __u8 command, __u8 param)
1da177e4
LT
2231{
2232 unsigned char *buffer;
2233 unsigned char *currentCommand;
2234 int length = 0;
2235 int status = 0;
2236
03f0dbf7 2237 buffer = kmalloc(10, GFP_ATOMIC);
1da177e4 2238 if (!buffer) {
03f0dbf7
AC
2239 dev_err(&edge_port->port->dev,
2240 "%s - kmalloc(%d) failed.\n", __func__, 10);
1da177e4
LT
2241 return -ENOMEM;
2242 }
2243
2244 currentCommand = buffer;
2245
03f0dbf7
AC
2246 MAKE_CMD_EXT_CMD(&currentCommand, &length,
2247 edge_port->port->number - edge_port->port->serial->minor,
2248 command, param);
1da177e4 2249
03f0dbf7 2250 status = write_cmd_usb(edge_port, buffer, length);
1da177e4
LT
2251 if (status) {
2252 /* something bad happened, let's free up the memory */
2253 kfree(buffer);
2254 }
2255
2256 return status;
2257}
2258
2259
2260/*****************************************************************************
2261 * write_cmd_usb
2262 * this function writes the given buffer out to the bulk write endpoint.
2263 *****************************************************************************/
03f0dbf7
AC
2264static int write_cmd_usb(struct edgeport_port *edge_port,
2265 unsigned char *buffer, int length)
1da177e4 2266{
03f0dbf7
AC
2267 struct edgeport_serial *edge_serial =
2268 usb_get_serial_data(edge_port->port->serial);
984f6868 2269 struct device *dev = &edge_port->port->dev;
1da177e4
LT
2270 int status = 0;
2271 struct urb *urb;
1da177e4 2272
59d33f2f 2273 usb_serial_debug_data(dev, __func__, length, buffer);
1da177e4
LT
2274
2275 /* Allocate our next urb */
03f0dbf7 2276 urb = usb_alloc_urb(0, GFP_ATOMIC);
1da177e4
LT
2277 if (!urb)
2278 return -ENOMEM;
2279
96c706ed 2280 atomic_inc(&CmdUrbs);
984f6868
GKH
2281 dev_dbg(dev, "%s - ALLOCATE URB %p (outstanding %d)\n",
2282 __func__, urb, atomic_read(&CmdUrbs));
1da177e4 2283
03f0dbf7
AC
2284 usb_fill_bulk_urb(urb, edge_serial->serial->dev,
2285 usb_sndbulkpipe(edge_serial->serial->dev,
2286 edge_serial->bulk_out_endpoint),
2287 buffer, length, edge_bulk_out_cmd_callback, edge_port);
1da177e4 2288
cb8eaa8b 2289 edge_port->commandPending = true;
1da177e4
LT
2290 status = usb_submit_urb(urb, GFP_ATOMIC);
2291
2292 if (status) {
2293 /* something went wrong */
984f6868
GKH
2294 dev_err(dev, "%s - usb_submit_urb(write command) failed, status = %d\n",
2295 __func__, status);
1da177e4
LT
2296 usb_kill_urb(urb);
2297 usb_free_urb(urb);
96c706ed 2298 atomic_dec(&CmdUrbs);
1da177e4
LT
2299 return status;
2300 }
2301
1da177e4 2302#if 0
03f0dbf7 2303 wait_event(&edge_port->wait_command, !edge_port->commandPending);
1da177e4 2304
cb8eaa8b 2305 if (edge_port->commandPending) {
1da177e4 2306 /* command timed out */
984f6868 2307 dev_dbg(dev, "%s - command timed out\n", __func__);
1da177e4
LT
2308 status = -EINVAL;
2309 }
2310#endif
2311 return status;
2312}
2313
2314
2315/*****************************************************************************
2316 * send_cmd_write_baud_rate
2317 * this function sends the proper command to change the baud rate of the
2318 * specified port.
2319 *****************************************************************************/
03f0dbf7
AC
2320static int send_cmd_write_baud_rate(struct edgeport_port *edge_port,
2321 int baudRate)
1da177e4 2322{
03f0dbf7
AC
2323 struct edgeport_serial *edge_serial =
2324 usb_get_serial_data(edge_port->port->serial);
984f6868 2325 struct device *dev = &edge_port->port->dev;
1da177e4
LT
2326 unsigned char *cmdBuffer;
2327 unsigned char *currCmd;
2328 int cmdLen = 0;
2329 int divisor;
2330 int status;
03f0dbf7
AC
2331 unsigned char number =
2332 edge_port->port->number - edge_port->port->serial->minor;
1da177e4 2333
bc71e479
AK
2334 if (edge_serial->is_epic &&
2335 !edge_serial->epic_descriptor.Supports.IOSPSetBaudRate) {
984f6868
GKH
2336 dev_dbg(dev, "SendCmdWriteBaudRate - NOT Setting baud rate for port = %d, baud = %d\n",
2337 edge_port->port->number, baudRate);
6e8cf775
GKH
2338 return 0;
2339 }
2340
984f6868
GKH
2341 dev_dbg(dev, "%s - port = %d, baud = %d\n", __func__,
2342 edge_port->port->number, baudRate);
1da177e4 2343
984f6868 2344 status = calc_baud_rate_divisor(dev, baudRate, &divisor);
1da177e4 2345 if (status) {
984f6868 2346 dev_err(dev, "%s - bad baud rate\n", __func__);
1da177e4
LT
2347 return status;
2348 }
2349
03f0dbf7
AC
2350 /* Alloc memory for the string of commands. */
2351 cmdBuffer = kmalloc(0x100, GFP_ATOMIC);
1da177e4 2352 if (!cmdBuffer) {
984f6868 2353 dev_err(dev, "%s - kmalloc(%d) failed.\n", __func__, 0x100);
1da177e4
LT
2354 return -ENOMEM;
2355 }
2356 currCmd = cmdBuffer;
2357
03f0dbf7
AC
2358 /* Enable access to divisor latch */
2359 MAKE_CMD_WRITE_REG(&currCmd, &cmdLen, number, LCR, LCR_DL_ENABLE);
1da177e4 2360
03f0dbf7
AC
2361 /* Write the divisor itself */
2362 MAKE_CMD_WRITE_REG(&currCmd, &cmdLen, number, DLL, LOW8(divisor));
2363 MAKE_CMD_WRITE_REG(&currCmd, &cmdLen, number, DLM, HIGH8(divisor));
1da177e4 2364
03f0dbf7
AC
2365 /* Restore original value to disable access to divisor latch */
2366 MAKE_CMD_WRITE_REG(&currCmd, &cmdLen, number, LCR,
2367 edge_port->shadowLCR);
1da177e4 2368
03f0dbf7 2369 status = write_cmd_usb(edge_port, cmdBuffer, cmdLen);
1da177e4
LT
2370 if (status) {
2371 /* something bad happened, let's free up the memory */
03f0dbf7 2372 kfree(cmdBuffer);
1da177e4
LT
2373 }
2374
2375 return status;
2376}
2377
2378
2379/*****************************************************************************
2380 * calc_baud_rate_divisor
2381 * this function calculates the proper baud rate divisor for the specified
2382 * baud rate.
2383 *****************************************************************************/
984f6868 2384static int calc_baud_rate_divisor(struct device *dev, int baudrate, int *divisor)
1da177e4
LT
2385{
2386 int i;
2387 __u16 custom;
2388
52950ed4 2389 for (i = 0; i < ARRAY_SIZE(divisor_table); i++) {
03f0dbf7 2390 if (divisor_table[i].BaudRate == baudrate) {
1da177e4
LT
2391 *divisor = divisor_table[i].Divisor;
2392 return 0;
2393 }
2394 }
2395
03f0dbf7
AC
2396 /* We have tried all of the standard baud rates
2397 * lets try to calculate the divisor for this baud rate
2398 * Make sure the baud rate is reasonable */
1da177e4 2399 if (baudrate > 50 && baudrate < 230400) {
03f0dbf7 2400 /* get divisor */
1da177e4
LT
2401 custom = (__u16)((230400L + baudrate/2) / baudrate);
2402
2403 *divisor = custom;
2404
984f6868 2405 dev_dbg(dev, "%s - Baud %d = %d\n", __func__, baudrate, custom);
1da177e4
LT
2406 return 0;
2407 }
2408
2409 return -1;
2410}
2411
2412
2413/*****************************************************************************
2414 * send_cmd_write_uart_register
fd589a8f 2415 * this function builds up a uart register message and sends to the device.
1da177e4 2416 *****************************************************************************/
03f0dbf7
AC
2417static int send_cmd_write_uart_register(struct edgeport_port *edge_port,
2418 __u8 regNum, __u8 regValue)
1da177e4 2419{
03f0dbf7
AC
2420 struct edgeport_serial *edge_serial =
2421 usb_get_serial_data(edge_port->port->serial);
984f6868 2422 struct device *dev = &edge_port->port->dev;
1da177e4
LT
2423 unsigned char *cmdBuffer;
2424 unsigned char *currCmd;
2425 unsigned long cmdLen = 0;
2426 int status;
2427
984f6868
GKH
2428 dev_dbg(dev, "%s - write to %s register 0x%02x\n",
2429 (regNum == MCR) ? "MCR" : "LCR", __func__, regValue);
1da177e4 2430
bc71e479
AK
2431 if (edge_serial->is_epic &&
2432 !edge_serial->epic_descriptor.Supports.IOSPWriteMCR &&
2433 regNum == MCR) {
984f6868 2434 dev_dbg(dev, "SendCmdWriteUartReg - Not writing to MCR Register\n");
6e8cf775
GKH
2435 return 0;
2436 }
2437
bc71e479
AK
2438 if (edge_serial->is_epic &&
2439 !edge_serial->epic_descriptor.Supports.IOSPWriteLCR &&
2440 regNum == LCR) {
984f6868 2441 dev_dbg(dev, "SendCmdWriteUartReg - Not writing to LCR Register\n");
6e8cf775
GKH
2442 return 0;
2443 }
2444
03f0dbf7
AC
2445 /* Alloc memory for the string of commands. */
2446 cmdBuffer = kmalloc(0x10, GFP_ATOMIC);
2447 if (cmdBuffer == NULL)
1da177e4 2448 return -ENOMEM;
1da177e4
LT
2449
2450 currCmd = cmdBuffer;
2451
03f0dbf7
AC
2452 /* Build a cmd in the buffer to write the given register */
2453 MAKE_CMD_WRITE_REG(&currCmd, &cmdLen,
2454 edge_port->port->number - edge_port->port->serial->minor,
2455 regNum, regValue);
1da177e4
LT
2456
2457 status = write_cmd_usb(edge_port, cmdBuffer, cmdLen);
2458 if (status) {
2459 /* something bad happened, let's free up the memory */
03f0dbf7 2460 kfree(cmdBuffer);
1da177e4
LT
2461 }
2462
2463 return status;
2464}
2465
2466
2467/*****************************************************************************
2468 * change_port_settings
03f0dbf7
AC
2469 * This routine is called to set the UART on the device to match the
2470 * specified new settings.
1da177e4 2471 *****************************************************************************/
95da310e
AC
2472
2473static void change_port_settings(struct tty_struct *tty,
2474 struct edgeport_port *edge_port, struct ktermios *old_termios)
1da177e4 2475{
984f6868 2476 struct device *dev = &edge_port->port->dev;
03f0dbf7
AC
2477 struct edgeport_serial *edge_serial =
2478 usb_get_serial_data(edge_port->port->serial);
1da177e4
LT
2479 int baud;
2480 unsigned cflag;
2481 __u8 mask = 0xff;
2482 __u8 lData;
2483 __u8 lParity;
2484 __u8 lStop;
2485 __u8 rxFlow;
2486 __u8 txFlow;
2487 int status;
2488
984f6868 2489 dev_dbg(dev, "%s - port %d\n", __func__, edge_port->port->number);
1da177e4 2490
cb8eaa8b
RK
2491 if (!edge_port->open &&
2492 !edge_port->openPending) {
984f6868 2493 dev_dbg(dev, "%s - port not opened\n", __func__);
1da177e4
LT
2494 return;
2495 }
2496
adc8d746 2497 cflag = tty->termios.c_cflag;
1da177e4
LT
2498
2499 switch (cflag & CSIZE) {
03f0dbf7
AC
2500 case CS5:
2501 lData = LCR_BITS_5; mask = 0x1f;
984f6868 2502 dev_dbg(dev, "%s - data bits = 5\n", __func__);
03f0dbf7
AC
2503 break;
2504 case CS6:
2505 lData = LCR_BITS_6; mask = 0x3f;
984f6868 2506 dev_dbg(dev, "%s - data bits = 6\n", __func__);
03f0dbf7
AC
2507 break;
2508 case CS7:
2509 lData = LCR_BITS_7; mask = 0x7f;
984f6868 2510 dev_dbg(dev, "%s - data bits = 7\n", __func__);
03f0dbf7
AC
2511 break;
2512 default:
2513 case CS8:
2514 lData = LCR_BITS_8;
984f6868 2515 dev_dbg(dev, "%s - data bits = 8\n", __func__);
03f0dbf7 2516 break;
1da177e4
LT
2517 }
2518
2519 lParity = LCR_PAR_NONE;
2520 if (cflag & PARENB) {
2521 if (cflag & CMSPAR) {
2522 if (cflag & PARODD) {
2523 lParity = LCR_PAR_MARK;
984f6868 2524 dev_dbg(dev, "%s - parity = mark\n", __func__);
1da177e4
LT
2525 } else {
2526 lParity = LCR_PAR_SPACE;
984f6868 2527 dev_dbg(dev, "%s - parity = space\n", __func__);
1da177e4
LT
2528 }
2529 } else if (cflag & PARODD) {
2530 lParity = LCR_PAR_ODD;
984f6868 2531 dev_dbg(dev, "%s - parity = odd\n", __func__);
1da177e4
LT
2532 } else {
2533 lParity = LCR_PAR_EVEN;
984f6868 2534 dev_dbg(dev, "%s - parity = even\n", __func__);
1da177e4
LT
2535 }
2536 } else {
984f6868 2537 dev_dbg(dev, "%s - parity = none\n", __func__);
1da177e4
LT
2538 }
2539
2540 if (cflag & CSTOPB) {
2541 lStop = LCR_STOP_2;
984f6868 2542 dev_dbg(dev, "%s - stop bits = 2\n", __func__);
1da177e4
LT
2543 } else {
2544 lStop = LCR_STOP_1;
984f6868 2545 dev_dbg(dev, "%s - stop bits = 1\n", __func__);
1da177e4
LT
2546 }
2547
2548 /* figure out the flow control settings */
2549 rxFlow = txFlow = 0x00;
2550 if (cflag & CRTSCTS) {
2551 rxFlow |= IOSP_RX_FLOW_RTS;
2552 txFlow |= IOSP_TX_FLOW_CTS;
984f6868 2553 dev_dbg(dev, "%s - RTS/CTS is enabled\n", __func__);
1da177e4 2554 } else {
984f6868 2555 dev_dbg(dev, "%s - RTS/CTS is disabled\n", __func__);
1da177e4
LT
2556 }
2557
03f0dbf7
AC
2558 /* if we are implementing XON/XOFF, set the start and stop character
2559 in the device */
1da177e4
LT
2560 if (I_IXOFF(tty) || I_IXON(tty)) {
2561 unsigned char stop_char = STOP_CHAR(tty);
2562 unsigned char start_char = START_CHAR(tty);
2563
6e8cf775
GKH
2564 if ((!edge_serial->is_epic) ||
2565 ((edge_serial->is_epic) &&
2566 (edge_serial->epic_descriptor.Supports.IOSPSetXChar))) {
03f0dbf7
AC
2567 send_iosp_ext_cmd(edge_port,
2568 IOSP_CMD_SET_XON_CHAR, start_char);
2569 send_iosp_ext_cmd(edge_port,
2570 IOSP_CMD_SET_XOFF_CHAR, stop_char);
6e8cf775 2571 }
1da177e4
LT
2572
2573 /* if we are implementing INBOUND XON/XOFF */
2574 if (I_IXOFF(tty)) {
2575 rxFlow |= IOSP_RX_FLOW_XON_XOFF;
984f6868
GKH
2576 dev_dbg(dev, "%s - INBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x\n",
2577 __func__, start_char, stop_char);
1da177e4 2578 } else {
984f6868 2579 dev_dbg(dev, "%s - INBOUND XON/XOFF is disabled\n", __func__);
1da177e4
LT
2580 }
2581
2582 /* if we are implementing OUTBOUND XON/XOFF */
2583 if (I_IXON(tty)) {
2584 txFlow |= IOSP_TX_FLOW_XON_XOFF;
984f6868
GKH
2585 dev_dbg(dev, "%s - OUTBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x\n",
2586 __func__, start_char, stop_char);
1da177e4 2587 } else {
984f6868 2588 dev_dbg(dev, "%s - OUTBOUND XON/XOFF is disabled\n", __func__);
1da177e4
LT
2589 }
2590 }
2591
2592 /* Set flow control to the configured value */
6e8cf775
GKH
2593 if ((!edge_serial->is_epic) ||
2594 ((edge_serial->is_epic) &&
2595 (edge_serial->epic_descriptor.Supports.IOSPSetRxFlow)))
2596 send_iosp_ext_cmd(edge_port, IOSP_CMD_SET_RX_FLOW, rxFlow);
2597 if ((!edge_serial->is_epic) ||
2598 ((edge_serial->is_epic) &&
2599 (edge_serial->epic_descriptor.Supports.IOSPSetTxFlow)))
2600 send_iosp_ext_cmd(edge_port, IOSP_CMD_SET_TX_FLOW, txFlow);
1da177e4
LT
2601
2602
2603 edge_port->shadowLCR &= ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
2604 edge_port->shadowLCR |= (lData | lParity | lStop);
2605
2606 edge_port->validDataMask = mask;
2607
2608 /* Send the updated LCR value to the EdgePort */
03f0dbf7
AC
2609 status = send_cmd_write_uart_register(edge_port, LCR,
2610 edge_port->shadowLCR);
2611 if (status != 0)
1da177e4 2612 return;
1da177e4
LT
2613
2614 /* set up the MCR register and send it to the EdgePort */
2615 edge_port->shadowMCR = MCR_MASTER_IE;
03f0dbf7 2616 if (cflag & CBAUD)
1da177e4 2617 edge_port->shadowMCR |= (MCR_DTR | MCR_RTS);
03f0dbf7
AC
2618
2619 status = send_cmd_write_uart_register(edge_port, MCR,
2620 edge_port->shadowMCR);
2621 if (status != 0)
1da177e4 2622 return;
1da177e4
LT
2623
2624 /* Determine divisor based on baud rate */
2625 baud = tty_get_baud_rate(tty);
2626 if (!baud) {
2627 /* pick a default, any default... */
2628 baud = 9600;
2629 }
2630
984f6868 2631 dev_dbg(dev, "%s - baud rate = %d\n", __func__, baud);
03f0dbf7 2632 status = send_cmd_write_baud_rate(edge_port, baud);
6ce073bd
AC
2633 if (status == -1) {
2634 /* Speed change was not possible - put back the old speed */
2635 baud = tty_termios_baud_rate(old_termios);
2636 tty_encode_baud_rate(tty, baud, baud);
2637 }
1da177e4
LT
2638}
2639
2640
2641/****************************************************************************
2642 * unicode_to_ascii
2643 * Turns a string from Unicode into ASCII.
2644 * Doesn't do a good job with any characters that are outside the normal
2645 * ASCII range, but it's only for debugging...
2646 * NOTE: expects the unicode in LE format
2647 ****************************************************************************/
03f0dbf7
AC
2648static void unicode_to_ascii(char *string, int buflen,
2649 __le16 *unicode, int unicode_size)
1da177e4
LT
2650{
2651 int i;
2652
ad93375a 2653 if (buflen <= 0) /* never happens, but... */
1da177e4 2654 return;
ad93375a 2655 --buflen; /* space for nul */
1da177e4 2656
ad93375a
PZ
2657 for (i = 0; i < unicode_size; i++) {
2658 if (i >= buflen)
2659 break;
1da177e4 2660 string[i] = (char)(le16_to_cpu(unicode[i]));
ad93375a
PZ
2661 }
2662 string[i] = 0x00;
1da177e4
LT
2663}
2664
2665
2666/****************************************************************************
2667 * get_manufacturing_desc
03f0dbf7 2668 * reads in the manufacturing descriptor and stores it into the serial
1da177e4
LT
2669 * structure.
2670 ****************************************************************************/
03f0dbf7 2671static void get_manufacturing_desc(struct edgeport_serial *edge_serial)
1da177e4 2672{
984f6868 2673 struct device *dev = &edge_serial->serial->dev->dev;
1da177e4
LT
2674 int response;
2675
984f6868 2676 dev_dbg(dev, "getting manufacturer descriptor\n");
1da177e4 2677
03f0dbf7
AC
2678 response = rom_read(edge_serial->serial,
2679 (EDGE_MANUF_DESC_ADDR & 0xffff0000) >> 16,
2680 (__u16)(EDGE_MANUF_DESC_ADDR & 0x0000ffff),
2681 EDGE_MANUF_DESC_LEN,
2682 (__u8 *)(&edge_serial->manuf_descriptor));
1da177e4 2683
03f0dbf7 2684 if (response < 1)
984f6868 2685 dev_err(dev, "error in getting manufacturer descriptor\n");
03f0dbf7 2686 else {
1da177e4 2687 char string[30];
984f6868
GKH
2688 dev_dbg(dev, "**Manufacturer Descriptor\n");
2689 dev_dbg(dev, " RomSize: %dK\n",
03f0dbf7 2690 edge_serial->manuf_descriptor.RomSize);
984f6868 2691 dev_dbg(dev, " RamSize: %dK\n",
03f0dbf7 2692 edge_serial->manuf_descriptor.RamSize);
984f6868 2693 dev_dbg(dev, " CpuRev: %d\n",
03f0dbf7 2694 edge_serial->manuf_descriptor.CpuRev);
984f6868 2695 dev_dbg(dev, " BoardRev: %d\n",
03f0dbf7 2696 edge_serial->manuf_descriptor.BoardRev);
984f6868 2697 dev_dbg(dev, " NumPorts: %d\n",
03f0dbf7 2698 edge_serial->manuf_descriptor.NumPorts);
984f6868 2699 dev_dbg(dev, " DescDate: %d/%d/%d\n",
03f0dbf7
AC
2700 edge_serial->manuf_descriptor.DescDate[0],
2701 edge_serial->manuf_descriptor.DescDate[1],
2702 edge_serial->manuf_descriptor.DescDate[2]+1900);
4bc203d9 2703 unicode_to_ascii(string, sizeof(string),
03f0dbf7
AC
2704 edge_serial->manuf_descriptor.SerialNumber,
2705 edge_serial->manuf_descriptor.SerNumLength/2);
984f6868 2706 dev_dbg(dev, " SerialNumber: %s\n", string);
4bc203d9 2707 unicode_to_ascii(string, sizeof(string),
03f0dbf7
AC
2708 edge_serial->manuf_descriptor.AssemblyNumber,
2709 edge_serial->manuf_descriptor.AssemblyNumLength/2);
984f6868 2710 dev_dbg(dev, " AssemblyNumber: %s\n", string);
4bc203d9 2711 unicode_to_ascii(string, sizeof(string),
ad93375a
PZ
2712 edge_serial->manuf_descriptor.OemAssyNumber,
2713 edge_serial->manuf_descriptor.OemAssyNumLength/2);
984f6868
GKH
2714 dev_dbg(dev, " OemAssyNumber: %s\n", string);
2715 dev_dbg(dev, " UartType: %d\n",
03f0dbf7 2716 edge_serial->manuf_descriptor.UartType);
984f6868 2717 dev_dbg(dev, " IonPid: %d\n",
03f0dbf7 2718 edge_serial->manuf_descriptor.IonPid);
984f6868 2719 dev_dbg(dev, " IonConfig: %d\n",
03f0dbf7 2720 edge_serial->manuf_descriptor.IonConfig);
1da177e4
LT
2721 }
2722}
2723
2724
2725/****************************************************************************
2726 * get_boot_desc
03f0dbf7 2727 * reads in the bootloader descriptor and stores it into the serial
1da177e4
LT
2728 * structure.
2729 ****************************************************************************/
03f0dbf7 2730static void get_boot_desc(struct edgeport_serial *edge_serial)
1da177e4 2731{
984f6868 2732 struct device *dev = &edge_serial->serial->dev->dev;
1da177e4
LT
2733 int response;
2734
984f6868 2735 dev_dbg(dev, "getting boot descriptor\n");
1da177e4 2736
03f0dbf7
AC
2737 response = rom_read(edge_serial->serial,
2738 (EDGE_BOOT_DESC_ADDR & 0xffff0000) >> 16,
2739 (__u16)(EDGE_BOOT_DESC_ADDR & 0x0000ffff),
2740 EDGE_BOOT_DESC_LEN,
2741 (__u8 *)(&edge_serial->boot_descriptor));
1da177e4 2742
03f0dbf7 2743 if (response < 1)
984f6868 2744 dev_err(dev, "error in getting boot descriptor\n");
03f0dbf7 2745 else {
984f6868
GKH
2746 dev_dbg(dev, "**Boot Descriptor:\n");
2747 dev_dbg(dev, " BootCodeLength: %d\n",
2748 le16_to_cpu(edge_serial->boot_descriptor.BootCodeLength));
2749 dev_dbg(dev, " MajorVersion: %d\n",
03f0dbf7 2750 edge_serial->boot_descriptor.MajorVersion);
984f6868 2751 dev_dbg(dev, " MinorVersion: %d\n",
03f0dbf7 2752 edge_serial->boot_descriptor.MinorVersion);
984f6868 2753 dev_dbg(dev, " BuildNumber: %d\n",
03f0dbf7 2754 le16_to_cpu(edge_serial->boot_descriptor.BuildNumber));
984f6868 2755 dev_dbg(dev, " Capabilities: 0x%x\n",
03f0dbf7 2756 le16_to_cpu(edge_serial->boot_descriptor.Capabilities));
984f6868 2757 dev_dbg(dev, " UConfig0: %d\n",
03f0dbf7 2758 edge_serial->boot_descriptor.UConfig0);
984f6868 2759 dev_dbg(dev, " UConfig1: %d\n",
03f0dbf7 2760 edge_serial->boot_descriptor.UConfig1);
1da177e4
LT
2761 }
2762}
2763
2764
2765/****************************************************************************
2766 * load_application_firmware
2767 * This is called to load the application firmware to the device
2768 ****************************************************************************/
03f0dbf7 2769static void load_application_firmware(struct edgeport_serial *edge_serial)
1da177e4 2770{
984f6868 2771 struct device *dev = &edge_serial->serial->dev->dev;
5b9ea932
JS
2772 const struct ihex_binrec *rec;
2773 const struct firmware *fw;
2774 const char *fw_name;
2775 const char *fw_info;
1da177e4 2776 int response;
5b9ea932
JS
2777 __u32 Operaddr;
2778 __u16 build;
1da177e4
LT
2779
2780 switch (edge_serial->product_info.iDownloadFile) {
2781 case EDGE_DOWNLOAD_FILE_I930:
5b9ea932
JS
2782 fw_info = "downloading firmware version (930)";
2783 fw_name = "edgeport/down.fw";
1da177e4
LT
2784 break;
2785
2786 case EDGE_DOWNLOAD_FILE_80251:
5b9ea932
JS
2787 fw_info = "downloading firmware version (80251)";
2788 fw_name = "edgeport/down2.fw";
1da177e4
LT
2789 break;
2790
2791 case EDGE_DOWNLOAD_FILE_NONE:
984f6868 2792 dev_dbg(dev, "No download file specified, skipping download\n");
1da177e4
LT
2793 return;
2794
2795 default:
2796 return;
2797 }
2798
5b9ea932
JS
2799 response = request_ihex_firmware(&fw, fw_name,
2800 &edge_serial->serial->dev->dev);
2801 if (response) {
984f6868 2802 dev_err(dev, "Failed to load image \"%s\" err %d\n",
5b9ea932
JS
2803 fw_name, response);
2804 return;
2805 }
2806
2807 rec = (const struct ihex_binrec *)fw->data;
2808 build = (rec->data[2] << 8) | rec->data[3];
2809
984f6868 2810 dev_dbg(dev, "%s %d.%d.%d\n", fw_info, rec->data[0], rec->data[1], build);
5b9ea932 2811
271c1150
BM
2812 edge_serial->product_info.FirmwareMajorVersion = rec->data[0];
2813 edge_serial->product_info.FirmwareMinorVersion = rec->data[1];
5b9ea932 2814 edge_serial->product_info.FirmwareBuildNumber = cpu_to_le16(build);
1da177e4 2815
5b9ea932
JS
2816 for (rec = ihex_next_binrec(rec); rec;
2817 rec = ihex_next_binrec(rec)) {
2818 Operaddr = be32_to_cpu(rec->addr);
2819 response = sram_write(edge_serial->serial,
2820 Operaddr >> 16,
2821 Operaddr & 0xFFFF,
2822 be16_to_cpu(rec->len),
2823 &rec->data[0]);
1da177e4 2824 if (response < 0) {
5b9ea932
JS
2825 dev_err(&edge_serial->serial->dev->dev,
2826 "sram_write failed (%x, %x, %d)\n",
2827 Operaddr >> 16, Operaddr & 0xFFFF,
2828 be16_to_cpu(rec->len));
1da177e4
LT
2829 break;
2830 }
2831 }
2832
984f6868
GKH
2833 dev_dbg(dev, "sending exec_dl_code\n");
2834 response = usb_control_msg (edge_serial->serial->dev,
2835 usb_sndctrlpipe(edge_serial->serial->dev, 0),
2836 USB_REQUEST_ION_EXEC_DL_CODE,
1da177e4
LT
2837 0x40, 0x4000, 0x0001, NULL, 0, 3000);
2838
5b9ea932 2839 release_firmware(fw);
1da177e4
LT
2840}
2841
2842
2843/****************************************************************************
2844 * edge_startup
2845 ****************************************************************************/
03f0dbf7 2846static int edge_startup(struct usb_serial *serial)
1da177e4
LT
2847{
2848 struct edgeport_serial *edge_serial;
1da177e4 2849 struct usb_device *dev;
984f6868 2850 struct device *ddev = &serial->dev->dev;
c27f3efc 2851 int i;
6e8cf775 2852 int response;
cb8eaa8b
RK
2853 bool interrupt_in_found;
2854 bool bulk_in_found;
2855 bool bulk_out_found;
6e8cf775
GKH
2856 static __u32 descriptor[3] = { EDGE_COMPATIBILITY_MASK0,
2857 EDGE_COMPATIBILITY_MASK1,
2858 EDGE_COMPATIBILITY_MASK2 };
1da177e4
LT
2859
2860 dev = serial->dev;
2861
2862 /* create our private serial structure */
80b6ca48 2863 edge_serial = kzalloc(sizeof(struct edgeport_serial), GFP_KERNEL);
1da177e4 2864 if (edge_serial == NULL) {
441b62c1 2865 dev_err(&serial->dev->dev, "%s - Out of memory\n", __func__);
1da177e4
LT
2866 return -ENOMEM;
2867 }
1da177e4
LT
2868 spin_lock_init(&edge_serial->es_lock);
2869 edge_serial->serial = serial;
2870 usb_set_serial_data(serial, edge_serial);
2871
2872 /* get the name for the device from the device */
f10718f5 2873 i = usb_string(dev, dev->descriptor.iManufacturer,
ad93375a 2874 &edge_serial->name[0], MAX_NAME_LEN+1);
f10718f5
DC
2875 if (i < 0)
2876 i = 0;
ad93375a 2877 edge_serial->name[i++] = ' ';
f10718f5 2878 usb_string(dev, dev->descriptor.iProduct,
ad93375a 2879 &edge_serial->name[i], MAX_NAME_LEN+2 - i);
1da177e4
LT
2880
2881 dev_info(&serial->dev->dev, "%s detected\n", edge_serial->name);
2882
6e8cf775
GKH
2883 /* Read the epic descriptor */
2884 if (get_epic_descriptor(edge_serial) <= 0) {
2885 /* memcpy descriptor to Supports structures */
2886 memcpy(&edge_serial->epic_descriptor.Supports, descriptor,
2887 sizeof(struct edge_compatibility_bits));
1da177e4 2888
6e8cf775 2889 /* get the manufacturing descriptor for this device */
03f0dbf7 2890 get_manufacturing_desc(edge_serial);
1da177e4 2891
6e8cf775 2892 /* get the boot descriptor */
03f0dbf7 2893 get_boot_desc(edge_serial);
6e8cf775
GKH
2894
2895 get_product_info(edge_serial);
2896 }
1da177e4
LT
2897
2898 /* set the number of ports from the manufacturing description */
2899 /* serial->num_ports = serial->product_info.NumPorts; */
6e8cf775
GKH
2900 if ((!edge_serial->is_epic) &&
2901 (edge_serial->product_info.NumPorts != serial->num_ports)) {
984f6868
GKH
2902 dev_warn(ddev,
2903 "Device Reported %d serial ports vs. core thinking we have %d ports, email greg@kroah.com this information.\n",
6e8cf775
GKH
2904 edge_serial->product_info.NumPorts,
2905 serial->num_ports);
1da177e4
LT
2906 }
2907
984f6868 2908 dev_dbg(ddev, "%s - time 1 %ld\n", __func__, jiffies);
1da177e4 2909
6e8cf775
GKH
2910 /* If not an EPiC device */
2911 if (!edge_serial->is_epic) {
2912 /* now load the application firmware into this device */
03f0dbf7 2913 load_application_firmware(edge_serial);
1da177e4 2914
984f6868 2915 dev_dbg(ddev, "%s - time 2 %ld\n", __func__, jiffies);
1da177e4 2916
6e8cf775 2917 /* Check current Edgeport EEPROM and update if necessary */
03f0dbf7 2918 update_edgeport_E2PROM(edge_serial);
1da177e4 2919
984f6868 2920 dev_dbg(ddev, "%s - time 3 %ld\n", __func__, jiffies);
6e8cf775
GKH
2921
2922 /* set the configuration to use #1 */
984f6868 2923/* dev_dbg(ddev, "set_configuration 1\n"); */
03f0dbf7 2924/* usb_set_configuration (dev, 1); */
6e8cf775 2925 }
984f6868 2926 dev_dbg(ddev, " FirmwareMajorVersion %d.%d.%d\n",
5b9ea932
JS
2927 edge_serial->product_info.FirmwareMajorVersion,
2928 edge_serial->product_info.FirmwareMinorVersion,
2929 le16_to_cpu(edge_serial->product_info.FirmwareBuildNumber));
1da177e4 2930
03f0dbf7 2931 /* we set up the pointers to the endpoints in the edge_open function,
1da177e4
LT
2932 * as the structures aren't created yet. */
2933
6e8cf775
GKH
2934 response = 0;
2935
2936 if (edge_serial->is_epic) {
03f0dbf7
AC
2937 /* EPIC thing, set up our interrupt polling now and our read
2938 * urb, so that the device knows it really is connected. */
cb8eaa8b 2939 interrupt_in_found = bulk_in_found = bulk_out_found = false;
03f0dbf7
AC
2940 for (i = 0; i < serial->interface->altsetting[0]
2941 .desc.bNumEndpoints; ++i) {
6e8cf775
GKH
2942 struct usb_endpoint_descriptor *endpoint;
2943 int buffer_size;
2944
03f0dbf7
AC
2945 endpoint = &serial->interface->altsetting[0].
2946 endpoint[i].desc;
29cc8897 2947 buffer_size = usb_endpoint_maxp(endpoint);
cb8eaa8b 2948 if (!interrupt_in_found &&
6e8cf775
GKH
2949 (usb_endpoint_is_int_in(endpoint))) {
2950 /* we found a interrupt in endpoint */
984f6868 2951 dev_dbg(ddev, "found interrupt in\n");
6e8cf775
GKH
2952
2953 /* not set up yet, so do it now */
03f0dbf7
AC
2954 edge_serial->interrupt_read_urb =
2955 usb_alloc_urb(0, GFP_KERNEL);
6e8cf775 2956 if (!edge_serial->interrupt_read_urb) {
984f6868 2957 dev_err(ddev, "out of memory\n");
6e8cf775
GKH
2958 return -ENOMEM;
2959 }
03f0dbf7
AC
2960 edge_serial->interrupt_in_buffer =
2961 kmalloc(buffer_size, GFP_KERNEL);
6e8cf775 2962 if (!edge_serial->interrupt_in_buffer) {
984f6868 2963 dev_err(ddev, "out of memory\n");
6e8cf775
GKH
2964 usb_free_urb(edge_serial->interrupt_read_urb);
2965 return -ENOMEM;
2966 }
03f0dbf7
AC
2967 edge_serial->interrupt_in_endpoint =
2968 endpoint->bEndpointAddress;
6e8cf775
GKH
2969
2970 /* set up our interrupt urb */
03f0dbf7
AC
2971 usb_fill_int_urb(
2972 edge_serial->interrupt_read_urb,
2973 dev,
2974 usb_rcvintpipe(dev,
2975 endpoint->bEndpointAddress),
2976 edge_serial->interrupt_in_buffer,
2977 buffer_size,
2978 edge_interrupt_callback,
2979 edge_serial,
2980 endpoint->bInterval);
6e8cf775 2981
cb8eaa8b 2982 interrupt_in_found = true;
6e8cf775
GKH
2983 }
2984
cb8eaa8b 2985 if (!bulk_in_found &&
03f0dbf7 2986 (usb_endpoint_is_bulk_in(endpoint))) {
6e8cf775 2987 /* we found a bulk in endpoint */
984f6868 2988 dev_dbg(ddev, "found bulk in\n");
6e8cf775
GKH
2989
2990 /* not set up yet, so do it now */
03f0dbf7
AC
2991 edge_serial->read_urb =
2992 usb_alloc_urb(0, GFP_KERNEL);
6e8cf775 2993 if (!edge_serial->read_urb) {
984f6868 2994 dev_err(ddev, "out of memory\n");
6e8cf775
GKH
2995 return -ENOMEM;
2996 }
03f0dbf7
AC
2997 edge_serial->bulk_in_buffer =
2998 kmalloc(buffer_size, GFP_KERNEL);
6e8cf775 2999 if (!edge_serial->bulk_in_buffer) {
194343d9 3000 dev_err(&dev->dev, "out of memory\n");
6e8cf775
GKH
3001 usb_free_urb(edge_serial->read_urb);
3002 return -ENOMEM;
3003 }
03f0dbf7
AC
3004 edge_serial->bulk_in_endpoint =
3005 endpoint->bEndpointAddress;
6e8cf775
GKH
3006
3007 /* set up our bulk in urb */
3008 usb_fill_bulk_urb(edge_serial->read_urb, dev,
03f0dbf7
AC
3009 usb_rcvbulkpipe(dev,
3010 endpoint->bEndpointAddress),
3011 edge_serial->bulk_in_buffer,
29cc8897 3012 usb_endpoint_maxp(endpoint),
03f0dbf7
AC
3013 edge_bulk_in_callback,
3014 edge_serial);
cb8eaa8b 3015 bulk_in_found = true;
6e8cf775
GKH
3016 }
3017
cb8eaa8b 3018 if (!bulk_out_found &&
6e8cf775
GKH
3019 (usb_endpoint_is_bulk_out(endpoint))) {
3020 /* we found a bulk out endpoint */
984f6868 3021 dev_dbg(ddev, "found bulk out\n");
03f0dbf7
AC
3022 edge_serial->bulk_out_endpoint =
3023 endpoint->bEndpointAddress;
cb8eaa8b 3024 bulk_out_found = true;
6e8cf775
GKH
3025 }
3026 }
3027
cb8eaa8b 3028 if (!interrupt_in_found || !bulk_in_found || !bulk_out_found) {
984f6868 3029 dev_err(ddev, "Error - the proper endpoints were not found!\n");
6e8cf775
GKH
3030 return -ENODEV;
3031 }
3032
3033 /* start interrupt read for this edgeport this interrupt will
3034 * continue as long as the edgeport is connected */
03f0dbf7
AC
3035 response = usb_submit_urb(edge_serial->interrupt_read_urb,
3036 GFP_KERNEL);
6e8cf775 3037 if (response)
984f6868 3038 dev_err(ddev, "%s - Error %d submitting control urb\n",
194343d9 3039 __func__, response);
6e8cf775
GKH
3040 }
3041 return response;
1da177e4
LT
3042}
3043
3044
3045/****************************************************************************
f9c99bb8 3046 * edge_disconnect
1da177e4
LT
3047 * This function is called whenever the device is removed from the usb bus.
3048 ****************************************************************************/
f9c99bb8 3049static void edge_disconnect(struct usb_serial *serial)
1da177e4 3050{
6e8cf775 3051 struct edgeport_serial *edge_serial = usb_get_serial_data(serial);
1da177e4 3052
1da177e4 3053 /* stop reads and writes on all ports */
6e8cf775
GKH
3054 /* free up our endpoint stuff */
3055 if (edge_serial->is_epic) {
74ac07e8 3056 usb_kill_urb(edge_serial->interrupt_read_urb);
6e8cf775
GKH
3057 usb_free_urb(edge_serial->interrupt_read_urb);
3058 kfree(edge_serial->interrupt_in_buffer);
3059
74ac07e8 3060 usb_kill_urb(edge_serial->read_urb);
6e8cf775
GKH
3061 usb_free_urb(edge_serial->read_urb);
3062 kfree(edge_serial->bulk_in_buffer);
3063 }
f9c99bb8
AS
3064}
3065
3066
3067/****************************************************************************
3068 * edge_release
3069 * This function is called when the device structure is deallocated.
3070 ****************************************************************************/
3071static void edge_release(struct usb_serial *serial)
3072{
3073 struct edgeport_serial *edge_serial = usb_get_serial_data(serial);
6e8cf775
GKH
3074
3075 kfree(edge_serial);
1da177e4
LT
3076}
3077
c27f3efc
JH
3078static int edge_port_probe(struct usb_serial_port *port)
3079{
3080 struct edgeport_port *edge_port;
3081
3082 edge_port = kzalloc(sizeof(*edge_port), GFP_KERNEL);
3083 if (!edge_port)
3084 return -ENOMEM;
3085
3086 spin_lock_init(&edge_port->ep_lock);
3087 edge_port->port = port;
3088
3089 usb_set_serial_port_data(port, edge_port);
3090
3091 return 0;
3092}
3093
3094static int edge_port_remove(struct usb_serial_port *port)
3095{
3096 struct edgeport_port *edge_port;
3097
3098 edge_port = usb_get_serial_port_data(port);
3099 kfree(edge_port);
3100
3101 return 0;
3102}
3103
68e24113 3104module_usb_serial_driver(serial_drivers, id_table_combined);
1da177e4 3105
03f0dbf7
AC
3106MODULE_AUTHOR(DRIVER_AUTHOR);
3107MODULE_DESCRIPTION(DRIVER_DESC);
1da177e4 3108MODULE_LICENSE("GPL");
5b9ea932
JS
3109MODULE_FIRMWARE("edgeport/boot.fw");
3110MODULE_FIRMWARE("edgeport/boot2.fw");
3111MODULE_FIRMWARE("edgeport/down.fw");
3112MODULE_FIRMWARE("edgeport/down2.fw");