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