blob: 29346d79df7b7b750cc9dc946c753601c1e5b199 [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>
Jaswinder Singh5b9ea932008-07-03 17:00:23 +053045#include <linux/firmware.h>
46#include <linux/ihex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <asm/uaccess.h>
48#include <linux/usb.h>
Greg Kroah-Hartmana9698882006-07-11 21:22:58 -070049#include <linux/usb/serial.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include "io_edgeport.h"
51#include "io_ionsp.h" /* info for the iosp messages */
52#include "io_16654.h" /* 16654 UART defines */
53
54/*
55 * Version Information
56 */
57#define DRIVER_VERSION "v2.7"
58#define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com> and David Iacovelli"
59#define DRIVER_DESC "Edgeport USB Serial Driver"
60
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#define MAX_NAME_LEN 64
62
63#define CHASE_TIMEOUT (5*HZ) /* 5 seconds */
64#define OPEN_TIMEOUT (5*HZ) /* 5 seconds */
65#define COMMAND_TIMEOUT (5*HZ) /* 5 seconds */
66
67/* receive port state */
68enum RXSTATE {
69 EXPECT_HDR1 = 0, /* Expect header byte 1 */
70 EXPECT_HDR2 = 1, /* Expect header byte 2 */
71 EXPECT_DATA = 2, /* Expect 'RxBytesRemaining' data */
72 EXPECT_HDR3 = 3, /* Expect header byte 3 (for status hdrs only) */
73};
74
75
76/* Transmit Fifo
77 * This Transmit queue is an extension of the edgeport Rx buffer.
78 * The maximum amount of data buffered in both the edgeport
79 * Rx buffer (maxTxCredits) and this buffer will never exceed maxTxCredits.
80 */
81struct TxFifo {
82 unsigned int head; /* index to head pointer (write) */
83 unsigned int tail; /* index to tail pointer (read) */
84 unsigned int count; /* Bytes in queue */
85 unsigned int size; /* Max size of queue (equal to Max number of TxCredits) */
86 unsigned char *fifo; /* allocated Buffer */
87};
88
89/* This structure holds all of the local port information */
90struct edgeport_port {
91 __u16 txCredits; /* our current credits for this port */
92 __u16 maxTxCredits; /* the max size of the port */
93
94 struct TxFifo txfifo; /* transmit fifo -- size will be maxTxCredits */
95 struct urb *write_urb; /* write URB for this port */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +010096 bool write_in_progress; /* 'true' while a write URB is outstanding */
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 spinlock_t ep_lock;
98
99 __u8 shadowLCR; /* last LCR value received */
100 __u8 shadowMCR; /* last MCR value received */
101 __u8 shadowMSR; /* last MSR value received */
102 __u8 shadowLSR; /* last LSR value received */
103 __u8 shadowXonChar; /* last value set as XON char in Edgeport */
104 __u8 shadowXoffChar; /* last value set as XOFF char in Edgeport */
105 __u8 validDataMask;
106 __u32 baudRate;
107
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100108 bool open;
109 bool openPending;
110 bool commandPending;
111 bool closePending;
112 bool chaseResponsePending;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
114 wait_queue_head_t wait_chase; /* for handling sleeping while waiting for chase to finish */
115 wait_queue_head_t wait_open; /* for handling sleeping while waiting for open to finish */
116 wait_queue_head_t wait_command; /* for handling sleeping while waiting for command to finish */
117 wait_queue_head_t delta_msr_wait; /* for handling sleeping while waiting for msr change to happen */
118
119 struct async_icount icount;
120 struct usb_serial_port *port; /* loop back to the owner of this object */
121};
122
123
124/* This structure holds all of the individual device information */
125struct edgeport_serial {
Pete Zaitcevad933752006-05-22 21:49:44 -0700126 char name[MAX_NAME_LEN+2]; /* string name of this device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
128 struct edge_manuf_descriptor manuf_descriptor; /* the manufacturer descriptor */
129 struct edge_boot_descriptor boot_descriptor; /* the boot firmware descriptor */
130 struct edgeport_product_info product_info; /* Product Info */
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -0800131 struct edge_compatibility_descriptor epic_descriptor; /* Edgeport compatible descriptor */
132 int is_epic; /* flag if EPiC device or not */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
134 __u8 interrupt_in_endpoint; /* the interrupt endpoint handle */
135 unsigned char * interrupt_in_buffer; /* the buffer we use for the interrupt endpoint */
136 struct urb * interrupt_read_urb; /* our interrupt urb */
137
138 __u8 bulk_in_endpoint; /* the bulk in endpoint handle */
139 unsigned char * bulk_in_buffer; /* the buffer we use for the bulk in endpoint */
140 struct urb * read_urb; /* our bulk read urb */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100141 bool read_in_progress;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 spinlock_t es_lock;
143
144 __u8 bulk_out_endpoint; /* the bulk out endpoint handle */
145
146 __s16 rxBytesAvail; /* the number of bytes that we need to read from this device */
147
148 enum RXSTATE rxState; /* the current state of the bulk receive processor */
149 __u8 rxHeader1; /* receive header byte 1 */
150 __u8 rxHeader2; /* receive header byte 2 */
151 __u8 rxHeader3; /* receive header byte 3 */
152 __u8 rxPort; /* the port that we are currently receiving data for */
153 __u8 rxStatusCode; /* the receive status code */
154 __u8 rxStatusParam; /* the receive status paramater */
155 __s16 rxBytesRemaining; /* the number of port bytes left to read */
156 struct usb_serial *serial; /* loop back to the owner of this object */
157};
158
159/* baud rate information */
160struct divisor_table_entry {
161 __u32 BaudRate;
162 __u16 Divisor;
163};
164
165//
166// Define table of divisors for Rev A EdgePort/4 hardware
167// These assume a 3.6864MHz crystal, the standard /16, and
168// MCR.7 = 0.
169//
Arjan van de Ven4c4c9432005-11-29 09:43:42 +0100170static const struct divisor_table_entry divisor_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 { 50, 4608},
172 { 75, 3072},
173 { 110, 2095}, /* 2094.545455 => 230450 => .0217 % over */
174 { 134, 1713}, /* 1713.011152 => 230398.5 => .00065% under */
175 { 150, 1536},
176 { 300, 768},
177 { 600, 384},
178 { 1200, 192},
179 { 1800, 128},
180 { 2400, 96},
181 { 4800, 48},
182 { 7200, 32},
183 { 9600, 24},
184 { 14400, 16},
185 { 19200, 12},
186 { 38400, 6},
187 { 57600, 4},
188 { 115200, 2},
189 { 230400, 1},
190};
191
192/* local variables */
193static int debug;
194
195static int low_latency = 1; /* tty low latency flag, on by default */
196
Oliver Neukum96c706e2007-03-15 15:27:17 +0100197static atomic_t CmdUrbs; /* Number of outstanding Command Write Urbs */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
199
200/* local function prototypes */
201
202/* function prototypes for all URB callbacks */
David Howells7d12e782006-10-05 14:55:46 +0100203static void edge_interrupt_callback (struct urb *urb);
204static void edge_bulk_in_callback (struct urb *urb);
205static void edge_bulk_out_data_callback (struct urb *urb);
206static void edge_bulk_out_cmd_callback (struct urb *urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
208/* function prototypes for the usbserial callbacks */
Alan Cox95da3102008-07-22 11:09:07 +0100209static int edge_open (struct tty_struct *tty, struct usb_serial_port *port, struct file *filp);
210static void edge_close (struct tty_struct *tty, struct usb_serial_port *port, struct file *filp);
211static int edge_write (struct tty_struct *tty, struct usb_serial_port *port, const unsigned char *buf, int count);
212static int edge_write_room (struct tty_struct *tty);
213static int edge_chars_in_buffer (struct tty_struct *tty);
214static void edge_throttle (struct tty_struct *tty);
215static void edge_unthrottle (struct tty_struct *tty);
216static void edge_set_termios (struct tty_struct *tty, struct usb_serial_port *port, struct ktermios *old_termios);
217static int edge_ioctl (struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg);
218static void edge_break (struct tty_struct *tty, int break_state);
219static int edge_tiocmget (struct tty_struct *tty, struct file *file);
220static int edge_tiocmset (struct tty_struct *tty, struct file *file, unsigned int set, unsigned int clear);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221static int edge_startup (struct usb_serial *serial);
222static void edge_shutdown (struct usb_serial *serial);
223
224
225#include "io_tables.h" /* all of the devices that this driver supports */
226
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227/* function prototypes for all of our local functions */
228static void process_rcvd_data (struct edgeport_serial *edge_serial, unsigned char *buffer, __u16 bufferLength);
229static void process_rcvd_status (struct edgeport_serial *edge_serial, __u8 byte2, __u8 byte3);
230static void edge_tty_recv (struct device *dev, struct tty_struct *tty, unsigned char *data, int length);
231static void handle_new_msr (struct edgeport_port *edge_port, __u8 newMsr);
232static void handle_new_lsr (struct edgeport_port *edge_port, __u8 lsrData, __u8 lsr, __u8 data);
233static int send_iosp_ext_cmd (struct edgeport_port *edge_port, __u8 command, __u8 param);
234static int calc_baud_rate_divisor (int baud_rate, int *divisor);
235static int send_cmd_write_baud_rate (struct edgeport_port *edge_port, int baudRate);
Alan Cox95da3102008-07-22 11:09:07 +0100236static void change_port_settings (struct tty_struct *tty, struct edgeport_port *edge_port,
237 struct ktermios *old_termios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238static int send_cmd_write_uart_register (struct edgeport_port *edge_port, __u8 regNum, __u8 regValue);
239static int write_cmd_usb (struct edgeport_port *edge_port, unsigned char *buffer, int writeLength);
240static void send_more_port_data (struct edgeport_serial *edge_serial, struct edgeport_port *edge_port);
241
Jaswinder Singh5b9ea932008-07-03 17:00:23 +0530242static int sram_write (struct usb_serial *serial, __u16 extAddr, __u16 addr, __u16 length, const __u8 *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243static int rom_read (struct usb_serial *serial, __u16 extAddr, __u16 addr, __u16 length, __u8 *data);
Jaswinder Singh5b9ea932008-07-03 17:00:23 +0530244static int rom_write (struct usb_serial *serial, __u16 extAddr, __u16 addr, __u16 length, const __u8 *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245static void get_manufacturing_desc (struct edgeport_serial *edge_serial);
246static void get_boot_desc (struct edgeport_serial *edge_serial);
247static void load_application_firmware (struct edgeport_serial *edge_serial);
248
Pete Zaitcevad933752006-05-22 21:49:44 -0700249static void unicode_to_ascii(char *string, int buflen, __le16 *unicode, int unicode_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
251
252// ************************************************************************
253// ************************************************************************
254// ************************************************************************
255// ************************************************************************
256
257/************************************************************************
258 * *
259 * update_edgeport_E2PROM() Compare current versions of *
260 * Boot ROM and Manufacture *
261 * Descriptors with versions *
262 * embedded in this driver *
263 * *
264 ************************************************************************/
265static void update_edgeport_E2PROM (struct edgeport_serial *edge_serial)
266{
267 __u32 BootCurVer;
268 __u32 BootNewVer;
Jaswinder Singh5b9ea932008-07-03 17:00:23 +0530269 __u8 BootMajorVersion;
270 __u8 BootMinorVersion;
271 __u16 BootBuildNumber;
272 __u32 Bootaddr;
273 const struct ihex_binrec *rec;
274 const struct firmware *fw;
275 const char *fw_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 int response;
277
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 switch (edge_serial->product_info.iDownloadFile) {
279 case EDGE_DOWNLOAD_FILE_I930:
Jaswinder Singh5b9ea932008-07-03 17:00:23 +0530280 fw_name = "edgeport/boot.fw";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 break;
282
283 case EDGE_DOWNLOAD_FILE_80251:
Jaswinder Singh5b9ea932008-07-03 17:00:23 +0530284 fw_name = "edgeport/boot2.fw";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 break;
286
287 default:
288 return;
289 }
290
Jaswinder Singh5b9ea932008-07-03 17:00:23 +0530291 response = request_ihex_firmware(&fw, fw_name,
292 &edge_serial->serial->dev->dev);
293 if (response) {
294 printk(KERN_ERR "Failed to load image \"%s\" err %d\n",
295 fw_name, response);
296 return;
297 }
298
299 rec = (const struct ihex_binrec *)fw->data;
300 BootMajorVersion = rec->data[0];
301 BootMinorVersion = rec->data[1];
302 BootBuildNumber = (rec->data[2] << 8) | rec->data[3];
303
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 // Check Boot Image Version
305 BootCurVer = (edge_serial->boot_descriptor.MajorVersion << 24) +
306 (edge_serial->boot_descriptor.MinorVersion << 16) +
307 le16_to_cpu(edge_serial->boot_descriptor.BuildNumber);
308
309 BootNewVer = (BootMajorVersion << 24) +
310 (BootMinorVersion << 16) +
Jaswinder Singh5b9ea932008-07-03 17:00:23 +0530311 BootBuildNumber;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
313 dbg("Current Boot Image version %d.%d.%d",
314 edge_serial->boot_descriptor.MajorVersion,
315 edge_serial->boot_descriptor.MinorVersion,
316 le16_to_cpu(edge_serial->boot_descriptor.BuildNumber));
317
318
319 if (BootNewVer > BootCurVer) {
320 dbg("**Update Boot Image from %d.%d.%d to %d.%d.%d",
321 edge_serial->boot_descriptor.MajorVersion,
322 edge_serial->boot_descriptor.MinorVersion,
323 le16_to_cpu(edge_serial->boot_descriptor.BuildNumber),
Jaswinder Singh5b9ea932008-07-03 17:00:23 +0530324 BootMajorVersion, BootMinorVersion, BootBuildNumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
326 dbg("Downloading new Boot Image");
327
Jaswinder Singh5b9ea932008-07-03 17:00:23 +0530328 for (rec = ihex_next_binrec(rec); rec;
329 rec = ihex_next_binrec(rec)) {
330 Bootaddr = be32_to_cpu(rec->addr);
331 response = rom_write(edge_serial->serial,
332 Bootaddr >> 16,
333 Bootaddr & 0xFFFF,
334 be16_to_cpu(rec->len),
335 &rec->data[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 if (response < 0) {
Jaswinder Singh5b9ea932008-07-03 17:00:23 +0530337 dev_err(&edge_serial->serial->dev->dev,
338 "rom_write failed (%x, %x, %d)\n",
339 Bootaddr >> 16, Bootaddr & 0xFFFF,
340 be16_to_cpu(rec->len));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 break;
342 }
343 }
344 } else {
345 dbg("Boot Image -- already up to date");
346 }
Jaswinder Singh5b9ea932008-07-03 17:00:23 +0530347 release_firmware(fw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348}
349
350
351/************************************************************************
352 * *
353 * Get string descriptor from device *
354 * *
355 ************************************************************************/
Pete Zaitcevad933752006-05-22 21:49:44 -0700356static int get_string (struct usb_device *dev, int Id, char *string, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357{
358 struct usb_string_descriptor StringDesc;
359 struct usb_string_descriptor *pStringDesc;
360
Harvey Harrison441b62c2008-03-03 16:08:34 -0800361 dbg("%s - USB String ID = %d", __func__, Id );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
363 if (!usb_get_descriptor(dev, USB_DT_STRING, Id, &StringDesc, sizeof(StringDesc))) {
364 return 0;
365 }
366
367 pStringDesc = kmalloc (StringDesc.bLength, GFP_KERNEL);
368
369 if (!pStringDesc) {
370 return 0;
371 }
372
373 if (!usb_get_descriptor(dev, USB_DT_STRING, Id, pStringDesc, StringDesc.bLength )) {
374 kfree(pStringDesc);
375 return 0;
376 }
377
Pete Zaitcevad933752006-05-22 21:49:44 -0700378 unicode_to_ascii(string, buflen, pStringDesc->wData, pStringDesc->bLength/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
380 kfree(pStringDesc);
Harvey Harrison441b62c2008-03-03 16:08:34 -0800381 dbg("%s - USB String %s", __func__, string);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 return strlen(string);
383}
384
385
386#if 0
387/************************************************************************
388 *
389 * Get string descriptor from device
390 *
391 ************************************************************************/
392static int get_string_desc (struct usb_device *dev, int Id, struct usb_string_descriptor **pRetDesc)
393{
394 struct usb_string_descriptor StringDesc;
395 struct usb_string_descriptor *pStringDesc;
396
Harvey Harrison441b62c2008-03-03 16:08:34 -0800397 dbg("%s - USB String ID = %d", __func__, Id );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
399 if (!usb_get_descriptor(dev, USB_DT_STRING, Id, &StringDesc, sizeof(StringDesc))) {
400 return 0;
401 }
402
403 pStringDesc = kmalloc (StringDesc.bLength, GFP_KERNEL);
404
405 if (!pStringDesc) {
406 return -1;
407 }
408
409 if (!usb_get_descriptor(dev, USB_DT_STRING, Id, pStringDesc, StringDesc.bLength )) {
410 kfree(pStringDesc);
411 return -1;
412 }
413
414 *pRetDesc = pStringDesc;
415 return 0;
416}
417#endif
418
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -0800419static void dump_product_info(struct edgeport_product_info *product_info)
420{
421 // Dump Product Info structure
422 dbg("**Product Information:");
423 dbg(" ProductId %x", product_info->ProductId );
424 dbg(" NumPorts %d", product_info->NumPorts );
425 dbg(" ProdInfoVer %d", product_info->ProdInfoVer );
426 dbg(" IsServer %d", product_info->IsServer);
427 dbg(" IsRS232 %d", product_info->IsRS232 );
428 dbg(" IsRS422 %d", product_info->IsRS422 );
429 dbg(" IsRS485 %d", product_info->IsRS485 );
430 dbg(" RomSize %d", product_info->RomSize );
431 dbg(" RamSize %d", product_info->RamSize );
432 dbg(" CpuRev %x", product_info->CpuRev );
433 dbg(" BoardRev %x", product_info->BoardRev);
434 dbg(" BootMajorVersion %d.%d.%d", product_info->BootMajorVersion,
435 product_info->BootMinorVersion,
436 le16_to_cpu(product_info->BootBuildNumber));
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -0800437 dbg(" ManufactureDescDate %d/%d/%d", product_info->ManufactureDescDate[0],
438 product_info->ManufactureDescDate[1],
439 product_info->ManufactureDescDate[2]+1900);
440 dbg(" iDownloadFile 0x%x", product_info->iDownloadFile);
441 dbg(" EpicVer %d", product_info->EpicVer);
442}
443
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444static void get_product_info(struct edgeport_serial *edge_serial)
445{
446 struct edgeport_product_info *product_info = &edge_serial->product_info;
447
448 memset (product_info, 0, sizeof(struct edgeport_product_info));
449
450 product_info->ProductId = (__u16)(le16_to_cpu(edge_serial->serial->dev->descriptor.idProduct) & ~ION_DEVICE_ID_80251_NETCHIP);
451 product_info->NumPorts = edge_serial->manuf_descriptor.NumPorts;
452 product_info->ProdInfoVer = 0;
453
454 product_info->RomSize = edge_serial->manuf_descriptor.RomSize;
455 product_info->RamSize = edge_serial->manuf_descriptor.RamSize;
456 product_info->CpuRev = edge_serial->manuf_descriptor.CpuRev;
457 product_info->BoardRev = edge_serial->manuf_descriptor.BoardRev;
458
459 product_info->BootMajorVersion = edge_serial->boot_descriptor.MajorVersion;
460 product_info->BootMinorVersion = edge_serial->boot_descriptor.MinorVersion;
461 product_info->BootBuildNumber = edge_serial->boot_descriptor.BuildNumber;
462
463 memcpy(product_info->ManufactureDescDate, edge_serial->manuf_descriptor.DescDate, sizeof(edge_serial->manuf_descriptor.DescDate));
464
465 // check if this is 2nd generation hardware
466 if (le16_to_cpu(edge_serial->serial->dev->descriptor.idProduct) & ION_DEVICE_ID_80251_NETCHIP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 product_info->iDownloadFile = EDGE_DOWNLOAD_FILE_80251;
468 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 product_info->iDownloadFile = EDGE_DOWNLOAD_FILE_I930;
470 }
471
472 // Determine Product type and set appropriate flags
473 switch (DEVICE_ID_FROM_USB_PRODUCT_ID(product_info->ProductId)) {
474 case ION_DEVICE_ID_EDGEPORT_COMPATIBLE:
475 case ION_DEVICE_ID_EDGEPORT_4T:
476 case ION_DEVICE_ID_EDGEPORT_4:
477 case ION_DEVICE_ID_EDGEPORT_2:
478 case ION_DEVICE_ID_EDGEPORT_8_DUAL_CPU:
479 case ION_DEVICE_ID_EDGEPORT_8:
480 case ION_DEVICE_ID_EDGEPORT_421:
481 case ION_DEVICE_ID_EDGEPORT_21:
482 case ION_DEVICE_ID_EDGEPORT_2_DIN:
483 case ION_DEVICE_ID_EDGEPORT_4_DIN:
484 case ION_DEVICE_ID_EDGEPORT_16_DUAL_CPU:
485 product_info->IsRS232 = 1;
486 break;
487
488 case ION_DEVICE_ID_EDGEPORT_2I: // Edgeport/2 RS422/RS485
489 product_info->IsRS422 = 1;
490 product_info->IsRS485 = 1;
491 break;
492
493 case ION_DEVICE_ID_EDGEPORT_8I: // Edgeport/4 RS422
494 case ION_DEVICE_ID_EDGEPORT_4I: // Edgeport/4 RS422
495 product_info->IsRS422 = 1;
496 break;
497 }
498
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -0800499 dump_product_info(product_info);
500}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -0800502static int get_epic_descriptor(struct edgeport_serial *ep)
503{
504 int result;
505 struct usb_serial *serial = ep->serial;
506 struct edgeport_product_info *product_info = &ep->product_info;
507 struct edge_compatibility_descriptor *epic = &ep->epic_descriptor;
508 struct edge_compatibility_bits *bits;
509
510 ep->is_epic = 0;
511 result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
512 USB_REQUEST_ION_GET_EPIC_DESC,
513 0xC0, 0x00, 0x00,
514 &ep->epic_descriptor,
515 sizeof(struct edge_compatibility_descriptor),
516 300);
517
Harvey Harrison441b62c2008-03-03 16:08:34 -0800518 dbg("%s result = %d", __func__, result);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -0800519
520 if (result > 0) {
521 ep->is_epic = 1;
522 memset(product_info, 0, sizeof(struct edgeport_product_info));
523
524 product_info->NumPorts = epic->NumPorts;
525 product_info->ProdInfoVer = 0;
526 product_info->FirmwareMajorVersion = epic->MajorVersion;
527 product_info->FirmwareMinorVersion = epic->MinorVersion;
528 product_info->FirmwareBuildNumber = epic->BuildNumber;
529 product_info->iDownloadFile = epic->iDownloadFile;
530 product_info->EpicVer = epic->EpicVer;
531 product_info->Epic = epic->Supports;
532 product_info->ProductId = ION_DEVICE_ID_EDGEPORT_COMPATIBLE;
533 dump_product_info(product_info);
534
535 bits = &ep->epic_descriptor.Supports;
536 dbg("**EPIC descriptor:");
537 dbg(" VendEnableSuspend: %s", bits->VendEnableSuspend ? "TRUE": "FALSE");
538 dbg(" IOSPOpen : %s", bits->IOSPOpen ? "TRUE": "FALSE" );
539 dbg(" IOSPClose : %s", bits->IOSPClose ? "TRUE": "FALSE" );
540 dbg(" IOSPChase : %s", bits->IOSPChase ? "TRUE": "FALSE" );
541 dbg(" IOSPSetRxFlow : %s", bits->IOSPSetRxFlow ? "TRUE": "FALSE" );
542 dbg(" IOSPSetTxFlow : %s", bits->IOSPSetTxFlow ? "TRUE": "FALSE" );
543 dbg(" IOSPSetXChar : %s", bits->IOSPSetXChar ? "TRUE": "FALSE" );
544 dbg(" IOSPRxCheck : %s", bits->IOSPRxCheck ? "TRUE": "FALSE" );
545 dbg(" IOSPSetClrBreak : %s", bits->IOSPSetClrBreak ? "TRUE": "FALSE" );
546 dbg(" IOSPWriteMCR : %s", bits->IOSPWriteMCR ? "TRUE": "FALSE" );
547 dbg(" IOSPWriteLCR : %s", bits->IOSPWriteLCR ? "TRUE": "FALSE" );
548 dbg(" IOSPSetBaudRate : %s", bits->IOSPSetBaudRate ? "TRUE": "FALSE" );
549 dbg(" TrueEdgeport : %s", bits->TrueEdgeport ? "TRUE": "FALSE" );
550 }
551
552 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553}
554
555
556/************************************************************************/
557/************************************************************************/
558/* U S B C A L L B A C K F U N C T I O N S */
559/* U S B C A L L B A C K F U N C T I O N S */
560/************************************************************************/
561/************************************************************************/
562
563/*****************************************************************************
564 * edge_interrupt_callback
565 * this is the callback function for when we have received data on the
566 * interrupt endpoint.
567 *****************************************************************************/
David Howells7d12e782006-10-05 14:55:46 +0100568static void edge_interrupt_callback (struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569{
Ming Leicdc97792008-02-24 18:41:47 +0800570 struct edgeport_serial *edge_serial = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 struct edgeport_port *edge_port;
572 struct usb_serial_port *port;
573 unsigned char *data = urb->transfer_buffer;
574 int length = urb->actual_length;
575 int bytes_avail;
576 int position;
577 int txCredits;
578 int portNumber;
579 int result;
Greg Kroah-Hartman2a393f52007-06-15 15:44:13 -0700580 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
Harvey Harrison441b62c2008-03-03 16:08:34 -0800582 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
Greg Kroah-Hartman2a393f52007-06-15 15:44:13 -0700584 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 case 0:
586 /* success */
587 break;
588 case -ECONNRESET:
589 case -ENOENT:
590 case -ESHUTDOWN:
591 /* this urb is terminated, clean up */
Greg Kroah-Hartman2a393f52007-06-15 15:44:13 -0700592 dbg("%s - urb shutting down with status: %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800593 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 return;
595 default:
Greg Kroah-Hartman2a393f52007-06-15 15:44:13 -0700596 dbg("%s - nonzero urb status received: %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800597 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 goto exit;
599 }
600
601 // process this interrupt-read even if there are no ports open
602 if (length) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800603 usb_serial_debug_data(debug, &edge_serial->serial->dev->dev, __func__, length, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
605 if (length > 1) {
606 bytes_avail = data[0] | (data[1] << 8);
607 if (bytes_avail) {
608 spin_lock(&edge_serial->es_lock);
609 edge_serial->rxBytesAvail += bytes_avail;
Harvey Harrison441b62c2008-03-03 16:08:34 -0800610 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 -0700611
612 if (edge_serial->rxBytesAvail > 0 &&
613 !edge_serial->read_in_progress) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800614 dbg("%s - posting a read", __func__);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100615 edge_serial->read_in_progress = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
617 /* we have pending bytes on the bulk in pipe, send a request */
618 edge_serial->read_urb->dev = edge_serial->serial->dev;
619 result = usb_submit_urb(edge_serial->read_urb, GFP_ATOMIC);
620 if (result) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800621 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 +0100622 edge_serial->read_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 }
624 }
625 spin_unlock(&edge_serial->es_lock);
626 }
627 }
628 /* grab the txcredits for the ports if available */
629 position = 2;
630 portNumber = 0;
631 while ((position < length) && (portNumber < edge_serial->serial->num_ports)) {
632 txCredits = data[position] | (data[position+1] << 8);
633 if (txCredits) {
634 port = edge_serial->serial->port[portNumber];
635 edge_port = usb_get_serial_port_data(port);
636 if (edge_port->open) {
637 spin_lock(&edge_port->ep_lock);
638 edge_port->txCredits += txCredits;
639 spin_unlock(&edge_port->ep_lock);
Harvey Harrison441b62c2008-03-03 16:08:34 -0800640 dbg("%s - txcredits for port%d = %d", __func__, portNumber, edge_port->txCredits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
642 /* tell the tty driver that something has changed */
Alan Cox95da3102008-07-22 11:09:07 +0100643 if (edge_port->port->port.tty)
644 tty_wakeup(edge_port->port->port.tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
646 // Since we have more credit, check if more data can be sent
647 send_more_port_data(edge_serial, edge_port);
648 }
649 }
650 position += 2;
651 ++portNumber;
652 }
653 }
654
655exit:
656 result = usb_submit_urb (urb, GFP_ATOMIC);
657 if (result) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800658 dev_err(&urb->dev->dev, "%s - Error %d submitting control urb\n", __func__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 }
660}
661
662
663/*****************************************************************************
664 * edge_bulk_in_callback
665 * this is the callback function for when we have received data on the
666 * bulk in endpoint.
667 *****************************************************************************/
David Howells7d12e782006-10-05 14:55:46 +0100668static void edge_bulk_in_callback (struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669{
Ming Leicdc97792008-02-24 18:41:47 +0800670 struct edgeport_serial *edge_serial = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 unsigned char *data = urb->transfer_buffer;
Greg Kroah-Hartman2a393f52007-06-15 15:44:13 -0700672 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 __u16 raw_data_length;
Greg Kroah-Hartman2a393f52007-06-15 15:44:13 -0700674 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
Harvey Harrison441b62c2008-03-03 16:08:34 -0800676 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
Greg Kroah-Hartman2a393f52007-06-15 15:44:13 -0700678 if (status) {
679 dbg("%s - nonzero read bulk status received: %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800680 __func__, status);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100681 edge_serial->read_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 return;
683 }
684
685 if (urb->actual_length == 0) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800686 dbg("%s - read bulk callback with no data", __func__);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100687 edge_serial->read_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 return;
689 }
690
691 raw_data_length = urb->actual_length;
692
Harvey Harrison441b62c2008-03-03 16:08:34 -0800693 usb_serial_debug_data(debug, &edge_serial->serial->dev->dev, __func__, raw_data_length, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
695 spin_lock(&edge_serial->es_lock);
696
697 /* decrement our rxBytes available by the number that we just got */
698 edge_serial->rxBytesAvail -= raw_data_length;
699
Harvey Harrison441b62c2008-03-03 16:08:34 -0800700 dbg("%s - Received = %d, rxBytesAvail %d", __func__, raw_data_length, edge_serial->rxBytesAvail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
702 process_rcvd_data (edge_serial, data, urb->actual_length);
703
704 /* check to see if there's any more data for us to read */
705 if (edge_serial->rxBytesAvail > 0) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800706 dbg("%s - posting a read", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 edge_serial->read_urb->dev = edge_serial->serial->dev;
Greg Kroah-Hartman2a393f52007-06-15 15:44:13 -0700708 retval = usb_submit_urb(edge_serial->read_urb, GFP_ATOMIC);
709 if (retval) {
710 dev_err(&urb->dev->dev,
711 "%s - usb_submit_urb(read bulk) failed, "
Harvey Harrison441b62c2008-03-03 16:08:34 -0800712 "retval = %d\n", __func__, retval);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100713 edge_serial->read_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 }
715 } else {
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100716 edge_serial->read_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 }
718
719 spin_unlock(&edge_serial->es_lock);
720}
721
722
723/*****************************************************************************
724 * edge_bulk_out_data_callback
725 * this is the callback function for when we have finished sending serial data
726 * on the bulk out endpoint.
727 *****************************************************************************/
David Howells7d12e782006-10-05 14:55:46 +0100728static void edge_bulk_out_data_callback (struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729{
Ming Leicdc97792008-02-24 18:41:47 +0800730 struct edgeport_port *edge_port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 struct tty_struct *tty;
Greg Kroah-Hartman2a393f52007-06-15 15:44:13 -0700732 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
Harvey Harrison441b62c2008-03-03 16:08:34 -0800734 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
Greg Kroah-Hartman2a393f52007-06-15 15:44:13 -0700736 if (status) {
737 dbg("%s - nonzero write bulk status received: %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800738 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 }
740
Alan Cox95da3102008-07-22 11:09:07 +0100741 tty = edge_port->port->port.tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
743 if (tty && edge_port->open) {
744 /* let the tty driver wakeup if it has a special write_wakeup function */
745 tty_wakeup(tty);
746 }
747
748 // Release the Write URB
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100749 edge_port->write_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
751 // Check if more data needs to be sent
752 send_more_port_data((struct edgeport_serial *)(usb_get_serial_data(edge_port->port->serial)), edge_port);
753}
754
755
756/*****************************************************************************
757 * BulkOutCmdCallback
758 * this is the callback function for when we have finished sending a command
759 * on the bulk out endpoint.
760 *****************************************************************************/
David Howells7d12e782006-10-05 14:55:46 +0100761static void edge_bulk_out_cmd_callback (struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762{
Ming Leicdc97792008-02-24 18:41:47 +0800763 struct edgeport_port *edge_port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 struct tty_struct *tty;
765 int status = urb->status;
766
Harvey Harrison441b62c2008-03-03 16:08:34 -0800767 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
Oliver Neukum96c706e2007-03-15 15:27:17 +0100769 atomic_dec(&CmdUrbs);
Harvey Harrison441b62c2008-03-03 16:08:34 -0800770 dbg("%s - FREE URB %p (outstanding %d)", __func__, urb, atomic_read(&CmdUrbs));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
772
773 /* clean up the transfer buffer */
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -0700774 kfree(urb->transfer_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
776 /* Free the command urb */
777 usb_free_urb (urb);
778
779 if (status) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800780 dbg("%s - nonzero write bulk status received: %d", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 return;
782 }
783
784 /* Get pointer to tty */
Alan Cox95da3102008-07-22 11:09:07 +0100785 tty = edge_port->port->port.tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
787 /* tell the tty driver that something has changed */
788 if (tty && edge_port->open)
789 tty_wakeup(tty);
790
791 /* we have completed the command */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100792 edge_port->commandPending = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 wake_up(&edge_port->wait_command);
794}
795
796
797/*****************************************************************************
798 * Driver tty interface functions
799 *****************************************************************************/
800
801/*****************************************************************************
802 * SerialOpen
803 * this function is called by the tty driver when a port is opened
804 * If successful, we return 0
805 * Otherwise we return a negative error number.
806 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +0100807static int edge_open(struct tty_struct *tty,
808 struct usb_serial_port *port, struct file * filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809{
810 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
811 struct usb_serial *serial;
812 struct edgeport_serial *edge_serial;
813 int response;
814
Harvey Harrison441b62c2008-03-03 16:08:34 -0800815 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
817 if (edge_port == NULL)
818 return -ENODEV;
819
Alan Cox95da3102008-07-22 11:09:07 +0100820 if (tty)
821 tty->low_latency = low_latency;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
823 /* see if we've set up our endpoint info yet (can't set it up in edge_startup
824 as the structures were not set up at that time.) */
825 serial = port->serial;
826 edge_serial = usb_get_serial_data(serial);
Alan Cox95da3102008-07-22 11:09:07 +0100827 if (edge_serial == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 if (edge_serial->interrupt_in_buffer == NULL) {
830 struct usb_serial_port *port0 = serial->port[0];
831
832 /* not set up yet, so do it now */
833 edge_serial->interrupt_in_buffer = port0->interrupt_in_buffer;
834 edge_serial->interrupt_in_endpoint = port0->interrupt_in_endpointAddress;
835 edge_serial->interrupt_read_urb = port0->interrupt_in_urb;
836 edge_serial->bulk_in_buffer = port0->bulk_in_buffer;
837 edge_serial->bulk_in_endpoint = port0->bulk_in_endpointAddress;
838 edge_serial->read_urb = port0->read_urb;
839 edge_serial->bulk_out_endpoint = port0->bulk_out_endpointAddress;
840
841 /* set up our interrupt urb */
842 usb_fill_int_urb(edge_serial->interrupt_read_urb,
843 serial->dev,
844 usb_rcvintpipe(serial->dev,
845 port0->interrupt_in_endpointAddress),
846 port0->interrupt_in_buffer,
847 edge_serial->interrupt_read_urb->transfer_buffer_length,
848 edge_interrupt_callback, edge_serial,
849 edge_serial->interrupt_read_urb->interval);
850
851 /* set up our bulk in urb */
852 usb_fill_bulk_urb(edge_serial->read_urb, serial->dev,
853 usb_rcvbulkpipe(serial->dev,
854 port0->bulk_in_endpointAddress),
855 port0->bulk_in_buffer,
856 edge_serial->read_urb->transfer_buffer_length,
857 edge_bulk_in_callback, edge_serial);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100858 edge_serial->read_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859
860 /* start interrupt read for this edgeport
861 * this interrupt will continue as long as the edgeport is connected */
862 response = usb_submit_urb (edge_serial->interrupt_read_urb, GFP_KERNEL);
863 if (response) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800864 dev_err(&port->dev, "%s - Error %d submitting control urb\n", __func__, response);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 }
866 }
867
868 /* initialize our wait queues */
869 init_waitqueue_head(&edge_port->wait_open);
870 init_waitqueue_head(&edge_port->wait_chase);
871 init_waitqueue_head(&edge_port->delta_msr_wait);
872 init_waitqueue_head(&edge_port->wait_command);
873
874 /* initialize our icount structure */
875 memset (&(edge_port->icount), 0x00, sizeof(edge_port->icount));
876
877 /* initialize our port settings */
878 edge_port->txCredits = 0; /* Can't send any data yet */
879 edge_port->shadowMCR = MCR_MASTER_IE; /* Must always set this bit to enable ints! */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100880 edge_port->chaseResponsePending = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
882 /* send a open port command */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100883 edge_port->openPending = true;
884 edge_port->open = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 response = send_iosp_ext_cmd (edge_port, IOSP_CMD_OPEN_PORT, 0);
886
887 if (response < 0) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800888 dev_err(&port->dev, "%s - error sending open port command\n", __func__);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100889 edge_port->openPending = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 return -ENODEV;
891 }
892
893 /* now wait for the port to be completely opened */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100894 wait_event_timeout(edge_port->wait_open, !edge_port->openPending, OPEN_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100896 if (!edge_port->open) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 /* open timed out */
Harvey Harrison441b62c2008-03-03 16:08:34 -0800898 dbg("%s - open timedout", __func__);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100899 edge_port->openPending = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 return -ENODEV;
901 }
902
903 /* create the txfifo */
904 edge_port->txfifo.head = 0;
905 edge_port->txfifo.tail = 0;
906 edge_port->txfifo.count = 0;
907 edge_port->txfifo.size = edge_port->maxTxCredits;
908 edge_port->txfifo.fifo = kmalloc (edge_port->maxTxCredits, GFP_KERNEL);
909
910 if (!edge_port->txfifo.fifo) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800911 dbg("%s - no memory", __func__);
Alan Cox95da3102008-07-22 11:09:07 +0100912 edge_close (tty, port, filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 return -ENOMEM;
914 }
915
916 /* Allocate a URB for the write */
917 edge_port->write_urb = usb_alloc_urb (0, GFP_KERNEL);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100918 edge_port->write_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
920 if (!edge_port->write_urb) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800921 dbg("%s - no memory", __func__);
Alan Cox95da3102008-07-22 11:09:07 +0100922 edge_close (tty, port, filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 return -ENOMEM;
924 }
925
Harvey Harrison441b62c2008-03-03 16:08:34 -0800926 dbg("%s(%d) - Initialize TX fifo to %d bytes", __func__, port->number, edge_port->maxTxCredits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
Harvey Harrison441b62c2008-03-03 16:08:34 -0800928 dbg("%s exited", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929
930 return 0;
931}
932
933
934/************************************************************************
935 *
936 * block_until_chase_response
937 *
938 * This function will block the close until one of the following:
939 * 1. Response to our Chase comes from Edgeport
Joe Perchesdc0d5c12007-12-17 11:40:18 -0800940 * 2. A timeout of 10 seconds without activity has expired
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 * (1K of Edgeport data @ 2400 baud ==> 4 sec to empty)
942 *
943 ************************************************************************/
944static void block_until_chase_response(struct edgeport_port *edge_port)
945{
946 DEFINE_WAIT(wait);
947 __u16 lastCredits;
948 int timeout = 1*HZ;
949 int loop = 10;
950
951 while (1) {
952 // Save Last credits
953 lastCredits = edge_port->txCredits;
954
955 // Did we get our Chase response
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100956 if (!edge_port->chaseResponsePending) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800957 dbg("%s - Got Chase Response", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
959 // did we get all of our credit back?
960 if (edge_port->txCredits == edge_port->maxTxCredits ) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800961 dbg("%s - Got all credits", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 return;
963 }
964 }
965
966 // Block the thread for a while
967 prepare_to_wait(&edge_port->wait_chase, &wait, TASK_UNINTERRUPTIBLE);
968 schedule_timeout(timeout);
969 finish_wait(&edge_port->wait_chase, &wait);
970
971 if (lastCredits == edge_port->txCredits) {
972 // No activity.. count down.
973 loop--;
974 if (loop == 0) {
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100975 edge_port->chaseResponsePending = false;
Harvey Harrison441b62c2008-03-03 16:08:34 -0800976 dbg("%s - Chase TIMEOUT", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 return;
978 }
979 } else {
Joe Perchesdc0d5c12007-12-17 11:40:18 -0800980 // Reset timeout value back to 10 seconds
Harvey Harrison441b62c2008-03-03 16:08:34 -0800981 dbg("%s - Last %d, Current %d", __func__, lastCredits, edge_port->txCredits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 loop = 10;
983 }
984 }
985}
986
987
988/************************************************************************
989 *
990 * block_until_tx_empty
991 *
992 * This function will block the close until one of the following:
993 * 1. TX count are 0
994 * 2. The edgeport has stopped
Joe Perchesdc0d5c12007-12-17 11:40:18 -0800995 * 3. A timeout of 3 seconds without activity has expired
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 *
997 ************************************************************************/
998static void block_until_tx_empty (struct edgeport_port *edge_port)
999{
1000 DEFINE_WAIT(wait);
1001 struct TxFifo *fifo = &edge_port->txfifo;
1002 __u32 lastCount;
1003 int timeout = HZ/10;
1004 int loop = 30;
1005
1006 while (1) {
1007 // Save Last count
1008 lastCount = fifo->count;
1009
1010 // Is the Edgeport Buffer empty?
1011 if (lastCount == 0) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001012 dbg("%s - TX Buffer Empty", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 return;
1014 }
1015
1016 // Block the thread for a while
1017 prepare_to_wait (&edge_port->wait_chase, &wait, TASK_UNINTERRUPTIBLE);
1018 schedule_timeout(timeout);
1019 finish_wait(&edge_port->wait_chase, &wait);
1020
Harvey Harrison441b62c2008-03-03 16:08:34 -08001021 dbg("%s wait", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
1023 if (lastCount == fifo->count) {
1024 // No activity.. count down.
1025 loop--;
1026 if (loop == 0) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001027 dbg("%s - TIMEOUT", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 return;
1029 }
1030 } else {
Joe Perchesdc0d5c12007-12-17 11:40:18 -08001031 // Reset timeout value back to seconds
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 loop = 30;
1033 }
1034 }
1035}
1036
1037
1038/*****************************************************************************
1039 * edge_close
1040 * this function is called by the tty driver when a port is closed
1041 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001042static void edge_close(struct tty_struct *tty,
1043 struct usb_serial_port *port, struct file * filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044{
1045 struct edgeport_serial *edge_serial;
1046 struct edgeport_port *edge_port;
1047 int status;
1048
Harvey Harrison441b62c2008-03-03 16:08:34 -08001049 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050
1051 edge_serial = usb_get_serial_data(port->serial);
1052 edge_port = usb_get_serial_port_data(port);
1053 if ((edge_serial == NULL) || (edge_port == NULL))
1054 return;
1055
1056 // block until tx is empty
1057 block_until_tx_empty(edge_port);
1058
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001059 edge_port->closePending = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001061 if ((!edge_serial->is_epic) ||
1062 ((edge_serial->is_epic) &&
1063 (edge_serial->epic_descriptor.Supports.IOSPChase))) {
1064 /* flush and chase */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001065 edge_port->chaseResponsePending = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
Harvey Harrison441b62c2008-03-03 16:08:34 -08001067 dbg("%s - Sending IOSP_CMD_CHASE_PORT", __func__);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001068 status = send_iosp_ext_cmd (edge_port, IOSP_CMD_CHASE_PORT, 0);
1069 if (status == 0) {
1070 // block until chase finished
1071 block_until_chase_response(edge_port);
1072 } else {
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001073 edge_port->chaseResponsePending = false;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001074 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 }
1076
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001077 if ((!edge_serial->is_epic) ||
1078 ((edge_serial->is_epic) &&
1079 (edge_serial->epic_descriptor.Supports.IOSPClose))) {
1080 /* close the port */
Harvey Harrison441b62c2008-03-03 16:08:34 -08001081 dbg("%s - Sending IOSP_CMD_CLOSE_PORT", __func__);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001082 send_iosp_ext_cmd (edge_port, IOSP_CMD_CLOSE_PORT, 0);
1083 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001085 //port->close = true;
1086 edge_port->closePending = false;
1087 edge_port->open = false;
1088 edge_port->openPending = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089
Mariusz Kozlowski9a25f442006-11-08 15:36:29 +01001090 usb_kill_urb(edge_port->write_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
1092 if (edge_port->write_urb) {
1093 /* if this urb had a transfer buffer already (old transfer) free it */
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07001094 kfree(edge_port->write_urb->transfer_buffer);
1095 usb_free_urb(edge_port->write_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 edge_port->write_urb = NULL;
1097 }
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07001098 kfree(edge_port->txfifo.fifo);
1099 edge_port->txfifo.fifo = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
Harvey Harrison441b62c2008-03-03 16:08:34 -08001101 dbg("%s exited", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102}
1103
1104/*****************************************************************************
1105 * SerialWrite
1106 * this function is called by the tty driver when data should be written to
1107 * the port.
1108 * If successful, we return the number of bytes written, otherwise we return
1109 * a negative error number.
1110 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001111static int edge_write(struct tty_struct *tty, struct usb_serial_port *port,
1112 const unsigned char *data, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113{
1114 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1115 struct TxFifo *fifo;
1116 int copySize;
1117 int bytesleft;
1118 int firsthalf;
1119 int secondhalf;
1120 unsigned long flags;
1121
Harvey Harrison441b62c2008-03-03 16:08:34 -08001122 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
1124 if (edge_port == NULL)
1125 return -ENODEV;
1126
1127 // get a pointer to the Tx fifo
1128 fifo = &edge_port->txfifo;
1129
1130 spin_lock_irqsave(&edge_port->ep_lock, flags);
1131
1132 // calculate number of bytes to put in fifo
1133 copySize = min ((unsigned int)count, (edge_port->txCredits - fifo->count));
1134
Harvey Harrison441b62c2008-03-03 16:08:34 -08001135 dbg("%s(%d) of %d byte(s) Fifo room %d -- will copy %d bytes", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 port->number, count, edge_port->txCredits - fifo->count, copySize);
1137
1138 /* catch writes of 0 bytes which the tty driver likes to give us, and when txCredits is empty */
1139 if (copySize == 0) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001140 dbg("%s - copySize = Zero", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 goto finish_write;
1142 }
1143
1144 // queue the data
1145 // since we can never overflow the buffer we do not have to check for full condition
1146
1147 // the copy is done is two parts -- first fill to the end of the buffer
1148 // then copy the reset from the start of the buffer
1149
1150 bytesleft = fifo->size - fifo->head;
1151 firsthalf = min (bytesleft, copySize);
Harvey Harrison441b62c2008-03-03 16:08:34 -08001152 dbg("%s - copy %d bytes of %d into fifo ", __func__, firsthalf, bytesleft);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153
1154 /* now copy our data */
1155 memcpy(&fifo->fifo[fifo->head], data, firsthalf);
Harvey Harrison441b62c2008-03-03 16:08:34 -08001156 usb_serial_debug_data(debug, &port->dev, __func__, firsthalf, &fifo->fifo[fifo->head]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157
1158 // update the index and size
1159 fifo->head += firsthalf;
1160 fifo->count += firsthalf;
1161
1162 // wrap the index
1163 if (fifo->head == fifo->size) {
1164 fifo->head = 0;
1165 }
1166
1167 secondhalf = copySize-firsthalf;
1168
1169 if (secondhalf) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001170 dbg("%s - copy rest of data %d", __func__, secondhalf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 memcpy(&fifo->fifo[fifo->head], &data[firsthalf], secondhalf);
Harvey Harrison441b62c2008-03-03 16:08:34 -08001172 usb_serial_debug_data(debug, &port->dev, __func__, secondhalf, &fifo->fifo[fifo->head]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 // update the index and size
1174 fifo->count += secondhalf;
1175 fifo->head += secondhalf;
1176 // No need to check for wrap since we can not get to end of fifo in this part
1177 }
1178
1179finish_write:
1180 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1181
1182 send_more_port_data((struct edgeport_serial *)usb_get_serial_data(port->serial), edge_port);
1183
Harvey Harrison441b62c2008-03-03 16:08:34 -08001184 dbg("%s wrote %d byte(s) TxCredits %d, Fifo %d", __func__, copySize, edge_port->txCredits, fifo->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
1186 return copySize;
1187}
1188
1189
1190/************************************************************************
1191 *
1192 * send_more_port_data()
1193 *
1194 * This routine attempts to write additional UART transmit data
1195 * to a port over the USB bulk pipe. It is called (1) when new
1196 * data has been written to a port's TxBuffer from higher layers
1197 * (2) when the peripheral sends us additional TxCredits indicating
1198 * that it can accept more Tx data for a given port; and (3) when
1199 * a bulk write completes successfully and we want to see if we
1200 * can transmit more.
1201 *
1202 ************************************************************************/
1203static void send_more_port_data(struct edgeport_serial *edge_serial, struct edgeport_port *edge_port)
1204{
1205 struct TxFifo *fifo = &edge_port->txfifo;
1206 struct urb *urb;
1207 unsigned char *buffer;
1208 int status;
1209 int count;
1210 int bytesleft;
1211 int firsthalf;
1212 int secondhalf;
1213 unsigned long flags;
1214
Harvey Harrison441b62c2008-03-03 16:08:34 -08001215 dbg("%s(%d)", __func__, edge_port->port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216
1217 spin_lock_irqsave(&edge_port->ep_lock, flags);
1218
1219 if (edge_port->write_in_progress ||
1220 !edge_port->open ||
1221 (fifo->count == 0)) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001222 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 -07001223 goto exit_send;
1224 }
1225
1226 // since the amount of data in the fifo will always fit into the
1227 // edgeport buffer we do not need to check the write length
1228
1229 // Do we have enough credits for this port to make it worthwhile
1230 // to bother queueing a write. If it's too small, say a few bytes,
1231 // it's better to wait for more credits so we can do a larger
1232 // write.
1233 if (edge_port->txCredits < EDGE_FW_GET_TX_CREDITS_SEND_THRESHOLD(edge_port->maxTxCredits,EDGE_FW_BULK_MAX_PACKET_SIZE)) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001234 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 -07001235 goto exit_send;
1236 }
1237
1238 // lock this write
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001239 edge_port->write_in_progress = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240
1241 // get a pointer to the write_urb
1242 urb = edge_port->write_urb;
1243
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07001244 /* make sure transfer buffer is freed */
1245 kfree(urb->transfer_buffer);
1246 urb->transfer_buffer = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247
1248 /* build the data header for the buffer and port that we are about to send out */
1249 count = fifo->count;
1250 buffer = kmalloc (count+2, GFP_ATOMIC);
1251 if (buffer == NULL) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001252 dev_err(&edge_port->port->dev, "%s - no more kernel memory...\n", __func__);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001253 edge_port->write_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 goto exit_send;
1255 }
1256 buffer[0] = IOSP_BUILD_DATA_HDR1 (edge_port->port->number - edge_port->port->serial->minor, count);
1257 buffer[1] = IOSP_BUILD_DATA_HDR2 (edge_port->port->number - edge_port->port->serial->minor, count);
1258
1259 /* now copy our data */
1260 bytesleft = fifo->size - fifo->tail;
1261 firsthalf = min (bytesleft, count);
1262 memcpy(&buffer[2], &fifo->fifo[fifo->tail], firsthalf);
1263 fifo->tail += firsthalf;
1264 fifo->count -= firsthalf;
1265 if (fifo->tail == fifo->size) {
1266 fifo->tail = 0;
1267 }
1268
1269 secondhalf = count-firsthalf;
1270 if (secondhalf) {
1271 memcpy(&buffer[2+firsthalf], &fifo->fifo[fifo->tail], secondhalf);
1272 fifo->tail += secondhalf;
1273 fifo->count -= secondhalf;
1274 }
1275
1276 if (count)
Harvey Harrison441b62c2008-03-03 16:08:34 -08001277 usb_serial_debug_data(debug, &edge_port->port->dev, __func__, count, &buffer[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278
1279 /* fill up the urb with all of our data and submit it */
1280 usb_fill_bulk_urb (urb, edge_serial->serial->dev,
1281 usb_sndbulkpipe(edge_serial->serial->dev, edge_serial->bulk_out_endpoint),
1282 buffer, count+2, edge_bulk_out_data_callback, edge_port);
1283
1284 /* decrement the number of credits we have by the number we just sent */
1285 edge_port->txCredits -= count;
1286 edge_port->icount.tx += count;
1287
1288 urb->dev = edge_serial->serial->dev;
1289 status = usb_submit_urb(urb, GFP_ATOMIC);
1290 if (status) {
1291 /* something went wrong */
Harvey Harrison441b62c2008-03-03 16:08:34 -08001292 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 +01001293 edge_port->write_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
1295 /* revert the credits as something bad happened. */
1296 edge_port->txCredits += count;
1297 edge_port->icount.tx -= count;
1298 }
Harvey Harrison441b62c2008-03-03 16:08:34 -08001299 dbg("%s wrote %d byte(s) TxCredit %d, Fifo %d", __func__, count, edge_port->txCredits, fifo->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300
1301exit_send:
1302 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1303}
1304
1305
1306/*****************************************************************************
1307 * edge_write_room
1308 * this function is called by the tty driver when it wants to know how many
1309 * bytes of data we can accept for a specific port.
1310 * If successful, we return the amount of room that we have for this port
1311 * (the txCredits),
1312 * Otherwise we return a negative error number.
1313 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001314static int edge_write_room(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315{
Alan Cox95da3102008-07-22 11:09:07 +01001316 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1318 int room;
1319 unsigned long flags;
1320
Harvey Harrison441b62c2008-03-03 16:08:34 -08001321 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322
1323 if (edge_port == NULL)
1324 return -ENODEV;
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001325 if (edge_port->closePending)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 return -ENODEV;
1327
Harvey Harrison441b62c2008-03-03 16:08:34 -08001328 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329
1330 if (!edge_port->open) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001331 dbg("%s - port not opened", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 return -EINVAL;
1333 }
1334
1335 // total of both buffers is still txCredit
1336 spin_lock_irqsave(&edge_port->ep_lock, flags);
1337 room = edge_port->txCredits - edge_port->txfifo.count;
1338 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1339
Harvey Harrison441b62c2008-03-03 16:08:34 -08001340 dbg("%s - returns %d", __func__, room);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 return room;
1342}
1343
1344
1345/*****************************************************************************
1346 * edge_chars_in_buffer
1347 * this function is called by the tty driver when it wants to know how many
1348 * bytes of data we currently have outstanding in the port (data that has
1349 * been written, but hasn't made it out the port yet)
1350 * If successful, we return the number of bytes left to be written in the
1351 * system,
1352 * Otherwise we return a negative error number.
1353 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001354static int edge_chars_in_buffer(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355{
Alan Cox95da3102008-07-22 11:09:07 +01001356 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1358 int num_chars;
1359 unsigned long flags;
1360
Harvey Harrison441b62c2008-03-03 16:08:34 -08001361 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
1363 if (edge_port == NULL)
1364 return -ENODEV;
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001365 if (edge_port->closePending)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 return -ENODEV;
1367
1368 if (!edge_port->open) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001369 dbg("%s - port not opened", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 return -EINVAL;
1371 }
1372
1373 spin_lock_irqsave(&edge_port->ep_lock, flags);
1374 num_chars = edge_port->maxTxCredits - edge_port->txCredits + edge_port->txfifo.count;
1375 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1376 if (num_chars) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001377 dbg("%s(port %d) - returns %d", __func__, port->number, num_chars);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 }
1379
1380 return num_chars;
1381}
1382
1383
1384/*****************************************************************************
1385 * SerialThrottle
1386 * this function is called by the tty driver when it wants to stop the data
1387 * being read from the port.
1388 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001389static void edge_throttle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390{
Alan Cox95da3102008-07-22 11:09:07 +01001391 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 int status;
1394
Harvey Harrison441b62c2008-03-03 16:08:34 -08001395 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396
1397 if (edge_port == NULL)
1398 return;
1399
1400 if (!edge_port->open) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001401 dbg("%s - port not opened", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 return;
1403 }
1404
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 /* if we are implementing XON/XOFF, send the stop character */
1406 if (I_IXOFF(tty)) {
1407 unsigned char stop_char = STOP_CHAR(tty);
Alan Cox95da3102008-07-22 11:09:07 +01001408 status = edge_write (tty, port, &stop_char, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 if (status <= 0) {
1410 return;
1411 }
1412 }
1413
1414 /* if we are implementing RTS/CTS, toggle that line */
1415 if (tty->termios->c_cflag & CRTSCTS) {
1416 edge_port->shadowMCR &= ~MCR_RTS;
1417 status = send_cmd_write_uart_register(edge_port, MCR, edge_port->shadowMCR);
1418 if (status != 0) {
1419 return;
1420 }
1421 }
1422
1423 return;
1424}
1425
1426
1427/*****************************************************************************
1428 * edge_unthrottle
1429 * this function is called by the tty driver when it wants to resume the data
1430 * being read from the port (called after SerialThrottle is called)
1431 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001432static void edge_unthrottle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433{
Alan Cox95da3102008-07-22 11:09:07 +01001434 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 int status;
1437
Harvey Harrison441b62c2008-03-03 16:08:34 -08001438 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439
1440 if (edge_port == NULL)
1441 return;
1442
1443 if (!edge_port->open) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001444 dbg("%s - port not opened", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 return;
1446 }
1447
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 /* if we are implementing XON/XOFF, send the start character */
1449 if (I_IXOFF(tty)) {
1450 unsigned char start_char = START_CHAR(tty);
Alan Cox95da3102008-07-22 11:09:07 +01001451 status = edge_write(tty, port, &start_char, 1);
1452 if (status <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 /* if we are implementing RTS/CTS, toggle that line */
1456 if (tty->termios->c_cflag & CRTSCTS) {
1457 edge_port->shadowMCR |= MCR_RTS;
Alan Cox95da3102008-07-22 11:09:07 +01001458 send_cmd_write_uart_register(edge_port, MCR, edge_port->shadowMCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460}
1461
1462
1463/*****************************************************************************
1464 * SerialSetTermios
1465 * this function is called by the tty driver when it wants to change the termios structure
1466 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001467static void edge_set_termios(struct tty_struct *tty,
1468 struct usb_serial_port *port, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469{
1470 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 unsigned int cflag;
1472
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 cflag = tty->termios->c_cflag;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001474 dbg("%s - clfag %08x iflag %08x", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 tty->termios->c_cflag, tty->termios->c_iflag);
Harvey Harrison441b62c2008-03-03 16:08:34 -08001476 dbg("%s - old clfag %08x old iflag %08x", __func__,
Alan Cox6ce073b2007-10-18 01:24:25 -07001477 old_termios->c_cflag, old_termios->c_iflag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478
Harvey Harrison441b62c2008-03-03 16:08:34 -08001479 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480
1481 if (edge_port == NULL)
1482 return;
1483
1484 if (!edge_port->open) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001485 dbg("%s - port not opened", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 return;
1487 }
1488
1489 /* change the port settings to the new ones specified */
Alan Cox95da3102008-07-22 11:09:07 +01001490 change_port_settings(tty, edge_port, old_termios);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491}
1492
1493
1494/*****************************************************************************
1495 * get_lsr_info - get line status register info
1496 *
1497 * Purpose: Let user call ioctl() to get info when the UART physically
1498 * is emptied. On bus types like RS485, the transmitter must
1499 * release the bus after transmitting. This must be done when
1500 * the transmit shift register is empty, not be done when the
1501 * transmit holding register is empty. This functionality
1502 * allows an RS485 driver to be written in user space.
1503 *****************************************************************************/
1504static int get_lsr_info(struct edgeport_port *edge_port, unsigned int __user *value)
1505{
1506 unsigned int result = 0;
1507 unsigned long flags;
1508
1509 spin_lock_irqsave(&edge_port->ep_lock, flags);
1510 if (edge_port->maxTxCredits == edge_port->txCredits &&
1511 edge_port->txfifo.count == 0) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001512 dbg("%s -- Empty", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 result = TIOCSER_TEMT;
1514 }
1515 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1516
1517 if (copy_to_user(value, &result, sizeof(int)))
1518 return -EFAULT;
1519 return 0;
1520}
1521
Alan Cox95da3102008-07-22 11:09:07 +01001522static int edge_tiocmset(struct tty_struct *tty, struct file *file, unsigned int set, unsigned int clear)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523{
Alan Cox95da3102008-07-22 11:09:07 +01001524 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1526 unsigned int mcr;
1527
Harvey Harrison441b62c2008-03-03 16:08:34 -08001528 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529
1530 mcr = edge_port->shadowMCR;
1531 if (set & TIOCM_RTS)
1532 mcr |= MCR_RTS;
1533 if (set & TIOCM_DTR)
1534 mcr |= MCR_DTR;
1535 if (set & TIOCM_LOOP)
1536 mcr |= MCR_LOOPBACK;
1537
1538 if (clear & TIOCM_RTS)
1539 mcr &= ~MCR_RTS;
1540 if (clear & TIOCM_DTR)
1541 mcr &= ~MCR_DTR;
1542 if (clear & TIOCM_LOOP)
1543 mcr &= ~MCR_LOOPBACK;
1544
1545 edge_port->shadowMCR = mcr;
1546
1547 send_cmd_write_uart_register(edge_port, MCR, edge_port->shadowMCR);
1548
1549 return 0;
1550}
1551
Alan Cox95da3102008-07-22 11:09:07 +01001552static int edge_tiocmget(struct tty_struct *tty, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553{
Alan Cox95da3102008-07-22 11:09:07 +01001554 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1556 unsigned int result = 0;
1557 unsigned int msr;
1558 unsigned int mcr;
1559
Harvey Harrison441b62c2008-03-03 16:08:34 -08001560 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561
1562 msr = edge_port->shadowMSR;
1563 mcr = edge_port->shadowMCR;
1564 result = ((mcr & MCR_DTR) ? TIOCM_DTR: 0) /* 0x002 */
1565 | ((mcr & MCR_RTS) ? TIOCM_RTS: 0) /* 0x004 */
1566 | ((msr & EDGEPORT_MSR_CTS) ? TIOCM_CTS: 0) /* 0x020 */
1567 | ((msr & EDGEPORT_MSR_CD) ? TIOCM_CAR: 0) /* 0x040 */
1568 | ((msr & EDGEPORT_MSR_RI) ? TIOCM_RI: 0) /* 0x080 */
1569 | ((msr & EDGEPORT_MSR_DSR) ? TIOCM_DSR: 0); /* 0x100 */
1570
1571
Harvey Harrison441b62c2008-03-03 16:08:34 -08001572 dbg("%s -- %x", __func__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573
1574 return result;
1575}
1576
1577static int get_serial_info(struct edgeport_port *edge_port, struct serial_struct __user *retinfo)
1578{
1579 struct serial_struct tmp;
1580
1581 if (!retinfo)
1582 return -EFAULT;
1583
1584 memset(&tmp, 0, sizeof(tmp));
1585
1586 tmp.type = PORT_16550A;
1587 tmp.line = edge_port->port->serial->minor;
1588 tmp.port = edge_port->port->number;
1589 tmp.irq = 0;
1590 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
1591 tmp.xmit_fifo_size = edge_port->maxTxCredits;
1592 tmp.baud_base = 9600;
1593 tmp.close_delay = 5*HZ;
1594 tmp.closing_wait = 30*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595
1596 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1597 return -EFAULT;
1598 return 0;
1599}
1600
1601
1602
1603/*****************************************************************************
1604 * SerialIoctl
1605 * this function handles any ioctl calls to the driver
1606 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001607static int edge_ioctl(struct tty_struct *tty, struct file *file,
1608 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609{
Alan Cox95da3102008-07-22 11:09:07 +01001610 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 DEFINE_WAIT(wait);
1612 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1613 struct async_icount cnow;
1614 struct async_icount cprev;
1615 struct serial_icounter_struct icount;
1616
Harvey Harrison441b62c2008-03-03 16:08:34 -08001617 dbg("%s - port %d, cmd = 0x%x", __func__, port->number, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618
1619 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 case TIOCSERGETLSR:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001621 dbg("%s (%d) TIOCSERGETLSR", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 return get_lsr_info(edge_port, (unsigned int __user *) arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623
1624 case TIOCGSERIAL:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001625 dbg("%s (%d) TIOCGSERIAL", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 return get_serial_info(edge_port, (struct serial_struct __user *) arg);
1627
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 case TIOCMIWAIT:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001629 dbg("%s (%d) TIOCMIWAIT", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 cprev = edge_port->icount;
1631 while (1) {
1632 prepare_to_wait(&edge_port->delta_msr_wait, &wait, TASK_INTERRUPTIBLE);
1633 schedule();
1634 finish_wait(&edge_port->delta_msr_wait, &wait);
1635 /* see if a signal did it */
1636 if (signal_pending(current))
1637 return -ERESTARTSYS;
1638 cnow = edge_port->icount;
1639 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
1640 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
1641 return -EIO; /* no change => error */
1642 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1643 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1644 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1645 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)) ) {
1646 return 0;
1647 }
1648 cprev = cnow;
1649 }
1650 /* NOTREACHED */
1651 break;
1652
1653 case TIOCGICOUNT:
1654 cnow = edge_port->icount;
1655 memset(&icount, 0, sizeof(icount));
1656 icount.cts = cnow.cts;
1657 icount.dsr = cnow.dsr;
1658 icount.rng = cnow.rng;
1659 icount.dcd = cnow.dcd;
1660 icount.rx = cnow.rx;
1661 icount.tx = cnow.tx;
1662 icount.frame = cnow.frame;
1663 icount.overrun = cnow.overrun;
1664 icount.parity = cnow.parity;
1665 icount.brk = cnow.brk;
1666 icount.buf_overrun = cnow.buf_overrun;
1667
Harvey Harrison441b62c2008-03-03 16:08:34 -08001668 dbg("%s (%d) TIOCGICOUNT RX=%d, TX=%d", __func__, port->number, icount.rx, icount.tx );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 if (copy_to_user((void __user *)arg, &icount, sizeof(icount)))
1670 return -EFAULT;
1671 return 0;
1672 }
1673
1674 return -ENOIOCTLCMD;
1675}
1676
1677
1678/*****************************************************************************
1679 * SerialBreak
1680 * this function sends a break to the port
1681 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001682static void edge_break (struct tty_struct *tty, int break_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683{
Alan Cox95da3102008-07-22 11:09:07 +01001684 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001686 struct edgeport_serial *edge_serial = usb_get_serial_data(port->serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 int status;
1688
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001689 if ((!edge_serial->is_epic) ||
1690 ((edge_serial->is_epic) &&
1691 (edge_serial->epic_descriptor.Supports.IOSPChase))) {
1692 /* flush and chase */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001693 edge_port->chaseResponsePending = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694
Harvey Harrison441b62c2008-03-03 16:08:34 -08001695 dbg("%s - Sending IOSP_CMD_CHASE_PORT", __func__);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001696 status = send_iosp_ext_cmd (edge_port, IOSP_CMD_CHASE_PORT, 0);
1697 if (status == 0) {
1698 // block until chase finished
1699 block_until_chase_response(edge_port);
1700 } else {
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001701 edge_port->chaseResponsePending = false;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001702 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 }
1704
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001705 if ((!edge_serial->is_epic) ||
1706 ((edge_serial->is_epic) &&
1707 (edge_serial->epic_descriptor.Supports.IOSPSetClrBreak))) {
1708 if (break_state == -1) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001709 dbg("%s - Sending IOSP_CMD_SET_BREAK", __func__);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001710 status = send_iosp_ext_cmd (edge_port, IOSP_CMD_SET_BREAK, 0);
1711 } else {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001712 dbg("%s - Sending IOSP_CMD_CLEAR_BREAK", __func__);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001713 status = send_iosp_ext_cmd (edge_port, IOSP_CMD_CLEAR_BREAK, 0);
1714 }
1715 if (status) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001716 dbg("%s - error sending break set/clear command.", __func__);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001717 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 }
1719
1720 return;
1721}
1722
1723
1724/*****************************************************************************
1725 * process_rcvd_data
1726 * this function handles the data received on the bulk in pipe.
1727 *****************************************************************************/
1728static void process_rcvd_data (struct edgeport_serial *edge_serial, unsigned char * buffer, __u16 bufferLength)
1729{
1730 struct usb_serial_port *port;
1731 struct edgeport_port *edge_port;
1732 struct tty_struct *tty;
1733 __u16 lastBufferLength;
1734 __u16 rxLen;
1735
Harvey Harrison441b62c2008-03-03 16:08:34 -08001736 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737
1738 lastBufferLength = bufferLength + 1;
1739
1740 while (bufferLength > 0) {
1741 /* failsafe incase we get a message that we don't understand */
1742 if (lastBufferLength == bufferLength) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001743 dbg("%s - stuck in loop, exiting it.", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 break;
1745 }
1746 lastBufferLength = bufferLength;
1747
1748 switch (edge_serial->rxState) {
1749 case EXPECT_HDR1:
1750 edge_serial->rxHeader1 = *buffer;
1751 ++buffer;
1752 --bufferLength;
1753
1754 if (bufferLength == 0) {
1755 edge_serial->rxState = EXPECT_HDR2;
1756 break;
1757 }
1758 /* otherwise, drop on through */
1759
1760 case EXPECT_HDR2:
1761 edge_serial->rxHeader2 = *buffer;
1762 ++buffer;
1763 --bufferLength;
1764
Harvey Harrison441b62c2008-03-03 16:08:34 -08001765 dbg("%s - Hdr1=%02X Hdr2=%02X", __func__, edge_serial->rxHeader1, edge_serial->rxHeader2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766
1767 // Process depending on whether this header is
1768 // data or status
1769
1770 if (IS_CMD_STAT_HDR(edge_serial->rxHeader1)) {
1771 // Decode this status header and goto EXPECT_HDR1 (if we
1772 // can process the status with only 2 bytes), or goto
1773 // EXPECT_HDR3 to get the third byte.
1774
1775 edge_serial->rxPort = IOSP_GET_HDR_PORT(edge_serial->rxHeader1);
1776 edge_serial->rxStatusCode = IOSP_GET_STATUS_CODE(edge_serial->rxHeader1);
1777
1778 if (!IOSP_STATUS_IS_2BYTE(edge_serial->rxStatusCode)) {
1779 // This status needs additional bytes. Save what we have
1780 // and then wait for more data.
1781 edge_serial->rxStatusParam = edge_serial->rxHeader2;
1782
1783 edge_serial->rxState = EXPECT_HDR3;
1784 break;
1785 }
1786
1787 // We have all the header bytes, process the status now
1788 process_rcvd_status (edge_serial, edge_serial->rxHeader2, 0);
1789 edge_serial->rxState = EXPECT_HDR1;
1790 break;
1791 } else {
1792 edge_serial->rxPort = IOSP_GET_HDR_PORT(edge_serial->rxHeader1);
1793 edge_serial->rxBytesRemaining = IOSP_GET_HDR_DATA_LEN(edge_serial->rxHeader1, edge_serial->rxHeader2);
1794
Harvey Harrison441b62c2008-03-03 16:08:34 -08001795 dbg("%s - Data for Port %u Len %u", __func__, edge_serial->rxPort, edge_serial->rxBytesRemaining);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796
1797 //ASSERT( DevExt->RxPort < DevExt->NumPorts );
1798 //ASSERT( DevExt->RxBytesRemaining < IOSP_MAX_DATA_LENGTH );
1799
1800 if (bufferLength == 0 ) {
1801 edge_serial->rxState = EXPECT_DATA;
1802 break;
1803 }
1804 // Else, drop through
1805 }
1806
1807 case EXPECT_DATA: // Expect data
1808
1809 if (bufferLength < edge_serial->rxBytesRemaining) {
1810 rxLen = bufferLength;
1811 edge_serial->rxState = EXPECT_DATA; // Expect data to start next buffer
1812 } else {
1813 // BufLen >= RxBytesRemaining
1814 rxLen = edge_serial->rxBytesRemaining;
1815 edge_serial->rxState = EXPECT_HDR1; // Start another header next time
1816 }
1817
1818 bufferLength -= rxLen;
1819 edge_serial->rxBytesRemaining -= rxLen;
1820
1821 /* spit this data back into the tty driver if this port is open */
1822 if (rxLen) {
1823 port = edge_serial->serial->port[edge_serial->rxPort];
1824 edge_port = usb_get_serial_port_data(port);
1825 if (edge_port->open) {
Alan Cox95da3102008-07-22 11:09:07 +01001826 tty = edge_port->port->port.tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 if (tty) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001828 dbg("%s - Sending %d bytes to TTY for port %d", __func__, rxLen, edge_serial->rxPort);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 edge_tty_recv(&edge_serial->serial->dev->dev, tty, buffer, rxLen);
1830 }
1831 edge_port->icount.rx += rxLen;
1832 }
1833 buffer += rxLen;
1834 }
1835
1836 break;
1837
1838 case EXPECT_HDR3: // Expect 3rd byte of status header
1839 edge_serial->rxHeader3 = *buffer;
1840 ++buffer;
1841 --bufferLength;
1842
1843 // We have all the header bytes, process the status now
1844 process_rcvd_status (edge_serial, edge_serial->rxStatusParam, edge_serial->rxHeader3);
1845 edge_serial->rxState = EXPECT_HDR1;
1846 break;
1847
1848 }
1849 }
1850}
1851
1852
1853/*****************************************************************************
1854 * process_rcvd_status
1855 * this function handles the any status messages received on the bulk in pipe.
1856 *****************************************************************************/
1857static void process_rcvd_status (struct edgeport_serial *edge_serial, __u8 byte2, __u8 byte3)
1858{
1859 struct usb_serial_port *port;
1860 struct edgeport_port *edge_port;
1861 __u8 code = edge_serial->rxStatusCode;
1862
1863 /* switch the port pointer to the one being currently talked about */
1864 port = edge_serial->serial->port[edge_serial->rxPort];
1865 edge_port = usb_get_serial_port_data(port);
1866 if (edge_port == NULL) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001867 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 -07001868 return;
1869 }
1870
Harvey Harrison441b62c2008-03-03 16:08:34 -08001871 dbg("%s - port %d", __func__, edge_serial->rxPort);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872
1873 if (code == IOSP_EXT_STATUS) {
1874 switch (byte2) {
1875 case IOSP_EXT_STATUS_CHASE_RSP:
1876 // we want to do EXT status regardless of port open/closed
Harvey Harrison441b62c2008-03-03 16:08:34 -08001877 dbg("%s - Port %u EXT CHASE_RSP Data = %02x", __func__, edge_serial->rxPort, byte3 );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 // Currently, the only EXT_STATUS is Chase, so process here instead of one more call
1879 // to one more subroutine. If/when more EXT_STATUS, there'll be more work to do.
1880 // Also, we currently clear flag and close the port regardless of content of above's Byte3.
1881 // We could choose to do something else when Byte3 says Timeout on Chase from Edgeport,
1882 // like wait longer in block_until_chase_response, but for now we don't.
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001883 edge_port->chaseResponsePending = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 wake_up (&edge_port->wait_chase);
1885 return;
1886
1887 case IOSP_EXT_STATUS_RX_CHECK_RSP:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001888 dbg("%s ========== Port %u CHECK_RSP Sequence = %02x =============\n", __func__, edge_serial->rxPort, byte3 );
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001889 //Port->RxCheckRsp = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 return;
1891 }
1892 }
1893
1894 if (code == IOSP_STATUS_OPEN_RSP) {
1895 edge_port->txCredits = GET_TX_BUFFER_SIZE(byte3);
1896 edge_port->maxTxCredits = edge_port->txCredits;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001897 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 -07001898 handle_new_msr (edge_port, byte2);
1899
1900 /* send the current line settings to the port so we are in sync with any further termios calls */
Alan Cox95da3102008-07-22 11:09:07 +01001901 /* FIXME: locking on tty */
1902 if (edge_port->port->port.tty)
1903 change_port_settings(edge_port->port->port.tty, edge_port, edge_port->port->port.tty->termios);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904
1905 /* we have completed the open */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001906 edge_port->openPending = false;
1907 edge_port->open = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 wake_up(&edge_port->wait_open);
1909 return;
1910 }
1911
1912 // If port is closed, silently discard all rcvd status. We can
1913 // have cases where buffered status is received AFTER the close
1914 // port command is sent to the Edgeport.
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001915 if (!edge_port->open || edge_port->closePending) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 return;
1917 }
1918
1919 switch (code) {
1920 // Not currently sent by Edgeport
1921 case IOSP_STATUS_LSR:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001922 dbg("%s - Port %u LSR Status = %02x", __func__, edge_serial->rxPort, byte2);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001923 handle_new_lsr(edge_port, false, byte2, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 break;
1925
1926 case IOSP_STATUS_LSR_DATA:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001927 dbg("%s - Port %u LSR Status = %02x, Data = %02x", __func__, edge_serial->rxPort, byte2, byte3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 // byte2 is LSR Register
1929 // byte3 is broken data byte
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001930 handle_new_lsr(edge_port, true, byte2, byte3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 break;
1932 //
1933 // case IOSP_EXT_4_STATUS:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001934 // dbg("%s - Port %u LSR Status = %02x Data = %02x", __func__, edge_serial->rxPort, byte2, byte3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 // break;
1936 //
1937 case IOSP_STATUS_MSR:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001938 dbg("%s - Port %u MSR Status = %02x", __func__, edge_serial->rxPort, byte2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939
1940 // Process this new modem status and generate appropriate
1941 // events, etc, based on the new status. This routine
1942 // also saves the MSR in Port->ShadowMsr.
1943 handle_new_msr(edge_port, byte2);
1944 break;
1945
1946 default:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001947 dbg("%s - Unrecognized IOSP status code %u\n", __func__, code);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 break;
1949 }
1950
1951 return;
1952}
1953
1954
1955/*****************************************************************************
1956 * edge_tty_recv
1957 * this function passes data on to the tty flip buffer
1958 *****************************************************************************/
1959static void edge_tty_recv(struct device *dev, struct tty_struct *tty, unsigned char *data, int length)
1960{
1961 int cnt;
1962
1963 do {
Alan Cox33f0f882006-01-09 20:54:13 -08001964 cnt = tty_buffer_request_room(tty, length);
1965 if (cnt < length) {
1966 dev_err(dev, "%s - dropping data, %d bytes lost\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001967 __func__, length - cnt);
Alan Cox33f0f882006-01-09 20:54:13 -08001968 if(cnt == 0)
1969 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 }
Alan Cox33f0f882006-01-09 20:54:13 -08001971 tty_insert_flip_string(tty, data, cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 data += cnt;
1973 length -= cnt;
1974 } while (length > 0);
1975
1976 tty_flip_buffer_push(tty);
1977}
1978
1979
1980/*****************************************************************************
1981 * handle_new_msr
1982 * this function handles any change to the msr register for a port.
1983 *****************************************************************************/
1984static void handle_new_msr(struct edgeport_port *edge_port, __u8 newMsr)
1985{
1986 struct async_icount *icount;
1987
Harvey Harrison441b62c2008-03-03 16:08:34 -08001988 dbg("%s %02x", __func__, newMsr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989
1990 if (newMsr & (EDGEPORT_MSR_DELTA_CTS | EDGEPORT_MSR_DELTA_DSR | EDGEPORT_MSR_DELTA_RI | EDGEPORT_MSR_DELTA_CD)) {
1991 icount = &edge_port->icount;
1992
1993 /* update input line counters */
1994 if (newMsr & EDGEPORT_MSR_DELTA_CTS) {
1995 icount->cts++;
1996 }
1997 if (newMsr & EDGEPORT_MSR_DELTA_DSR) {
1998 icount->dsr++;
1999 }
2000 if (newMsr & EDGEPORT_MSR_DELTA_CD) {
2001 icount->dcd++;
2002 }
2003 if (newMsr & EDGEPORT_MSR_DELTA_RI) {
2004 icount->rng++;
2005 }
2006 wake_up_interruptible(&edge_port->delta_msr_wait);
2007 }
2008
2009 /* Save the new modem status */
2010 edge_port->shadowMSR = newMsr & 0xf0;
2011
2012 return;
2013}
2014
2015
2016/*****************************************************************************
2017 * handle_new_lsr
2018 * this function handles any change to the lsr register for a port.
2019 *****************************************************************************/
2020static void handle_new_lsr(struct edgeport_port *edge_port, __u8 lsrData, __u8 lsr, __u8 data)
2021{
2022 __u8 newLsr = (__u8)(lsr & (__u8)(LSR_OVER_ERR | LSR_PAR_ERR | LSR_FRM_ERR | LSR_BREAK));
2023 struct async_icount *icount;
2024
Harvey Harrison441b62c2008-03-03 16:08:34 -08002025 dbg("%s - %02x", __func__, newLsr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026
2027 edge_port->shadowLSR = lsr;
2028
2029 if (newLsr & LSR_BREAK) {
2030 //
2031 // Parity and Framing errors only count if they
2032 // occur exclusive of a break being
2033 // received.
2034 //
2035 newLsr &= (__u8)(LSR_OVER_ERR | LSR_BREAK);
2036 }
2037
2038 /* Place LSR data byte into Rx buffer */
Alan Cox95da3102008-07-22 11:09:07 +01002039 if (lsrData && edge_port->port->port.tty)
2040 edge_tty_recv(&edge_port->port->dev, edge_port->port->port.tty, &data, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041
2042 /* update input line counters */
2043 icount = &edge_port->icount;
2044 if (newLsr & LSR_BREAK) {
2045 icount->brk++;
2046 }
2047 if (newLsr & LSR_OVER_ERR) {
2048 icount->overrun++;
2049 }
2050 if (newLsr & LSR_PAR_ERR) {
2051 icount->parity++;
2052 }
2053 if (newLsr & LSR_FRM_ERR) {
2054 icount->frame++;
2055 }
2056
2057 return;
2058}
2059
2060
2061/****************************************************************************
2062 * sram_write
2063 * writes a number of bytes to the Edgeport device's sram starting at the
2064 * given address.
2065 * If successful returns the number of bytes written, otherwise it returns
2066 * a negative error number of the problem.
2067 ****************************************************************************/
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302068static int sram_write(struct usb_serial *serial, __u16 extAddr, __u16 addr, __u16 length, const __u8 *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069{
2070 int result;
2071 __u16 current_length;
2072 unsigned char *transfer_buffer;
2073
Harvey Harrison441b62c2008-03-03 16:08:34 -08002074 dbg("%s - %x, %x, %d", __func__, extAddr, addr, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075
2076 transfer_buffer = kmalloc (64, GFP_KERNEL);
2077 if (!transfer_buffer) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08002078 dev_err(&serial->dev->dev, "%s - kmalloc(%d) failed.\n", __func__, 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 return -ENOMEM;
2080 }
2081
2082 /* need to split these writes up into 64 byte chunks */
2083 result = 0;
2084 while (length > 0) {
2085 if (length > 64) {
2086 current_length = 64;
2087 } else {
2088 current_length = length;
2089 }
Harvey Harrison441b62c2008-03-03 16:08:34 -08002090// dbg("%s - writing %x, %x, %d", __func__, extAddr, addr, current_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 memcpy (transfer_buffer, data, current_length);
2092 result = usb_control_msg (serial->dev, usb_sndctrlpipe(serial->dev, 0), USB_REQUEST_ION_WRITE_RAM,
2093 0x40, addr, extAddr, transfer_buffer, current_length, 300);
2094 if (result < 0)
2095 break;
2096 length -= current_length;
2097 addr += current_length;
2098 data += current_length;
2099 }
2100
2101 kfree (transfer_buffer);
2102 return result;
2103}
2104
2105
2106/****************************************************************************
2107 * rom_write
2108 * writes a number of bytes to the Edgeport device's ROM starting at the
2109 * given address.
2110 * If successful returns the number of bytes written, otherwise it returns
2111 * a negative error number of the problem.
2112 ****************************************************************************/
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302113static int rom_write(struct usb_serial *serial, __u16 extAddr, __u16 addr, __u16 length, const __u8 *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114{
2115 int result;
2116 __u16 current_length;
2117 unsigned char *transfer_buffer;
2118
Harvey Harrison441b62c2008-03-03 16:08:34 -08002119// dbg("%s - %x, %x, %d", __func__, extAddr, addr, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120
2121 transfer_buffer = kmalloc (64, GFP_KERNEL);
2122 if (!transfer_buffer) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08002123 dev_err(&serial->dev->dev, "%s - kmalloc(%d) failed.\n", __func__, 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 return -ENOMEM;
2125 }
2126
2127 /* need to split these writes up into 64 byte chunks */
2128 result = 0;
2129 while (length > 0) {
2130 if (length > 64) {
2131 current_length = 64;
2132 } else {
2133 current_length = length;
2134 }
Harvey Harrison441b62c2008-03-03 16:08:34 -08002135// dbg("%s - writing %x, %x, %d", __func__, extAddr, addr, current_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 memcpy (transfer_buffer, data, current_length);
2137 result = usb_control_msg (serial->dev, usb_sndctrlpipe(serial->dev, 0), USB_REQUEST_ION_WRITE_ROM,
2138 0x40, addr, extAddr, transfer_buffer, current_length, 300);
2139 if (result < 0)
2140 break;
2141 length -= current_length;
2142 addr += current_length;
2143 data += current_length;
2144 }
2145
2146 kfree (transfer_buffer);
2147 return result;
2148}
2149
2150
2151/****************************************************************************
2152 * rom_read
2153 * reads a number of bytes from the Edgeport device starting at the given
2154 * address.
2155 * If successful returns the number of bytes read, otherwise it returns
2156 * a negative error number of the problem.
2157 ****************************************************************************/
2158static int rom_read (struct usb_serial *serial, __u16 extAddr, __u16 addr, __u16 length, __u8 *data)
2159{
2160 int result;
2161 __u16 current_length;
2162 unsigned char *transfer_buffer;
2163
Harvey Harrison441b62c2008-03-03 16:08:34 -08002164 dbg("%s - %x, %x, %d", __func__, extAddr, addr, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165
2166 transfer_buffer = kmalloc (64, GFP_KERNEL);
2167 if (!transfer_buffer) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08002168 dev_err(&serial->dev->dev, "%s - kmalloc(%d) failed.\n", __func__, 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 return -ENOMEM;
2170 }
2171
2172 /* need to split these reads up into 64 byte chunks */
2173 result = 0;
2174 while (length > 0) {
2175 if (length > 64) {
2176 current_length = 64;
2177 } else {
2178 current_length = length;
2179 }
Harvey Harrison441b62c2008-03-03 16:08:34 -08002180// dbg("%s - %x, %x, %d", __func__, extAddr, addr, current_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 result = usb_control_msg (serial->dev, usb_rcvctrlpipe(serial->dev, 0), USB_REQUEST_ION_READ_ROM,
2182 0xC0, addr, extAddr, transfer_buffer, current_length, 300);
2183 if (result < 0)
2184 break;
2185 memcpy (data, transfer_buffer, current_length);
2186 length -= current_length;
2187 addr += current_length;
2188 data += current_length;
2189 }
2190
2191 kfree (transfer_buffer);
2192 return result;
2193}
2194
2195
2196/****************************************************************************
2197 * send_iosp_ext_cmd
2198 * Is used to send a IOSP message to the Edgeport device
2199 ****************************************************************************/
2200static int send_iosp_ext_cmd (struct edgeport_port *edge_port, __u8 command, __u8 param)
2201{
2202 unsigned char *buffer;
2203 unsigned char *currentCommand;
2204 int length = 0;
2205 int status = 0;
2206
Harvey Harrison441b62c2008-03-03 16:08:34 -08002207 dbg("%s - %d, %d", __func__, command, param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208
2209 buffer = kmalloc (10, GFP_ATOMIC);
2210 if (!buffer) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08002211 dev_err(&edge_port->port->dev, "%s - kmalloc(%d) failed.\n", __func__, 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 return -ENOMEM;
2213 }
2214
2215 currentCommand = buffer;
2216
2217 MAKE_CMD_EXT_CMD (&currentCommand, &length,
2218 edge_port->port->number - edge_port->port->serial->minor,
2219 command, param);
2220
2221 status = write_cmd_usb (edge_port, buffer, length);
2222 if (status) {
2223 /* something bad happened, let's free up the memory */
2224 kfree(buffer);
2225 }
2226
2227 return status;
2228}
2229
2230
2231/*****************************************************************************
2232 * write_cmd_usb
2233 * this function writes the given buffer out to the bulk write endpoint.
2234 *****************************************************************************/
2235static int write_cmd_usb (struct edgeport_port *edge_port, unsigned char *buffer, int length)
2236{
2237 struct edgeport_serial *edge_serial = usb_get_serial_data(edge_port->port->serial);
2238 int status = 0;
2239 struct urb *urb;
2240 int timeout;
2241
Harvey Harrison441b62c2008-03-03 16:08:34 -08002242 usb_serial_debug_data(debug, &edge_port->port->dev, __func__, length, buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243
2244 /* Allocate our next urb */
2245 urb = usb_alloc_urb (0, GFP_ATOMIC);
2246 if (!urb)
2247 return -ENOMEM;
2248
Oliver Neukum96c706e2007-03-15 15:27:17 +01002249 atomic_inc(&CmdUrbs);
Harvey Harrison441b62c2008-03-03 16:08:34 -08002250 dbg("%s - ALLOCATE URB %p (outstanding %d)", __func__, urb, atomic_read(&CmdUrbs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251
2252 usb_fill_bulk_urb (urb, edge_serial->serial->dev,
2253 usb_sndbulkpipe(edge_serial->serial->dev, edge_serial->bulk_out_endpoint),
2254 buffer, length, edge_bulk_out_cmd_callback, edge_port);
2255
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01002256 edge_port->commandPending = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 status = usb_submit_urb(urb, GFP_ATOMIC);
2258
2259 if (status) {
2260 /* something went wrong */
Harvey Harrison441b62c2008-03-03 16:08:34 -08002261 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 -07002262 usb_kill_urb(urb);
2263 usb_free_urb(urb);
Oliver Neukum96c706e2007-03-15 15:27:17 +01002264 atomic_dec(&CmdUrbs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 return status;
2266 }
2267
2268 // wait for command to finish
2269 timeout = COMMAND_TIMEOUT;
2270#if 0
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01002271 wait_event (&edge_port->wait_command, !edge_port->commandPending);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01002273 if (edge_port->commandPending) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 /* command timed out */
Harvey Harrison441b62c2008-03-03 16:08:34 -08002275 dbg("%s - command timed out", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 status = -EINVAL;
2277 }
2278#endif
2279 return status;
2280}
2281
2282
2283/*****************************************************************************
2284 * send_cmd_write_baud_rate
2285 * this function sends the proper command to change the baud rate of the
2286 * specified port.
2287 *****************************************************************************/
2288static int send_cmd_write_baud_rate (struct edgeport_port *edge_port, int baudRate)
2289{
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002290 struct edgeport_serial *edge_serial = usb_get_serial_data(edge_port->port->serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 unsigned char *cmdBuffer;
2292 unsigned char *currCmd;
2293 int cmdLen = 0;
2294 int divisor;
2295 int status;
2296 unsigned char number = edge_port->port->number - edge_port->port->serial->minor;
2297
Adam Kropelinbc71e472007-07-29 11:03:29 -04002298 if (edge_serial->is_epic &&
2299 !edge_serial->epic_descriptor.Supports.IOSPSetBaudRate) {
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002300 dbg("SendCmdWriteBaudRate - NOT Setting baud rate for port = %d, baud = %d",
2301 edge_port->port->number, baudRate);
2302 return 0;
2303 }
2304
Harvey Harrison441b62c2008-03-03 16:08:34 -08002305 dbg("%s - port = %d, baud = %d", __func__, edge_port->port->number, baudRate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306
2307 status = calc_baud_rate_divisor (baudRate, &divisor);
2308 if (status) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08002309 dev_err(&edge_port->port->dev, "%s - bad baud rate\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 return status;
2311 }
2312
2313 // Alloc memory for the string of commands.
2314 cmdBuffer = kmalloc (0x100, GFP_ATOMIC);
2315 if (!cmdBuffer) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08002316 dev_err(&edge_port->port->dev, "%s - kmalloc(%d) failed.\n", __func__, 0x100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 return -ENOMEM;
2318 }
2319 currCmd = cmdBuffer;
2320
2321 // Enable access to divisor latch
2322 MAKE_CMD_WRITE_REG( &currCmd, &cmdLen, number, LCR, LCR_DL_ENABLE );
2323
2324 // Write the divisor itself
2325 MAKE_CMD_WRITE_REG( &currCmd, &cmdLen, number, DLL, LOW8 (divisor) );
2326 MAKE_CMD_WRITE_REG( &currCmd, &cmdLen, number, DLM, HIGH8(divisor) );
2327
2328 // Restore original value to disable access to divisor latch
2329 MAKE_CMD_WRITE_REG( &currCmd, &cmdLen, number, LCR, edge_port->shadowLCR);
2330
2331 status = write_cmd_usb(edge_port, cmdBuffer, cmdLen );
2332 if (status) {
2333 /* something bad happened, let's free up the memory */
2334 kfree (cmdBuffer);
2335 }
2336
2337 return status;
2338}
2339
2340
2341/*****************************************************************************
2342 * calc_baud_rate_divisor
2343 * this function calculates the proper baud rate divisor for the specified
2344 * baud rate.
2345 *****************************************************************************/
2346static int calc_baud_rate_divisor (int baudrate, int *divisor)
2347{
2348 int i;
2349 __u16 custom;
2350
2351
Harvey Harrison441b62c2008-03-03 16:08:34 -08002352 dbg("%s - %d", __func__, baudrate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353
Tobias Klauser52950ed2005-12-11 16:20:08 +01002354 for (i = 0; i < ARRAY_SIZE(divisor_table); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 if ( divisor_table[i].BaudRate == baudrate ) {
2356 *divisor = divisor_table[i].Divisor;
2357 return 0;
2358 }
2359 }
2360
2361 // We have tried all of the standard baud rates
2362 // lets try to calculate the divisor for this baud rate
2363 // Make sure the baud rate is reasonable
2364 if (baudrate > 50 && baudrate < 230400) {
2365 // get divisor
2366 custom = (__u16)((230400L + baudrate/2) / baudrate);
2367
2368 *divisor = custom;
2369
Harvey Harrison441b62c2008-03-03 16:08:34 -08002370 dbg("%s - Baud %d = %d\n", __func__, baudrate, custom);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 return 0;
2372 }
2373
2374 return -1;
2375}
2376
2377
2378/*****************************************************************************
2379 * send_cmd_write_uart_register
2380 * this function builds up a uart register message and sends to to the device.
2381 *****************************************************************************/
2382static int send_cmd_write_uart_register (struct edgeport_port *edge_port, __u8 regNum, __u8 regValue)
2383{
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002384 struct edgeport_serial *edge_serial = usb_get_serial_data(edge_port->port->serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 unsigned char *cmdBuffer;
2386 unsigned char *currCmd;
2387 unsigned long cmdLen = 0;
2388 int status;
2389
Harvey Harrison441b62c2008-03-03 16:08:34 -08002390 dbg("%s - write to %s register 0x%02x", (regNum == MCR) ? "MCR" : "LCR", __func__, regValue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391
Adam Kropelinbc71e472007-07-29 11:03:29 -04002392 if (edge_serial->is_epic &&
2393 !edge_serial->epic_descriptor.Supports.IOSPWriteMCR &&
2394 regNum == MCR) {
Robert P. J. Daybeb7dd82007-05-09 07:14:03 +02002395 dbg("SendCmdWriteUartReg - Not writing to MCR Register");
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002396 return 0;
2397 }
2398
Adam Kropelinbc71e472007-07-29 11:03:29 -04002399 if (edge_serial->is_epic &&
2400 !edge_serial->epic_descriptor.Supports.IOSPWriteLCR &&
2401 regNum == LCR) {
Robert P. J. Daybeb7dd82007-05-09 07:14:03 +02002402 dbg ("SendCmdWriteUartReg - Not writing to LCR Register");
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002403 return 0;
2404 }
2405
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 // Alloc memory for the string of commands.
2407 cmdBuffer = kmalloc (0x10, GFP_ATOMIC);
2408 if (cmdBuffer == NULL ) {
2409 return -ENOMEM;
2410 }
2411
2412 currCmd = cmdBuffer;
2413
2414 // Build a cmd in the buffer to write the given register
2415 MAKE_CMD_WRITE_REG (&currCmd, &cmdLen,
2416 edge_port->port->number - edge_port->port->serial->minor,
2417 regNum, regValue);
2418
2419 status = write_cmd_usb(edge_port, cmdBuffer, cmdLen);
2420 if (status) {
2421 /* something bad happened, let's free up the memory */
2422 kfree (cmdBuffer);
2423 }
2424
2425 return status;
2426}
2427
2428
2429/*****************************************************************************
2430 * change_port_settings
2431 * This routine is called to set the UART on the device to match the specified
2432 * new settings.
2433 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01002434
2435static void change_port_settings(struct tty_struct *tty,
2436 struct edgeport_port *edge_port, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437{
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002438 struct edgeport_serial *edge_serial = usb_get_serial_data(edge_port->port->serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 int baud;
2440 unsigned cflag;
2441 __u8 mask = 0xff;
2442 __u8 lData;
2443 __u8 lParity;
2444 __u8 lStop;
2445 __u8 rxFlow;
2446 __u8 txFlow;
2447 int status;
2448
Harvey Harrison441b62c2008-03-03 16:08:34 -08002449 dbg("%s - port %d", __func__, edge_port->port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01002451 if (!edge_port->open &&
2452 !edge_port->openPending) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08002453 dbg("%s - port not opened", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 return;
2455 }
2456
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 cflag = tty->termios->c_cflag;
2458
2459 switch (cflag & CSIZE) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08002460 case CS5: lData = LCR_BITS_5; mask = 0x1f; dbg("%s - data bits = 5", __func__); break;
2461 case CS6: lData = LCR_BITS_6; mask = 0x3f; dbg("%s - data bits = 6", __func__); break;
2462 case CS7: lData = LCR_BITS_7; mask = 0x7f; dbg("%s - data bits = 7", __func__); break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 default:
Harvey Harrison441b62c2008-03-03 16:08:34 -08002464 case CS8: lData = LCR_BITS_8; dbg("%s - data bits = 8", __func__); break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 }
2466
2467 lParity = LCR_PAR_NONE;
2468 if (cflag & PARENB) {
2469 if (cflag & CMSPAR) {
2470 if (cflag & PARODD) {
2471 lParity = LCR_PAR_MARK;
Harvey Harrison441b62c2008-03-03 16:08:34 -08002472 dbg("%s - parity = mark", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 } else {
2474 lParity = LCR_PAR_SPACE;
Harvey Harrison441b62c2008-03-03 16:08:34 -08002475 dbg("%s - parity = space", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 }
2477 } else if (cflag & PARODD) {
2478 lParity = LCR_PAR_ODD;
Harvey Harrison441b62c2008-03-03 16:08:34 -08002479 dbg("%s - parity = odd", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 } else {
2481 lParity = LCR_PAR_EVEN;
Harvey Harrison441b62c2008-03-03 16:08:34 -08002482 dbg("%s - parity = even", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 }
2484 } else {
Harvey Harrison441b62c2008-03-03 16:08:34 -08002485 dbg("%s - parity = none", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 }
2487
2488 if (cflag & CSTOPB) {
2489 lStop = LCR_STOP_2;
Harvey Harrison441b62c2008-03-03 16:08:34 -08002490 dbg("%s - stop bits = 2", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 } else {
2492 lStop = LCR_STOP_1;
Harvey Harrison441b62c2008-03-03 16:08:34 -08002493 dbg("%s - stop bits = 1", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 }
2495
2496 /* figure out the flow control settings */
2497 rxFlow = txFlow = 0x00;
2498 if (cflag & CRTSCTS) {
2499 rxFlow |= IOSP_RX_FLOW_RTS;
2500 txFlow |= IOSP_TX_FLOW_CTS;
Harvey Harrison441b62c2008-03-03 16:08:34 -08002501 dbg("%s - RTS/CTS is enabled", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 } else {
Harvey Harrison441b62c2008-03-03 16:08:34 -08002503 dbg("%s - RTS/CTS is disabled", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 }
2505
2506 /* if we are implementing XON/XOFF, set the start and stop character in the device */
2507 if (I_IXOFF(tty) || I_IXON(tty)) {
2508 unsigned char stop_char = STOP_CHAR(tty);
2509 unsigned char start_char = START_CHAR(tty);
2510
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002511 if ((!edge_serial->is_epic) ||
2512 ((edge_serial->is_epic) &&
2513 (edge_serial->epic_descriptor.Supports.IOSPSetXChar))) {
2514 send_iosp_ext_cmd(edge_port, IOSP_CMD_SET_XON_CHAR, start_char);
2515 send_iosp_ext_cmd(edge_port, IOSP_CMD_SET_XOFF_CHAR, stop_char);
2516 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517
2518 /* if we are implementing INBOUND XON/XOFF */
2519 if (I_IXOFF(tty)) {
2520 rxFlow |= IOSP_RX_FLOW_XON_XOFF;
Harvey Harrison441b62c2008-03-03 16:08:34 -08002521 dbg("%s - INBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x", __func__, start_char, stop_char);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 } else {
Harvey Harrison441b62c2008-03-03 16:08:34 -08002523 dbg("%s - INBOUND XON/XOFF is disabled", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524 }
2525
2526 /* if we are implementing OUTBOUND XON/XOFF */
2527 if (I_IXON(tty)) {
2528 txFlow |= IOSP_TX_FLOW_XON_XOFF;
Harvey Harrison441b62c2008-03-03 16:08:34 -08002529 dbg("%s - OUTBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x", __func__, start_char, stop_char);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 } else {
Harvey Harrison441b62c2008-03-03 16:08:34 -08002531 dbg("%s - OUTBOUND XON/XOFF is disabled", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 }
2533 }
2534
2535 /* Set flow control to the configured value */
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002536 if ((!edge_serial->is_epic) ||
2537 ((edge_serial->is_epic) &&
2538 (edge_serial->epic_descriptor.Supports.IOSPSetRxFlow)))
2539 send_iosp_ext_cmd(edge_port, IOSP_CMD_SET_RX_FLOW, rxFlow);
2540 if ((!edge_serial->is_epic) ||
2541 ((edge_serial->is_epic) &&
2542 (edge_serial->epic_descriptor.Supports.IOSPSetTxFlow)))
2543 send_iosp_ext_cmd(edge_port, IOSP_CMD_SET_TX_FLOW, txFlow);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544
2545
2546 edge_port->shadowLCR &= ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
2547 edge_port->shadowLCR |= (lData | lParity | lStop);
2548
2549 edge_port->validDataMask = mask;
2550
2551 /* Send the updated LCR value to the EdgePort */
2552 status = send_cmd_write_uart_register(edge_port, LCR, edge_port->shadowLCR);
2553 if (status != 0) {
2554 return;
2555 }
2556
2557 /* set up the MCR register and send it to the EdgePort */
2558 edge_port->shadowMCR = MCR_MASTER_IE;
2559 if (cflag & CBAUD) {
2560 edge_port->shadowMCR |= (MCR_DTR | MCR_RTS);
2561 }
2562 status = send_cmd_write_uart_register(edge_port, MCR, edge_port->shadowMCR);
2563 if (status != 0) {
2564 return;
2565 }
2566
2567 /* Determine divisor based on baud rate */
2568 baud = tty_get_baud_rate(tty);
2569 if (!baud) {
2570 /* pick a default, any default... */
2571 baud = 9600;
2572 }
2573
Harvey Harrison441b62c2008-03-03 16:08:34 -08002574 dbg("%s - baud rate = %d", __func__, baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575 status = send_cmd_write_baud_rate (edge_port, baud);
Alan Cox6ce073b2007-10-18 01:24:25 -07002576 if (status == -1) {
2577 /* Speed change was not possible - put back the old speed */
2578 baud = tty_termios_baud_rate(old_termios);
2579 tty_encode_baud_rate(tty, baud, baud);
2580 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 return;
2582}
2583
2584
2585/****************************************************************************
2586 * unicode_to_ascii
2587 * Turns a string from Unicode into ASCII.
2588 * Doesn't do a good job with any characters that are outside the normal
2589 * ASCII range, but it's only for debugging...
2590 * NOTE: expects the unicode in LE format
2591 ****************************************************************************/
Pete Zaitcevad933752006-05-22 21:49:44 -07002592static void unicode_to_ascii(char *string, int buflen, __le16 *unicode, int unicode_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593{
2594 int i;
2595
Pete Zaitcevad933752006-05-22 21:49:44 -07002596 if (buflen <= 0) /* never happens, but... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 return;
Pete Zaitcevad933752006-05-22 21:49:44 -07002598 --buflen; /* space for nul */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599
Pete Zaitcevad933752006-05-22 21:49:44 -07002600 for (i = 0; i < unicode_size; i++) {
2601 if (i >= buflen)
2602 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 string[i] = (char)(le16_to_cpu(unicode[i]));
Pete Zaitcevad933752006-05-22 21:49:44 -07002604 }
2605 string[i] = 0x00;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606}
2607
2608
2609/****************************************************************************
2610 * get_manufacturing_desc
2611 * reads in the manufacturing descriptor and stores it into the serial
2612 * structure.
2613 ****************************************************************************/
2614static void get_manufacturing_desc (struct edgeport_serial *edge_serial)
2615{
2616 int response;
2617
2618 dbg("getting manufacturer descriptor");
2619
2620 response = rom_read (edge_serial->serial, (EDGE_MANUF_DESC_ADDR & 0xffff0000) >> 16,
2621 (__u16)(EDGE_MANUF_DESC_ADDR & 0x0000ffff), EDGE_MANUF_DESC_LEN,
2622 (__u8 *)(&edge_serial->manuf_descriptor));
2623
2624 if (response < 1) {
2625 dev_err(&edge_serial->serial->dev->dev, "error in getting manufacturer descriptor\n");
2626 } else {
2627 char string[30];
2628 dbg("**Manufacturer Descriptor");
2629 dbg(" RomSize: %dK", edge_serial->manuf_descriptor.RomSize);
2630 dbg(" RamSize: %dK", edge_serial->manuf_descriptor.RamSize);
2631 dbg(" CpuRev: %d", edge_serial->manuf_descriptor.CpuRev);
2632 dbg(" BoardRev: %d", edge_serial->manuf_descriptor.BoardRev);
2633 dbg(" NumPorts: %d", edge_serial->manuf_descriptor.NumPorts);
2634 dbg(" DescDate: %d/%d/%d", edge_serial->manuf_descriptor.DescDate[0], edge_serial->manuf_descriptor.DescDate[1], edge_serial->manuf_descriptor.DescDate[2]+1900);
Pete Zaitcev4bc203d2006-06-06 18:18:33 -07002635 unicode_to_ascii(string, sizeof(string),
Pete Zaitcevad933752006-05-22 21:49:44 -07002636 edge_serial->manuf_descriptor.SerialNumber,
2637 edge_serial->manuf_descriptor.SerNumLength/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 dbg(" SerialNumber: %s", string);
Pete Zaitcev4bc203d2006-06-06 18:18:33 -07002639 unicode_to_ascii(string, sizeof(string),
Pete Zaitcevad933752006-05-22 21:49:44 -07002640 edge_serial->manuf_descriptor.AssemblyNumber,
2641 edge_serial->manuf_descriptor.AssemblyNumLength/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 dbg(" AssemblyNumber: %s", string);
Pete Zaitcev4bc203d2006-06-06 18:18:33 -07002643 unicode_to_ascii(string, sizeof(string),
Pete Zaitcevad933752006-05-22 21:49:44 -07002644 edge_serial->manuf_descriptor.OemAssyNumber,
2645 edge_serial->manuf_descriptor.OemAssyNumLength/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 dbg(" OemAssyNumber: %s", string);
2647 dbg(" UartType: %d", edge_serial->manuf_descriptor.UartType);
2648 dbg(" IonPid: %d", edge_serial->manuf_descriptor.IonPid);
2649 dbg(" IonConfig: %d", edge_serial->manuf_descriptor.IonConfig);
2650 }
2651}
2652
2653
2654/****************************************************************************
2655 * get_boot_desc
2656 * reads in the bootloader descriptor and stores it into the serial
2657 * structure.
2658 ****************************************************************************/
2659static void get_boot_desc (struct edgeport_serial *edge_serial)
2660{
2661 int response;
2662
2663 dbg("getting boot descriptor");
2664
2665 response = rom_read (edge_serial->serial, (EDGE_BOOT_DESC_ADDR & 0xffff0000) >> 16,
2666 (__u16)(EDGE_BOOT_DESC_ADDR & 0x0000ffff), EDGE_BOOT_DESC_LEN,
2667 (__u8 *)(&edge_serial->boot_descriptor));
2668
2669 if (response < 1) {
2670 dev_err(&edge_serial->serial->dev->dev, "error in getting boot descriptor\n");
2671 } else {
2672 dbg("**Boot Descriptor:");
2673 dbg(" BootCodeLength: %d", le16_to_cpu(edge_serial->boot_descriptor.BootCodeLength));
2674 dbg(" MajorVersion: %d", edge_serial->boot_descriptor.MajorVersion);
2675 dbg(" MinorVersion: %d", edge_serial->boot_descriptor.MinorVersion);
2676 dbg(" BuildNumber: %d", le16_to_cpu(edge_serial->boot_descriptor.BuildNumber));
2677 dbg(" Capabilities: 0x%x", le16_to_cpu(edge_serial->boot_descriptor.Capabilities));
2678 dbg(" UConfig0: %d", edge_serial->boot_descriptor.UConfig0);
2679 dbg(" UConfig1: %d", edge_serial->boot_descriptor.UConfig1);
2680 }
2681}
2682
2683
2684/****************************************************************************
2685 * load_application_firmware
2686 * This is called to load the application firmware to the device
2687 ****************************************************************************/
2688static void load_application_firmware (struct edgeport_serial *edge_serial)
2689{
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302690 const struct ihex_binrec *rec;
2691 const struct firmware *fw;
2692 const char *fw_name;
2693 const char *fw_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 int response;
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302695 __u32 Operaddr;
2696 __u16 build;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697
2698 switch (edge_serial->product_info.iDownloadFile) {
2699 case EDGE_DOWNLOAD_FILE_I930:
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302700 fw_info = "downloading firmware version (930)";
2701 fw_name = "edgeport/down.fw";
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 break;
2703
2704 case EDGE_DOWNLOAD_FILE_80251:
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302705 fw_info = "downloading firmware version (80251)";
2706 fw_name = "edgeport/down2.fw";
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 break;
2708
2709 case EDGE_DOWNLOAD_FILE_NONE:
2710 dbg ("No download file specified, skipping download\n");
2711 return;
2712
2713 default:
2714 return;
2715 }
2716
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302717 response = request_ihex_firmware(&fw, fw_name,
2718 &edge_serial->serial->dev->dev);
2719 if (response) {
2720 printk(KERN_ERR "Failed to load image \"%s\" err %d\n",
2721 fw_name, response);
2722 return;
2723 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302725 rec = (const struct ihex_binrec *)fw->data;
2726 build = (rec->data[2] << 8) | rec->data[3];
2727
2728 dbg("%s %d.%d.%d", fw_info, rec->data[0], rec->data[1], build);
2729
2730 edge_serial->product_info.FirmwareMajorVersion = fw->data[0];
2731 edge_serial->product_info.FirmwareMinorVersion = fw->data[1];
2732 edge_serial->product_info.FirmwareBuildNumber = cpu_to_le16(build);
2733
2734 for (rec = ihex_next_binrec(rec); rec;
2735 rec = ihex_next_binrec(rec)) {
2736 Operaddr = be32_to_cpu(rec->addr);
2737 response = sram_write(edge_serial->serial,
2738 Operaddr >> 16,
2739 Operaddr & 0xFFFF,
2740 be16_to_cpu(rec->len),
2741 &rec->data[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 if (response < 0) {
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302743 dev_err(&edge_serial->serial->dev->dev,
2744 "sram_write failed (%x, %x, %d)\n",
2745 Operaddr >> 16, Operaddr & 0xFFFF,
2746 be16_to_cpu(rec->len));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 break;
2748 }
2749 }
2750
2751 dbg("sending exec_dl_code");
2752 response = usb_control_msg (edge_serial->serial->dev,
2753 usb_sndctrlpipe(edge_serial->serial->dev, 0),
2754 USB_REQUEST_ION_EXEC_DL_CODE,
2755 0x40, 0x4000, 0x0001, NULL, 0, 3000);
2756
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302757 release_firmware(fw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 return;
2759}
2760
2761
2762/****************************************************************************
2763 * edge_startup
2764 ****************************************************************************/
2765static int edge_startup (struct usb_serial *serial)
2766{
2767 struct edgeport_serial *edge_serial;
2768 struct edgeport_port *edge_port;
2769 struct usb_device *dev;
Chris Lundbfd5df32006-06-03 13:58:19 -07002770 int i, j;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002771 int response;
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01002772 bool interrupt_in_found;
2773 bool bulk_in_found;
2774 bool bulk_out_found;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002775 static __u32 descriptor[3] = { EDGE_COMPATIBILITY_MASK0,
2776 EDGE_COMPATIBILITY_MASK1,
2777 EDGE_COMPATIBILITY_MASK2 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778
2779 dev = serial->dev;
2780
2781 /* create our private serial structure */
Eric Sesterhenn80b6ca42006-02-27 21:29:43 +01002782 edge_serial = kzalloc(sizeof(struct edgeport_serial), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783 if (edge_serial == NULL) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08002784 dev_err(&serial->dev->dev, "%s - Out of memory\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 return -ENOMEM;
2786 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 spin_lock_init(&edge_serial->es_lock);
2788 edge_serial->serial = serial;
2789 usb_set_serial_data(serial, edge_serial);
2790
2791 /* get the name for the device from the device */
Pete Zaitcevad933752006-05-22 21:49:44 -07002792 i = get_string(dev, dev->descriptor.iManufacturer,
2793 &edge_serial->name[0], MAX_NAME_LEN+1);
2794 edge_serial->name[i++] = ' ';
2795 get_string(dev, dev->descriptor.iProduct,
2796 &edge_serial->name[i], MAX_NAME_LEN+2 - i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797
2798 dev_info(&serial->dev->dev, "%s detected\n", edge_serial->name);
2799
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002800 /* Read the epic descriptor */
2801 if (get_epic_descriptor(edge_serial) <= 0) {
2802 /* memcpy descriptor to Supports structures */
2803 memcpy(&edge_serial->epic_descriptor.Supports, descriptor,
2804 sizeof(struct edge_compatibility_bits));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002806 /* get the manufacturing descriptor for this device */
2807 get_manufacturing_desc (edge_serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002809 /* get the boot descriptor */
2810 get_boot_desc (edge_serial);
2811
2812 get_product_info(edge_serial);
2813 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814
2815 /* set the number of ports from the manufacturing description */
2816 /* serial->num_ports = serial->product_info.NumPorts; */
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002817 if ((!edge_serial->is_epic) &&
2818 (edge_serial->product_info.NumPorts != serial->num_ports)) {
2819 dev_warn(&serial->dev->dev, "Device Reported %d serial ports "
2820 "vs. core thinking we have %d ports, email "
Joe Perches898eb712007-10-18 03:06:30 -07002821 "greg@kroah.com this information.\n",
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002822 edge_serial->product_info.NumPorts,
2823 serial->num_ports);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824 }
2825
Harvey Harrison441b62c2008-03-03 16:08:34 -08002826 dbg("%s - time 1 %ld", __func__, jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002828 /* If not an EPiC device */
2829 if (!edge_serial->is_epic) {
2830 /* now load the application firmware into this device */
2831 load_application_firmware (edge_serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832
Harvey Harrison441b62c2008-03-03 16:08:34 -08002833 dbg("%s - time 2 %ld", __func__, jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002835 /* Check current Edgeport EEPROM and update if necessary */
2836 update_edgeport_E2PROM (edge_serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837
Harvey Harrison441b62c2008-03-03 16:08:34 -08002838 dbg("%s - time 3 %ld", __func__, jiffies);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002839
2840 /* set the configuration to use #1 */
2841// dbg("set_configuration 1");
2842// usb_set_configuration (dev, 1);
2843 }
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302844 dbg(" FirmwareMajorVersion %d.%d.%d",
2845 edge_serial->product_info.FirmwareMajorVersion,
2846 edge_serial->product_info.FirmwareMinorVersion,
2847 le16_to_cpu(edge_serial->product_info.FirmwareBuildNumber));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848
2849 /* we set up the pointers to the endpoints in the edge_open function,
2850 * as the structures aren't created yet. */
2851
2852 /* set up our port private structures */
2853 for (i = 0; i < serial->num_ports; ++i) {
2854 edge_port = kmalloc (sizeof(struct edgeport_port), GFP_KERNEL);
2855 if (edge_port == NULL) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08002856 dev_err(&serial->dev->dev, "%s - Out of memory\n", __func__);
Chris Lundbfd5df32006-06-03 13:58:19 -07002857 for (j = 0; j < i; ++j) {
2858 kfree (usb_get_serial_port_data(serial->port[j]));
2859 usb_set_serial_port_data(serial->port[j], NULL);
2860 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 usb_set_serial_data(serial, NULL);
2862 kfree(edge_serial);
2863 return -ENOMEM;
2864 }
2865 memset (edge_port, 0, sizeof(struct edgeport_port));
2866 spin_lock_init(&edge_port->ep_lock);
2867 edge_port->port = serial->port[i];
2868 usb_set_serial_port_data(serial->port[i], edge_port);
2869 }
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002870
2871 response = 0;
2872
2873 if (edge_serial->is_epic) {
2874 /* EPIC thing, set up our interrupt polling now and our read urb, so
2875 * that the device knows it really is connected. */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01002876 interrupt_in_found = bulk_in_found = bulk_out_found = false;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002877 for (i = 0; i < serial->interface->altsetting[0].desc.bNumEndpoints; ++i) {
2878 struct usb_endpoint_descriptor *endpoint;
2879 int buffer_size;
2880
2881 endpoint = &serial->interface->altsetting[0].endpoint[i].desc;
2882 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01002883 if (!interrupt_in_found &&
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002884 (usb_endpoint_is_int_in(endpoint))) {
2885 /* we found a interrupt in endpoint */
2886 dbg("found interrupt in");
2887
2888 /* not set up yet, so do it now */
2889 edge_serial->interrupt_read_urb = usb_alloc_urb(0, GFP_KERNEL);
2890 if (!edge_serial->interrupt_read_urb) {
2891 err("out of memory");
2892 return -ENOMEM;
2893 }
2894 edge_serial->interrupt_in_buffer = kmalloc(buffer_size, GFP_KERNEL);
2895 if (!edge_serial->interrupt_in_buffer) {
2896 err("out of memory");
2897 usb_free_urb(edge_serial->interrupt_read_urb);
2898 return -ENOMEM;
2899 }
2900 edge_serial->interrupt_in_endpoint = endpoint->bEndpointAddress;
2901
2902 /* set up our interrupt urb */
2903 usb_fill_int_urb(edge_serial->interrupt_read_urb,
2904 dev,
2905 usb_rcvintpipe(dev, endpoint->bEndpointAddress),
2906 edge_serial->interrupt_in_buffer,
2907 buffer_size,
2908 edge_interrupt_callback,
2909 edge_serial,
2910 endpoint->bInterval);
2911
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01002912 interrupt_in_found = true;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002913 }
2914
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01002915 if (!bulk_in_found &&
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002916 (usb_endpoint_is_bulk_in(endpoint))) {
2917 /* we found a bulk in endpoint */
2918 dbg("found bulk in");
2919
2920 /* not set up yet, so do it now */
2921 edge_serial->read_urb = usb_alloc_urb(0, GFP_KERNEL);
2922 if (!edge_serial->read_urb) {
2923 err("out of memory");
2924 return -ENOMEM;
2925 }
2926 edge_serial->bulk_in_buffer = kmalloc(buffer_size, GFP_KERNEL);
2927 if (!edge_serial->bulk_in_buffer) {
2928 err ("out of memory");
2929 usb_free_urb(edge_serial->read_urb);
2930 return -ENOMEM;
2931 }
2932 edge_serial->bulk_in_endpoint = endpoint->bEndpointAddress;
2933
2934 /* set up our bulk in urb */
2935 usb_fill_bulk_urb(edge_serial->read_urb, dev,
2936 usb_rcvbulkpipe(dev, endpoint->bEndpointAddress),
2937 edge_serial->bulk_in_buffer,
Al Virofd05e722008-04-28 07:00:16 +01002938 le16_to_cpu(endpoint->wMaxPacketSize),
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002939 edge_bulk_in_callback,
2940 edge_serial);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01002941 bulk_in_found = true;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002942 }
2943
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01002944 if (!bulk_out_found &&
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002945 (usb_endpoint_is_bulk_out(endpoint))) {
2946 /* we found a bulk out endpoint */
2947 dbg("found bulk out");
2948 edge_serial->bulk_out_endpoint = endpoint->bEndpointAddress;
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01002949 bulk_out_found = true;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002950 }
2951 }
2952
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01002953 if (!interrupt_in_found || !bulk_in_found || !bulk_out_found) {
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002954 err ("Error - the proper endpoints were not found!");
2955 return -ENODEV;
2956 }
2957
2958 /* start interrupt read for this edgeport this interrupt will
2959 * continue as long as the edgeport is connected */
2960 response = usb_submit_urb(edge_serial->interrupt_read_urb, GFP_KERNEL);
2961 if (response)
Harvey Harrison441b62c2008-03-03 16:08:34 -08002962 err("%s - Error %d submitting control urb", __func__, response);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002963 }
2964 return response;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965}
2966
2967
2968/****************************************************************************
2969 * edge_shutdown
2970 * This function is called whenever the device is removed from the usb bus.
2971 ****************************************************************************/
2972static void edge_shutdown (struct usb_serial *serial)
2973{
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002974 struct edgeport_serial *edge_serial = usb_get_serial_data(serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975 int i;
2976
Harvey Harrison441b62c2008-03-03 16:08:34 -08002977 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978
2979 /* stop reads and writes on all ports */
2980 for (i=0; i < serial->num_ports; ++i) {
2981 kfree (usb_get_serial_port_data(serial->port[i]));
2982 usb_set_serial_port_data(serial->port[i], NULL);
2983 }
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002984 /* free up our endpoint stuff */
2985 if (edge_serial->is_epic) {
Oliver Neukum74ac07e2007-06-13 18:50:41 +02002986 usb_kill_urb(edge_serial->interrupt_read_urb);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002987 usb_free_urb(edge_serial->interrupt_read_urb);
2988 kfree(edge_serial->interrupt_in_buffer);
2989
Oliver Neukum74ac07e2007-06-13 18:50:41 +02002990 usb_kill_urb(edge_serial->read_urb);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002991 usb_free_urb(edge_serial->read_urb);
2992 kfree(edge_serial->bulk_in_buffer);
2993 }
2994
2995 kfree(edge_serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996 usb_set_serial_data(serial, NULL);
2997}
2998
2999
3000/****************************************************************************
3001 * edgeport_init
3002 * This is called by the module subsystem, or on startup to initialize us
3003 ****************************************************************************/
3004static int __init edgeport_init(void)
3005{
3006 int retval;
3007
3008 retval = usb_serial_register(&edgeport_2port_device);
3009 if (retval)
3010 goto failed_2port_device_register;
3011 retval = usb_serial_register(&edgeport_4port_device);
3012 if (retval)
3013 goto failed_4port_device_register;
3014 retval = usb_serial_register(&edgeport_8port_device);
3015 if (retval)
3016 goto failed_8port_device_register;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003017 retval = usb_serial_register(&epic_device);
3018 if (retval)
3019 goto failed_epic_device_register;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 retval = usb_register(&io_driver);
3021 if (retval)
3022 goto failed_usb_register;
Oliver Neukum96c706e2007-03-15 15:27:17 +01003023 atomic_set(&CmdUrbs, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024 info(DRIVER_DESC " " DRIVER_VERSION);
3025 return 0;
3026
3027failed_usb_register:
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003028 usb_serial_deregister(&epic_device);
3029failed_epic_device_register:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030 usb_serial_deregister(&edgeport_8port_device);
3031failed_8port_device_register:
3032 usb_serial_deregister(&edgeport_4port_device);
3033failed_4port_device_register:
3034 usb_serial_deregister(&edgeport_2port_device);
3035failed_2port_device_register:
3036 return retval;
3037}
3038
3039
3040/****************************************************************************
3041 * edgeport_exit
3042 * Called when the driver is about to be unloaded.
3043 ****************************************************************************/
3044static void __exit edgeport_exit (void)
3045{
3046 usb_deregister (&io_driver);
3047 usb_serial_deregister (&edgeport_2port_device);
3048 usb_serial_deregister (&edgeport_4port_device);
3049 usb_serial_deregister (&edgeport_8port_device);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003050 usb_serial_deregister (&epic_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051}
3052
3053module_init(edgeport_init);
3054module_exit(edgeport_exit);
3055
3056/* Module information */
3057MODULE_AUTHOR( DRIVER_AUTHOR );
3058MODULE_DESCRIPTION( DRIVER_DESC );
3059MODULE_LICENSE("GPL");
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05303060MODULE_FIRMWARE("edgeport/boot.fw");
3061MODULE_FIRMWARE("edgeport/boot2.fw");
3062MODULE_FIRMWARE("edgeport/down.fw");
3063MODULE_FIRMWARE("edgeport/down2.fw");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064
3065module_param(debug, bool, S_IRUGO | S_IWUSR);
3066MODULE_PARM_DESC(debug, "Debug enabled or not");
3067
3068module_param(low_latency, bool, S_IRUGO | S_IWUSR);
3069MODULE_PARM_DESC(low_latency, "Low latency enabled or not");