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