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