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

Subversion Repositories or1k

[/] [or1k/] [trunk/] [linux/] [linux-2.4/] [drivers/] [char/] [istallion.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1275 phoenix
/*****************************************************************************/
2
 
3
/*
4
 *      istallion.c  -- stallion intelligent multiport serial driver.
5
 *
6
 *      Copyright (C) 1996-1999  Stallion Technologies (support@stallion.oz.au).
7
 *      Copyright (C) 1994-1996  Greg Ungerer.
8
 *
9
 *      This code is loosely based on the Linux serial driver, written by
10
 *      Linus Torvalds, Theodore T'so and others.
11
 *
12
 *      This program is free software; you can redistribute it and/or modify
13
 *      it under the terms of the GNU General Public License as published by
14
 *      the Free Software Foundation; either version 2 of the License, or
15
 *      (at your option) any later version.
16
 *
17
 *      This program is distributed in the hope that it will be useful,
18
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 *      GNU General Public License for more details.
21
 *
22
 *      You should have received a copy of the GNU General Public License
23
 *      along with this program; if not, write to the Free Software
24
 *      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25
 */
26
 
27
/*****************************************************************************/
28
 
29
#include <linux/config.h>
30
#include <linux/module.h>
31
#include <linux/slab.h>
32
#include <linux/interrupt.h>
33
#include <linux/tty.h>
34
#include <linux/tty_flip.h>
35
#include <linux/serial.h>
36
#include <linux/cdk.h>
37
#include <linux/comstats.h>
38
#include <linux/version.h>
39
#include <linux/istallion.h>
40
#include <linux/ioport.h>
41
#include <linux/delay.h>
42
#include <linux/init.h>
43
#include <linux/devfs_fs_kernel.h>
44
 
45
#include <asm/io.h>
46
#include <asm/uaccess.h>
47
 
48
#ifdef CONFIG_PCI
49
#include <linux/pci.h>
50
#endif
51
 
52
/*****************************************************************************/
53
 
54
/*
55
 *      Define different board types. Not all of the following board types
56
 *      are supported by this driver. But I will use the standard "assigned"
57
 *      board numbers. Currently supported boards are abbreviated as:
58
 *      ECP = EasyConnection 8/64, ONB = ONboard, BBY = Brumby and
59
 *      STAL = Stallion.
60
 */
61
#define BRD_UNKNOWN     0
62
#define BRD_STALLION    1
63
#define BRD_BRUMBY4     2
64
#define BRD_ONBOARD2    3
65
#define BRD_ONBOARD     4
66
#define BRD_BRUMBY8     5
67
#define BRD_BRUMBY16    6
68
#define BRD_ONBOARDE    7
69
#define BRD_ONBOARD32   9
70
#define BRD_ONBOARD2_32 10
71
#define BRD_ONBOARDRS   11
72
#define BRD_EASYIO      20
73
#define BRD_ECH         21
74
#define BRD_ECHMC       22
75
#define BRD_ECP         23
76
#define BRD_ECPE        24
77
#define BRD_ECPMC       25
78
#define BRD_ECHPCI      26
79
#define BRD_ECH64PCI    27
80
#define BRD_EASYIOPCI   28
81
#define BRD_ECPPCI      29
82
 
83
#define BRD_BRUMBY      BRD_BRUMBY4
84
 
85
/*
86
 *      Define a configuration structure to hold the board configuration.
87
 *      Need to set this up in the code (for now) with the boards that are
88
 *      to be configured into the system. This is what needs to be modified
89
 *      when adding/removing/modifying boards. Each line entry in the
90
 *      stli_brdconf[] array is a board. Each line contains io/irq/memory
91
 *      ranges for that board (as well as what type of board it is).
92
 *      Some examples:
93
 *              { BRD_ECP, 0x2a0, 0, 0xcc000, 0, 0 },
94
 *      This line will configure an EasyConnection 8/64 at io address 2a0,
95
 *      and shared memory address of cc000. Multiple EasyConnection 8/64
96
 *      boards can share the same shared memory address space. No interrupt
97
 *      is required for this board type.
98
 *      Another example:
99
 *              { BRD_ECPE, 0x5000, 0, 0x80000000, 0, 0 },
100
 *      This line will configure an EasyConnection 8/64 EISA in slot 5 and
101
 *      shared memory address of 0x80000000 (2 GByte). Multiple
102
 *      EasyConnection 8/64 EISA boards can share the same shared memory
103
 *      address space. No interrupt is required for this board type.
104
 *      Another example:
105
 *              { BRD_ONBOARD, 0x240, 0, 0xd0000, 0, 0 },
106
 *      This line will configure an ONboard (ISA type) at io address 240,
107
 *      and shared memory address of d0000. Multiple ONboards can share
108
 *      the same shared memory address space. No interrupt required.
109
 *      Another example:
110
 *              { BRD_BRUMBY4, 0x360, 0, 0xc8000, 0, 0 },
111
 *      This line will configure a Brumby board (any number of ports!) at
112
 *      io address 360 and shared memory address of c8000. All Brumby boards
113
 *      configured into a system must have their own separate io and memory
114
 *      addresses. No interrupt is required.
115
 *      Another example:
116
 *              { BRD_STALLION, 0x330, 0, 0xd0000, 0, 0 },
117
 *      This line will configure an original Stallion board at io address 330
118
 *      and shared memory address d0000 (this would only be valid for a "V4.0"
119
 *      or Rev.O Stallion board). All Stallion boards configured into the
120
 *      system must have their own separate io and memory addresses. No
121
 *      interrupt is required.
122
 */
123
 
124
typedef struct {
125
        int             brdtype;
126
        int             ioaddr1;
127
        int             ioaddr2;
128
        unsigned long   memaddr;
129
        int             irq;
130
        int             irqtype;
131
} stlconf_t;
132
 
133
static stlconf_t        stli_brdconf[] = {
134
        /*{ BRD_ECP, 0x2a0, 0, 0xcc000, 0, 0 },*/
135
};
136
 
137
static int      stli_nrbrds = sizeof(stli_brdconf) / sizeof(stlconf_t);
138
 
139
/*
140
 *      There is some experimental EISA board detection code in this driver.
141
 *      By default it is disabled, but for those that want to try it out,
142
 *      then set the define below to be 1.
143
 */
144
#define STLI_EISAPROBE  0
145
 
146
static devfs_handle_t devfs_handle;
147
 
148
/*****************************************************************************/
149
 
150
/*
151
 *      Define some important driver characteristics. Device major numbers
152
 *      allocated as per Linux Device Registry.
153
 */
154
#ifndef STL_SIOMEMMAJOR
155
#define STL_SIOMEMMAJOR         28
156
#endif
157
#ifndef STL_SERIALMAJOR
158
#define STL_SERIALMAJOR         24
159
#endif
160
#ifndef STL_CALLOUTMAJOR
161
#define STL_CALLOUTMAJOR        25
162
#endif
163
 
164
#define STL_DRVTYPSERIAL        1
165
#define STL_DRVTYPCALLOUT       2
166
 
167
/*****************************************************************************/
168
 
169
/*
170
 *      Define our local driver identity first. Set up stuff to deal with
171
 *      all the local structures required by a serial tty driver.
172
 */
173
static char     *stli_drvtitle = "Stallion Intelligent Multiport Serial Driver";
174
static char     *stli_drvname = "istallion";
175
static char     *stli_drvversion = "5.6.0";
176
static char     *stli_serialname = "ttyE";
177
static char     *stli_calloutname = "cue";
178
 
179
static struct tty_driver        stli_serial;
180
static struct tty_driver        stli_callout;
181
static struct tty_struct        *stli_ttys[STL_MAXDEVS];
182
static struct termios           *stli_termios[STL_MAXDEVS];
183
static struct termios           *stli_termioslocked[STL_MAXDEVS];
184
static int                      stli_refcount;
185
 
186
/*
187
 *      We will need to allocate a temporary write buffer for chars that
188
 *      come direct from user space. The problem is that a copy from user
189
 *      space might cause a page fault (typically on a system that is
190
 *      swapping!). All ports will share one buffer - since if the system
191
 *      is already swapping a shared buffer won't make things any worse.
192
 */
193
static char                     *stli_tmpwritebuf;
194
static DECLARE_MUTEX(stli_tmpwritesem);
195
 
196
#define STLI_TXBUFSIZE          4096
197
 
198
/*
199
 *      Use a fast local buffer for cooked characters. Typically a whole
200
 *      bunch of cooked characters come in for a port, 1 at a time. So we
201
 *      save those up into a local buffer, then write out the whole lot
202
 *      with a large memcpy. Just use 1 buffer for all ports, since its
203
 *      use it is only need for short periods of time by each port.
204
 */
205
static char                     *stli_txcookbuf;
206
static int                      stli_txcooksize;
207
static int                      stli_txcookrealsize;
208
static struct tty_struct        *stli_txcooktty;
209
 
210
/*
211
 *      Define a local default termios struct. All ports will be created
212
 *      with this termios initially. Basically all it defines is a raw port
213
 *      at 9600 baud, 8 data bits, no parity, 1 stop bit.
214
 */
215
static struct termios           stli_deftermios = {
216
        c_cflag:        (B9600 | CS8 | CREAD | HUPCL | CLOCAL),
217
        c_cc:           INIT_C_CC,
218
};
219
 
220
/*
221
 *      Define global stats structures. Not used often, and can be
222
 *      re-used for each stats call.
223
 */
224
static comstats_t       stli_comstats;
225
static combrd_t         stli_brdstats;
226
static asystats_t       stli_cdkstats;
227
static stlibrd_t        stli_dummybrd;
228
static stliport_t       stli_dummyport;
229
 
230
/*****************************************************************************/
231
 
232
static stlibrd_t        *stli_brds[STL_MAXBRDS];
233
 
234
static int              stli_shared;
235
 
236
/*
237
 *      Per board state flags. Used with the state field of the board struct.
238
 *      Not really much here... All we need to do is keep track of whether
239
 *      the board has been detected, and whether it is actually running a slave
240
 *      or not.
241
 */
242
#define BST_FOUND       0x1
243
#define BST_STARTED     0x2
244
 
245
/*
246
 *      Define the set of port state flags. These are marked for internal
247
 *      state purposes only, usually to do with the state of communications
248
 *      with the slave. Most of them need to be updated atomically, so always
249
 *      use the bit setting operations (unless protected by cli/sti).
250
 */
251
#define ST_INITIALIZING 1
252
#define ST_OPENING      2
253
#define ST_CLOSING      3
254
#define ST_CMDING       4
255
#define ST_TXBUSY       5
256
#define ST_RXING        6
257
#define ST_DOFLUSHRX    7
258
#define ST_DOFLUSHTX    8
259
#define ST_DOSIGS       9
260
#define ST_RXSTOP       10
261
#define ST_GETSIGS      11
262
 
263
/*
264
 *      Define an array of board names as printable strings. Handy for
265
 *      referencing boards when printing trace and stuff.
266
 */
267
static char     *stli_brdnames[] = {
268
        "Unknown",
269
        "Stallion",
270
        "Brumby",
271
        "ONboard-MC",
272
        "ONboard",
273
        "Brumby",
274
        "Brumby",
275
        "ONboard-EI",
276
        (char *) NULL,
277
        "ONboard",
278
        "ONboard-MC",
279
        "ONboard-MC",
280
        (char *) NULL,
281
        (char *) NULL,
282
        (char *) NULL,
283
        (char *) NULL,
284
        (char *) NULL,
285
        (char *) NULL,
286
        (char *) NULL,
287
        (char *) NULL,
288
        "EasyIO",
289
        "EC8/32-AT",
290
        "EC8/32-MC",
291
        "EC8/64-AT",
292
        "EC8/64-EI",
293
        "EC8/64-MC",
294
        "EC8/32-PCI",
295
        "EC8/64-PCI",
296
        "EasyIO-PCI",
297
        "EC/RA-PCI",
298
};
299
 
300
/*****************************************************************************/
301
 
302
#ifdef MODULE
303
/*
304
 *      Define some string labels for arguments passed from the module
305
 *      load line. These allow for easy board definitions, and easy
306
 *      modification of the io, memory and irq resoucres.
307
 */
308
 
309
static char     *board0[8];
310
static char     *board1[8];
311
static char     *board2[8];
312
static char     *board3[8];
313
 
314
static char     **stli_brdsp[] = {
315
        (char **) &board0,
316
        (char **) &board1,
317
        (char **) &board2,
318
        (char **) &board3
319
};
320
 
321
/*
322
 *      Define a set of common board names, and types. This is used to
323
 *      parse any module arguments.
324
 */
325
 
326
typedef struct stlibrdtype {
327
        char    *name;
328
        int     type;
329
} stlibrdtype_t;
330
 
331
static stlibrdtype_t    stli_brdstr[] = {
332
        { "stallion", BRD_STALLION },
333
        { "1", BRD_STALLION },
334
        { "brumby", BRD_BRUMBY },
335
        { "brumby4", BRD_BRUMBY },
336
        { "brumby/4", BRD_BRUMBY },
337
        { "brumby-4", BRD_BRUMBY },
338
        { "brumby8", BRD_BRUMBY },
339
        { "brumby/8", BRD_BRUMBY },
340
        { "brumby-8", BRD_BRUMBY },
341
        { "brumby16", BRD_BRUMBY },
342
        { "brumby/16", BRD_BRUMBY },
343
        { "brumby-16", BRD_BRUMBY },
344
        { "2", BRD_BRUMBY },
345
        { "onboard2", BRD_ONBOARD2 },
346
        { "onboard-2", BRD_ONBOARD2 },
347
        { "onboard/2", BRD_ONBOARD2 },
348
        { "onboard-mc", BRD_ONBOARD2 },
349
        { "onboard/mc", BRD_ONBOARD2 },
350
        { "onboard-mca", BRD_ONBOARD2 },
351
        { "onboard/mca", BRD_ONBOARD2 },
352
        { "3", BRD_ONBOARD2 },
353
        { "onboard", BRD_ONBOARD },
354
        { "onboardat", BRD_ONBOARD },
355
        { "4", BRD_ONBOARD },
356
        { "onboarde", BRD_ONBOARDE },
357
        { "onboard-e", BRD_ONBOARDE },
358
        { "onboard/e", BRD_ONBOARDE },
359
        { "onboard-ei", BRD_ONBOARDE },
360
        { "onboard/ei", BRD_ONBOARDE },
361
        { "7", BRD_ONBOARDE },
362
        { "ecp", BRD_ECP },
363
        { "ecpat", BRD_ECP },
364
        { "ec8/64", BRD_ECP },
365
        { "ec8/64-at", BRD_ECP },
366
        { "ec8/64-isa", BRD_ECP },
367
        { "23", BRD_ECP },
368
        { "ecpe", BRD_ECPE },
369
        { "ecpei", BRD_ECPE },
370
        { "ec8/64-e", BRD_ECPE },
371
        { "ec8/64-ei", BRD_ECPE },
372
        { "24", BRD_ECPE },
373
        { "ecpmc", BRD_ECPMC },
374
        { "ec8/64-mc", BRD_ECPMC },
375
        { "ec8/64-mca", BRD_ECPMC },
376
        { "25", BRD_ECPMC },
377
        { "ecppci", BRD_ECPPCI },
378
        { "ec/ra", BRD_ECPPCI },
379
        { "ec/ra-pc", BRD_ECPPCI },
380
        { "ec/ra-pci", BRD_ECPPCI },
381
        { "29", BRD_ECPPCI },
382
};
383
 
384
/*
385
 *      Define the module agruments.
386
 */
387
MODULE_AUTHOR("Greg Ungerer");
388
MODULE_DESCRIPTION("Stallion Intelligent Multiport Serial Driver");
389
MODULE_LICENSE("GPL");
390
 
391
 
392
MODULE_PARM(board0, "1-3s");
393
MODULE_PARM_DESC(board0, "Board 0 config -> name[,ioaddr[,memaddr]");
394
MODULE_PARM(board1, "1-3s");
395
MODULE_PARM_DESC(board1, "Board 1 config -> name[,ioaddr[,memaddr]");
396
MODULE_PARM(board2, "1-3s");
397
MODULE_PARM_DESC(board2, "Board 2 config -> name[,ioaddr[,memaddr]");
398
MODULE_PARM(board3, "1-3s");
399
MODULE_PARM_DESC(board3, "Board 3 config -> name[,ioaddr[,memaddr]");
400
 
401
#endif
402
 
403
/*
404
 *      Set up a default memory address table for EISA board probing.
405
 *      The default addresses are all bellow 1Mbyte, which has to be the
406
 *      case anyway. They should be safe, since we only read values from
407
 *      them, and interrupts are disabled while we do it. If the higher
408
 *      memory support is compiled in then we also try probing around
409
 *      the 1Gb, 2Gb and 3Gb areas as well...
410
 */
411
static unsigned long    stli_eisamemprobeaddrs[] = {
412
        0xc0000,    0xd0000,    0xe0000,    0xf0000,
413
        0x80000000, 0x80010000, 0x80020000, 0x80030000,
414
        0x40000000, 0x40010000, 0x40020000, 0x40030000,
415
        0xc0000000, 0xc0010000, 0xc0020000, 0xc0030000,
416
        0xff000000, 0xff010000, 0xff020000, 0xff030000,
417
};
418
 
419
static int      stli_eisamempsize = sizeof(stli_eisamemprobeaddrs) / sizeof(unsigned long);
420
int             stli_eisaprobe = STLI_EISAPROBE;
421
 
422
/*
423
 *      Define the Stallion PCI vendor and device IDs.
424
 */
425
#ifdef CONFIG_PCI
426
#ifndef PCI_VENDOR_ID_STALLION
427
#define PCI_VENDOR_ID_STALLION          0x124d
428
#endif
429
#ifndef PCI_DEVICE_ID_ECRA
430
#define PCI_DEVICE_ID_ECRA              0x0004
431
#endif
432
#endif
433
 
434
static struct pci_device_id istallion_pci_tbl[] = {
435
        { PCI_VENDOR_ID_STALLION, PCI_DEVICE_ID_ECRA, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
436
        { 0 }
437
};
438
MODULE_DEVICE_TABLE(pci, istallion_pci_tbl);
439
 
440
/*****************************************************************************/
441
 
442
/*
443
 *      Hardware configuration info for ECP boards. These defines apply
444
 *      to the directly accessible io ports of the ECP. There is a set of
445
 *      defines for each ECP board type, ISA, EISA, MCA and PCI.
446
 */
447
#define ECP_IOSIZE      4
448
 
449
#define ECP_MEMSIZE     (128 * 1024)
450
#define ECP_PCIMEMSIZE  (256 * 1024)
451
 
452
#define ECP_ATPAGESIZE  (4 * 1024)
453
#define ECP_MCPAGESIZE  (4 * 1024)
454
#define ECP_EIPAGESIZE  (64 * 1024)
455
#define ECP_PCIPAGESIZE (64 * 1024)
456
 
457
#define STL_EISAID      0x8c4e
458
 
459
/*
460
 *      Important defines for the ISA class of ECP board.
461
 */
462
#define ECP_ATIREG      0
463
#define ECP_ATCONFR     1
464
#define ECP_ATMEMAR     2
465
#define ECP_ATMEMPR     3
466
#define ECP_ATSTOP      0x1
467
#define ECP_ATINTENAB   0x10
468
#define ECP_ATENABLE    0x20
469
#define ECP_ATDISABLE   0x00
470
#define ECP_ATADDRMASK  0x3f000
471
#define ECP_ATADDRSHFT  12
472
 
473
/*
474
 *      Important defines for the EISA class of ECP board.
475
 */
476
#define ECP_EIIREG      0
477
#define ECP_EIMEMARL    1
478
#define ECP_EICONFR     2
479
#define ECP_EIMEMARH    3
480
#define ECP_EIENABLE    0x1
481
#define ECP_EIDISABLE   0x0
482
#define ECP_EISTOP      0x4
483
#define ECP_EIEDGE      0x00
484
#define ECP_EILEVEL     0x80
485
#define ECP_EIADDRMASKL 0x00ff0000
486
#define ECP_EIADDRSHFTL 16
487
#define ECP_EIADDRMASKH 0xff000000
488
#define ECP_EIADDRSHFTH 24
489
#define ECP_EIBRDENAB   0xc84
490
 
491
#define ECP_EISAID      0x4
492
 
493
/*
494
 *      Important defines for the Micro-channel class of ECP board.
495
 *      (It has a lot in common with the ISA boards.)
496
 */
497
#define ECP_MCIREG      0
498
#define ECP_MCCONFR     1
499
#define ECP_MCSTOP      0x20
500
#define ECP_MCENABLE    0x80
501
#define ECP_MCDISABLE   0x00
502
 
503
/*
504
 *      Important defines for the PCI class of ECP board.
505
 *      (It has a lot in common with the other ECP boards.)
506
 */
507
#define ECP_PCIIREG     0
508
#define ECP_PCICONFR    1
509
#define ECP_PCISTOP     0x01
510
 
511
/*
512
 *      Hardware configuration info for ONboard and Brumby boards. These
513
 *      defines apply to the directly accessible io ports of these boards.
514
 */
515
#define ONB_IOSIZE      16
516
#define ONB_MEMSIZE     (64 * 1024)
517
#define ONB_ATPAGESIZE  (64 * 1024)
518
#define ONB_MCPAGESIZE  (64 * 1024)
519
#define ONB_EIMEMSIZE   (128 * 1024)
520
#define ONB_EIPAGESIZE  (64 * 1024)
521
 
522
/*
523
 *      Important defines for the ISA class of ONboard board.
524
 */
525
#define ONB_ATIREG      0
526
#define ONB_ATMEMAR     1
527
#define ONB_ATCONFR     2
528
#define ONB_ATSTOP      0x4
529
#define ONB_ATENABLE    0x01
530
#define ONB_ATDISABLE   0x00
531
#define ONB_ATADDRMASK  0xff0000
532
#define ONB_ATADDRSHFT  16
533
 
534
#define ONB_MEMENABLO   0
535
#define ONB_MEMENABHI   0x02
536
 
537
/*
538
 *      Important defines for the EISA class of ONboard board.
539
 */
540
#define ONB_EIIREG      0
541
#define ONB_EIMEMARL    1
542
#define ONB_EICONFR     2
543
#define ONB_EIMEMARH    3
544
#define ONB_EIENABLE    0x1
545
#define ONB_EIDISABLE   0x0
546
#define ONB_EISTOP      0x4
547
#define ONB_EIEDGE      0x00
548
#define ONB_EILEVEL     0x80
549
#define ONB_EIADDRMASKL 0x00ff0000
550
#define ONB_EIADDRSHFTL 16
551
#define ONB_EIADDRMASKH 0xff000000
552
#define ONB_EIADDRSHFTH 24
553
#define ONB_EIBRDENAB   0xc84
554
 
555
#define ONB_EISAID      0x1
556
 
557
/*
558
 *      Important defines for the Brumby boards. They are pretty simple,
559
 *      there is not much that is programmably configurable.
560
 */
561
#define BBY_IOSIZE      16
562
#define BBY_MEMSIZE     (64 * 1024)
563
#define BBY_PAGESIZE    (16 * 1024)
564
 
565
#define BBY_ATIREG      0
566
#define BBY_ATCONFR     1
567
#define BBY_ATSTOP      0x4
568
 
569
/*
570
 *      Important defines for the Stallion boards. They are pretty simple,
571
 *      there is not much that is programmably configurable.
572
 */
573
#define STAL_IOSIZE     16
574
#define STAL_MEMSIZE    (64 * 1024)
575
#define STAL_PAGESIZE   (64 * 1024)
576
 
577
/*
578
 *      Define the set of status register values for EasyConnection panels.
579
 *      The signature will return with the status value for each panel. From
580
 *      this we can determine what is attached to the board - before we have
581
 *      actually down loaded any code to it.
582
 */
583
#define ECH_PNLSTATUS   2
584
#define ECH_PNL16PORT   0x20
585
#define ECH_PNLIDMASK   0x07
586
#define ECH_PNLXPID     0x40
587
#define ECH_PNLINTRPEND 0x80
588
 
589
/*
590
 *      Define some macros to do things to the board. Even those these boards
591
 *      are somewhat related there is often significantly different ways of
592
 *      doing some operation on it (like enable, paging, reset, etc). So each
593
 *      board class has a set of functions which do the commonly required
594
 *      operations. The macros below basically just call these functions,
595
 *      generally checking for a NULL function - which means that the board
596
 *      needs nothing done to it to achieve this operation!
597
 */
598
#define EBRDINIT(brdp)                                          \
599
        if (brdp->init != NULL)                                 \
600
                (* brdp->init)(brdp)
601
 
602
#define EBRDENABLE(brdp)                                        \
603
        if (brdp->enable != NULL)                               \
604
                (* brdp->enable)(brdp);
605
 
606
#define EBRDDISABLE(brdp)                                       \
607
        if (brdp->disable != NULL)                              \
608
                (* brdp->disable)(brdp);
609
 
610
#define EBRDINTR(brdp)                                          \
611
        if (brdp->intr != NULL)                                 \
612
                (* brdp->intr)(brdp);
613
 
614
#define EBRDRESET(brdp)                                         \
615
        if (brdp->reset != NULL)                                \
616
                (* brdp->reset)(brdp);
617
 
618
#define EBRDGETMEMPTR(brdp,offset)                              \
619
        (* brdp->getmemptr)(brdp, offset, __LINE__)
620
 
621
/*
622
 *      Define the maximal baud rate, and the default baud base for ports.
623
 */
624
#define STL_MAXBAUD     460800
625
#define STL_BAUDBASE    115200
626
#define STL_CLOSEDELAY  (5 * HZ / 10)
627
 
628
/*****************************************************************************/
629
 
630
/*
631
 *      Define macros to extract a brd or port number from a minor number.
632
 */
633
#define MINOR2BRD(min)          (((min) & 0xc0) >> 6)
634
#define MINOR2PORT(min)         ((min) & 0x3f)
635
 
636
/*
637
 *      Define a baud rate table that converts termios baud rate selector
638
 *      into the actual baud rate value. All baud rate calculations are based
639
 *      on the actual baud rate required.
640
 */
641
static unsigned int     stli_baudrates[] = {
642
        0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
643
        9600, 19200, 38400, 57600, 115200, 230400, 460800, 921600
644
};
645
 
646
/*****************************************************************************/
647
 
648
/*
649
 *      Define some handy local macros...
650
 */
651
#undef MIN
652
#define MIN(a,b)        (((a) <= (b)) ? (a) : (b))
653
 
654
#undef  TOLOWER
655
#define TOLOWER(x)      ((((x) >= 'A') && ((x) <= 'Z')) ? ((x) + 0x20) : (x))
656
 
657
/*****************************************************************************/
658
 
659
/*
660
 *      Prototype all functions in this driver!
661
 */
662
 
663
#ifdef MODULE
664
int             init_module(void);
665
void            cleanup_module(void);
666
static void     stli_argbrds(void);
667
static int      stli_parsebrd(stlconf_t *confp, char **argp);
668
 
669
static unsigned long    stli_atol(char *str);
670
#endif
671
 
672
int             stli_init(void);
673
static int      stli_open(struct tty_struct *tty, struct file *filp);
674
static void     stli_close(struct tty_struct *tty, struct file *filp);
675
static int      stli_write(struct tty_struct *tty, int from_user, const unsigned char *buf, int count);
676
static void     stli_putchar(struct tty_struct *tty, unsigned char ch);
677
static void     stli_flushchars(struct tty_struct *tty);
678
static int      stli_writeroom(struct tty_struct *tty);
679
static int      stli_charsinbuffer(struct tty_struct *tty);
680
static int      stli_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg);
681
static void     stli_settermios(struct tty_struct *tty, struct termios *old);
682
static void     stli_throttle(struct tty_struct *tty);
683
static void     stli_unthrottle(struct tty_struct *tty);
684
static void     stli_stop(struct tty_struct *tty);
685
static void     stli_start(struct tty_struct *tty);
686
static void     stli_flushbuffer(struct tty_struct *tty);
687
static void     stli_breakctl(struct tty_struct *tty, int state);
688
static void     stli_waituntilsent(struct tty_struct *tty, int timeout);
689
static void     stli_sendxchar(struct tty_struct *tty, char ch);
690
static void     stli_hangup(struct tty_struct *tty);
691
static int      stli_portinfo(stlibrd_t *brdp, stliport_t *portp, int portnr, char *pos);
692
 
693
static int      stli_brdinit(stlibrd_t *brdp);
694
static int      stli_startbrd(stlibrd_t *brdp);
695
static ssize_t  stli_memread(struct file *fp, char *buf, size_t count, loff_t *offp);
696
static ssize_t  stli_memwrite(struct file *fp, const char *buf, size_t count, loff_t *offp);
697
static int      stli_memioctl(struct inode *ip, struct file *fp, unsigned int cmd, unsigned long arg);
698
static void     stli_brdpoll(stlibrd_t *brdp, volatile cdkhdr_t *hdrp);
699
static void     stli_poll(unsigned long arg);
700
static int      stli_hostcmd(stlibrd_t *brdp, stliport_t *portp);
701
static int      stli_initopen(stlibrd_t *brdp, stliport_t *portp);
702
static int      stli_rawopen(stlibrd_t *brdp, stliport_t *portp, unsigned long arg, int wait);
703
static int      stli_rawclose(stlibrd_t *brdp, stliport_t *portp, unsigned long arg, int wait);
704
static int      stli_waitcarrier(stlibrd_t *brdp, stliport_t *portp, struct file *filp);
705
static void     stli_dohangup(void *arg);
706
static void     stli_delay(int len);
707
static int      stli_setport(stliport_t *portp);
708
static int      stli_cmdwait(stlibrd_t *brdp, stliport_t *portp, unsigned long cmd, void *arg, int size, int copyback);
709
static void     stli_sendcmd(stlibrd_t *brdp, stliport_t *portp, unsigned long cmd, void *arg, int size, int copyback);
710
static void     stli_dodelaycmd(stliport_t *portp, volatile cdkctrl_t *cp);
711
static void     stli_mkasyport(stliport_t *portp, asyport_t *pp, struct termios *tiosp);
712
static void     stli_mkasysigs(asysigs_t *sp, int dtr, int rts);
713
static long     stli_mktiocm(unsigned long sigvalue);
714
static void     stli_read(stlibrd_t *brdp, stliport_t *portp);
715
static void     stli_getserial(stliport_t *portp, struct serial_struct *sp);
716
static int      stli_setserial(stliport_t *portp, struct serial_struct *sp);
717
static int      stli_getbrdstats(combrd_t *bp);
718
static int      stli_getportstats(stliport_t *portp, comstats_t *cp);
719
static int      stli_portcmdstats(stliport_t *portp);
720
static int      stli_clrportstats(stliport_t *portp, comstats_t *cp);
721
static int      stli_getportstruct(unsigned long arg);
722
static int      stli_getbrdstruct(unsigned long arg);
723
static void     *stli_memalloc(int len);
724
static stlibrd_t *stli_allocbrd(void);
725
 
726
static void     stli_ecpinit(stlibrd_t *brdp);
727
static void     stli_ecpenable(stlibrd_t *brdp);
728
static void     stli_ecpdisable(stlibrd_t *brdp);
729
static char     *stli_ecpgetmemptr(stlibrd_t *brdp, unsigned long offset, int line);
730
static void     stli_ecpreset(stlibrd_t *brdp);
731
static void     stli_ecpintr(stlibrd_t *brdp);
732
static void     stli_ecpeiinit(stlibrd_t *brdp);
733
static void     stli_ecpeienable(stlibrd_t *brdp);
734
static void     stli_ecpeidisable(stlibrd_t *brdp);
735
static char     *stli_ecpeigetmemptr(stlibrd_t *brdp, unsigned long offset, int line);
736
static void     stli_ecpeireset(stlibrd_t *brdp);
737
static void     stli_ecpmcenable(stlibrd_t *brdp);
738
static void     stli_ecpmcdisable(stlibrd_t *brdp);
739
static char     *stli_ecpmcgetmemptr(stlibrd_t *brdp, unsigned long offset, int line);
740
static void     stli_ecpmcreset(stlibrd_t *brdp);
741
static void     stli_ecppciinit(stlibrd_t *brdp);
742
static char     *stli_ecppcigetmemptr(stlibrd_t *brdp, unsigned long offset, int line);
743
static void     stli_ecppcireset(stlibrd_t *brdp);
744
 
745
static void     stli_onbinit(stlibrd_t *brdp);
746
static void     stli_onbenable(stlibrd_t *brdp);
747
static void     stli_onbdisable(stlibrd_t *brdp);
748
static char     *stli_onbgetmemptr(stlibrd_t *brdp, unsigned long offset, int line);
749
static void     stli_onbreset(stlibrd_t *brdp);
750
static void     stli_onbeinit(stlibrd_t *brdp);
751
static void     stli_onbeenable(stlibrd_t *brdp);
752
static void     stli_onbedisable(stlibrd_t *brdp);
753
static char     *stli_onbegetmemptr(stlibrd_t *brdp, unsigned long offset, int line);
754
static void     stli_onbereset(stlibrd_t *brdp);
755
static void     stli_bbyinit(stlibrd_t *brdp);
756
static char     *stli_bbygetmemptr(stlibrd_t *brdp, unsigned long offset, int line);
757
static void     stli_bbyreset(stlibrd_t *brdp);
758
static void     stli_stalinit(stlibrd_t *brdp);
759
static char     *stli_stalgetmemptr(stlibrd_t *brdp, unsigned long offset, int line);
760
static void     stli_stalreset(stlibrd_t *brdp);
761
 
762
static stliport_t *stli_getport(int brdnr, int panelnr, int portnr);
763
 
764
static inline int       stli_initbrds(void);
765
static inline int       stli_initecp(stlibrd_t *brdp);
766
static inline int       stli_initonb(stlibrd_t *brdp);
767
static inline int       stli_findeisabrds(void);
768
static inline int       stli_eisamemprobe(stlibrd_t *brdp);
769
static inline int       stli_initports(stlibrd_t *brdp);
770
static inline int       stli_getbrdnr(void);
771
 
772
#ifdef  CONFIG_PCI
773
static inline int       stli_findpcibrds(void);
774
static inline int       stli_initpcibrd(int brdtype, struct pci_dev *devp);
775
#endif
776
 
777
/*****************************************************************************/
778
 
779
/*
780
 *      Define the driver info for a user level shared memory device. This
781
 *      device will work sort of like the /dev/kmem device - except that it
782
 *      will give access to the shared memory on the Stallion intelligent
783
 *      board. This is also a very useful debugging tool.
784
 */
785
static struct file_operations   stli_fsiomem = {
786
        owner:          THIS_MODULE,
787
        read:           stli_memread,
788
        write:          stli_memwrite,
789
        ioctl:          stli_memioctl,
790
};
791
 
792
/*****************************************************************************/
793
 
794
/*
795
 *      Define a timer_list entry for our poll routine. The slave board
796
 *      is polled every so often to see if anything needs doing. This is
797
 *      much cheaper on host cpu than using interrupts. It turns out to
798
 *      not increase character latency by much either...
799
 */
800
static struct timer_list        stli_timerlist = {
801
        function: stli_poll
802
};
803
 
804
static int      stli_timeron;
805
 
806
/*
807
 *      Define the calculation for the timeout routine.
808
 */
809
#define STLI_TIMEOUT    (jiffies + 1)
810
 
811
/*****************************************************************************/
812
 
813
#ifdef MODULE
814
 
815
/*
816
 *      Loadable module initialization stuff.
817
 */
818
 
819
int init_module()
820
{
821
        unsigned long   flags;
822
 
823
#if DEBUG
824
        printk("init_module()\n");
825
#endif
826
 
827
        save_flags(flags);
828
        cli();
829
        stli_init();
830
        restore_flags(flags);
831
 
832
        return(0);
833
}
834
 
835
/*****************************************************************************/
836
 
837
void cleanup_module()
838
{
839
        stlibrd_t       *brdp;
840
        stliport_t      *portp;
841
        unsigned long   flags;
842
        int             i, j;
843
 
844
#if DEBUG
845
        printk("cleanup_module()\n");
846
#endif
847
 
848
        printk(KERN_INFO "Unloading %s: version %s\n", stli_drvtitle,
849
                stli_drvversion);
850
 
851
        save_flags(flags);
852
        cli();
853
 
854
/*
855
 *      Free up all allocated resources used by the ports. This includes
856
 *      memory and interrupts.
857
 */
858
        if (stli_timeron) {
859
                stli_timeron = 0;
860
                del_timer(&stli_timerlist);
861
        }
862
 
863
        i = tty_unregister_driver(&stli_serial);
864
        j = tty_unregister_driver(&stli_callout);
865
        if (i || j) {
866
                printk("STALLION: failed to un-register tty driver, "
867
                        "errno=%d,%d\n", -i, -j);
868
                restore_flags(flags);
869
                return;
870
        }
871
        devfs_unregister (devfs_handle);
872
        if ((i = devfs_unregister_chrdev(STL_SIOMEMMAJOR, "staliomem")))
873
                printk("STALLION: failed to un-register serial memory device, "
874
                        "errno=%d\n", -i);
875
        if (stli_tmpwritebuf != (char *) NULL)
876
                kfree(stli_tmpwritebuf);
877
        if (stli_txcookbuf != (char *) NULL)
878
                kfree(stli_txcookbuf);
879
 
880
        for (i = 0; (i < stli_nrbrds); i++) {
881
                if ((brdp = stli_brds[i]) == (stlibrd_t *) NULL)
882
                        continue;
883
                for (j = 0; (j < STL_MAXPORTS); j++) {
884
                        portp = brdp->ports[j];
885
                        if (portp != (stliport_t *) NULL) {
886
                                if (portp->tty != (struct tty_struct *) NULL)
887
                                        tty_hangup(portp->tty);
888
                                kfree(portp);
889
                        }
890
                }
891
 
892
                iounmap(brdp->membase);
893
                if (brdp->iosize > 0)
894
                        release_region(brdp->iobase, brdp->iosize);
895
                kfree(brdp);
896
                stli_brds[i] = (stlibrd_t *) NULL;
897
        }
898
 
899
        restore_flags(flags);
900
}
901
 
902
/*****************************************************************************/
903
 
904
/*
905
 *      Check for any arguments passed in on the module load command line.
906
 */
907
 
908
static void stli_argbrds()
909
{
910
        stlconf_t       conf;
911
        stlibrd_t       *brdp;
912
        int             nrargs, i;
913
 
914
#if DEBUG
915
        printk("stli_argbrds()\n");
916
#endif
917
 
918
        nrargs = sizeof(stli_brdsp) / sizeof(char **);
919
 
920
        for (i = stli_nrbrds; (i < nrargs); i++) {
921
                memset(&conf, 0, sizeof(conf));
922
                if (stli_parsebrd(&conf, stli_brdsp[i]) == 0)
923
                        continue;
924
                if ((brdp = stli_allocbrd()) == (stlibrd_t *) NULL)
925
                        continue;
926
                stli_nrbrds = i + 1;
927
                brdp->brdnr = i;
928
                brdp->brdtype = conf.brdtype;
929
                brdp->iobase = conf.ioaddr1;
930
                brdp->memaddr = conf.memaddr;
931
                stli_brdinit(brdp);
932
        }
933
}
934
 
935
/*****************************************************************************/
936
 
937
/*
938
 *      Convert an ascii string number into an unsigned long.
939
 */
940
 
941
static unsigned long stli_atol(char *str)
942
{
943
        unsigned long   val;
944
        int             base, c;
945
        char            *sp;
946
 
947
        val = 0;
948
        sp = str;
949
        if ((*sp == '0') && (*(sp+1) == 'x')) {
950
                base = 16;
951
                sp += 2;
952
        } else if (*sp == '0') {
953
                base = 8;
954
                sp++;
955
        } else {
956
                base = 10;
957
        }
958
 
959
        for (; (*sp != 0); sp++) {
960
                c = (*sp > '9') ? (TOLOWER(*sp) - 'a' + 10) : (*sp - '0');
961
                if ((c < 0) || (c >= base)) {
962
                        printk("STALLION: invalid argument %s\n", str);
963
                        val = 0;
964
                        break;
965
                }
966
                val = (val * base) + c;
967
        }
968
        return(val);
969
}
970
 
971
/*****************************************************************************/
972
 
973
/*
974
 *      Parse the supplied argument string, into the board conf struct.
975
 */
976
 
977
static int stli_parsebrd(stlconf_t *confp, char **argp)
978
{
979
        char    *sp;
980
        int     nrbrdnames, i;
981
 
982
#if DEBUG
983
        printk("stli_parsebrd(confp=%x,argp=%x)\n", (int) confp, (int) argp);
984
#endif
985
 
986
        if ((argp[0] == (char *) NULL) || (*argp[0] == 0))
987
                return(0);
988
 
989
        for (sp = argp[0], i = 0; ((*sp != 0) && (i < 25)); sp++, i++)
990
                *sp = TOLOWER(*sp);
991
 
992
        nrbrdnames = sizeof(stli_brdstr) / sizeof(stlibrdtype_t);
993
        for (i = 0; (i < nrbrdnames); i++) {
994
                if (strcmp(stli_brdstr[i].name, argp[0]) == 0)
995
                        break;
996
        }
997
        if (i >= nrbrdnames) {
998
                printk("STALLION: unknown board name, %s?\n", argp[0]);
999
                return(0);
1000
        }
1001
 
1002
        confp->brdtype = stli_brdstr[i].type;
1003
        if ((argp[1] != (char *) NULL) && (*argp[1] != 0))
1004
                confp->ioaddr1 = stli_atol(argp[1]);
1005
        if ((argp[2] != (char *) NULL) && (*argp[2] != 0))
1006
                confp->memaddr = stli_atol(argp[2]);
1007
        return(1);
1008
}
1009
 
1010
#endif
1011
 
1012
/*****************************************************************************/
1013
 
1014
/*
1015
 *      Local driver kernel malloc routine.
1016
 */
1017
 
1018
static void *stli_memalloc(int len)
1019
{
1020
        return((void *) kmalloc(len, GFP_KERNEL));
1021
}
1022
 
1023
/*****************************************************************************/
1024
 
1025
static int stli_open(struct tty_struct *tty, struct file *filp)
1026
{
1027
        stlibrd_t       *brdp;
1028
        stliport_t      *portp;
1029
        unsigned int    minordev;
1030
        int             brdnr, portnr, rc;
1031
 
1032
#if DEBUG
1033
        printk("stli_open(tty=%x,filp=%x): device=%x\n", (int) tty,
1034
                (int) filp, tty->device);
1035
#endif
1036
 
1037
        minordev = MINOR(tty->device);
1038
        brdnr = MINOR2BRD(minordev);
1039
        if (brdnr >= stli_nrbrds)
1040
                return(-ENODEV);
1041
        brdp = stli_brds[brdnr];
1042
        if (brdp == (stlibrd_t *) NULL)
1043
                return(-ENODEV);
1044
        if ((brdp->state & BST_STARTED) == 0)
1045
                return(-ENODEV);
1046
        portnr = MINOR2PORT(minordev);
1047
        if ((portnr < 0) || (portnr > brdp->nrports))
1048
                return(-ENODEV);
1049
 
1050
        portp = brdp->ports[portnr];
1051
        if (portp == (stliport_t *) NULL)
1052
                return(-ENODEV);
1053
        if (portp->devnr < 1)
1054
                return(-ENODEV);
1055
 
1056
        MOD_INC_USE_COUNT;
1057
 
1058
/*
1059
 *      Check if this port is in the middle of closing. If so then wait
1060
 *      until it is closed then return error status based on flag settings.
1061
 *      The sleep here does not need interrupt protection since the wakeup
1062
 *      for it is done with the same context.
1063
 */
1064
        if (portp->flags & ASYNC_CLOSING) {
1065
                interruptible_sleep_on(&portp->close_wait);
1066
                if (portp->flags & ASYNC_HUP_NOTIFY)
1067
                        return(-EAGAIN);
1068
                return(-ERESTARTSYS);
1069
        }
1070
 
1071
/*
1072
 *      On the first open of the device setup the port hardware, and
1073
 *      initialize the per port data structure. Since initializing the port
1074
 *      requires several commands to the board we will need to wait for any
1075
 *      other open that is already initializing the port.
1076
 */
1077
        portp->tty = tty;
1078
        tty->driver_data = portp;
1079
        portp->refcount++;
1080
 
1081
        while (test_bit(ST_INITIALIZING, &portp->state)) {
1082
                if (signal_pending(current))
1083
                        return(-ERESTARTSYS);
1084
                interruptible_sleep_on(&portp->raw_wait);
1085
        }
1086
 
1087
        if ((portp->flags & ASYNC_INITIALIZED) == 0) {
1088
                set_bit(ST_INITIALIZING, &portp->state);
1089
                if ((rc = stli_initopen(brdp, portp)) >= 0) {
1090
                        portp->flags |= ASYNC_INITIALIZED;
1091
                        clear_bit(TTY_IO_ERROR, &tty->flags);
1092
                }
1093
                clear_bit(ST_INITIALIZING, &portp->state);
1094
                wake_up_interruptible(&portp->raw_wait);
1095
                if (rc < 0)
1096
                        return(rc);
1097
        }
1098
 
1099
/*
1100
 *      Check if this port is in the middle of closing. If so then wait
1101
 *      until it is closed then return error status, based on flag settings.
1102
 *      The sleep here does not need interrupt protection since the wakeup
1103
 *      for it is done with the same context.
1104
 */
1105
        if (portp->flags & ASYNC_CLOSING) {
1106
                interruptible_sleep_on(&portp->close_wait);
1107
                if (portp->flags & ASYNC_HUP_NOTIFY)
1108
                        return(-EAGAIN);
1109
                return(-ERESTARTSYS);
1110
        }
1111
 
1112
/*
1113
 *      Based on type of open being done check if it can overlap with any
1114
 *      previous opens still in effect. If we are a normal serial device
1115
 *      then also we might have to wait for carrier.
1116
 */
1117
        if (tty->driver.subtype == STL_DRVTYPCALLOUT) {
1118
                if (portp->flags & ASYNC_NORMAL_ACTIVE)
1119
                        return(-EBUSY);
1120
                if (portp->flags & ASYNC_CALLOUT_ACTIVE) {
1121
                        if ((portp->flags & ASYNC_SESSION_LOCKOUT) &&
1122
                            (portp->session != current->session))
1123
                                return(-EBUSY);
1124
                        if ((portp->flags & ASYNC_PGRP_LOCKOUT) &&
1125
                            (portp->pgrp != current->pgrp))
1126
                                return(-EBUSY);
1127
                }
1128
                portp->flags |= ASYNC_CALLOUT_ACTIVE;
1129
        } else {
1130
                if (filp->f_flags & O_NONBLOCK) {
1131
                        if (portp->flags & ASYNC_CALLOUT_ACTIVE)
1132
                                return(-EBUSY);
1133
                } else {
1134
                        if ((rc = stli_waitcarrier(brdp, portp, filp)) != 0)
1135
                                return(rc);
1136
                }
1137
                portp->flags |= ASYNC_NORMAL_ACTIVE;
1138
        }
1139
 
1140
        if ((portp->refcount == 1) && (portp->flags & ASYNC_SPLIT_TERMIOS)) {
1141
                if (tty->driver.subtype == STL_DRVTYPSERIAL)
1142
                        *tty->termios = portp->normaltermios;
1143
                else
1144
                        *tty->termios = portp->callouttermios;
1145
                stli_setport(portp);
1146
        }
1147
 
1148
        portp->session = current->session;
1149
        portp->pgrp = current->pgrp;
1150
        return(0);
1151
}
1152
 
1153
/*****************************************************************************/
1154
 
1155
static void stli_close(struct tty_struct *tty, struct file *filp)
1156
{
1157
        stlibrd_t       *brdp;
1158
        stliport_t      *portp;
1159
        unsigned long   flags;
1160
 
1161
#if DEBUG
1162
        printk("stli_close(tty=%x,filp=%x)\n", (int) tty, (int) filp);
1163
#endif
1164
 
1165
        portp = tty->driver_data;
1166
        if (portp == (stliport_t *) NULL)
1167
                return;
1168
 
1169
        save_flags(flags);
1170
        cli();
1171
        if (tty_hung_up_p(filp)) {
1172
                MOD_DEC_USE_COUNT;
1173
                restore_flags(flags);
1174
                return;
1175
        }
1176
        if ((tty->count == 1) && (portp->refcount != 1))
1177
                portp->refcount = 1;
1178
        if (portp->refcount-- > 1) {
1179
                MOD_DEC_USE_COUNT;
1180
                restore_flags(flags);
1181
                return;
1182
        }
1183
 
1184
        portp->flags |= ASYNC_CLOSING;
1185
 
1186
        if (portp->flags & ASYNC_NORMAL_ACTIVE)
1187
                portp->normaltermios = *tty->termios;
1188
        if (portp->flags & ASYNC_CALLOUT_ACTIVE)
1189
                portp->callouttermios = *tty->termios;
1190
 
1191
/*
1192
 *      May want to wait for data to drain before closing. The BUSY flag
1193
 *      keeps track of whether we are still transmitting or not. It is
1194
 *      updated by messages from the slave - indicating when all chars
1195
 *      really have drained.
1196
 */
1197
        if (tty == stli_txcooktty)
1198
                stli_flushchars(tty);
1199
        tty->closing = 1;
1200
        if (portp->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1201
                tty_wait_until_sent(tty, portp->closing_wait);
1202
 
1203
        portp->flags &= ~ASYNC_INITIALIZED;
1204
        brdp = stli_brds[portp->brdnr];
1205
        stli_rawclose(brdp, portp, 0, 0);
1206
        if (tty->termios->c_cflag & HUPCL) {
1207
                stli_mkasysigs(&portp->asig, 0, 0);
1208
                if (test_bit(ST_CMDING, &portp->state))
1209
                        set_bit(ST_DOSIGS, &portp->state);
1210
                else
1211
                        stli_sendcmd(brdp, portp, A_SETSIGNALS, &portp->asig,
1212
                                sizeof(asysigs_t), 0);
1213
        }
1214
        clear_bit(ST_TXBUSY, &portp->state);
1215
        clear_bit(ST_RXSTOP, &portp->state);
1216
        set_bit(TTY_IO_ERROR, &tty->flags);
1217
        if (tty->ldisc.flush_buffer)
1218
                (tty->ldisc.flush_buffer)(tty);
1219
        set_bit(ST_DOFLUSHRX, &portp->state);
1220
        stli_flushbuffer(tty);
1221
 
1222
        tty->closing = 0;
1223
        portp->tty = (struct tty_struct *) NULL;
1224
 
1225
        if (portp->openwaitcnt) {
1226
                if (portp->close_delay)
1227
                        stli_delay(portp->close_delay);
1228
                wake_up_interruptible(&portp->open_wait);
1229
        }
1230
 
1231
        portp->flags &= ~(ASYNC_CALLOUT_ACTIVE | ASYNC_NORMAL_ACTIVE |
1232
                ASYNC_CLOSING);
1233
        wake_up_interruptible(&portp->close_wait);
1234
        MOD_DEC_USE_COUNT;
1235
        restore_flags(flags);
1236
}
1237
 
1238
/*****************************************************************************/
1239
 
1240
/*
1241
 *      Carry out first open operations on a port. This involves a number of
1242
 *      commands to be sent to the slave. We need to open the port, set the
1243
 *      notification events, set the initial port settings, get and set the
1244
 *      initial signal values. We sleep and wait in between each one. But
1245
 *      this still all happens pretty quickly.
1246
 */
1247
 
1248
static int stli_initopen(stlibrd_t *brdp, stliport_t *portp)
1249
{
1250
        struct tty_struct       *tty;
1251
        asynotify_t             nt;
1252
        asyport_t               aport;
1253
        int                     rc;
1254
 
1255
#if DEBUG
1256
        printk("stli_initopen(brdp=%x,portp=%x)\n", (int) brdp, (int) portp);
1257
#endif
1258
 
1259
        if ((rc = stli_rawopen(brdp, portp, 0, 1)) < 0)
1260
                return(rc);
1261
 
1262
        memset(&nt, 0, sizeof(asynotify_t));
1263
        nt.data = (DT_TXLOW | DT_TXEMPTY | DT_RXBUSY | DT_RXBREAK);
1264
        nt.signal = SG_DCD;
1265
        if ((rc = stli_cmdwait(brdp, portp, A_SETNOTIFY, &nt,
1266
            sizeof(asynotify_t), 0)) < 0)
1267
                return(rc);
1268
 
1269
        tty = portp->tty;
1270
        if (tty == (struct tty_struct *) NULL)
1271
                return(-ENODEV);
1272
        stli_mkasyport(portp, &aport, tty->termios);
1273
        if ((rc = stli_cmdwait(brdp, portp, A_SETPORT, &aport,
1274
            sizeof(asyport_t), 0)) < 0)
1275
                return(rc);
1276
 
1277
        set_bit(ST_GETSIGS, &portp->state);
1278
        if ((rc = stli_cmdwait(brdp, portp, A_GETSIGNALS, &portp->asig,
1279
            sizeof(asysigs_t), 1)) < 0)
1280
                return(rc);
1281
        if (test_and_clear_bit(ST_GETSIGS, &portp->state))
1282
                portp->sigs = stli_mktiocm(portp->asig.sigvalue);
1283
        stli_mkasysigs(&portp->asig, 1, 1);
1284
        if ((rc = stli_cmdwait(brdp, portp, A_SETSIGNALS, &portp->asig,
1285
            sizeof(asysigs_t), 0)) < 0)
1286
                return(rc);
1287
 
1288
        return(0);
1289
}
1290
 
1291
/*****************************************************************************/
1292
 
1293
/*
1294
 *      Send an open message to the slave. This will sleep waiting for the
1295
 *      acknowledgement, so must have user context. We need to co-ordinate
1296
 *      with close events here, since we don't want open and close events
1297
 *      to overlap.
1298
 */
1299
 
1300
static int stli_rawopen(stlibrd_t *brdp, stliport_t *portp, unsigned long arg, int wait)
1301
{
1302
        volatile cdkhdr_t       *hdrp;
1303
        volatile cdkctrl_t      *cp;
1304
        volatile unsigned char  *bits;
1305
        unsigned long           flags;
1306
        int                     rc;
1307
 
1308
#if DEBUG
1309
        printk("stli_rawopen(brdp=%x,portp=%x,arg=%x,wait=%d)\n",
1310
                (int) brdp, (int) portp, (int) arg, wait);
1311
#endif
1312
 
1313
/*
1314
 *      Send a message to the slave to open this port.
1315
 */
1316
        save_flags(flags);
1317
        cli();
1318
 
1319
/*
1320
 *      Slave is already closing this port. This can happen if a hangup
1321
 *      occurs on this port. So we must wait until it is complete. The
1322
 *      order of opens and closes may not be preserved across shared
1323
 *      memory, so we must wait until it is complete.
1324
 */
1325
        while (test_bit(ST_CLOSING, &portp->state)) {
1326
                if (signal_pending(current)) {
1327
                        restore_flags(flags);
1328
                        return(-ERESTARTSYS);
1329
                }
1330
                interruptible_sleep_on(&portp->raw_wait);
1331
        }
1332
 
1333
/*
1334
 *      Everything is ready now, so write the open message into shared
1335
 *      memory. Once the message is in set the service bits to say that
1336
 *      this port wants service.
1337
 */
1338
        EBRDENABLE(brdp);
1339
        cp = &((volatile cdkasy_t *) EBRDGETMEMPTR(brdp, portp->addr))->ctrl;
1340
        cp->openarg = arg;
1341
        cp->open = 1;
1342
        hdrp = (volatile cdkhdr_t *) EBRDGETMEMPTR(brdp, CDK_CDKADDR);
1343
        bits = ((volatile unsigned char *) hdrp) + brdp->slaveoffset +
1344
                portp->portidx;
1345
        *bits |= portp->portbit;
1346
        EBRDDISABLE(brdp);
1347
 
1348
        if (wait == 0) {
1349
                restore_flags(flags);
1350
                return(0);
1351
        }
1352
 
1353
/*
1354
 *      Slave is in action, so now we must wait for the open acknowledgment
1355
 *      to come back.
1356
 */
1357
        rc = 0;
1358
        set_bit(ST_OPENING, &portp->state);
1359
        while (test_bit(ST_OPENING, &portp->state)) {
1360
                if (signal_pending(current)) {
1361
                        rc = -ERESTARTSYS;
1362
                        break;
1363
                }
1364
                interruptible_sleep_on(&portp->raw_wait);
1365
        }
1366
        restore_flags(flags);
1367
 
1368
        if ((rc == 0) && (portp->rc != 0))
1369
                rc = -EIO;
1370
        return(rc);
1371
}
1372
 
1373
/*****************************************************************************/
1374
 
1375
/*
1376
 *      Send a close message to the slave. Normally this will sleep waiting
1377
 *      for the acknowledgement, but if wait parameter is 0 it will not. If
1378
 *      wait is true then must have user context (to sleep).
1379
 */
1380
 
1381
static int stli_rawclose(stlibrd_t *brdp, stliport_t *portp, unsigned long arg, int wait)
1382
{
1383
        volatile cdkhdr_t       *hdrp;
1384
        volatile cdkctrl_t      *cp;
1385
        volatile unsigned char  *bits;
1386
        unsigned long           flags;
1387
        int                     rc;
1388
 
1389
#if DEBUG
1390
        printk("stli_rawclose(brdp=%x,portp=%x,arg=%x,wait=%d)\n",
1391
                (int) brdp, (int) portp, (int) arg, wait);
1392
#endif
1393
 
1394
        save_flags(flags);
1395
        cli();
1396
 
1397
/*
1398
 *      Slave is already closing this port. This can happen if a hangup
1399
 *      occurs on this port.
1400
 */
1401
        if (wait) {
1402
                while (test_bit(ST_CLOSING, &portp->state)) {
1403
                        if (signal_pending(current)) {
1404
                                restore_flags(flags);
1405
                                return(-ERESTARTSYS);
1406
                        }
1407
                        interruptible_sleep_on(&portp->raw_wait);
1408
                }
1409
        }
1410
 
1411
/*
1412
 *      Write the close command into shared memory.
1413
 */
1414
        EBRDENABLE(brdp);
1415
        cp = &((volatile cdkasy_t *) EBRDGETMEMPTR(brdp, portp->addr))->ctrl;
1416
        cp->closearg = arg;
1417
        cp->close = 1;
1418
        hdrp = (volatile cdkhdr_t *) EBRDGETMEMPTR(brdp, CDK_CDKADDR);
1419
        bits = ((volatile unsigned char *) hdrp) + brdp->slaveoffset +
1420
                portp->portidx;
1421
        *bits |= portp->portbit;
1422
        EBRDDISABLE(brdp);
1423
 
1424
        set_bit(ST_CLOSING, &portp->state);
1425
        if (wait == 0) {
1426
                restore_flags(flags);
1427
                return(0);
1428
        }
1429
 
1430
/*
1431
 *      Slave is in action, so now we must wait for the open acknowledgment
1432
 *      to come back.
1433
 */
1434
        rc = 0;
1435
        while (test_bit(ST_CLOSING, &portp->state)) {
1436
                if (signal_pending(current)) {
1437
                        rc = -ERESTARTSYS;
1438
                        break;
1439
                }
1440
                interruptible_sleep_on(&portp->raw_wait);
1441
        }
1442
        restore_flags(flags);
1443
 
1444
        if ((rc == 0) && (portp->rc != 0))
1445
                rc = -EIO;
1446
        return(rc);
1447
}
1448
 
1449
/*****************************************************************************/
1450
 
1451
/*
1452
 *      Send a command to the slave and wait for the response. This must
1453
 *      have user context (it sleeps). This routine is generic in that it
1454
 *      can send any type of command. Its purpose is to wait for that command
1455
 *      to complete (as opposed to initiating the command then returning).
1456
 */
1457
 
1458
static int stli_cmdwait(stlibrd_t *brdp, stliport_t *portp, unsigned long cmd, void *arg, int size, int copyback)
1459
{
1460
        unsigned long   flags;
1461
 
1462
#if DEBUG
1463
        printk("stli_cmdwait(brdp=%x,portp=%x,cmd=%x,arg=%x,size=%d,"
1464
                "copyback=%d)\n", (int) brdp, (int) portp, (int) cmd,
1465
                (int) arg, size, copyback);
1466
#endif
1467
 
1468
        save_flags(flags);
1469
        cli();
1470
        while (test_bit(ST_CMDING, &portp->state)) {
1471
                if (signal_pending(current)) {
1472
                        restore_flags(flags);
1473
                        return(-ERESTARTSYS);
1474
                }
1475
                interruptible_sleep_on(&portp->raw_wait);
1476
        }
1477
 
1478
        stli_sendcmd(brdp, portp, cmd, arg, size, copyback);
1479
 
1480
        while (test_bit(ST_CMDING, &portp->state)) {
1481
                if (signal_pending(current)) {
1482
                        restore_flags(flags);
1483
                        return(-ERESTARTSYS);
1484
                }
1485
                interruptible_sleep_on(&portp->raw_wait);
1486
        }
1487
        restore_flags(flags);
1488
 
1489
        if (portp->rc != 0)
1490
                return(-EIO);
1491
        return(0);
1492
}
1493
 
1494
/*****************************************************************************/
1495
 
1496
/*
1497
 *      Send the termios settings for this port to the slave. This sleeps
1498
 *      waiting for the command to complete - so must have user context.
1499
 */
1500
 
1501
static int stli_setport(stliport_t *portp)
1502
{
1503
        stlibrd_t       *brdp;
1504
        asyport_t       aport;
1505
 
1506
#if DEBUG
1507
        printk("stli_setport(portp=%x)\n", (int) portp);
1508
#endif
1509
 
1510
        if (portp == (stliport_t *) NULL)
1511
                return(-ENODEV);
1512
        if (portp->tty == (struct tty_struct *) NULL)
1513
                return(-ENODEV);
1514
        if ((portp->brdnr < 0) && (portp->brdnr >= stli_nrbrds))
1515
                return(-ENODEV);
1516
        brdp = stli_brds[portp->brdnr];
1517
        if (brdp == (stlibrd_t *) NULL)
1518
                return(-ENODEV);
1519
 
1520
        stli_mkasyport(portp, &aport, portp->tty->termios);
1521
        return(stli_cmdwait(brdp, portp, A_SETPORT, &aport, sizeof(asyport_t), 0));
1522
}
1523
 
1524
/*****************************************************************************/
1525
 
1526
/*
1527
 *      Wait for a specified delay period, this is not a busy-loop. It will
1528
 *      give up the processor while waiting. Unfortunately this has some
1529
 *      rather intimate knowledge of the process management stuff.
1530
 */
1531
 
1532
static void stli_delay(int len)
1533
{
1534
#if DEBUG
1535
        printk("stli_delay(len=%d)\n", len);
1536
#endif
1537
        if (len > 0) {
1538
                set_current_state(TASK_INTERRUPTIBLE);
1539
                schedule_timeout(len);
1540
                set_current_state(TASK_RUNNING);
1541
        }
1542
}
1543
 
1544
/*****************************************************************************/
1545
 
1546
/*
1547
 *      Possibly need to wait for carrier (DCD signal) to come high. Say
1548
 *      maybe because if we are clocal then we don't need to wait...
1549
 */
1550
 
1551
static int stli_waitcarrier(stlibrd_t *brdp, stliport_t *portp, struct file *filp)
1552
{
1553
        unsigned long   flags;
1554
        int             rc, doclocal;
1555
 
1556
#if DEBUG
1557
        printk("stli_waitcarrier(brdp=%x,portp=%x,filp=%x)\n",
1558
                (int) brdp, (int) portp, (int) filp);
1559
#endif
1560
 
1561
        rc = 0;
1562
        doclocal = 0;
1563
 
1564
        if (portp->flags & ASYNC_CALLOUT_ACTIVE) {
1565
                if (portp->normaltermios.c_cflag & CLOCAL)
1566
                        doclocal++;
1567
        } else {
1568
                if (portp->tty->termios->c_cflag & CLOCAL)
1569
                        doclocal++;
1570
        }
1571
 
1572
        save_flags(flags);
1573
        cli();
1574
        portp->openwaitcnt++;
1575
        if (! tty_hung_up_p(filp))
1576
                portp->refcount--;
1577
 
1578
        for (;;) {
1579
                if ((portp->flags & ASYNC_CALLOUT_ACTIVE) == 0) {
1580
                        stli_mkasysigs(&portp->asig, 1, 1);
1581
                        if ((rc = stli_cmdwait(brdp, portp, A_SETSIGNALS,
1582
                            &portp->asig, sizeof(asysigs_t), 0)) < 0)
1583
                                break;
1584
                }
1585
                if (tty_hung_up_p(filp) ||
1586
                    ((portp->flags & ASYNC_INITIALIZED) == 0)) {
1587
                        if (portp->flags & ASYNC_HUP_NOTIFY)
1588
                                rc = -EBUSY;
1589
                        else
1590
                                rc = -ERESTARTSYS;
1591
                        break;
1592
                }
1593
                if (((portp->flags & ASYNC_CALLOUT_ACTIVE) == 0) &&
1594
                    ((portp->flags & ASYNC_CLOSING) == 0) &&
1595
                    (doclocal || (portp->sigs & TIOCM_CD))) {
1596
                        break;
1597
                }
1598
                if (signal_pending(current)) {
1599
                        rc = -ERESTARTSYS;
1600
                        break;
1601
                }
1602
                interruptible_sleep_on(&portp->open_wait);
1603
        }
1604
 
1605
        if (! tty_hung_up_p(filp))
1606
                portp->refcount++;
1607
        portp->openwaitcnt--;
1608
        restore_flags(flags);
1609
 
1610
        return(rc);
1611
}
1612
 
1613
/*****************************************************************************/
1614
 
1615
/*
1616
 *      Write routine. Take the data and put it in the shared memory ring
1617
 *      queue. If port is not already sending chars then need to mark the
1618
 *      service bits for this port.
1619
 */
1620
 
1621
static int stli_write(struct tty_struct *tty, int from_user, const unsigned char *buf, int count)
1622
{
1623
        volatile cdkasy_t       *ap;
1624
        volatile cdkhdr_t       *hdrp;
1625
        volatile unsigned char  *bits;
1626
        unsigned char           *shbuf, *chbuf;
1627
        stliport_t              *portp;
1628
        stlibrd_t               *brdp;
1629
        unsigned int            len, stlen, head, tail, size;
1630
        unsigned long           flags;
1631
 
1632
#if DEBUG
1633
        printk("stli_write(tty=%x,from_user=%d,buf=%x,count=%d)\n",
1634
                (int) tty, from_user, (int) buf, count);
1635
#endif
1636
 
1637
        if ((tty == (struct tty_struct *) NULL) ||
1638
            (stli_tmpwritebuf == (char *) NULL))
1639
                return(0);
1640
        if (tty == stli_txcooktty)
1641
                stli_flushchars(tty);
1642
        portp = tty->driver_data;
1643
        if (portp == (stliport_t *) NULL)
1644
                return(0);
1645
        if ((portp->brdnr < 0) || (portp->brdnr >= stli_nrbrds))
1646
                return(0);
1647
        brdp = stli_brds[portp->brdnr];
1648
        if (brdp == (stlibrd_t *) NULL)
1649
                return(0);
1650
        chbuf = (unsigned char *) buf;
1651
 
1652
/*
1653
 *      If copying direct from user space we need to be able to handle page
1654
 *      faults while we are copying. To do this copy as much as we can now
1655
 *      into a kernel buffer. From there we copy it into shared memory. The
1656
 *      big problem is that we do not want shared memory enabled when we are
1657
 *      sleeping (other boards may be serviced while asleep). Something else
1658
 *      to note here is the reading of the tail twice. Since the boards
1659
 *      shared memory can be on an 8-bit bus then we need to be very careful
1660
 *      reading 16 bit quantities - since both the board (slave) and host
1661
 *      could be writing and reading at the same time.
1662
 */
1663
        if (from_user) {
1664
                save_flags(flags);
1665
                cli();
1666
                EBRDENABLE(brdp);
1667
                ap = (volatile cdkasy_t *) EBRDGETMEMPTR(brdp, portp->addr);
1668
                head = (unsigned int) ap->txq.head;
1669
                tail = (unsigned int) ap->txq.tail;
1670
                if (tail != ((unsigned int) ap->txq.tail))
1671
                        tail = (unsigned int) ap->txq.tail;
1672
                len = (head >= tail) ? (portp->txsize - (head - tail) - 1) :
1673
                        (tail - head - 1);
1674
                count = MIN(len, count);
1675
                EBRDDISABLE(brdp);
1676
                restore_flags(flags);
1677
 
1678
                down(&stli_tmpwritesem);
1679
                copy_from_user(stli_tmpwritebuf, chbuf, count);
1680
                chbuf = &stli_tmpwritebuf[0];
1681
        }
1682
 
1683
/*
1684
 *      All data is now local, shove as much as possible into shared memory.
1685
 */
1686
        save_flags(flags);
1687
        cli();
1688
        EBRDENABLE(brdp);
1689
        ap = (volatile cdkasy_t *) EBRDGETMEMPTR(brdp, portp->addr);
1690
        head = (unsigned int) ap->txq.head;
1691
        tail = (unsigned int) ap->txq.tail;
1692
        if (tail != ((unsigned int) ap->txq.tail))
1693
                tail = (unsigned int) ap->txq.tail;
1694
        size = portp->txsize;
1695
        if (head >= tail) {
1696
                len = size - (head - tail) - 1;
1697
                stlen = size - head;
1698
        } else {
1699
                len = tail - head - 1;
1700
                stlen = len;
1701
        }
1702
 
1703
        len = MIN(len, count);
1704
        count = 0;
1705
        shbuf = (char *) EBRDGETMEMPTR(brdp, portp->txoffset);
1706
 
1707
        while (len > 0) {
1708
                stlen = MIN(len, stlen);
1709
                memcpy((shbuf + head), chbuf, stlen);
1710
                chbuf += stlen;
1711
                len -= stlen;
1712
                count += stlen;
1713
                head += stlen;
1714
                if (head >= size) {
1715
                        head = 0;
1716
                        stlen = tail;
1717
                }
1718
        }
1719
 
1720
        ap = (volatile cdkasy_t *) EBRDGETMEMPTR(brdp, portp->addr);
1721
        ap->txq.head = head;
1722
        if (test_bit(ST_TXBUSY, &portp->state)) {
1723
                if (ap->changed.data & DT_TXEMPTY)
1724
                        ap->changed.data &= ~DT_TXEMPTY;
1725
        }
1726
        hdrp = (volatile cdkhdr_t *) EBRDGETMEMPTR(brdp, CDK_CDKADDR);
1727
        bits = ((volatile unsigned char *) hdrp) + brdp->slaveoffset +
1728
                portp->portidx;
1729
        *bits |= portp->portbit;
1730
        set_bit(ST_TXBUSY, &portp->state);
1731
        EBRDDISABLE(brdp);
1732
 
1733
        if (from_user)
1734
                up(&stli_tmpwritesem);
1735
        restore_flags(flags);
1736
 
1737
        return(count);
1738
}
1739
 
1740
/*****************************************************************************/
1741
 
1742
/*
1743
 *      Output a single character. We put it into a temporary local buffer
1744
 *      (for speed) then write out that buffer when the flushchars routine
1745
 *      is called. There is a safety catch here so that if some other port
1746
 *      writes chars before the current buffer has been, then we write them
1747
 *      first them do the new ports.
1748
 */
1749
 
1750
static void stli_putchar(struct tty_struct *tty, unsigned char ch)
1751
{
1752
#if DEBUG
1753
        printk("stli_putchar(tty=%x,ch=%x)\n", (int) tty, (int) ch);
1754
#endif
1755
 
1756
        if (tty == (struct tty_struct *) NULL)
1757
                return;
1758
        if (tty != stli_txcooktty) {
1759
                if (stli_txcooktty != (struct tty_struct *) NULL)
1760
                        stli_flushchars(stli_txcooktty);
1761
                stli_txcooktty = tty;
1762
        }
1763
 
1764
        stli_txcookbuf[stli_txcooksize++] = ch;
1765
}
1766
 
1767
/*****************************************************************************/
1768
 
1769
/*
1770
 *      Transfer characters from the local TX cooking buffer to the board.
1771
 *      We sort of ignore the tty that gets passed in here. We rely on the
1772
 *      info stored with the TX cook buffer to tell us which port to flush
1773
 *      the data on. In any case we clean out the TX cook buffer, for re-use
1774
 *      by someone else.
1775
 */
1776
 
1777
static void stli_flushchars(struct tty_struct *tty)
1778
{
1779
        volatile cdkhdr_t       *hdrp;
1780
        volatile unsigned char  *bits;
1781
        volatile cdkasy_t       *ap;
1782
        struct tty_struct       *cooktty;
1783
        stliport_t              *portp;
1784
        stlibrd_t               *brdp;
1785
        unsigned int            len, stlen, head, tail, size, count, cooksize;
1786
        unsigned char           *buf, *shbuf;
1787
        unsigned long           flags;
1788
 
1789
#if DEBUG
1790
        printk("stli_flushchars(tty=%x)\n", (int) tty);
1791
#endif
1792
 
1793
        cooksize = stli_txcooksize;
1794
        cooktty = stli_txcooktty;
1795
        stli_txcooksize = 0;
1796
        stli_txcookrealsize = 0;
1797
        stli_txcooktty = (struct tty_struct *) NULL;
1798
 
1799
        if (tty == (struct tty_struct *) NULL)
1800
                return;
1801
        if (cooktty == (struct tty_struct *) NULL)
1802
                return;
1803
        if (tty != cooktty)
1804
                tty = cooktty;
1805
        if (cooksize == 0)
1806
                return;
1807
 
1808
        portp = tty->driver_data;
1809
        if (portp == (stliport_t *) NULL)
1810
                return;
1811
        if ((portp->brdnr < 0) || (portp->brdnr >= stli_nrbrds))
1812
                return;
1813
        brdp = stli_brds[portp->brdnr];
1814
        if (brdp == (stlibrd_t *) NULL)
1815
                return;
1816
 
1817
        save_flags(flags);
1818
        cli();
1819
        EBRDENABLE(brdp);
1820
 
1821
        ap = (volatile cdkasy_t *) EBRDGETMEMPTR(brdp, portp->addr);
1822
        head = (unsigned int) ap->txq.head;
1823
        tail = (unsigned int) ap->txq.tail;
1824
        if (tail != ((unsigned int) ap->txq.tail))
1825
                tail = (unsigned int) ap->txq.tail;
1826
        size = portp->txsize;
1827
        if (head >= tail) {
1828
                len = size - (head - tail) - 1;
1829
                stlen = size - head;
1830
        } else {
1831
                len = tail - head - 1;
1832
                stlen = len;
1833
        }
1834
 
1835
        len = MIN(len, cooksize);
1836
        count = 0;
1837
        shbuf = (char *) EBRDGETMEMPTR(brdp, portp->txoffset);
1838
        buf = stli_txcookbuf;
1839
 
1840
        while (len > 0) {
1841
                stlen = MIN(len, stlen);
1842
                memcpy((shbuf + head), buf, stlen);
1843
                buf += stlen;
1844
                len -= stlen;
1845
                count += stlen;
1846
                head += stlen;
1847
                if (head >= size) {
1848
                        head = 0;
1849
                        stlen = tail;
1850
                }
1851
        }
1852
 
1853
        ap = (volatile cdkasy_t *) EBRDGETMEMPTR(brdp, portp->addr);
1854
        ap->txq.head = head;
1855
 
1856
        if (test_bit(ST_TXBUSY, &portp->state)) {
1857
                if (ap->changed.data & DT_TXEMPTY)
1858
                        ap->changed.data &= ~DT_TXEMPTY;
1859
        }
1860
        hdrp = (volatile cdkhdr_t *) EBRDGETMEMPTR(brdp, CDK_CDKADDR);
1861
        bits = ((volatile unsigned char *) hdrp) + brdp->slaveoffset +
1862
                portp->portidx;
1863
        *bits |= portp->portbit;
1864
        set_bit(ST_TXBUSY, &portp->state);
1865
 
1866
        EBRDDISABLE(brdp);
1867
        restore_flags(flags);
1868
}
1869
 
1870
/*****************************************************************************/
1871
 
1872
static int stli_writeroom(struct tty_struct *tty)
1873
{
1874
        volatile cdkasyrq_t     *rp;
1875
        stliport_t              *portp;
1876
        stlibrd_t               *brdp;
1877
        unsigned int            head, tail, len;
1878
        unsigned long           flags;
1879
 
1880
#if DEBUG
1881
        printk("stli_writeroom(tty=%x)\n", (int) tty);
1882
#endif
1883
 
1884
        if (tty == (struct tty_struct *) NULL)
1885
                return(0);
1886
        if (tty == stli_txcooktty) {
1887
                if (stli_txcookrealsize != 0) {
1888
                        len = stli_txcookrealsize - stli_txcooksize;
1889
                        return(len);
1890
                }
1891
        }
1892
 
1893
        portp = tty->driver_data;
1894
        if (portp == (stliport_t *) NULL)
1895
                return(0);
1896
        if ((portp->brdnr < 0) || (portp->brdnr >= stli_nrbrds))
1897
                return(0);
1898
        brdp = stli_brds[portp->brdnr];
1899
        if (brdp == (stlibrd_t *) NULL)
1900
                return(0);
1901
 
1902
        save_flags(flags);
1903
        cli();
1904
        EBRDENABLE(brdp);
1905
        rp = &((volatile cdkasy_t *) EBRDGETMEMPTR(brdp, portp->addr))->txq;
1906
        head = (unsigned int) rp->head;
1907
        tail = (unsigned int) rp->tail;
1908
        if (tail != ((unsigned int) rp->tail))
1909
                tail = (unsigned int) rp->tail;
1910
        len = (head >= tail) ? (portp->txsize - (head - tail)) : (tail - head);
1911
        len--;
1912
        EBRDDISABLE(brdp);
1913
        restore_flags(flags);
1914
 
1915
        if (tty == stli_txcooktty) {
1916
                stli_txcookrealsize = len;
1917
                len -= stli_txcooksize;
1918
        }
1919
        return(len);
1920
}
1921
 
1922
/*****************************************************************************/
1923
 
1924
/*
1925
 *      Return the number of characters in the transmit buffer. Normally we
1926
 *      will return the number of chars in the shared memory ring queue.
1927
 *      We need to kludge around the case where the shared memory buffer is
1928
 *      empty but not all characters have drained yet, for this case just
1929
 *      return that there is 1 character in the buffer!
1930
 */
1931
 
1932
static int stli_charsinbuffer(struct tty_struct *tty)
1933
{
1934
        volatile cdkasyrq_t     *rp;
1935
        stliport_t              *portp;
1936
        stlibrd_t               *brdp;
1937
        unsigned int            head, tail, len;
1938
        unsigned long           flags;
1939
 
1940
#if DEBUG
1941
        printk("stli_charsinbuffer(tty=%x)\n", (int) tty);
1942
#endif
1943
 
1944
        if (tty == (struct tty_struct *) NULL)
1945
                return(0);
1946
        if (tty == stli_txcooktty)
1947
                stli_flushchars(tty);
1948
        portp = tty->driver_data;
1949
        if (portp == (stliport_t *) NULL)
1950
                return(0);
1951
        if ((portp->brdnr < 0) || (portp->brdnr >= stli_nrbrds))
1952
                return(0);
1953
        brdp = stli_brds[portp->brdnr];
1954
        if (brdp == (stlibrd_t *) NULL)
1955
                return(0);
1956
 
1957
        save_flags(flags);
1958
        cli();
1959
        EBRDENABLE(brdp);
1960
        rp = &((volatile cdkasy_t *) EBRDGETMEMPTR(brdp, portp->addr))->txq;
1961
        head = (unsigned int) rp->head;
1962
        tail = (unsigned int) rp->tail;
1963
        if (tail != ((unsigned int) rp->tail))
1964
                tail = (unsigned int) rp->tail;
1965
        len = (head >= tail) ? (head - tail) : (portp->txsize - (tail - head));
1966
        if ((len == 0) && test_bit(ST_TXBUSY, &portp->state))
1967
                len = 1;
1968
        EBRDDISABLE(brdp);
1969
        restore_flags(flags);
1970
 
1971
        return(len);
1972
}
1973
 
1974
/*****************************************************************************/
1975
 
1976
/*
1977
 *      Generate the serial struct info.
1978
 */
1979
 
1980
static void stli_getserial(stliport_t *portp, struct serial_struct *sp)
1981
{
1982
        struct serial_struct    sio;
1983
        stlibrd_t               *brdp;
1984
 
1985
#if DEBUG
1986
        printk("stli_getserial(portp=%x,sp=%x)\n", (int) portp, (int) sp);
1987
#endif
1988
 
1989
        memset(&sio, 0, sizeof(struct serial_struct));
1990
        sio.type = PORT_UNKNOWN;
1991
        sio.line = portp->portnr;
1992
        sio.irq = 0;
1993
        sio.flags = portp->flags;
1994
        sio.baud_base = portp->baud_base;
1995
        sio.close_delay = portp->close_delay;
1996
        sio.closing_wait = portp->closing_wait;
1997
        sio.custom_divisor = portp->custom_divisor;
1998
        sio.xmit_fifo_size = 0;
1999
        sio.hub6 = 0;
2000
 
2001
        brdp = stli_brds[portp->brdnr];
2002
        if (brdp != (stlibrd_t *) NULL)
2003
                sio.port = brdp->iobase;
2004
 
2005
        copy_to_user(sp, &sio, sizeof(struct serial_struct));
2006
}
2007
 
2008
/*****************************************************************************/
2009
 
2010
/*
2011
 *      Set port according to the serial struct info.
2012
 *      At this point we do not do any auto-configure stuff, so we will
2013
 *      just quietly ignore any requests to change irq, etc.
2014
 */
2015
 
2016
static int stli_setserial(stliport_t *portp, struct serial_struct *sp)
2017
{
2018
        struct serial_struct    sio;
2019
        int                     rc;
2020
 
2021
#if DEBUG
2022
        printk("stli_setserial(portp=%x,sp=%x)\n", (int) portp, (int) sp);
2023
#endif
2024
 
2025
        if (copy_from_user(&sio, sp, sizeof(struct serial_struct)))
2026
                return -EFAULT;
2027
        if (!capable(CAP_SYS_ADMIN)) {
2028
                if ((sio.baud_base != portp->baud_base) ||
2029
                    (sio.close_delay != portp->close_delay) ||
2030
                    ((sio.flags & ~ASYNC_USR_MASK) !=
2031
                    (portp->flags & ~ASYNC_USR_MASK)))
2032
                        return(-EPERM);
2033
        }
2034
 
2035
        portp->flags = (portp->flags & ~ASYNC_USR_MASK) |
2036
                (sio.flags & ASYNC_USR_MASK);
2037
        portp->baud_base = sio.baud_base;
2038
        portp->close_delay = sio.close_delay;
2039
        portp->closing_wait = sio.closing_wait;
2040
        portp->custom_divisor = sio.custom_divisor;
2041
 
2042
        if ((rc = stli_setport(portp)) < 0)
2043
                return(rc);
2044
        return(0);
2045
}
2046
 
2047
/*****************************************************************************/
2048
 
2049
static int stli_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg)
2050
{
2051
        stliport_t      *portp;
2052
        stlibrd_t       *brdp;
2053
        unsigned long   lval;
2054
        unsigned int    ival;
2055
        int             rc;
2056
 
2057
#if DEBUG
2058
        printk("stli_ioctl(tty=%x,file=%x,cmd=%x,arg=%x)\n",
2059
                (int) tty, (int) file, cmd, (int) arg);
2060
#endif
2061
 
2062
        if (tty == (struct tty_struct *) NULL)
2063
                return(-ENODEV);
2064
        portp = tty->driver_data;
2065
        if (portp == (stliport_t *) NULL)
2066
                return(-ENODEV);
2067
        if ((portp->brdnr < 0) || (portp->brdnr >= stli_nrbrds))
2068
                return(0);
2069
        brdp = stli_brds[portp->brdnr];
2070
        if (brdp == (stlibrd_t *) NULL)
2071
                return(0);
2072
 
2073
        if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
2074
            (cmd != COM_GETPORTSTATS) && (cmd != COM_CLRPORTSTATS)) {
2075
                if (tty->flags & (1 << TTY_IO_ERROR))
2076
                        return(-EIO);
2077
        }
2078
 
2079
        rc = 0;
2080
 
2081
        switch (cmd) {
2082
        case TIOCGSOFTCAR:
2083
                rc = put_user(((tty->termios->c_cflag & CLOCAL) ? 1 : 0),
2084
                        (unsigned int *) arg);
2085
                break;
2086
        case TIOCSSOFTCAR:
2087
                if ((rc = get_user(ival, (unsigned int *) arg)) == 0)
2088
                        tty->termios->c_cflag =
2089
                                (tty->termios->c_cflag & ~CLOCAL) |
2090
                                (ival ? CLOCAL : 0);
2091
                break;
2092
        case TIOCMGET:
2093
                if ((rc = verify_area(VERIFY_WRITE, (void *) arg,
2094
                    sizeof(unsigned int))) == 0) {
2095
                        if ((rc = stli_cmdwait(brdp, portp, A_GETSIGNALS,
2096
                            &portp->asig, sizeof(asysigs_t), 1)) < 0)
2097
                                return(rc);
2098
                        lval = stli_mktiocm(portp->asig.sigvalue);
2099
                        put_user(lval, (unsigned int *) arg);
2100
                }
2101
                break;
2102
        case TIOCMBIS:
2103
                if ((rc = get_user(ival, (unsigned int *) arg)) == 0) {
2104
                        stli_mkasysigs(&portp->asig,
2105
                                ((ival & TIOCM_DTR) ? 1 : -1),
2106
                                ((ival & TIOCM_RTS) ? 1 : -1));
2107
                        rc = stli_cmdwait(brdp, portp, A_SETSIGNALS,
2108
                                &portp->asig, sizeof(asysigs_t), 0);
2109
                }
2110
                break;
2111
        case TIOCMBIC:
2112
                if ((rc = get_user(ival, (unsigned int *) arg)) == 0) {
2113
                        stli_mkasysigs(&portp->asig,
2114
                                ((ival & TIOCM_DTR) ? 0 : -1),
2115
                                ((ival & TIOCM_RTS) ? 0 : -1));
2116
                        rc = stli_cmdwait(brdp, portp, A_SETSIGNALS,
2117
                                &portp->asig, sizeof(asysigs_t), 0);
2118
                }
2119
                break;
2120
        case TIOCMSET:
2121
                if ((rc = get_user(ival, (unsigned int *) arg)) == 0) {
2122
                        stli_mkasysigs(&portp->asig,
2123
                                ((ival & TIOCM_DTR) ? 1 : 0),
2124
                                ((ival & TIOCM_RTS) ? 1 : 0));
2125
                        rc = stli_cmdwait(brdp, portp, A_SETSIGNALS,
2126
                                &portp->asig, sizeof(asysigs_t), 0);
2127
                }
2128
                break;
2129
        case TIOCGSERIAL:
2130
                if ((rc = verify_area(VERIFY_WRITE, (void *) arg,
2131
                    sizeof(struct serial_struct))) == 0)
2132
                        stli_getserial(portp, (struct serial_struct *) arg);
2133
                break;
2134
        case TIOCSSERIAL:
2135
                if ((rc = verify_area(VERIFY_READ, (void *) arg,
2136
                    sizeof(struct serial_struct))) == 0)
2137
                        rc = stli_setserial(portp, (struct serial_struct *)arg);
2138
                break;
2139
        case STL_GETPFLAG:
2140
                rc = put_user(portp->pflag, (unsigned int *) arg);
2141
                break;
2142
        case STL_SETPFLAG:
2143
                if ((rc = get_user(portp->pflag, (unsigned int *) arg)) == 0)
2144
                        stli_setport(portp);
2145
                break;
2146
        case COM_GETPORTSTATS:
2147
                if ((rc = verify_area(VERIFY_WRITE, (void *) arg,
2148
                    sizeof(comstats_t))) == 0)
2149
                        rc = stli_getportstats(portp, (comstats_t *) arg);
2150
                break;
2151
        case COM_CLRPORTSTATS:
2152
                if ((rc = verify_area(VERIFY_WRITE, (void *) arg,
2153
                    sizeof(comstats_t))) == 0)
2154
                        rc = stli_clrportstats(portp, (comstats_t *) arg);
2155
                break;
2156
        case TIOCSERCONFIG:
2157
        case TIOCSERGWILD:
2158
        case TIOCSERSWILD:
2159
        case TIOCSERGETLSR:
2160
        case TIOCSERGSTRUCT:
2161
        case TIOCSERGETMULTI:
2162
        case TIOCSERSETMULTI:
2163
        default:
2164
                rc = -ENOIOCTLCMD;
2165
                break;
2166
        }
2167
 
2168
        return(rc);
2169
}
2170
 
2171
/*****************************************************************************/
2172
 
2173
/*
2174
 *      This routine assumes that we have user context and can sleep.
2175
 *      Looks like it is true for the current ttys implementation..!!
2176
 */
2177
 
2178
static void stli_settermios(struct tty_struct *tty, struct termios *old)
2179
{
2180
        stliport_t      *portp;
2181
        stlibrd_t       *brdp;
2182
        struct termios  *tiosp;
2183
        asyport_t       aport;
2184
 
2185
#if DEBUG
2186
        printk("stli_settermios(tty=%x,old=%x)\n", (int) tty, (int) old);
2187
#endif
2188
 
2189
        if (tty == (struct tty_struct *) NULL)
2190
                return;
2191
        portp = tty->driver_data;
2192
        if (portp == (stliport_t *) NULL)
2193
                return;
2194
        if ((portp->brdnr < 0) || (portp->brdnr >= stli_nrbrds))
2195
                return;
2196
        brdp = stli_brds[portp->brdnr];
2197
        if (brdp == (stlibrd_t *) NULL)
2198
                return;
2199
 
2200
        tiosp = tty->termios;
2201
        if ((tiosp->c_cflag == old->c_cflag) &&
2202
            (tiosp->c_iflag == old->c_iflag))
2203
                return;
2204
 
2205
        stli_mkasyport(portp, &aport, tiosp);
2206
        stli_cmdwait(brdp, portp, A_SETPORT, &aport, sizeof(asyport_t), 0);
2207
        stli_mkasysigs(&portp->asig, ((tiosp->c_cflag & CBAUD) ? 1 : 0), -1);
2208
        stli_cmdwait(brdp, portp, A_SETSIGNALS, &portp->asig,
2209
                sizeof(asysigs_t), 0);
2210
        if ((old->c_cflag & CRTSCTS) && ((tiosp->c_cflag & CRTSCTS) == 0))
2211
                tty->hw_stopped = 0;
2212
        if (((old->c_cflag & CLOCAL) == 0) && (tiosp->c_cflag & CLOCAL))
2213
                wake_up_interruptible(&portp->open_wait);
2214
}
2215
 
2216
/*****************************************************************************/
2217
 
2218
/*
2219
 *      Attempt to flow control who ever is sending us data. We won't really
2220
 *      do any flow control action here. We can't directly, and even if we
2221
 *      wanted to we would have to send a command to the slave. The slave
2222
 *      knows how to flow control, and will do so when its buffers reach its
2223
 *      internal high water marks. So what we will do is set a local state
2224
 *      bit that will stop us sending any RX data up from the poll routine
2225
 *      (which is the place where RX data from the slave is handled).
2226
 */
2227
 
2228
static void stli_throttle(struct tty_struct *tty)
2229
{
2230
        stliport_t      *portp;
2231
 
2232
#if DEBUG
2233
        printk("stli_throttle(tty=%x)\n", (int) tty);
2234
#endif
2235
 
2236
        if (tty == (struct tty_struct *) NULL)
2237
                return;
2238
        portp = tty->driver_data;
2239
        if (portp == (stliport_t *) NULL)
2240
                return;
2241
 
2242
        set_bit(ST_RXSTOP, &portp->state);
2243
}
2244
 
2245
/*****************************************************************************/
2246
 
2247
/*
2248
 *      Unflow control the device sending us data... That means that all
2249
 *      we have to do is clear the RXSTOP state bit. The next poll call
2250
 *      will then be able to pass the RX data back up.
2251
 */
2252
 
2253
static void stli_unthrottle(struct tty_struct *tty)
2254
{
2255
        stliport_t      *portp;
2256
 
2257
#if DEBUG
2258
        printk("stli_unthrottle(tty=%x)\n", (int) tty);
2259
#endif
2260
 
2261
        if (tty == (struct tty_struct *) NULL)
2262
                return;
2263
        portp = tty->driver_data;
2264
        if (portp == (stliport_t *) NULL)
2265
                return;
2266
 
2267
        clear_bit(ST_RXSTOP, &portp->state);
2268
}
2269
 
2270
/*****************************************************************************/
2271
 
2272
/*
2273
 *      Stop the transmitter. Basically to do this we will just turn TX
2274
 *      interrupts off.
2275
 */
2276
 
2277
static void stli_stop(struct tty_struct *tty)
2278
{
2279
        stlibrd_t       *brdp;
2280
        stliport_t      *portp;
2281
        asyctrl_t       actrl;
2282
 
2283
#if DEBUG
2284
        printk("stli_stop(tty=%x)\n", (int) tty);
2285
#endif
2286
 
2287
        if (tty == (struct tty_struct *) NULL)
2288
                return;
2289
        portp = tty->driver_data;
2290
        if (portp == (stliport_t *) NULL)
2291
                return;
2292
        if ((portp->brdnr < 0) || (portp->brdnr >= stli_nrbrds))
2293
                return;
2294
        brdp = stli_brds[portp->brdnr];
2295
        if (brdp == (stlibrd_t *) NULL)
2296
                return;
2297
 
2298
        memset(&actrl, 0, sizeof(asyctrl_t));
2299
        actrl.txctrl = CT_STOPFLOW;
2300
#if 0
2301
        stli_cmdwait(brdp, portp, A_PORTCTRL, &actrl, sizeof(asyctrl_t), 0);
2302
#endif
2303
}
2304
 
2305
/*****************************************************************************/
2306
 
2307
/*
2308
 *      Start the transmitter again. Just turn TX interrupts back on.
2309
 */
2310
 
2311
static void stli_start(struct tty_struct *tty)
2312
{
2313
        stliport_t      *portp;
2314
        stlibrd_t       *brdp;
2315
        asyctrl_t       actrl;
2316
 
2317
#if DEBUG
2318
        printk("stli_start(tty=%x)\n", (int) tty);
2319
#endif
2320
 
2321
        if (tty == (struct tty_struct *) NULL)
2322
                return;
2323
        portp = tty->driver_data;
2324
        if (portp == (stliport_t *) NULL)
2325
                return;
2326
        if ((portp->brdnr < 0) || (portp->brdnr >= stli_nrbrds))
2327
                return;
2328
        brdp = stli_brds[portp->brdnr];
2329
        if (brdp == (stlibrd_t *) NULL)
2330
                return;
2331
 
2332
        memset(&actrl, 0, sizeof(asyctrl_t));
2333
        actrl.txctrl = CT_STARTFLOW;
2334
#if 0
2335
        stli_cmdwait(brdp, portp, A_PORTCTRL, &actrl, sizeof(asyctrl_t), 0);
2336
#endif
2337
}
2338
 
2339
/*****************************************************************************/
2340
 
2341
/*
2342
 *      Scheduler called hang up routine. This is called from the scheduler,
2343
 *      not direct from the driver "poll" routine. We can't call it there
2344
 *      since the real local hangup code will enable/disable the board and
2345
 *      other things that we can't do while handling the poll. Much easier
2346
 *      to deal with it some time later (don't really care when, hangups
2347
 *      aren't that time critical).
2348
 */
2349
 
2350
static void stli_dohangup(void *arg)
2351
{
2352
        stliport_t      *portp;
2353
 
2354
#if DEBUG
2355
        printk(KERN_DEBUG "stli_dohangup(portp=%x)\n", (int) arg);
2356
#endif
2357
 
2358
        /*
2359
         * FIXME: There's a module removal race here: tty_hangup
2360
         * calls schedule_task which will call into this
2361
         * driver later.
2362
         */
2363
        portp = (stliport_t *) arg;
2364
        if (portp != (stliport_t *) NULL) {
2365
                if (portp->tty != (struct tty_struct *) NULL) {
2366
                        tty_hangup(portp->tty);
2367
                }
2368
        }
2369
        MOD_DEC_USE_COUNT;
2370
}
2371
 
2372
/*****************************************************************************/
2373
 
2374
/*
2375
 *      Hangup this port. This is pretty much like closing the port, only
2376
 *      a little more brutal. No waiting for data to drain. Shutdown the
2377
 *      port and maybe drop signals. This is rather tricky really. We want
2378
 *      to close the port as well.
2379
 */
2380
 
2381
static void stli_hangup(struct tty_struct *tty)
2382
{
2383
        stliport_t      *portp;
2384
        stlibrd_t       *brdp;
2385
        unsigned long   flags;
2386
 
2387
#if DEBUG
2388
        printk(KERN_DEBUG "stli_hangup(tty=%x)\n", (int) tty);
2389
#endif
2390
 
2391
        if (tty == (struct tty_struct *) NULL)
2392
                return;
2393
        portp = tty->driver_data;
2394
        if (portp == (stliport_t *) NULL)
2395
                return;
2396
        if ((portp->brdnr < 0) || (portp->brdnr >= stli_nrbrds))
2397
                return;
2398
        brdp = stli_brds[portp->brdnr];
2399
        if (brdp == (stlibrd_t *) NULL)
2400
                return;
2401
 
2402
        portp->flags &= ~ASYNC_INITIALIZED;
2403
 
2404
        save_flags(flags);
2405
        cli();
2406
        if (! test_bit(ST_CLOSING, &portp->state))
2407
                stli_rawclose(brdp, portp, 0, 0);
2408
        if (tty->termios->c_cflag & HUPCL) {
2409
                stli_mkasysigs(&portp->asig, 0, 0);
2410
                if (test_bit(ST_CMDING, &portp->state)) {
2411
                        set_bit(ST_DOSIGS, &portp->state);
2412
                        set_bit(ST_DOFLUSHTX, &portp->state);
2413
                        set_bit(ST_DOFLUSHRX, &portp->state);
2414
                } else {
2415
                        stli_sendcmd(brdp, portp, A_SETSIGNALSF,
2416
                                &portp->asig, sizeof(asysigs_t), 0);
2417
                }
2418
        }
2419
        restore_flags(flags);
2420
 
2421
        clear_bit(ST_TXBUSY, &portp->state);
2422
        clear_bit(ST_RXSTOP, &portp->state);
2423
        set_bit(TTY_IO_ERROR, &tty->flags);
2424
        portp->tty = (struct tty_struct *) NULL;
2425
        portp->flags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CALLOUT_ACTIVE);
2426
        portp->refcount = 0;
2427
        wake_up_interruptible(&portp->open_wait);
2428
}
2429
 
2430
/*****************************************************************************/
2431
 
2432
/*
2433
 *      Flush characters from the lower buffer. We may not have user context
2434
 *      so we cannot sleep waiting for it to complete. Also we need to check
2435
 *      if there is chars for this port in the TX cook buffer, and flush them
2436
 *      as well.
2437
 */
2438
 
2439
static void stli_flushbuffer(struct tty_struct *tty)
2440
{
2441
        stliport_t      *portp;
2442
        stlibrd_t       *brdp;
2443
        unsigned long   ftype, flags;
2444
 
2445
#if DEBUG
2446
        printk(KERN_DEBUG "stli_flushbuffer(tty=%x)\n", (int) tty);
2447
#endif
2448
 
2449
        if (tty == (struct tty_struct *) NULL)
2450
                return;
2451
        portp = tty->driver_data;
2452
        if (portp == (stliport_t *) NULL)
2453
                return;
2454
        if ((portp->brdnr < 0) || (portp->brdnr >= stli_nrbrds))
2455
                return;
2456
        brdp = stli_brds[portp->brdnr];
2457
        if (brdp == (stlibrd_t *) NULL)
2458
                return;
2459
 
2460
        save_flags(flags);
2461
        cli();
2462
        if (tty == stli_txcooktty) {
2463
                stli_txcooktty = (struct tty_struct *) NULL;
2464
                stli_txcooksize = 0;
2465
                stli_txcookrealsize = 0;
2466
        }
2467
        if (test_bit(ST_CMDING, &portp->state)) {
2468
                set_bit(ST_DOFLUSHTX, &portp->state);
2469
        } else {
2470
                ftype = FLUSHTX;
2471
                if (test_bit(ST_DOFLUSHRX, &portp->state)) {
2472
                        ftype |= FLUSHRX;
2473
                        clear_bit(ST_DOFLUSHRX, &portp->state);
2474
                }
2475
                stli_sendcmd(brdp, portp, A_FLUSH, &ftype,
2476
                        sizeof(unsigned long), 0);
2477
        }
2478
        restore_flags(flags);
2479
 
2480
        wake_up_interruptible(&tty->write_wait);
2481
        if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
2482
            tty->ldisc.write_wakeup)
2483
                (tty->ldisc.write_wakeup)(tty);
2484
}
2485
 
2486
/*****************************************************************************/
2487
 
2488
static void stli_breakctl(struct tty_struct *tty, int state)
2489
{
2490
        stlibrd_t       *brdp;
2491
        stliport_t      *portp;
2492
        long            arg;
2493
        /* long savestate, savetime; */
2494
 
2495
#if DEBUG
2496
        printk(KERN_DEBUG "stli_breakctl(tty=%x,state=%d)\n", (int) tty, state);
2497
#endif
2498
 
2499
        if (tty == (struct tty_struct *) NULL)
2500
                return;
2501
        portp = tty->driver_data;
2502
        if (portp == (stliport_t *) NULL)
2503
                return;
2504
        if ((portp->brdnr < 0) || (portp->brdnr >= stli_nrbrds))
2505
                return;
2506
        brdp = stli_brds[portp->brdnr];
2507
        if (brdp == (stlibrd_t *) NULL)
2508
                return;
2509
 
2510
/*
2511
 *      Due to a bug in the tty send_break() code we need to preserve
2512
 *      the current process state and timeout...
2513
        savetime = current->timeout;
2514
        savestate = current->state;
2515
 */
2516
 
2517
        arg = (state == -1) ? BREAKON : BREAKOFF;
2518
        stli_cmdwait(brdp, portp, A_BREAK, &arg, sizeof(long), 0);
2519
 
2520
/*
2521
 *
2522
        current->timeout = savetime;
2523
        current->state = savestate;
2524
 */
2525
}
2526
 
2527
/*****************************************************************************/
2528
 
2529
static void stli_waituntilsent(struct tty_struct *tty, int timeout)
2530
{
2531
        stliport_t      *portp;
2532
        unsigned long   tend;
2533
 
2534
#if DEBUG
2535
        printk(KERN_DEBUG "stli_waituntilsent(tty=%x,timeout=%x)\n", (int) tty, timeout);
2536
#endif
2537
 
2538
        if (tty == (struct tty_struct *) NULL)
2539
                return;
2540
        portp = tty->driver_data;
2541
        if (portp == (stliport_t *) NULL)
2542
                return;
2543
 
2544
        if (timeout == 0)
2545
                timeout = HZ;
2546
        tend = jiffies + timeout;
2547
 
2548
        while (test_bit(ST_TXBUSY, &portp->state)) {
2549
                if (signal_pending(current))
2550
                        break;
2551
                stli_delay(2);
2552
                if (time_after_eq(jiffies, tend))
2553
                        break;
2554
        }
2555
}
2556
 
2557
/*****************************************************************************/
2558
 
2559
static void stli_sendxchar(struct tty_struct *tty, char ch)
2560
{
2561
        stlibrd_t       *brdp;
2562
        stliport_t      *portp;
2563
        asyctrl_t       actrl;
2564
 
2565
#if DEBUG
2566
        printk(KERN_DEBUG "stli_sendxchar(tty=%x,ch=%x)\n", (int) tty, ch);
2567
#endif
2568
 
2569
        if (tty == (struct tty_struct *) NULL)
2570
                return;
2571
        portp = tty->driver_data;
2572
        if (portp == (stliport_t *) NULL)
2573
                return;
2574
        if ((portp->brdnr < 0) || (portp->brdnr >= stli_nrbrds))
2575
                return;
2576
        brdp = stli_brds[portp->brdnr];
2577
        if (brdp == (stlibrd_t *) NULL)
2578
                return;
2579
 
2580
        memset(&actrl, 0, sizeof(asyctrl_t));
2581
        if (ch == STOP_CHAR(tty)) {
2582
                actrl.rxctrl = CT_STOPFLOW;
2583
        } else if (ch == START_CHAR(tty)) {
2584
                actrl.rxctrl = CT_STARTFLOW;
2585
        } else {
2586
                actrl.txctrl = CT_SENDCHR;
2587
                actrl.tximdch = ch;
2588
        }
2589
 
2590
        stli_cmdwait(brdp, portp, A_PORTCTRL, &actrl, sizeof(asyctrl_t), 0);
2591
}
2592
 
2593
/*****************************************************************************/
2594
 
2595
#define MAXLINE         80
2596
 
2597
/*
2598
 *      Format info for a specified port. The line is deliberately limited
2599
 *      to 80 characters. (If it is too long it will be truncated, if too
2600
 *      short then padded with spaces).
2601
 */
2602
 
2603
static int stli_portinfo(stlibrd_t *brdp, stliport_t *portp, int portnr, char *pos)
2604
{
2605
        char    *sp, *uart;
2606
        int     rc, cnt;
2607
 
2608
        rc = stli_portcmdstats(portp);
2609
 
2610
        uart = "UNKNOWN";
2611
        if (brdp->state & BST_STARTED) {
2612
                switch (stli_comstats.hwid) {
2613
                case 0:          uart = "2681"; break;
2614
                case 1:         uart = "SC26198"; break;
2615
                default:        uart = "CD1400"; break;
2616
                }
2617
        }
2618
 
2619
        sp = pos;
2620
        sp += sprintf(sp, "%d: uart:%s ", portnr, uart);
2621
 
2622
        if ((brdp->state & BST_STARTED) && (rc >= 0)) {
2623
                sp += sprintf(sp, "tx:%d rx:%d", (int) stli_comstats.txtotal,
2624
                        (int) stli_comstats.rxtotal);
2625
 
2626
                if (stli_comstats.rxframing)
2627
                        sp += sprintf(sp, " fe:%d",
2628
                                (int) stli_comstats.rxframing);
2629
                if (stli_comstats.rxparity)
2630
                        sp += sprintf(sp, " pe:%d",
2631
                                (int) stli_comstats.rxparity);
2632
                if (stli_comstats.rxbreaks)
2633
                        sp += sprintf(sp, " brk:%d",
2634
                                (int) stli_comstats.rxbreaks);
2635
                if (stli_comstats.rxoverrun)
2636
                        sp += sprintf(sp, " oe:%d",
2637
                                (int) stli_comstats.rxoverrun);
2638
 
2639
                cnt = sprintf(sp, "%s%s%s%s%s ",
2640
                        (stli_comstats.signals & TIOCM_RTS) ? "|RTS" : "",
2641
                        (stli_comstats.signals & TIOCM_CTS) ? "|CTS" : "",
2642
                        (stli_comstats.signals & TIOCM_DTR) ? "|DTR" : "",
2643
                        (stli_comstats.signals & TIOCM_CD) ? "|DCD" : "",
2644
                        (stli_comstats.signals & TIOCM_DSR) ? "|DSR" : "");
2645
                *sp = ' ';
2646
                sp += cnt;
2647
        }
2648
 
2649
        for (cnt = (sp - pos); (cnt < (MAXLINE - 1)); cnt++)
2650
                *sp++ = ' ';
2651
        if (cnt >= MAXLINE)
2652
                pos[(MAXLINE - 2)] = '+';
2653
        pos[(MAXLINE - 1)] = '\n';
2654
 
2655
        return(MAXLINE);
2656
}
2657
 
2658
/*****************************************************************************/
2659
 
2660
/*
2661
 *      Port info, read from the /proc file system.
2662
 */
2663
 
2664
static int stli_readproc(char *page, char **start, off_t off, int count, int *eof, void *data)
2665
{
2666
        stlibrd_t       *brdp;
2667
        stliport_t      *portp;
2668
        int             brdnr, portnr, totalport;
2669
        int             curoff, maxoff;
2670
        char            *pos;
2671
 
2672
#if DEBUG
2673
        printk(KERN_DEBUG "stli_readproc(page=%x,start=%x,off=%x,count=%d,eof=%x,"
2674
                "data=%x\n", (int) page, (int) start, (int) off, count,
2675
                (int) eof, (int) data);
2676
#endif
2677
 
2678
        pos = page;
2679
        totalport = 0;
2680
        curoff = 0;
2681
 
2682
        if (off == 0) {
2683
                pos += sprintf(pos, "%s: version %s", stli_drvtitle,
2684
                        stli_drvversion);
2685
                while (pos < (page + MAXLINE - 1))
2686
                        *pos++ = ' ';
2687
                *pos++ = '\n';
2688
        }
2689
        curoff =  MAXLINE;
2690
 
2691
/*
2692
 *      We scan through for each board, panel and port. The offset is
2693
 *      calculated on the fly, and irrelevant ports are skipped.
2694
 */
2695
        for (brdnr = 0; (brdnr < stli_nrbrds); brdnr++) {
2696
                brdp = stli_brds[brdnr];
2697
                if (brdp == (stlibrd_t *) NULL)
2698
                        continue;
2699
                if (brdp->state == 0)
2700
                        continue;
2701
 
2702
                maxoff = curoff + (brdp->nrports * MAXLINE);
2703
                if (off >= maxoff) {
2704
                        curoff = maxoff;
2705
                        continue;
2706
                }
2707
 
2708
                totalport = brdnr * STL_MAXPORTS;
2709
                for (portnr = 0; (portnr < brdp->nrports); portnr++,
2710
                    totalport++) {
2711
                        portp = brdp->ports[portnr];
2712
                        if (portp == (stliport_t *) NULL)
2713
                                continue;
2714
                        if (off >= (curoff += MAXLINE))
2715
                                continue;
2716
                        if ((pos - page + MAXLINE) > count)
2717
                                goto stli_readdone;
2718
                        pos += stli_portinfo(brdp, portp, totalport, pos);
2719
                }
2720
        }
2721
 
2722
        *eof = 1;
2723
 
2724
stli_readdone:
2725
        *start = page;
2726
        return(pos - page);
2727
}
2728
 
2729
/*****************************************************************************/
2730
 
2731
/*
2732
 *      Generic send command routine. This will send a message to the slave,
2733
 *      of the specified type with the specified argument. Must be very
2734
 *      careful of data that will be copied out from shared memory -
2735
 *      containing command results. The command completion is all done from
2736
 *      a poll routine that does not have user context. Therefore you cannot
2737
 *      copy back directly into user space, or to the kernel stack of a
2738
 *      process. This routine does not sleep, so can be called from anywhere.
2739
 */
2740
 
2741
static void stli_sendcmd(stlibrd_t *brdp, stliport_t *portp, unsigned long cmd, void *arg, int size, int copyback)
2742
{
2743
        volatile cdkhdr_t       *hdrp;
2744
        volatile cdkctrl_t      *cp;
2745
        volatile unsigned char  *bits;
2746
        unsigned long           flags;
2747
 
2748
#if DEBUG
2749
        printk(KERN_DEBUG "stli_sendcmd(brdp=%x,portp=%x,cmd=%x,arg=%x,size=%d,"
2750
                "copyback=%d)\n", (int) brdp, (int) portp, (int) cmd,
2751
                (int) arg, size, copyback);
2752
#endif
2753
 
2754
        save_flags(flags);
2755
        cli();
2756
 
2757
        if (test_bit(ST_CMDING, &portp->state)) {
2758
                printk(KERN_ERR "STALLION: command already busy, cmd=%x!\n",
2759
                                (int) cmd);
2760
                restore_flags(flags);
2761
                return;
2762
        }
2763
 
2764
        EBRDENABLE(brdp);
2765
        cp = &((volatile cdkasy_t *) EBRDGETMEMPTR(brdp, portp->addr))->ctrl;
2766
        if (size > 0) {
2767
                memcpy((void *) &(cp->args[0]), arg, size);
2768
                if (copyback) {
2769
                        portp->argp = arg;
2770
                        portp->argsize = size;
2771
                }
2772
        }
2773
        cp->status = 0;
2774
        cp->cmd = cmd;
2775
        hdrp = (volatile cdkhdr_t *) EBRDGETMEMPTR(brdp, CDK_CDKADDR);
2776
        bits = ((volatile unsigned char *) hdrp) + brdp->slaveoffset +
2777
                portp->portidx;
2778
        *bits |= portp->portbit;
2779
        set_bit(ST_CMDING, &portp->state);
2780
        EBRDDISABLE(brdp);
2781
        restore_flags(flags);
2782
}
2783
 
2784
/*****************************************************************************/
2785
 
2786
/*
2787
 *      Read data from shared memory. This assumes that the shared memory
2788
 *      is enabled and that interrupts are off. Basically we just empty out
2789
 *      the shared memory buffer into the tty buffer. Must be careful to
2790
 *      handle the case where we fill up the tty buffer, but still have
2791
 *      more chars to unload.
2792
 */
2793
 
2794
static inline void stli_read(stlibrd_t *brdp, stliport_t *portp)
2795
{
2796
        volatile cdkasyrq_t     *rp;
2797
        volatile char           *shbuf;
2798
        struct tty_struct       *tty;
2799
        unsigned int            head, tail, size;
2800
        unsigned int            len, stlen;
2801
 
2802
#if DEBUG
2803
        printk(KERN_DEBUG "stli_read(brdp=%x,portp=%d)\n",
2804
                        (int) brdp, (int) portp);
2805
#endif
2806
 
2807
        if (test_bit(ST_RXSTOP, &portp->state))
2808
                return;
2809
        tty = portp->tty;
2810
        if (tty == (struct tty_struct *) NULL)
2811
                return;
2812
 
2813
        rp = &((volatile cdkasy_t *) EBRDGETMEMPTR(brdp, portp->addr))->rxq;
2814
        head = (unsigned int) rp->head;
2815
        if (head != ((unsigned int) rp->head))
2816
                head = (unsigned int) rp->head;
2817
        tail = (unsigned int) rp->tail;
2818
        size = portp->rxsize;
2819
        if (head >= tail) {
2820
                len = head - tail;
2821
                stlen = len;
2822
        } else {
2823
                len = size - (tail - head);
2824
                stlen = size - tail;
2825
        }
2826
 
2827
        len = MIN(len, (TTY_FLIPBUF_SIZE - tty->flip.count));
2828
        shbuf = (volatile char *) EBRDGETMEMPTR(brdp, portp->rxoffset);
2829
 
2830
        while (len > 0) {
2831
                stlen = MIN(len, stlen);
2832
                memcpy(tty->flip.char_buf_ptr, (char *) (shbuf + tail), stlen);
2833
                memset(tty->flip.flag_buf_ptr, 0, stlen);
2834
                tty->flip.char_buf_ptr += stlen;
2835
                tty->flip.flag_buf_ptr += stlen;
2836
                tty->flip.count += stlen;
2837
 
2838
                len -= stlen;
2839
                tail += stlen;
2840
                if (tail >= size) {
2841
                        tail = 0;
2842
                        stlen = head;
2843
                }
2844
        }
2845
        rp = &((volatile cdkasy_t *) EBRDGETMEMPTR(brdp, portp->addr))->rxq;
2846
        rp->tail = tail;
2847
 
2848
        if (head != tail)
2849
                set_bit(ST_RXING, &portp->state);
2850
 
2851
        tty_schedule_flip(tty);
2852
}
2853
 
2854
/*****************************************************************************/
2855
 
2856
/*
2857
 *      Set up and carry out any delayed commands. There is only a small set
2858
 *      of slave commands that can be done "off-level". So it is not too
2859
 *      difficult to deal with them here.
2860
 */
2861
 
2862
static inline void stli_dodelaycmd(stliport_t *portp, volatile cdkctrl_t *cp)
2863
{
2864
        int     cmd;
2865
 
2866
        if (test_bit(ST_DOSIGS, &portp->state)) {
2867
                if (test_bit(ST_DOFLUSHTX, &portp->state) &&
2868
                    test_bit(ST_DOFLUSHRX, &portp->state))
2869
                        cmd = A_SETSIGNALSF;
2870
                else if (test_bit(ST_DOFLUSHTX, &portp->state))
2871
                        cmd = A_SETSIGNALSFTX;
2872
                else if (test_bit(ST_DOFLUSHRX, &portp->state))
2873
                        cmd = A_SETSIGNALSFRX;
2874
                else
2875
                        cmd = A_SETSIGNALS;
2876
                clear_bit(ST_DOFLUSHTX, &portp->state);
2877
                clear_bit(ST_DOFLUSHRX, &portp->state);
2878
                clear_bit(ST_DOSIGS, &portp->state);
2879
                memcpy((void *) &(cp->args[0]), (void *) &portp->asig,
2880
                        sizeof(asysigs_t));
2881
                cp->status = 0;
2882
                cp->cmd = cmd;
2883
                set_bit(ST_CMDING, &portp->state);
2884
        } else if (test_bit(ST_DOFLUSHTX, &portp->state) ||
2885
            test_bit(ST_DOFLUSHRX, &portp->state)) {
2886
                cmd = ((test_bit(ST_DOFLUSHTX, &portp->state)) ? FLUSHTX : 0);
2887
                cmd |= ((test_bit(ST_DOFLUSHRX, &portp->state)) ? FLUSHRX : 0);
2888
                clear_bit(ST_DOFLUSHTX, &portp->state);
2889
                clear_bit(ST_DOFLUSHRX, &portp->state);
2890
                memcpy((void *) &(cp->args[0]), (void *) &cmd, sizeof(int));
2891
                cp->status = 0;
2892
                cp->cmd = A_FLUSH;
2893
                set_bit(ST_CMDING, &portp->state);
2894
        }
2895
}
2896
 
2897
/*****************************************************************************/
2898
 
2899
/*
2900
 *      Host command service checking. This handles commands or messages
2901
 *      coming from the slave to the host. Must have board shared memory
2902
 *      enabled and interrupts off when called. Notice that by servicing the
2903
 *      read data last we don't need to change the shared memory pointer
2904
 *      during processing (which is a slow IO operation).
2905
 *      Return value indicates if this port is still awaiting actions from
2906
 *      the slave (like open, command, or even TX data being sent). If 0
2907
 *      then port is still busy, otherwise no longer busy.
2908
 */
2909
 
2910
static inline int stli_hostcmd(stlibrd_t *brdp, stliport_t *portp)
2911
{
2912
        volatile cdkasy_t       *ap;
2913
        volatile cdkctrl_t      *cp;
2914
        struct tty_struct       *tty;
2915
        asynotify_t             nt;
2916
        unsigned long           oldsigs;
2917
        int                     rc, donerx;
2918
 
2919
#if DEBUG
2920
        printk(KERN_DEBUG "stli_hostcmd(brdp=%x,channr=%d)\n",
2921
                        (int) brdp, channr);
2922
#endif
2923
 
2924
        ap = (volatile cdkasy_t *) EBRDGETMEMPTR(brdp, portp->addr);
2925
        cp = &ap->ctrl;
2926
 
2927
/*
2928
 *      Check if we are waiting for an open completion message.
2929
 */
2930
        if (test_bit(ST_OPENING, &portp->state)) {
2931
                rc = (int) cp->openarg;
2932
                if ((cp->open == 0) && (rc != 0)) {
2933
                        if (rc > 0)
2934
                                rc--;
2935
                        cp->openarg = 0;
2936
                        portp->rc = rc;
2937
                        clear_bit(ST_OPENING, &portp->state);
2938
                        wake_up_interruptible(&portp->raw_wait);
2939
                }
2940
        }
2941
 
2942
/*
2943
 *      Check if we are waiting for a close completion message.
2944
 */
2945
        if (test_bit(ST_CLOSING, &portp->state)) {
2946
                rc = (int) cp->closearg;
2947
                if ((cp->close == 0) && (rc != 0)) {
2948
                        if (rc > 0)
2949
                                rc--;
2950
                        cp->closearg = 0;
2951
                        portp->rc = rc;
2952
                        clear_bit(ST_CLOSING, &portp->state);
2953
                        wake_up_interruptible(&portp->raw_wait);
2954
                }
2955
        }
2956
 
2957
/*
2958
 *      Check if we are waiting for a command completion message. We may
2959
 *      need to copy out the command results associated with this command.
2960
 */
2961
        if (test_bit(ST_CMDING, &portp->state)) {
2962
                rc = cp->status;
2963
                if ((cp->cmd == 0) && (rc != 0)) {
2964
                        if (rc > 0)
2965
                                rc--;
2966
                        if (portp->argp != (void *) NULL) {
2967
                                memcpy(portp->argp, (void *) &(cp->args[0]),
2968
                                        portp->argsize);
2969
                                portp->argp = (void *) NULL;
2970
                        }
2971
                        cp->status = 0;
2972
                        portp->rc = rc;
2973
                        clear_bit(ST_CMDING, &portp->state);
2974
                        stli_dodelaycmd(portp, cp);
2975
                        wake_up_interruptible(&portp->raw_wait);
2976
                }
2977
        }
2978
 
2979
/*
2980
 *      Check for any notification messages ready. This includes lots of
2981
 *      different types of events - RX chars ready, RX break received,
2982
 *      TX data low or empty in the slave, modem signals changed state.
2983
 */
2984
        donerx = 0;
2985
 
2986
        if (ap->notify) {
2987
                nt = ap->changed;
2988
                ap->notify = 0;
2989
                tty = portp->tty;
2990
 
2991
                if (nt.signal & SG_DCD) {
2992
                        oldsigs = portp->sigs;
2993
                        portp->sigs = stli_mktiocm(nt.sigvalue);
2994
                        clear_bit(ST_GETSIGS, &portp->state);
2995
                        if ((portp->sigs & TIOCM_CD) &&
2996
                            ((oldsigs & TIOCM_CD) == 0))
2997
                                wake_up_interruptible(&portp->open_wait);
2998
                        if ((oldsigs & TIOCM_CD) &&
2999
                            ((portp->sigs & TIOCM_CD) == 0)) {
3000
                                if (portp->flags & ASYNC_CHECK_CD) {
3001
                                        if (! ((portp->flags & ASYNC_CALLOUT_ACTIVE) &&
3002
                                            (portp->flags & ASYNC_CALLOUT_NOHUP))) {
3003
                                                if (tty != (struct tty_struct *) NULL) {
3004
                                                        MOD_INC_USE_COUNT;
3005
                                                        if (schedule_task(&portp->tqhangup) == 0)
3006
                                                                MOD_DEC_USE_COUNT;
3007
                                                }
3008
                                        }
3009
                                }
3010
                        }
3011
                }
3012
 
3013
                if (nt.data & DT_TXEMPTY)
3014
                        clear_bit(ST_TXBUSY, &portp->state);
3015
                if (nt.data & (DT_TXEMPTY | DT_TXLOW)) {
3016
                        if (tty != (struct tty_struct *) NULL) {
3017
                                if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
3018
                                    tty->ldisc.write_wakeup) {
3019
                                        (tty->ldisc.write_wakeup)(tty);
3020
                                        EBRDENABLE(brdp);
3021
                                }
3022
                                wake_up_interruptible(&tty->write_wait);
3023
                        }
3024
                }
3025
 
3026
                if ((nt.data & DT_RXBREAK) && (portp->rxmarkmsk & BRKINT)) {
3027
                        if (tty != (struct tty_struct *) NULL) {
3028
                                if (tty->flip.count < TTY_FLIPBUF_SIZE) {
3029
                                        tty->flip.count++;
3030
                                        *tty->flip.flag_buf_ptr++ = TTY_BREAK;
3031
                                        *tty->flip.char_buf_ptr++ = 0;
3032
                                        if (portp->flags & ASYNC_SAK) {
3033
                                                do_SAK(tty);
3034
                                                EBRDENABLE(brdp);
3035
                                        }
3036
                                        tty_schedule_flip(tty);
3037
                                }
3038
                        }
3039
                }
3040
 
3041
                if (nt.data & DT_RXBUSY) {
3042
                        donerx++;
3043
                        stli_read(brdp, portp);
3044
                }
3045
        }
3046
 
3047
/*
3048
 *      It might seem odd that we are checking for more RX chars here.
3049
 *      But, we need to handle the case where the tty buffer was previously
3050
 *      filled, but we had more characters to pass up. The slave will not
3051
 *      send any more RX notify messages until the RX buffer has been emptied.
3052
 *      But it will leave the service bits on (since the buffer is not empty).
3053
 *      So from here we can try to process more RX chars.
3054
 */
3055
        if ((!donerx) && test_bit(ST_RXING, &portp->state)) {
3056
                clear_bit(ST_RXING, &portp->state);
3057
                stli_read(brdp, portp);
3058
        }
3059
 
3060
        return((test_bit(ST_OPENING, &portp->state) ||
3061
                test_bit(ST_CLOSING, &portp->state) ||
3062
                test_bit(ST_CMDING, &portp->state) ||
3063
                test_bit(ST_TXBUSY, &portp->state) ||
3064
                test_bit(ST_RXING, &portp->state)) ? 0 : 1);
3065
}
3066
 
3067
/*****************************************************************************/
3068
 
3069
/*
3070
 *      Service all ports on a particular board. Assumes that the boards
3071
 *      shared memory is enabled, and that the page pointer is pointed
3072
 *      at the cdk header structure.
3073
 */
3074
 
3075
static inline void stli_brdpoll(stlibrd_t *brdp, volatile cdkhdr_t *hdrp)
3076
{
3077
        stliport_t      *portp;
3078
        unsigned char   hostbits[(STL_MAXCHANS / 8) + 1];
3079
        unsigned char   slavebits[(STL_MAXCHANS / 8) + 1];
3080
        unsigned char   *slavep;
3081
        int             bitpos, bitat, bitsize;
3082
        int             channr, nrdevs, slavebitchange;
3083
 
3084
        bitsize = brdp->bitsize;
3085
        nrdevs = brdp->nrdevs;
3086
 
3087
/*
3088
 *      Check if slave wants any service. Basically we try to do as
3089
 *      little work as possible here. There are 2 levels of service
3090
 *      bits. So if there is nothing to do we bail early. We check
3091
 *      8 service bits at a time in the inner loop, so we can bypass
3092
 *      the lot if none of them want service.
3093
 */
3094
        memcpy(&hostbits[0], (((unsigned char *) hdrp) + brdp->hostoffset),
3095
                bitsize);
3096
 
3097
        memset(&slavebits[0], 0, bitsize);
3098
        slavebitchange = 0;
3099
 
3100
        for (bitpos = 0; (bitpos < bitsize); bitpos++) {
3101
                if (hostbits[bitpos] == 0)
3102
                        continue;
3103
                channr = bitpos * 8;
3104
                for (bitat = 0x1; (channr < nrdevs); channr++, bitat <<= 1) {
3105
                        if (hostbits[bitpos] & bitat) {
3106
                                portp = brdp->ports[(channr - 1)];
3107
                                if (stli_hostcmd(brdp, portp)) {
3108
                                        slavebitchange++;
3109
                                        slavebits[bitpos] |= bitat;
3110
                                }
3111
                        }
3112
                }
3113
        }
3114
 
3115
/*
3116
 *      If any of the ports are no longer busy then update them in the
3117
 *      slave request bits. We need to do this after, since a host port
3118
 *      service may initiate more slave requests.
3119
 */
3120
        if (slavebitchange) {
3121
                hdrp = (volatile cdkhdr_t *) EBRDGETMEMPTR(brdp, CDK_CDKADDR);
3122
                slavep = ((unsigned char *) hdrp) + brdp->slaveoffset;
3123
                for (bitpos = 0; (bitpos < bitsize); bitpos++) {
3124
                        if (slavebits[bitpos])
3125
                                slavep[bitpos] &= ~slavebits[bitpos];
3126
                }
3127
        }
3128
}
3129
 
3130
/*****************************************************************************/
3131
 
3132
/*
3133
 *      Driver poll routine. This routine polls the boards in use and passes
3134
 *      messages back up to host when necessary. This is actually very
3135
 *      CPU efficient, since we will always have the kernel poll clock, it
3136
 *      adds only a few cycles when idle (since board service can be
3137
 *      determined very easily), but when loaded generates no interrupts
3138
 *      (with their expensive associated context change).
3139
 */
3140
 
3141
static void stli_poll(unsigned long arg)
3142
{
3143
        volatile cdkhdr_t       *hdrp;
3144
        stlibrd_t               *brdp;
3145
        int                     brdnr;
3146
 
3147
        stli_timerlist.expires = STLI_TIMEOUT;
3148
        add_timer(&stli_timerlist);
3149
 
3150
/*
3151
 *      Check each board and do any servicing required.
3152
 */
3153
        for (brdnr = 0; (brdnr < stli_nrbrds); brdnr++) {
3154
                brdp = stli_brds[brdnr];
3155
                if (brdp == (stlibrd_t *) NULL)
3156
                        continue;
3157
                if ((brdp->state & BST_STARTED) == 0)
3158
                        continue;
3159
 
3160
                EBRDENABLE(brdp);
3161
                hdrp = (volatile cdkhdr_t *) EBRDGETMEMPTR(brdp, CDK_CDKADDR);
3162
                if (hdrp->hostreq)
3163
                        stli_brdpoll(brdp, hdrp);
3164
                EBRDDISABLE(brdp);
3165
        }
3166
}
3167
 
3168
/*****************************************************************************/
3169
 
3170
/*
3171
 *      Translate the termios settings into the port setting structure of
3172
 *      the slave.
3173
 */
3174
 
3175
static void stli_mkasyport(stliport_t *portp, asyport_t *pp, struct termios *tiosp)
3176
{
3177
#if DEBUG
3178
        printk(KERN_DEBUG "stli_mkasyport(portp=%x,pp=%x,tiosp=%d)\n",
3179
                (int) portp, (int) pp, (int) tiosp);
3180
#endif
3181
 
3182
        memset(pp, 0, sizeof(asyport_t));
3183
 
3184
/*
3185
 *      Start of by setting the baud, char size, parity and stop bit info.
3186
 */
3187
        pp->baudout = tiosp->c_cflag & CBAUD;
3188
        if (pp->baudout & CBAUDEX) {
3189
                pp->baudout &= ~CBAUDEX;
3190
                if ((pp->baudout < 1) || (pp->baudout > 4))
3191
                        tiosp->c_cflag &= ~CBAUDEX;
3192
                else
3193
                        pp->baudout += 15;
3194
        }
3195
        pp->baudout = stli_baudrates[pp->baudout];
3196
        if ((tiosp->c_cflag & CBAUD) == B38400) {
3197
                if ((portp->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
3198
                        pp->baudout = 57600;
3199
                else if ((portp->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
3200
                        pp->baudout = 115200;
3201
                else if ((portp->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
3202
                        pp->baudout = 230400;
3203
                else if ((portp->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
3204
                        pp->baudout = 460800;
3205
                else if ((portp->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST)
3206
                        pp->baudout = (portp->baud_base / portp->custom_divisor);
3207
        }
3208
        if (pp->baudout > STL_MAXBAUD)
3209
                pp->baudout = STL_MAXBAUD;
3210
        pp->baudin = pp->baudout;
3211
 
3212
        switch (tiosp->c_cflag & CSIZE) {
3213
        case CS5:
3214
                pp->csize = 5;
3215
                break;
3216
        case CS6:
3217
                pp->csize = 6;
3218
                break;
3219
        case CS7:
3220
                pp->csize = 7;
3221
                break;
3222
        default:
3223
                pp->csize = 8;
3224
                break;
3225
        }
3226
 
3227
        if (tiosp->c_cflag & CSTOPB)
3228
                pp->stopbs = PT_STOP2;
3229
        else
3230
                pp->stopbs = PT_STOP1;
3231
 
3232
        if (tiosp->c_cflag & PARENB) {
3233
                if (tiosp->c_cflag & PARODD)
3234
                        pp->parity = PT_ODDPARITY;
3235
                else
3236
                        pp->parity = PT_EVENPARITY;
3237
        } else {
3238
                pp->parity = PT_NOPARITY;
3239
        }
3240
 
3241
/*
3242
 *      Set up any flow control options enabled.
3243
 */
3244
        if (tiosp->c_iflag & IXON) {
3245
                pp->flow |= F_IXON;
3246
                if (tiosp->c_iflag & IXANY)
3247
                        pp->flow |= F_IXANY;
3248
        }
3249
        if (tiosp->c_cflag & CRTSCTS)
3250
                pp->flow |= (F_RTSFLOW | F_CTSFLOW);
3251
 
3252
        pp->startin = tiosp->c_cc[VSTART];
3253
        pp->stopin = tiosp->c_cc[VSTOP];
3254
        pp->startout = tiosp->c_cc[VSTART];
3255
        pp->stopout = tiosp->c_cc[VSTOP];
3256
 
3257
/*
3258
 *      Set up the RX char marking mask with those RX error types we must
3259
 *      catch. We can get the slave to help us out a little here, it will
3260
 *      ignore parity errors and breaks for us, and mark parity errors in
3261
 *      the data stream.
3262
 */
3263
        if (tiosp->c_iflag & IGNPAR)
3264
                pp->iflag |= FI_IGNRXERRS;
3265
        if (tiosp->c_iflag & IGNBRK)
3266
                pp->iflag |= FI_IGNBREAK;
3267
 
3268
        portp->rxmarkmsk = 0;
3269
        if (tiosp->c_iflag & (INPCK | PARMRK))
3270
                pp->iflag |= FI_1MARKRXERRS;
3271
        if (tiosp->c_iflag & BRKINT)
3272
                portp->rxmarkmsk |= BRKINT;
3273
 
3274
/*
3275
 *      Set up clocal processing as required.
3276
 */
3277
        if (tiosp->c_cflag & CLOCAL)
3278
                portp->flags &= ~ASYNC_CHECK_CD;
3279
        else
3280
                portp->flags |= ASYNC_CHECK_CD;
3281
 
3282
/*
3283
 *      Transfer any persistent flags into the asyport structure.
3284
 */
3285
        pp->pflag = (portp->pflag & 0xffff);
3286
        pp->vmin = (portp->pflag & P_RXIMIN) ? 1 : 0;
3287
        pp->vtime = (portp->pflag & P_RXITIME) ? 1 : 0;
3288
        pp->cc[1] = (portp->pflag & P_RXTHOLD) ? 1 : 0;
3289
}
3290
 
3291
/*****************************************************************************/
3292
 
3293
/*
3294
 *      Construct a slave signals structure for setting the DTR and RTS
3295
 *      signals as specified.
3296
 */
3297
 
3298
static void stli_mkasysigs(asysigs_t *sp, int dtr, int rts)
3299
{
3300
#if DEBUG
3301
        printk(KERN_DEBUG "stli_mkasysigs(sp=%x,dtr=%d,rts=%d)\n",
3302
                        (int) sp, dtr, rts);
3303
#endif
3304
 
3305
        memset(sp, 0, sizeof(asysigs_t));
3306
        if (dtr >= 0) {
3307
                sp->signal |= SG_DTR;
3308
                sp->sigvalue |= ((dtr > 0) ? SG_DTR : 0);
3309
        }
3310
        if (rts >= 0) {
3311
                sp->signal |= SG_RTS;
3312
                sp->sigvalue |= ((rts > 0) ? SG_RTS : 0);
3313
        }
3314
}
3315
 
3316
/*****************************************************************************/
3317
 
3318
/*
3319
 *      Convert the signals returned from the slave into a local TIOCM type
3320
 *      signals value. We keep them locally in TIOCM format.
3321
 */
3322
 
3323
static long stli_mktiocm(unsigned long sigvalue)
3324
{
3325
        long    tiocm;
3326
 
3327
#if DEBUG
3328
        printk(KERN_DEBUG "stli_mktiocm(sigvalue=%x)\n", (int) sigvalue);
3329
#endif
3330
 
3331
        tiocm = 0;
3332
        tiocm |= ((sigvalue & SG_DCD) ? TIOCM_CD : 0);
3333
        tiocm |= ((sigvalue & SG_CTS) ? TIOCM_CTS : 0);
3334
        tiocm |= ((sigvalue & SG_RI) ? TIOCM_RI : 0);
3335
        tiocm |= ((sigvalue & SG_DSR) ? TIOCM_DSR : 0);
3336
        tiocm |= ((sigvalue & SG_DTR) ? TIOCM_DTR : 0);
3337
        tiocm |= ((sigvalue & SG_RTS) ? TIOCM_RTS : 0);
3338
        return(tiocm);
3339
}
3340
 
3341
/*****************************************************************************/
3342
 
3343
/*
3344
 *      All panels and ports actually attached have been worked out. All
3345
 *      we need to do here is set up the appropriate per port data structures.
3346
 */
3347
 
3348
static inline int stli_initports(stlibrd_t *brdp)
3349
{
3350
        stliport_t      *portp;
3351
        int             i, panelnr, panelport;
3352
 
3353
#if DEBUG
3354
        printk(KERN_DEBUG "stli_initports(brdp=%x)\n", (int) brdp);
3355
#endif
3356
 
3357
        for (i = 0, panelnr = 0, panelport = 0; (i < brdp->nrports); i++) {
3358
                portp = (stliport_t *) stli_memalloc(sizeof(stliport_t));
3359
                if (portp == (stliport_t *) NULL) {
3360
                        printk("STALLION: failed to allocate port structure\n");
3361
                        continue;
3362
                }
3363
 
3364
                memset(portp, 0, sizeof(stliport_t));
3365
                portp->magic = STLI_PORTMAGIC;
3366
                portp->portnr = i;
3367
                portp->brdnr = brdp->brdnr;
3368
                portp->panelnr = panelnr;
3369
                portp->baud_base = STL_BAUDBASE;
3370
                portp->close_delay = STL_CLOSEDELAY;
3371
                portp->closing_wait = 30 * HZ;
3372
                portp->tqhangup.routine = stli_dohangup;
3373
                portp->tqhangup.data = portp;
3374
                init_waitqueue_head(&portp->open_wait);
3375
                init_waitqueue_head(&portp->close_wait);
3376
                init_waitqueue_head(&portp->raw_wait);
3377
                portp->normaltermios = stli_deftermios;
3378
                portp->callouttermios = stli_deftermios;
3379
                panelport++;
3380
                if (panelport >= brdp->panels[panelnr]) {
3381
                        panelport = 0;
3382
                        panelnr++;
3383
                }
3384
                brdp->ports[i] = portp;
3385
        }
3386
 
3387
        return(0);
3388
}
3389
 
3390
/*****************************************************************************/
3391
 
3392
/*
3393
 *      All the following routines are board specific hardware operations.
3394
 */
3395
 
3396
static void stli_ecpinit(stlibrd_t *brdp)
3397
{
3398
        unsigned long   memconf;
3399
 
3400
#if DEBUG
3401
        printk(KERN_DEBUG "stli_ecpinit(brdp=%d)\n", (int) brdp);
3402
#endif
3403
 
3404
        outb(ECP_ATSTOP, (brdp->iobase + ECP_ATCONFR));
3405
        udelay(10);
3406
        outb(ECP_ATDISABLE, (brdp->iobase + ECP_ATCONFR));
3407
        udelay(100);
3408
 
3409
        memconf = (brdp->memaddr & ECP_ATADDRMASK) >> ECP_ATADDRSHFT;
3410
        outb(memconf, (brdp->iobase + ECP_ATMEMAR));
3411
}
3412
 
3413
/*****************************************************************************/
3414
 
3415
static void stli_ecpenable(stlibrd_t *brdp)
3416
{
3417
#if DEBUG
3418
        printk(KERN_DEBUG "stli_ecpenable(brdp=%x)\n", (int) brdp);
3419
#endif
3420
        outb(ECP_ATENABLE, (brdp->iobase + ECP_ATCONFR));
3421
}
3422
 
3423
/*****************************************************************************/
3424
 
3425
static void stli_ecpdisable(stlibrd_t *brdp)
3426
{
3427
#if DEBUG
3428
        printk(KERN_DEBUG "stli_ecpdisable(brdp=%x)\n", (int) brdp);
3429
#endif
3430
        outb(ECP_ATDISABLE, (brdp->iobase + ECP_ATCONFR));
3431
}
3432
 
3433
/*****************************************************************************/
3434
 
3435
static char *stli_ecpgetmemptr(stlibrd_t *brdp, unsigned long offset, int line)
3436
{
3437
        void            *ptr;
3438
        unsigned char   val;
3439
 
3440
#if DEBUG
3441
        printk(KERN_DEBUG "stli_ecpgetmemptr(brdp=%x,offset=%x)\n", (int) brdp,
3442
                (int) offset);
3443
#endif
3444
 
3445
        if (offset > brdp->memsize) {
3446
                printk(KERN_ERR "STALLION: shared memory pointer=%x out of "
3447
                                "range at line=%d(%d), brd=%d\n",
3448
                        (int) offset, line, __LINE__, brdp->brdnr);
3449
                ptr = 0;
3450
                val = 0;
3451
        } else {
3452
                ptr = brdp->membase + (offset % ECP_ATPAGESIZE);
3453
                val = (unsigned char) (offset / ECP_ATPAGESIZE);
3454
        }
3455
        outb(val, (brdp->iobase + ECP_ATMEMPR));
3456
        return(ptr);
3457
}
3458
 
3459
/*****************************************************************************/
3460
 
3461
static void stli_ecpreset(stlibrd_t *brdp)
3462
{
3463
#if DEBUG
3464
        printk(KERN_DEBUG "stli_ecpreset(brdp=%x)\n", (int) brdp);
3465
#endif
3466
 
3467
        outb(ECP_ATSTOP, (brdp->iobase + ECP_ATCONFR));
3468
        udelay(10);
3469
        outb(ECP_ATDISABLE, (brdp->iobase + ECP_ATCONFR));
3470
        udelay(500);
3471
}
3472
 
3473
/*****************************************************************************/
3474
 
3475
static void stli_ecpintr(stlibrd_t *brdp)
3476
{
3477
#if DEBUG
3478
        printk(KERN_DEBUG "stli_ecpintr(brdp=%x)\n", (int) brdp);
3479
#endif
3480
        outb(0x1, brdp->iobase);
3481
}
3482
 
3483
/*****************************************************************************/
3484
 
3485
/*
3486
 *      The following set of functions act on ECP EISA boards.
3487
 */
3488
 
3489
static void stli_ecpeiinit(stlibrd_t *brdp)
3490
{
3491
        unsigned long   memconf;
3492
 
3493
#if DEBUG
3494
        printk(KERN_DEBUG "stli_ecpeiinit(brdp=%x)\n", (int) brdp);
3495
#endif
3496
 
3497
        outb(0x1, (brdp->iobase + ECP_EIBRDENAB));
3498
        outb(ECP_EISTOP, (brdp->iobase + ECP_EICONFR));
3499
        udelay(10);
3500
        outb(ECP_EIDISABLE, (brdp->iobase + ECP_EICONFR));
3501
        udelay(500);
3502
 
3503
        memconf = (brdp->memaddr & ECP_EIADDRMASKL) >> ECP_EIADDRSHFTL;
3504
        outb(memconf, (brdp->iobase + ECP_EIMEMARL));
3505
        memconf = (brdp->memaddr & ECP_EIADDRMASKH) >> ECP_EIADDRSHFTH;
3506
        outb(memconf, (brdp->iobase + ECP_EIMEMARH));
3507
}
3508
 
3509
/*****************************************************************************/
3510
 
3511
static void stli_ecpeienable(stlibrd_t *brdp)
3512
{
3513
        outb(ECP_EIENABLE, (brdp->iobase + ECP_EICONFR));
3514
}
3515
 
3516
/*****************************************************************************/
3517
 
3518
static void stli_ecpeidisable(stlibrd_t *brdp)
3519
{
3520
        outb(ECP_EIDISABLE, (brdp->iobase + ECP_EICONFR));
3521
}
3522
 
3523
/*****************************************************************************/
3524
 
3525
static char *stli_ecpeigetmemptr(stlibrd_t *brdp, unsigned long offset, int line)
3526
{
3527
        void            *ptr;
3528
        unsigned char   val;
3529
 
3530
#if DEBUG
3531
        printk(KERN_DEBUG "stli_ecpeigetmemptr(brdp=%x,offset=%x,line=%d)\n",
3532
                (int) brdp, (int) offset, line);
3533
#endif
3534
 
3535
        if (offset > brdp->memsize) {
3536
                printk(KERN_ERR "STALLION: shared memory pointer=%x out of "
3537
                                "range at line=%d(%d), brd=%d\n",
3538
                        (int) offset, line, __LINE__, brdp->brdnr);
3539
                ptr = 0;
3540
                val = 0;
3541
        } else {
3542
                ptr = brdp->membase + (offset % ECP_EIPAGESIZE);
3543
                if (offset < ECP_EIPAGESIZE)
3544
                        val = ECP_EIENABLE;
3545
                else
3546
                        val = ECP_EIENABLE | 0x40;
3547
        }
3548
        outb(val, (brdp->iobase + ECP_EICONFR));
3549
        return(ptr);
3550
}
3551
 
3552
/*****************************************************************************/
3553
 
3554
static void stli_ecpeireset(stlibrd_t *brdp)
3555
{
3556
        outb(ECP_EISTOP, (brdp->iobase + ECP_EICONFR));
3557
        udelay(10);
3558
        outb(ECP_EIDISABLE, (brdp->iobase + ECP_EICONFR));
3559
        udelay(500);
3560
}
3561
 
3562
/*****************************************************************************/
3563
 
3564
/*
3565
 *      The following set of functions act on ECP MCA boards.
3566
 */
3567
 
3568
static void stli_ecpmcenable(stlibrd_t *brdp)
3569
{
3570
        outb(ECP_MCENABLE, (brdp->iobase + ECP_MCCONFR));
3571
}
3572
 
3573
/*****************************************************************************/
3574
 
3575
static void stli_ecpmcdisable(stlibrd_t *brdp)
3576
{
3577
        outb(ECP_MCDISABLE, (brdp->iobase + ECP_MCCONFR));
3578
}
3579
 
3580
/*****************************************************************************/
3581
 
3582
static char *stli_ecpmcgetmemptr(stlibrd_t *brdp, unsigned long offset, int line)
3583
{
3584
        void            *ptr;
3585
        unsigned char   val;
3586
 
3587
        if (offset > brdp->memsize) {
3588
                printk(KERN_ERR "STALLION: shared memory pointer=%x out of "
3589
                                "range at line=%d(%d), brd=%d\n",
3590
                        (int) offset, line, __LINE__, brdp->brdnr);
3591
                ptr = 0;
3592
                val = 0;
3593
        } else {
3594
                ptr = brdp->membase + (offset % ECP_MCPAGESIZE);
3595
                val = ((unsigned char) (offset / ECP_MCPAGESIZE)) | ECP_MCENABLE;
3596
        }
3597
        outb(val, (brdp->iobase + ECP_MCCONFR));
3598
        return(ptr);
3599
}
3600
 
3601
/*****************************************************************************/
3602
 
3603
static void stli_ecpmcreset(stlibrd_t *brdp)
3604
{
3605
        outb(ECP_MCSTOP, (brdp->iobase + ECP_MCCONFR));
3606
        udelay(10);
3607
        outb(ECP_MCDISABLE, (brdp->iobase + ECP_MCCONFR));
3608
        udelay(500);
3609
}
3610
 
3611
/*****************************************************************************/
3612
 
3613
/*
3614
 *      The following set of functions act on ECP PCI boards.
3615
 */
3616
 
3617
static void stli_ecppciinit(stlibrd_t *brdp)
3618
{
3619
#if DEBUG
3620
        printk(KERN_DEBUG "stli_ecppciinit(brdp=%x)\n", (int) brdp);
3621
#endif
3622
 
3623
        outb(ECP_PCISTOP, (brdp->iobase + ECP_PCICONFR));
3624
        udelay(10);
3625
        outb(0, (brdp->iobase + ECP_PCICONFR));
3626
        udelay(500);
3627
}
3628
 
3629
/*****************************************************************************/
3630
 
3631
static char *stli_ecppcigetmemptr(stlibrd_t *brdp, unsigned long offset, int line)
3632
{
3633
        void            *ptr;
3634
        unsigned char   val;
3635
 
3636
#if DEBUG
3637
        printk(KERN_DEBUG "stli_ecppcigetmemptr(brdp=%x,offset=%x,line=%d)\n",
3638
                (int) brdp, (int) offset, line);
3639
#endif
3640
 
3641
        if (offset > brdp->memsize) {
3642
                printk(KERN_ERR "STALLION: shared memory pointer=%x out of "
3643
                                "range at line=%d(%d), board=%d\n",
3644
                                (int) offset, line, __LINE__, brdp->brdnr);
3645
                ptr = 0;
3646
                val = 0;
3647
        } else {
3648
                ptr = brdp->membase + (offset % ECP_PCIPAGESIZE);
3649
                val = (offset / ECP_PCIPAGESIZE) << 1;
3650
        }
3651
        outb(val, (brdp->iobase + ECP_PCICONFR));
3652
        return(ptr);
3653
}
3654
 
3655
/*****************************************************************************/
3656
 
3657
static void stli_ecppcireset(stlibrd_t *brdp)
3658
{
3659
        outb(ECP_PCISTOP, (brdp->iobase + ECP_PCICONFR));
3660
        udelay(10);
3661
        outb(0, (brdp->iobase + ECP_PCICONFR));
3662
        udelay(500);
3663
}
3664
 
3665
/*****************************************************************************/
3666
 
3667
/*
3668
 *      The following routines act on ONboards.
3669
 */
3670
 
3671
static void stli_onbinit(stlibrd_t *brdp)
3672
{
3673
        unsigned long   memconf;
3674
 
3675
#if DEBUG
3676
        printk(KERN_DEBUG "stli_onbinit(brdp=%d)\n", (int) brdp);
3677
#endif
3678
 
3679
        outb(ONB_ATSTOP, (brdp->iobase + ONB_ATCONFR));
3680
        udelay(10);
3681
        outb(ONB_ATDISABLE, (brdp->iobase + ONB_ATCONFR));
3682
        mdelay(1000);
3683
 
3684
        memconf = (brdp->memaddr & ONB_ATADDRMASK) >> ONB_ATADDRSHFT;
3685
        outb(memconf, (brdp->iobase + ONB_ATMEMAR));
3686
        outb(0x1, brdp->iobase);
3687
        mdelay(1);
3688
}
3689
 
3690
/*****************************************************************************/
3691
 
3692
static void stli_onbenable(stlibrd_t *brdp)
3693
{
3694
#if DEBUG
3695
        printk(KERN_DEBUG "stli_onbenable(brdp=%x)\n", (int) brdp);
3696
#endif
3697
        outb((brdp->enabval | ONB_ATENABLE), (brdp->iobase + ONB_ATCONFR));
3698
}
3699
 
3700
/*****************************************************************************/
3701
 
3702
static void stli_onbdisable(stlibrd_t *brdp)
3703
{
3704
#if DEBUG
3705
        printk(KERN_DEBUG "stli_onbdisable(brdp=%x)\n", (int) brdp);
3706
#endif
3707
        outb((brdp->enabval | ONB_ATDISABLE), (brdp->iobase + ONB_ATCONFR));
3708
}
3709
 
3710
/*****************************************************************************/
3711
 
3712
static char *stli_onbgetmemptr(stlibrd_t *brdp, unsigned long offset, int line)
3713
{
3714
        void    *ptr;
3715
 
3716
#if DEBUG
3717
        printk(KERN_DEBUG "stli_onbgetmemptr(brdp=%x,offset=%x)\n", (int) brdp,
3718
                (int) offset);
3719
#endif
3720
 
3721
        if (offset > brdp->memsize) {
3722
                printk(KERN_ERR "STALLION: shared memory pointer=%x out of "
3723
                                "range at line=%d(%d), brd=%d\n",
3724
                                (int) offset, line, __LINE__, brdp->brdnr);
3725
                ptr = 0;
3726
        } else {
3727
                ptr = brdp->membase + (offset % ONB_ATPAGESIZE);
3728
        }
3729
        return(ptr);
3730
}
3731
 
3732
/*****************************************************************************/
3733
 
3734
static void stli_onbreset(stlibrd_t *brdp)
3735
{
3736
 
3737
#if DEBUG
3738
        printk(KERN_DEBUG "stli_onbreset(brdp=%x)\n", (int) brdp);
3739
#endif
3740
 
3741
        outb(ONB_ATSTOP, (brdp->iobase + ONB_ATCONFR));
3742
        udelay(10);
3743
        outb(ONB_ATDISABLE, (brdp->iobase + ONB_ATCONFR));
3744
        mdelay(1000);
3745
}
3746
 
3747
/*****************************************************************************/
3748
 
3749
/*
3750
 *      The following routines act on ONboard EISA.
3751
 */
3752
 
3753
static void stli_onbeinit(stlibrd_t *brdp)
3754
{
3755
        unsigned long   memconf;
3756
 
3757
#if DEBUG
3758
        printk(KERN_DEBUG "stli_onbeinit(brdp=%d)\n", (int) brdp);
3759
#endif
3760
 
3761
        outb(0x1, (brdp->iobase + ONB_EIBRDENAB));
3762
        outb(ONB_EISTOP, (brdp->iobase + ONB_EICONFR));
3763
        udelay(10);
3764
        outb(ONB_EIDISABLE, (brdp->iobase + ONB_EICONFR));
3765
        mdelay(1000);
3766
 
3767
        memconf = (brdp->memaddr & ONB_EIADDRMASKL) >> ONB_EIADDRSHFTL;
3768
        outb(memconf, (brdp->iobase + ONB_EIMEMARL));
3769
        memconf = (brdp->memaddr & ONB_EIADDRMASKH) >> ONB_EIADDRSHFTH;
3770
        outb(memconf, (brdp->iobase + ONB_EIMEMARH));
3771
        outb(0x1, brdp->iobase);
3772
        mdelay(1);
3773
}
3774
 
3775
/*****************************************************************************/
3776
 
3777
static void stli_onbeenable(stlibrd_t *brdp)
3778
{
3779
#if DEBUG
3780
        printk(KERN_DEBUG "stli_onbeenable(brdp=%x)\n", (int) brdp);
3781
#endif
3782
        outb(ONB_EIENABLE, (brdp->iobase + ONB_EICONFR));
3783
}
3784
 
3785
/*****************************************************************************/
3786
 
3787
static void stli_onbedisable(stlibrd_t *brdp)
3788
{
3789
#if DEBUG
3790
        printk(KERN_DEBUG "stli_onbedisable(brdp=%x)\n", (int) brdp);
3791
#endif
3792
        outb(ONB_EIDISABLE, (brdp->iobase + ONB_EICONFR));
3793
}
3794
 
3795
/*****************************************************************************/
3796
 
3797
static char *stli_onbegetmemptr(stlibrd_t *brdp, unsigned long offset, int line)
3798
{
3799
        void            *ptr;
3800
        unsigned char   val;
3801
 
3802
#if DEBUG
3803
        printk(KERN_DEBUG "stli_onbegetmemptr(brdp=%x,offset=%x,line=%d)\n",
3804
                (int) brdp, (int) offset, line);
3805
#endif
3806
 
3807
        if (offset > brdp->memsize) {
3808
                printk(KERN_ERR "STALLION: shared memory pointer=%x out of "
3809
                                "range at line=%d(%d), brd=%d\n",
3810
                        (int) offset, line, __LINE__, brdp->brdnr);
3811
                ptr = 0;
3812
                val = 0;
3813
        } else {
3814
                ptr = brdp->membase + (offset % ONB_EIPAGESIZE);
3815
                if (offset < ONB_EIPAGESIZE)
3816
                        val = ONB_EIENABLE;
3817
                else
3818
                        val = ONB_EIENABLE | 0x40;
3819
        }
3820
        outb(val, (brdp->iobase + ONB_EICONFR));
3821
        return(ptr);
3822
}
3823
 
3824
/*****************************************************************************/
3825
 
3826
static void stli_onbereset(stlibrd_t *brdp)
3827
{
3828
 
3829
#if DEBUG
3830
        printk(KERN_ERR "stli_onbereset(brdp=%x)\n", (int) brdp);
3831
#endif
3832
 
3833
        outb(ONB_EISTOP, (brdp->iobase + ONB_EICONFR));
3834
        udelay(10);
3835
        outb(ONB_EIDISABLE, (brdp->iobase + ONB_EICONFR));
3836
        mdelay(1000);
3837
}
3838
 
3839
/*****************************************************************************/
3840
 
3841
/*
3842
 *      The following routines act on Brumby boards.
3843
 */
3844
 
3845
static void stli_bbyinit(stlibrd_t *brdp)
3846
{
3847
 
3848
#if DEBUG
3849
        printk(KERN_ERR "stli_bbyinit(brdp=%d)\n", (int) brdp);
3850
#endif
3851
 
3852
        outb(BBY_ATSTOP, (brdp->iobase + BBY_ATCONFR));
3853
        udelay(10);
3854
        outb(0, (brdp->iobase + BBY_ATCONFR));
3855
        mdelay(1000);
3856
        outb(0x1, brdp->iobase);
3857
        mdelay(1);
3858
}
3859
 
3860
/*****************************************************************************/
3861
 
3862
static char *stli_bbygetmemptr(stlibrd_t *brdp, unsigned long offset, int line)
3863
{
3864
        void            *ptr;
3865
        unsigned char   val;
3866
 
3867
#if DEBUG
3868
        printk(KERN_ERR "stli_bbygetmemptr(brdp=%x,offset=%x)\n", (int) brdp,
3869
                (int) offset);
3870
#endif
3871
 
3872
        if (offset > brdp->memsize) {
3873
                printk(KERN_ERR "STALLION: shared memory pointer=%x out of "
3874
                                "range at line=%d(%d), brd=%d\n",
3875
                                (int) offset, line, __LINE__, brdp->brdnr);
3876
                ptr = 0;
3877
                val = 0;
3878
        } else {
3879
                ptr = brdp->membase + (offset % BBY_PAGESIZE);
3880
                val = (unsigned char) (offset / BBY_PAGESIZE);
3881
        }
3882
        outb(val, (brdp->iobase + BBY_ATCONFR));
3883
        return(ptr);
3884
}
3885
 
3886
/*****************************************************************************/
3887
 
3888
static void stli_bbyreset(stlibrd_t *brdp)
3889
{
3890
 
3891
#if DEBUG
3892
        printk(KERN_DEBUG "stli_bbyreset(brdp=%x)\n", (int) brdp);
3893
#endif
3894
 
3895
        outb(BBY_ATSTOP, (brdp->iobase + BBY_ATCONFR));
3896
        udelay(10);
3897
        outb(0, (brdp->iobase + BBY_ATCONFR));
3898
        mdelay(1000);
3899
}
3900
 
3901
/*****************************************************************************/
3902
 
3903
/*
3904
 *      The following routines act on original old Stallion boards.
3905
 */
3906
 
3907
static void stli_stalinit(stlibrd_t *brdp)
3908
{
3909
 
3910
#if DEBUG
3911
        printk(KERN_DEBUG "stli_stalinit(brdp=%d)\n", (int) brdp);
3912
#endif
3913
 
3914
        outb(0x1, brdp->iobase);
3915
        mdelay(1000);
3916
}
3917
 
3918
/*****************************************************************************/
3919
 
3920
static char *stli_stalgetmemptr(stlibrd_t *brdp, unsigned long offset, int line)
3921
{
3922
        void    *ptr;
3923
 
3924
#if DEBUG
3925
        printk(KERN_DEBUG "stli_stalgetmemptr(brdp=%x,offset=%x)\n", (int) brdp,
3926
                (int) offset);
3927
#endif
3928
 
3929
        if (offset > brdp->memsize) {
3930
                printk(KERN_ERR "STALLION: shared memory pointer=%x out of "
3931
                                "range at line=%d(%d), brd=%d\n",
3932
                                (int) offset, line, __LINE__, brdp->brdnr);
3933
                ptr = 0;
3934
        } else {
3935
                ptr = brdp->membase + (offset % STAL_PAGESIZE);
3936
        }
3937
        return(ptr);
3938
}
3939
 
3940
/*****************************************************************************/
3941
 
3942
static void stli_stalreset(stlibrd_t *brdp)
3943
{
3944
        volatile unsigned long  *vecp;
3945
 
3946
#if DEBUG
3947
        printk(KERN_DEBUG "stli_stalreset(brdp=%x)\n", (int) brdp);
3948
#endif
3949
 
3950
        vecp = (volatile unsigned long *) (brdp->membase + 0x30);
3951
        *vecp = 0xffff0000;
3952
        outb(0, brdp->iobase);
3953
        mdelay(1000);
3954
}
3955
 
3956
/*****************************************************************************/
3957
 
3958
/*
3959
 *      Try to find an ECP board and initialize it. This handles only ECP
3960
 *      board types.
3961
 */
3962
 
3963
static inline int stli_initecp(stlibrd_t *brdp)
3964
{
3965
        cdkecpsig_t     sig;
3966
        cdkecpsig_t     *sigsp;
3967
        unsigned int    status, nxtid;
3968
        char            *name;
3969
        int             panelnr, nrports;
3970
 
3971
#if DEBUG
3972
        printk(KERN_DEBUG "stli_initecp(brdp=%x)\n", (int) brdp);
3973
#endif
3974
 
3975
/*
3976
 *      Do a basic sanity check on the IO and memory addresses.
3977
 */
3978
        if ((brdp->iobase == 0) || (brdp->memaddr == 0))
3979
                return(-ENODEV);
3980
 
3981
        brdp->iosize = ECP_IOSIZE;
3982
        if (check_region(brdp->iobase, brdp->iosize))
3983
                printk(KERN_ERR "STALLION: Warning, board %d I/O address %x "
3984
                                "conflicts with another device\n",
3985
                                brdp->brdnr, brdp->iobase);
3986
 
3987
/*
3988
 *      Based on the specific board type setup the common vars to access
3989
 *      and enable shared memory. Set all board specific information now
3990
 *      as well.
3991
 */
3992
        switch (brdp->brdtype) {
3993
        case BRD_ECP:
3994
                brdp->membase = (void *) brdp->memaddr;
3995
                brdp->memsize = ECP_MEMSIZE;
3996
                brdp->pagesize = ECP_ATPAGESIZE;
3997
                brdp->init = stli_ecpinit;
3998
                brdp->enable = stli_ecpenable;
3999
                brdp->reenable = stli_ecpenable;
4000
                brdp->disable = stli_ecpdisable;
4001
                brdp->getmemptr = stli_ecpgetmemptr;
4002
                brdp->intr = stli_ecpintr;
4003
                brdp->reset = stli_ecpreset;
4004
                name = "serial(EC8/64)";
4005
                break;
4006
 
4007
        case BRD_ECPE:
4008
                brdp->membase = (void *) brdp->memaddr;
4009
                brdp->memsize = ECP_MEMSIZE;
4010
                brdp->pagesize = ECP_EIPAGESIZE;
4011
                brdp->init = stli_ecpeiinit;
4012
                brdp->enable = stli_ecpeienable;
4013
                brdp->reenable = stli_ecpeienable;
4014
                brdp->disable = stli_ecpeidisable;
4015
                brdp->getmemptr = stli_ecpeigetmemptr;
4016
                brdp->intr = stli_ecpintr;
4017
                brdp->reset = stli_ecpeireset;
4018
                name = "serial(EC8/64-EI)";
4019
                break;
4020
 
4021
        case BRD_ECPMC:
4022
                brdp->membase = (void *) brdp->memaddr;
4023
                brdp->memsize = ECP_MEMSIZE;
4024
                brdp->pagesize = ECP_MCPAGESIZE;
4025
                brdp->init = NULL;
4026
                brdp->enable = stli_ecpmcenable;
4027
                brdp->reenable = stli_ecpmcenable;
4028
                brdp->disable = stli_ecpmcdisable;
4029
                brdp->getmemptr = stli_ecpmcgetmemptr;
4030
                brdp->intr = stli_ecpintr;
4031
                brdp->reset = stli_ecpmcreset;
4032
                name = "serial(EC8/64-MCA)";
4033
                break;
4034
 
4035
        case BRD_ECPPCI:
4036
                brdp->membase = (void *) brdp->memaddr;
4037
                brdp->memsize = ECP_PCIMEMSIZE;
4038
                brdp->pagesize = ECP_PCIPAGESIZE;
4039
                brdp->init = stli_ecppciinit;
4040
                brdp->enable = NULL;
4041
                brdp->reenable = NULL;
4042
                brdp->disable = NULL;
4043
                brdp->getmemptr = stli_ecppcigetmemptr;
4044
                brdp->intr = stli_ecpintr;
4045
                brdp->reset = stli_ecppcireset;
4046
                name = "serial(EC/RA-PCI)";
4047
                break;
4048
 
4049
        default:
4050
                return(-EINVAL);
4051
        }
4052
 
4053
/*
4054
 *      The per-board operations structure is all set up, so now let's go
4055
 *      and get the board operational. Firstly initialize board configuration
4056
 *      registers. Set the memory mapping info so we can get at the boards
4057
 *      shared memory.
4058
 */
4059
        EBRDINIT(brdp);
4060
 
4061
        brdp->membase = ioremap(brdp->memaddr, brdp->memsize);
4062
        if (brdp->membase == (void *) NULL)
4063
                return(-ENOMEM);
4064
 
4065
/*
4066
 *      Now that all specific code is set up, enable the shared memory and
4067
 *      look for the a signature area that will tell us exactly what board
4068
 *      this is, and what it is connected to it.
4069
 */
4070
        EBRDENABLE(brdp);
4071
        sigsp = (cdkecpsig_t *) EBRDGETMEMPTR(brdp, CDK_SIGADDR);
4072
        memcpy(&sig, sigsp, sizeof(cdkecpsig_t));
4073
        EBRDDISABLE(brdp);
4074
 
4075
#if 0
4076
        printk("%s(%d): sig-> magic=%x rom=%x panel=%x,%x,%x,%x,%x,%x,%x,%x\n",
4077
                __FILE__, __LINE__, (int) sig.magic, sig.romver, sig.panelid[0],
4078
                (int) sig.panelid[1], (int) sig.panelid[2],
4079
                (int) sig.panelid[3], (int) sig.panelid[4],
4080
                (int) sig.panelid[5], (int) sig.panelid[6],
4081
                (int) sig.panelid[7]);
4082
#endif
4083
 
4084
        if (sig.magic != ECP_MAGIC)
4085
                return(-ENODEV);
4086
 
4087
/*
4088
 *      Scan through the signature looking at the panels connected to the
4089
 *      board. Calculate the total number of ports as we go.
4090
 */
4091
        for (panelnr = 0, nxtid = 0; (panelnr < STL_MAXPANELS); panelnr++) {
4092
                status = sig.panelid[nxtid];
4093
                if ((status & ECH_PNLIDMASK) != nxtid)
4094
                        break;
4095
 
4096
                brdp->panelids[panelnr] = status;
4097
                nrports = (status & ECH_PNL16PORT) ? 16 : 8;
4098
                if ((nrports == 16) && ((status & ECH_PNLXPID) == 0))
4099
                        nxtid++;
4100
                brdp->panels[panelnr] = nrports;
4101
                brdp->nrports += nrports;
4102
                nxtid++;
4103
                brdp->nrpanels++;
4104
        }
4105
 
4106
        request_region(brdp->iobase, brdp->iosize, name);
4107
        brdp->state |= BST_FOUND;
4108
        return(0);
4109
}
4110
 
4111
/*****************************************************************************/
4112
 
4113
/*
4114
 *      Try to find an ONboard, Brumby or Stallion board and initialize it.
4115
 *      This handles only these board types.
4116
 */
4117
 
4118
static inline int stli_initonb(stlibrd_t *brdp)
4119
{
4120
        cdkonbsig_t     sig;
4121
        cdkonbsig_t     *sigsp;
4122
        char            *name;
4123
        int             i;
4124
 
4125
#if DEBUG
4126
        printk(KERN_DEBUG "stli_initonb(brdp=%x)\n", (int) brdp);
4127
#endif
4128
 
4129
/*
4130
 *      Do a basic sanity check on the IO and memory addresses.
4131
 */
4132
        if ((brdp->iobase == 0) || (brdp->memaddr == 0))
4133
                return(-ENODEV);
4134
 
4135
        brdp->iosize = ONB_IOSIZE;
4136
        if (check_region(brdp->iobase, brdp->iosize))
4137
                printk(KERN_ERR "STALLION: Warning, board %d I/O address %x "
4138
                                "conflicts with another device\n",
4139
                                brdp->brdnr, brdp->iobase);
4140
 
4141
/*
4142
 *      Based on the specific board type setup the common vars to access
4143
 *      and enable shared memory. Set all board specific information now
4144
 *      as well.
4145
 */
4146
        switch (brdp->brdtype) {
4147
        case BRD_ONBOARD:
4148
        case BRD_ONBOARD32:
4149
        case BRD_ONBOARD2:
4150
        case BRD_ONBOARD2_32:
4151
        case BRD_ONBOARDRS:
4152
                brdp->membase = (void *) brdp->memaddr;
4153
                brdp->memsize = ONB_MEMSIZE;
4154
                brdp->pagesize = ONB_ATPAGESIZE;
4155
                brdp->init = stli_onbinit;
4156
                brdp->enable = stli_onbenable;
4157
                brdp->reenable = stli_onbenable;
4158
                brdp->disable = stli_onbdisable;
4159
                brdp->getmemptr = stli_onbgetmemptr;
4160
                brdp->intr = stli_ecpintr;
4161
                brdp->reset = stli_onbreset;
4162
                if (brdp->memaddr > 0x100000)
4163
                        brdp->enabval = ONB_MEMENABHI;
4164
                else
4165
                        brdp->enabval = ONB_MEMENABLO;
4166
                name = "serial(ONBoard)";
4167
                break;
4168
 
4169
        case BRD_ONBOARDE:
4170
                brdp->membase = (void *) brdp->memaddr;
4171
                brdp->memsize = ONB_EIMEMSIZE;
4172
                brdp->pagesize = ONB_EIPAGESIZE;
4173
                brdp->init = stli_onbeinit;
4174
                brdp->enable = stli_onbeenable;
4175
                brdp->reenable = stli_onbeenable;
4176
                brdp->disable = stli_onbedisable;
4177
                brdp->getmemptr = stli_onbegetmemptr;
4178
                brdp->intr = stli_ecpintr;
4179
                brdp->reset = stli_onbereset;
4180
                name = "serial(ONBoard/E)";
4181
                break;
4182
 
4183
        case BRD_BRUMBY4:
4184
        case BRD_BRUMBY8:
4185
        case BRD_BRUMBY16:
4186
                brdp->membase = (void *) brdp->memaddr;
4187
                brdp->memsize = BBY_MEMSIZE;
4188
                brdp->pagesize = BBY_PAGESIZE;
4189
                brdp->init = stli_bbyinit;
4190
                brdp->enable = NULL;
4191
                brdp->reenable = NULL;
4192
                brdp->disable = NULL;
4193
                brdp->getmemptr = stli_bbygetmemptr;
4194
                brdp->intr = stli_ecpintr;
4195
                brdp->reset = stli_bbyreset;
4196
                name = "serial(Brumby)";
4197
                break;
4198
 
4199
        case BRD_STALLION:
4200
                brdp->membase = (void *) brdp->memaddr;
4201
                brdp->memsize = STAL_MEMSIZE;
4202
                brdp->pagesize = STAL_PAGESIZE;
4203
                brdp->init = stli_stalinit;
4204
                brdp->enable = NULL;
4205
                brdp->reenable = NULL;
4206
                brdp->disable = NULL;
4207
                brdp->getmemptr = stli_stalgetmemptr;
4208
                brdp->intr = stli_ecpintr;
4209
                brdp->reset = stli_stalreset;
4210
                name = "serial(Stallion)";
4211
                break;
4212
 
4213
        default:
4214
                return(-EINVAL);
4215
        }
4216
 
4217
/*
4218
 *      The per-board operations structure is all set up, so now let's go
4219
 *      and get the board operational. Firstly initialize board configuration
4220
 *      registers. Set the memory mapping info so we can get at the boards
4221
 *      shared memory.
4222
 */
4223
        EBRDINIT(brdp);
4224
 
4225
        brdp->membase = ioremap(brdp->memaddr, brdp->memsize);
4226
        if (brdp->membase == (void *) NULL)
4227
                return(-ENOMEM);
4228
 
4229
/*
4230
 *      Now that all specific code is set up, enable the shared memory and
4231
 *      look for the a signature area that will tell us exactly what board
4232
 *      this is, and how many ports.
4233
 */
4234
        EBRDENABLE(brdp);
4235
        sigsp = (cdkonbsig_t *) EBRDGETMEMPTR(brdp, CDK_SIGADDR);
4236
        memcpy(&sig, sigsp, sizeof(cdkonbsig_t));
4237
        EBRDDISABLE(brdp);
4238
 
4239
#if 0
4240
        printk("%s(%d): sig-> magic=%x:%x:%x:%x romver=%x amask=%x:%x:%x\n",
4241
                __FILE__, __LINE__, sig.magic0, sig.magic1, sig.magic2,
4242
                sig.magic3, sig.romver, sig.amask0, sig.amask1, sig.amask2);
4243
#endif
4244
 
4245
        if ((sig.magic0 != ONB_MAGIC0) || (sig.magic1 != ONB_MAGIC1) ||
4246
            (sig.magic2 != ONB_MAGIC2) || (sig.magic3 != ONB_MAGIC3))
4247
                return(-ENODEV);
4248
 
4249
/*
4250
 *      Scan through the signature alive mask and calculate how many ports
4251
 *      there are on this board.
4252
 */
4253
        brdp->nrpanels = 1;
4254
        if (sig.amask1) {
4255
                brdp->nrports = 32;
4256
        } else {
4257
                for (i = 0; (i < 16); i++) {
4258
                        if (((sig.amask0 << i) & 0x8000) == 0)
4259
                                break;
4260
                }
4261
                brdp->nrports = i;
4262
        }
4263
        brdp->panels[0] = brdp->nrports;
4264
 
4265
        request_region(brdp->iobase, brdp->iosize, name);
4266
        brdp->state |= BST_FOUND;
4267
        return(0);
4268
}
4269
 
4270
/*****************************************************************************/
4271
 
4272
/*
4273
 *      Start up a running board. This routine is only called after the
4274
 *      code has been down loaded to the board and is operational. It will
4275
 *      read in the memory map, and get the show on the road...
4276
 */
4277
 
4278
static int stli_startbrd(stlibrd_t *brdp)
4279
{
4280
        volatile cdkhdr_t       *hdrp;
4281
        volatile cdkmem_t       *memp;
4282
        volatile cdkasy_t       *ap;
4283
        unsigned long           flags;
4284
        stliport_t              *portp;
4285
        int                     portnr, nrdevs, i, rc;
4286
 
4287
#if DEBUG
4288
        printk(KERN_DEBUG "stli_startbrd(brdp=%x)\n", (int) brdp);
4289
#endif
4290
 
4291
        rc = 0;
4292
 
4293
        save_flags(flags);
4294
        cli();
4295
        EBRDENABLE(brdp);
4296
        hdrp = (volatile cdkhdr_t *) EBRDGETMEMPTR(brdp, CDK_CDKADDR);
4297
        nrdevs = hdrp->nrdevs;
4298
 
4299
#if 0
4300
        printk("%s(%d): CDK version %d.%d.%d --> "
4301
                "nrdevs=%d memp=%x hostp=%x slavep=%x\n",
4302
                 __FILE__, __LINE__, hdrp->ver_release, hdrp->ver_modification,
4303
                 hdrp->ver_fix, nrdevs, (int) hdrp->memp, (int) hdrp->hostp,
4304
                 (int) hdrp->slavep);
4305
#endif
4306
 
4307
        if (nrdevs < (brdp->nrports + 1)) {
4308
                printk(KERN_ERR "STALLION: slave failed to allocate memory for "
4309
                                "all devices, devices=%d\n", nrdevs);
4310
                brdp->nrports = nrdevs - 1;
4311
        }
4312
        brdp->nrdevs = nrdevs;
4313
        brdp->hostoffset = hdrp->hostp - CDK_CDKADDR;
4314
        brdp->slaveoffset = hdrp->slavep - CDK_CDKADDR;
4315
        brdp->bitsize = (nrdevs + 7) / 8;
4316
        memp = (volatile cdkmem_t *) hdrp->memp;
4317
        if (((unsigned long) memp) > brdp->memsize) {
4318
                printk(KERN_ERR "STALLION: corrupted shared memory region?\n");
4319
                rc = -EIO;
4320
                goto stli_donestartup;
4321
        }
4322
        memp = (volatile cdkmem_t *) EBRDGETMEMPTR(brdp, (unsigned long) memp);
4323
        if (memp->dtype != TYP_ASYNCTRL) {
4324
                printk(KERN_ERR "STALLION: no slave control device found\n");
4325
                goto stli_donestartup;
4326
        }
4327
        memp++;
4328
 
4329
/*
4330
 *      Cycle through memory allocation of each port. We are guaranteed to
4331
 *      have all ports inside the first page of slave window, so no need to
4332
 *      change pages while reading memory map.
4333
 */
4334
        for (i = 1, portnr = 0; (i < nrdevs); i++, portnr++, memp++) {
4335
                if (memp->dtype != TYP_ASYNC)
4336
                        break;
4337
                portp = brdp->ports[portnr];
4338
                if (portp == (stliport_t *) NULL)
4339
                        break;
4340
                portp->devnr = i;
4341
                portp->addr = memp->offset;
4342
                portp->reqbit = (unsigned char) (0x1 << (i * 8 / nrdevs));
4343
                portp->portidx = (unsigned char) (i / 8);
4344
                portp->portbit = (unsigned char) (0x1 << (i % 8));
4345
        }
4346
 
4347
        hdrp->slavereq = 0xff;
4348
 
4349
/*
4350
 *      For each port setup a local copy of the RX and TX buffer offsets
4351
 *      and sizes. We do this separate from the above, because we need to
4352
 *      move the shared memory page...
4353
 */
4354
        for (i = 1, portnr = 0; (i < nrdevs); i++, portnr++) {
4355
                portp = brdp->ports[portnr];
4356
                if (portp == (stliport_t *) NULL)
4357
                        break;
4358
                if (portp->addr == 0)
4359
                        break;
4360
                ap = (volatile cdkasy_t *) EBRDGETMEMPTR(brdp, portp->addr);
4361
                if (ap != (volatile cdkasy_t *) NULL) {
4362
                        portp->rxsize = ap->rxq.size;
4363
                        portp->txsize = ap->txq.size;
4364
                        portp->rxoffset = ap->rxq.offset;
4365
                        portp->txoffset = ap->txq.offset;
4366
                }
4367
        }
4368
 
4369
stli_donestartup:
4370
        EBRDDISABLE(brdp);
4371
        restore_flags(flags);
4372
 
4373
        if (rc == 0)
4374
                brdp->state |= BST_STARTED;
4375
 
4376
        if (! stli_timeron) {
4377
                stli_timeron++;
4378
                stli_timerlist.expires = STLI_TIMEOUT;
4379
                add_timer(&stli_timerlist);
4380
        }
4381
 
4382
        return(rc);
4383
}
4384
 
4385
/*****************************************************************************/
4386
 
4387
/*
4388
 *      Probe and initialize the specified board.
4389
 */
4390
 
4391
static int __init stli_brdinit(stlibrd_t *brdp)
4392
{
4393
#if DEBUG
4394
        printk(KERN_DEBUG "stli_brdinit(brdp=%x)\n", (int) brdp);
4395
#endif
4396
 
4397
        stli_brds[brdp->brdnr] = brdp;
4398
 
4399
        switch (brdp->brdtype) {
4400
        case BRD_ECP:
4401
        case BRD_ECPE:
4402
        case BRD_ECPMC:
4403
        case BRD_ECPPCI:
4404
                stli_initecp(brdp);
4405
                break;
4406
        case BRD_ONBOARD:
4407
        case BRD_ONBOARDE:
4408
        case BRD_ONBOARD2:
4409
        case BRD_ONBOARD32:
4410
        case BRD_ONBOARD2_32:
4411
        case BRD_ONBOARDRS:
4412
        case BRD_BRUMBY4:
4413
        case BRD_BRUMBY8:
4414
        case BRD_BRUMBY16:
4415
        case BRD_STALLION:
4416
                stli_initonb(brdp);
4417
                break;
4418
        case BRD_EASYIO:
4419
        case BRD_ECH:
4420
        case BRD_ECHMC:
4421
        case BRD_ECHPCI:
4422
                printk(KERN_ERR "STALLION: %s board type not supported in "
4423
                                "this driver\n", stli_brdnames[brdp->brdtype]);
4424
                return(ENODEV);
4425
        default:
4426
                printk(KERN_ERR "STALLION: board=%d is unknown board "
4427
                                "type=%d\n", brdp->brdnr, brdp->brdtype);
4428
                return(ENODEV);
4429
        }
4430
 
4431
        if ((brdp->state & BST_FOUND) == 0) {
4432
                printk(KERN_ERR "STALLION: %s board not found, board=%d "
4433
                                "io=%x mem=%x\n",
4434
                        stli_brdnames[brdp->brdtype], brdp->brdnr,
4435
                        brdp->iobase, (int) brdp->memaddr);
4436
                return(ENODEV);
4437
        }
4438
 
4439
        stli_initports(brdp);
4440
        printk(KERN_INFO "STALLION: %s found, board=%d io=%x mem=%x "
4441
                "nrpanels=%d nrports=%d\n", stli_brdnames[brdp->brdtype],
4442
                brdp->brdnr, brdp->iobase, (int) brdp->memaddr,
4443
                brdp->nrpanels, brdp->nrports);
4444
        return(0);
4445
}
4446
 
4447
/*****************************************************************************/
4448
 
4449
/*
4450
 *      Probe around trying to find where the EISA boards shared memory
4451
 *      might be. This is a bit if hack, but it is the best we can do.
4452
 */
4453
 
4454
static inline int stli_eisamemprobe(stlibrd_t *brdp)
4455
{
4456
        cdkecpsig_t     ecpsig, *ecpsigp;
4457
        cdkonbsig_t     onbsig, *onbsigp;
4458
        int             i, foundit;
4459
 
4460
#if DEBUG
4461
        printk(KERN_DEBUG "stli_eisamemprobe(brdp=%x)\n", (int) brdp);
4462
#endif
4463
 
4464
/*
4465
 *      First up we reset the board, to get it into a known state. There
4466
 *      is only 2 board types here we need to worry about. Don;t use the
4467
 *      standard board init routine here, it programs up the shared
4468
 *      memory address, and we don't know it yet...
4469
 */
4470
        if (brdp->brdtype == BRD_ECPE) {
4471
                outb(0x1, (brdp->iobase + ECP_EIBRDENAB));
4472
                outb(ECP_EISTOP, (brdp->iobase + ECP_EICONFR));
4473
                udelay(10);
4474
                outb(ECP_EIDISABLE, (brdp->iobase + ECP_EICONFR));
4475
                udelay(500);
4476
                stli_ecpeienable(brdp);
4477
        } else if (brdp->brdtype == BRD_ONBOARDE) {
4478
                outb(0x1, (brdp->iobase + ONB_EIBRDENAB));
4479
                outb(ONB_EISTOP, (brdp->iobase + ONB_EICONFR));
4480
                udelay(10);
4481
                outb(ONB_EIDISABLE, (brdp->iobase + ONB_EICONFR));
4482
                mdelay(100);
4483
                outb(0x1, brdp->iobase);
4484
                mdelay(1);
4485
                stli_onbeenable(brdp);
4486
        } else {
4487
                return(-ENODEV);
4488
        }
4489
 
4490
        foundit = 0;
4491
        brdp->memsize = ECP_MEMSIZE;
4492
 
4493
/*
4494
 *      Board shared memory is enabled, so now we have a poke around and
4495
 *      see if we can find it.
4496
 */
4497
        for (i = 0; (i < stli_eisamempsize); i++) {
4498
                brdp->memaddr = stli_eisamemprobeaddrs[i];
4499
                brdp->membase = (void *) brdp->memaddr;
4500
                brdp->membase = ioremap(brdp->memaddr, brdp->memsize);
4501
                if (brdp->membase == (void *) NULL)
4502
                        continue;
4503
 
4504
                if (brdp->brdtype == BRD_ECPE) {
4505
                        ecpsigp = (cdkecpsig_t *) stli_ecpeigetmemptr(brdp,
4506
                                CDK_SIGADDR, __LINE__);
4507
                        memcpy(&ecpsig, ecpsigp, sizeof(cdkecpsig_t));
4508
                        if (ecpsig.magic == ECP_MAGIC)
4509
                                foundit = 1;
4510
                } else {
4511
                        onbsigp = (cdkonbsig_t *) stli_onbegetmemptr(brdp,
4512
                                CDK_SIGADDR, __LINE__);
4513
                        memcpy(&onbsig, onbsigp, sizeof(cdkonbsig_t));
4514
                        if ((onbsig.magic0 == ONB_MAGIC0) &&
4515
                            (onbsig.magic1 == ONB_MAGIC1) &&
4516
                            (onbsig.magic2 == ONB_MAGIC2) &&
4517
                            (onbsig.magic3 == ONB_MAGIC3))
4518
                                foundit = 1;
4519
                }
4520
 
4521
                iounmap(brdp->membase);
4522
                if (foundit)
4523
                        break;
4524
        }
4525
 
4526
/*
4527
 *      Regardless of whether we found the shared memory or not we must
4528
 *      disable the region. After that return success or failure.
4529
 */
4530
        if (brdp->brdtype == BRD_ECPE)
4531
                stli_ecpeidisable(brdp);
4532
        else
4533
                stli_onbedisable(brdp);
4534
 
4535
        if (! foundit) {
4536
                brdp->memaddr = 0;
4537
                brdp->membase = 0;
4538
                printk(KERN_ERR "STALLION: failed to probe shared memory "
4539
                                "region for %s in EISA slot=%d\n",
4540
                        stli_brdnames[brdp->brdtype], (brdp->iobase >> 12));
4541
                return(-ENODEV);
4542
        }
4543
        return(0);
4544
}
4545
 
4546
/*****************************************************************************/
4547
 
4548
/*
4549
 *      Probe around and try to find any EISA boards in system. The biggest
4550
 *      problem here is finding out what memory address is associated with
4551
 *      an EISA board after it is found. The registers of the ECPE and
4552
 *      ONboardE are not readable - so we can't read them from there. We
4553
 *      don't have access to the EISA CMOS (or EISA BIOS) so we don't
4554
 *      actually have any way to find out the real value. The best we can
4555
 *      do is go probing around in the usual places hoping we can find it.
4556
 */
4557
 
4558
static inline int stli_findeisabrds()
4559
{
4560
        stlibrd_t       *brdp;
4561
        unsigned int    iobase, eid;
4562
        int             i;
4563
 
4564
#if DEBUG
4565
        printk(KERN_DEBUG "stli_findeisabrds()\n");
4566
#endif
4567
 
4568
/*
4569
 *      Firstly check if this is an EISA system. Do this by probing for
4570
 *      the system board EISA ID. If this is not an EISA system then
4571
 *      don't bother going any further!
4572
 */
4573
        outb(0xff, 0xc80);
4574
        if (inb(0xc80) == 0xff)
4575
                return(0);
4576
 
4577
/*
4578
 *      Looks like an EISA system, so go searching for EISA boards.
4579
 */
4580
        for (iobase = 0x1000; (iobase <= 0xc000); iobase += 0x1000) {
4581
                outb(0xff, (iobase + 0xc80));
4582
                eid = inb(iobase + 0xc80);
4583
                eid |= inb(iobase + 0xc81) << 8;
4584
                if (eid != STL_EISAID)
4585
                        continue;
4586
 
4587
/*
4588
 *              We have found a board. Need to check if this board was
4589
 *              statically configured already (just in case!).
4590
 */
4591
                for (i = 0; (i < STL_MAXBRDS); i++) {
4592
                        brdp = stli_brds[i];
4593
                        if (brdp == (stlibrd_t *) NULL)
4594
                                continue;
4595
                        if (brdp->iobase == iobase)
4596
                                break;
4597
                }
4598
                if (i < STL_MAXBRDS)
4599
                        continue;
4600
 
4601
/*
4602
 *              We have found a Stallion board and it is not configured already.
4603
 *              Allocate a board structure and initialize it.
4604
 */
4605
                if ((brdp = stli_allocbrd()) == (stlibrd_t *) NULL)
4606
                        return(-ENOMEM);
4607
                if ((brdp->brdnr = stli_getbrdnr()) < 0)
4608
                        return(-ENOMEM);
4609
                eid = inb(iobase + 0xc82);
4610
                if (eid == ECP_EISAID)
4611
                        brdp->brdtype = BRD_ECPE;
4612
                else if (eid == ONB_EISAID)
4613
                        brdp->brdtype = BRD_ONBOARDE;
4614
                else
4615
                        brdp->brdtype = BRD_UNKNOWN;
4616
                brdp->iobase = iobase;
4617
                outb(0x1, (iobase + 0xc84));
4618
                if (stli_eisamemprobe(brdp))
4619
                        outb(0, (iobase + 0xc84));
4620
                stli_brdinit(brdp);
4621
        }
4622
 
4623
        return(0);
4624
}
4625
 
4626
/*****************************************************************************/
4627
 
4628
/*
4629
 *      Find the next available board number that is free.
4630
 */
4631
 
4632
static inline int stli_getbrdnr()
4633
{
4634
        int     i;
4635
 
4636
        for (i = 0; (i < STL_MAXBRDS); i++) {
4637
                if (stli_brds[i] == (stlibrd_t *) NULL) {
4638
                        if (i >= stli_nrbrds)
4639
                                stli_nrbrds = i + 1;
4640
                        return(i);
4641
                }
4642
        }
4643
        return(-1);
4644
}
4645
 
4646
/*****************************************************************************/
4647
 
4648
#ifdef  CONFIG_PCI
4649
 
4650
/*
4651
 *      We have a Stallion board. Allocate a board structure and
4652
 *      initialize it. Read its IO and MEMORY resources from PCI
4653
 *      configuration space.
4654
 */
4655
 
4656
static inline int stli_initpcibrd(int brdtype, struct pci_dev *devp)
4657
{
4658
        stlibrd_t       *brdp;
4659
 
4660
#if DEBUG
4661
        printk(KERN_DEBUG "stli_initpcibrd(brdtype=%d,busnr=%x,devnr=%x)\n",
4662
                brdtype, dev->bus->number, dev->devfn);
4663
#endif
4664
 
4665
        if (pci_enable_device(devp))
4666
                return(-EIO);
4667
        if ((brdp = stli_allocbrd()) == (stlibrd_t *) NULL)
4668
                return(-ENOMEM);
4669
        if ((brdp->brdnr = stli_getbrdnr()) < 0) {
4670
                printk(KERN_INFO "STALLION: too many boards found, "
4671
                        "maximum supported %d\n", STL_MAXBRDS);
4672
                return(0);
4673
        }
4674
        brdp->brdtype = brdtype;
4675
 
4676
#if DEBUG
4677
        printk(KERN_DEBUG "%s(%d): BAR[]=%lx,%lx,%lx,%lx\n", __FILE__, __LINE__,
4678
                pci_resource_start(devp, 0),
4679
                pci_resource_start(devp, 1),
4680
                pci_resource_start(devp, 2),
4681
                pci_resource_start(devp, 3));
4682
#endif
4683
 
4684
/*
4685
 *      We have all resources from the board, so lets setup the actual
4686
 *      board structure now.
4687
 */
4688
        brdp->iobase = pci_resource_start(devp, 3);
4689
        brdp->memaddr = pci_resource_start(devp, 2);
4690
        stli_brdinit(brdp);
4691
 
4692
        return(0);
4693
}
4694
 
4695
/*****************************************************************************/
4696
 
4697
/*
4698
 *      Find all Stallion PCI boards that might be installed. Initialize each
4699
 *      one as it is found.
4700
 */
4701
 
4702
static inline int stli_findpcibrds()
4703
{
4704
        struct pci_dev  *dev = NULL;
4705
        int             rc;
4706
 
4707
#if DEBUG
4708
        printk("stli_findpcibrds()\n");
4709
#endif
4710
 
4711
        if (! pci_present())
4712
                return(0);
4713
 
4714
        while ((dev = pci_find_device(PCI_VENDOR_ID_STALLION,
4715
            PCI_DEVICE_ID_ECRA, dev))) {
4716
                if ((rc = stli_initpcibrd(BRD_ECPPCI, dev)))
4717
                        return(rc);
4718
        }
4719
 
4720
        return(0);
4721
}
4722
 
4723
#endif
4724
 
4725
/*****************************************************************************/
4726
 
4727
/*
4728
 *      Allocate a new board structure. Fill out the basic info in it.
4729
 */
4730
 
4731
static stlibrd_t *stli_allocbrd()
4732
{
4733
        stlibrd_t       *brdp;
4734
 
4735
        brdp = (stlibrd_t *) stli_memalloc(sizeof(stlibrd_t));
4736
        if (brdp == (stlibrd_t *) NULL) {
4737
                printk(KERN_ERR "STALLION: failed to allocate memory "
4738
                                "(size=%d)\n", sizeof(stlibrd_t));
4739
                return((stlibrd_t *) NULL);
4740
        }
4741
 
4742
        memset(brdp, 0, sizeof(stlibrd_t));
4743
        brdp->magic = STLI_BOARDMAGIC;
4744
        return(brdp);
4745
}
4746
 
4747
/*****************************************************************************/
4748
 
4749
/*
4750
 *      Scan through all the boards in the configuration and see what we
4751
 *      can find.
4752
 */
4753
 
4754
static inline int stli_initbrds()
4755
{
4756
        stlibrd_t       *brdp, *nxtbrdp;
4757
        stlconf_t       *confp;
4758
        int             i, j;
4759
 
4760
#if DEBUG
4761
        printk(KERN_DEBUG "stli_initbrds()\n");
4762
#endif
4763
 
4764
        if (stli_nrbrds > STL_MAXBRDS) {
4765
                printk(KERN_INFO "STALLION: too many boards in configuration "
4766
                        "table, truncating to %d\n", STL_MAXBRDS);
4767
                stli_nrbrds = STL_MAXBRDS;
4768
        }
4769
 
4770
/*
4771
 *      Firstly scan the list of static boards configured. Allocate
4772
 *      resources and initialize the boards as found. If this is a
4773
 *      module then let the module args override static configuration.
4774
 */
4775
        for (i = 0; (i < stli_nrbrds); i++) {
4776
                confp = &stli_brdconf[i];
4777
#ifdef MODULE
4778
                stli_parsebrd(confp, stli_brdsp[i]);
4779
#endif
4780
                if ((brdp = stli_allocbrd()) == (stlibrd_t *) NULL)
4781
                        return(-ENOMEM);
4782
                brdp->brdnr = i;
4783
                brdp->brdtype = confp->brdtype;
4784
                brdp->iobase = confp->ioaddr1;
4785
                brdp->memaddr = confp->memaddr;
4786
                stli_brdinit(brdp);
4787
        }
4788
 
4789
/*
4790
 *      Static configuration table done, so now use dynamic methods to
4791
 *      see if any more boards should be configured.
4792
 */
4793
#ifdef MODULE
4794
        stli_argbrds();
4795
#endif
4796
        if (stli_eisaprobe)
4797
                stli_findeisabrds();
4798
#ifdef CONFIG_PCI
4799
        stli_findpcibrds();
4800
#endif
4801
 
4802
/*
4803
 *      All found boards are initialized. Now for a little optimization, if
4804
 *      no boards are sharing the "shared memory" regions then we can just
4805
 *      leave them all enabled. This is in fact the usual case.
4806
 */
4807
        stli_shared = 0;
4808
        if (stli_nrbrds > 1) {
4809
                for (i = 0; (i < stli_nrbrds); i++) {
4810
                        brdp = stli_brds[i];
4811
                        if (brdp == (stlibrd_t *) NULL)
4812
                                continue;
4813
                        for (j = i + 1; (j < stli_nrbrds); j++) {
4814
                                nxtbrdp = stli_brds[j];
4815
                                if (nxtbrdp == (stlibrd_t *) NULL)
4816
                                        continue;
4817
                                if ((brdp->membase >= nxtbrdp->membase) &&
4818
                                    (brdp->membase <= (nxtbrdp->membase +
4819
                                    nxtbrdp->memsize - 1))) {
4820
                                        stli_shared++;
4821
                                        break;
4822
                                }
4823
                        }
4824
                }
4825
        }
4826
 
4827
        if (stli_shared == 0) {
4828
                for (i = 0; (i < stli_nrbrds); i++) {
4829
                        brdp = stli_brds[i];
4830
                        if (brdp == (stlibrd_t *) NULL)
4831
                                continue;
4832
                        if (brdp->state & BST_FOUND) {
4833
                                EBRDENABLE(brdp);
4834
                                brdp->enable = NULL;
4835
                                brdp->disable = NULL;
4836
                        }
4837
                }
4838
        }
4839
 
4840
        return(0);
4841
}
4842
 
4843
/*****************************************************************************/
4844
 
4845
/*
4846
 *      Code to handle an "staliomem" read operation. This device is the
4847
 *      contents of the board shared memory. It is used for down loading
4848
 *      the slave image (and debugging :-)
4849
 */
4850
 
4851
static ssize_t stli_memread(struct file *fp, char *buf, size_t count, loff_t *offp)
4852
{
4853
        unsigned long   flags;
4854
        void            *memptr;
4855
        stlibrd_t       *brdp;
4856
        int             brdnr, size, n;
4857
 
4858
#if DEBUG
4859
        printk(KERN_DEBUG "stli_memread(fp=%x,buf=%x,count=%x,offp=%x)\n",
4860
                        (int) fp, (int) buf, count, (int) offp);
4861
#endif
4862
 
4863
        brdnr = MINOR(fp->f_dentry->d_inode->i_rdev);
4864
        if (brdnr >= stli_nrbrds)
4865
                return(-ENODEV);
4866
        brdp = stli_brds[brdnr];
4867
        if (brdp == (stlibrd_t *) NULL)
4868
                return(-ENODEV);
4869
        if (brdp->state == 0)
4870
                return(-ENODEV);
4871
        if (fp->f_pos >= brdp->memsize)
4872
                return(0);
4873
 
4874
        size = MIN(count, (brdp->memsize - fp->f_pos));
4875
 
4876
        save_flags(flags);
4877
        cli();
4878
        EBRDENABLE(brdp);
4879
        while (size > 0) {
4880
                memptr = (void *) EBRDGETMEMPTR(brdp, fp->f_pos);
4881
                n = MIN(size, (brdp->pagesize - (((unsigned long) fp->f_pos) % brdp->pagesize)));
4882
                if (copy_to_user(buf, memptr, n)) {
4883
                        count = -EFAULT;
4884
                        goto out;
4885
                }
4886
                fp->f_pos += n;
4887
                buf += n;
4888
                size -= n;
4889
        }
4890
out:
4891
        EBRDDISABLE(brdp);
4892
        restore_flags(flags);
4893
 
4894
        return(count);
4895
}
4896
 
4897
/*****************************************************************************/
4898
 
4899
/*
4900
 *      Code to handle an "staliomem" write operation. This device is the
4901
 *      contents of the board shared memory. It is used for down loading
4902
 *      the slave image (and debugging :-)
4903
 */
4904
 
4905
static ssize_t stli_memwrite(struct file *fp, const char *buf, size_t count, loff_t *offp)
4906
{
4907
        unsigned long   flags;
4908
        void            *memptr;
4909
        stlibrd_t       *brdp;
4910
        char            *chbuf;
4911
        int             brdnr, size, n;
4912
 
4913
#if DEBUG
4914
        printk(KERN_DEBUG "stli_memwrite(fp=%x,buf=%x,count=%x,offp=%x)\n",
4915
                        (int) fp, (int) buf, count, (int) offp);
4916
#endif
4917
 
4918
        brdnr = MINOR(fp->f_dentry->d_inode->i_rdev);
4919
        if (brdnr >= stli_nrbrds)
4920
                return(-ENODEV);
4921
        brdp = stli_brds[brdnr];
4922
        if (brdp == (stlibrd_t *) NULL)
4923
                return(-ENODEV);
4924
        if (brdp->state == 0)
4925
                return(-ENODEV);
4926
        if (fp->f_pos >= brdp->memsize)
4927
                return(0);
4928
 
4929
        chbuf = (char *) buf;
4930
        size = MIN(count, (brdp->memsize - fp->f_pos));
4931
 
4932
        save_flags(flags);
4933
        cli();
4934
        EBRDENABLE(brdp);
4935
        while (size > 0) {
4936
                memptr = (void *) EBRDGETMEMPTR(brdp, fp->f_pos);
4937
                n = MIN(size, (brdp->pagesize - (((unsigned long) fp->f_pos) % brdp->pagesize)));
4938
                if (copy_from_user(memptr, chbuf, n)) {
4939
                        count = -EFAULT;
4940
                        goto out;
4941
                }
4942
                fp->f_pos += n;
4943
                chbuf += n;
4944
                size -= n;
4945
        }
4946
out:
4947
        EBRDDISABLE(brdp);
4948
        restore_flags(flags);
4949
 
4950
        return(count);
4951
}
4952
 
4953
/*****************************************************************************/
4954
 
4955
/*
4956
 *      Return the board stats structure to user app.
4957
 */
4958
 
4959
static int stli_getbrdstats(combrd_t *bp)
4960
{
4961
        stlibrd_t       *brdp;
4962
        int             i;
4963
 
4964
        if (copy_from_user(&stli_brdstats, bp, sizeof(combrd_t)))
4965
                return -EFAULT;
4966
        if (stli_brdstats.brd >= STL_MAXBRDS)
4967
                return(-ENODEV);
4968
        brdp = stli_brds[stli_brdstats.brd];
4969
        if (brdp == (stlibrd_t *) NULL)
4970
                return(-ENODEV);
4971
 
4972
        memset(&stli_brdstats, 0, sizeof(combrd_t));
4973
        stli_brdstats.brd = brdp->brdnr;
4974
        stli_brdstats.type = brdp->brdtype;
4975
        stli_brdstats.hwid = 0;
4976
        stli_brdstats.state = brdp->state;
4977
        stli_brdstats.ioaddr = brdp->iobase;
4978
        stli_brdstats.memaddr = brdp->memaddr;
4979
        stli_brdstats.nrpanels = brdp->nrpanels;
4980
        stli_brdstats.nrports = brdp->nrports;
4981
        for (i = 0; (i < brdp->nrpanels); i++) {
4982
                stli_brdstats.panels[i].panel = i;
4983
                stli_brdstats.panels[i].hwid = brdp->panelids[i];
4984
                stli_brdstats.panels[i].nrports = brdp->panels[i];
4985
        }
4986
 
4987
        if (copy_to_user(bp, &stli_brdstats, sizeof(combrd_t)))
4988
                return -EFAULT;
4989
        return(0);
4990
}
4991
 
4992
/*****************************************************************************/
4993
 
4994
/*
4995
 *      Resolve the referenced port number into a port struct pointer.
4996
 */
4997
 
4998
static stliport_t *stli_getport(int brdnr, int panelnr, int portnr)
4999
{
5000
        stlibrd_t       *brdp;
5001
        int             i;
5002
 
5003
        if ((brdnr < 0) || (brdnr >= STL_MAXBRDS))
5004
                return((stliport_t *) NULL);
5005
        brdp = stli_brds[brdnr];
5006
        if (brdp == (stlibrd_t *) NULL)
5007
                return((stliport_t *) NULL);
5008
        for (i = 0; (i < panelnr); i++)
5009
                portnr += brdp->panels[i];
5010
        if ((portnr < 0) || (portnr >= brdp->nrports))
5011
                return((stliport_t *) NULL);
5012
        return(brdp->ports[portnr]);
5013
}
5014
 
5015
/*****************************************************************************/
5016
 
5017
/*
5018
 *      Return the port stats structure to user app. A NULL port struct
5019
 *      pointer passed in means that we need to find out from the app
5020
 *      what port to get stats for (used through board control device).
5021
 */
5022
 
5023
static int stli_portcmdstats(stliport_t *portp)
5024
{
5025
        unsigned long   flags;
5026
        stlibrd_t       *brdp;
5027
        int             rc;
5028
 
5029
        memset(&stli_comstats, 0, sizeof(comstats_t));
5030
 
5031
        if (portp == (stliport_t *) NULL)
5032
                return(-ENODEV);
5033
        brdp = stli_brds[portp->brdnr];
5034
        if (brdp == (stlibrd_t *) NULL)
5035
                return(-ENODEV);
5036
 
5037
        if (brdp->state & BST_STARTED) {
5038
                if ((rc = stli_cmdwait(brdp, portp, A_GETSTATS,
5039
                    &stli_cdkstats, sizeof(asystats_t), 1)) < 0)
5040
                        return(rc);
5041
        } else {
5042
                memset(&stli_cdkstats, 0, sizeof(asystats_t));
5043
        }
5044
 
5045
        stli_comstats.brd = portp->brdnr;
5046
        stli_comstats.panel = portp->panelnr;
5047
        stli_comstats.port = portp->portnr;
5048
        stli_comstats.state = portp->state;
5049
        stli_comstats.flags = portp->flags;
5050
 
5051
        save_flags(flags);
5052
        cli();
5053
        if (portp->tty != (struct tty_struct *) NULL) {
5054
                if (portp->tty->driver_data == portp) {
5055
                        stli_comstats.ttystate = portp->tty->flags;
5056
                        stli_comstats.rxbuffered = portp->tty->flip.count;
5057
                        if (portp->tty->termios != (struct termios *) NULL) {
5058
                                stli_comstats.cflags = portp->tty->termios->c_cflag;
5059
                                stli_comstats.iflags = portp->tty->termios->c_iflag;
5060
                                stli_comstats.oflags = portp->tty->termios->c_oflag;
5061
                                stli_comstats.lflags = portp->tty->termios->c_lflag;
5062
                        }
5063
                }
5064
        }
5065
        restore_flags(flags);
5066
 
5067
        stli_comstats.txtotal = stli_cdkstats.txchars;
5068
        stli_comstats.rxtotal = stli_cdkstats.rxchars + stli_cdkstats.ringover;
5069
        stli_comstats.txbuffered = stli_cdkstats.txringq;
5070
        stli_comstats.rxbuffered += stli_cdkstats.rxringq;
5071
        stli_comstats.rxoverrun = stli_cdkstats.overruns;
5072
        stli_comstats.rxparity = stli_cdkstats.parity;
5073
        stli_comstats.rxframing = stli_cdkstats.framing;
5074
        stli_comstats.rxlost = stli_cdkstats.ringover;
5075
        stli_comstats.rxbreaks = stli_cdkstats.rxbreaks;
5076
        stli_comstats.txbreaks = stli_cdkstats.txbreaks;
5077
        stli_comstats.txxon = stli_cdkstats.txstart;
5078
        stli_comstats.txxoff = stli_cdkstats.txstop;
5079
        stli_comstats.rxxon = stli_cdkstats.rxstart;
5080
        stli_comstats.rxxoff = stli_cdkstats.rxstop;
5081
        stli_comstats.rxrtsoff = stli_cdkstats.rtscnt / 2;
5082
        stli_comstats.rxrtson = stli_cdkstats.rtscnt - stli_comstats.rxrtsoff;
5083
        stli_comstats.modem = stli_cdkstats.dcdcnt;
5084
        stli_comstats.hwid = stli_cdkstats.hwid;
5085
        stli_comstats.signals = stli_mktiocm(stli_cdkstats.signals);
5086
 
5087
        return(0);
5088
}
5089
 
5090
/*****************************************************************************/
5091
 
5092
/*
5093
 *      Return the port stats structure to user app. A NULL port struct
5094
 *      pointer passed in means that we need to find out from the app
5095
 *      what port to get stats for (used through board control device).
5096
 */
5097
 
5098
static int stli_getportstats(stliport_t *portp, comstats_t *cp)
5099
{
5100
        stlibrd_t       *brdp;
5101
        int             rc;
5102
 
5103
        if (portp == (stliport_t *) NULL) {
5104
                if (copy_from_user(&stli_comstats, cp, sizeof(comstats_t)))
5105
                        return -EFAULT;
5106
                portp = stli_getport(stli_comstats.brd, stli_comstats.panel,
5107
                        stli_comstats.port);
5108
                if (portp == (stliport_t *) NULL)
5109
                        return(-ENODEV);
5110
        }
5111
 
5112
        brdp = stli_brds[portp->brdnr];
5113
        if (brdp == (stlibrd_t *) NULL)
5114
                return(-ENODEV);
5115
 
5116
        if ((rc = stli_portcmdstats(portp)) < 0)
5117
                return(rc);
5118
 
5119
        return copy_to_user(cp, &stli_comstats, sizeof(comstats_t)) ?
5120
                        -EFAULT : 0;
5121
}
5122
 
5123
/*****************************************************************************/
5124
 
5125
/*
5126
 *      Clear the port stats structure. We also return it zeroed out...
5127
 */
5128
 
5129
static int stli_clrportstats(stliport_t *portp, comstats_t *cp)
5130
{
5131
        stlibrd_t       *brdp;
5132
        int             rc;
5133
 
5134
        if (portp == (stliport_t *) NULL) {
5135
                if (copy_from_user(&stli_comstats, cp, sizeof(comstats_t)))
5136
                        return -EFAULT;
5137
                portp = stli_getport(stli_comstats.brd, stli_comstats.panel,
5138
                        stli_comstats.port);
5139
                if (portp == (stliport_t *) NULL)
5140
                        return(-ENODEV);
5141
        }
5142
 
5143
        brdp = stli_brds[portp->brdnr];
5144
        if (brdp == (stlibrd_t *) NULL)
5145
                return(-ENODEV);
5146
 
5147
        if (brdp->state & BST_STARTED) {
5148
                if ((rc = stli_cmdwait(brdp, portp, A_CLEARSTATS, 0, 0, 0)) < 0)
5149
                        return(rc);
5150
        }
5151
 
5152
        memset(&stli_comstats, 0, sizeof(comstats_t));
5153
        stli_comstats.brd = portp->brdnr;
5154
        stli_comstats.panel = portp->panelnr;
5155
        stli_comstats.port = portp->portnr;
5156
 
5157
        if (copy_to_user(cp, &stli_comstats, sizeof(comstats_t)))
5158
                return -EFAULT;
5159
        return(0);
5160
}
5161
 
5162
/*****************************************************************************/
5163
 
5164
/*
5165
 *      Return the entire driver ports structure to a user app.
5166
 */
5167
 
5168
static int stli_getportstruct(unsigned long arg)
5169
{
5170
        stliport_t      *portp;
5171
 
5172
        if (copy_from_user(&stli_dummyport, (void *)arg, sizeof(stliport_t)))
5173
                return -EFAULT;
5174
        portp = stli_getport(stli_dummyport.brdnr, stli_dummyport.panelnr,
5175
                 stli_dummyport.portnr);
5176
        if (portp == (stliport_t *) NULL)
5177
                return(-ENODEV);
5178
        if (copy_to_user((void *) arg, portp, sizeof(stliport_t)))
5179
                return -EFAULT;
5180
        return(0);
5181
}
5182
 
5183
/*****************************************************************************/
5184
 
5185
/*
5186
 *      Return the entire driver board structure to a user app.
5187
 */
5188
 
5189
static int stli_getbrdstruct(unsigned long arg)
5190
{
5191
        stlibrd_t       *brdp;
5192
 
5193
        if (copy_from_user(&stli_dummybrd, (void *)arg, sizeof(stlibrd_t)))
5194
                return -EFAULT;
5195
        if ((stli_dummybrd.brdnr < 0) || (stli_dummybrd.brdnr >= STL_MAXBRDS))
5196
                return(-ENODEV);
5197
        brdp = stli_brds[stli_dummybrd.brdnr];
5198
        if (brdp == (stlibrd_t *) NULL)
5199
                return(-ENODEV);
5200
        if (copy_to_user((void *) arg, brdp, sizeof(stlibrd_t)))
5201
                return -EFAULT;
5202
        return(0);
5203
}
5204
 
5205
/*****************************************************************************/
5206
 
5207
/*
5208
 *      The "staliomem" device is also required to do some special operations on
5209
 *      the board. We need to be able to send an interrupt to the board,
5210
 *      reset it, and start/stop it.
5211
 */
5212
 
5213
static int stli_memioctl(struct inode *ip, struct file *fp, unsigned int cmd, unsigned long arg)
5214
{
5215
        stlibrd_t       *brdp;
5216
        int             brdnr, rc, done;
5217
 
5218
#if DEBUG
5219
        printk(KERN_DEBUG "stli_memioctl(ip=%x,fp=%x,cmd=%x,arg=%x)\n",
5220
                        (int) ip, (int) fp, cmd, (int) arg);
5221
#endif
5222
 
5223
/*
5224
 *      First up handle the board independent ioctls.
5225
 */
5226
        done = 0;
5227
        rc = 0;
5228
 
5229
        switch (cmd) {
5230
        case COM_GETPORTSTATS:
5231
                rc = stli_getportstats((stliport_t *)NULL, (comstats_t *)arg);
5232
                done++;
5233
                break;
5234
        case COM_CLRPORTSTATS:
5235
                rc = stli_clrportstats((stliport_t *)NULL, (comstats_t *)arg);
5236
                done++;
5237
                break;
5238
        case COM_GETBRDSTATS:
5239
                rc = stli_getbrdstats((combrd_t *) arg);
5240
                done++;
5241
                break;
5242
        case COM_READPORT:
5243
                rc = stli_getportstruct(arg);
5244
                done++;
5245
                break;
5246
        case COM_READBOARD:
5247
                rc = stli_getbrdstruct(arg);
5248
                done++;
5249
                break;
5250
        }
5251
 
5252
        if (done)
5253
                return(rc);
5254
 
5255
/*
5256
 *      Now handle the board specific ioctls. These all depend on the
5257
 *      minor number of the device they were called from.
5258
 */
5259
        brdnr = MINOR(ip->i_rdev);
5260
        if (brdnr >= STL_MAXBRDS)
5261
                return(-ENODEV);
5262
        brdp = stli_brds[brdnr];
5263
        if (brdp == (stlibrd_t *) NULL)
5264
                return(-ENODEV);
5265
        if (brdp->state == 0)
5266
                return(-ENODEV);
5267
 
5268
        switch (cmd) {
5269
        case STL_BINTR:
5270
                EBRDINTR(brdp);
5271
                break;
5272
        case STL_BSTART:
5273
                rc = stli_startbrd(brdp);
5274
                break;
5275
        case STL_BSTOP:
5276
                brdp->state &= ~BST_STARTED;
5277
                break;
5278
        case STL_BRESET:
5279
                brdp->state &= ~BST_STARTED;
5280
                EBRDRESET(brdp);
5281
                if (stli_shared == 0) {
5282
                        if (brdp->reenable != NULL)
5283
                                (* brdp->reenable)(brdp);
5284
                }
5285
                break;
5286
        default:
5287
                rc = -ENOIOCTLCMD;
5288
                break;
5289
        }
5290
 
5291
        return(rc);
5292
}
5293
 
5294
/*****************************************************************************/
5295
 
5296
int __init stli_init(void)
5297
{
5298
        printk(KERN_INFO "%s: version %s\n", stli_drvtitle, stli_drvversion);
5299
 
5300
        stli_initbrds();
5301
 
5302
/*
5303
 *      Allocate a temporary write buffer.
5304
 */
5305
        stli_tmpwritebuf = (char *) stli_memalloc(STLI_TXBUFSIZE);
5306
        if (stli_tmpwritebuf == (char *) NULL)
5307
                printk(KERN_ERR "STALLION: failed to allocate memory "
5308
                                "(size=%d)\n", STLI_TXBUFSIZE);
5309
        stli_txcookbuf = stli_memalloc(STLI_TXBUFSIZE);
5310
        if (stli_txcookbuf == (char *) NULL)
5311
                printk(KERN_ERR "STALLION: failed to allocate memory "
5312
                                "(size=%d)\n", STLI_TXBUFSIZE);
5313
 
5314
/*
5315
 *      Set up a character driver for the shared memory region. We need this
5316
 *      to down load the slave code image. Also it is a useful debugging tool.
5317
 */
5318
        if (devfs_register_chrdev(STL_SIOMEMMAJOR, "staliomem", &stli_fsiomem))
5319
                printk(KERN_ERR "STALLION: failed to register serial memory "
5320
                                "device\n");
5321
 
5322
        devfs_handle = devfs_mk_dir (NULL, "staliomem", NULL);
5323
        devfs_register_series (devfs_handle, "%u", 4, DEVFS_FL_DEFAULT,
5324
                               STL_SIOMEMMAJOR, 0,
5325
                               S_IFCHR | S_IRUSR | S_IWUSR,
5326
                               &stli_fsiomem, NULL);
5327
 
5328
/*
5329
 *      Set up the tty driver structure and register us as a driver.
5330
 *      Also setup the callout tty device.
5331
 */
5332
        memset(&stli_serial, 0, sizeof(struct tty_driver));
5333
        stli_serial.magic = TTY_DRIVER_MAGIC;
5334
        stli_serial.driver_name = stli_drvname;
5335
        stli_serial.name = stli_serialname;
5336
        stli_serial.major = STL_SERIALMAJOR;
5337
        stli_serial.minor_start = 0;
5338
        stli_serial.num = STL_MAXBRDS * STL_MAXPORTS;
5339
        stli_serial.type = TTY_DRIVER_TYPE_SERIAL;
5340
        stli_serial.subtype = STL_DRVTYPSERIAL;
5341
        stli_serial.init_termios = stli_deftermios;
5342
        stli_serial.flags = TTY_DRIVER_REAL_RAW;
5343
        stli_serial.refcount = &stli_refcount;
5344
        stli_serial.table = stli_ttys;
5345
        stli_serial.termios = stli_termios;
5346
        stli_serial.termios_locked = stli_termioslocked;
5347
 
5348
        stli_serial.open = stli_open;
5349
        stli_serial.close = stli_close;
5350
        stli_serial.write = stli_write;
5351
        stli_serial.put_char = stli_putchar;
5352
        stli_serial.flush_chars = stli_flushchars;
5353
        stli_serial.write_room = stli_writeroom;
5354
        stli_serial.chars_in_buffer = stli_charsinbuffer;
5355
        stli_serial.ioctl = stli_ioctl;
5356
        stli_serial.set_termios = stli_settermios;
5357
        stli_serial.throttle = stli_throttle;
5358
        stli_serial.unthrottle = stli_unthrottle;
5359
        stli_serial.stop = stli_stop;
5360
        stli_serial.start = stli_start;
5361
        stli_serial.hangup = stli_hangup;
5362
        stli_serial.flush_buffer = stli_flushbuffer;
5363
        stli_serial.break_ctl = stli_breakctl;
5364
        stli_serial.wait_until_sent = stli_waituntilsent;
5365
        stli_serial.send_xchar = stli_sendxchar;
5366
        stli_serial.read_proc = stli_readproc;
5367
 
5368
        stli_callout = stli_serial;
5369
        stli_callout.name = stli_calloutname;
5370
        stli_callout.major = STL_CALLOUTMAJOR;
5371
        stli_callout.subtype = STL_DRVTYPCALLOUT;
5372
        stli_callout.read_proc = 0;
5373
 
5374
        if (tty_register_driver(&stli_serial))
5375
                printk(KERN_ERR "STALLION: failed to register serial driver\n");
5376
        if (tty_register_driver(&stli_callout))
5377
                printk(KERN_ERR "STALLION: failed to register callout driver\n");
5378
 
5379
        return(0);
5380
}
5381
 
5382
/*****************************************************************************/

powered by: WebSVN 2.1.0

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