OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [linux/] [linux-2.4/] [drivers/] [usb/] [serial/] [io_edgeport.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.