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