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