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

Subversion Repositories or1k

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

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1275 phoenix
 
2
/* sx.c -- driver for the Specialix SX series cards.
3
 *
4
 *  This driver will also support the older SI, and XIO cards.
5
 *
6
 *
7
 *   (C) 1998 - 2000  R.E.Wolff@BitWizard.nl
8
 *
9
 *  Simon Allen (simonallen@cix.compulink.co.uk) wrote a previous
10
 *  version of this driver. Some fragments may have been copied. (none
11
 *  yet :-)
12
 *
13
 * Specialix pays for the development and support of this driver.
14
 * Please DO contact support@specialix.co.uk if you require
15
 * support. But please read the documentation (sx.txt) first.
16
 *
17
 *
18
 *
19
 *      This program is free software; you can redistribute it and/or
20
 *      modify it under the terms of the GNU General Public License as
21
 *      published by the Free Software Foundation; either version 2 of
22
 *      the License, or (at your option) any later version.
23
 *
24
 *      This program is distributed in the hope that it will be
25
 *      useful, but WITHOUT ANY WARRANTY; without even the implied
26
 *      warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
27
 *      PURPOSE.  See the GNU General Public License for more details.
28
 *
29
 *      You should have received a copy of the GNU General Public
30
 *      License along with this program; if not, write to the Free
31
 *      Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139,
32
 *      USA.
33
 *
34
 * Revision history:
35
 * $Log: not supported by cvs2svn $
36
 * Revision 1.33  2000/03/09 10:00:00  pvdl,wolff
37
 * - Fixed module and port counting
38
 * - Fixed signal handling
39
 * - Fixed an Ooops
40
 *
41
 * Revision 1.32  2000/03/07 09:00:00  wolff,pvdl
42
 * - Fixed some sx_dprintk typos
43
 * - added detection for an invalid board/module configuration
44
 *
45
 * Revision 1.31  2000/03/06 12:00:00  wolff,pvdl
46
 * - Added support for EISA
47
 *
48
 * Revision 1.30  2000/01/21 17:43:06  wolff
49
 * - Added support for SX+
50
 *
51
 * Revision 1.26  1999/08/05 15:22:14  wolff
52
 * - Port to 2.3.x
53
 * - Reformatted to Linus' liking.
54
 *
55
 * Revision 1.25  1999/07/30 14:24:08  wolff
56
 * Had accidentally left "gs_debug" set to "-1" instead of "off" (=0).
57
 *
58
 * Revision 1.24  1999/07/28 09:41:52  wolff
59
 * - I noticed the remark about use-count straying in sx.txt. I checked
60
 *   sx_open, and found a few places where that could happen. I hope it's
61
 *   fixed now.
62
 *
63
 * Revision 1.23  1999/07/28 08:56:06  wolff
64
 * - Fixed crash when sx_firmware run twice.
65
 * - Added sx_slowpoll as a module parameter (I guess nobody really wanted
66
 *   to change it from the default... )
67
 * - Fixed a stupid editing problem I introduced in 1.22.
68
 * - Fixed dropping characters on a termios change.
69
 *
70
 * Revision 1.22  1999/07/26 21:01:43  wolff
71
 * Russell Brown noticed that I had overlooked 4 out of six modem control
72
 * signals in sx_getsignals. Ooops.
73
 *
74
 * Revision 1.21  1999/07/23 09:11:33  wolff
75
 * I forgot to free dynamically allocated memory when the driver is unloaded.
76
 *
77
 * Revision 1.20  1999/07/20 06:25:26  wolff
78
 * The "closing wait" wasn't honoured. Thanks to James Griffiths for
79
 * reporting this.
80
 *
81
 * Revision 1.19  1999/07/11 08:59:59  wolff
82
 * Fixed an oops in close, when an open was pending. Changed the memtest
83
 * a bit. Should also test the board in word-mode, however my card fails the
84
 * memtest then. I still have to figure out what is wrong...
85
 *
86
 * Revision 1.18  1999/06/10 09:38:42  wolff
87
 * Changed the format of the firmware revision from %04x to %x.%02x .
88
 *
89
 * Revision 1.17  1999/06/04 09:44:35  wolff
90
 * fixed problem: reference to pci stuff when config_pci was off...
91
 * Thanks to Jorge Novo for noticing this.
92
 *
93
 * Revision 1.16  1999/06/02 08:30:15  wolff
94
 * added/removed the workaround for the DCD bug in the Firmware.
95
 * A bit more debugging code to locate that...
96
 *
97
 * Revision 1.15  1999/06/01 11:35:30  wolff
98
 * when DCD is left low (floating?), on TA's the firmware first tells us
99
 * that DCD is high, but after a short while suddenly comes to the
100
 * conclusion that it is low. All this would be fine, if it weren't that
101
 * Unix requires us to send a "hangup" signal in that case. This usually
102
 * all happens BEFORE the program has had a chance to ioctl the device
103
 * into clocal mode..
104
 *
105
 * Revision 1.14  1999/05/25 11:18:59  wolff
106
 * Added PCI-fix.
107
 * Added checks for return code of sx_sendcommand.
108
 * Don't issue "reconfig" if port isn't open yet. (bit us on TA modules...)
109
 *
110
 * Revision 1.13  1999/04/29 15:18:01  wolff
111
 * Fixed an "oops" that showed on SuSE 6.0 systems.
112
 * Activate DTR again after stty 0.
113
 *
114
 * Revision 1.12  1999/04/29 07:49:52  wolff
115
 * Improved "stty 0" handling a bit. (used to change baud to 9600 assuming
116
 *     the connection would be dropped anyway. That is not always the case,
117
 *     and confuses people).
118
 * Told the card to always monitor the modem signals.
119
 * Added support for dynamic  gs_debug adjustments.
120
 * Now tells the rest of the system the number of ports.
121
 *
122
 * Revision 1.11  1999/04/24 11:11:30  wolff
123
 * Fixed two stupid typos in the memory test.
124
 *
125
 * Revision 1.10  1999/04/24 10:53:39  wolff
126
 * Added some of Christian's suggestions.
127
 * Fixed an HW_COOK_IN bug (ISIG was not in I_OTHER. We used to trust the
128
 * card to send the signal to the process.....)
129
 *
130
 * Revision 1.9  1999/04/23 07:26:38  wolff
131
 * Included Christian Lademann's 2.0 compile-warning fixes and interrupt
132
 *    assignment redesign.
133
 * Cleanup of some other stuff.
134
 *
135
 * Revision 1.8  1999/04/16 13:05:30  wolff
136
 * fixed a DCD change unnoticed bug.
137
 *
138
 * Revision 1.7  1999/04/14 22:19:51  wolff
139
 * Fixed typo that showed up in 2.0.x builds (get_user instead of Get_user!)
140
 *
141
 * Revision 1.6  1999/04/13 18:40:20  wolff
142
 * changed misc-minor to 161, as assigned by HPA.
143
 *
144
 * Revision 1.5  1999/04/13 15:12:25  wolff
145
 * Fixed use-count leak when "hangup" occurred.
146
 * Added workaround for a stupid-PCIBIOS bug.
147
 *
148
 *
149
 * Revision 1.4  1999/04/01 22:47:40  wolff
150
 * Fixed < 1M linux-2.0 problem.
151
 * (vremap isn't compatible with ioremap in that case)
152
 *
153
 * Revision 1.3  1999/03/31 13:45:45  wolff
154
 * Firmware loading is now done through a separate IOCTL.
155
 *
156
 * Revision 1.2  1999/03/28 12:22:29  wolff
157
 * rcs cleanup
158
 *
159
 * Revision 1.1  1999/03/28 12:10:34  wolff
160
 * Readying for release on 2.0.x (sorry David, 1.01 becomes 1.1 for RCS).
161
 *
162
 * Revision 0.12  1999/03/28 09:20:10  wolff
163
 * Fixed problem in 0.11, continueing cleanup.
164
 *
165
 * Revision 0.11  1999/03/28 08:46:44  wolff
166
 * cleanup. Not good.
167
 *
168
 * Revision 0.10  1999/03/28 08:09:43  wolff
169
 * Fixed loosing characters on close.
170
 *
171
 * Revision 0.9  1999/03/21 22:52:01  wolff
172
 * Ported back to 2.2.... (minor things)
173
 *
174
 * Revision 0.8  1999/03/21 22:40:33  wolff
175
 * Port to 2.0
176
 *
177
 * Revision 0.7  1999/03/21 19:06:34  wolff
178
 * Fixed hangup processing.
179
 *
180
 * Revision 0.6  1999/02/05 08:45:14  wolff
181
 * fixed real_raw problems. Inclusion into kernel imminent.
182
 *
183
 * Revision 0.5  1998/12/21 23:51:06  wolff
184
 * Snatched a nasty bug: sx_transmit_chars was getting re-entered, and it
185
 * shouldn't have. THATs why I want to have transmit interrupts even when
186
 * the buffer is empty.
187
 *
188
 * Revision 0.4  1998/12/17 09:34:46  wolff
189
 * PPP works. ioctl works. Basically works!
190
 *
191
 * Revision 0.3  1998/12/15 13:05:18  wolff
192
 * It works! Wow! Gotta start implementing IOCTL and stuff....
193
 *
194
 * Revision 0.2  1998/12/01 08:33:53  wolff
195
 * moved over to 2.1.130
196
 *
197
 * Revision 0.1  1998/11/03 21:23:51  wolff
198
 * Initial revision. Detects SX card.
199
 *
200
 * */
201
 
202
 
203
#define RCS_ID "$Id: sx.c,v 1.1.1.1 2004-04-15 01:58:04 phoenix Exp $"
204
#define RCS_REV "$Revision: 1.1.1.1 $"
205
 
206
 
207
#include <linux/module.h>
208
#include <linux/config.h> 
209
#include <linux/kdev_t.h>
210
#include <asm/io.h>
211
#include <linux/kernel.h>
212
#include <linux/sched.h>
213
#include <linux/ioport.h>
214
#include <linux/interrupt.h>
215
#include <linux/errno.h>
216
#include <linux/tty.h>
217
#include <linux/tty_flip.h>
218
#include <linux/mm.h>
219
#include <linux/serial.h>
220
#include <linux/fcntl.h>
221
#include <linux/major.h>
222
#include <linux/delay.h>
223
#include <linux/tqueue.h>
224
#include <linux/version.h>
225
#include <linux/pci.h>
226
#include <linux/slab.h>
227
#include <linux/init.h>
228
#include <linux/miscdevice.h>
229
 
230
/* The 3.0.0 version of sxboards/sxwindow.h  uses BYTE and WORD.... */
231
#define BYTE u8
232
#define WORD u16
233
 
234
/* .... but the 3.0.4 version uses _u8 and _u16. */
235
#define _u8 u8
236
#define _u16 u16
237
 
238
#include "sxboards.h"
239
#include "sxwindow.h"
240
 
241
#include <linux/compatmac.h>
242
#include <linux/generic_serial.h>
243
#include "sx.h"
244
 
245
 
246
/* I don't think that this driver can handle more than 256 ports on
247
   one machine. You'll have to increase the number of boards in sx.h
248
   if you want more than 4 boards.  */
249
 
250
 
251
/* Why the hell am I defining these here? */
252
#define SX_TYPE_NORMAL 1
253
#define SX_TYPE_CALLOUT 2
254
 
255
 
256
#ifndef PCI_DEVICE_ID_SPECIALIX_SX_XIO_IO8
257
#define PCI_DEVICE_ID_SPECIALIX_SX_XIO_IO8 0x2000
258
#endif
259
 
260
static struct pci_device_id sx_pci_tbl[] = {
261
        { PCI_VENDOR_ID_SPECIALIX, PCI_DEVICE_ID_SPECIALIX_SX_XIO_IO8, PCI_ANY_ID, PCI_ANY_ID },
262
        { 0 }
263
};
264
MODULE_DEVICE_TABLE(pci, sx_pci_tbl);
265
 
266
/* Configurable options:
267
   (Don't be too sure that it'll work if you toggle them) */
268
 
269
/* Am I paranoid or not ? ;-) */
270
#undef SX_PARANOIA_CHECK
271
 
272
 
273
/* 20 -> 2000 per second. The card should rate-limit interrupts at 100
274
   Hz, but it is user configurable. I don't recommend going above 1000
275
   Hz. The interrupt ratelimit might trigger if the interrupt is
276
   shared with a very active other device. */
277
#define IRQ_RATE_LIMIT 20
278
 
279
/* Sharing interrupts is possible now. If the other device wants more
280
   than 2000 interrupts per second, we'd gracefully decline further
281
   interrupts. That's not what we want. On the other hand, if the
282
   other device interrupts 2000 times a second, don't use the SX
283
   interrupt. Use polling. */
284
#undef IRQ_RATE_LIMIT
285
 
286
 
287
#if 0
288
/* Not implemented */
289
/*
290
 * The following defines are mostly for testing purposes. But if you need
291
 * some nice reporting in your syslog, you can define them also.
292
 */
293
#define SX_REPORT_FIFO
294
#define SX_REPORT_OVERRUN
295
#endif 
296
 
297
 
298
/* Function prototypes */
299
static void sx_disable_tx_interrupts (void * ptr);
300
static void sx_enable_tx_interrupts (void * ptr);
301
static void sx_disable_rx_interrupts (void * ptr);
302
static void sx_enable_rx_interrupts (void * ptr);
303
static int  sx_get_CD (void * ptr);
304
static void sx_shutdown_port (void * ptr);
305
static int  sx_set_real_termios (void  *ptr);
306
static void sx_hungup (void  *ptr);
307
static void sx_close (void  *ptr);
308
static int sx_chars_in_buffer (void * ptr);
309
static int sx_init_board (struct sx_board *board);
310
static int sx_init_portstructs (int nboards, int nports);
311
static int sx_fw_ioctl (struct inode *inode, struct file *filp,
312
                        unsigned int cmd, unsigned long arg);
313
static int sx_init_drivers(void);
314
 
315
 
316
static struct tty_driver sx_driver, sx_callout_driver;
317
 
318
static struct tty_struct * sx_table[SX_NPORTS];
319
static struct termios ** sx_termios;
320
static struct termios ** sx_termios_locked;
321
 
322
static struct sx_board boards[SX_NBOARDS];
323
static struct sx_port *sx_ports;
324
static int sx_refcount;
325
static int sx_initialized;
326
static int sx_nports;
327
static int sx_debug;
328
 
329
 
330
/* You can have the driver poll your card.
331
    - Set sx_poll to 1 to poll every timer tick (10ms on Intel).
332
      This is used when the card cannot use an interrupt for some reason.
333
 
334
    - set sx_slowpoll to 100 to do an extra poll once a second (on Intel). If
335
      the driver misses an interrupt (report this if it DOES happen to you!)
336
      everything will continue to work....
337
 */
338
static int sx_poll = 1;
339
static int sx_slowpoll;
340
 
341
/* The card limits the number of interrupts per second.
342
   At 115k2 "100" should be sufficient.
343
   If you're using higher baudrates, you can increase this...
344
 */
345
 
346
static int sx_maxints = 100;
347
 
348
/* These are the only open spaces in my computer. Yours may have more
349
   or less.... -- REW
350
   duh: Card at 0xa0000 is possible on HP Netserver?? -- pvdl
351
*/
352
static int sx_probe_addrs[]= {0xc0000, 0xd0000, 0xe0000,
353
                              0xc8000, 0xd8000, 0xe8000};
354
static int si_probe_addrs[]= {0xc0000, 0xd0000, 0xe0000,
355
                              0xc8000, 0xd8000, 0xe8000, 0xa0000};
356
static int si1_probe_addrs[]= { 0xd0000};
357
 
358
#define NR_SX_ADDRS (sizeof(sx_probe_addrs)/sizeof (int))
359
#define NR_SI_ADDRS (sizeof(si_probe_addrs)/sizeof (int))
360
#define NR_SI1_ADDRS (sizeof(si1_probe_addrs)/sizeof (int))
361
 
362
 
363
/* Set the mask to all-ones. This alas, only supports 32 interrupts.
364
   Some architectures may need more. */
365
static int sx_irqmask = -1;
366
 
367
MODULE_PARM(sx_probe_addrs, "i");
368
MODULE_PARM(si_probe_addrs, "i");
369
MODULE_PARM(sx_poll, "i");
370
MODULE_PARM(sx_slowpoll, "i");
371
MODULE_PARM(sx_maxints, "i");
372
MODULE_PARM(sx_debug, "i");
373
MODULE_PARM(sx_irqmask, "i");
374
 
375
MODULE_LICENSE("GPL");
376
 
377
static struct real_driver sx_real_driver = {
378
        sx_disable_tx_interrupts,
379
        sx_enable_tx_interrupts,
380
        sx_disable_rx_interrupts,
381
        sx_enable_rx_interrupts,
382
        sx_get_CD,
383
        sx_shutdown_port,
384
        sx_set_real_termios,
385
        sx_chars_in_buffer,
386
        sx_close,
387
        sx_hungup,
388
};
389
 
390
 
391
/*
392
   This driver can spew a whole lot of debugging output at you. If you
393
   need maximum performance, you should disable the DEBUG define. To
394
   aid in debugging in the field, I'm leaving the compile-time debug
395
   features enabled, and disable them "runtime". That allows me to
396
   instruct people with problems to enable debugging without requiring
397
   them to recompile...
398
*/
399
#define DEBUG
400
 
401
 
402
#ifdef DEBUG
403
#define sx_dprintk(f, str...) if (sx_debug & f) printk (str)
404
#else
405
#define sx_dprintk(f, str...) /* nothing */
406
#endif
407
 
408
 
409
 
410
#define func_enter() sx_dprintk (SX_DEBUG_FLOW, "sx: enter %s\b",__FUNCTION__)
411
#define func_exit()  sx_dprintk (SX_DEBUG_FLOW, "sx: exit  %s\n", __FUNCTION__)
412
 
413
#define func_enter2() sx_dprintk (SX_DEBUG_FLOW, "sx: enter %s (port %d)\n", \
414
                                        __FUNCTION__, port->line)
415
 
416
 
417
 
418
 
419
/*
420
 *  Firmware loader driver specific routines
421
 *
422
 */
423
 
424
static struct file_operations sx_fw_fops = {
425
        owner:          THIS_MODULE,
426
        ioctl:          sx_fw_ioctl,
427
};
428
 
429
static struct miscdevice sx_fw_device = {
430
        SXCTL_MISC_MINOR, "sxctl", &sx_fw_fops
431
};
432
 
433
 
434
 
435
 
436
 
437
#ifdef SX_PARANOIA_CHECK
438
 
439
/* This doesn't work. Who's paranoid around here? Not me! */
440
 
441
static inline int sx_paranoia_check(struct sx_port const * port,
442
                                    kdev_t device, const char *routine)
443
{
444
 
445
        static const char *badmagic =
446
          KERN_ERR "sx: Warning: bad sx port magic number for device %s in %s\n";
447
        static const char *badinfo =
448
          KERN_ERR "sx: Warning: null sx port for device %s in %s\n";
449
 
450
        if (!port) {
451
                printk(badinfo, kdevname(device), routine);
452
                return 1;
453
        }
454
        if (port->magic != SX_MAGIC) {
455
                printk(badmagic, kdevname(device), routine);
456
                return 1;
457
        }
458
 
459
        return 0;
460
}
461
#else
462
#define sx_paranoia_check(a,b,c) 0
463
#endif
464
 
465
/* The timeouts. First try 30 times as fast as possible. Then give
466
   the card some time to breathe between accesses. (Otherwise the
467
   processor on the card might not be able to access its OWN bus... */
468
 
469
#define TIMEOUT_1 30
470
#define TIMEOUT_2 1000000
471
 
472
 
473
#ifdef DEBUG
474
static void my_hd (unsigned char *addr, int len)
475
{
476
        int i, j, ch;
477
 
478
        for (i=0;i<len;i+=16) {
479
                printk ("%p ", addr+i);
480
                for (j=0;j<16;j++) {
481
                        printk ("%02x %s", addr[j+i], (j==7)?" ":"");
482
                }
483
                for (j=0;j<16;j++) {
484
                        ch = addr[j+i];
485
                        printk ("%c", (ch < 0x20)?'.':((ch > 0x7f)?'.':ch));
486
                }
487
                printk ("\n");
488
        }
489
}
490
#endif
491
 
492
 
493
 
494
/* This needs redoing for Alpha -- REW -- Done. */
495
 
496
static inline void write_sx_byte (struct sx_board *board, int offset, u8 byte)
497
{
498
        writeb (byte, board->base+offset);
499
}
500
 
501
static inline u8 read_sx_byte (struct sx_board *board, int offset)
502
{
503
        return readb (board->base+offset);
504
}
505
 
506
 
507
static inline void write_sx_word (struct sx_board *board, int offset, u16 word)
508
{
509
        writew (word, board->base+offset);
510
}
511
 
512
static inline u16 read_sx_word (struct sx_board *board, int offset)
513
{
514
        return readw (board->base + offset);
515
}
516
 
517
 
518
static int sx_busy_wait_eq (struct sx_board *board,
519
                            int offset, int mask, int correctval)
520
{
521
        int i;
522
 
523
        func_enter ();
524
 
525
        for (i=0; i < TIMEOUT_1 > 0;i++)
526
                if ((read_sx_byte (board, offset) & mask) == correctval) {
527
                        func_exit ();
528
                        return 1;
529
                }
530
 
531
        for (i=0; i < TIMEOUT_2 > 0;i++) {
532
                if ((read_sx_byte (board, offset) & mask) == correctval) {
533
                        func_exit ();
534
                        return 1;
535
                }
536
                udelay (1);
537
        }
538
 
539
        func_exit ();
540
        return 0;
541
}
542
 
543
 
544
static int sx_busy_wait_neq (struct sx_board *board,
545
                             int offset, int mask, int badval)
546
{
547
        int i;
548
 
549
        func_enter ();
550
 
551
        for (i=0; i < TIMEOUT_1 > 0;i++)
552
                if ((read_sx_byte (board, offset) & mask) != badval) {
553
                        func_exit ();
554
                        return 1;
555
                }
556
 
557
        for (i=0; i < TIMEOUT_2 > 0;i++) {
558
                if ((read_sx_byte (board, offset) & mask) != badval) {
559
                        func_exit ();
560
                        return 1;
561
                }
562
                udelay (1);
563
        }
564
 
565
        func_exit ();
566
        return 0;
567
}
568
 
569
 
570
 
571
/* 5.6.4 of 6210028 r2.3 */
572
static int sx_reset (struct sx_board *board)
573
{
574
        func_enter ();
575
 
576
        if (IS_SX_BOARD (board)) {
577
 
578
                write_sx_byte (board, SX_CONFIG, 0);
579
                write_sx_byte (board, SX_RESET, 1); /* Value doesn't matter */
580
 
581
                if (!sx_busy_wait_eq (board, SX_RESET_STATUS, 1, 0)) {
582
                        printk (KERN_INFO "sx: Card doesn't respond to reset....\n");
583
                        return 0;
584
                }
585
        } else if (IS_EISA_BOARD(board)) {
586
                outb(board->irq<<4, board->eisa_base+0xc02);
587
        } else if (IS_SI1_BOARD(board)) {
588
                write_sx_byte (board, SI1_ISA_RESET,   0); // value does not matter
589
        } else {
590
                /* Gory details of the SI/ISA board */
591
                write_sx_byte (board, SI2_ISA_RESET,    SI2_ISA_RESET_SET);
592
                write_sx_byte (board, SI2_ISA_IRQ11,    SI2_ISA_IRQ11_CLEAR);
593
                write_sx_byte (board, SI2_ISA_IRQ12,    SI2_ISA_IRQ12_CLEAR);
594
                write_sx_byte (board, SI2_ISA_IRQ15,    SI2_ISA_IRQ15_CLEAR);
595
                write_sx_byte (board, SI2_ISA_INTCLEAR, SI2_ISA_INTCLEAR_CLEAR);
596
                write_sx_byte (board, SI2_ISA_IRQSET,   SI2_ISA_IRQSET_CLEAR);
597
        }
598
 
599
        func_exit ();
600
        return 1;
601
}
602
 
603
 
604
/* This doesn't work on machines where "NULL" isn't 0 */
605
/* If you have one of those, someone will need to write
606
   the equivalent of this, which will amount to about 3 lines. I don't
607
   want to complicate this right now. -- REW
608
   (See, I do write comments every now and then :-) */
609
#define OFFSETOF(strct, elem) ((long)&(((struct strct *)NULL)->elem))
610
 
611
 
612
#define CHAN_OFFSET(port,elem) (port->ch_base + OFFSETOF (_SXCHANNEL, elem))
613
#define MODU_OFFSET(board,addr,elem)    (addr + OFFSETOF (_SXMODULE, elem))
614
#define  BRD_OFFSET(board,elem)                (OFFSETOF (_SXCARD, elem))
615
 
616
 
617
#define sx_write_channel_byte(port, elem, val) \
618
   write_sx_byte (port->board, CHAN_OFFSET (port, elem), val)
619
 
620
#define sx_read_channel_byte(port, elem) \
621
   read_sx_byte (port->board, CHAN_OFFSET (port, elem))
622
 
623
#define sx_write_channel_word(port, elem, val) \
624
   write_sx_word (port->board, CHAN_OFFSET (port, elem), val)
625
 
626
#define sx_read_channel_word(port, elem) \
627
   read_sx_word (port->board, CHAN_OFFSET (port, elem))
628
 
629
 
630
#define sx_write_module_byte(board, addr, elem, val) \
631
   write_sx_byte (board, MODU_OFFSET (board, addr, elem), val)
632
 
633
#define sx_read_module_byte(board, addr, elem) \
634
   read_sx_byte (board, MODU_OFFSET (board, addr, elem))
635
 
636
#define sx_write_module_word(board, addr, elem, val) \
637
   write_sx_word (board, MODU_OFFSET (board, addr, elem), val)
638
 
639
#define sx_read_module_word(board, addr, elem) \
640
   read_sx_word (board, MODU_OFFSET (board, addr, elem))
641
 
642
 
643
#define sx_write_board_byte(board, elem, val) \
644
   write_sx_byte (board, BRD_OFFSET (board, elem), val)
645
 
646
#define sx_read_board_byte(board, elem) \
647
   read_sx_byte (board, BRD_OFFSET (board, elem))
648
 
649
#define sx_write_board_word(board, elem, val) \
650
   write_sx_word (board, BRD_OFFSET (board, elem), val)
651
 
652
#define sx_read_board_word(board, elem) \
653
   read_sx_word (board, BRD_OFFSET (board, elem))
654
 
655
 
656
static int sx_start_board (struct sx_board *board)
657
{
658
        if (IS_SX_BOARD (board)) {
659
                write_sx_byte (board, SX_CONFIG, SX_CONF_BUSEN);
660
        } else if (IS_EISA_BOARD(board)) {
661
                write_sx_byte(board, SI2_EISA_OFF, SI2_EISA_VAL);
662
                outb((board->irq<<4)|4, board->eisa_base+0xc02);
663
        } else if (IS_SI1_BOARD(board)) {
664
                write_sx_byte (board, SI1_ISA_RESET_CLEAR, 0);
665
                write_sx_byte (board, SI1_ISA_INTCL, 0);
666
        } else {
667
                /* Don't bug me about the clear_set.
668
                   I haven't the foggiest idea what it's about -- REW */
669
                write_sx_byte (board, SI2_ISA_RESET,    SI2_ISA_RESET_CLEAR);
670
                write_sx_byte (board, SI2_ISA_INTCLEAR, SI2_ISA_INTCLEAR_SET);
671
        }
672
        return 1;
673
}
674
 
675
#define SX_IRQ_REG_VAL(board) \
676
        ((board->flags & SX_ISA_BOARD)?(board->irq << 4):0)
677
 
678
/* Note. The SX register is write-only. Therefore, we have to enable the
679
   bus too. This is a no-op, if you don't mess with this driver... */
680
static int sx_start_interrupts (struct sx_board *board)
681
{
682
 
683
        /* Don't call this with board->irq == 0 */
684
 
685
        if (IS_SX_BOARD(board)) {
686
                write_sx_byte (board, SX_CONFIG, SX_IRQ_REG_VAL (board) |
687
                                                 SX_CONF_BUSEN |
688
                                                 SX_CONF_HOSTIRQ);
689
        } else if (IS_EISA_BOARD(board)) {
690
                inb(board->eisa_base+0xc03);
691
        } else if (IS_SI1_BOARD(board)) {
692
               write_sx_byte (board, SI1_ISA_INTCL,0);
693
               write_sx_byte (board, SI1_ISA_INTCL_CLEAR,0);
694
        } else {
695
                switch (board->irq) {
696
                case 11:write_sx_byte (board, SI2_ISA_IRQ11, SI2_ISA_IRQ11_SET);break;
697
                case 12:write_sx_byte (board, SI2_ISA_IRQ12, SI2_ISA_IRQ12_SET);break;
698
                case 15:write_sx_byte (board, SI2_ISA_IRQ15, SI2_ISA_IRQ15_SET);break;
699
                default:printk (KERN_INFO "sx: SI/XIO card doesn't support interrupt %d.\n",
700
                                board->irq);
701
                return 0;
702
                }
703
                write_sx_byte (board, SI2_ISA_INTCLEAR, SI2_ISA_INTCLEAR_SET);
704
        }
705
 
706
        return 1;
707
}
708
 
709
 
710
static int sx_send_command (struct sx_port *port,
711
                            int command, int mask, int newstat)
712
{
713
        func_enter2 ();
714
        write_sx_byte (port->board, CHAN_OFFSET (port, hi_hstat), command);
715
        func_exit ();
716
        return sx_busy_wait_eq (port->board, CHAN_OFFSET (port, hi_hstat), mask, newstat);
717
}
718
 
719
 
720
static char *mod_type_s (int module_type)
721
{
722
        switch (module_type) {
723
        case TA4:       return "TA4";
724
        case TA8:       return "TA8";
725
        case TA4_ASIC:  return "TA4_ASIC";
726
        case TA8_ASIC:  return "TA8_ASIC";
727
        case MTA_CD1400:return "MTA_CD1400";
728
        case SXDC:      return "SXDC";
729
        default:return "Unknown/invalid";
730
        }
731
}
732
 
733
 
734
static char *pan_type_s (int pan_type)
735
{
736
        switch (pan_type) {
737
        case MOD_RS232DB25:     return "MOD_RS232DB25";
738
        case MOD_RS232RJ45:     return "MOD_RS232RJ45";
739
        case MOD_RS422DB25:     return "MOD_RS422DB25";
740
        case MOD_PARALLEL:      return "MOD_PARALLEL";
741
        case MOD_2_RS232DB25:   return "MOD_2_RS232DB25";
742
        case MOD_2_RS232RJ45:   return "MOD_2_RS232RJ45";
743
        case MOD_2_RS422DB25:   return "MOD_2_RS422DB25";
744
        case MOD_RS232DB25MALE: return "MOD_RS232DB25MALE";
745
        case MOD_2_PARALLEL:    return "MOD_2_PARALLEL";
746
        case MOD_BLANK:         return "empty";
747
        default:return "invalid";
748
        }
749
}
750
 
751
 
752
static int mod_compat_type (int module_type)
753
{
754
        return module_type >> 4;
755
}
756
 
757
static void sx_reconfigure_port(struct sx_port *port)
758
{
759
        if (sx_read_channel_byte (port, hi_hstat) == HS_IDLE_OPEN) {
760
                if (sx_send_command (port, HS_CONFIG, -1, HS_IDLE_OPEN) != 1) {
761
                        printk (KERN_WARNING "sx: Sent reconfigure command, but card didn't react.\n");
762
                }
763
        } else {
764
                sx_dprintk (SX_DEBUG_TERMIOS,
765
                            "sx: Not sending reconfigure: port isn't open (%02x).\n",
766
                            sx_read_channel_byte (port, hi_hstat));
767
        }
768
}
769
 
770
static void sx_setsignals (struct sx_port *port, int dtr, int rts)
771
{
772
        int t;
773
        func_enter2 ();
774
 
775
        t = sx_read_channel_byte (port, hi_op);
776
        if (dtr >= 0) t = dtr? (t | OP_DTR): (t & ~OP_DTR);
777
        if (rts >= 0) t = rts? (t | OP_RTS): (t & ~OP_RTS);
778
        sx_write_channel_byte (port, hi_op, t);
779
        sx_dprintk (SX_DEBUG_MODEMSIGNALS, "setsignals: %d/%d\n", dtr, rts);
780
 
781
        func_exit ();
782
}
783
 
784
 
785
 
786
static int sx_getsignals (struct sx_port *port)
787
{
788
        int i_stat,o_stat;
789
 
790
        o_stat = sx_read_channel_byte (port, hi_op);
791
        i_stat = sx_read_channel_byte (port, hi_ip);
792
 
793
        sx_dprintk (SX_DEBUG_MODEMSIGNALS, "getsignals: %d/%d  (%d/%d) %02x/%02x\n",
794
                    (o_stat & OP_DTR) != 0, (o_stat & OP_RTS) != 0,
795
                    port->c_dcd, sx_get_CD (port),
796
                    sx_read_channel_byte (port, hi_ip),
797
                    sx_read_channel_byte (port, hi_state));
798
 
799
        return (((o_stat & OP_DTR)?TIOCM_DTR:0) |
800
                ((o_stat & OP_RTS)?TIOCM_RTS:0) |
801
                ((i_stat & IP_CTS)?TIOCM_CTS:0) |
802
                ((i_stat & IP_DCD)?TIOCM_CAR:0) |
803
                ((i_stat & IP_DSR)?TIOCM_DSR:0) |
804
                ((i_stat & IP_RI)?TIOCM_RNG:0)
805
                );
806
}
807
 
808
 
809
static void sx_set_baud (struct sx_port *port)
810
{
811
        int t;
812
 
813
        if (port->board->ta_type == MOD_SXDC) {
814
                switch (port->gs.baud) {
815
                  /* Save some typing work... */
816
#define e(x) case x:t= BAUD_ ## x ; break
817
                        e(50);e(75);e(110);e(150);e(200);e(300);e(600);
818
                        e(1200);e(1800);e(2000);e(2400);e(4800);e(7200);
819
                        e(9600);e(14400);e(19200);e(28800);e(38400);
820
                        e(56000);e(57600);e(64000);e(76800);e(115200);
821
                        e(128000);e(150000);e(230400);e(256000);e(460800);
822
                        e(921600);
823
                case 134    :t = BAUD_134_5;   break;
824
                case 0      :t = -1;
825
                                                                 break;
826
                default:
827
                        /* Can I return "invalid"? */
828
                        t = BAUD_9600;
829
                        printk (KERN_INFO "sx: unsupported baud rate: %d.\n", port->gs.baud);
830
                        break;
831
                }
832
#undef e
833
                if (t > 0) {
834
                        /* The baud rate is not set to 0, so we're enabeling DTR... -- REW */
835
                        sx_setsignals (port, 1, -1);
836
                        /* XXX This is not TA & MTA compatible */
837
                        sx_write_channel_byte (port, hi_csr, 0xff);
838
 
839
                        sx_write_channel_byte (port, hi_txbaud, t);
840
                        sx_write_channel_byte (port, hi_rxbaud, t);
841
                } else {
842
                        sx_setsignals (port, 0, -1);
843
                }
844
        } else {
845
                switch (port->gs.baud) {
846
#define e(x) case x:t= CSR_ ## x ; break
847
                        e(75);e(150);e(300);e(600);e(1200);e(2400);e(4800);
848
                        e(1800);e(9600);
849
                        e(19200);e(57600);e(38400);
850
                        /* TA supports 110, but not 115200, MTA supports 115200, but not 110 */
851
                case 110:
852
                        if (port->board->ta_type == MOD_TA) {
853
                                t = CSR_110;
854
                                break;
855
                        } else {
856
                                t = CSR_9600;
857
                                printk (KERN_INFO "sx: Unsupported baud rate: %d.\n", port->gs.baud);
858
                                break;
859
                        }
860
                case 115200:
861
                        if (port->board->ta_type == MOD_TA) {
862
                                t = CSR_9600;
863
                                printk (KERN_INFO "sx: Unsupported baud rate: %d.\n", port->gs.baud);
864
                                break;
865
                        } else {
866
                                t = CSR_110;
867
                                break;
868
                        }
869
                case 0      :t = -1;
870
                                                                 break;
871
                default:
872
                        t = CSR_9600;
873
                        printk (KERN_INFO "sx: Unsupported baud rate: %d.\n", port->gs.baud);
874
                        break;
875
                }
876
#undef e
877
                if (t >= 0) {
878
                        sx_setsignals (port, 1, -1);
879
                        sx_write_channel_byte (port, hi_csr, t * 0x11);
880
                } else {
881
                        sx_setsignals (port, 0, -1);
882
                }
883
        }
884
}
885
 
886
 
887
/* Simon Allen's version of this routine was 225 lines long. 85 is a lot
888
   better. -- REW */
889
 
890
static int sx_set_real_termios (void *ptr)
891
{
892
        struct sx_port *port = ptr;
893
 
894
        func_enter2();
895
 
896
        if (!port->gs.tty)
897
                return 0;
898
 
899
        /* What is this doing here? -- REW
900
           Ha! figured it out. It is to allow you to get DTR active again
901
           if you've dropped it with stty 0. Moved to set_baud, where it
902
           belongs (next to the drop dtr if baud == 0) -- REW */
903
        /* sx_setsignals (port, 1, -1); */
904
 
905
        sx_set_baud (port);
906
 
907
#define CFLAG port->gs.tty->termios->c_cflag
908
        sx_write_channel_byte (port, hi_mr1,
909
                               (C_PARENB (port->gs.tty)? MR1_WITH:MR1_NONE) |
910
                               (C_PARODD (port->gs.tty)? MR1_ODD:MR1_EVEN) |
911
                               (C_CRTSCTS(port->gs.tty)? MR1_RTS_RXFLOW:0) |
912
                               (((CFLAG & CSIZE)==CS8) ? MR1_8_BITS:0) |
913
                               (((CFLAG & CSIZE)==CS7) ? MR1_7_BITS:0) |
914
                               (((CFLAG & CSIZE)==CS6) ? MR1_6_BITS:0) |
915
                               (((CFLAG & CSIZE)==CS5) ? MR1_5_BITS:0) );
916
 
917
        sx_write_channel_byte (port, hi_mr2,
918
                               (C_CRTSCTS(port->gs.tty)?MR2_CTS_TXFLOW:0) |
919
                               (C_CSTOPB (port->gs.tty)?MR2_2_STOP:MR2_1_STOP));
920
 
921
        switch (CFLAG & CSIZE) {
922
        case CS8:sx_write_channel_byte (port, hi_mask, 0xff);break;
923
        case CS7:sx_write_channel_byte (port, hi_mask, 0x7f);break;
924
        case CS6:sx_write_channel_byte (port, hi_mask, 0x3f);break;
925
        case CS5:sx_write_channel_byte (port, hi_mask, 0x1f);break;
926
        default:
927
                printk (KERN_INFO "sx: Invalid wordsize: %d\n", CFLAG & CSIZE);
928
                break;
929
        }
930
 
931
        sx_write_channel_byte (port, hi_prtcl,
932
                               (I_IXON   (port->gs.tty)?SP_TXEN:0) |
933
                               (I_IXOFF  (port->gs.tty)?SP_RXEN:0) |
934
                               (I_IXANY  (port->gs.tty)?SP_TANY:0) |
935
                               SP_DCEN);
936
 
937
        sx_write_channel_byte (port, hi_break,
938
                               (I_IGNBRK(port->gs.tty)?BR_IGN:0 |
939
                                I_BRKINT(port->gs.tty)?BR_INT:0));
940
 
941
        sx_write_channel_byte (port, hi_txon,  START_CHAR (port->gs.tty));
942
        sx_write_channel_byte (port, hi_rxon,  START_CHAR (port->gs.tty));
943
        sx_write_channel_byte (port, hi_txoff, STOP_CHAR  (port->gs.tty));
944
        sx_write_channel_byte (port, hi_rxoff, STOP_CHAR  (port->gs.tty));
945
 
946
        sx_reconfigure_port(port);
947
 
948
        /* Tell line discipline whether we will do input cooking */
949
        if(I_OTHER(port->gs.tty)) {
950
                clear_bit(TTY_HW_COOK_IN, &port->gs.tty->flags);
951
        } else {
952
                set_bit(TTY_HW_COOK_IN, &port->gs.tty->flags);
953
        }
954
        sx_dprintk (SX_DEBUG_TERMIOS, "iflags: %x(%d) ",
955
                    port->gs.tty->termios->c_iflag,
956
                    I_OTHER(port->gs.tty));
957
 
958
 
959
/* Tell line discipline whether we will do output cooking.
960
 * If OPOST is set and no other output flags are set then we can do output
961
 * processing.  Even if only *one* other flag in the O_OTHER group is set
962
 * we do cooking in software.
963
 */
964
        if(O_OPOST(port->gs.tty) && !O_OTHER(port->gs.tty)) {
965
                set_bit(TTY_HW_COOK_OUT, &port->gs.tty->flags);
966
        } else {
967
                clear_bit(TTY_HW_COOK_OUT, &port->gs.tty->flags);
968
        }
969
        sx_dprintk (SX_DEBUG_TERMIOS, "oflags: %x(%d)\n",
970
                    port->gs.tty->termios->c_oflag,
971
                    O_OTHER(port->gs.tty));
972
        /* port->c_dcd = sx_get_CD (port); */
973
        func_exit ();
974
        return 0;
975
}
976
 
977
 
978
 
979
/* ********************************************************************** *
980
 *                   the interrupt related routines                       *
981
 * ********************************************************************** */
982
 
983
/* Note:
984
   Other drivers use the macro "MIN" to calculate how much to copy.
985
   This has the disadvantage that it will evaluate parts twice. That's
986
   expensive when it's IO (and the compiler cannot optimize those away!).
987
   Moreover, I'm not sure that you're race-free.
988
 
989
   I assign a value, and then only allow the value to decrease. This
990
   is always safe. This makes the code a few lines longer, and you
991
   know I'm dead against that, but I think it is required in this
992
   case.  */
993
 
994
 
995
static void sx_transmit_chars (struct sx_port *port)
996
{
997
        int c;
998
        int tx_ip;
999
        int txroom;
1000
 
1001
        func_enter2 ();
1002
        sx_dprintk (SX_DEBUG_TRANSMIT, "Port %p: transmit %d chars\n",
1003
                    port, port->gs.xmit_cnt);
1004
 
1005
        if (test_and_set_bit (SX_PORT_TRANSMIT_LOCK, &port->locks)) {
1006
                return;
1007
        }
1008
 
1009
        while (1) {
1010
                c = port->gs.xmit_cnt;
1011
 
1012
                sx_dprintk (SX_DEBUG_TRANSMIT, "Copying %d ", c);
1013
                tx_ip  = sx_read_channel_byte (port, hi_txipos);
1014
 
1015
                /* Took me 5 minutes to deduce this formula.
1016
                   Luckily it is literally in the manual in section 6.5.4.3.5 */
1017
                txroom = (sx_read_channel_byte (port, hi_txopos) - tx_ip - 1) & 0xff;
1018
 
1019
                /* Don't copy more bytes than there is room for in the buffer */
1020
                if (c > txroom)
1021
                        c = txroom;
1022
                sx_dprintk (SX_DEBUG_TRANSMIT, " %d(%d) ", c, txroom );
1023
 
1024
                /* Don't copy past the end of the hardware transmit buffer */
1025
                if (c > 0x100 - tx_ip)
1026
                        c = 0x100 - tx_ip;
1027
 
1028
                sx_dprintk (SX_DEBUG_TRANSMIT, " %d(%d) ", c, 0x100-tx_ip );
1029
 
1030
                /* Don't copy pas the end of the source buffer */
1031
                if (c > SERIAL_XMIT_SIZE - port->gs.xmit_tail)
1032
                        c = SERIAL_XMIT_SIZE - port->gs.xmit_tail;
1033
 
1034
                sx_dprintk (SX_DEBUG_TRANSMIT, " %d(%ld) \n",
1035
                            c, SERIAL_XMIT_SIZE- port->gs.xmit_tail);
1036
 
1037
                /* If for one reason or another, we can't copy more data, we're done! */
1038
                if (c == 0) break;
1039
 
1040
 
1041
                memcpy_toio (port->board->base + CHAN_OFFSET(port,hi_txbuf) + tx_ip,
1042
                             port->gs.xmit_buf + port->gs.xmit_tail, c);
1043
 
1044
                /* Update the pointer in the card */
1045
                sx_write_channel_byte (port, hi_txipos, (tx_ip+c) & 0xff);
1046
 
1047
                /* Update the kernel buffer end */
1048
                port->gs.xmit_tail = (port->gs.xmit_tail + c) & (SERIAL_XMIT_SIZE-1);
1049
 
1050
                /* This one last. (this is essential)
1051
                   It would allow others to start putting more data into the buffer! */
1052
                port->gs.xmit_cnt -= c;
1053
        }
1054
 
1055
        if (port->gs.xmit_cnt == 0) {
1056
                sx_disable_tx_interrupts (port);
1057
        }
1058
 
1059
        if ((port->gs.xmit_cnt <= port->gs.wakeup_chars) && port->gs.tty) {
1060
                if ((port->gs.tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
1061
                    port->gs.tty->ldisc.write_wakeup)
1062
                        (port->gs.tty->ldisc.write_wakeup)(port->gs.tty);
1063
                sx_dprintk (SX_DEBUG_TRANSMIT, "Waking up.... ldisc (%d)....\n",
1064
                            port->gs.wakeup_chars);
1065
                wake_up_interruptible(&port->gs.tty->write_wait);
1066
        }
1067
 
1068
        clear_bit (SX_PORT_TRANSMIT_LOCK, &port->locks);
1069
        func_exit ();
1070
}
1071
 
1072
 
1073
/* Note the symmetry between receiving chars and transmitting them!
1074
   Note: The kernel should have implemented both a receive buffer and
1075
   a transmit buffer. */
1076
 
1077
/* Inlined: Called only once. Remove the inline when you add another call */
1078
static inline void sx_receive_chars (struct sx_port *port)
1079
{
1080
        int c;
1081
        int rx_op;
1082
        struct tty_struct *tty;
1083
        int copied=0;
1084
 
1085
        func_enter2 ();
1086
        tty = port->gs.tty;
1087
        while (1) {
1088
                rx_op = sx_read_channel_byte (port, hi_rxopos);
1089
                c = (sx_read_channel_byte (port, hi_rxipos) - rx_op) & 0xff;
1090
 
1091
                sx_dprintk (SX_DEBUG_RECEIVE, "rxop=%d, c = %d.\n", rx_op, c);
1092
 
1093
                /* Don't copy more bytes than there is room for in the buffer */
1094
                if (tty->flip.count + c > TTY_FLIPBUF_SIZE)
1095
                        c = TTY_FLIPBUF_SIZE - tty->flip.count;
1096
 
1097
                sx_dprintk (SX_DEBUG_RECEIVE, "c = %d.\n", c);
1098
 
1099
                /* Don't copy past the end of the hardware receive buffer */
1100
                if (rx_op + c > 0x100) c = 0x100 - rx_op;
1101
 
1102
                sx_dprintk (SX_DEBUG_RECEIVE, "c = %d.\n", c);
1103
 
1104
                /* If for one reason or another, we can't copy more data, we're done! */
1105
                if (c == 0) break;
1106
 
1107
                sx_dprintk (SX_DEBUG_RECEIVE , "Copying over %d chars. First is %d at %lx\n", c,
1108
                            read_sx_byte (port->board, CHAN_OFFSET(port,hi_rxbuf) + rx_op),
1109
                            CHAN_OFFSET(port, hi_rxbuf));
1110
                memcpy_fromio (tty->flip.char_buf_ptr,
1111
                               port->board->base + CHAN_OFFSET(port,hi_rxbuf) + rx_op, c);
1112
                memset(tty->flip.flag_buf_ptr, TTY_NORMAL, c);
1113
 
1114
                /* Update the kernel buffer end */
1115
                tty->flip.count += c;
1116
                tty->flip.char_buf_ptr += c;
1117
                tty->flip.flag_buf_ptr += c;
1118
 
1119
                /* This one last. ( Not essential.)
1120
                   It allows the card to start putting more data into the buffer!
1121
                   Update the pointer in the card */
1122
                sx_write_channel_byte (port, hi_rxopos, (rx_op + c) & 0xff);
1123
 
1124
                copied += c;
1125
        }
1126
        if (copied) {
1127
                struct timeval tv;
1128
 
1129
                do_gettimeofday (&tv);
1130
                sx_dprintk (SX_DEBUG_RECEIVE,
1131
                            "pushing flipq port %d (%3d chars): %d.%06d  (%d/%d)\n",
1132
                            port->line, copied,
1133
                            (int) (tv.tv_sec % 60), (int)tv.tv_usec, tty->raw, tty->real_raw);
1134
 
1135
                /* Tell the rest of the system the news. Great news. New characters! */
1136
                tty_flip_buffer_push (tty);
1137
                /*    tty_schedule_flip (tty); */
1138
        }
1139
 
1140
        func_exit ();
1141
}
1142
 
1143
/* Inlined: it is called only once. Remove the inline if you add another
1144
   call */
1145
static inline void sx_check_modem_signals (struct sx_port *port)
1146
{
1147
        int hi_state;
1148
        int c_dcd;
1149
 
1150
        hi_state = sx_read_channel_byte (port, hi_state);
1151
        sx_dprintk (SX_DEBUG_MODEMSIGNALS, "Checking modem signals (%d/%d)\n",
1152
                    port->c_dcd, sx_get_CD (port));
1153
 
1154
        if (hi_state & ST_BREAK) {
1155
                hi_state &= ~ST_BREAK;
1156
                sx_dprintk (SX_DEBUG_MODEMSIGNALS, "got a break.\n");
1157
 
1158
                sx_write_channel_byte (port, hi_state, hi_state);
1159
                gs_got_break (&port->gs);
1160
        }
1161
        if (hi_state & ST_DCD) {
1162
                hi_state &= ~ST_DCD;
1163
                sx_dprintk (SX_DEBUG_MODEMSIGNALS, "got a DCD change.\n");
1164
                sx_write_channel_byte (port, hi_state, hi_state);
1165
                c_dcd = sx_get_CD (port);
1166
                sx_dprintk (SX_DEBUG_MODEMSIGNALS, "DCD is now %d\n", c_dcd);
1167
                if (c_dcd != port->c_dcd) {
1168
                        port->c_dcd = c_dcd;
1169
                        if (sx_get_CD (port)) {
1170
                                /* DCD went UP */
1171
                                if( (~(port->gs.flags & ASYNC_NORMAL_ACTIVE) ||
1172
                                                 ~(port->gs.flags & ASYNC_CALLOUT_ACTIVE)) &&
1173
                                                (sx_read_channel_byte(port, hi_hstat) != HS_IDLE_CLOSED) &&
1174
                                                !(port->gs.tty->termios->c_cflag & CLOCAL) ) {
1175
                                        /* Are we blocking in open?*/
1176
                                        sx_dprintk (SX_DEBUG_MODEMSIGNALS, "DCD active, unblocking open\n");
1177
                                        wake_up_interruptible(&port->gs.open_wait);
1178
                                } else {
1179
                                        sx_dprintk (SX_DEBUG_MODEMSIGNALS, "DCD raised. Ignoring.\n");
1180
                                }
1181
                        } else {
1182
                                /* DCD went down! */
1183
                                if (!((port->gs.flags & ASYNC_CALLOUT_ACTIVE) &&
1184
                                      (port->gs.flags & ASYNC_CALLOUT_NOHUP)) &&
1185
                                    !(port->gs.tty->termios->c_cflag & CLOCAL) ) {
1186
                                        sx_dprintk (SX_DEBUG_MODEMSIGNALS, "DCD dropped. hanging up....\n");
1187
                                        tty_hangup (port->gs.tty);
1188
                                } else {
1189
                                        sx_dprintk (SX_DEBUG_MODEMSIGNALS, "DCD dropped. ignoring.\n");
1190
                                }
1191
                        }
1192
                } else {
1193
                        sx_dprintk (SX_DEBUG_MODEMSIGNALS, "Hmmm. card told us DCD changed, but it didn't.\n");
1194
                }
1195
        }
1196
}
1197
 
1198
 
1199
/* This is what an interrupt routine should look like.
1200
 * Small, elegant, clear.
1201
 */
1202
 
1203
static void sx_interrupt (int irq, void *ptr, struct pt_regs *regs)
1204
{
1205
        struct sx_board *board = ptr;
1206
        struct sx_port *port;
1207
        int i;
1208
 
1209
        /*   func_enter ();  */
1210
        sx_dprintk (SX_DEBUG_FLOW, "sx: enter sx_interrupt (%d/%d)\n", irq, board->irq);
1211
 
1212
        /* AAargh! The order in which to do these things is essential and
1213
           not trivial.
1214
 
1215
           - Rate limit goes before "recursive". Otherwise a series of
1216
             recursive calls will hang the machine in the interrupt routine.
1217
 
1218
           - hardware twiddling goes before "recursive". Otherwise when we
1219
             poll the card, and a recursive interrupt happens, we wont
1220
             ack the card, so it might keep on interrupting us. (especially
1221
             level sensitive interrupt systems like PCI).
1222
 
1223
           - Rate limit goes before hardware twiddling. Otherwise we won't
1224
             catch a card that has gone bonkers.
1225
 
1226
           - The "initialized" test goes after the hardware twiddling. Otherwise
1227
             the card will stick us in the interrupt routine again.
1228
 
1229
           - The initialized test goes before recursive.
1230
        */
1231
 
1232
 
1233
 
1234
#ifdef IRQ_RATE_LIMIT
1235
        /* Aaargh! I'm ashamed. This costs more lines-of-code than the
1236
           actual interrupt routine!. (Well, used to when I wrote that comment) */
1237
        {
1238
                static int lastjif;
1239
                static int nintr=0;
1240
 
1241
                if (lastjif == jiffies) {
1242
                        if (++nintr > IRQ_RATE_LIMIT) {
1243
                                free_irq (board->irq, board);
1244
                                printk (KERN_ERR "sx: Too many interrupts. Turning off interrupt %d.\n",
1245
                                              board->irq);
1246
                        }
1247
                } else {
1248
                        lastjif = jiffies;
1249
                        nintr = 0;
1250
                }
1251
        }
1252
#endif
1253
 
1254
 
1255
        if (board->irq == irq) {
1256
                /* Tell the card we've noticed the interrupt. */
1257
 
1258
                sx_write_board_word (board, cc_int_pending, 0);
1259
                if (IS_SX_BOARD (board)) {
1260
                        write_sx_byte (board, SX_RESET_IRQ, 1);
1261
                } else if (IS_EISA_BOARD(board)) {
1262
                        inb(board->eisa_base+0xc03);
1263
                        write_sx_word(board, 8, 0);
1264
                } else {
1265
                        write_sx_byte (board, SI2_ISA_INTCLEAR, SI2_ISA_INTCLEAR_CLEAR);
1266
                        write_sx_byte (board, SI2_ISA_INTCLEAR, SI2_ISA_INTCLEAR_SET);
1267
                }
1268
        }
1269
 
1270
        if (!sx_initialized) return;
1271
        if (!(board->flags & SX_BOARD_INITIALIZED)) return;
1272
 
1273
        if (test_and_set_bit (SX_BOARD_INTR_LOCK, &board->locks)) {
1274
                printk (KERN_ERR "Recursive interrupt! (%d)\n", board->irq);
1275
                return;
1276
        }
1277
 
1278
         for (i=0;i<board->nports;i++) {
1279
                port = &board->ports[i];
1280
                if (port->gs.flags & GS_ACTIVE) {
1281
                        if (sx_read_channel_byte (port, hi_state)) {
1282
                                sx_dprintk (SX_DEBUG_INTERRUPTS,
1283
                                            "Port %d: modem signal change?... \n", i);
1284
                                sx_check_modem_signals (port);
1285
                        }
1286
                        if (port->gs.xmit_cnt) {
1287
                                sx_transmit_chars (port);
1288
                        }
1289
                        if (!(port->gs.flags & SX_RX_THROTTLE)) {
1290
                                sx_receive_chars (port);
1291
                        }
1292
                }
1293
        }
1294
 
1295
        clear_bit (SX_BOARD_INTR_LOCK, &board->locks);
1296
 
1297
        sx_dprintk (SX_DEBUG_FLOW, "sx: exit sx_interrupt (%d/%d)\n", irq, board->irq);
1298
        /*  func_exit ();  */
1299
}
1300
 
1301
 
1302
static void sx_pollfunc (unsigned long data)
1303
{
1304
        struct sx_board *board = (struct sx_board *) data;
1305
 
1306
        func_enter ();
1307
 
1308
        sx_interrupt (0, board, NULL);
1309
 
1310
        init_timer(&board->timer);
1311
 
1312
        board->timer.expires = jiffies + sx_poll;
1313
        add_timer (&board->timer);
1314
        func_exit ();
1315
}
1316
 
1317
 
1318
 
1319
/* ********************************************************************** *
1320
 *                Here are the routines that actually                     *
1321
 *              interface with the generic_serial driver                  *
1322
 * ********************************************************************** */
1323
 
1324
/* Ehhm. I don't know how to fiddle with interrupts on the SX card. --REW */
1325
/* Hmm. Ok I figured it out. You don't.  */
1326
 
1327
static void sx_disable_tx_interrupts (void * ptr)
1328
{
1329
        struct sx_port *port = ptr;
1330
        func_enter2();
1331
 
1332
        port->gs.flags &= ~GS_TX_INTEN;
1333
 
1334
        func_exit();
1335
}
1336
 
1337
 
1338
static void sx_enable_tx_interrupts (void * ptr)
1339
{
1340
        struct sx_port *port = ptr;
1341
        int data_in_buffer;
1342
        func_enter2();
1343
 
1344
        /* First transmit the characters that we're supposed to */
1345
        sx_transmit_chars (port);
1346
 
1347
        /* The sx card will never interrupt us if we don't fill the buffer
1348
           past 25%. So we keep considering interrupts off if that's the case. */
1349
        data_in_buffer = (sx_read_channel_byte (port, hi_txipos) -
1350
                          sx_read_channel_byte (port, hi_txopos)) & 0xff;
1351
 
1352
        /* XXX Must be "HIGH_WATER" for SI card according to doc. */
1353
        if (data_in_buffer < LOW_WATER)
1354
                port->gs.flags &= ~GS_TX_INTEN;
1355
 
1356
        func_exit();
1357
}
1358
 
1359
 
1360
static void sx_disable_rx_interrupts (void * ptr)
1361
{
1362
        /*  struct sx_port *port = ptr; */
1363
        func_enter();
1364
 
1365
        func_exit();
1366
}
1367
 
1368
static void sx_enable_rx_interrupts (void * ptr)
1369
{
1370
        /*  struct sx_port *port = ptr; */
1371
        func_enter();
1372
 
1373
        func_exit();
1374
}
1375
 
1376
 
1377
/* Jeez. Isn't this simple? */
1378
static int sx_get_CD (void * ptr)
1379
{
1380
        struct sx_port *port = ptr;
1381
        func_enter2();
1382
 
1383
        func_exit();
1384
        return ((sx_read_channel_byte (port, hi_ip) & IP_DCD) != 0);
1385
}
1386
 
1387
 
1388
/* Jeez. Isn't this simple? */
1389
static int sx_chars_in_buffer (void * ptr)
1390
{
1391
        struct sx_port *port = ptr;
1392
        func_enter2();
1393
 
1394
        func_exit();
1395
        return ((sx_read_channel_byte (port, hi_txipos) -
1396
                 sx_read_channel_byte (port, hi_txopos)) & 0xff);
1397
}
1398
 
1399
 
1400
static void sx_shutdown_port (void * ptr)
1401
{
1402
        struct sx_port *port = ptr;
1403
 
1404
        func_enter();
1405
 
1406
        port->gs.flags &= ~ GS_ACTIVE;
1407
        if (port->gs.tty && (port->gs.tty->termios->c_cflag & HUPCL)) {
1408
                sx_setsignals (port, 0, 0);
1409
                sx_reconfigure_port(port);
1410
        }
1411
 
1412
        func_exit();
1413
}
1414
 
1415
 
1416
 
1417
 
1418
 
1419
/* ********************************************************************** *
1420
 *                Here are the routines that actually                     *
1421
 *               interface with the rest of the system                    *
1422
 * ********************************************************************** */
1423
 
1424
static int sx_open  (struct tty_struct * tty, struct file * filp)
1425
{
1426
        struct sx_port *port;
1427
        int retval, line;
1428
 
1429
        func_enter();
1430
 
1431
        if (!sx_initialized) {
1432
                return -EIO;
1433
        }
1434
 
1435
        line = MINOR(tty->device);
1436
        sx_dprintk (SX_DEBUG_OPEN, "%d: opening line %d. tty=%p ctty=%p, np=%d)\n",
1437
                    current->pid, line, tty, current->tty, sx_nports);
1438
 
1439
        if ((line < 0) || (line >= SX_NPORTS) || (line >= sx_nports))
1440
                return -ENODEV;
1441
 
1442
        port = & sx_ports[line];
1443
        port->c_dcd = 0; /* Make sure that the first interrupt doesn't detect a
1444
                            1 -> 0 transition. */
1445
 
1446
 
1447
        sx_dprintk (SX_DEBUG_OPEN, "port = %p c_dcd = %d\n", port, port->c_dcd);
1448
 
1449
        tty->driver_data = port;
1450
        port->gs.tty = tty;
1451
        if (!port->gs.count)
1452
                MOD_INC_USE_COUNT;
1453
        port->gs.count++;
1454
 
1455
        sx_dprintk (SX_DEBUG_OPEN, "starting port\n");
1456
 
1457
        /*
1458
         * Start up serial port
1459
         */
1460
        retval = gs_init_port(&port->gs);
1461
        sx_dprintk (SX_DEBUG_OPEN, "done gs_init\n");
1462
        if (retval) {
1463
                port->gs.count--;
1464
                if (port->gs.count) MOD_DEC_USE_COUNT;
1465
                return retval;
1466
        }
1467
 
1468
        port->gs.flags |= GS_ACTIVE;
1469
        sx_setsignals (port, 1,1);
1470
 
1471
#if 0
1472
        if (sx_debug & SX_DEBUG_OPEN)
1473
                my_hd ((unsigned char *)port, sizeof (*port));
1474
#else
1475
        if (sx_debug & SX_DEBUG_OPEN)
1476
                my_hd ((unsigned char *)port->board->base + port->ch_base,
1477
                       sizeof (*port));
1478
#endif
1479
 
1480
        if (sx_send_command (port, HS_LOPEN, -1, HS_IDLE_OPEN) != 1) {
1481
                printk (KERN_ERR "sx: Card didn't respond to LOPEN command.\n");
1482
                port->gs.count--;
1483
                if (!port->gs.count) MOD_DEC_USE_COUNT;
1484
                return -EIO;
1485
        }
1486
 
1487
        retval = gs_block_til_ready(port, filp);
1488
        sx_dprintk (SX_DEBUG_OPEN, "Block til ready returned %d. Count=%d\n",
1489
                    retval, port->gs.count);
1490
 
1491
        if (retval) {
1492
                /*
1493
                 * Don't lower gs.count here because sx_close() will be called later
1494
                 */
1495
 
1496
                return retval;
1497
        }
1498
        /* tty->low_latency = 1; */
1499
 
1500
        if ((port->gs.count == 1) && (port->gs.flags & ASYNC_SPLIT_TERMIOS)) {
1501
                if (tty->driver.subtype == SERIAL_TYPE_NORMAL)
1502
                        *tty->termios = port->gs.normal_termios;
1503
                else
1504
                        *tty->termios = port->gs.callout_termios;
1505
                sx_set_real_termios (port);
1506
        }
1507
 
1508
        port->gs.session = current->session;
1509
        port->gs.pgrp = current->pgrp;
1510
        port->c_dcd = sx_get_CD (port);
1511
        sx_dprintk (SX_DEBUG_OPEN, "at open: cd=%d\n", port->c_dcd);
1512
        func_exit();
1513
        return 0;
1514
 
1515
}
1516
 
1517
 
1518
/* I haven't the foggiest why the decrement use count has to happen
1519
   here. The whole linux serial drivers stuff needs to be redesigned.
1520
   My guess is that this is a hack to minimize the impact of a bug
1521
   elsewhere. Thinking about it some more. (try it sometime) Try
1522
   running minicom on a serial port that is driven by a modularized
1523
   driver. Have the modem hangup. Then remove the driver module. Then
1524
   exit minicom.  I expect an "oops".  -- REW */
1525
static void sx_hungup (void *ptr)
1526
{
1527
  /*
1528
        struct sx_port *port = ptr;
1529
  */
1530
        func_enter ();
1531
 
1532
        /* Don't force the SX card to close. mgetty doesn't like it !!!!!! -- pvdl */
1533
        /* For some reson we added this code. Don't know why anymore ;-( -- pvdl */
1534
        /*
1535
        sx_setsignals (port, 0, 0);
1536
        sx_reconfigure_port(port);
1537
        sx_send_command (port, HS_CLOSE, 0, 0);
1538
 
1539
        if (sx_read_channel_byte (port, hi_hstat) != HS_IDLE_CLOSED) {
1540
                if (sx_send_command (port, HS_FORCE_CLOSED, -1, HS_IDLE_CLOSED) != 1) {
1541
                        printk (KERN_ERR
1542
                                "sx: sent the force_close command, but card didn't react\n");
1543
                } else
1544
                        sx_dprintk (SX_DEBUG_CLOSE, "sent the force_close command.\n");
1545
        }
1546
        */
1547
        MOD_DEC_USE_COUNT;
1548
        func_exit ();
1549
}
1550
 
1551
 
1552
static void sx_close (void *ptr)
1553
{
1554
        struct sx_port *port = ptr;
1555
        /* Give the port 5 seconds to close down. */
1556
        int to = 5 * HZ;
1557
 
1558
        func_enter ();
1559
 
1560
        sx_setsignals (port, 0, 0);
1561
        sx_reconfigure_port(port);
1562
        sx_send_command (port, HS_CLOSE, 0, 0);
1563
 
1564
        while (to-- && (sx_read_channel_byte (port, hi_hstat) != HS_IDLE_CLOSED)) {
1565
                current->state = TASK_INTERRUPTIBLE;
1566
                schedule_timeout (1);
1567
                if (signal_pending (current))
1568
                                break;
1569
        }
1570
        current->state = TASK_RUNNING;
1571
        if (sx_read_channel_byte (port, hi_hstat) != HS_IDLE_CLOSED) {
1572
                if (sx_send_command (port, HS_FORCE_CLOSED, -1, HS_IDLE_CLOSED) != 1) {
1573
                        printk (KERN_ERR
1574
                                "sx: sent the force_close command, but card didn't react\n");
1575
                } else
1576
                        sx_dprintk (SX_DEBUG_CLOSE, "sent the force_close command.\n");
1577
        }
1578
 
1579
        sx_dprintk (SX_DEBUG_CLOSE, "waited %d jiffies for close. count=%d\n",
1580
                    5 * HZ - to - 1, port->gs.count);
1581
 
1582
        if(port->gs.count) {
1583
                sx_dprintk(SX_DEBUG_CLOSE, "WARNING port count:%d\n", port->gs.count);
1584
                port->gs.count = 0;
1585
        }
1586
 
1587
        MOD_DEC_USE_COUNT;
1588
        func_exit ();
1589
}
1590
 
1591
 
1592
 
1593
/* This is relatively thorough. But then again it is only 20 lines. */
1594
#define MARCHUP    for (i=min;i<max;i++) 
1595
#define MARCHDOWN  for (i=max-1;i>=min;i--)
1596
#define W0         write_sx_byte (board, i, 0x55)
1597
#define W1         write_sx_byte (board, i, 0xaa)
1598
#define R0         if (read_sx_byte (board, i) != 0x55) return 1
1599
#define R1         if (read_sx_byte (board, i) != 0xaa) return 1
1600
 
1601
/* This memtest takes a human-noticable time. You normally only do it
1602
   once a boot, so I guess that it is worth it. */
1603
static int do_memtest (struct sx_board *board, int min, int max)
1604
{
1605
        int i;
1606
 
1607
        /* This is a marchb. Theoretically, marchb catches much more than
1608
           simpler tests. In practise, the longer test just catches more
1609
           intermittent errors. -- REW
1610
           (For the theory behind memory testing see:
1611
           Testing Semiconductor Memories by A.J. van de Goor.) */
1612
        MARCHUP  {W0;}
1613
        MARCHUP   {R0;W1;R1;W0;R0;W1;}
1614
        MARCHUP   {R1;W0;W1;}
1615
        MARCHDOWN {R1;W0;W1;W0;}
1616
        MARCHDOWN {R0;W1;W0;}
1617
 
1618
        return 0;
1619
}
1620
 
1621
 
1622
#undef MARCHUP
1623
#undef MARCHDOWN
1624
#undef W0
1625
#undef W1
1626
#undef R0
1627
#undef R1
1628
 
1629
#define MARCHUP    for (i=min;i<max;i+=2) 
1630
#define MARCHDOWN  for (i=max-1;i>=min;i-=2)
1631
#define W0         write_sx_word (board, i, 0x55aa)
1632
#define W1         write_sx_word (board, i, 0xaa55)
1633
#define R0         if (read_sx_word (board, i) != 0x55aa) return 1
1634
#define R1         if (read_sx_word (board, i) != 0xaa55) return 1
1635
 
1636
#if 0
1637
/* This memtest takes a human-noticable time. You normally only do it
1638
   once a boot, so I guess that it is worth it. */
1639
static int do_memtest_w (struct sx_board *board, int min, int max)
1640
{
1641
        int i;
1642
 
1643
        MARCHUP   {W0;}
1644
        MARCHUP   {R0;W1;R1;W0;R0;W1;}
1645
        MARCHUP   {R1;W0;W1;}
1646
        MARCHDOWN {R1;W0;W1;W0;}
1647
        MARCHDOWN {R0;W1;W0;}
1648
 
1649
        return 0;
1650
}
1651
#endif
1652
 
1653
 
1654
static int sx_fw_ioctl (struct inode *inode, struct file *filp,
1655
                        unsigned int cmd, unsigned long arg)
1656
{
1657
        int rc = 0;
1658
        int *descr = (int *)arg, i;
1659
        static struct sx_board *board = NULL;
1660
        int nbytes, offset;
1661
        unsigned long data;
1662
        char *tmp;
1663
 
1664
        func_enter();
1665
 
1666
#if 0 
1667
        /* Removed superuser check: Sysops can use the permissions on the device
1668
           file to restrict access. Recommendation: Root only. (root.root 600) */
1669
        if (!capable(CAP_SYS_ADMIN)) {
1670
                return -EPERM;
1671
        }
1672
#endif
1673
 
1674
        sx_dprintk (SX_DEBUG_FIRMWARE, "IOCTL %x: %lx\n", cmd, arg);
1675
 
1676
        if (!board) board = &boards[0];
1677
        if (board->flags & SX_BOARD_PRESENT) {
1678
                sx_dprintk (SX_DEBUG_FIRMWARE, "Board present! (%x)\n",
1679
                            board->flags);
1680
        } else {
1681
                sx_dprintk (SX_DEBUG_FIRMWARE, "Board not present! (%x) all:",
1682
                            board->flags);
1683
                for (i=0;i< SX_NBOARDS;i++)
1684
                        sx_dprintk (SX_DEBUG_FIRMWARE, "<%x> ", boards[i].flags);
1685
                sx_dprintk (SX_DEBUG_FIRMWARE, "\n");
1686
                return -EIO;
1687
        }
1688
 
1689
        switch (cmd) {
1690
        case SXIO_SET_BOARD:
1691
                sx_dprintk (SX_DEBUG_FIRMWARE, "set board to %ld\n", arg);
1692
                if (arg > SX_NBOARDS) return -EIO;
1693
                sx_dprintk (SX_DEBUG_FIRMWARE, "not out of range\n");
1694
                if (!(boards[arg].flags & SX_BOARD_PRESENT)) return -EIO;
1695
                sx_dprintk (SX_DEBUG_FIRMWARE, ".. and present!\n");
1696
                board = &boards[arg];
1697
                break;
1698
        case SXIO_GET_TYPE:
1699
                rc = -ENOENT; /* If we manage to miss one, return error. */
1700
                if (IS_SX_BOARD (board)) rc = SX_TYPE_SX;
1701
                if (IS_CF_BOARD (board)) rc = SX_TYPE_CF;
1702
                if (IS_SI_BOARD (board)) rc = SX_TYPE_SI;
1703
                if (IS_SI1_BOARD (board)) rc = SX_TYPE_SI;
1704
                if (IS_EISA_BOARD (board)) rc = SX_TYPE_SI;
1705
                sx_dprintk (SX_DEBUG_FIRMWARE, "returning type= %d\n", rc);
1706
                break;
1707
        case SXIO_DO_RAMTEST:
1708
                if (sx_initialized) /* Already initialized: better not ramtest the board.  */
1709
                        return -EPERM;
1710
                if (IS_SX_BOARD (board)) {
1711
                        rc          = do_memtest   (board, 0, 0x7000);
1712
                        if (!rc) rc = do_memtest   (board, 0, 0x7000);
1713
                        /*if (!rc) rc = do_memtest_w (board, 0, 0x7000);*/
1714
                } else {
1715
                        rc             = do_memtest   (board, 0, 0x7ff8);
1716
                        /* if (!rc) rc = do_memtest_w (board, 0, 0x7ff8); */
1717
                }
1718
                sx_dprintk (SX_DEBUG_FIRMWARE, "returning memtest result= %d\n", rc);
1719
                break;
1720
        case SXIO_DOWNLOAD:
1721
                if (sx_initialized) /* Already initialized */
1722
                        return -EEXIST;
1723
                if (!sx_reset (board))
1724
                        return -EIO;
1725
                sx_dprintk (SX_DEBUG_INIT, "reset the board...\n");
1726
 
1727
                tmp = kmalloc (SX_CHUNK_SIZE, GFP_USER);
1728
                if (!tmp) return -ENOMEM;
1729
                Get_user (nbytes, descr++);
1730
                Get_user (offset, descr++);
1731
                Get_user (data,  descr++);
1732
                while (nbytes && data) {
1733
                        for (i=0;i<nbytes;i += SX_CHUNK_SIZE) {
1734
                                if (copy_from_user(tmp, (char *)data + i,
1735
                                                   (i + SX_CHUNK_SIZE >
1736
                                                    nbytes) ? nbytes - i :
1737
                                                              SX_CHUNK_SIZE)) {
1738
                                        kfree (tmp);
1739
                                        return -EFAULT;
1740
                                }
1741
                                memcpy_toio    ((char *) (board->base2 + offset + i), tmp,
1742
                                                (i+SX_CHUNK_SIZE>nbytes)?nbytes-i:SX_CHUNK_SIZE);
1743
                        }
1744
 
1745
                        Get_user (nbytes, descr++);
1746
                        Get_user (offset, descr++);
1747
                        Get_user (data,   descr++);
1748
                }
1749
                kfree (tmp);
1750
                sx_nports += sx_init_board (board);
1751
                rc = sx_nports;
1752
                break;
1753
        case SXIO_INIT:
1754
                if (sx_initialized) /* Already initialized */
1755
                        return -EEXIST;
1756
                /* This is not allowed until all boards are initialized... */
1757
                for (i=0;i<SX_NBOARDS;i++) {
1758
                        if ( (boards[i].flags & SX_BOARD_PRESENT) &&
1759
                             !(boards[i].flags & SX_BOARD_INITIALIZED))
1760
                                return -EIO;
1761
                }
1762
                for (i=0;i<SX_NBOARDS;i++)
1763
                        if (!(boards[i].flags & SX_BOARD_PRESENT)) break;
1764
 
1765
                sx_dprintk (SX_DEBUG_FIRMWARE, "initing portstructs, %d boards, "
1766
                            "%d channels, first board: %d ports\n",
1767
                            i, sx_nports, boards[0].nports);
1768
                rc = sx_init_portstructs (i, sx_nports);
1769
                sx_init_drivers ();
1770
                if (rc >= 0)
1771
                        sx_initialized++;
1772
                break;
1773
        case SXIO_SETDEBUG:
1774
                sx_debug = arg;
1775
                break;
1776
        case SXIO_GETDEBUG:
1777
                rc = sx_debug;
1778
                break;
1779
        case SXIO_GETGSDEBUG:
1780
        case SXIO_SETGSDEBUG:
1781
                rc = -EINVAL;
1782
                break;
1783
        case SXIO_GETNPORTS:
1784
                rc = sx_nports;
1785
                break;
1786
        default:
1787
                printk (KERN_WARNING "Unknown ioctl on firmware device (%x).\n", cmd);
1788
                break;
1789
        }
1790
        func_exit ();
1791
        return rc;
1792
}
1793
 
1794
 
1795
static void sx_break (struct tty_struct * tty, int flag)
1796
{
1797
        struct sx_port *port = tty->driver_data;
1798
        int rv;
1799
 
1800
        if (flag)
1801
                rv = sx_send_command (port, HS_START, -1, HS_IDLE_BREAK);
1802
        else
1803
                rv = sx_send_command (port, HS_STOP, -1, HS_IDLE_OPEN);
1804
        if (rv != 1) printk (KERN_ERR "sx: couldn't send break (%x).\n",
1805
                        read_sx_byte (port->board, CHAN_OFFSET (port, hi_hstat)));
1806
}
1807
 
1808
 
1809
static int sx_ioctl (struct tty_struct * tty, struct file * filp,
1810
                     unsigned int cmd, unsigned long arg)
1811
{
1812
        int rc;
1813
        struct sx_port *port = tty->driver_data;
1814
        int ival;
1815
 
1816
        /* func_enter2(); */
1817
 
1818
        rc = 0;
1819
        switch (cmd) {
1820
        case TIOCGSOFTCAR:
1821
                rc = Put_user(((tty->termios->c_cflag & CLOCAL) ? 1 : 0),
1822
                              (unsigned int *) arg);
1823
                break;
1824
        case TIOCSSOFTCAR:
1825
                if ((rc = verify_area(VERIFY_READ, (void *) arg,
1826
                                      sizeof(int))) == 0) {
1827
                        Get_user(ival, (unsigned int *) arg);
1828
                        tty->termios->c_cflag =
1829
                                (tty->termios->c_cflag & ~CLOCAL) |
1830
                                (ival ? CLOCAL : 0);
1831
                }
1832
                break;
1833
        case TIOCGSERIAL:
1834
                if ((rc = verify_area(VERIFY_WRITE, (void *) arg,
1835
                                      sizeof(struct serial_struct))) == 0)
1836
                        rc = gs_getserial(&port->gs, (struct serial_struct *) arg);
1837
                break;
1838
        case TIOCSSERIAL:
1839
                if ((rc = verify_area(VERIFY_READ, (void *) arg,
1840
                                      sizeof(struct serial_struct))) == 0)
1841
                        rc = gs_setserial(&port->gs, (struct serial_struct *) arg);
1842
                break;
1843
        case TIOCMGET:
1844
                if ((rc = verify_area(VERIFY_WRITE, (void *) arg,
1845
                                      sizeof(unsigned int))) == 0) {
1846
                        ival = sx_getsignals(port);
1847
                        put_user(ival, (unsigned int *) arg);
1848
                }
1849
                break;
1850
        case TIOCMBIS:
1851
                if ((rc = verify_area(VERIFY_READ, (void *) arg,
1852
                                      sizeof(unsigned int))) == 0) {
1853
                        Get_user(ival, (unsigned int *) arg);
1854
                        sx_setsignals(port, ((ival & TIOCM_DTR) ? 1 : -1),
1855
                                             ((ival & TIOCM_RTS) ? 1 : -1));
1856
                        sx_reconfigure_port(port);
1857
                }
1858
                break;
1859
        case TIOCMBIC:
1860
                if ((rc = verify_area(VERIFY_READ, (void *) arg,
1861
                                      sizeof(unsigned int))) == 0) {
1862
                        Get_user(ival, (unsigned int *) arg);
1863
                        sx_setsignals(port, ((ival & TIOCM_DTR) ? 0 : -1),
1864
                                             ((ival & TIOCM_RTS) ? 0 : -1));
1865
                        sx_reconfigure_port(port);
1866
                }
1867
                break;
1868
        case TIOCMSET:
1869
                if ((rc = verify_area(VERIFY_READ, (void *) arg,
1870
                                      sizeof(unsigned int))) == 0) {
1871
                        Get_user(ival, (unsigned int *) arg);
1872
                        sx_setsignals(port, ((ival & TIOCM_DTR) ? 1 : 0),
1873
                                             ((ival & TIOCM_RTS) ? 1 : 0));
1874
                        sx_reconfigure_port(port);
1875
                }
1876
                break;
1877
        default:
1878
                rc = -ENOIOCTLCMD;
1879
                break;
1880
        }
1881
 
1882
        /* func_exit(); */
1883
        return rc;
1884
}
1885
 
1886
 
1887
/* The throttle/unthrottle scheme for the Specialix card is different
1888
 * from other drivers and deserves some explanation.
1889
 * The Specialix hardware takes care of XON/XOFF
1890
 * and CTS/RTS flow control itself.  This means that all we have to
1891
 * do when signalled by the upper tty layer to throttle/unthrottle is
1892
 * to make a note of it here.  When we come to read characters from the
1893
 * rx buffers on the card (sx_receive_chars()) we look to see if the
1894
 * upper layer can accept more (as noted here in sx_rx_throt[]).
1895
 * If it can't we simply don't remove chars from the cards buffer.
1896
 * When the tty layer can accept chars, we again note that here and when
1897
 * sx_receive_chars() is called it will remove them from the cards buffer.
1898
 * The card will notice that a ports buffer has drained below some low
1899
 * water mark and will unflow control the line itself, using whatever
1900
 * flow control scheme is in use for that port. -- Simon Allen
1901
 */
1902
 
1903
static void sx_throttle (struct tty_struct * tty)
1904
{
1905
        struct sx_port *port = (struct sx_port *)tty->driver_data;
1906
 
1907
        func_enter2();
1908
        /* If the port is using any type of input flow
1909
         * control then throttle the port.
1910
         */
1911
        if((tty->termios->c_cflag & CRTSCTS) || (I_IXOFF(tty)) ) {
1912
                port->gs.flags |= SX_RX_THROTTLE;
1913
        }
1914
        func_exit();
1915
}
1916
 
1917
 
1918
static void sx_unthrottle (struct tty_struct * tty)
1919
{
1920
        struct sx_port *port = (struct sx_port *)tty->driver_data;
1921
 
1922
        func_enter2();
1923
        /* Always unthrottle even if flow control is not enabled on
1924
         * this port in case we disabled flow control while the port
1925
         * was throttled
1926
         */
1927
        port->gs.flags &= ~SX_RX_THROTTLE;
1928
        func_exit();
1929
        return;
1930
}
1931
 
1932
 
1933
/* ********************************************************************** *
1934
 *                    Here are the initialization routines.               *
1935
 * ********************************************************************** */
1936
 
1937
 
1938
 
1939
 
1940
static int sx_init_board (struct sx_board *board)
1941
{
1942
        int addr;
1943
        int chans;
1944
        int type;
1945
 
1946
        func_enter();
1947
 
1948
        /* This is preceded by downloading the download code. */
1949
 
1950
        board->flags |= SX_BOARD_INITIALIZED;
1951
 
1952
        if (read_sx_byte (board, 0))
1953
                /* CF boards may need this. */
1954
                write_sx_byte(board,0, 0);
1955
 
1956
        /* This resets the processor again, to make sure it didn't do any
1957
           foolish things while we were downloading the image */
1958
        if (!sx_reset (board))
1959
                return 0;
1960
 
1961
        sx_start_board (board);
1962
        udelay (10);
1963
        if (!sx_busy_wait_neq (board, 0, 0xff, 0)) {
1964
                printk (KERN_ERR "sx: Ooops. Board won't initialize.\n");
1965
                return 0;
1966
        }
1967
 
1968
        /* Ok. So now the processor on the card is running. It gathered
1969
           some info for us... */
1970
        sx_dprintk (SX_DEBUG_INIT, "The sxcard structure:\n");
1971
        if (sx_debug & SX_DEBUG_INIT) my_hd ((char *)(board->base), 0x10);
1972
        sx_dprintk (SX_DEBUG_INIT, "the first sx_module structure:\n");
1973
        if (sx_debug & SX_DEBUG_INIT) my_hd ((char *)(board->base + 0x80), 0x30);
1974
 
1975
        sx_dprintk (SX_DEBUG_INIT,
1976
                    "init_status: %x, %dk memory, firmware V%x.%02x,\n",
1977
                    read_sx_byte (board, 0), read_sx_byte(board, 1),
1978
                    read_sx_byte (board, 5), read_sx_byte(board, 4));
1979
 
1980
        if (read_sx_byte (board, 0) == 0xff) {
1981
                printk (KERN_INFO "sx: No modules found. Sorry.\n");
1982
                board->nports = 0;
1983
                return 0;
1984
        }
1985
 
1986
        chans = 0;
1987
 
1988
        if (IS_SX_BOARD(board)) {
1989
                sx_write_board_word (board, cc_int_count, sx_maxints);
1990
        } else {
1991
                if (sx_maxints)
1992
                        sx_write_board_word (board, cc_int_count, SI_PROCESSOR_CLOCK/8/sx_maxints);
1993
        }
1994
 
1995
        /* grab the first module type... */
1996
        /*  board->ta_type = mod_compat_type (read_sx_byte (board, 0x80 + 0x08)); */
1997
        board->ta_type = mod_compat_type (sx_read_module_byte (board, 0x80, mc_chip));
1998
 
1999
        /* XXX byteorder */
2000
        for (addr = 0x80;addr != 0;addr = read_sx_word (board, addr) & 0x7fff) {
2001
                type = sx_read_module_byte (board, addr, mc_chip);
2002
                sx_dprintk (SX_DEBUG_INIT, "Module at %x: %d channels\n",
2003
                            addr, read_sx_byte (board, addr + 2));
2004
 
2005
                chans += sx_read_module_byte (board, addr, mc_type);
2006
 
2007
                sx_dprintk (SX_DEBUG_INIT, "module is an %s, which has %s/%s panels\n",
2008
                            mod_type_s (type),
2009
                            pan_type_s (sx_read_module_byte (board, addr, mc_mods) & 0xf),
2010
                            pan_type_s (sx_read_module_byte (board, addr, mc_mods) >> 4));
2011
 
2012
                sx_dprintk (SX_DEBUG_INIT, "CD1400 versions: %x/%x, ASIC version: %x\n",
2013
                            sx_read_module_byte (board, addr, mc_rev1),
2014
                            sx_read_module_byte (board, addr, mc_rev2),
2015
                            sx_read_module_byte (board, addr, mc_mtaasic_rev));
2016
 
2017
                /* The following combinations are illegal: It should theoretically
2018
                   work, but timing problems make the bus HANG. */
2019
 
2020
                if (mod_compat_type (type) != board->ta_type) {
2021
                        printk (KERN_ERR "sx: This is an invalid configuration.\n"
2022
                                "Don't mix TA/MTA/SXDC on the same hostadapter.\n");
2023
                        chans=0;
2024
                        break;
2025
                }
2026
                if ((IS_EISA_BOARD(board) ||
2027
                     IS_SI_BOARD(board)) && (mod_compat_type(type) == 4)) {
2028
                        printk (KERN_ERR "sx: This is an invalid configuration.\n"
2029
                                "Don't use SXDCs on an SI/XIO adapter.\n");
2030
                        chans=0;
2031
                        break;
2032
                }
2033
#if 0 /* Problem fixed: firmware 3.05 */
2034
                if (IS_SX_BOARD(board) && (type == TA8)) {
2035
                        /* There are some issues with the firmware and the DCD/RTS
2036
                           lines. It might work if you tie them together or something.
2037
                           It might also work if you get a newer sx_firmware.   Therefore
2038
                           this is just a warning. */
2039
                        printk (KERN_WARNING "sx: The SX host doesn't work too well "
2040
                                "with the TA8 adapters.\nSpecialix is working on it.\n");
2041
                }
2042
#endif
2043
        }
2044
 
2045
        if (chans) {
2046
                /* board->flags |= SX_BOARD_PRESENT; */
2047
                if(board->irq > 0) {
2048
                        /* fixed irq, probably PCI */
2049
                        if(sx_irqmask & (1 << board->irq)) { /* may we use this irq? */
2050
                                if(request_irq(board->irq, sx_interrupt, SA_SHIRQ | SA_INTERRUPT, "sx", board)) {
2051
                                        printk(KERN_ERR "sx: Cannot allocate irq %d.\n", board->irq);
2052
                                        board->irq = 0;
2053
                                }
2054
                        } else
2055
                                board->irq = 0;
2056
                } else if(board->irq < 0 && sx_irqmask) {
2057
                        /* auto-allocate irq */
2058
                        int irqnr;
2059
                        int irqmask = sx_irqmask & (IS_SX_BOARD(board) ? SX_ISA_IRQ_MASK : SI2_ISA_IRQ_MASK);
2060
                        for(irqnr = 15; irqnr > 0; irqnr--)
2061
                                if(irqmask & (1 << irqnr))
2062
                                        if(! request_irq(irqnr, sx_interrupt, SA_SHIRQ | SA_INTERRUPT, "sx", board))
2063
                                                break;
2064
                        if(! irqnr)
2065
                                printk(KERN_ERR "sx: Cannot allocate IRQ.\n");
2066
                        board->irq = irqnr;
2067
                } else
2068
                        board->irq = 0;
2069
 
2070
                if (board->irq) {
2071
                        /* Found a valid interrupt, start up interrupts! */
2072
                        sx_dprintk (SX_DEBUG_INIT, "Using irq %d.\n", board->irq);
2073
                        sx_start_interrupts (board);
2074
                        board->poll = sx_slowpoll;
2075
                        board->flags |= SX_IRQ_ALLOCATED;
2076
                } else {
2077
                        /* no irq: setup board for polled operation */
2078
                        board->poll = sx_poll;
2079
                        sx_dprintk (SX_DEBUG_INIT, "Using poll-interval %d.\n", board->poll);
2080
                }
2081
 
2082
                /* The timer should be initialized anyway: That way we can safely
2083
                         del_timer it when the module is unloaded. */
2084
                init_timer (&board->timer);
2085
 
2086
                if (board->poll) {
2087
                        board->timer.data = (unsigned long) board;
2088
                        board->timer.function = sx_pollfunc;
2089
                        board->timer.expires = jiffies + board->poll;
2090
                        add_timer (&board->timer);
2091
                }
2092
        } else {
2093
                board->irq = 0;
2094
        }
2095
 
2096
        board->nports = chans;
2097
        sx_dprintk (SX_DEBUG_INIT, "returning %d ports.", board->nports);
2098
 
2099
        func_exit();
2100
        return chans;
2101
}
2102
 
2103
 
2104
static void printheader(void)
2105
{
2106
        static int header_printed;
2107
 
2108
        if (!header_printed) {
2109
                printk (KERN_INFO "Specialix SX driver "
2110
                        "(C) 1998/1999 R.E.Wolff@BitWizard.nl \n");
2111
                printk (KERN_INFO "sx: version %s\n", RCS_ID);
2112
                header_printed = 1;
2113
        }
2114
}
2115
 
2116
 
2117
static int probe_sx (struct sx_board *board)
2118
{
2119
        struct vpd_prom vpdp;
2120
        char *p;
2121
        int i;
2122
 
2123
        func_enter();
2124
 
2125
        if (!IS_CF_BOARD (board)) {
2126
                sx_dprintk (SX_DEBUG_PROBE, "Going to verify vpd prom at %lx.\n",
2127
                            board->base + SX_VPD_ROM);
2128
 
2129
                if (sx_debug & SX_DEBUG_PROBE)
2130
                        my_hd ((char *)(board->base + SX_VPD_ROM), 0x40);
2131
 
2132
                p = (char *) &vpdp;
2133
                for (i=0;i< sizeof (struct vpd_prom);i++)
2134
                        *p++ = read_sx_byte (board, SX_VPD_ROM + i*2);
2135
 
2136
                if (sx_debug & SX_DEBUG_PROBE)
2137
                        my_hd ((char *)&vpdp, 0x20);
2138
 
2139
                sx_dprintk (SX_DEBUG_PROBE, "checking identifier...\n");
2140
 
2141
                if (strncmp (vpdp.identifier, SX_VPD_IDENT_STRING, 16) != 0) {
2142
                        sx_dprintk (SX_DEBUG_PROBE, "Got non-SX identifier: '%s'\n",
2143
                                    vpdp.identifier);
2144
                        return 0;
2145
                }
2146
        }
2147
 
2148
        printheader ();
2149
 
2150
        if (!IS_CF_BOARD (board)) {
2151
                printk (KERN_DEBUG "sx: Found an SX board at %lx\n", board->hw_base);
2152
                printk (KERN_DEBUG "sx: hw_rev: %d, assembly level: %d, uniq ID:%08x, ",
2153
                        vpdp.hwrev, vpdp.hwass, vpdp.uniqid);
2154
                printk (           "Manufactured: %d/%d\n",
2155
                        1970 + vpdp.myear, vpdp.mweek);
2156
 
2157
 
2158
                if ((((vpdp.uniqid >> 24) & SX_UNIQUEID_MASK) != SX_PCI_UNIQUEID1) &&
2159
                    (((vpdp.uniqid >> 24) & SX_UNIQUEID_MASK) != SX_ISA_UNIQUEID1)) {
2160
                        /* This might be a bit harsh. This was the primary reason the
2161
                           SX/ISA card didn't work at first... */
2162
                        printk (KERN_ERR "sx: Hmm. Not an SX/PCI or SX/ISA card. Sorry: giving up.\n");
2163
                        return (0);
2164
                }
2165
 
2166
                if (((vpdp.uniqid >> 24) & SX_UNIQUEID_MASK) == SX_ISA_UNIQUEID1) {
2167
                        if (board->base & 0x8000) {
2168
                                printk (KERN_WARNING "sx: Warning: There may be hardware problems with the card at %lx.\n", board->base);
2169
                                printk (KERN_WARNING "sx: Read sx.txt for more info.\n");
2170
                        }
2171
                }
2172
        }
2173
 
2174
        board->nports = -1;
2175
 
2176
        /* This resets the processor, and keeps it off the bus. */
2177
        if (!sx_reset (board))
2178
                return 0;
2179
        sx_dprintk (SX_DEBUG_INIT, "reset the board...\n");
2180
 
2181
        board->flags |= SX_BOARD_PRESENT;
2182
 
2183
        func_exit();
2184
        return 1;
2185
}
2186
 
2187
 
2188
 
2189
/* Specialix probes for this card at 32k increments from 640k to 16M.
2190
   I consider machines with less than 16M unlikely nowadays, so I'm
2191
   not probing above 1Mb. Also, 0xa0000, 0xb0000, are taken by the VGA
2192
   card. 0xe0000 and 0xf0000 are taken by the BIOS. That only leaves
2193
   0xc0000, 0xc8000, 0xd0000 and 0xd8000 . */
2194
 
2195
static int probe_si (struct sx_board *board)
2196
{
2197
        int i;
2198
 
2199
        func_enter();
2200
        sx_dprintk (SX_DEBUG_PROBE, "Going to verify SI signature hw %lx at %lx.\n", board->hw_base,
2201
                    board->base + SI2_ISA_ID_BASE);
2202
 
2203
        if (sx_debug & SX_DEBUG_PROBE)
2204
                my_hd ((char *)(board->base + SI2_ISA_ID_BASE), 0x8);
2205
 
2206
        if (!IS_EISA_BOARD(board)  ) {
2207
          if( IS_SI1_BOARD(board) )
2208
            {
2209
                for (i=0;i<8;i++) {
2210
                  write_sx_byte (board, SI2_ISA_ID_BASE+7-i,i);
2211
 
2212
                }
2213
            }
2214
                for (i=0;i<8;i++) {
2215
                        if ((read_sx_byte (board, SI2_ISA_ID_BASE+7-i) & 7) != i) {
2216
                                return 0;
2217
                        }
2218
                }
2219
        }
2220
 
2221
        /* Now we're pretty much convinced that there is an SI board here,
2222
           but to prevent trouble, we'd better double check that we don't
2223
           have an SI1 board when we're probing for an SI2 board.... */
2224
 
2225
        write_sx_byte (board, SI2_ISA_ID_BASE,0x10);
2226
        if ( IS_SI1_BOARD(board)) {
2227
                /* This should be an SI1 board, which has this
2228
                   location writable... */
2229
                if (read_sx_byte (board, SI2_ISA_ID_BASE) != 0x10)
2230
                        return 0;
2231
        } else {
2232
                /* This should be an SI2 board, which has the bottom
2233
                   3 bits non-writable... */
2234
                if (read_sx_byte (board, SI2_ISA_ID_BASE) == 0x10)
2235
                        return 0;
2236
        }
2237
 
2238
        printheader ();
2239
 
2240
        printk (KERN_DEBUG "sx: Found an SI board at %lx\n", board->hw_base);
2241
        /* Compared to the SX boards, it is a complete guess as to what
2242
                 this card is up to... */
2243
 
2244
        board->nports = -1;
2245
 
2246
        /* This resets the processor, and keeps it off the bus. */
2247
        if (!sx_reset (board))
2248
                return 0;
2249
        sx_dprintk (SX_DEBUG_INIT, "reset the board...\n");
2250
 
2251
        board->flags |= SX_BOARD_PRESENT;
2252
 
2253
        func_exit();
2254
        return 1;
2255
}
2256
 
2257
 
2258
static int sx_init_drivers(void)
2259
{
2260
        int error;
2261
 
2262
        func_enter();
2263
 
2264
        memset(&sx_driver, 0, sizeof(sx_driver));
2265
        sx_driver.magic = TTY_DRIVER_MAGIC;
2266
        sx_driver.driver_name = "specialix_sx";
2267
        sx_driver.name = "ttyX";
2268
        sx_driver.major = SX_NORMAL_MAJOR;
2269
        sx_driver.num = sx_nports;
2270
        sx_driver.type = TTY_DRIVER_TYPE_SERIAL;
2271
        sx_driver.subtype = SX_TYPE_NORMAL;
2272
        sx_driver.init_termios = tty_std_termios;
2273
        sx_driver.init_termios.c_cflag =
2274
          B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2275
        sx_driver.flags = TTY_DRIVER_REAL_RAW;
2276
        sx_driver.refcount = &sx_refcount;
2277
        sx_driver.table = sx_table;
2278
        sx_driver.termios = sx_termios;
2279
        sx_driver.termios_locked = sx_termios_locked;
2280
        sx_driver.break_ctl = sx_break;
2281
 
2282
        sx_driver.open  = sx_open;
2283
        sx_driver.close = gs_close;
2284
        sx_driver.write = gs_write;
2285
        sx_driver.put_char = gs_put_char;
2286
        sx_driver.flush_chars = gs_flush_chars;
2287
        sx_driver.write_room = gs_write_room;
2288
        sx_driver.chars_in_buffer = gs_chars_in_buffer;
2289
        sx_driver.flush_buffer = gs_flush_buffer;
2290
        sx_driver.ioctl = sx_ioctl;
2291
        sx_driver.throttle = sx_throttle;
2292
        sx_driver.unthrottle = sx_unthrottle;
2293
        sx_driver.set_termios = gs_set_termios;
2294
        sx_driver.stop = gs_stop;
2295
        sx_driver.start = gs_start;
2296
        sx_driver.hangup = gs_hangup;
2297
 
2298
        sx_callout_driver = sx_driver;
2299
        sx_callout_driver.name = "cux";
2300
        sx_callout_driver.major = SX_CALLOUT_MAJOR;
2301
        sx_callout_driver.subtype = SX_TYPE_CALLOUT;
2302
 
2303
        if ((error = tty_register_driver(&sx_driver))) {
2304
                printk(KERN_ERR "sx: Couldn't register sx driver, error = %d\n",
2305
                       error);
2306
                return 1;
2307
        }
2308
        if ((error = tty_register_driver(&sx_callout_driver))) {
2309
                tty_unregister_driver(&sx_driver);
2310
                printk(KERN_ERR "sx: Couldn't register sx callout driver, error = %d\n",
2311
                       error);
2312
                return 1;
2313
        }
2314
 
2315
        func_exit();
2316
        return 0;
2317
}
2318
 
2319
 
2320
static void * ckmalloc (int size)
2321
{
2322
        void *p;
2323
 
2324
        p = kmalloc(size, GFP_KERNEL);
2325
        if (p)
2326
                memset(p, 0, size);
2327
        return p;
2328
}
2329
 
2330
 
2331
static int sx_init_portstructs (int nboards, int nports)
2332
{
2333
        struct sx_board *board;
2334
        struct sx_port *port;
2335
        int i, j;
2336
        int addr, chans;
2337
        int portno;
2338
 
2339
        func_enter();
2340
 
2341
        /* Many drivers statically allocate the maximum number of ports
2342
           There is no reason not to allocate them dynamically. Is there? -- REW */
2343
        sx_ports          = ckmalloc(nports * sizeof (struct sx_port));
2344
        if (!sx_ports)
2345
                return -ENOMEM;
2346
 
2347
        sx_termios        = ckmalloc(nports * sizeof (struct termios *));
2348
        if (!sx_termios) {
2349
                kfree (sx_ports);
2350
                return -ENOMEM;
2351
        }
2352
 
2353
        sx_termios_locked = ckmalloc(nports * sizeof (struct termios *));
2354
        if (!sx_termios_locked) {
2355
                kfree (sx_ports);
2356
                kfree (sx_termios);
2357
                return -ENOMEM;
2358
        }
2359
 
2360
        /* Adjust the values in the "driver" */
2361
        sx_driver.termios = sx_termios;
2362
        sx_driver.termios_locked = sx_termios_locked;
2363
 
2364
        port = sx_ports;
2365
        for (i = 0; i < nboards; i++) {
2366
                board = &boards[i];
2367
                board->ports = port;
2368
                for (j=0; j < boards[i].nports;j++) {
2369
                        sx_dprintk (SX_DEBUG_INIT, "initing port %d\n", j);
2370
                        port->gs.callout_termios = tty_std_termios;
2371
                        port->gs.normal_termios = tty_std_termios;
2372
                        port->gs.magic = SX_MAGIC;
2373
                        port->gs.close_delay = HZ/2;
2374
                        port->gs.closing_wait = 30 * HZ;
2375
                        port->board = board;
2376
                        port->gs.rd = &sx_real_driver;
2377
#ifdef NEW_WRITE_LOCKING
2378
                        port->gs.port_write_sem = MUTEX;
2379
#endif
2380
                        /*
2381
                         * Initializing wait queue
2382
                         */
2383
                        init_waitqueue_head(&port->gs.open_wait);
2384
                        init_waitqueue_head(&port->gs.close_wait);
2385
 
2386
                        port++;
2387
                }
2388
        }
2389
 
2390
        port = sx_ports;
2391
        portno = 0;
2392
        for (i = 0; i < nboards; i++) {
2393
                board = &boards[i];
2394
                board->port_base = portno;
2395
                /* Possibly the configuration was rejected. */
2396
                sx_dprintk (SX_DEBUG_PROBE, "Board has %d channels\n", board->nports);
2397
                if (board->nports <= 0) continue;
2398
                /* XXX byteorder ?? */
2399
                for (addr = 0x80;addr != 0;addr = read_sx_word (board, addr) & 0x7fff) {
2400
                        chans = sx_read_module_byte (board, addr, mc_type);
2401
                        sx_dprintk (SX_DEBUG_PROBE, "Module at %x: %d channels\n", addr, chans);
2402
                        sx_dprintk (SX_DEBUG_PROBE, "Port at");
2403
                        for (j=0;j<chans;j++) {
2404
                                /* The "sx-way" is the way it SHOULD be done. That way in the
2405
                                   future, the firmware may for example pack the structures a bit
2406
                                   more efficient. Neil tells me it isn't going to happen anytime
2407
                                   soon though. */
2408
                                if (IS_SX_BOARD(board))
2409
                                        port->ch_base = sx_read_module_word (board, addr+j*2, mc_chan_pointer);
2410
                                else
2411
                                        port->ch_base = addr + 0x100 + 0x300*j;
2412
 
2413
                                sx_dprintk (SX_DEBUG_PROBE, " %x", port->ch_base);
2414
                                port->line = portno++;
2415
                                port++;
2416
                        }
2417
                        sx_dprintk (SX_DEBUG_PROBE, "\n");
2418
                }
2419
                /* This has to be done earlier. */
2420
                /* board->flags |= SX_BOARD_INITIALIZED; */
2421
        }
2422
 
2423
        func_exit();
2424
        return 0;
2425
}
2426
 
2427
static void __exit sx_release_drivers(void)
2428
{
2429
        func_enter();
2430
        tty_unregister_driver(&sx_driver);
2431
        tty_unregister_driver(&sx_callout_driver);
2432
        func_exit();
2433
}
2434
 
2435
#ifdef TWO_ZERO
2436
#define PDEV unsigned char pci_bus, unsigned pci_fun
2437
#define pdev pci_bus, pci_fun
2438
#else
2439
#define PDEV   struct pci_dev *pdev
2440
#endif
2441
 
2442
 
2443
#ifdef CONFIG_PCI
2444
 /********************************************************
2445
 * Setting bit 17 in the CNTRL register of the PLX 9050  *
2446
 * chip forces a retry on writes while a read is pending.*
2447
 * This is to prevent the card locking up on Intel Xeon  *
2448
 * multiprocessor systems with the NX chipset.    -- NV  *
2449
 ********************************************************/
2450
 
2451
/* Newer cards are produced with this bit set from the configuration
2452
   EEprom.  As the bit is read/write for the CPU, we can fix it here,
2453
   if we detect that it isn't set correctly. -- REW */
2454
 
2455
static void fix_sx_pci (PDEV, struct sx_board *board)
2456
{
2457
        unsigned int hwbase;
2458
        unsigned long rebase;
2459
        unsigned int t;
2460
 
2461
#define CNTRL_REG_OFFSET        0x50
2462
#define CNTRL_REG_GOODVALUE     0x18260000
2463
 
2464
        pci_read_config_dword(pdev, PCI_BASE_ADDRESS_0, &hwbase);
2465
        hwbase &= PCI_BASE_ADDRESS_MEM_MASK;
2466
        rebase = (ulong) ioremap(hwbase, 0x80);
2467
        t = readl (rebase + CNTRL_REG_OFFSET);
2468
        if (t != CNTRL_REG_GOODVALUE) {
2469
                printk (KERN_DEBUG "sx: performing cntrl reg fix: %08x -> %08x\n", t, CNTRL_REG_GOODVALUE);
2470
                writel (CNTRL_REG_GOODVALUE, rebase + CNTRL_REG_OFFSET);
2471
        }
2472
        my_iounmap (hwbase, rebase);
2473
}
2474
#endif
2475
 
2476
 
2477
static int __init sx_init(void)
2478
{
2479
        int i;
2480
        int found = 0;
2481
        int eisa_slot;
2482
        struct sx_board *board;
2483
 
2484
#ifdef CONFIG_PCI
2485
#ifndef TWO_ZERO
2486
        struct pci_dev *pdev = NULL;
2487
#else
2488
        unsigned char pci_bus, pci_fun;
2489
        /* in 2.2.x pdev is a pointer defining a PCI device. In 2.0 its the bus/fn */
2490
#endif
2491
        unsigned int tint;
2492
        unsigned short tshort;
2493
#endif
2494
 
2495
        func_enter();
2496
        sx_dprintk (SX_DEBUG_INIT, "Initing sx module... (sx_debug=%d)\n", sx_debug);
2497
        if (abs ((long) (&sx_debug) - sx_debug) < 0x10000) {
2498
                printk (KERN_WARNING "sx: sx_debug is an address, instead of a value. "
2499
                        "Assuming -1.\n");
2500
                printk ("(%p)\n", &sx_debug);
2501
                sx_debug=-1;
2502
        }
2503
 
2504
#ifdef CONFIG_PCI
2505
        if (pci_present ()) {
2506
#ifndef TWO_ZERO
2507
                while ((pdev = pci_find_device (PCI_VENDOR_ID_SPECIALIX,
2508
                                                PCI_DEVICE_ID_SPECIALIX_SX_XIO_IO8,
2509
                                                      pdev))) {
2510
                        if (pci_enable_device(pdev))
2511
                                continue;
2512
#else
2513
                        for (i=0;i< SX_NBOARDS;i++) {
2514
                                if (pcibios_find_device (PCI_VENDOR_ID_SPECIALIX,
2515
                                                         PCI_DEVICE_ID_SPECIALIX_SX_XIO_IO8, i,
2516
                                                               &pci_bus, &pci_fun)) break;
2517
#endif
2518
                        /* Specialix has a whole bunch of cards with
2519
                           0x2000 as the device ID. They say its because
2520
                           the standard requires it. Stupid standard. */
2521
                        /* It seems that reading a word doesn't work reliably on 2.0.
2522
                           Also, reading a non-aligned dword doesn't work. So we read the
2523
                           whole dword at 0x2c and extract the word at 0x2e (SUBSYSTEM_ID)
2524
                           ourselves */
2525
                        /* I don't know why the define doesn't work, constant 0x2c does --REW */
2526
                        pci_read_config_dword (pdev, 0x2c, &tint);
2527
                        tshort = (tint >> 16) & 0xffff;
2528
                        sx_dprintk (SX_DEBUG_PROBE, "Got a specialix card: %x.\n", tint);
2529
                        /* sx_dprintk (SX_DEBUG_PROBE, "pdev = %d/%d    (%x)\n", pdev, tint); */
2530
                        if ((tshort != 0x0200) && (tshort != 0x0300)) {
2531
                                sx_dprintk (SX_DEBUG_PROBE, "But it's not an SX card (%d)...\n",
2532
                                            tshort);
2533
                                continue;
2534
                        }
2535
                        board = &boards[found];
2536
 
2537
                        board->flags &= ~SX_BOARD_TYPE;
2538
                        board->flags |= (tshort == 0x200)?SX_PCI_BOARD:
2539
                                                          SX_CFPCI_BOARD;
2540
 
2541
                        /* CF boards use base address 3.... */
2542
                        if (IS_CF_BOARD (board))
2543
                                board->hw_base = pci_resource_start (pdev, 3);
2544
                        else
2545
                                board->hw_base = pci_resource_start (pdev, 2);
2546
                        board->base2 =
2547
                        board->base = (ulong) ioremap(board->hw_base, WINDOW_LEN (board));
2548
                        if (!board->base) {
2549
                                printk(KERN_ERR "ioremap failed\n");
2550
                                /* XXX handle error */
2551
                        }
2552
 
2553
                        /* Most of the stuff on the CF board is offset by
2554
                           0x18000 ....  */
2555
                        if (IS_CF_BOARD (board)) board->base += 0x18000;
2556
 
2557
                        board->irq = get_irq (pdev);
2558
 
2559
                        sx_dprintk (SX_DEBUG_PROBE, "Got a specialix card: %x/%lx(%d) %x.\n",
2560
                                    tint, boards[found].base, board->irq, board->flags);
2561
 
2562
                        if (probe_sx (board)) {
2563
                                found++;
2564
                                fix_sx_pci (pdev, board);
2565
                        } else
2566
                                my_iounmap (board->hw_base, board->base);
2567
                }
2568
        }
2569
#endif
2570
 
2571
        for (i=0;i<NR_SX_ADDRS;i++) {
2572
                board = &boards[found];
2573
                board->hw_base = sx_probe_addrs[i];
2574
                board->base2 =
2575
                board->base = (ulong) ioremap(board->hw_base, SX_WINDOW_LEN);
2576
                board->flags &= ~SX_BOARD_TYPE;
2577
                board->flags |= SX_ISA_BOARD;
2578
                board->irq = sx_irqmask?-1:0;
2579
 
2580
                if (probe_sx (board)) {
2581
                        found++;
2582
                } else {
2583
                        my_iounmap (board->hw_base, board->base);
2584
                }
2585
        }
2586
 
2587
        for (i=0;i<NR_SI_ADDRS;i++) {
2588
                board = &boards[found];
2589
                board->hw_base = si_probe_addrs[i];
2590
                board->base2 =
2591
                board->base = (ulong) ioremap(board->hw_base, SI2_ISA_WINDOW_LEN);
2592
                board->flags &= ~SX_BOARD_TYPE;
2593
                board->flags |=  SI_ISA_BOARD;
2594
                board->irq = sx_irqmask ?-1:0;
2595
 
2596
                if (probe_si (board)) {
2597
                        found++;
2598
                } else {
2599
                        my_iounmap (board->hw_base, board->base);
2600
                }
2601
        }
2602
        for (i=0;i<NR_SI1_ADDRS;i++) {
2603
                board = &boards[found];
2604
                board->hw_base = si1_probe_addrs[i];
2605
                board->base2 =
2606
                board->base = (ulong) ioremap(board->hw_base, SI1_ISA_WINDOW_LEN);
2607
                board->flags &= ~SX_BOARD_TYPE;
2608
                board->flags |=  SI1_ISA_BOARD;
2609
                board->irq = sx_irqmask ?-1:0;
2610
 
2611
                if (probe_si (board)) {
2612
                        found++;
2613
                } else {
2614
                        my_iounmap (board->hw_base, board->base);
2615
                }
2616
        }
2617
 
2618
        sx_dprintk(SX_DEBUG_PROBE, "Probing for EISA cards\n");
2619
        for(eisa_slot=0x1000; eisa_slot<0x10000; eisa_slot+=0x1000)
2620
        {
2621
                if((inb(eisa_slot+0xc80)==0x4d) &&
2622
                   (inb(eisa_slot+0xc81)==0x98))
2623
                {
2624
                        sx_dprintk(SX_DEBUG_PROBE, "%s : Signature found in EISA slot %d, Product %d Rev %d\n",
2625
                                                "XIO", (eisa_slot>>12), inb(eisa_slot+0xc82), inb(eisa_slot+0xc83));
2626
 
2627
                        board = &boards[found];
2628
                        board->eisa_base = eisa_slot;
2629
                        board->flags &= ~SX_BOARD_TYPE;
2630
                        board->flags |= SI_EISA_BOARD;
2631
 
2632
                        board->hw_base = (((inb(0xc01+eisa_slot) << 8) + inb(0xc00+eisa_slot)) << 16);
2633
                        board->base2 =
2634
                        board->base = (ulong) ioremap(board->hw_base, SI2_EISA_WINDOW_LEN);
2635
 
2636
                        sx_dprintk(SX_DEBUG_PROBE, "IO hw_base address: %lx\n", board->hw_base);
2637
                        sx_dprintk(SX_DEBUG_PROBE, "base: %lx\n", board->base);
2638
                        board->irq = inb(board->eisa_base+0xc02)>>4;
2639
                        sx_dprintk(SX_DEBUG_PROBE, "IRQ: %d\n", board->irq);
2640
 
2641
                        probe_si(board);
2642
 
2643
                        found++;
2644
                }
2645
        }
2646
        if (found) {
2647
                printk (KERN_INFO "sx: total of %d boards detected.\n", found);
2648
 
2649
                if (misc_register(&sx_fw_device) < 0) {
2650
                        printk(KERN_ERR "SX: Unable to register firmware loader driver.\n");
2651
                        return -EIO;
2652
                }
2653
        }
2654
 
2655
        func_exit();
2656
        return found?0:-EIO;
2657
}
2658
 
2659
 
2660
static void __exit sx_exit (void)
2661
{
2662
        int i;
2663
        struct sx_board *board;
2664
 
2665
        func_enter();
2666
        for (i = 0; i < SX_NBOARDS; i++) {
2667
                board = &boards[i];
2668
                if (board->flags & SX_BOARD_INITIALIZED) {
2669
                        sx_dprintk (SX_DEBUG_CLEANUP, "Cleaning up board at %lx\n", board->base);
2670
                        /* The board should stop messing with us.
2671
                           (actually I mean the interrupt) */
2672
                        sx_reset (board);
2673
                        if ((board->irq) && (board->flags & SX_IRQ_ALLOCATED))
2674
                                free_irq (board->irq, board);
2675
 
2676
                        /* It is safe/allowed to del_timer a non-active timer */
2677
                        del_timer (& board->timer);
2678
                        my_iounmap (board->hw_base, board->base);
2679
                }
2680
        }
2681
        if (misc_deregister(&sx_fw_device) < 0) {
2682
                printk (KERN_INFO "sx: couldn't deregister firmware loader device\n");
2683
        }
2684
        sx_dprintk (SX_DEBUG_CLEANUP, "Cleaning up drivers (%d)\n", sx_initialized);
2685
        if (sx_initialized)
2686
                sx_release_drivers ();
2687
 
2688
        kfree (sx_ports);
2689
        kfree (sx_termios);
2690
        kfree (sx_termios_locked);
2691
        func_exit();
2692
}
2693
 
2694
module_init(sx_init);
2695
module_exit(sx_exit);
2696
 

powered by: WebSVN 2.1.0

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