blob: 2b676732e9fa419fc90513afbe5f8a4af235728b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 */
31
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#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>
45#include <asm/uaccess.h>
46#include <linux/usb.h>
Greg Kroah-Hartmana9698882006-07-11 21:22:58 -070047#include <linux/usb/serial.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include "io_edgeport.h"
49#include "io_ionsp.h" /* info for the iosp messages */
50#include "io_16654.h" /* 16654 UART defines */
51
52/*
53 * Version Information
54 */
55#define DRIVER_VERSION "v2.7"
56#define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com> and David Iacovelli"
57#define DRIVER_DESC "Edgeport USB Serial Driver"
58
59/* First, the latest boot code - for first generation edgeports */
60#define IMAGE_ARRAY_NAME BootCodeImage_GEN1
61#define IMAGE_VERSION_NAME BootCodeImageVersion_GEN1
62#include "io_fw_boot.h" /* the bootloader firmware to download to a device, if it needs it */
63
64/* for second generation edgeports */
65#define IMAGE_ARRAY_NAME BootCodeImage_GEN2
66#define IMAGE_VERSION_NAME BootCodeImageVersion_GEN2
67#include "io_fw_boot2.h" /* the bootloader firmware to download to a device, if it needs it */
68
69/* Then finally the main run-time operational code - for first generation edgeports */
70#define IMAGE_ARRAY_NAME OperationalCodeImage_GEN1
71#define IMAGE_VERSION_NAME OperationalCodeImageVersion_GEN1
72#include "io_fw_down.h" /* Define array OperationalCodeImage[] */
73
74/* for second generation edgeports */
75#define IMAGE_ARRAY_NAME OperationalCodeImage_GEN2
76#define IMAGE_VERSION_NAME OperationalCodeImageVersion_GEN2
77#include "io_fw_down2.h" /* Define array OperationalCodeImage[] */
78
79#define MAX_NAME_LEN 64
80
81#define CHASE_TIMEOUT (5*HZ) /* 5 seconds */
82#define OPEN_TIMEOUT (5*HZ) /* 5 seconds */
83#define COMMAND_TIMEOUT (5*HZ) /* 5 seconds */
84
85/* receive port state */
86enum RXSTATE {
87 EXPECT_HDR1 = 0, /* Expect header byte 1 */
88 EXPECT_HDR2 = 1, /* Expect header byte 2 */
89 EXPECT_DATA = 2, /* Expect 'RxBytesRemaining' data */
90 EXPECT_HDR3 = 3, /* Expect header byte 3 (for status hdrs only) */
91};
92
93
94/* Transmit Fifo
95 * This Transmit queue is an extension of the edgeport Rx buffer.
96 * The maximum amount of data buffered in both the edgeport
97 * Rx buffer (maxTxCredits) and this buffer will never exceed maxTxCredits.
98 */
99struct TxFifo {
100 unsigned int head; /* index to head pointer (write) */
101 unsigned int tail; /* index to tail pointer (read) */
102 unsigned int count; /* Bytes in queue */
103 unsigned int size; /* Max size of queue (equal to Max number of TxCredits) */
104 unsigned char *fifo; /* allocated Buffer */
105};
106
107/* This structure holds all of the local port information */
108struct edgeport_port {
109 __u16 txCredits; /* our current credits for this port */
110 __u16 maxTxCredits; /* the max size of the port */
111
112 struct TxFifo txfifo; /* transmit fifo -- size will be maxTxCredits */
113 struct urb *write_urb; /* write URB for this port */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100114 bool write_in_progress; /* 'true' while a write URB is outstanding */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 spinlock_t ep_lock;
116
117 __u8 shadowLCR; /* last LCR value received */
118 __u8 shadowMCR; /* last MCR value received */
119 __u8 shadowMSR; /* last MSR value received */
120 __u8 shadowLSR; /* last LSR value received */
121 __u8 shadowXonChar; /* last value set as XON char in Edgeport */
122 __u8 shadowXoffChar; /* last value set as XOFF char in Edgeport */
123 __u8 validDataMask;
124 __u32 baudRate;
125
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100126 bool open;
127 bool openPending;
128 bool commandPending;
129 bool closePending;
130 bool chaseResponsePending;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
132 wait_queue_head_t wait_chase; /* for handling sleeping while waiting for chase to finish */
133 wait_queue_head_t wait_open; /* for handling sleeping while waiting for open to finish */
134 wait_queue_head_t wait_command; /* for handling sleeping while waiting for command to finish */
135 wait_queue_head_t delta_msr_wait; /* for handling sleeping while waiting for msr change to happen */
136
137 struct async_icount icount;
138 struct usb_serial_port *port; /* loop back to the owner of this object */
139};
140
141
142/* This structure holds all of the individual device information */
143struct edgeport_serial {
Pete Zaitcevad933752006-05-22 21:49:44 -0700144 char name[MAX_NAME_LEN+2]; /* string name of this device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
146 struct edge_manuf_descriptor manuf_descriptor; /* the manufacturer descriptor */
147 struct edge_boot_descriptor boot_descriptor; /* the boot firmware descriptor */
148 struct edgeport_product_info product_info; /* Product Info */
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -0800149 struct edge_compatibility_descriptor epic_descriptor; /* Edgeport compatible descriptor */
150 int is_epic; /* flag if EPiC device or not */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
152 __u8 interrupt_in_endpoint; /* the interrupt endpoint handle */
153 unsigned char * interrupt_in_buffer; /* the buffer we use for the interrupt endpoint */
154 struct urb * interrupt_read_urb; /* our interrupt urb */
155
156 __u8 bulk_in_endpoint; /* the bulk in endpoint handle */
157 unsigned char * bulk_in_buffer; /* the buffer we use for the bulk in endpoint */
158 struct urb * read_urb; /* our bulk read urb */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100159 bool read_in_progress;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 spinlock_t es_lock;
161
162 __u8 bulk_out_endpoint; /* the bulk out endpoint handle */
163
164 __s16 rxBytesAvail; /* the number of bytes that we need to read from this device */
165
166 enum RXSTATE rxState; /* the current state of the bulk receive processor */
167 __u8 rxHeader1; /* receive header byte 1 */
168 __u8 rxHeader2; /* receive header byte 2 */
169 __u8 rxHeader3; /* receive header byte 3 */
170 __u8 rxPort; /* the port that we are currently receiving data for */
171 __u8 rxStatusCode; /* the receive status code */
172 __u8 rxStatusParam; /* the receive status paramater */
173 __s16 rxBytesRemaining; /* the number of port bytes left to read */
174 struct usb_serial *serial; /* loop back to the owner of this object */
175};
176
177/* baud rate information */
178struct divisor_table_entry {
179 __u32 BaudRate;
180 __u16 Divisor;
181};
182
183//
184// Define table of divisors for Rev A EdgePort/4 hardware
185// These assume a 3.6864MHz crystal, the standard /16, and
186// MCR.7 = 0.
187//
Arjan van de Ven4c4c9432005-11-29 09:43:42 +0100188static const struct divisor_table_entry divisor_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 { 50, 4608},
190 { 75, 3072},
191 { 110, 2095}, /* 2094.545455 => 230450 => .0217 % over */
192 { 134, 1713}, /* 1713.011152 => 230398.5 => .00065% under */
193 { 150, 1536},
194 { 300, 768},
195 { 600, 384},
196 { 1200, 192},
197 { 1800, 128},
198 { 2400, 96},
199 { 4800, 48},
200 { 7200, 32},
201 { 9600, 24},
202 { 14400, 16},
203 { 19200, 12},
204 { 38400, 6},
205 { 57600, 4},
206 { 115200, 2},
207 { 230400, 1},
208};
209
210/* local variables */
211static int debug;
212
213static int low_latency = 1; /* tty low latency flag, on by default */
214
Oliver Neukum96c706e2007-03-15 15:27:17 +0100215static atomic_t CmdUrbs; /* Number of outstanding Command Write Urbs */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
217
218/* local function prototypes */
219
220/* function prototypes for all URB callbacks */
David Howells7d12e782006-10-05 14:55:46 +0100221static void edge_interrupt_callback (struct urb *urb);
222static void edge_bulk_in_callback (struct urb *urb);
223static void edge_bulk_out_data_callback (struct urb *urb);
224static void edge_bulk_out_cmd_callback (struct urb *urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
226/* function prototypes for the usbserial callbacks */
227static int edge_open (struct usb_serial_port *port, struct file *filp);
228static void edge_close (struct usb_serial_port *port, struct file *filp);
229static int edge_write (struct usb_serial_port *port, const unsigned char *buf, int count);
230static int edge_write_room (struct usb_serial_port *port);
231static int edge_chars_in_buffer (struct usb_serial_port *port);
232static void edge_throttle (struct usb_serial_port *port);
233static void edge_unthrottle (struct usb_serial_port *port);
Alan Cox606d0992006-12-08 02:38:45 -0800234static void edge_set_termios (struct usb_serial_port *port, struct ktermios *old_termios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235static int edge_ioctl (struct usb_serial_port *port, struct file *file, unsigned int cmd, unsigned long arg);
236static void edge_break (struct usb_serial_port *port, int break_state);
237static int edge_tiocmget (struct usb_serial_port *port, struct file *file);
238static int edge_tiocmset (struct usb_serial_port *port, struct file *file, unsigned int set, unsigned int clear);
239static int edge_startup (struct usb_serial *serial);
240static void edge_shutdown (struct usb_serial *serial);
241
242
243#include "io_tables.h" /* all of the devices that this driver supports */
244
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245/* function prototypes for all of our local functions */
246static void process_rcvd_data (struct edgeport_serial *edge_serial, unsigned char *buffer, __u16 bufferLength);
247static void process_rcvd_status (struct edgeport_serial *edge_serial, __u8 byte2, __u8 byte3);
248static void edge_tty_recv (struct device *dev, struct tty_struct *tty, unsigned char *data, int length);
249static void handle_new_msr (struct edgeport_port *edge_port, __u8 newMsr);
250static void handle_new_lsr (struct edgeport_port *edge_port, __u8 lsrData, __u8 lsr, __u8 data);
251static int send_iosp_ext_cmd (struct edgeport_port *edge_port, __u8 command, __u8 param);
252static int calc_baud_rate_divisor (int baud_rate, int *divisor);
253static int send_cmd_write_baud_rate (struct edgeport_port *edge_port, int baudRate);
Alan Cox606d0992006-12-08 02:38:45 -0800254static void change_port_settings (struct edgeport_port *edge_port, struct ktermios *old_termios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255static int send_cmd_write_uart_register (struct edgeport_port *edge_port, __u8 regNum, __u8 regValue);
256static int write_cmd_usb (struct edgeport_port *edge_port, unsigned char *buffer, int writeLength);
257static void send_more_port_data (struct edgeport_serial *edge_serial, struct edgeport_port *edge_port);
258
259static int sram_write (struct usb_serial *serial, __u16 extAddr, __u16 addr, __u16 length, __u8 *data);
260static int rom_read (struct usb_serial *serial, __u16 extAddr, __u16 addr, __u16 length, __u8 *data);
261static int rom_write (struct usb_serial *serial, __u16 extAddr, __u16 addr, __u16 length, __u8 *data);
262static void get_manufacturing_desc (struct edgeport_serial *edge_serial);
263static void get_boot_desc (struct edgeport_serial *edge_serial);
264static void load_application_firmware (struct edgeport_serial *edge_serial);
265
Pete Zaitcevad933752006-05-22 21:49:44 -0700266static void unicode_to_ascii(char *string, int buflen, __le16 *unicode, int unicode_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
268
269// ************************************************************************
270// ************************************************************************
271// ************************************************************************
272// ************************************************************************
273
274/************************************************************************
275 * *
276 * update_edgeport_E2PROM() Compare current versions of *
277 * Boot ROM and Manufacture *
278 * Descriptors with versions *
279 * embedded in this driver *
280 * *
281 ************************************************************************/
282static void update_edgeport_E2PROM (struct edgeport_serial *edge_serial)
283{
284 __u32 BootCurVer;
285 __u32 BootNewVer;
286 __u8 BootMajorVersion;
287 __u8 BootMinorVersion;
288 __le16 BootBuildNumber;
289 __u8 *BootImage;
290 __u32 BootSize;
291 struct edge_firmware_image_record *record;
292 unsigned char *firmware;
293 int response;
294
295
296 switch (edge_serial->product_info.iDownloadFile) {
297 case EDGE_DOWNLOAD_FILE_I930:
298 BootMajorVersion = BootCodeImageVersion_GEN1.MajorVersion;
299 BootMinorVersion = BootCodeImageVersion_GEN1.MinorVersion;
300 BootBuildNumber = cpu_to_le16(BootCodeImageVersion_GEN1.BuildNumber);
301 BootImage = &BootCodeImage_GEN1[0];
302 BootSize = sizeof( BootCodeImage_GEN1 );
303 break;
304
305 case EDGE_DOWNLOAD_FILE_80251:
306 BootMajorVersion = BootCodeImageVersion_GEN2.MajorVersion;
307 BootMinorVersion = BootCodeImageVersion_GEN2.MinorVersion;
308 BootBuildNumber = cpu_to_le16(BootCodeImageVersion_GEN2.BuildNumber);
309 BootImage = &BootCodeImage_GEN2[0];
310 BootSize = sizeof( BootCodeImage_GEN2 );
311 break;
312
313 default:
314 return;
315 }
316
317 // Check Boot Image Version
318 BootCurVer = (edge_serial->boot_descriptor.MajorVersion << 24) +
319 (edge_serial->boot_descriptor.MinorVersion << 16) +
320 le16_to_cpu(edge_serial->boot_descriptor.BuildNumber);
321
322 BootNewVer = (BootMajorVersion << 24) +
323 (BootMinorVersion << 16) +
324 le16_to_cpu(BootBuildNumber);
325
326 dbg("Current Boot Image version %d.%d.%d",
327 edge_serial->boot_descriptor.MajorVersion,
328 edge_serial->boot_descriptor.MinorVersion,
329 le16_to_cpu(edge_serial->boot_descriptor.BuildNumber));
330
331
332 if (BootNewVer > BootCurVer) {
333 dbg("**Update Boot Image from %d.%d.%d to %d.%d.%d",
334 edge_serial->boot_descriptor.MajorVersion,
335 edge_serial->boot_descriptor.MinorVersion,
336 le16_to_cpu(edge_serial->boot_descriptor.BuildNumber),
337 BootMajorVersion,
338 BootMinorVersion,
339 le16_to_cpu(BootBuildNumber));
340
341
342 dbg("Downloading new Boot Image");
343
344 firmware = BootImage;
345
346 for (;;) {
347 record = (struct edge_firmware_image_record *)firmware;
348 response = rom_write (edge_serial->serial, le16_to_cpu(record->ExtAddr), le16_to_cpu(record->Addr), le16_to_cpu(record->Len), &record->Data[0]);
349 if (response < 0) {
350 dev_err(&edge_serial->serial->dev->dev, "rom_write failed (%x, %x, %d)\n", le16_to_cpu(record->ExtAddr), le16_to_cpu(record->Addr), le16_to_cpu(record->Len));
351 break;
352 }
353 firmware += sizeof (struct edge_firmware_image_record) + le16_to_cpu(record->Len);
354 if (firmware >= &BootImage[BootSize]) {
355 break;
356 }
357 }
358 } else {
359 dbg("Boot Image -- already up to date");
360 }
361}
362
363
364/************************************************************************
365 * *
366 * Get string descriptor from device *
367 * *
368 ************************************************************************/
Pete Zaitcevad933752006-05-22 21:49:44 -0700369static int get_string (struct usb_device *dev, int Id, char *string, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370{
371 struct usb_string_descriptor StringDesc;
372 struct usb_string_descriptor *pStringDesc;
373
Harvey Harrison441b62c2008-03-03 16:08:34 -0800374 dbg("%s - USB String ID = %d", __func__, Id );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
376 if (!usb_get_descriptor(dev, USB_DT_STRING, Id, &StringDesc, sizeof(StringDesc))) {
377 return 0;
378 }
379
380 pStringDesc = kmalloc (StringDesc.bLength, GFP_KERNEL);
381
382 if (!pStringDesc) {
383 return 0;
384 }
385
386 if (!usb_get_descriptor(dev, USB_DT_STRING, Id, pStringDesc, StringDesc.bLength )) {
387 kfree(pStringDesc);
388 return 0;
389 }
390
Pete Zaitcevad933752006-05-22 21:49:44 -0700391 unicode_to_ascii(string, buflen, pStringDesc->wData, pStringDesc->bLength/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
393 kfree(pStringDesc);
Harvey Harrison441b62c2008-03-03 16:08:34 -0800394 dbg("%s - USB String %s", __func__, string);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 return strlen(string);
396}
397
398
399#if 0
400/************************************************************************
401 *
402 * Get string descriptor from device
403 *
404 ************************************************************************/
405static int get_string_desc (struct usb_device *dev, int Id, struct usb_string_descriptor **pRetDesc)
406{
407 struct usb_string_descriptor StringDesc;
408 struct usb_string_descriptor *pStringDesc;
409
Harvey Harrison441b62c2008-03-03 16:08:34 -0800410 dbg("%s - USB String ID = %d", __func__, Id );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
412 if (!usb_get_descriptor(dev, USB_DT_STRING, Id, &StringDesc, sizeof(StringDesc))) {
413 return 0;
414 }
415
416 pStringDesc = kmalloc (StringDesc.bLength, GFP_KERNEL);
417
418 if (!pStringDesc) {
419 return -1;
420 }
421
422 if (!usb_get_descriptor(dev, USB_DT_STRING, Id, pStringDesc, StringDesc.bLength )) {
423 kfree(pStringDesc);
424 return -1;
425 }
426
427 *pRetDesc = pStringDesc;
428 return 0;
429}
430#endif
431
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -0800432static void dump_product_info(struct edgeport_product_info *product_info)
433{
434 // Dump Product Info structure
435 dbg("**Product Information:");
436 dbg(" ProductId %x", product_info->ProductId );
437 dbg(" NumPorts %d", product_info->NumPorts );
438 dbg(" ProdInfoVer %d", product_info->ProdInfoVer );
439 dbg(" IsServer %d", product_info->IsServer);
440 dbg(" IsRS232 %d", product_info->IsRS232 );
441 dbg(" IsRS422 %d", product_info->IsRS422 );
442 dbg(" IsRS485 %d", product_info->IsRS485 );
443 dbg(" RomSize %d", product_info->RomSize );
444 dbg(" RamSize %d", product_info->RamSize );
445 dbg(" CpuRev %x", product_info->CpuRev );
446 dbg(" BoardRev %x", product_info->BoardRev);
447 dbg(" BootMajorVersion %d.%d.%d", product_info->BootMajorVersion,
448 product_info->BootMinorVersion,
449 le16_to_cpu(product_info->BootBuildNumber));
450 dbg(" FirmwareMajorVersion %d.%d.%d", product_info->FirmwareMajorVersion,
451 product_info->FirmwareMinorVersion,
452 le16_to_cpu(product_info->FirmwareBuildNumber));
453 dbg(" ManufactureDescDate %d/%d/%d", product_info->ManufactureDescDate[0],
454 product_info->ManufactureDescDate[1],
455 product_info->ManufactureDescDate[2]+1900);
456 dbg(" iDownloadFile 0x%x", product_info->iDownloadFile);
457 dbg(" EpicVer %d", product_info->EpicVer);
458}
459
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460static void get_product_info(struct edgeport_serial *edge_serial)
461{
462 struct edgeport_product_info *product_info = &edge_serial->product_info;
463
464 memset (product_info, 0, sizeof(struct edgeport_product_info));
465
466 product_info->ProductId = (__u16)(le16_to_cpu(edge_serial->serial->dev->descriptor.idProduct) & ~ION_DEVICE_ID_80251_NETCHIP);
467 product_info->NumPorts = edge_serial->manuf_descriptor.NumPorts;
468 product_info->ProdInfoVer = 0;
469
470 product_info->RomSize = edge_serial->manuf_descriptor.RomSize;
471 product_info->RamSize = edge_serial->manuf_descriptor.RamSize;
472 product_info->CpuRev = edge_serial->manuf_descriptor.CpuRev;
473 product_info->BoardRev = edge_serial->manuf_descriptor.BoardRev;
474
475 product_info->BootMajorVersion = edge_serial->boot_descriptor.MajorVersion;
476 product_info->BootMinorVersion = edge_serial->boot_descriptor.MinorVersion;
477 product_info->BootBuildNumber = edge_serial->boot_descriptor.BuildNumber;
478
479 memcpy(product_info->ManufactureDescDate, edge_serial->manuf_descriptor.DescDate, sizeof(edge_serial->manuf_descriptor.DescDate));
480
481 // check if this is 2nd generation hardware
482 if (le16_to_cpu(edge_serial->serial->dev->descriptor.idProduct) & ION_DEVICE_ID_80251_NETCHIP) {
483 product_info->FirmwareMajorVersion = OperationalCodeImageVersion_GEN2.MajorVersion;
484 product_info->FirmwareMinorVersion = OperationalCodeImageVersion_GEN2.MinorVersion;
485 product_info->FirmwareBuildNumber = cpu_to_le16(OperationalCodeImageVersion_GEN2.BuildNumber);
486 product_info->iDownloadFile = EDGE_DOWNLOAD_FILE_80251;
487 } else {
488 product_info->FirmwareMajorVersion = OperationalCodeImageVersion_GEN1.MajorVersion;
489 product_info->FirmwareMinorVersion = OperationalCodeImageVersion_GEN1.MinorVersion;
490 product_info->FirmwareBuildNumber = cpu_to_le16(OperationalCodeImageVersion_GEN1.BuildNumber);
491 product_info->iDownloadFile = EDGE_DOWNLOAD_FILE_I930;
492 }
493
494 // Determine Product type and set appropriate flags
495 switch (DEVICE_ID_FROM_USB_PRODUCT_ID(product_info->ProductId)) {
496 case ION_DEVICE_ID_EDGEPORT_COMPATIBLE:
497 case ION_DEVICE_ID_EDGEPORT_4T:
498 case ION_DEVICE_ID_EDGEPORT_4:
499 case ION_DEVICE_ID_EDGEPORT_2:
500 case ION_DEVICE_ID_EDGEPORT_8_DUAL_CPU:
501 case ION_DEVICE_ID_EDGEPORT_8:
502 case ION_DEVICE_ID_EDGEPORT_421:
503 case ION_DEVICE_ID_EDGEPORT_21:
504 case ION_DEVICE_ID_EDGEPORT_2_DIN:
505 case ION_DEVICE_ID_EDGEPORT_4_DIN:
506 case ION_DEVICE_ID_EDGEPORT_16_DUAL_CPU:
507 product_info->IsRS232 = 1;
508 break;
509
510 case ION_DEVICE_ID_EDGEPORT_2I: // Edgeport/2 RS422/RS485
511 product_info->IsRS422 = 1;
512 product_info->IsRS485 = 1;
513 break;
514
515 case ION_DEVICE_ID_EDGEPORT_8I: // Edgeport/4 RS422
516 case ION_DEVICE_ID_EDGEPORT_4I: // Edgeport/4 RS422
517 product_info->IsRS422 = 1;
518 break;
519 }
520
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -0800521 dump_product_info(product_info);
522}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -0800524static int get_epic_descriptor(struct edgeport_serial *ep)
525{
526 int result;
527 struct usb_serial *serial = ep->serial;
528 struct edgeport_product_info *product_info = &ep->product_info;
529 struct edge_compatibility_descriptor *epic = &ep->epic_descriptor;
530 struct edge_compatibility_bits *bits;
531
532 ep->is_epic = 0;
533 result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
534 USB_REQUEST_ION_GET_EPIC_DESC,
535 0xC0, 0x00, 0x00,
536 &ep->epic_descriptor,
537 sizeof(struct edge_compatibility_descriptor),
538 300);
539
Harvey Harrison441b62c2008-03-03 16:08:34 -0800540 dbg("%s result = %d", __func__, result);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -0800541
542 if (result > 0) {
543 ep->is_epic = 1;
544 memset(product_info, 0, sizeof(struct edgeport_product_info));
545
546 product_info->NumPorts = epic->NumPorts;
547 product_info->ProdInfoVer = 0;
548 product_info->FirmwareMajorVersion = epic->MajorVersion;
549 product_info->FirmwareMinorVersion = epic->MinorVersion;
550 product_info->FirmwareBuildNumber = epic->BuildNumber;
551 product_info->iDownloadFile = epic->iDownloadFile;
552 product_info->EpicVer = epic->EpicVer;
553 product_info->Epic = epic->Supports;
554 product_info->ProductId = ION_DEVICE_ID_EDGEPORT_COMPATIBLE;
555 dump_product_info(product_info);
556
557 bits = &ep->epic_descriptor.Supports;
558 dbg("**EPIC descriptor:");
559 dbg(" VendEnableSuspend: %s", bits->VendEnableSuspend ? "TRUE": "FALSE");
560 dbg(" IOSPOpen : %s", bits->IOSPOpen ? "TRUE": "FALSE" );
561 dbg(" IOSPClose : %s", bits->IOSPClose ? "TRUE": "FALSE" );
562 dbg(" IOSPChase : %s", bits->IOSPChase ? "TRUE": "FALSE" );
563 dbg(" IOSPSetRxFlow : %s", bits->IOSPSetRxFlow ? "TRUE": "FALSE" );
564 dbg(" IOSPSetTxFlow : %s", bits->IOSPSetTxFlow ? "TRUE": "FALSE" );
565 dbg(" IOSPSetXChar : %s", bits->IOSPSetXChar ? "TRUE": "FALSE" );
566 dbg(" IOSPRxCheck : %s", bits->IOSPRxCheck ? "TRUE": "FALSE" );
567 dbg(" IOSPSetClrBreak : %s", bits->IOSPSetClrBreak ? "TRUE": "FALSE" );
568 dbg(" IOSPWriteMCR : %s", bits->IOSPWriteMCR ? "TRUE": "FALSE" );
569 dbg(" IOSPWriteLCR : %s", bits->IOSPWriteLCR ? "TRUE": "FALSE" );
570 dbg(" IOSPSetBaudRate : %s", bits->IOSPSetBaudRate ? "TRUE": "FALSE" );
571 dbg(" TrueEdgeport : %s", bits->TrueEdgeport ? "TRUE": "FALSE" );
572 }
573
574 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575}
576
577
578/************************************************************************/
579/************************************************************************/
580/* U S B C A L L B A C K F U N C T I O N S */
581/* U S B C A L L B A C K F U N C T I O N S */
582/************************************************************************/
583/************************************************************************/
584
585/*****************************************************************************
586 * edge_interrupt_callback
587 * this is the callback function for when we have received data on the
588 * interrupt endpoint.
589 *****************************************************************************/
David Howells7d12e782006-10-05 14:55:46 +0100590static void edge_interrupt_callback (struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591{
592 struct edgeport_serial *edge_serial = (struct edgeport_serial *)urb->context;
593 struct edgeport_port *edge_port;
594 struct usb_serial_port *port;
595 unsigned char *data = urb->transfer_buffer;
596 int length = urb->actual_length;
597 int bytes_avail;
598 int position;
599 int txCredits;
600 int portNumber;
601 int result;
Greg Kroah-Hartman2a393f52007-06-15 15:44:13 -0700602 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
Harvey Harrison441b62c2008-03-03 16:08:34 -0800604 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
Greg Kroah-Hartman2a393f52007-06-15 15:44:13 -0700606 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 case 0:
608 /* success */
609 break;
610 case -ECONNRESET:
611 case -ENOENT:
612 case -ESHUTDOWN:
613 /* this urb is terminated, clean up */
Greg Kroah-Hartman2a393f52007-06-15 15:44:13 -0700614 dbg("%s - urb shutting down with status: %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800615 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 return;
617 default:
Greg Kroah-Hartman2a393f52007-06-15 15:44:13 -0700618 dbg("%s - nonzero urb status received: %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800619 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 goto exit;
621 }
622
623 // process this interrupt-read even if there are no ports open
624 if (length) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800625 usb_serial_debug_data(debug, &edge_serial->serial->dev->dev, __func__, length, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
627 if (length > 1) {
628 bytes_avail = data[0] | (data[1] << 8);
629 if (bytes_avail) {
630 spin_lock(&edge_serial->es_lock);
631 edge_serial->rxBytesAvail += bytes_avail;
Harvey Harrison441b62c2008-03-03 16:08:34 -0800632 dbg("%s - bytes_avail=%d, rxBytesAvail=%d, read_in_progress=%d", __func__, bytes_avail, edge_serial->rxBytesAvail, edge_serial->read_in_progress);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
634 if (edge_serial->rxBytesAvail > 0 &&
635 !edge_serial->read_in_progress) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800636 dbg("%s - posting a read", __func__);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100637 edge_serial->read_in_progress = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
639 /* we have pending bytes on the bulk in pipe, send a request */
640 edge_serial->read_urb->dev = edge_serial->serial->dev;
641 result = usb_submit_urb(edge_serial->read_urb, GFP_ATOMIC);
642 if (result) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800643 dev_err(&edge_serial->serial->dev->dev, "%s - usb_submit_urb(read bulk) failed with result = %d\n", __func__, result);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100644 edge_serial->read_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 }
646 }
647 spin_unlock(&edge_serial->es_lock);
648 }
649 }
650 /* grab the txcredits for the ports if available */
651 position = 2;
652 portNumber = 0;
653 while ((position < length) && (portNumber < edge_serial->serial->num_ports)) {
654 txCredits = data[position] | (data[position+1] << 8);
655 if (txCredits) {
656 port = edge_serial->serial->port[portNumber];
657 edge_port = usb_get_serial_port_data(port);
658 if (edge_port->open) {
659 spin_lock(&edge_port->ep_lock);
660 edge_port->txCredits += txCredits;
661 spin_unlock(&edge_port->ep_lock);
Harvey Harrison441b62c2008-03-03 16:08:34 -0800662 dbg("%s - txcredits for port%d = %d", __func__, portNumber, edge_port->txCredits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
664 /* tell the tty driver that something has changed */
665 if (edge_port->port->tty)
666 tty_wakeup(edge_port->port->tty);
667
668 // Since we have more credit, check if more data can be sent
669 send_more_port_data(edge_serial, edge_port);
670 }
671 }
672 position += 2;
673 ++portNumber;
674 }
675 }
676
677exit:
678 result = usb_submit_urb (urb, GFP_ATOMIC);
679 if (result) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800680 dev_err(&urb->dev->dev, "%s - Error %d submitting control urb\n", __func__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 }
682}
683
684
685/*****************************************************************************
686 * edge_bulk_in_callback
687 * this is the callback function for when we have received data on the
688 * bulk in endpoint.
689 *****************************************************************************/
David Howells7d12e782006-10-05 14:55:46 +0100690static void edge_bulk_in_callback (struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691{
692 struct edgeport_serial *edge_serial = (struct edgeport_serial *)urb->context;
693 unsigned char *data = urb->transfer_buffer;
Greg Kroah-Hartman2a393f52007-06-15 15:44:13 -0700694 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 __u16 raw_data_length;
Greg Kroah-Hartman2a393f52007-06-15 15:44:13 -0700696 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
Harvey Harrison441b62c2008-03-03 16:08:34 -0800698 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
Greg Kroah-Hartman2a393f52007-06-15 15:44:13 -0700700 if (status) {
701 dbg("%s - nonzero read bulk status received: %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800702 __func__, status);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100703 edge_serial->read_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 return;
705 }
706
707 if (urb->actual_length == 0) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800708 dbg("%s - read bulk callback with no data", __func__);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100709 edge_serial->read_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 return;
711 }
712
713 raw_data_length = urb->actual_length;
714
Harvey Harrison441b62c2008-03-03 16:08:34 -0800715 usb_serial_debug_data(debug, &edge_serial->serial->dev->dev, __func__, raw_data_length, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
717 spin_lock(&edge_serial->es_lock);
718
719 /* decrement our rxBytes available by the number that we just got */
720 edge_serial->rxBytesAvail -= raw_data_length;
721
Harvey Harrison441b62c2008-03-03 16:08:34 -0800722 dbg("%s - Received = %d, rxBytesAvail %d", __func__, raw_data_length, edge_serial->rxBytesAvail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
724 process_rcvd_data (edge_serial, data, urb->actual_length);
725
726 /* check to see if there's any more data for us to read */
727 if (edge_serial->rxBytesAvail > 0) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800728 dbg("%s - posting a read", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 edge_serial->read_urb->dev = edge_serial->serial->dev;
Greg Kroah-Hartman2a393f52007-06-15 15:44:13 -0700730 retval = usb_submit_urb(edge_serial->read_urb, GFP_ATOMIC);
731 if (retval) {
732 dev_err(&urb->dev->dev,
733 "%s - usb_submit_urb(read bulk) failed, "
Harvey Harrison441b62c2008-03-03 16:08:34 -0800734 "retval = %d\n", __func__, retval);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100735 edge_serial->read_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 }
737 } else {
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100738 edge_serial->read_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 }
740
741 spin_unlock(&edge_serial->es_lock);
742}
743
744
745/*****************************************************************************
746 * edge_bulk_out_data_callback
747 * this is the callback function for when we have finished sending serial data
748 * on the bulk out endpoint.
749 *****************************************************************************/
David Howells7d12e782006-10-05 14:55:46 +0100750static void edge_bulk_out_data_callback (struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751{
752 struct edgeport_port *edge_port = (struct edgeport_port *)urb->context;
753 struct tty_struct *tty;
Greg Kroah-Hartman2a393f52007-06-15 15:44:13 -0700754 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
Harvey Harrison441b62c2008-03-03 16:08:34 -0800756 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
Greg Kroah-Hartman2a393f52007-06-15 15:44:13 -0700758 if (status) {
759 dbg("%s - nonzero write bulk status received: %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800760 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 }
762
763 tty = edge_port->port->tty;
764
765 if (tty && edge_port->open) {
766 /* let the tty driver wakeup if it has a special write_wakeup function */
767 tty_wakeup(tty);
768 }
769
770 // Release the Write URB
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100771 edge_port->write_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
773 // Check if more data needs to be sent
774 send_more_port_data((struct edgeport_serial *)(usb_get_serial_data(edge_port->port->serial)), edge_port);
775}
776
777
778/*****************************************************************************
779 * BulkOutCmdCallback
780 * this is the callback function for when we have finished sending a command
781 * on the bulk out endpoint.
782 *****************************************************************************/
David Howells7d12e782006-10-05 14:55:46 +0100783static void edge_bulk_out_cmd_callback (struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784{
785 struct edgeport_port *edge_port = (struct edgeport_port *)urb->context;
786 struct tty_struct *tty;
787 int status = urb->status;
788
Harvey Harrison441b62c2008-03-03 16:08:34 -0800789 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
Oliver Neukum96c706e2007-03-15 15:27:17 +0100791 atomic_dec(&CmdUrbs);
Harvey Harrison441b62c2008-03-03 16:08:34 -0800792 dbg("%s - FREE URB %p (outstanding %d)", __func__, urb, atomic_read(&CmdUrbs));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
794
795 /* clean up the transfer buffer */
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -0700796 kfree(urb->transfer_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
798 /* Free the command urb */
799 usb_free_urb (urb);
800
801 if (status) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800802 dbg("%s - nonzero write bulk status received: %d", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 return;
804 }
805
806 /* Get pointer to tty */
807 tty = edge_port->port->tty;
808
809 /* tell the tty driver that something has changed */
810 if (tty && edge_port->open)
811 tty_wakeup(tty);
812
813 /* we have completed the command */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100814 edge_port->commandPending = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 wake_up(&edge_port->wait_command);
816}
817
818
819/*****************************************************************************
820 * Driver tty interface functions
821 *****************************************************************************/
822
823/*****************************************************************************
824 * SerialOpen
825 * this function is called by the tty driver when a port is opened
826 * If successful, we return 0
827 * Otherwise we return a negative error number.
828 *****************************************************************************/
829static int edge_open (struct usb_serial_port *port, struct file * filp)
830{
831 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
832 struct usb_serial *serial;
833 struct edgeport_serial *edge_serial;
834 int response;
835
Harvey Harrison441b62c2008-03-03 16:08:34 -0800836 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
838 if (edge_port == NULL)
839 return -ENODEV;
840
841 if (port->tty)
842 port->tty->low_latency = low_latency;
843
844 /* see if we've set up our endpoint info yet (can't set it up in edge_startup
845 as the structures were not set up at that time.) */
846 serial = port->serial;
847 edge_serial = usb_get_serial_data(serial);
848 if (edge_serial == NULL) {
849 return -ENODEV;
850 }
851 if (edge_serial->interrupt_in_buffer == NULL) {
852 struct usb_serial_port *port0 = serial->port[0];
853
854 /* not set up yet, so do it now */
855 edge_serial->interrupt_in_buffer = port0->interrupt_in_buffer;
856 edge_serial->interrupt_in_endpoint = port0->interrupt_in_endpointAddress;
857 edge_serial->interrupt_read_urb = port0->interrupt_in_urb;
858 edge_serial->bulk_in_buffer = port0->bulk_in_buffer;
859 edge_serial->bulk_in_endpoint = port0->bulk_in_endpointAddress;
860 edge_serial->read_urb = port0->read_urb;
861 edge_serial->bulk_out_endpoint = port0->bulk_out_endpointAddress;
862
863 /* set up our interrupt urb */
864 usb_fill_int_urb(edge_serial->interrupt_read_urb,
865 serial->dev,
866 usb_rcvintpipe(serial->dev,
867 port0->interrupt_in_endpointAddress),
868 port0->interrupt_in_buffer,
869 edge_serial->interrupt_read_urb->transfer_buffer_length,
870 edge_interrupt_callback, edge_serial,
871 edge_serial->interrupt_read_urb->interval);
872
873 /* set up our bulk in urb */
874 usb_fill_bulk_urb(edge_serial->read_urb, serial->dev,
875 usb_rcvbulkpipe(serial->dev,
876 port0->bulk_in_endpointAddress),
877 port0->bulk_in_buffer,
878 edge_serial->read_urb->transfer_buffer_length,
879 edge_bulk_in_callback, edge_serial);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100880 edge_serial->read_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
882 /* start interrupt read for this edgeport
883 * this interrupt will continue as long as the edgeport is connected */
884 response = usb_submit_urb (edge_serial->interrupt_read_urb, GFP_KERNEL);
885 if (response) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800886 dev_err(&port->dev, "%s - Error %d submitting control urb\n", __func__, response);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 }
888 }
889
890 /* initialize our wait queues */
891 init_waitqueue_head(&edge_port->wait_open);
892 init_waitqueue_head(&edge_port->wait_chase);
893 init_waitqueue_head(&edge_port->delta_msr_wait);
894 init_waitqueue_head(&edge_port->wait_command);
895
896 /* initialize our icount structure */
897 memset (&(edge_port->icount), 0x00, sizeof(edge_port->icount));
898
899 /* initialize our port settings */
900 edge_port->txCredits = 0; /* Can't send any data yet */
901 edge_port->shadowMCR = MCR_MASTER_IE; /* Must always set this bit to enable ints! */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100902 edge_port->chaseResponsePending = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
904 /* send a open port command */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100905 edge_port->openPending = true;
906 edge_port->open = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 response = send_iosp_ext_cmd (edge_port, IOSP_CMD_OPEN_PORT, 0);
908
909 if (response < 0) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800910 dev_err(&port->dev, "%s - error sending open port command\n", __func__);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100911 edge_port->openPending = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 return -ENODEV;
913 }
914
915 /* now wait for the port to be completely opened */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100916 wait_event_timeout(edge_port->wait_open, !edge_port->openPending, OPEN_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100918 if (!edge_port->open) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 /* open timed out */
Harvey Harrison441b62c2008-03-03 16:08:34 -0800920 dbg("%s - open timedout", __func__);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100921 edge_port->openPending = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 return -ENODEV;
923 }
924
925 /* create the txfifo */
926 edge_port->txfifo.head = 0;
927 edge_port->txfifo.tail = 0;
928 edge_port->txfifo.count = 0;
929 edge_port->txfifo.size = edge_port->maxTxCredits;
930 edge_port->txfifo.fifo = kmalloc (edge_port->maxTxCredits, GFP_KERNEL);
931
932 if (!edge_port->txfifo.fifo) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800933 dbg("%s - no memory", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 edge_close (port, filp);
935 return -ENOMEM;
936 }
937
938 /* Allocate a URB for the write */
939 edge_port->write_urb = usb_alloc_urb (0, GFP_KERNEL);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100940 edge_port->write_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
942 if (!edge_port->write_urb) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800943 dbg("%s - no memory", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 edge_close (port, filp);
945 return -ENOMEM;
946 }
947
Harvey Harrison441b62c2008-03-03 16:08:34 -0800948 dbg("%s(%d) - Initialize TX fifo to %d bytes", __func__, port->number, edge_port->maxTxCredits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
Harvey Harrison441b62c2008-03-03 16:08:34 -0800950 dbg("%s exited", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
952 return 0;
953}
954
955
956/************************************************************************
957 *
958 * block_until_chase_response
959 *
960 * This function will block the close until one of the following:
961 * 1. Response to our Chase comes from Edgeport
Joe Perchesdc0d5c12007-12-17 11:40:18 -0800962 * 2. A timeout of 10 seconds without activity has expired
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 * (1K of Edgeport data @ 2400 baud ==> 4 sec to empty)
964 *
965 ************************************************************************/
966static void block_until_chase_response(struct edgeport_port *edge_port)
967{
968 DEFINE_WAIT(wait);
969 __u16 lastCredits;
970 int timeout = 1*HZ;
971 int loop = 10;
972
973 while (1) {
974 // Save Last credits
975 lastCredits = edge_port->txCredits;
976
977 // Did we get our Chase response
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100978 if (!edge_port->chaseResponsePending) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800979 dbg("%s - Got Chase Response", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
981 // did we get all of our credit back?
982 if (edge_port->txCredits == edge_port->maxTxCredits ) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800983 dbg("%s - Got all credits", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 return;
985 }
986 }
987
988 // Block the thread for a while
989 prepare_to_wait(&edge_port->wait_chase, &wait, TASK_UNINTERRUPTIBLE);
990 schedule_timeout(timeout);
991 finish_wait(&edge_port->wait_chase, &wait);
992
993 if (lastCredits == edge_port->txCredits) {
994 // No activity.. count down.
995 loop--;
996 if (loop == 0) {
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100997 edge_port->chaseResponsePending = false;
Harvey Harrison441b62c2008-03-03 16:08:34 -0800998 dbg("%s - Chase TIMEOUT", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 return;
1000 }
1001 } else {
Joe Perchesdc0d5c12007-12-17 11:40:18 -08001002 // Reset timeout value back to 10 seconds
Harvey Harrison441b62c2008-03-03 16:08:34 -08001003 dbg("%s - Last %d, Current %d", __func__, lastCredits, edge_port->txCredits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 loop = 10;
1005 }
1006 }
1007}
1008
1009
1010/************************************************************************
1011 *
1012 * block_until_tx_empty
1013 *
1014 * This function will block the close until one of the following:
1015 * 1. TX count are 0
1016 * 2. The edgeport has stopped
Joe Perchesdc0d5c12007-12-17 11:40:18 -08001017 * 3. A timeout of 3 seconds without activity has expired
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 *
1019 ************************************************************************/
1020static void block_until_tx_empty (struct edgeport_port *edge_port)
1021{
1022 DEFINE_WAIT(wait);
1023 struct TxFifo *fifo = &edge_port->txfifo;
1024 __u32 lastCount;
1025 int timeout = HZ/10;
1026 int loop = 30;
1027
1028 while (1) {
1029 // Save Last count
1030 lastCount = fifo->count;
1031
1032 // Is the Edgeport Buffer empty?
1033 if (lastCount == 0) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001034 dbg("%s - TX Buffer Empty", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 return;
1036 }
1037
1038 // Block the thread for a while
1039 prepare_to_wait (&edge_port->wait_chase, &wait, TASK_UNINTERRUPTIBLE);
1040 schedule_timeout(timeout);
1041 finish_wait(&edge_port->wait_chase, &wait);
1042
Harvey Harrison441b62c2008-03-03 16:08:34 -08001043 dbg("%s wait", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
1045 if (lastCount == fifo->count) {
1046 // No activity.. count down.
1047 loop--;
1048 if (loop == 0) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001049 dbg("%s - TIMEOUT", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 return;
1051 }
1052 } else {
Joe Perchesdc0d5c12007-12-17 11:40:18 -08001053 // Reset timeout value back to seconds
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 loop = 30;
1055 }
1056 }
1057}
1058
1059
1060/*****************************************************************************
1061 * edge_close
1062 * this function is called by the tty driver when a port is closed
1063 *****************************************************************************/
1064static void edge_close (struct usb_serial_port *port, struct file * filp)
1065{
1066 struct edgeport_serial *edge_serial;
1067 struct edgeport_port *edge_port;
1068 int status;
1069
Harvey Harrison441b62c2008-03-03 16:08:34 -08001070 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071
1072 edge_serial = usb_get_serial_data(port->serial);
1073 edge_port = usb_get_serial_port_data(port);
1074 if ((edge_serial == NULL) || (edge_port == NULL))
1075 return;
1076
1077 // block until tx is empty
1078 block_until_tx_empty(edge_port);
1079
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001080 edge_port->closePending = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001082 if ((!edge_serial->is_epic) ||
1083 ((edge_serial->is_epic) &&
1084 (edge_serial->epic_descriptor.Supports.IOSPChase))) {
1085 /* flush and chase */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001086 edge_port->chaseResponsePending = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087
Harvey Harrison441b62c2008-03-03 16:08:34 -08001088 dbg("%s - Sending IOSP_CMD_CHASE_PORT", __func__);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001089 status = send_iosp_ext_cmd (edge_port, IOSP_CMD_CHASE_PORT, 0);
1090 if (status == 0) {
1091 // block until chase finished
1092 block_until_chase_response(edge_port);
1093 } else {
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001094 edge_port->chaseResponsePending = false;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001095 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 }
1097
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001098 if ((!edge_serial->is_epic) ||
1099 ((edge_serial->is_epic) &&
1100 (edge_serial->epic_descriptor.Supports.IOSPClose))) {
1101 /* close the port */
Harvey Harrison441b62c2008-03-03 16:08:34 -08001102 dbg("%s - Sending IOSP_CMD_CLOSE_PORT", __func__);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001103 send_iosp_ext_cmd (edge_port, IOSP_CMD_CLOSE_PORT, 0);
1104 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001106 //port->close = true;
1107 edge_port->closePending = false;
1108 edge_port->open = false;
1109 edge_port->openPending = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110
Mariusz Kozlowski9a25f442006-11-08 15:36:29 +01001111 usb_kill_urb(edge_port->write_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112
1113 if (edge_port->write_urb) {
1114 /* if this urb had a transfer buffer already (old transfer) free it */
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07001115 kfree(edge_port->write_urb->transfer_buffer);
1116 usb_free_urb(edge_port->write_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 edge_port->write_urb = NULL;
1118 }
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07001119 kfree(edge_port->txfifo.fifo);
1120 edge_port->txfifo.fifo = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
Harvey Harrison441b62c2008-03-03 16:08:34 -08001122 dbg("%s exited", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123}
1124
1125/*****************************************************************************
1126 * SerialWrite
1127 * this function is called by the tty driver when data should be written to
1128 * the port.
1129 * If successful, we return the number of bytes written, otherwise we return
1130 * a negative error number.
1131 *****************************************************************************/
1132static int edge_write (struct usb_serial_port *port, const unsigned char *data, int count)
1133{
1134 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1135 struct TxFifo *fifo;
1136 int copySize;
1137 int bytesleft;
1138 int firsthalf;
1139 int secondhalf;
1140 unsigned long flags;
1141
Harvey Harrison441b62c2008-03-03 16:08:34 -08001142 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143
1144 if (edge_port == NULL)
1145 return -ENODEV;
1146
1147 // get a pointer to the Tx fifo
1148 fifo = &edge_port->txfifo;
1149
1150 spin_lock_irqsave(&edge_port->ep_lock, flags);
1151
1152 // calculate number of bytes to put in fifo
1153 copySize = min ((unsigned int)count, (edge_port->txCredits - fifo->count));
1154
Harvey Harrison441b62c2008-03-03 16:08:34 -08001155 dbg("%s(%d) of %d byte(s) Fifo room %d -- will copy %d bytes", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 port->number, count, edge_port->txCredits - fifo->count, copySize);
1157
1158 /* catch writes of 0 bytes which the tty driver likes to give us, and when txCredits is empty */
1159 if (copySize == 0) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001160 dbg("%s - copySize = Zero", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 goto finish_write;
1162 }
1163
1164 // queue the data
1165 // since we can never overflow the buffer we do not have to check for full condition
1166
1167 // the copy is done is two parts -- first fill to the end of the buffer
1168 // then copy the reset from the start of the buffer
1169
1170 bytesleft = fifo->size - fifo->head;
1171 firsthalf = min (bytesleft, copySize);
Harvey Harrison441b62c2008-03-03 16:08:34 -08001172 dbg("%s - copy %d bytes of %d into fifo ", __func__, firsthalf, bytesleft);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173
1174 /* now copy our data */
1175 memcpy(&fifo->fifo[fifo->head], data, firsthalf);
Harvey Harrison441b62c2008-03-03 16:08:34 -08001176 usb_serial_debug_data(debug, &port->dev, __func__, firsthalf, &fifo->fifo[fifo->head]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177
1178 // update the index and size
1179 fifo->head += firsthalf;
1180 fifo->count += firsthalf;
1181
1182 // wrap the index
1183 if (fifo->head == fifo->size) {
1184 fifo->head = 0;
1185 }
1186
1187 secondhalf = copySize-firsthalf;
1188
1189 if (secondhalf) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001190 dbg("%s - copy rest of data %d", __func__, secondhalf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 memcpy(&fifo->fifo[fifo->head], &data[firsthalf], secondhalf);
Harvey Harrison441b62c2008-03-03 16:08:34 -08001192 usb_serial_debug_data(debug, &port->dev, __func__, secondhalf, &fifo->fifo[fifo->head]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 // update the index and size
1194 fifo->count += secondhalf;
1195 fifo->head += secondhalf;
1196 // No need to check for wrap since we can not get to end of fifo in this part
1197 }
1198
1199finish_write:
1200 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1201
1202 send_more_port_data((struct edgeport_serial *)usb_get_serial_data(port->serial), edge_port);
1203
Harvey Harrison441b62c2008-03-03 16:08:34 -08001204 dbg("%s wrote %d byte(s) TxCredits %d, Fifo %d", __func__, copySize, edge_port->txCredits, fifo->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205
1206 return copySize;
1207}
1208
1209
1210/************************************************************************
1211 *
1212 * send_more_port_data()
1213 *
1214 * This routine attempts to write additional UART transmit data
1215 * to a port over the USB bulk pipe. It is called (1) when new
1216 * data has been written to a port's TxBuffer from higher layers
1217 * (2) when the peripheral sends us additional TxCredits indicating
1218 * that it can accept more Tx data for a given port; and (3) when
1219 * a bulk write completes successfully and we want to see if we
1220 * can transmit more.
1221 *
1222 ************************************************************************/
1223static void send_more_port_data(struct edgeport_serial *edge_serial, struct edgeport_port *edge_port)
1224{
1225 struct TxFifo *fifo = &edge_port->txfifo;
1226 struct urb *urb;
1227 unsigned char *buffer;
1228 int status;
1229 int count;
1230 int bytesleft;
1231 int firsthalf;
1232 int secondhalf;
1233 unsigned long flags;
1234
Harvey Harrison441b62c2008-03-03 16:08:34 -08001235 dbg("%s(%d)", __func__, edge_port->port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236
1237 spin_lock_irqsave(&edge_port->ep_lock, flags);
1238
1239 if (edge_port->write_in_progress ||
1240 !edge_port->open ||
1241 (fifo->count == 0)) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001242 dbg("%s(%d) EXIT - fifo %d, PendingWrite = %d", __func__, edge_port->port->number, fifo->count, edge_port->write_in_progress);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 goto exit_send;
1244 }
1245
1246 // since the amount of data in the fifo will always fit into the
1247 // edgeport buffer we do not need to check the write length
1248
1249 // Do we have enough credits for this port to make it worthwhile
1250 // to bother queueing a write. If it's too small, say a few bytes,
1251 // it's better to wait for more credits so we can do a larger
1252 // write.
1253 if (edge_port->txCredits < EDGE_FW_GET_TX_CREDITS_SEND_THRESHOLD(edge_port->maxTxCredits,EDGE_FW_BULK_MAX_PACKET_SIZE)) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001254 dbg("%s(%d) Not enough credit - fifo %d TxCredit %d", __func__, edge_port->port->number, fifo->count, edge_port->txCredits );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 goto exit_send;
1256 }
1257
1258 // lock this write
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001259 edge_port->write_in_progress = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
1261 // get a pointer to the write_urb
1262 urb = edge_port->write_urb;
1263
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07001264 /* make sure transfer buffer is freed */
1265 kfree(urb->transfer_buffer);
1266 urb->transfer_buffer = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
1268 /* build the data header for the buffer and port that we are about to send out */
1269 count = fifo->count;
1270 buffer = kmalloc (count+2, GFP_ATOMIC);
1271 if (buffer == NULL) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001272 dev_err(&edge_port->port->dev, "%s - no more kernel memory...\n", __func__);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001273 edge_port->write_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 goto exit_send;
1275 }
1276 buffer[0] = IOSP_BUILD_DATA_HDR1 (edge_port->port->number - edge_port->port->serial->minor, count);
1277 buffer[1] = IOSP_BUILD_DATA_HDR2 (edge_port->port->number - edge_port->port->serial->minor, count);
1278
1279 /* now copy our data */
1280 bytesleft = fifo->size - fifo->tail;
1281 firsthalf = min (bytesleft, count);
1282 memcpy(&buffer[2], &fifo->fifo[fifo->tail], firsthalf);
1283 fifo->tail += firsthalf;
1284 fifo->count -= firsthalf;
1285 if (fifo->tail == fifo->size) {
1286 fifo->tail = 0;
1287 }
1288
1289 secondhalf = count-firsthalf;
1290 if (secondhalf) {
1291 memcpy(&buffer[2+firsthalf], &fifo->fifo[fifo->tail], secondhalf);
1292 fifo->tail += secondhalf;
1293 fifo->count -= secondhalf;
1294 }
1295
1296 if (count)
Harvey Harrison441b62c2008-03-03 16:08:34 -08001297 usb_serial_debug_data(debug, &edge_port->port->dev, __func__, count, &buffer[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298
1299 /* fill up the urb with all of our data and submit it */
1300 usb_fill_bulk_urb (urb, edge_serial->serial->dev,
1301 usb_sndbulkpipe(edge_serial->serial->dev, edge_serial->bulk_out_endpoint),
1302 buffer, count+2, edge_bulk_out_data_callback, edge_port);
1303
1304 /* decrement the number of credits we have by the number we just sent */
1305 edge_port->txCredits -= count;
1306 edge_port->icount.tx += count;
1307
1308 urb->dev = edge_serial->serial->dev;
1309 status = usb_submit_urb(urb, GFP_ATOMIC);
1310 if (status) {
1311 /* something went wrong */
Harvey Harrison441b62c2008-03-03 16:08:34 -08001312 dev_err(&edge_port->port->dev, "%s - usb_submit_urb(write bulk) failed, status = %d, data lost\n", __func__, status);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001313 edge_port->write_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314
1315 /* revert the credits as something bad happened. */
1316 edge_port->txCredits += count;
1317 edge_port->icount.tx -= count;
1318 }
Harvey Harrison441b62c2008-03-03 16:08:34 -08001319 dbg("%s wrote %d byte(s) TxCredit %d, Fifo %d", __func__, count, edge_port->txCredits, fifo->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320
1321exit_send:
1322 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1323}
1324
1325
1326/*****************************************************************************
1327 * edge_write_room
1328 * this function is called by the tty driver when it wants to know how many
1329 * bytes of data we can accept for a specific port.
1330 * If successful, we return the amount of room that we have for this port
1331 * (the txCredits),
1332 * Otherwise we return a negative error number.
1333 *****************************************************************************/
1334static int edge_write_room (struct usb_serial_port *port)
1335{
1336 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1337 int room;
1338 unsigned long flags;
1339
Harvey Harrison441b62c2008-03-03 16:08:34 -08001340 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
1342 if (edge_port == NULL)
1343 return -ENODEV;
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001344 if (edge_port->closePending)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 return -ENODEV;
1346
Harvey Harrison441b62c2008-03-03 16:08:34 -08001347 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348
1349 if (!edge_port->open) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001350 dbg("%s - port not opened", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 return -EINVAL;
1352 }
1353
1354 // total of both buffers is still txCredit
1355 spin_lock_irqsave(&edge_port->ep_lock, flags);
1356 room = edge_port->txCredits - edge_port->txfifo.count;
1357 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1358
Harvey Harrison441b62c2008-03-03 16:08:34 -08001359 dbg("%s - returns %d", __func__, room);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 return room;
1361}
1362
1363
1364/*****************************************************************************
1365 * edge_chars_in_buffer
1366 * this function is called by the tty driver when it wants to know how many
1367 * bytes of data we currently have outstanding in the port (data that has
1368 * been written, but hasn't made it out the port yet)
1369 * If successful, we return the number of bytes left to be written in the
1370 * system,
1371 * Otherwise we return a negative error number.
1372 *****************************************************************************/
1373static int edge_chars_in_buffer (struct usb_serial_port *port)
1374{
1375 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1376 int num_chars;
1377 unsigned long flags;
1378
Harvey Harrison441b62c2008-03-03 16:08:34 -08001379 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380
1381 if (edge_port == NULL)
1382 return -ENODEV;
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001383 if (edge_port->closePending)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 return -ENODEV;
1385
1386 if (!edge_port->open) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001387 dbg("%s - port not opened", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 return -EINVAL;
1389 }
1390
1391 spin_lock_irqsave(&edge_port->ep_lock, flags);
1392 num_chars = edge_port->maxTxCredits - edge_port->txCredits + edge_port->txfifo.count;
1393 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1394 if (num_chars) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001395 dbg("%s(port %d) - returns %d", __func__, port->number, num_chars);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 }
1397
1398 return num_chars;
1399}
1400
1401
1402/*****************************************************************************
1403 * SerialThrottle
1404 * this function is called by the tty driver when it wants to stop the data
1405 * being read from the port.
1406 *****************************************************************************/
1407static void edge_throttle (struct usb_serial_port *port)
1408{
1409 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1410 struct tty_struct *tty;
1411 int status;
1412
Harvey Harrison441b62c2008-03-03 16:08:34 -08001413 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414
1415 if (edge_port == NULL)
1416 return;
1417
1418 if (!edge_port->open) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001419 dbg("%s - port not opened", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 return;
1421 }
1422
1423 tty = port->tty;
1424 if (!tty) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001425 dbg ("%s - no tty available", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 return;
1427 }
1428
1429 /* if we are implementing XON/XOFF, send the stop character */
1430 if (I_IXOFF(tty)) {
1431 unsigned char stop_char = STOP_CHAR(tty);
1432 status = edge_write (port, &stop_char, 1);
1433 if (status <= 0) {
1434 return;
1435 }
1436 }
1437
1438 /* if we are implementing RTS/CTS, toggle that line */
1439 if (tty->termios->c_cflag & CRTSCTS) {
1440 edge_port->shadowMCR &= ~MCR_RTS;
1441 status = send_cmd_write_uart_register(edge_port, MCR, edge_port->shadowMCR);
1442 if (status != 0) {
1443 return;
1444 }
1445 }
1446
1447 return;
1448}
1449
1450
1451/*****************************************************************************
1452 * edge_unthrottle
1453 * this function is called by the tty driver when it wants to resume the data
1454 * being read from the port (called after SerialThrottle is called)
1455 *****************************************************************************/
1456static void edge_unthrottle (struct usb_serial_port *port)
1457{
1458 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1459 struct tty_struct *tty;
1460 int status;
1461
Harvey Harrison441b62c2008-03-03 16:08:34 -08001462 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463
1464 if (edge_port == NULL)
1465 return;
1466
1467 if (!edge_port->open) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001468 dbg("%s - port not opened", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 return;
1470 }
1471
1472 tty = port->tty;
1473 if (!tty) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001474 dbg ("%s - no tty available", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 return;
1476 }
1477
1478 /* if we are implementing XON/XOFF, send the start character */
1479 if (I_IXOFF(tty)) {
1480 unsigned char start_char = START_CHAR(tty);
1481 status = edge_write (port, &start_char, 1);
1482 if (status <= 0) {
1483 return;
1484 }
1485 }
1486
1487 /* if we are implementing RTS/CTS, toggle that line */
1488 if (tty->termios->c_cflag & CRTSCTS) {
1489 edge_port->shadowMCR |= MCR_RTS;
1490 status = send_cmd_write_uart_register(edge_port, MCR, edge_port->shadowMCR);
1491 if (status != 0) {
1492 return;
1493 }
1494 }
1495
1496 return;
1497}
1498
1499
1500/*****************************************************************************
1501 * SerialSetTermios
1502 * this function is called by the tty driver when it wants to change the termios structure
1503 *****************************************************************************/
Alan Cox606d0992006-12-08 02:38:45 -08001504static void edge_set_termios (struct usb_serial_port *port, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505{
Alan Cox6ce073b2007-10-18 01:24:25 -07001506 /* FIXME: This function appears unused ?? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1508 struct tty_struct *tty = port->tty;
1509 unsigned int cflag;
1510
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 cflag = tty->termios->c_cflag;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001512 dbg("%s - clfag %08x iflag %08x", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 tty->termios->c_cflag, tty->termios->c_iflag);
Harvey Harrison441b62c2008-03-03 16:08:34 -08001514 dbg("%s - old clfag %08x old iflag %08x", __func__,
Alan Cox6ce073b2007-10-18 01:24:25 -07001515 old_termios->c_cflag, old_termios->c_iflag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516
Harvey Harrison441b62c2008-03-03 16:08:34 -08001517 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518
1519 if (edge_port == NULL)
1520 return;
1521
1522 if (!edge_port->open) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001523 dbg("%s - port not opened", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 return;
1525 }
1526
1527 /* change the port settings to the new ones specified */
1528 change_port_settings (edge_port, old_termios);
1529
1530 return;
1531}
1532
1533
1534/*****************************************************************************
1535 * get_lsr_info - get line status register info
1536 *
1537 * Purpose: Let user call ioctl() to get info when the UART physically
1538 * is emptied. On bus types like RS485, the transmitter must
1539 * release the bus after transmitting. This must be done when
1540 * the transmit shift register is empty, not be done when the
1541 * transmit holding register is empty. This functionality
1542 * allows an RS485 driver to be written in user space.
1543 *****************************************************************************/
1544static int get_lsr_info(struct edgeport_port *edge_port, unsigned int __user *value)
1545{
1546 unsigned int result = 0;
1547 unsigned long flags;
1548
1549 spin_lock_irqsave(&edge_port->ep_lock, flags);
1550 if (edge_port->maxTxCredits == edge_port->txCredits &&
1551 edge_port->txfifo.count == 0) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001552 dbg("%s -- Empty", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 result = TIOCSER_TEMT;
1554 }
1555 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1556
1557 if (copy_to_user(value, &result, sizeof(int)))
1558 return -EFAULT;
1559 return 0;
1560}
1561
1562static int get_number_bytes_avail(struct edgeport_port *edge_port, unsigned int __user *value)
1563{
1564 unsigned int result = 0;
1565 struct tty_struct *tty = edge_port->port->tty;
1566
1567 if (!tty)
1568 return -ENOIOCTLCMD;
1569
1570 result = tty->read_cnt;
1571
Harvey Harrison441b62c2008-03-03 16:08:34 -08001572 dbg("%s(%d) = %d", __func__, edge_port->port->number, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 if (copy_to_user(value, &result, sizeof(int)))
1574 return -EFAULT;
1575 //return 0;
1576 return -ENOIOCTLCMD;
1577}
1578
1579static int edge_tiocmset (struct usb_serial_port *port, struct file *file, unsigned int set, unsigned int clear)
1580{
1581 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1582 unsigned int mcr;
1583
Harvey Harrison441b62c2008-03-03 16:08:34 -08001584 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585
1586 mcr = edge_port->shadowMCR;
1587 if (set & TIOCM_RTS)
1588 mcr |= MCR_RTS;
1589 if (set & TIOCM_DTR)
1590 mcr |= MCR_DTR;
1591 if (set & TIOCM_LOOP)
1592 mcr |= MCR_LOOPBACK;
1593
1594 if (clear & TIOCM_RTS)
1595 mcr &= ~MCR_RTS;
1596 if (clear & TIOCM_DTR)
1597 mcr &= ~MCR_DTR;
1598 if (clear & TIOCM_LOOP)
1599 mcr &= ~MCR_LOOPBACK;
1600
1601 edge_port->shadowMCR = mcr;
1602
1603 send_cmd_write_uart_register(edge_port, MCR, edge_port->shadowMCR);
1604
1605 return 0;
1606}
1607
1608static int edge_tiocmget(struct usb_serial_port *port, struct file *file)
1609{
1610 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1611 unsigned int result = 0;
1612 unsigned int msr;
1613 unsigned int mcr;
1614
Harvey Harrison441b62c2008-03-03 16:08:34 -08001615 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616
1617 msr = edge_port->shadowMSR;
1618 mcr = edge_port->shadowMCR;
1619 result = ((mcr & MCR_DTR) ? TIOCM_DTR: 0) /* 0x002 */
1620 | ((mcr & MCR_RTS) ? TIOCM_RTS: 0) /* 0x004 */
1621 | ((msr & EDGEPORT_MSR_CTS) ? TIOCM_CTS: 0) /* 0x020 */
1622 | ((msr & EDGEPORT_MSR_CD) ? TIOCM_CAR: 0) /* 0x040 */
1623 | ((msr & EDGEPORT_MSR_RI) ? TIOCM_RI: 0) /* 0x080 */
1624 | ((msr & EDGEPORT_MSR_DSR) ? TIOCM_DSR: 0); /* 0x100 */
1625
1626
Harvey Harrison441b62c2008-03-03 16:08:34 -08001627 dbg("%s -- %x", __func__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628
1629 return result;
1630}
1631
1632static int get_serial_info(struct edgeport_port *edge_port, struct serial_struct __user *retinfo)
1633{
1634 struct serial_struct tmp;
1635
1636 if (!retinfo)
1637 return -EFAULT;
1638
1639 memset(&tmp, 0, sizeof(tmp));
1640
1641 tmp.type = PORT_16550A;
1642 tmp.line = edge_port->port->serial->minor;
1643 tmp.port = edge_port->port->number;
1644 tmp.irq = 0;
1645 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
1646 tmp.xmit_fifo_size = edge_port->maxTxCredits;
1647 tmp.baud_base = 9600;
1648 tmp.close_delay = 5*HZ;
1649 tmp.closing_wait = 30*HZ;
1650// tmp.custom_divisor = state->custom_divisor;
1651// tmp.hub6 = state->hub6;
1652// tmp.io_type = state->io_type;
1653
1654 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1655 return -EFAULT;
1656 return 0;
1657}
1658
1659
1660
1661/*****************************************************************************
1662 * SerialIoctl
1663 * this function handles any ioctl calls to the driver
1664 *****************************************************************************/
1665static int edge_ioctl (struct usb_serial_port *port, struct file *file, unsigned int cmd, unsigned long arg)
1666{
1667 DEFINE_WAIT(wait);
1668 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1669 struct async_icount cnow;
1670 struct async_icount cprev;
1671 struct serial_icounter_struct icount;
1672
Harvey Harrison441b62c2008-03-03 16:08:34 -08001673 dbg("%s - port %d, cmd = 0x%x", __func__, port->number, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674
1675 switch (cmd) {
1676 // return number of bytes available
1677 case TIOCINQ:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001678 dbg("%s (%d) TIOCINQ", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 return get_number_bytes_avail(edge_port, (unsigned int __user *) arg);
1680 break;
1681
1682 case TIOCSERGETLSR:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001683 dbg("%s (%d) TIOCSERGETLSR", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 return get_lsr_info(edge_port, (unsigned int __user *) arg);
1685 return 0;
1686
1687 case TIOCGSERIAL:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001688 dbg("%s (%d) TIOCGSERIAL", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 return get_serial_info(edge_port, (struct serial_struct __user *) arg);
1690
1691 case TIOCSSERIAL:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001692 dbg("%s (%d) TIOCSSERIAL", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 break;
1694
1695 case TIOCMIWAIT:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001696 dbg("%s (%d) TIOCMIWAIT", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 cprev = edge_port->icount;
1698 while (1) {
1699 prepare_to_wait(&edge_port->delta_msr_wait, &wait, TASK_INTERRUPTIBLE);
1700 schedule();
1701 finish_wait(&edge_port->delta_msr_wait, &wait);
1702 /* see if a signal did it */
1703 if (signal_pending(current))
1704 return -ERESTARTSYS;
1705 cnow = edge_port->icount;
1706 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
1707 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
1708 return -EIO; /* no change => error */
1709 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1710 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1711 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1712 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)) ) {
1713 return 0;
1714 }
1715 cprev = cnow;
1716 }
1717 /* NOTREACHED */
1718 break;
1719
1720 case TIOCGICOUNT:
1721 cnow = edge_port->icount;
1722 memset(&icount, 0, sizeof(icount));
1723 icount.cts = cnow.cts;
1724 icount.dsr = cnow.dsr;
1725 icount.rng = cnow.rng;
1726 icount.dcd = cnow.dcd;
1727 icount.rx = cnow.rx;
1728 icount.tx = cnow.tx;
1729 icount.frame = cnow.frame;
1730 icount.overrun = cnow.overrun;
1731 icount.parity = cnow.parity;
1732 icount.brk = cnow.brk;
1733 icount.buf_overrun = cnow.buf_overrun;
1734
Harvey Harrison441b62c2008-03-03 16:08:34 -08001735 dbg("%s (%d) TIOCGICOUNT RX=%d, TX=%d", __func__, port->number, icount.rx, icount.tx );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 if (copy_to_user((void __user *)arg, &icount, sizeof(icount)))
1737 return -EFAULT;
1738 return 0;
1739 }
1740
1741 return -ENOIOCTLCMD;
1742}
1743
1744
1745/*****************************************************************************
1746 * SerialBreak
1747 * this function sends a break to the port
1748 *****************************************************************************/
1749static void edge_break (struct usb_serial_port *port, int break_state)
1750{
1751 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001752 struct edgeport_serial *edge_serial = usb_get_serial_data(port->serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 int status;
1754
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001755 if ((!edge_serial->is_epic) ||
1756 ((edge_serial->is_epic) &&
1757 (edge_serial->epic_descriptor.Supports.IOSPChase))) {
1758 /* flush and chase */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001759 edge_port->chaseResponsePending = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760
Harvey Harrison441b62c2008-03-03 16:08:34 -08001761 dbg("%s - Sending IOSP_CMD_CHASE_PORT", __func__);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001762 status = send_iosp_ext_cmd (edge_port, IOSP_CMD_CHASE_PORT, 0);
1763 if (status == 0) {
1764 // block until chase finished
1765 block_until_chase_response(edge_port);
1766 } else {
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001767 edge_port->chaseResponsePending = false;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001768 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 }
1770
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001771 if ((!edge_serial->is_epic) ||
1772 ((edge_serial->is_epic) &&
1773 (edge_serial->epic_descriptor.Supports.IOSPSetClrBreak))) {
1774 if (break_state == -1) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001775 dbg("%s - Sending IOSP_CMD_SET_BREAK", __func__);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001776 status = send_iosp_ext_cmd (edge_port, IOSP_CMD_SET_BREAK, 0);
1777 } else {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001778 dbg("%s - Sending IOSP_CMD_CLEAR_BREAK", __func__);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001779 status = send_iosp_ext_cmd (edge_port, IOSP_CMD_CLEAR_BREAK, 0);
1780 }
1781 if (status) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001782 dbg("%s - error sending break set/clear command.", __func__);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001783 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 }
1785
1786 return;
1787}
1788
1789
1790/*****************************************************************************
1791 * process_rcvd_data
1792 * this function handles the data received on the bulk in pipe.
1793 *****************************************************************************/
1794static void process_rcvd_data (struct edgeport_serial *edge_serial, unsigned char * buffer, __u16 bufferLength)
1795{
1796 struct usb_serial_port *port;
1797 struct edgeport_port *edge_port;
1798 struct tty_struct *tty;
1799 __u16 lastBufferLength;
1800 __u16 rxLen;
1801
Harvey Harrison441b62c2008-03-03 16:08:34 -08001802 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803
1804 lastBufferLength = bufferLength + 1;
1805
1806 while (bufferLength > 0) {
1807 /* failsafe incase we get a message that we don't understand */
1808 if (lastBufferLength == bufferLength) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001809 dbg("%s - stuck in loop, exiting it.", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 break;
1811 }
1812 lastBufferLength = bufferLength;
1813
1814 switch (edge_serial->rxState) {
1815 case EXPECT_HDR1:
1816 edge_serial->rxHeader1 = *buffer;
1817 ++buffer;
1818 --bufferLength;
1819
1820 if (bufferLength == 0) {
1821 edge_serial->rxState = EXPECT_HDR2;
1822 break;
1823 }
1824 /* otherwise, drop on through */
1825
1826 case EXPECT_HDR2:
1827 edge_serial->rxHeader2 = *buffer;
1828 ++buffer;
1829 --bufferLength;
1830
Harvey Harrison441b62c2008-03-03 16:08:34 -08001831 dbg("%s - Hdr1=%02X Hdr2=%02X", __func__, edge_serial->rxHeader1, edge_serial->rxHeader2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832
1833 // Process depending on whether this header is
1834 // data or status
1835
1836 if (IS_CMD_STAT_HDR(edge_serial->rxHeader1)) {
1837 // Decode this status header and goto EXPECT_HDR1 (if we
1838 // can process the status with only 2 bytes), or goto
1839 // EXPECT_HDR3 to get the third byte.
1840
1841 edge_serial->rxPort = IOSP_GET_HDR_PORT(edge_serial->rxHeader1);
1842 edge_serial->rxStatusCode = IOSP_GET_STATUS_CODE(edge_serial->rxHeader1);
1843
1844 if (!IOSP_STATUS_IS_2BYTE(edge_serial->rxStatusCode)) {
1845 // This status needs additional bytes. Save what we have
1846 // and then wait for more data.
1847 edge_serial->rxStatusParam = edge_serial->rxHeader2;
1848
1849 edge_serial->rxState = EXPECT_HDR3;
1850 break;
1851 }
1852
1853 // We have all the header bytes, process the status now
1854 process_rcvd_status (edge_serial, edge_serial->rxHeader2, 0);
1855 edge_serial->rxState = EXPECT_HDR1;
1856 break;
1857 } else {
1858 edge_serial->rxPort = IOSP_GET_HDR_PORT(edge_serial->rxHeader1);
1859 edge_serial->rxBytesRemaining = IOSP_GET_HDR_DATA_LEN(edge_serial->rxHeader1, edge_serial->rxHeader2);
1860
Harvey Harrison441b62c2008-03-03 16:08:34 -08001861 dbg("%s - Data for Port %u Len %u", __func__, edge_serial->rxPort, edge_serial->rxBytesRemaining);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862
1863 //ASSERT( DevExt->RxPort < DevExt->NumPorts );
1864 //ASSERT( DevExt->RxBytesRemaining < IOSP_MAX_DATA_LENGTH );
1865
1866 if (bufferLength == 0 ) {
1867 edge_serial->rxState = EXPECT_DATA;
1868 break;
1869 }
1870 // Else, drop through
1871 }
1872
1873 case EXPECT_DATA: // Expect data
1874
1875 if (bufferLength < edge_serial->rxBytesRemaining) {
1876 rxLen = bufferLength;
1877 edge_serial->rxState = EXPECT_DATA; // Expect data to start next buffer
1878 } else {
1879 // BufLen >= RxBytesRemaining
1880 rxLen = edge_serial->rxBytesRemaining;
1881 edge_serial->rxState = EXPECT_HDR1; // Start another header next time
1882 }
1883
1884 bufferLength -= rxLen;
1885 edge_serial->rxBytesRemaining -= rxLen;
1886
1887 /* spit this data back into the tty driver if this port is open */
1888 if (rxLen) {
1889 port = edge_serial->serial->port[edge_serial->rxPort];
1890 edge_port = usb_get_serial_port_data(port);
1891 if (edge_port->open) {
1892 tty = edge_port->port->tty;
1893 if (tty) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001894 dbg("%s - Sending %d bytes to TTY for port %d", __func__, rxLen, edge_serial->rxPort);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 edge_tty_recv(&edge_serial->serial->dev->dev, tty, buffer, rxLen);
1896 }
1897 edge_port->icount.rx += rxLen;
1898 }
1899 buffer += rxLen;
1900 }
1901
1902 break;
1903
1904 case EXPECT_HDR3: // Expect 3rd byte of status header
1905 edge_serial->rxHeader3 = *buffer;
1906 ++buffer;
1907 --bufferLength;
1908
1909 // We have all the header bytes, process the status now
1910 process_rcvd_status (edge_serial, edge_serial->rxStatusParam, edge_serial->rxHeader3);
1911 edge_serial->rxState = EXPECT_HDR1;
1912 break;
1913
1914 }
1915 }
1916}
1917
1918
1919/*****************************************************************************
1920 * process_rcvd_status
1921 * this function handles the any status messages received on the bulk in pipe.
1922 *****************************************************************************/
1923static void process_rcvd_status (struct edgeport_serial *edge_serial, __u8 byte2, __u8 byte3)
1924{
1925 struct usb_serial_port *port;
1926 struct edgeport_port *edge_port;
1927 __u8 code = edge_serial->rxStatusCode;
1928
1929 /* switch the port pointer to the one being currently talked about */
1930 port = edge_serial->serial->port[edge_serial->rxPort];
1931 edge_port = usb_get_serial_port_data(port);
1932 if (edge_port == NULL) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001933 dev_err(&edge_serial->serial->dev->dev, "%s - edge_port == NULL for port %d\n", __func__, edge_serial->rxPort);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 return;
1935 }
1936
Harvey Harrison441b62c2008-03-03 16:08:34 -08001937 dbg("%s - port %d", __func__, edge_serial->rxPort);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938
1939 if (code == IOSP_EXT_STATUS) {
1940 switch (byte2) {
1941 case IOSP_EXT_STATUS_CHASE_RSP:
1942 // we want to do EXT status regardless of port open/closed
Harvey Harrison441b62c2008-03-03 16:08:34 -08001943 dbg("%s - Port %u EXT CHASE_RSP Data = %02x", __func__, edge_serial->rxPort, byte3 );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 // Currently, the only EXT_STATUS is Chase, so process here instead of one more call
1945 // to one more subroutine. If/when more EXT_STATUS, there'll be more work to do.
1946 // Also, we currently clear flag and close the port regardless of content of above's Byte3.
1947 // We could choose to do something else when Byte3 says Timeout on Chase from Edgeport,
1948 // like wait longer in block_until_chase_response, but for now we don't.
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001949 edge_port->chaseResponsePending = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 wake_up (&edge_port->wait_chase);
1951 return;
1952
1953 case IOSP_EXT_STATUS_RX_CHECK_RSP:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001954 dbg("%s ========== Port %u CHECK_RSP Sequence = %02x =============\n", __func__, edge_serial->rxPort, byte3 );
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001955 //Port->RxCheckRsp = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 return;
1957 }
1958 }
1959
1960 if (code == IOSP_STATUS_OPEN_RSP) {
1961 edge_port->txCredits = GET_TX_BUFFER_SIZE(byte3);
1962 edge_port->maxTxCredits = edge_port->txCredits;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001963 dbg("%s - Port %u Open Response Inital MSR = %02x TxBufferSize = %d", __func__, edge_serial->rxPort, byte2, edge_port->txCredits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 handle_new_msr (edge_port, byte2);
1965
1966 /* send the current line settings to the port so we are in sync with any further termios calls */
1967 if (edge_port->port->tty)
1968 change_port_settings (edge_port, edge_port->port->tty->termios);
1969
1970 /* we have completed the open */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001971 edge_port->openPending = false;
1972 edge_port->open = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 wake_up(&edge_port->wait_open);
1974 return;
1975 }
1976
1977 // If port is closed, silently discard all rcvd status. We can
1978 // have cases where buffered status is received AFTER the close
1979 // port command is sent to the Edgeport.
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001980 if (!edge_port->open || edge_port->closePending) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 return;
1982 }
1983
1984 switch (code) {
1985 // Not currently sent by Edgeport
1986 case IOSP_STATUS_LSR:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001987 dbg("%s - Port %u LSR Status = %02x", __func__, edge_serial->rxPort, byte2);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001988 handle_new_lsr(edge_port, false, byte2, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 break;
1990
1991 case IOSP_STATUS_LSR_DATA:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001992 dbg("%s - Port %u LSR Status = %02x, Data = %02x", __func__, edge_serial->rxPort, byte2, byte3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 // byte2 is LSR Register
1994 // byte3 is broken data byte
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001995 handle_new_lsr(edge_port, true, byte2, byte3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 break;
1997 //
1998 // case IOSP_EXT_4_STATUS:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001999 // dbg("%s - Port %u LSR Status = %02x Data = %02x", __func__, edge_serial->rxPort, byte2, byte3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 // break;
2001 //
2002 case IOSP_STATUS_MSR:
Harvey Harrison441b62c2008-03-03 16:08:34 -08002003 dbg("%s - Port %u MSR Status = %02x", __func__, edge_serial->rxPort, byte2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004
2005 // Process this new modem status and generate appropriate
2006 // events, etc, based on the new status. This routine
2007 // also saves the MSR in Port->ShadowMsr.
2008 handle_new_msr(edge_port, byte2);
2009 break;
2010
2011 default:
Harvey Harrison441b62c2008-03-03 16:08:34 -08002012 dbg("%s - Unrecognized IOSP status code %u\n", __func__, code);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 break;
2014 }
2015
2016 return;
2017}
2018
2019
2020/*****************************************************************************
2021 * edge_tty_recv
2022 * this function passes data on to the tty flip buffer
2023 *****************************************************************************/
2024static void edge_tty_recv(struct device *dev, struct tty_struct *tty, unsigned char *data, int length)
2025{
2026 int cnt;
2027
2028 do {
Alan Cox33f0f882006-01-09 20:54:13 -08002029 cnt = tty_buffer_request_room(tty, length);
2030 if (cnt < length) {
2031 dev_err(dev, "%s - dropping data, %d bytes lost\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -08002032 __func__, length - cnt);
Alan Cox33f0f882006-01-09 20:54:13 -08002033 if(cnt == 0)
2034 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 }
Alan Cox33f0f882006-01-09 20:54:13 -08002036 tty_insert_flip_string(tty, data, cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 data += cnt;
2038 length -= cnt;
2039 } while (length > 0);
2040
2041 tty_flip_buffer_push(tty);
2042}
2043
2044
2045/*****************************************************************************
2046 * handle_new_msr
2047 * this function handles any change to the msr register for a port.
2048 *****************************************************************************/
2049static void handle_new_msr(struct edgeport_port *edge_port, __u8 newMsr)
2050{
2051 struct async_icount *icount;
2052
Harvey Harrison441b62c2008-03-03 16:08:34 -08002053 dbg("%s %02x", __func__, newMsr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054
2055 if (newMsr & (EDGEPORT_MSR_DELTA_CTS | EDGEPORT_MSR_DELTA_DSR | EDGEPORT_MSR_DELTA_RI | EDGEPORT_MSR_DELTA_CD)) {
2056 icount = &edge_port->icount;
2057
2058 /* update input line counters */
2059 if (newMsr & EDGEPORT_MSR_DELTA_CTS) {
2060 icount->cts++;
2061 }
2062 if (newMsr & EDGEPORT_MSR_DELTA_DSR) {
2063 icount->dsr++;
2064 }
2065 if (newMsr & EDGEPORT_MSR_DELTA_CD) {
2066 icount->dcd++;
2067 }
2068 if (newMsr & EDGEPORT_MSR_DELTA_RI) {
2069 icount->rng++;
2070 }
2071 wake_up_interruptible(&edge_port->delta_msr_wait);
2072 }
2073
2074 /* Save the new modem status */
2075 edge_port->shadowMSR = newMsr & 0xf0;
2076
2077 return;
2078}
2079
2080
2081/*****************************************************************************
2082 * handle_new_lsr
2083 * this function handles any change to the lsr register for a port.
2084 *****************************************************************************/
2085static void handle_new_lsr(struct edgeport_port *edge_port, __u8 lsrData, __u8 lsr, __u8 data)
2086{
2087 __u8 newLsr = (__u8)(lsr & (__u8)(LSR_OVER_ERR | LSR_PAR_ERR | LSR_FRM_ERR | LSR_BREAK));
2088 struct async_icount *icount;
2089
Harvey Harrison441b62c2008-03-03 16:08:34 -08002090 dbg("%s - %02x", __func__, newLsr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091
2092 edge_port->shadowLSR = lsr;
2093
2094 if (newLsr & LSR_BREAK) {
2095 //
2096 // Parity and Framing errors only count if they
2097 // occur exclusive of a break being
2098 // received.
2099 //
2100 newLsr &= (__u8)(LSR_OVER_ERR | LSR_BREAK);
2101 }
2102
2103 /* Place LSR data byte into Rx buffer */
2104 if (lsrData && edge_port->port->tty)
2105 edge_tty_recv(&edge_port->port->dev, edge_port->port->tty, &data, 1);
2106
2107 /* update input line counters */
2108 icount = &edge_port->icount;
2109 if (newLsr & LSR_BREAK) {
2110 icount->brk++;
2111 }
2112 if (newLsr & LSR_OVER_ERR) {
2113 icount->overrun++;
2114 }
2115 if (newLsr & LSR_PAR_ERR) {
2116 icount->parity++;
2117 }
2118 if (newLsr & LSR_FRM_ERR) {
2119 icount->frame++;
2120 }
2121
2122 return;
2123}
2124
2125
2126/****************************************************************************
2127 * sram_write
2128 * writes a number of bytes to the Edgeport device's sram starting at the
2129 * given address.
2130 * If successful returns the number of bytes written, otherwise it returns
2131 * a negative error number of the problem.
2132 ****************************************************************************/
2133static int sram_write (struct usb_serial *serial, __u16 extAddr, __u16 addr, __u16 length, __u8 *data)
2134{
2135 int result;
2136 __u16 current_length;
2137 unsigned char *transfer_buffer;
2138
Harvey Harrison441b62c2008-03-03 16:08:34 -08002139 dbg("%s - %x, %x, %d", __func__, extAddr, addr, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140
2141 transfer_buffer = kmalloc (64, GFP_KERNEL);
2142 if (!transfer_buffer) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08002143 dev_err(&serial->dev->dev, "%s - kmalloc(%d) failed.\n", __func__, 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 return -ENOMEM;
2145 }
2146
2147 /* need to split these writes up into 64 byte chunks */
2148 result = 0;
2149 while (length > 0) {
2150 if (length > 64) {
2151 current_length = 64;
2152 } else {
2153 current_length = length;
2154 }
Harvey Harrison441b62c2008-03-03 16:08:34 -08002155// dbg("%s - writing %x, %x, %d", __func__, extAddr, addr, current_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 memcpy (transfer_buffer, data, current_length);
2157 result = usb_control_msg (serial->dev, usb_sndctrlpipe(serial->dev, 0), USB_REQUEST_ION_WRITE_RAM,
2158 0x40, addr, extAddr, transfer_buffer, current_length, 300);
2159 if (result < 0)
2160 break;
2161 length -= current_length;
2162 addr += current_length;
2163 data += current_length;
2164 }
2165
2166 kfree (transfer_buffer);
2167 return result;
2168}
2169
2170
2171/****************************************************************************
2172 * rom_write
2173 * writes a number of bytes to the Edgeport device's ROM starting at the
2174 * given address.
2175 * If successful returns the number of bytes written, otherwise it returns
2176 * a negative error number of the problem.
2177 ****************************************************************************/
2178static int rom_write (struct usb_serial *serial, __u16 extAddr, __u16 addr, __u16 length, __u8 *data)
2179{
2180 int result;
2181 __u16 current_length;
2182 unsigned char *transfer_buffer;
2183
Harvey Harrison441b62c2008-03-03 16:08:34 -08002184// dbg("%s - %x, %x, %d", __func__, extAddr, addr, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185
2186 transfer_buffer = kmalloc (64, GFP_KERNEL);
2187 if (!transfer_buffer) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08002188 dev_err(&serial->dev->dev, "%s - kmalloc(%d) failed.\n", __func__, 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 return -ENOMEM;
2190 }
2191
2192 /* need to split these writes up into 64 byte chunks */
2193 result = 0;
2194 while (length > 0) {
2195 if (length > 64) {
2196 current_length = 64;
2197 } else {
2198 current_length = length;
2199 }
Harvey Harrison441b62c2008-03-03 16:08:34 -08002200// dbg("%s - writing %x, %x, %d", __func__, extAddr, addr, current_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 memcpy (transfer_buffer, data, current_length);
2202 result = usb_control_msg (serial->dev, usb_sndctrlpipe(serial->dev, 0), USB_REQUEST_ION_WRITE_ROM,
2203 0x40, addr, extAddr, transfer_buffer, current_length, 300);
2204 if (result < 0)
2205 break;
2206 length -= current_length;
2207 addr += current_length;
2208 data += current_length;
2209 }
2210
2211 kfree (transfer_buffer);
2212 return result;
2213}
2214
2215
2216/****************************************************************************
2217 * rom_read
2218 * reads a number of bytes from the Edgeport device starting at the given
2219 * address.
2220 * If successful returns the number of bytes read, otherwise it returns
2221 * a negative error number of the problem.
2222 ****************************************************************************/
2223static int rom_read (struct usb_serial *serial, __u16 extAddr, __u16 addr, __u16 length, __u8 *data)
2224{
2225 int result;
2226 __u16 current_length;
2227 unsigned char *transfer_buffer;
2228
Harvey Harrison441b62c2008-03-03 16:08:34 -08002229 dbg("%s - %x, %x, %d", __func__, extAddr, addr, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230
2231 transfer_buffer = kmalloc (64, GFP_KERNEL);
2232 if (!transfer_buffer) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08002233 dev_err(&serial->dev->dev, "%s - kmalloc(%d) failed.\n", __func__, 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 return -ENOMEM;
2235 }
2236
2237 /* need to split these reads up into 64 byte chunks */
2238 result = 0;
2239 while (length > 0) {
2240 if (length > 64) {
2241 current_length = 64;
2242 } else {
2243 current_length = length;
2244 }
Harvey Harrison441b62c2008-03-03 16:08:34 -08002245// dbg("%s - %x, %x, %d", __func__, extAddr, addr, current_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 result = usb_control_msg (serial->dev, usb_rcvctrlpipe(serial->dev, 0), USB_REQUEST_ION_READ_ROM,
2247 0xC0, addr, extAddr, transfer_buffer, current_length, 300);
2248 if (result < 0)
2249 break;
2250 memcpy (data, transfer_buffer, current_length);
2251 length -= current_length;
2252 addr += current_length;
2253 data += current_length;
2254 }
2255
2256 kfree (transfer_buffer);
2257 return result;
2258}
2259
2260
2261/****************************************************************************
2262 * send_iosp_ext_cmd
2263 * Is used to send a IOSP message to the Edgeport device
2264 ****************************************************************************/
2265static int send_iosp_ext_cmd (struct edgeport_port *edge_port, __u8 command, __u8 param)
2266{
2267 unsigned char *buffer;
2268 unsigned char *currentCommand;
2269 int length = 0;
2270 int status = 0;
2271
Harvey Harrison441b62c2008-03-03 16:08:34 -08002272 dbg("%s - %d, %d", __func__, command, param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273
2274 buffer = kmalloc (10, GFP_ATOMIC);
2275 if (!buffer) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08002276 dev_err(&edge_port->port->dev, "%s - kmalloc(%d) failed.\n", __func__, 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 return -ENOMEM;
2278 }
2279
2280 currentCommand = buffer;
2281
2282 MAKE_CMD_EXT_CMD (&currentCommand, &length,
2283 edge_port->port->number - edge_port->port->serial->minor,
2284 command, param);
2285
2286 status = write_cmd_usb (edge_port, buffer, length);
2287 if (status) {
2288 /* something bad happened, let's free up the memory */
2289 kfree(buffer);
2290 }
2291
2292 return status;
2293}
2294
2295
2296/*****************************************************************************
2297 * write_cmd_usb
2298 * this function writes the given buffer out to the bulk write endpoint.
2299 *****************************************************************************/
2300static int write_cmd_usb (struct edgeport_port *edge_port, unsigned char *buffer, int length)
2301{
2302 struct edgeport_serial *edge_serial = usb_get_serial_data(edge_port->port->serial);
2303 int status = 0;
2304 struct urb *urb;
2305 int timeout;
2306
Harvey Harrison441b62c2008-03-03 16:08:34 -08002307 usb_serial_debug_data(debug, &edge_port->port->dev, __func__, length, buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308
2309 /* Allocate our next urb */
2310 urb = usb_alloc_urb (0, GFP_ATOMIC);
2311 if (!urb)
2312 return -ENOMEM;
2313
Oliver Neukum96c706e2007-03-15 15:27:17 +01002314 atomic_inc(&CmdUrbs);
Harvey Harrison441b62c2008-03-03 16:08:34 -08002315 dbg("%s - ALLOCATE URB %p (outstanding %d)", __func__, urb, atomic_read(&CmdUrbs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316
2317 usb_fill_bulk_urb (urb, edge_serial->serial->dev,
2318 usb_sndbulkpipe(edge_serial->serial->dev, edge_serial->bulk_out_endpoint),
2319 buffer, length, edge_bulk_out_cmd_callback, edge_port);
2320
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01002321 edge_port->commandPending = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 status = usb_submit_urb(urb, GFP_ATOMIC);
2323
2324 if (status) {
2325 /* something went wrong */
Harvey Harrison441b62c2008-03-03 16:08:34 -08002326 dev_err(&edge_port->port->dev, "%s - usb_submit_urb(write command) failed, status = %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 usb_kill_urb(urb);
2328 usb_free_urb(urb);
Oliver Neukum96c706e2007-03-15 15:27:17 +01002329 atomic_dec(&CmdUrbs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 return status;
2331 }
2332
2333 // wait for command to finish
2334 timeout = COMMAND_TIMEOUT;
2335#if 0
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01002336 wait_event (&edge_port->wait_command, !edge_port->commandPending);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01002338 if (edge_port->commandPending) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 /* command timed out */
Harvey Harrison441b62c2008-03-03 16:08:34 -08002340 dbg("%s - command timed out", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 status = -EINVAL;
2342 }
2343#endif
2344 return status;
2345}
2346
2347
2348/*****************************************************************************
2349 * send_cmd_write_baud_rate
2350 * this function sends the proper command to change the baud rate of the
2351 * specified port.
2352 *****************************************************************************/
2353static int send_cmd_write_baud_rate (struct edgeport_port *edge_port, int baudRate)
2354{
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002355 struct edgeport_serial *edge_serial = usb_get_serial_data(edge_port->port->serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 unsigned char *cmdBuffer;
2357 unsigned char *currCmd;
2358 int cmdLen = 0;
2359 int divisor;
2360 int status;
2361 unsigned char number = edge_port->port->number - edge_port->port->serial->minor;
2362
Adam Kropelinbc71e472007-07-29 11:03:29 -04002363 if (edge_serial->is_epic &&
2364 !edge_serial->epic_descriptor.Supports.IOSPSetBaudRate) {
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002365 dbg("SendCmdWriteBaudRate - NOT Setting baud rate for port = %d, baud = %d",
2366 edge_port->port->number, baudRate);
2367 return 0;
2368 }
2369
Harvey Harrison441b62c2008-03-03 16:08:34 -08002370 dbg("%s - port = %d, baud = %d", __func__, edge_port->port->number, baudRate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371
2372 status = calc_baud_rate_divisor (baudRate, &divisor);
2373 if (status) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08002374 dev_err(&edge_port->port->dev, "%s - bad baud rate\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 return status;
2376 }
2377
2378 // Alloc memory for the string of commands.
2379 cmdBuffer = kmalloc (0x100, GFP_ATOMIC);
2380 if (!cmdBuffer) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08002381 dev_err(&edge_port->port->dev, "%s - kmalloc(%d) failed.\n", __func__, 0x100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 return -ENOMEM;
2383 }
2384 currCmd = cmdBuffer;
2385
2386 // Enable access to divisor latch
2387 MAKE_CMD_WRITE_REG( &currCmd, &cmdLen, number, LCR, LCR_DL_ENABLE );
2388
2389 // Write the divisor itself
2390 MAKE_CMD_WRITE_REG( &currCmd, &cmdLen, number, DLL, LOW8 (divisor) );
2391 MAKE_CMD_WRITE_REG( &currCmd, &cmdLen, number, DLM, HIGH8(divisor) );
2392
2393 // Restore original value to disable access to divisor latch
2394 MAKE_CMD_WRITE_REG( &currCmd, &cmdLen, number, LCR, edge_port->shadowLCR);
2395
2396 status = write_cmd_usb(edge_port, cmdBuffer, cmdLen );
2397 if (status) {
2398 /* something bad happened, let's free up the memory */
2399 kfree (cmdBuffer);
2400 }
2401
2402 return status;
2403}
2404
2405
2406/*****************************************************************************
2407 * calc_baud_rate_divisor
2408 * this function calculates the proper baud rate divisor for the specified
2409 * baud rate.
2410 *****************************************************************************/
2411static int calc_baud_rate_divisor (int baudrate, int *divisor)
2412{
2413 int i;
2414 __u16 custom;
2415
2416
Harvey Harrison441b62c2008-03-03 16:08:34 -08002417 dbg("%s - %d", __func__, baudrate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418
Tobias Klauser52950ed2005-12-11 16:20:08 +01002419 for (i = 0; i < ARRAY_SIZE(divisor_table); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 if ( divisor_table[i].BaudRate == baudrate ) {
2421 *divisor = divisor_table[i].Divisor;
2422 return 0;
2423 }
2424 }
2425
2426 // We have tried all of the standard baud rates
2427 // lets try to calculate the divisor for this baud rate
2428 // Make sure the baud rate is reasonable
2429 if (baudrate > 50 && baudrate < 230400) {
2430 // get divisor
2431 custom = (__u16)((230400L + baudrate/2) / baudrate);
2432
2433 *divisor = custom;
2434
Harvey Harrison441b62c2008-03-03 16:08:34 -08002435 dbg("%s - Baud %d = %d\n", __func__, baudrate, custom);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 return 0;
2437 }
2438
2439 return -1;
2440}
2441
2442
2443/*****************************************************************************
2444 * send_cmd_write_uart_register
2445 * this function builds up a uart register message and sends to to the device.
2446 *****************************************************************************/
2447static int send_cmd_write_uart_register (struct edgeport_port *edge_port, __u8 regNum, __u8 regValue)
2448{
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002449 struct edgeport_serial *edge_serial = usb_get_serial_data(edge_port->port->serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 unsigned char *cmdBuffer;
2451 unsigned char *currCmd;
2452 unsigned long cmdLen = 0;
2453 int status;
2454
Harvey Harrison441b62c2008-03-03 16:08:34 -08002455 dbg("%s - write to %s register 0x%02x", (regNum == MCR) ? "MCR" : "LCR", __func__, regValue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456
Adam Kropelinbc71e472007-07-29 11:03:29 -04002457 if (edge_serial->is_epic &&
2458 !edge_serial->epic_descriptor.Supports.IOSPWriteMCR &&
2459 regNum == MCR) {
Robert P. J. Daybeb7dd82007-05-09 07:14:03 +02002460 dbg("SendCmdWriteUartReg - Not writing to MCR Register");
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002461 return 0;
2462 }
2463
Adam Kropelinbc71e472007-07-29 11:03:29 -04002464 if (edge_serial->is_epic &&
2465 !edge_serial->epic_descriptor.Supports.IOSPWriteLCR &&
2466 regNum == LCR) {
Robert P. J. Daybeb7dd82007-05-09 07:14:03 +02002467 dbg ("SendCmdWriteUartReg - Not writing to LCR Register");
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002468 return 0;
2469 }
2470
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 // Alloc memory for the string of commands.
2472 cmdBuffer = kmalloc (0x10, GFP_ATOMIC);
2473 if (cmdBuffer == NULL ) {
2474 return -ENOMEM;
2475 }
2476
2477 currCmd = cmdBuffer;
2478
2479 // Build a cmd in the buffer to write the given register
2480 MAKE_CMD_WRITE_REG (&currCmd, &cmdLen,
2481 edge_port->port->number - edge_port->port->serial->minor,
2482 regNum, regValue);
2483
2484 status = write_cmd_usb(edge_port, cmdBuffer, cmdLen);
2485 if (status) {
2486 /* something bad happened, let's free up the memory */
2487 kfree (cmdBuffer);
2488 }
2489
2490 return status;
2491}
2492
2493
2494/*****************************************************************************
2495 * change_port_settings
2496 * This routine is called to set the UART on the device to match the specified
2497 * new settings.
2498 *****************************************************************************/
2499#ifndef CMSPAR
2500#define CMSPAR 0
2501#endif
Alan Cox606d0992006-12-08 02:38:45 -08002502static void change_port_settings (struct edgeport_port *edge_port, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503{
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002504 struct edgeport_serial *edge_serial = usb_get_serial_data(edge_port->port->serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 struct tty_struct *tty;
2506 int baud;
2507 unsigned cflag;
2508 __u8 mask = 0xff;
2509 __u8 lData;
2510 __u8 lParity;
2511 __u8 lStop;
2512 __u8 rxFlow;
2513 __u8 txFlow;
2514 int status;
2515
Harvey Harrison441b62c2008-03-03 16:08:34 -08002516 dbg("%s - port %d", __func__, edge_port->port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01002518 if (!edge_port->open &&
2519 !edge_port->openPending) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08002520 dbg("%s - port not opened", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 return;
2522 }
2523
2524 tty = edge_port->port->tty;
2525 if ((!tty) ||
2526 (!tty->termios)) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08002527 dbg("%s - no tty structures", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 return;
2529 }
2530
2531 cflag = tty->termios->c_cflag;
2532
2533 switch (cflag & CSIZE) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08002534 case CS5: lData = LCR_BITS_5; mask = 0x1f; dbg("%s - data bits = 5", __func__); break;
2535 case CS6: lData = LCR_BITS_6; mask = 0x3f; dbg("%s - data bits = 6", __func__); break;
2536 case CS7: lData = LCR_BITS_7; mask = 0x7f; dbg("%s - data bits = 7", __func__); break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 default:
Harvey Harrison441b62c2008-03-03 16:08:34 -08002538 case CS8: lData = LCR_BITS_8; dbg("%s - data bits = 8", __func__); break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 }
2540
2541 lParity = LCR_PAR_NONE;
2542 if (cflag & PARENB) {
2543 if (cflag & CMSPAR) {
2544 if (cflag & PARODD) {
2545 lParity = LCR_PAR_MARK;
Harvey Harrison441b62c2008-03-03 16:08:34 -08002546 dbg("%s - parity = mark", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 } else {
2548 lParity = LCR_PAR_SPACE;
Harvey Harrison441b62c2008-03-03 16:08:34 -08002549 dbg("%s - parity = space", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550 }
2551 } else if (cflag & PARODD) {
2552 lParity = LCR_PAR_ODD;
Harvey Harrison441b62c2008-03-03 16:08:34 -08002553 dbg("%s - parity = odd", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 } else {
2555 lParity = LCR_PAR_EVEN;
Harvey Harrison441b62c2008-03-03 16:08:34 -08002556 dbg("%s - parity = even", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557 }
2558 } else {
Harvey Harrison441b62c2008-03-03 16:08:34 -08002559 dbg("%s - parity = none", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560 }
2561
2562 if (cflag & CSTOPB) {
2563 lStop = LCR_STOP_2;
Harvey Harrison441b62c2008-03-03 16:08:34 -08002564 dbg("%s - stop bits = 2", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 } else {
2566 lStop = LCR_STOP_1;
Harvey Harrison441b62c2008-03-03 16:08:34 -08002567 dbg("%s - stop bits = 1", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 }
2569
2570 /* figure out the flow control settings */
2571 rxFlow = txFlow = 0x00;
2572 if (cflag & CRTSCTS) {
2573 rxFlow |= IOSP_RX_FLOW_RTS;
2574 txFlow |= IOSP_TX_FLOW_CTS;
Harvey Harrison441b62c2008-03-03 16:08:34 -08002575 dbg("%s - RTS/CTS is enabled", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 } else {
Harvey Harrison441b62c2008-03-03 16:08:34 -08002577 dbg("%s - RTS/CTS is disabled", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 }
2579
2580 /* if we are implementing XON/XOFF, set the start and stop character in the device */
2581 if (I_IXOFF(tty) || I_IXON(tty)) {
2582 unsigned char stop_char = STOP_CHAR(tty);
2583 unsigned char start_char = START_CHAR(tty);
2584
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002585 if ((!edge_serial->is_epic) ||
2586 ((edge_serial->is_epic) &&
2587 (edge_serial->epic_descriptor.Supports.IOSPSetXChar))) {
2588 send_iosp_ext_cmd(edge_port, IOSP_CMD_SET_XON_CHAR, start_char);
2589 send_iosp_ext_cmd(edge_port, IOSP_CMD_SET_XOFF_CHAR, stop_char);
2590 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591
2592 /* if we are implementing INBOUND XON/XOFF */
2593 if (I_IXOFF(tty)) {
2594 rxFlow |= IOSP_RX_FLOW_XON_XOFF;
Harvey Harrison441b62c2008-03-03 16:08:34 -08002595 dbg("%s - INBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x", __func__, start_char, stop_char);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 } else {
Harvey Harrison441b62c2008-03-03 16:08:34 -08002597 dbg("%s - INBOUND XON/XOFF is disabled", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 }
2599
2600 /* if we are implementing OUTBOUND XON/XOFF */
2601 if (I_IXON(tty)) {
2602 txFlow |= IOSP_TX_FLOW_XON_XOFF;
Harvey Harrison441b62c2008-03-03 16:08:34 -08002603 dbg("%s - OUTBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x", __func__, start_char, stop_char);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 } else {
Harvey Harrison441b62c2008-03-03 16:08:34 -08002605 dbg("%s - OUTBOUND XON/XOFF is disabled", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 }
2607 }
2608
2609 /* Set flow control to the configured value */
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002610 if ((!edge_serial->is_epic) ||
2611 ((edge_serial->is_epic) &&
2612 (edge_serial->epic_descriptor.Supports.IOSPSetRxFlow)))
2613 send_iosp_ext_cmd(edge_port, IOSP_CMD_SET_RX_FLOW, rxFlow);
2614 if ((!edge_serial->is_epic) ||
2615 ((edge_serial->is_epic) &&
2616 (edge_serial->epic_descriptor.Supports.IOSPSetTxFlow)))
2617 send_iosp_ext_cmd(edge_port, IOSP_CMD_SET_TX_FLOW, txFlow);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618
2619
2620 edge_port->shadowLCR &= ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
2621 edge_port->shadowLCR |= (lData | lParity | lStop);
2622
2623 edge_port->validDataMask = mask;
2624
2625 /* Send the updated LCR value to the EdgePort */
2626 status = send_cmd_write_uart_register(edge_port, LCR, edge_port->shadowLCR);
2627 if (status != 0) {
2628 return;
2629 }
2630
2631 /* set up the MCR register and send it to the EdgePort */
2632 edge_port->shadowMCR = MCR_MASTER_IE;
2633 if (cflag & CBAUD) {
2634 edge_port->shadowMCR |= (MCR_DTR | MCR_RTS);
2635 }
2636 status = send_cmd_write_uart_register(edge_port, MCR, edge_port->shadowMCR);
2637 if (status != 0) {
2638 return;
2639 }
2640
2641 /* Determine divisor based on baud rate */
2642 baud = tty_get_baud_rate(tty);
2643 if (!baud) {
2644 /* pick a default, any default... */
2645 baud = 9600;
2646 }
2647
Harvey Harrison441b62c2008-03-03 16:08:34 -08002648 dbg("%s - baud rate = %d", __func__, baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 status = send_cmd_write_baud_rate (edge_port, baud);
Alan Cox6ce073b2007-10-18 01:24:25 -07002650 if (status == -1) {
2651 /* Speed change was not possible - put back the old speed */
2652 baud = tty_termios_baud_rate(old_termios);
2653 tty_encode_baud_rate(tty, baud, baud);
2654 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655 return;
2656}
2657
2658
2659/****************************************************************************
2660 * unicode_to_ascii
2661 * Turns a string from Unicode into ASCII.
2662 * Doesn't do a good job with any characters that are outside the normal
2663 * ASCII range, but it's only for debugging...
2664 * NOTE: expects the unicode in LE format
2665 ****************************************************************************/
Pete Zaitcevad933752006-05-22 21:49:44 -07002666static void unicode_to_ascii(char *string, int buflen, __le16 *unicode, int unicode_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667{
2668 int i;
2669
Pete Zaitcevad933752006-05-22 21:49:44 -07002670 if (buflen <= 0) /* never happens, but... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 return;
Pete Zaitcevad933752006-05-22 21:49:44 -07002672 --buflen; /* space for nul */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673
Pete Zaitcevad933752006-05-22 21:49:44 -07002674 for (i = 0; i < unicode_size; i++) {
2675 if (i >= buflen)
2676 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677 string[i] = (char)(le16_to_cpu(unicode[i]));
Pete Zaitcevad933752006-05-22 21:49:44 -07002678 }
2679 string[i] = 0x00;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680}
2681
2682
2683/****************************************************************************
2684 * get_manufacturing_desc
2685 * reads in the manufacturing descriptor and stores it into the serial
2686 * structure.
2687 ****************************************************************************/
2688static void get_manufacturing_desc (struct edgeport_serial *edge_serial)
2689{
2690 int response;
2691
2692 dbg("getting manufacturer descriptor");
2693
2694 response = rom_read (edge_serial->serial, (EDGE_MANUF_DESC_ADDR & 0xffff0000) >> 16,
2695 (__u16)(EDGE_MANUF_DESC_ADDR & 0x0000ffff), EDGE_MANUF_DESC_LEN,
2696 (__u8 *)(&edge_serial->manuf_descriptor));
2697
2698 if (response < 1) {
2699 dev_err(&edge_serial->serial->dev->dev, "error in getting manufacturer descriptor\n");
2700 } else {
2701 char string[30];
2702 dbg("**Manufacturer Descriptor");
2703 dbg(" RomSize: %dK", edge_serial->manuf_descriptor.RomSize);
2704 dbg(" RamSize: %dK", edge_serial->manuf_descriptor.RamSize);
2705 dbg(" CpuRev: %d", edge_serial->manuf_descriptor.CpuRev);
2706 dbg(" BoardRev: %d", edge_serial->manuf_descriptor.BoardRev);
2707 dbg(" NumPorts: %d", edge_serial->manuf_descriptor.NumPorts);
2708 dbg(" DescDate: %d/%d/%d", edge_serial->manuf_descriptor.DescDate[0], edge_serial->manuf_descriptor.DescDate[1], edge_serial->manuf_descriptor.DescDate[2]+1900);
Pete Zaitcev4bc203d2006-06-06 18:18:33 -07002709 unicode_to_ascii(string, sizeof(string),
Pete Zaitcevad933752006-05-22 21:49:44 -07002710 edge_serial->manuf_descriptor.SerialNumber,
2711 edge_serial->manuf_descriptor.SerNumLength/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 dbg(" SerialNumber: %s", string);
Pete Zaitcev4bc203d2006-06-06 18:18:33 -07002713 unicode_to_ascii(string, sizeof(string),
Pete Zaitcevad933752006-05-22 21:49:44 -07002714 edge_serial->manuf_descriptor.AssemblyNumber,
2715 edge_serial->manuf_descriptor.AssemblyNumLength/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 dbg(" AssemblyNumber: %s", string);
Pete Zaitcev4bc203d2006-06-06 18:18:33 -07002717 unicode_to_ascii(string, sizeof(string),
Pete Zaitcevad933752006-05-22 21:49:44 -07002718 edge_serial->manuf_descriptor.OemAssyNumber,
2719 edge_serial->manuf_descriptor.OemAssyNumLength/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 dbg(" OemAssyNumber: %s", string);
2721 dbg(" UartType: %d", edge_serial->manuf_descriptor.UartType);
2722 dbg(" IonPid: %d", edge_serial->manuf_descriptor.IonPid);
2723 dbg(" IonConfig: %d", edge_serial->manuf_descriptor.IonConfig);
2724 }
2725}
2726
2727
2728/****************************************************************************
2729 * get_boot_desc
2730 * reads in the bootloader descriptor and stores it into the serial
2731 * structure.
2732 ****************************************************************************/
2733static void get_boot_desc (struct edgeport_serial *edge_serial)
2734{
2735 int response;
2736
2737 dbg("getting boot descriptor");
2738
2739 response = rom_read (edge_serial->serial, (EDGE_BOOT_DESC_ADDR & 0xffff0000) >> 16,
2740 (__u16)(EDGE_BOOT_DESC_ADDR & 0x0000ffff), EDGE_BOOT_DESC_LEN,
2741 (__u8 *)(&edge_serial->boot_descriptor));
2742
2743 if (response < 1) {
2744 dev_err(&edge_serial->serial->dev->dev, "error in getting boot descriptor\n");
2745 } else {
2746 dbg("**Boot Descriptor:");
2747 dbg(" BootCodeLength: %d", le16_to_cpu(edge_serial->boot_descriptor.BootCodeLength));
2748 dbg(" MajorVersion: %d", edge_serial->boot_descriptor.MajorVersion);
2749 dbg(" MinorVersion: %d", edge_serial->boot_descriptor.MinorVersion);
2750 dbg(" BuildNumber: %d", le16_to_cpu(edge_serial->boot_descriptor.BuildNumber));
2751 dbg(" Capabilities: 0x%x", le16_to_cpu(edge_serial->boot_descriptor.Capabilities));
2752 dbg(" UConfig0: %d", edge_serial->boot_descriptor.UConfig0);
2753 dbg(" UConfig1: %d", edge_serial->boot_descriptor.UConfig1);
2754 }
2755}
2756
2757
2758/****************************************************************************
2759 * load_application_firmware
2760 * This is called to load the application firmware to the device
2761 ****************************************************************************/
2762static void load_application_firmware (struct edgeport_serial *edge_serial)
2763{
2764 struct edge_firmware_image_record *record;
2765 unsigned char *firmware;
2766 unsigned char *FirmwareImage;
2767 int ImageSize;
2768 int response;
2769
2770
2771 switch (edge_serial->product_info.iDownloadFile) {
2772 case EDGE_DOWNLOAD_FILE_I930:
2773 dbg("downloading firmware version (930) %d.%d.%d",
2774 OperationalCodeImageVersion_GEN1.MajorVersion,
2775 OperationalCodeImageVersion_GEN1.MinorVersion,
2776 OperationalCodeImageVersion_GEN1.BuildNumber);
2777 firmware = &OperationalCodeImage_GEN1[0];
2778 FirmwareImage = &OperationalCodeImage_GEN1[0];
2779 ImageSize = sizeof(OperationalCodeImage_GEN1);
2780 break;
2781
2782 case EDGE_DOWNLOAD_FILE_80251:
2783 dbg("downloading firmware version (80251) %d.%d.%d",
2784 OperationalCodeImageVersion_GEN2.MajorVersion,
2785 OperationalCodeImageVersion_GEN2.MinorVersion,
2786 OperationalCodeImageVersion_GEN2.BuildNumber);
2787 firmware = &OperationalCodeImage_GEN2[0];
2788 FirmwareImage = &OperationalCodeImage_GEN2[0];
2789 ImageSize = sizeof(OperationalCodeImage_GEN2);
2790 break;
2791
2792 case EDGE_DOWNLOAD_FILE_NONE:
2793 dbg ("No download file specified, skipping download\n");
2794 return;
2795
2796 default:
2797 return;
2798 }
2799
2800
2801 for (;;) {
2802 record = (struct edge_firmware_image_record *)firmware;
2803 response = sram_write (edge_serial->serial, le16_to_cpu(record->ExtAddr), le16_to_cpu(record->Addr), le16_to_cpu(record->Len), &record->Data[0]);
2804 if (response < 0) {
2805 dev_err(&edge_serial->serial->dev->dev, "sram_write failed (%x, %x, %d)\n", le16_to_cpu(record->ExtAddr), le16_to_cpu(record->Addr), le16_to_cpu(record->Len));
2806 break;
2807 }
2808 firmware += sizeof (struct edge_firmware_image_record) + le16_to_cpu(record->Len);
2809 if (firmware >= &FirmwareImage[ImageSize]) {
2810 break;
2811 }
2812 }
2813
2814 dbg("sending exec_dl_code");
2815 response = usb_control_msg (edge_serial->serial->dev,
2816 usb_sndctrlpipe(edge_serial->serial->dev, 0),
2817 USB_REQUEST_ION_EXEC_DL_CODE,
2818 0x40, 0x4000, 0x0001, NULL, 0, 3000);
2819
2820 return;
2821}
2822
2823
2824/****************************************************************************
2825 * edge_startup
2826 ****************************************************************************/
2827static int edge_startup (struct usb_serial *serial)
2828{
2829 struct edgeport_serial *edge_serial;
2830 struct edgeport_port *edge_port;
2831 struct usb_device *dev;
Chris Lundbfd5df32006-06-03 13:58:19 -07002832 int i, j;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002833 int response;
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01002834 bool interrupt_in_found;
2835 bool bulk_in_found;
2836 bool bulk_out_found;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002837 static __u32 descriptor[3] = { EDGE_COMPATIBILITY_MASK0,
2838 EDGE_COMPATIBILITY_MASK1,
2839 EDGE_COMPATIBILITY_MASK2 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840
2841 dev = serial->dev;
2842
2843 /* create our private serial structure */
Eric Sesterhenn80b6ca42006-02-27 21:29:43 +01002844 edge_serial = kzalloc(sizeof(struct edgeport_serial), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 if (edge_serial == NULL) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08002846 dev_err(&serial->dev->dev, "%s - Out of memory\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847 return -ENOMEM;
2848 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849 spin_lock_init(&edge_serial->es_lock);
2850 edge_serial->serial = serial;
2851 usb_set_serial_data(serial, edge_serial);
2852
2853 /* get the name for the device from the device */
Pete Zaitcevad933752006-05-22 21:49:44 -07002854 i = get_string(dev, dev->descriptor.iManufacturer,
2855 &edge_serial->name[0], MAX_NAME_LEN+1);
2856 edge_serial->name[i++] = ' ';
2857 get_string(dev, dev->descriptor.iProduct,
2858 &edge_serial->name[i], MAX_NAME_LEN+2 - i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859
2860 dev_info(&serial->dev->dev, "%s detected\n", edge_serial->name);
2861
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002862 /* Read the epic descriptor */
2863 if (get_epic_descriptor(edge_serial) <= 0) {
2864 /* memcpy descriptor to Supports structures */
2865 memcpy(&edge_serial->epic_descriptor.Supports, descriptor,
2866 sizeof(struct edge_compatibility_bits));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002868 /* get the manufacturing descriptor for this device */
2869 get_manufacturing_desc (edge_serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002871 /* get the boot descriptor */
2872 get_boot_desc (edge_serial);
2873
2874 get_product_info(edge_serial);
2875 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876
2877 /* set the number of ports from the manufacturing description */
2878 /* serial->num_ports = serial->product_info.NumPorts; */
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002879 if ((!edge_serial->is_epic) &&
2880 (edge_serial->product_info.NumPorts != serial->num_ports)) {
2881 dev_warn(&serial->dev->dev, "Device Reported %d serial ports "
2882 "vs. core thinking we have %d ports, email "
Joe Perches898eb712007-10-18 03:06:30 -07002883 "greg@kroah.com this information.\n",
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002884 edge_serial->product_info.NumPorts,
2885 serial->num_ports);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886 }
2887
Harvey Harrison441b62c2008-03-03 16:08:34 -08002888 dbg("%s - time 1 %ld", __func__, jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002890 /* If not an EPiC device */
2891 if (!edge_serial->is_epic) {
2892 /* now load the application firmware into this device */
2893 load_application_firmware (edge_serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894
Harvey Harrison441b62c2008-03-03 16:08:34 -08002895 dbg("%s - time 2 %ld", __func__, jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002897 /* Check current Edgeport EEPROM and update if necessary */
2898 update_edgeport_E2PROM (edge_serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899
Harvey Harrison441b62c2008-03-03 16:08:34 -08002900 dbg("%s - time 3 %ld", __func__, jiffies);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002901
2902 /* set the configuration to use #1 */
2903// dbg("set_configuration 1");
2904// usb_set_configuration (dev, 1);
2905 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906
2907 /* we set up the pointers to the endpoints in the edge_open function,
2908 * as the structures aren't created yet. */
2909
2910 /* set up our port private structures */
2911 for (i = 0; i < serial->num_ports; ++i) {
2912 edge_port = kmalloc (sizeof(struct edgeport_port), GFP_KERNEL);
2913 if (edge_port == NULL) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08002914 dev_err(&serial->dev->dev, "%s - Out of memory\n", __func__);
Chris Lundbfd5df32006-06-03 13:58:19 -07002915 for (j = 0; j < i; ++j) {
2916 kfree (usb_get_serial_port_data(serial->port[j]));
2917 usb_set_serial_port_data(serial->port[j], NULL);
2918 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919 usb_set_serial_data(serial, NULL);
2920 kfree(edge_serial);
2921 return -ENOMEM;
2922 }
2923 memset (edge_port, 0, sizeof(struct edgeport_port));
2924 spin_lock_init(&edge_port->ep_lock);
2925 edge_port->port = serial->port[i];
2926 usb_set_serial_port_data(serial->port[i], edge_port);
2927 }
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002928
2929 response = 0;
2930
2931 if (edge_serial->is_epic) {
2932 /* EPIC thing, set up our interrupt polling now and our read urb, so
2933 * that the device knows it really is connected. */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01002934 interrupt_in_found = bulk_in_found = bulk_out_found = false;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002935 for (i = 0; i < serial->interface->altsetting[0].desc.bNumEndpoints; ++i) {
2936 struct usb_endpoint_descriptor *endpoint;
2937 int buffer_size;
2938
2939 endpoint = &serial->interface->altsetting[0].endpoint[i].desc;
2940 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01002941 if (!interrupt_in_found &&
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002942 (usb_endpoint_is_int_in(endpoint))) {
2943 /* we found a interrupt in endpoint */
2944 dbg("found interrupt in");
2945
2946 /* not set up yet, so do it now */
2947 edge_serial->interrupt_read_urb = usb_alloc_urb(0, GFP_KERNEL);
2948 if (!edge_serial->interrupt_read_urb) {
2949 err("out of memory");
2950 return -ENOMEM;
2951 }
2952 edge_serial->interrupt_in_buffer = kmalloc(buffer_size, GFP_KERNEL);
2953 if (!edge_serial->interrupt_in_buffer) {
2954 err("out of memory");
2955 usb_free_urb(edge_serial->interrupt_read_urb);
2956 return -ENOMEM;
2957 }
2958 edge_serial->interrupt_in_endpoint = endpoint->bEndpointAddress;
2959
2960 /* set up our interrupt urb */
2961 usb_fill_int_urb(edge_serial->interrupt_read_urb,
2962 dev,
2963 usb_rcvintpipe(dev, endpoint->bEndpointAddress),
2964 edge_serial->interrupt_in_buffer,
2965 buffer_size,
2966 edge_interrupt_callback,
2967 edge_serial,
2968 endpoint->bInterval);
2969
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01002970 interrupt_in_found = true;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002971 }
2972
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01002973 if (!bulk_in_found &&
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002974 (usb_endpoint_is_bulk_in(endpoint))) {
2975 /* we found a bulk in endpoint */
2976 dbg("found bulk in");
2977
2978 /* not set up yet, so do it now */
2979 edge_serial->read_urb = usb_alloc_urb(0, GFP_KERNEL);
2980 if (!edge_serial->read_urb) {
2981 err("out of memory");
2982 return -ENOMEM;
2983 }
2984 edge_serial->bulk_in_buffer = kmalloc(buffer_size, GFP_KERNEL);
2985 if (!edge_serial->bulk_in_buffer) {
2986 err ("out of memory");
2987 usb_free_urb(edge_serial->read_urb);
2988 return -ENOMEM;
2989 }
2990 edge_serial->bulk_in_endpoint = endpoint->bEndpointAddress;
2991
2992 /* set up our bulk in urb */
2993 usb_fill_bulk_urb(edge_serial->read_urb, dev,
2994 usb_rcvbulkpipe(dev, endpoint->bEndpointAddress),
2995 edge_serial->bulk_in_buffer,
2996 endpoint->wMaxPacketSize,
2997 edge_bulk_in_callback,
2998 edge_serial);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01002999 bulk_in_found = true;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003000 }
3001
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01003002 if (!bulk_out_found &&
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003003 (usb_endpoint_is_bulk_out(endpoint))) {
3004 /* we found a bulk out endpoint */
3005 dbg("found bulk out");
3006 edge_serial->bulk_out_endpoint = endpoint->bEndpointAddress;
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01003007 bulk_out_found = true;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003008 }
3009 }
3010
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01003011 if (!interrupt_in_found || !bulk_in_found || !bulk_out_found) {
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003012 err ("Error - the proper endpoints were not found!");
3013 return -ENODEV;
3014 }
3015
3016 /* start interrupt read for this edgeport this interrupt will
3017 * continue as long as the edgeport is connected */
3018 response = usb_submit_urb(edge_serial->interrupt_read_urb, GFP_KERNEL);
3019 if (response)
Harvey Harrison441b62c2008-03-03 16:08:34 -08003020 err("%s - Error %d submitting control urb", __func__, response);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003021 }
3022 return response;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023}
3024
3025
3026/****************************************************************************
3027 * edge_shutdown
3028 * This function is called whenever the device is removed from the usb bus.
3029 ****************************************************************************/
3030static void edge_shutdown (struct usb_serial *serial)
3031{
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003032 struct edgeport_serial *edge_serial = usb_get_serial_data(serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 int i;
3034
Harvey Harrison441b62c2008-03-03 16:08:34 -08003035 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036
3037 /* stop reads and writes on all ports */
3038 for (i=0; i < serial->num_ports; ++i) {
3039 kfree (usb_get_serial_port_data(serial->port[i]));
3040 usb_set_serial_port_data(serial->port[i], NULL);
3041 }
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003042 /* free up our endpoint stuff */
3043 if (edge_serial->is_epic) {
Oliver Neukum74ac07e2007-06-13 18:50:41 +02003044 usb_kill_urb(edge_serial->interrupt_read_urb);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003045 usb_free_urb(edge_serial->interrupt_read_urb);
3046 kfree(edge_serial->interrupt_in_buffer);
3047
Oliver Neukum74ac07e2007-06-13 18:50:41 +02003048 usb_kill_urb(edge_serial->read_urb);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003049 usb_free_urb(edge_serial->read_urb);
3050 kfree(edge_serial->bulk_in_buffer);
3051 }
3052
3053 kfree(edge_serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054 usb_set_serial_data(serial, NULL);
3055}
3056
3057
3058/****************************************************************************
3059 * edgeport_init
3060 * This is called by the module subsystem, or on startup to initialize us
3061 ****************************************************************************/
3062static int __init edgeport_init(void)
3063{
3064 int retval;
3065
3066 retval = usb_serial_register(&edgeport_2port_device);
3067 if (retval)
3068 goto failed_2port_device_register;
3069 retval = usb_serial_register(&edgeport_4port_device);
3070 if (retval)
3071 goto failed_4port_device_register;
3072 retval = usb_serial_register(&edgeport_8port_device);
3073 if (retval)
3074 goto failed_8port_device_register;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003075 retval = usb_serial_register(&epic_device);
3076 if (retval)
3077 goto failed_epic_device_register;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 retval = usb_register(&io_driver);
3079 if (retval)
3080 goto failed_usb_register;
Oliver Neukum96c706e2007-03-15 15:27:17 +01003081 atomic_set(&CmdUrbs, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 info(DRIVER_DESC " " DRIVER_VERSION);
3083 return 0;
3084
3085failed_usb_register:
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003086 usb_serial_deregister(&epic_device);
3087failed_epic_device_register:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088 usb_serial_deregister(&edgeport_8port_device);
3089failed_8port_device_register:
3090 usb_serial_deregister(&edgeport_4port_device);
3091failed_4port_device_register:
3092 usb_serial_deregister(&edgeport_2port_device);
3093failed_2port_device_register:
3094 return retval;
3095}
3096
3097
3098/****************************************************************************
3099 * edgeport_exit
3100 * Called when the driver is about to be unloaded.
3101 ****************************************************************************/
3102static void __exit edgeport_exit (void)
3103{
3104 usb_deregister (&io_driver);
3105 usb_serial_deregister (&edgeport_2port_device);
3106 usb_serial_deregister (&edgeport_4port_device);
3107 usb_serial_deregister (&edgeport_8port_device);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003108 usb_serial_deregister (&epic_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109}
3110
3111module_init(edgeport_init);
3112module_exit(edgeport_exit);
3113
3114/* Module information */
3115MODULE_AUTHOR( DRIVER_AUTHOR );
3116MODULE_DESCRIPTION( DRIVER_DESC );
3117MODULE_LICENSE("GPL");
3118
3119module_param(debug, bool, S_IRUGO | S_IWUSR);
3120MODULE_PARM_DESC(debug, "Debug enabled or not");
3121
3122module_param(low_latency, bool, S_IRUGO | S_IWUSR);
3123MODULE_PARM_DESC(low_latency, "Low latency enabled or not");