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