blob: 8f591945ebd91d747f861b14ac0c440f99f5aac7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*****************************************************************************/
2
3/*
4 * istallion.c -- stallion intelligent multiport serial driver.
5 *
6 * Copyright (C) 1996-1999 Stallion Technologies
7 * Copyright (C) 1994-1996 Greg Ungerer.
8 *
9 * This code is loosely based on the Linux serial driver, written by
10 * Linus Torvalds, Theodore T'so and others.
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 */
26
27/*****************************************************************************/
28
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/module.h>
30#include <linux/slab.h>
31#include <linux/interrupt.h>
32#include <linux/tty.h>
33#include <linux/tty_flip.h>
34#include <linux/serial.h>
35#include <linux/cdk.h>
36#include <linux/comstats.h>
37#include <linux/istallion.h>
38#include <linux/ioport.h>
39#include <linux/delay.h>
40#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/device.h>
42#include <linux/wait.h>
Alan Cox4ac43602006-06-28 04:26:52 -070043#include <linux/eisa.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45#include <asm/io.h>
46#include <asm/uaccess.h>
47
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/pci.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50/*****************************************************************************/
51
52/*
53 * Define different board types. Not all of the following board types
54 * are supported by this driver. But I will use the standard "assigned"
55 * board numbers. Currently supported boards are abbreviated as:
56 * ECP = EasyConnection 8/64, ONB = ONboard, BBY = Brumby and
57 * STAL = Stallion.
58 */
59#define BRD_UNKNOWN 0
60#define BRD_STALLION 1
61#define BRD_BRUMBY4 2
62#define BRD_ONBOARD2 3
63#define BRD_ONBOARD 4
64#define BRD_BRUMBY8 5
65#define BRD_BRUMBY16 6
66#define BRD_ONBOARDE 7
67#define BRD_ONBOARD32 9
68#define BRD_ONBOARD2_32 10
69#define BRD_ONBOARDRS 11
70#define BRD_EASYIO 20
71#define BRD_ECH 21
72#define BRD_ECHMC 22
73#define BRD_ECP 23
74#define BRD_ECPE 24
75#define BRD_ECPMC 25
76#define BRD_ECHPCI 26
77#define BRD_ECH64PCI 27
78#define BRD_EASYIOPCI 28
79#define BRD_ECPPCI 29
80
81#define BRD_BRUMBY BRD_BRUMBY4
82
83/*
84 * Define a configuration structure to hold the board configuration.
85 * Need to set this up in the code (for now) with the boards that are
86 * to be configured into the system. This is what needs to be modified
87 * when adding/removing/modifying boards. Each line entry in the
88 * stli_brdconf[] array is a board. Each line contains io/irq/memory
89 * ranges for that board (as well as what type of board it is).
90 * Some examples:
91 * { BRD_ECP, 0x2a0, 0, 0xcc000, 0, 0 },
92 * This line will configure an EasyConnection 8/64 at io address 2a0,
93 * and shared memory address of cc000. Multiple EasyConnection 8/64
94 * boards can share the same shared memory address space. No interrupt
95 * is required for this board type.
96 * Another example:
97 * { BRD_ECPE, 0x5000, 0, 0x80000000, 0, 0 },
98 * This line will configure an EasyConnection 8/64 EISA in slot 5 and
99 * shared memory address of 0x80000000 (2 GByte). Multiple
100 * EasyConnection 8/64 EISA boards can share the same shared memory
101 * address space. No interrupt is required for this board type.
102 * Another example:
103 * { BRD_ONBOARD, 0x240, 0, 0xd0000, 0, 0 },
104 * This line will configure an ONboard (ISA type) at io address 240,
105 * and shared memory address of d0000. Multiple ONboards can share
106 * the same shared memory address space. No interrupt required.
107 * Another example:
108 * { BRD_BRUMBY4, 0x360, 0, 0xc8000, 0, 0 },
109 * This line will configure a Brumby board (any number of ports!) at
110 * io address 360 and shared memory address of c8000. All Brumby boards
111 * configured into a system must have their own separate io and memory
112 * addresses. No interrupt is required.
113 * Another example:
114 * { BRD_STALLION, 0x330, 0, 0xd0000, 0, 0 },
115 * This line will configure an original Stallion board at io address 330
116 * and shared memory address d0000 (this would only be valid for a "V4.0"
117 * or Rev.O Stallion board). All Stallion boards configured into the
118 * system must have their own separate io and memory addresses. No
119 * interrupt is required.
120 */
121
122typedef struct {
123 int brdtype;
124 int ioaddr1;
125 int ioaddr2;
126 unsigned long memaddr;
127 int irq;
128 int irqtype;
129} stlconf_t;
130
131static stlconf_t stli_brdconf[] = {
132 /*{ BRD_ECP, 0x2a0, 0, 0xcc000, 0, 0 },*/
133};
134
Tobias Klauserfe971072006-01-09 20:54:02 -0800135static int stli_nrbrds = ARRAY_SIZE(stli_brdconf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Alan Cox4ac43602006-06-28 04:26:52 -0700137/* stli_lock must NOT be taken holding brd_lock */
138static spinlock_t stli_lock; /* TTY logic lock */
139static spinlock_t brd_lock; /* Board logic lock */
140
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141/*
142 * There is some experimental EISA board detection code in this driver.
143 * By default it is disabled, but for those that want to try it out,
144 * then set the define below to be 1.
145 */
146#define STLI_EISAPROBE 0
147
148/*****************************************************************************/
149
150/*
151 * Define some important driver characteristics. Device major numbers
152 * allocated as per Linux Device Registry.
153 */
154#ifndef STL_SIOMEMMAJOR
155#define STL_SIOMEMMAJOR 28
156#endif
157#ifndef STL_SERIALMAJOR
158#define STL_SERIALMAJOR 24
159#endif
160#ifndef STL_CALLOUTMAJOR
161#define STL_CALLOUTMAJOR 25
162#endif
163
164/*****************************************************************************/
165
166/*
167 * Define our local driver identity first. Set up stuff to deal with
168 * all the local structures required by a serial tty driver.
169 */
170static char *stli_drvtitle = "Stallion Intelligent Multiport Serial Driver";
171static char *stli_drvname = "istallion";
172static char *stli_drvversion = "5.6.0";
173static char *stli_serialname = "ttyE";
174
175static struct tty_driver *stli_serial;
176
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
178#define STLI_TXBUFSIZE 4096
179
180/*
181 * Use a fast local buffer for cooked characters. Typically a whole
182 * bunch of cooked characters come in for a port, 1 at a time. So we
183 * save those up into a local buffer, then write out the whole lot
184 * with a large memcpy. Just use 1 buffer for all ports, since its
185 * use it is only need for short periods of time by each port.
186 */
187static char *stli_txcookbuf;
188static int stli_txcooksize;
189static int stli_txcookrealsize;
190static struct tty_struct *stli_txcooktty;
191
192/*
193 * Define a local default termios struct. All ports will be created
194 * with this termios initially. Basically all it defines is a raw port
195 * at 9600 baud, 8 data bits, no parity, 1 stop bit.
196 */
197static struct termios stli_deftermios = {
198 .c_cflag = (B9600 | CS8 | CREAD | HUPCL | CLOCAL),
199 .c_cc = INIT_C_CC,
200};
201
202/*
203 * Define global stats structures. Not used often, and can be
204 * re-used for each stats call.
205 */
206static comstats_t stli_comstats;
207static combrd_t stli_brdstats;
208static asystats_t stli_cdkstats;
209static stlibrd_t stli_dummybrd;
210static stliport_t stli_dummyport;
211
212/*****************************************************************************/
213
214static stlibrd_t *stli_brds[STL_MAXBRDS];
215
216static int stli_shared;
217
218/*
219 * Per board state flags. Used with the state field of the board struct.
220 * Not really much here... All we need to do is keep track of whether
221 * the board has been detected, and whether it is actually running a slave
222 * or not.
223 */
224#define BST_FOUND 0x1
225#define BST_STARTED 0x2
226
227/*
228 * Define the set of port state flags. These are marked for internal
229 * state purposes only, usually to do with the state of communications
230 * with the slave. Most of them need to be updated atomically, so always
231 * use the bit setting operations (unless protected by cli/sti).
232 */
233#define ST_INITIALIZING 1
234#define ST_OPENING 2
235#define ST_CLOSING 3
236#define ST_CMDING 4
237#define ST_TXBUSY 5
238#define ST_RXING 6
239#define ST_DOFLUSHRX 7
240#define ST_DOFLUSHTX 8
241#define ST_DOSIGS 9
242#define ST_RXSTOP 10
243#define ST_GETSIGS 11
244
245/*
246 * Define an array of board names as printable strings. Handy for
247 * referencing boards when printing trace and stuff.
248 */
249static char *stli_brdnames[] = {
250 "Unknown",
251 "Stallion",
252 "Brumby",
253 "ONboard-MC",
254 "ONboard",
255 "Brumby",
256 "Brumby",
257 "ONboard-EI",
258 (char *) NULL,
259 "ONboard",
260 "ONboard-MC",
261 "ONboard-MC",
262 (char *) NULL,
263 (char *) NULL,
264 (char *) NULL,
265 (char *) NULL,
266 (char *) NULL,
267 (char *) NULL,
268 (char *) NULL,
269 (char *) NULL,
270 "EasyIO",
271 "EC8/32-AT",
272 "EC8/32-MC",
273 "EC8/64-AT",
274 "EC8/64-EI",
275 "EC8/64-MC",
276 "EC8/32-PCI",
277 "EC8/64-PCI",
278 "EasyIO-PCI",
279 "EC/RA-PCI",
280};
281
282/*****************************************************************************/
283
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284/*
285 * Define some string labels for arguments passed from the module
286 * load line. These allow for easy board definitions, and easy
287 * modification of the io, memory and irq resoucres.
288 */
289
290static char *board0[8];
291static char *board1[8];
292static char *board2[8];
293static char *board3[8];
294
295static char **stli_brdsp[] = {
296 (char **) &board0,
297 (char **) &board1,
298 (char **) &board2,
299 (char **) &board3
300};
301
302/*
303 * Define a set of common board names, and types. This is used to
304 * parse any module arguments.
305 */
306
307typedef struct stlibrdtype {
308 char *name;
309 int type;
310} stlibrdtype_t;
311
312static stlibrdtype_t stli_brdstr[] = {
313 { "stallion", BRD_STALLION },
314 { "1", BRD_STALLION },
315 { "brumby", BRD_BRUMBY },
316 { "brumby4", BRD_BRUMBY },
317 { "brumby/4", BRD_BRUMBY },
318 { "brumby-4", BRD_BRUMBY },
319 { "brumby8", BRD_BRUMBY },
320 { "brumby/8", BRD_BRUMBY },
321 { "brumby-8", BRD_BRUMBY },
322 { "brumby16", BRD_BRUMBY },
323 { "brumby/16", BRD_BRUMBY },
324 { "brumby-16", BRD_BRUMBY },
325 { "2", BRD_BRUMBY },
326 { "onboard2", BRD_ONBOARD2 },
327 { "onboard-2", BRD_ONBOARD2 },
328 { "onboard/2", BRD_ONBOARD2 },
329 { "onboard-mc", BRD_ONBOARD2 },
330 { "onboard/mc", BRD_ONBOARD2 },
331 { "onboard-mca", BRD_ONBOARD2 },
332 { "onboard/mca", BRD_ONBOARD2 },
333 { "3", BRD_ONBOARD2 },
334 { "onboard", BRD_ONBOARD },
335 { "onboardat", BRD_ONBOARD },
336 { "4", BRD_ONBOARD },
337 { "onboarde", BRD_ONBOARDE },
338 { "onboard-e", BRD_ONBOARDE },
339 { "onboard/e", BRD_ONBOARDE },
340 { "onboard-ei", BRD_ONBOARDE },
341 { "onboard/ei", BRD_ONBOARDE },
342 { "7", BRD_ONBOARDE },
343 { "ecp", BRD_ECP },
344 { "ecpat", BRD_ECP },
345 { "ec8/64", BRD_ECP },
346 { "ec8/64-at", BRD_ECP },
347 { "ec8/64-isa", BRD_ECP },
348 { "23", BRD_ECP },
349 { "ecpe", BRD_ECPE },
350 { "ecpei", BRD_ECPE },
351 { "ec8/64-e", BRD_ECPE },
352 { "ec8/64-ei", BRD_ECPE },
353 { "24", BRD_ECPE },
354 { "ecpmc", BRD_ECPMC },
355 { "ec8/64-mc", BRD_ECPMC },
356 { "ec8/64-mca", BRD_ECPMC },
357 { "25", BRD_ECPMC },
358 { "ecppci", BRD_ECPPCI },
359 { "ec/ra", BRD_ECPPCI },
360 { "ec/ra-pc", BRD_ECPPCI },
361 { "ec/ra-pci", BRD_ECPPCI },
362 { "29", BRD_ECPPCI },
363};
364
365/*
366 * Define the module agruments.
367 */
368MODULE_AUTHOR("Greg Ungerer");
369MODULE_DESCRIPTION("Stallion Intelligent Multiport Serial Driver");
370MODULE_LICENSE("GPL");
371
372
Rusty Russell8d3b33f2006-03-25 03:07:05 -0800373module_param_array(board0, charp, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374MODULE_PARM_DESC(board0, "Board 0 config -> name[,ioaddr[,memaddr]");
Rusty Russell8d3b33f2006-03-25 03:07:05 -0800375module_param_array(board1, charp, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376MODULE_PARM_DESC(board1, "Board 1 config -> name[,ioaddr[,memaddr]");
Rusty Russell8d3b33f2006-03-25 03:07:05 -0800377module_param_array(board2, charp, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378MODULE_PARM_DESC(board2, "Board 2 config -> name[,ioaddr[,memaddr]");
Rusty Russell8d3b33f2006-03-25 03:07:05 -0800379module_param_array(board3, charp, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380MODULE_PARM_DESC(board3, "Board 3 config -> name[,ioaddr[,memaddr]");
381
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382/*
383 * Set up a default memory address table for EISA board probing.
384 * The default addresses are all bellow 1Mbyte, which has to be the
385 * case anyway. They should be safe, since we only read values from
386 * them, and interrupts are disabled while we do it. If the higher
387 * memory support is compiled in then we also try probing around
388 * the 1Gb, 2Gb and 3Gb areas as well...
389 */
390static unsigned long stli_eisamemprobeaddrs[] = {
391 0xc0000, 0xd0000, 0xe0000, 0xf0000,
392 0x80000000, 0x80010000, 0x80020000, 0x80030000,
393 0x40000000, 0x40010000, 0x40020000, 0x40030000,
394 0xc0000000, 0xc0010000, 0xc0020000, 0xc0030000,
395 0xff000000, 0xff010000, 0xff020000, 0xff030000,
396};
397
Tobias Klauserfe971072006-01-09 20:54:02 -0800398static int stli_eisamempsize = ARRAY_SIZE(stli_eisamemprobeaddrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
400/*
401 * Define the Stallion PCI vendor and device IDs.
402 */
403#ifdef CONFIG_PCI
404#ifndef PCI_VENDOR_ID_STALLION
405#define PCI_VENDOR_ID_STALLION 0x124d
406#endif
407#ifndef PCI_DEVICE_ID_ECRA
408#define PCI_DEVICE_ID_ECRA 0x0004
409#endif
410
411static struct pci_device_id istallion_pci_tbl[] = {
Alan Cox4ac43602006-06-28 04:26:52 -0700412 { PCI_DEVICE(PCI_VENDOR_ID_STALLION, PCI_DEVICE_ID_ECRA), },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 { 0 }
414};
415MODULE_DEVICE_TABLE(pci, istallion_pci_tbl);
416
417#endif /* CONFIG_PCI */
418
419/*****************************************************************************/
420
421/*
422 * Hardware configuration info for ECP boards. These defines apply
423 * to the directly accessible io ports of the ECP. There is a set of
424 * defines for each ECP board type, ISA, EISA, MCA and PCI.
425 */
426#define ECP_IOSIZE 4
427
428#define ECP_MEMSIZE (128 * 1024)
429#define ECP_PCIMEMSIZE (256 * 1024)
430
431#define ECP_ATPAGESIZE (4 * 1024)
432#define ECP_MCPAGESIZE (4 * 1024)
433#define ECP_EIPAGESIZE (64 * 1024)
434#define ECP_PCIPAGESIZE (64 * 1024)
435
436#define STL_EISAID 0x8c4e
437
438/*
439 * Important defines for the ISA class of ECP board.
440 */
441#define ECP_ATIREG 0
442#define ECP_ATCONFR 1
443#define ECP_ATMEMAR 2
444#define ECP_ATMEMPR 3
445#define ECP_ATSTOP 0x1
446#define ECP_ATINTENAB 0x10
447#define ECP_ATENABLE 0x20
448#define ECP_ATDISABLE 0x00
449#define ECP_ATADDRMASK 0x3f000
450#define ECP_ATADDRSHFT 12
451
452/*
453 * Important defines for the EISA class of ECP board.
454 */
455#define ECP_EIIREG 0
456#define ECP_EIMEMARL 1
457#define ECP_EICONFR 2
458#define ECP_EIMEMARH 3
459#define ECP_EIENABLE 0x1
460#define ECP_EIDISABLE 0x0
461#define ECP_EISTOP 0x4
462#define ECP_EIEDGE 0x00
463#define ECP_EILEVEL 0x80
464#define ECP_EIADDRMASKL 0x00ff0000
465#define ECP_EIADDRSHFTL 16
466#define ECP_EIADDRMASKH 0xff000000
467#define ECP_EIADDRSHFTH 24
468#define ECP_EIBRDENAB 0xc84
469
470#define ECP_EISAID 0x4
471
472/*
473 * Important defines for the Micro-channel class of ECP board.
474 * (It has a lot in common with the ISA boards.)
475 */
476#define ECP_MCIREG 0
477#define ECP_MCCONFR 1
478#define ECP_MCSTOP 0x20
479#define ECP_MCENABLE 0x80
480#define ECP_MCDISABLE 0x00
481
482/*
483 * Important defines for the PCI class of ECP board.
484 * (It has a lot in common with the other ECP boards.)
485 */
486#define ECP_PCIIREG 0
487#define ECP_PCICONFR 1
488#define ECP_PCISTOP 0x01
489
490/*
491 * Hardware configuration info for ONboard and Brumby boards. These
492 * defines apply to the directly accessible io ports of these boards.
493 */
494#define ONB_IOSIZE 16
495#define ONB_MEMSIZE (64 * 1024)
496#define ONB_ATPAGESIZE (64 * 1024)
497#define ONB_MCPAGESIZE (64 * 1024)
498#define ONB_EIMEMSIZE (128 * 1024)
499#define ONB_EIPAGESIZE (64 * 1024)
500
501/*
502 * Important defines for the ISA class of ONboard board.
503 */
504#define ONB_ATIREG 0
505#define ONB_ATMEMAR 1
506#define ONB_ATCONFR 2
507#define ONB_ATSTOP 0x4
508#define ONB_ATENABLE 0x01
509#define ONB_ATDISABLE 0x00
510#define ONB_ATADDRMASK 0xff0000
511#define ONB_ATADDRSHFT 16
512
513#define ONB_MEMENABLO 0
514#define ONB_MEMENABHI 0x02
515
516/*
517 * Important defines for the EISA class of ONboard board.
518 */
519#define ONB_EIIREG 0
520#define ONB_EIMEMARL 1
521#define ONB_EICONFR 2
522#define ONB_EIMEMARH 3
523#define ONB_EIENABLE 0x1
524#define ONB_EIDISABLE 0x0
525#define ONB_EISTOP 0x4
526#define ONB_EIEDGE 0x00
527#define ONB_EILEVEL 0x80
528#define ONB_EIADDRMASKL 0x00ff0000
529#define ONB_EIADDRSHFTL 16
530#define ONB_EIADDRMASKH 0xff000000
531#define ONB_EIADDRSHFTH 24
532#define ONB_EIBRDENAB 0xc84
533
534#define ONB_EISAID 0x1
535
536/*
537 * Important defines for the Brumby boards. They are pretty simple,
538 * there is not much that is programmably configurable.
539 */
540#define BBY_IOSIZE 16
541#define BBY_MEMSIZE (64 * 1024)
542#define BBY_PAGESIZE (16 * 1024)
543
544#define BBY_ATIREG 0
545#define BBY_ATCONFR 1
546#define BBY_ATSTOP 0x4
547
548/*
549 * Important defines for the Stallion boards. They are pretty simple,
550 * there is not much that is programmably configurable.
551 */
552#define STAL_IOSIZE 16
553#define STAL_MEMSIZE (64 * 1024)
554#define STAL_PAGESIZE (64 * 1024)
555
556/*
557 * Define the set of status register values for EasyConnection panels.
558 * The signature will return with the status value for each panel. From
559 * this we can determine what is attached to the board - before we have
560 * actually down loaded any code to it.
561 */
562#define ECH_PNLSTATUS 2
563#define ECH_PNL16PORT 0x20
564#define ECH_PNLIDMASK 0x07
565#define ECH_PNLXPID 0x40
566#define ECH_PNLINTRPEND 0x80
567
568/*
569 * Define some macros to do things to the board. Even those these boards
570 * are somewhat related there is often significantly different ways of
571 * doing some operation on it (like enable, paging, reset, etc). So each
572 * board class has a set of functions which do the commonly required
573 * operations. The macros below basically just call these functions,
574 * generally checking for a NULL function - which means that the board
575 * needs nothing done to it to achieve this operation!
576 */
577#define EBRDINIT(brdp) \
578 if (brdp->init != NULL) \
579 (* brdp->init)(brdp)
580
581#define EBRDENABLE(brdp) \
582 if (brdp->enable != NULL) \
583 (* brdp->enable)(brdp);
584
585#define EBRDDISABLE(brdp) \
586 if (brdp->disable != NULL) \
587 (* brdp->disable)(brdp);
588
589#define EBRDINTR(brdp) \
590 if (brdp->intr != NULL) \
591 (* brdp->intr)(brdp);
592
593#define EBRDRESET(brdp) \
594 if (brdp->reset != NULL) \
595 (* brdp->reset)(brdp);
596
597#define EBRDGETMEMPTR(brdp,offset) \
598 (* brdp->getmemptr)(brdp, offset, __LINE__)
599
600/*
601 * Define the maximal baud rate, and the default baud base for ports.
602 */
603#define STL_MAXBAUD 460800
604#define STL_BAUDBASE 115200
605#define STL_CLOSEDELAY (5 * HZ / 10)
606
607/*****************************************************************************/
608
609/*
610 * Define macros to extract a brd or port number from a minor number.
611 */
612#define MINOR2BRD(min) (((min) & 0xc0) >> 6)
613#define MINOR2PORT(min) ((min) & 0x3f)
614
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615/*****************************************************************************/
616
617/*
618 * Define some handy local macros...
619 */
620#undef MIN
621#define MIN(a,b) (((a) <= (b)) ? (a) : (b))
622
623#undef TOLOWER
624#define TOLOWER(x) ((((x) >= 'A') && ((x) <= 'Z')) ? ((x) + 0x20) : (x))
625
626/*****************************************************************************/
627
628/*
629 * Prototype all functions in this driver!
630 */
631
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632static int stli_parsebrd(stlconf_t *confp, char **argp);
Adrian Bunk672b2712006-06-30 01:55:30 -0700633static int stli_init(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634static int stli_open(struct tty_struct *tty, struct file *filp);
635static void stli_close(struct tty_struct *tty, struct file *filp);
636static int stli_write(struct tty_struct *tty, const unsigned char *buf, int count);
637static void stli_putchar(struct tty_struct *tty, unsigned char ch);
638static void stli_flushchars(struct tty_struct *tty);
639static int stli_writeroom(struct tty_struct *tty);
640static int stli_charsinbuffer(struct tty_struct *tty);
641static int stli_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg);
642static void stli_settermios(struct tty_struct *tty, struct termios *old);
643static void stli_throttle(struct tty_struct *tty);
644static void stli_unthrottle(struct tty_struct *tty);
645static void stli_stop(struct tty_struct *tty);
646static void stli_start(struct tty_struct *tty);
647static void stli_flushbuffer(struct tty_struct *tty);
648static void stli_breakctl(struct tty_struct *tty, int state);
649static void stli_waituntilsent(struct tty_struct *tty, int timeout);
650static void stli_sendxchar(struct tty_struct *tty, char ch);
651static void stli_hangup(struct tty_struct *tty);
652static int stli_portinfo(stlibrd_t *brdp, stliport_t *portp, int portnr, char *pos);
653
654static int stli_brdinit(stlibrd_t *brdp);
655static int stli_startbrd(stlibrd_t *brdp);
656static ssize_t stli_memread(struct file *fp, char __user *buf, size_t count, loff_t *offp);
657static ssize_t stli_memwrite(struct file *fp, const char __user *buf, size_t count, loff_t *offp);
658static int stli_memioctl(struct inode *ip, struct file *fp, unsigned int cmd, unsigned long arg);
Alan Cox4ac43602006-06-28 04:26:52 -0700659static void stli_brdpoll(stlibrd_t *brdp, cdkhdr_t __iomem *hdrp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660static void stli_poll(unsigned long arg);
661static int stli_hostcmd(stlibrd_t *brdp, stliport_t *portp);
662static int stli_initopen(stlibrd_t *brdp, stliport_t *portp);
663static int stli_rawopen(stlibrd_t *brdp, stliport_t *portp, unsigned long arg, int wait);
664static int stli_rawclose(stlibrd_t *brdp, stliport_t *portp, unsigned long arg, int wait);
665static int stli_waitcarrier(stlibrd_t *brdp, stliport_t *portp, struct file *filp);
Al Viro3e577a82006-12-06 18:41:45 +0000666static void stli_dohangup(struct work_struct *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667static int stli_setport(stliport_t *portp);
668static int stli_cmdwait(stlibrd_t *brdp, stliport_t *portp, unsigned long cmd, void *arg, int size, int copyback);
669static void stli_sendcmd(stlibrd_t *brdp, stliport_t *portp, unsigned long cmd, void *arg, int size, int copyback);
Alan Cox4ac43602006-06-28 04:26:52 -0700670static void __stli_sendcmd(stlibrd_t *brdp, stliport_t *portp, unsigned long cmd, void *arg, int size, int copyback);
671static void stli_dodelaycmd(stliport_t *portp, cdkctrl_t __iomem *cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672static void stli_mkasyport(stliport_t *portp, asyport_t *pp, struct termios *tiosp);
673static void stli_mkasysigs(asysigs_t *sp, int dtr, int rts);
674static long stli_mktiocm(unsigned long sigvalue);
675static void stli_read(stlibrd_t *brdp, stliport_t *portp);
676static int stli_getserial(stliport_t *portp, struct serial_struct __user *sp);
677static int stli_setserial(stliport_t *portp, struct serial_struct __user *sp);
678static int stli_getbrdstats(combrd_t __user *bp);
679static int stli_getportstats(stliport_t *portp, comstats_t __user *cp);
680static int stli_portcmdstats(stliport_t *portp);
681static int stli_clrportstats(stliport_t *portp, comstats_t __user *cp);
682static int stli_getportstruct(stliport_t __user *arg);
683static int stli_getbrdstruct(stlibrd_t __user *arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684static stlibrd_t *stli_allocbrd(void);
685
686static void stli_ecpinit(stlibrd_t *brdp);
687static void stli_ecpenable(stlibrd_t *brdp);
688static void stli_ecpdisable(stlibrd_t *brdp);
Al Viro29756fa2006-10-10 22:47:27 +0100689static void __iomem *stli_ecpgetmemptr(stlibrd_t *brdp, unsigned long offset, int line);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690static void stli_ecpreset(stlibrd_t *brdp);
691static void stli_ecpintr(stlibrd_t *brdp);
692static void stli_ecpeiinit(stlibrd_t *brdp);
693static void stli_ecpeienable(stlibrd_t *brdp);
694static void stli_ecpeidisable(stlibrd_t *brdp);
Al Viro29756fa2006-10-10 22:47:27 +0100695static void __iomem *stli_ecpeigetmemptr(stlibrd_t *brdp, unsigned long offset, int line);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696static void stli_ecpeireset(stlibrd_t *brdp);
697static void stli_ecpmcenable(stlibrd_t *brdp);
698static void stli_ecpmcdisable(stlibrd_t *brdp);
Al Viro29756fa2006-10-10 22:47:27 +0100699static void __iomem *stli_ecpmcgetmemptr(stlibrd_t *brdp, unsigned long offset, int line);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700static void stli_ecpmcreset(stlibrd_t *brdp);
701static void stli_ecppciinit(stlibrd_t *brdp);
Al Viro29756fa2006-10-10 22:47:27 +0100702static void __iomem *stli_ecppcigetmemptr(stlibrd_t *brdp, unsigned long offset, int line);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703static void stli_ecppcireset(stlibrd_t *brdp);
704
705static void stli_onbinit(stlibrd_t *brdp);
706static void stli_onbenable(stlibrd_t *brdp);
707static void stli_onbdisable(stlibrd_t *brdp);
Al Viro29756fa2006-10-10 22:47:27 +0100708static void __iomem *stli_onbgetmemptr(stlibrd_t *brdp, unsigned long offset, int line);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709static void stli_onbreset(stlibrd_t *brdp);
710static void stli_onbeinit(stlibrd_t *brdp);
711static void stli_onbeenable(stlibrd_t *brdp);
712static void stli_onbedisable(stlibrd_t *brdp);
Al Viro29756fa2006-10-10 22:47:27 +0100713static void __iomem *stli_onbegetmemptr(stlibrd_t *brdp, unsigned long offset, int line);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714static void stli_onbereset(stlibrd_t *brdp);
715static void stli_bbyinit(stlibrd_t *brdp);
Al Viro29756fa2006-10-10 22:47:27 +0100716static void __iomem *stli_bbygetmemptr(stlibrd_t *brdp, unsigned long offset, int line);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717static void stli_bbyreset(stlibrd_t *brdp);
718static void stli_stalinit(stlibrd_t *brdp);
Al Viro29756fa2006-10-10 22:47:27 +0100719static void __iomem *stli_stalgetmemptr(stlibrd_t *brdp, unsigned long offset, int line);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720static void stli_stalreset(stlibrd_t *brdp);
721
722static stliport_t *stli_getport(int brdnr, int panelnr, int portnr);
723
724static int stli_initecp(stlibrd_t *brdp);
725static int stli_initonb(stlibrd_t *brdp);
726static int stli_eisamemprobe(stlibrd_t *brdp);
727static int stli_initports(stlibrd_t *brdp);
728
729#ifdef CONFIG_PCI
730static int stli_initpcibrd(int brdtype, struct pci_dev *devp);
731#endif
732
733/*****************************************************************************/
734
735/*
736 * Define the driver info for a user level shared memory device. This
737 * device will work sort of like the /dev/kmem device - except that it
738 * will give access to the shared memory on the Stallion intelligent
739 * board. This is also a very useful debugging tool.
740 */
Arjan van de Ven62322d22006-07-03 00:24:21 -0700741static const struct file_operations stli_fsiomem = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 .owner = THIS_MODULE,
743 .read = stli_memread,
744 .write = stli_memwrite,
745 .ioctl = stli_memioctl,
746};
747
748/*****************************************************************************/
749
750/*
751 * Define a timer_list entry for our poll routine. The slave board
752 * is polled every so often to see if anything needs doing. This is
753 * much cheaper on host cpu than using interrupts. It turns out to
754 * not increase character latency by much either...
755 */
Ingo Molnar8d06afa2005-09-09 13:10:40 -0700756static DEFINE_TIMER(stli_timerlist, stli_poll, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
758static int stli_timeron;
759
760/*
761 * Define the calculation for the timeout routine.
762 */
763#define STLI_TIMEOUT (jiffies + 1)
764
765/*****************************************************************************/
766
gregkh@suse.deca8eca62005-03-23 09:53:09 -0800767static struct class *istallion_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769/*
770 * Loadable module initialization stuff.
771 */
772
773static int __init istallion_module_init(void)
774{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 stli_init();
Alan Cox4ac43602006-06-28 04:26:52 -0700776 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777}
778
779/*****************************************************************************/
780
781static void __exit istallion_module_exit(void)
782{
783 stlibrd_t *brdp;
784 stliport_t *portp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 int i, j;
786
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 printk(KERN_INFO "Unloading %s: version %s\n", stli_drvtitle,
788 stli_drvversion);
789
Alan Cox4ac43602006-06-28 04:26:52 -0700790 /*
791 * Free up all allocated resources used by the ports. This includes
792 * memory and interrupts.
793 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 if (stli_timeron) {
795 stli_timeron = 0;
Alan Cox4ac43602006-06-28 04:26:52 -0700796 del_timer_sync(&stli_timerlist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 }
798
799 i = tty_unregister_driver(stli_serial);
800 if (i) {
801 printk("STALLION: failed to un-register tty driver, "
802 "errno=%d\n", -i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 return;
804 }
805 put_tty_driver(stli_serial);
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -0700806 for (i = 0; i < 4; i++)
gregkh@suse.deca8eca62005-03-23 09:53:09 -0800807 class_device_destroy(istallion_class, MKDEV(STL_SIOMEMMAJOR, i));
gregkh@suse.deca8eca62005-03-23 09:53:09 -0800808 class_destroy(istallion_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 if ((i = unregister_chrdev(STL_SIOMEMMAJOR, "staliomem")))
810 printk("STALLION: failed to un-register serial memory device, "
811 "errno=%d\n", -i);
Jesper Juhl735d5662005-11-07 01:01:29 -0800812
Jesper Juhl735d5662005-11-07 01:01:29 -0800813 kfree(stli_txcookbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
815 for (i = 0; (i < stli_nrbrds); i++) {
Alan Cox4ac43602006-06-28 04:26:52 -0700816 if ((brdp = stli_brds[i]) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 continue;
818 for (j = 0; (j < STL_MAXPORTS); j++) {
819 portp = brdp->ports[j];
Alan Cox4ac43602006-06-28 04:26:52 -0700820 if (portp != NULL) {
821 if (portp->tty != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 tty_hangup(portp->tty);
823 kfree(portp);
824 }
825 }
826
827 iounmap(brdp->membase);
828 if (brdp->iosize > 0)
829 release_region(brdp->iobase, brdp->iosize);
830 kfree(brdp);
Alan Cox4ac43602006-06-28 04:26:52 -0700831 stli_brds[i] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833}
834
835module_init(istallion_module_init);
836module_exit(istallion_module_exit);
837
838/*****************************************************************************/
839
840/*
841 * Check for any arguments passed in on the module load command line.
842 */
843
844static void stli_argbrds(void)
845{
Alan Cox4ac43602006-06-28 04:26:52 -0700846 stlconf_t conf;
847 stlibrd_t *brdp;
848 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
Tobias Klauserfe971072006-01-09 20:54:02 -0800850 for (i = stli_nrbrds; i < ARRAY_SIZE(stli_brdsp); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 memset(&conf, 0, sizeof(conf));
852 if (stli_parsebrd(&conf, stli_brdsp[i]) == 0)
853 continue;
Alan Cox4ac43602006-06-28 04:26:52 -0700854 if ((brdp = stli_allocbrd()) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 continue;
856 stli_nrbrds = i + 1;
857 brdp->brdnr = i;
858 brdp->brdtype = conf.brdtype;
859 brdp->iobase = conf.ioaddr1;
860 brdp->memaddr = conf.memaddr;
861 stli_brdinit(brdp);
862 }
863}
864
865/*****************************************************************************/
866
867/*
868 * Convert an ascii string number into an unsigned long.
869 */
870
871static unsigned long stli_atol(char *str)
872{
Alan Cox4ac43602006-06-28 04:26:52 -0700873 unsigned long val;
874 int base, c;
875 char *sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
877 val = 0;
878 sp = str;
879 if ((*sp == '0') && (*(sp+1) == 'x')) {
880 base = 16;
881 sp += 2;
882 } else if (*sp == '0') {
883 base = 8;
884 sp++;
885 } else {
886 base = 10;
887 }
888
889 for (; (*sp != 0); sp++) {
890 c = (*sp > '9') ? (TOLOWER(*sp) - 'a' + 10) : (*sp - '0');
891 if ((c < 0) || (c >= base)) {
892 printk("STALLION: invalid argument %s\n", str);
893 val = 0;
894 break;
895 }
896 val = (val * base) + c;
897 }
898 return(val);
899}
900
901/*****************************************************************************/
902
903/*
904 * Parse the supplied argument string, into the board conf struct.
905 */
906
907static int stli_parsebrd(stlconf_t *confp, char **argp)
908{
Alan Cox4ac43602006-06-28 04:26:52 -0700909 char *sp;
910 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
Alan Cox4ac43602006-06-28 04:26:52 -0700912 if (argp[0] == NULL || *argp[0] == 0)
913 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
915 for (sp = argp[0], i = 0; ((*sp != 0) && (i < 25)); sp++, i++)
916 *sp = TOLOWER(*sp);
917
Tobias Klauserfe971072006-01-09 20:54:02 -0800918 for (i = 0; i < ARRAY_SIZE(stli_brdstr); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 if (strcmp(stli_brdstr[i].name, argp[0]) == 0)
920 break;
921 }
Tobias Klauserfe971072006-01-09 20:54:02 -0800922 if (i == ARRAY_SIZE(stli_brdstr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 printk("STALLION: unknown board name, %s?\n", argp[0]);
Tobias Klauserfe971072006-01-09 20:54:02 -0800924 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 }
926
927 confp->brdtype = stli_brdstr[i].type;
Alan Cox4ac43602006-06-28 04:26:52 -0700928 if (argp[1] != NULL && *argp[1] != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 confp->ioaddr1 = stli_atol(argp[1]);
Alan Cox4ac43602006-06-28 04:26:52 -0700930 if (argp[2] != NULL && *argp[2] != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 confp->memaddr = stli_atol(argp[2]);
932 return(1);
933}
934
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935/*****************************************************************************/
936
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937static int stli_open(struct tty_struct *tty, struct file *filp)
938{
Alan Cox4ac43602006-06-28 04:26:52 -0700939 stlibrd_t *brdp;
940 stliport_t *portp;
941 unsigned int minordev;
942 int brdnr, portnr, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
944 minordev = tty->index;
945 brdnr = MINOR2BRD(minordev);
946 if (brdnr >= stli_nrbrds)
Alan Cox4ac43602006-06-28 04:26:52 -0700947 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 brdp = stli_brds[brdnr];
Alan Cox4ac43602006-06-28 04:26:52 -0700949 if (brdp == NULL)
950 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 if ((brdp->state & BST_STARTED) == 0)
Alan Cox4ac43602006-06-28 04:26:52 -0700952 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 portnr = MINOR2PORT(minordev);
954 if ((portnr < 0) || (portnr > brdp->nrports))
Alan Cox4ac43602006-06-28 04:26:52 -0700955 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956
957 portp = brdp->ports[portnr];
Alan Cox4ac43602006-06-28 04:26:52 -0700958 if (portp == NULL)
959 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 if (portp->devnr < 1)
Alan Cox4ac43602006-06-28 04:26:52 -0700961 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962
963
964/*
965 * Check if this port is in the middle of closing. If so then wait
966 * until it is closed then return error status based on flag settings.
967 * The sleep here does not need interrupt protection since the wakeup
968 * for it is done with the same context.
969 */
970 if (portp->flags & ASYNC_CLOSING) {
971 interruptible_sleep_on(&portp->close_wait);
972 if (portp->flags & ASYNC_HUP_NOTIFY)
Alan Cox4ac43602006-06-28 04:26:52 -0700973 return -EAGAIN;
974 return -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 }
976
977/*
978 * On the first open of the device setup the port hardware, and
979 * initialize the per port data structure. Since initializing the port
980 * requires several commands to the board we will need to wait for any
981 * other open that is already initializing the port.
982 */
983 portp->tty = tty;
984 tty->driver_data = portp;
985 portp->refcount++;
986
987 wait_event_interruptible(portp->raw_wait,
988 !test_bit(ST_INITIALIZING, &portp->state));
989 if (signal_pending(current))
Alan Cox4ac43602006-06-28 04:26:52 -0700990 return -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
992 if ((portp->flags & ASYNC_INITIALIZED) == 0) {
993 set_bit(ST_INITIALIZING, &portp->state);
994 if ((rc = stli_initopen(brdp, portp)) >= 0) {
995 portp->flags |= ASYNC_INITIALIZED;
996 clear_bit(TTY_IO_ERROR, &tty->flags);
997 }
998 clear_bit(ST_INITIALIZING, &portp->state);
999 wake_up_interruptible(&portp->raw_wait);
1000 if (rc < 0)
Alan Cox4ac43602006-06-28 04:26:52 -07001001 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 }
1003
1004/*
1005 * Check if this port is in the middle of closing. If so then wait
1006 * until it is closed then return error status, based on flag settings.
1007 * The sleep here does not need interrupt protection since the wakeup
1008 * for it is done with the same context.
1009 */
1010 if (portp->flags & ASYNC_CLOSING) {
1011 interruptible_sleep_on(&portp->close_wait);
1012 if (portp->flags & ASYNC_HUP_NOTIFY)
Alan Cox4ac43602006-06-28 04:26:52 -07001013 return -EAGAIN;
1014 return -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 }
1016
1017/*
1018 * Based on type of open being done check if it can overlap with any
1019 * previous opens still in effect. If we are a normal serial device
1020 * then also we might have to wait for carrier.
1021 */
1022 if (!(filp->f_flags & O_NONBLOCK)) {
1023 if ((rc = stli_waitcarrier(brdp, portp, filp)) != 0)
Alan Cox4ac43602006-06-28 04:26:52 -07001024 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 }
1026 portp->flags |= ASYNC_NORMAL_ACTIVE;
Alan Cox4ac43602006-06-28 04:26:52 -07001027 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028}
1029
1030/*****************************************************************************/
1031
1032static void stli_close(struct tty_struct *tty, struct file *filp)
1033{
Alan Cox4ac43602006-06-28 04:26:52 -07001034 stlibrd_t *brdp;
1035 stliport_t *portp;
1036 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037
1038 portp = tty->driver_data;
Alan Cox4ac43602006-06-28 04:26:52 -07001039 if (portp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 return;
1041
Alan Cox4ac43602006-06-28 04:26:52 -07001042 spin_lock_irqsave(&stli_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 if (tty_hung_up_p(filp)) {
Alan Cox4ac43602006-06-28 04:26:52 -07001044 spin_unlock_irqrestore(&stli_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 return;
1046 }
1047 if ((tty->count == 1) && (portp->refcount != 1))
1048 portp->refcount = 1;
1049 if (portp->refcount-- > 1) {
Alan Cox4ac43602006-06-28 04:26:52 -07001050 spin_unlock_irqrestore(&stli_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 return;
1052 }
1053
1054 portp->flags |= ASYNC_CLOSING;
1055
1056/*
1057 * May want to wait for data to drain before closing. The BUSY flag
1058 * keeps track of whether we are still transmitting or not. It is
1059 * updated by messages from the slave - indicating when all chars
1060 * really have drained.
1061 */
1062 if (tty == stli_txcooktty)
1063 stli_flushchars(tty);
1064 tty->closing = 1;
Alan Cox4ac43602006-06-28 04:26:52 -07001065 spin_unlock_irqrestore(&stli_lock, flags);
1066
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 if (portp->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1068 tty_wait_until_sent(tty, portp->closing_wait);
1069
1070 portp->flags &= ~ASYNC_INITIALIZED;
1071 brdp = stli_brds[portp->brdnr];
1072 stli_rawclose(brdp, portp, 0, 0);
1073 if (tty->termios->c_cflag & HUPCL) {
1074 stli_mkasysigs(&portp->asig, 0, 0);
1075 if (test_bit(ST_CMDING, &portp->state))
1076 set_bit(ST_DOSIGS, &portp->state);
1077 else
1078 stli_sendcmd(brdp, portp, A_SETSIGNALS, &portp->asig,
1079 sizeof(asysigs_t), 0);
1080 }
1081 clear_bit(ST_TXBUSY, &portp->state);
1082 clear_bit(ST_RXSTOP, &portp->state);
1083 set_bit(TTY_IO_ERROR, &tty->flags);
1084 if (tty->ldisc.flush_buffer)
1085 (tty->ldisc.flush_buffer)(tty);
1086 set_bit(ST_DOFLUSHRX, &portp->state);
1087 stli_flushbuffer(tty);
1088
1089 tty->closing = 0;
Alan Cox4ac43602006-06-28 04:26:52 -07001090 portp->tty = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
1092 if (portp->openwaitcnt) {
1093 if (portp->close_delay)
1094 msleep_interruptible(jiffies_to_msecs(portp->close_delay));
1095 wake_up_interruptible(&portp->open_wait);
1096 }
1097
1098 portp->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
1099 wake_up_interruptible(&portp->close_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100}
1101
1102/*****************************************************************************/
1103
1104/*
1105 * Carry out first open operations on a port. This involves a number of
1106 * commands to be sent to the slave. We need to open the port, set the
1107 * notification events, set the initial port settings, get and set the
1108 * initial signal values. We sleep and wait in between each one. But
1109 * this still all happens pretty quickly.
1110 */
1111
1112static int stli_initopen(stlibrd_t *brdp, stliport_t *portp)
1113{
Alan Cox4ac43602006-06-28 04:26:52 -07001114 struct tty_struct *tty;
1115 asynotify_t nt;
1116 asyport_t aport;
1117 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118
1119 if ((rc = stli_rawopen(brdp, portp, 0, 1)) < 0)
Alan Cox4ac43602006-06-28 04:26:52 -07001120 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
1122 memset(&nt, 0, sizeof(asynotify_t));
1123 nt.data = (DT_TXLOW | DT_TXEMPTY | DT_RXBUSY | DT_RXBREAK);
1124 nt.signal = SG_DCD;
1125 if ((rc = stli_cmdwait(brdp, portp, A_SETNOTIFY, &nt,
1126 sizeof(asynotify_t), 0)) < 0)
Alan Cox4ac43602006-06-28 04:26:52 -07001127 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
1129 tty = portp->tty;
Alan Cox4ac43602006-06-28 04:26:52 -07001130 if (tty == NULL)
1131 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 stli_mkasyport(portp, &aport, tty->termios);
1133 if ((rc = stli_cmdwait(brdp, portp, A_SETPORT, &aport,
1134 sizeof(asyport_t), 0)) < 0)
Alan Cox4ac43602006-06-28 04:26:52 -07001135 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136
1137 set_bit(ST_GETSIGS, &portp->state);
1138 if ((rc = stli_cmdwait(brdp, portp, A_GETSIGNALS, &portp->asig,
1139 sizeof(asysigs_t), 1)) < 0)
Alan Cox4ac43602006-06-28 04:26:52 -07001140 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 if (test_and_clear_bit(ST_GETSIGS, &portp->state))
1142 portp->sigs = stli_mktiocm(portp->asig.sigvalue);
1143 stli_mkasysigs(&portp->asig, 1, 1);
1144 if ((rc = stli_cmdwait(brdp, portp, A_SETSIGNALS, &portp->asig,
1145 sizeof(asysigs_t), 0)) < 0)
Alan Cox4ac43602006-06-28 04:26:52 -07001146 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147
Alan Cox4ac43602006-06-28 04:26:52 -07001148 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149}
1150
1151/*****************************************************************************/
1152
1153/*
1154 * Send an open message to the slave. This will sleep waiting for the
1155 * acknowledgement, so must have user context. We need to co-ordinate
1156 * with close events here, since we don't want open and close events
1157 * to overlap.
1158 */
1159
1160static int stli_rawopen(stlibrd_t *brdp, stliport_t *portp, unsigned long arg, int wait)
1161{
Alan Cox4ac43602006-06-28 04:26:52 -07001162 cdkhdr_t __iomem *hdrp;
1163 cdkctrl_t __iomem *cp;
1164 unsigned char __iomem *bits;
1165 unsigned long flags;
1166 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
1168/*
1169 * Send a message to the slave to open this port.
1170 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171
1172/*
1173 * Slave is already closing this port. This can happen if a hangup
1174 * occurs on this port. So we must wait until it is complete. The
1175 * order of opens and closes may not be preserved across shared
1176 * memory, so we must wait until it is complete.
1177 */
1178 wait_event_interruptible(portp->raw_wait,
1179 !test_bit(ST_CLOSING, &portp->state));
1180 if (signal_pending(current)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 return -ERESTARTSYS;
1182 }
1183
1184/*
1185 * Everything is ready now, so write the open message into shared
1186 * memory. Once the message is in set the service bits to say that
1187 * this port wants service.
1188 */
Alan Cox4ac43602006-06-28 04:26:52 -07001189 spin_lock_irqsave(&brd_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 EBRDENABLE(brdp);
Alan Cox4ac43602006-06-28 04:26:52 -07001191 cp = &((cdkasy_t __iomem *) EBRDGETMEMPTR(brdp, portp->addr))->ctrl;
1192 writel(arg, &cp->openarg);
1193 writeb(1, &cp->open);
1194 hdrp = (cdkhdr_t __iomem *) EBRDGETMEMPTR(brdp, CDK_CDKADDR);
1195 bits = ((unsigned char __iomem *) hdrp) + brdp->slaveoffset +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 portp->portidx;
Alan Cox4ac43602006-06-28 04:26:52 -07001197 writeb(readb(bits) | portp->portbit, bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 EBRDDISABLE(brdp);
1199
1200 if (wait == 0) {
Alan Cox4ac43602006-06-28 04:26:52 -07001201 spin_unlock_irqrestore(&brd_lock, flags);
1202 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 }
1204
1205/*
1206 * Slave is in action, so now we must wait for the open acknowledgment
1207 * to come back.
1208 */
1209 rc = 0;
1210 set_bit(ST_OPENING, &portp->state);
Alan Cox4ac43602006-06-28 04:26:52 -07001211 spin_unlock_irqrestore(&brd_lock, flags);
1212
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 wait_event_interruptible(portp->raw_wait,
1214 !test_bit(ST_OPENING, &portp->state));
1215 if (signal_pending(current))
1216 rc = -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
1218 if ((rc == 0) && (portp->rc != 0))
1219 rc = -EIO;
Alan Cox4ac43602006-06-28 04:26:52 -07001220 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221}
1222
1223/*****************************************************************************/
1224
1225/*
1226 * Send a close message to the slave. Normally this will sleep waiting
1227 * for the acknowledgement, but if wait parameter is 0 it will not. If
1228 * wait is true then must have user context (to sleep).
1229 */
1230
1231static int stli_rawclose(stlibrd_t *brdp, stliport_t *portp, unsigned long arg, int wait)
1232{
Alan Cox4ac43602006-06-28 04:26:52 -07001233 cdkhdr_t __iomem *hdrp;
1234 cdkctrl_t __iomem *cp;
1235 unsigned char __iomem *bits;
1236 unsigned long flags;
1237 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238
1239/*
1240 * Slave is already closing this port. This can happen if a hangup
1241 * occurs on this port.
1242 */
1243 if (wait) {
1244 wait_event_interruptible(portp->raw_wait,
1245 !test_bit(ST_CLOSING, &portp->state));
1246 if (signal_pending(current)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 return -ERESTARTSYS;
1248 }
1249 }
1250
1251/*
1252 * Write the close command into shared memory.
1253 */
Alan Cox4ac43602006-06-28 04:26:52 -07001254 spin_lock_irqsave(&brd_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 EBRDENABLE(brdp);
Alan Cox4ac43602006-06-28 04:26:52 -07001256 cp = &((cdkasy_t __iomem *) EBRDGETMEMPTR(brdp, portp->addr))->ctrl;
1257 writel(arg, &cp->closearg);
1258 writeb(1, &cp->close);
1259 hdrp = (cdkhdr_t __iomem *) EBRDGETMEMPTR(brdp, CDK_CDKADDR);
1260 bits = ((unsigned char __iomem *) hdrp) + brdp->slaveoffset +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 portp->portidx;
Alan Cox4ac43602006-06-28 04:26:52 -07001262 writeb(readb(bits) |portp->portbit, bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 EBRDDISABLE(brdp);
1264
1265 set_bit(ST_CLOSING, &portp->state);
Alan Cox4ac43602006-06-28 04:26:52 -07001266 spin_unlock_irqrestore(&brd_lock, flags);
1267
1268 if (wait == 0)
1269 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270
1271/*
1272 * Slave is in action, so now we must wait for the open acknowledgment
1273 * to come back.
1274 */
1275 rc = 0;
1276 wait_event_interruptible(portp->raw_wait,
1277 !test_bit(ST_CLOSING, &portp->state));
1278 if (signal_pending(current))
1279 rc = -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280
1281 if ((rc == 0) && (portp->rc != 0))
1282 rc = -EIO;
Alan Cox4ac43602006-06-28 04:26:52 -07001283 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284}
1285
1286/*****************************************************************************/
1287
1288/*
1289 * Send a command to the slave and wait for the response. This must
1290 * have user context (it sleeps). This routine is generic in that it
1291 * can send any type of command. Its purpose is to wait for that command
1292 * to complete (as opposed to initiating the command then returning).
1293 */
1294
1295static int stli_cmdwait(stlibrd_t *brdp, stliport_t *portp, unsigned long cmd, void *arg, int size, int copyback)
1296{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 wait_event_interruptible(portp->raw_wait,
1298 !test_bit(ST_CMDING, &portp->state));
Alan Cox4ac43602006-06-28 04:26:52 -07001299 if (signal_pending(current))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 return -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
1302 stli_sendcmd(brdp, portp, cmd, arg, size, copyback);
1303
1304 wait_event_interruptible(portp->raw_wait,
1305 !test_bit(ST_CMDING, &portp->state));
Alan Cox4ac43602006-06-28 04:26:52 -07001306 if (signal_pending(current))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 return -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308
1309 if (portp->rc != 0)
Alan Cox4ac43602006-06-28 04:26:52 -07001310 return -EIO;
1311 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312}
1313
1314/*****************************************************************************/
1315
1316/*
1317 * Send the termios settings for this port to the slave. This sleeps
1318 * waiting for the command to complete - so must have user context.
1319 */
1320
1321static int stli_setport(stliport_t *portp)
1322{
Alan Cox4ac43602006-06-28 04:26:52 -07001323 stlibrd_t *brdp;
1324 asyport_t aport;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325
Alan Cox4ac43602006-06-28 04:26:52 -07001326 if (portp == NULL)
1327 return -ENODEV;
1328 if (portp->tty == NULL)
1329 return -ENODEV;
1330 if (portp->brdnr < 0 && portp->brdnr >= stli_nrbrds)
1331 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 brdp = stli_brds[portp->brdnr];
Alan Cox4ac43602006-06-28 04:26:52 -07001333 if (brdp == NULL)
1334 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335
1336 stli_mkasyport(portp, &aport, portp->tty->termios);
1337 return(stli_cmdwait(brdp, portp, A_SETPORT, &aport, sizeof(asyport_t), 0));
1338}
1339
1340/*****************************************************************************/
1341
1342/*
1343 * Possibly need to wait for carrier (DCD signal) to come high. Say
1344 * maybe because if we are clocal then we don't need to wait...
1345 */
1346
1347static int stli_waitcarrier(stlibrd_t *brdp, stliport_t *portp, struct file *filp)
1348{
Alan Cox4ac43602006-06-28 04:26:52 -07001349 unsigned long flags;
1350 int rc, doclocal;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351
1352 rc = 0;
1353 doclocal = 0;
1354
1355 if (portp->tty->termios->c_cflag & CLOCAL)
1356 doclocal++;
1357
Alan Cox4ac43602006-06-28 04:26:52 -07001358 spin_lock_irqsave(&stli_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 portp->openwaitcnt++;
1360 if (! tty_hung_up_p(filp))
1361 portp->refcount--;
Alan Cox4ac43602006-06-28 04:26:52 -07001362 spin_unlock_irqrestore(&stli_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
1364 for (;;) {
1365 stli_mkasysigs(&portp->asig, 1, 1);
1366 if ((rc = stli_cmdwait(brdp, portp, A_SETSIGNALS,
1367 &portp->asig, sizeof(asysigs_t), 0)) < 0)
1368 break;
1369 if (tty_hung_up_p(filp) ||
1370 ((portp->flags & ASYNC_INITIALIZED) == 0)) {
1371 if (portp->flags & ASYNC_HUP_NOTIFY)
1372 rc = -EBUSY;
1373 else
1374 rc = -ERESTARTSYS;
1375 break;
1376 }
1377 if (((portp->flags & ASYNC_CLOSING) == 0) &&
1378 (doclocal || (portp->sigs & TIOCM_CD))) {
1379 break;
1380 }
1381 if (signal_pending(current)) {
1382 rc = -ERESTARTSYS;
1383 break;
1384 }
1385 interruptible_sleep_on(&portp->open_wait);
1386 }
1387
Alan Cox4ac43602006-06-28 04:26:52 -07001388 spin_lock_irqsave(&stli_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 if (! tty_hung_up_p(filp))
1390 portp->refcount++;
1391 portp->openwaitcnt--;
Alan Cox4ac43602006-06-28 04:26:52 -07001392 spin_unlock_irqrestore(&stli_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393
Alan Cox4ac43602006-06-28 04:26:52 -07001394 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395}
1396
1397/*****************************************************************************/
1398
1399/*
1400 * Write routine. Take the data and put it in the shared memory ring
1401 * queue. If port is not already sending chars then need to mark the
1402 * service bits for this port.
1403 */
1404
1405static int stli_write(struct tty_struct *tty, const unsigned char *buf, int count)
1406{
Alan Cox4ac43602006-06-28 04:26:52 -07001407 cdkasy_t __iomem *ap;
1408 cdkhdr_t __iomem *hdrp;
1409 unsigned char __iomem *bits;
1410 unsigned char __iomem *shbuf;
1411 unsigned char *chbuf;
1412 stliport_t *portp;
1413 stlibrd_t *brdp;
1414 unsigned int len, stlen, head, tail, size;
1415 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 if (tty == stli_txcooktty)
1418 stli_flushchars(tty);
1419 portp = tty->driver_data;
Alan Cox4ac43602006-06-28 04:26:52 -07001420 if (portp == NULL)
1421 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 if ((portp->brdnr < 0) || (portp->brdnr >= stli_nrbrds))
Alan Cox4ac43602006-06-28 04:26:52 -07001423 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 brdp = stli_brds[portp->brdnr];
Alan Cox4ac43602006-06-28 04:26:52 -07001425 if (brdp == NULL)
1426 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 chbuf = (unsigned char *) buf;
1428
1429/*
1430 * All data is now local, shove as much as possible into shared memory.
1431 */
Alan Cox4ac43602006-06-28 04:26:52 -07001432 spin_lock_irqsave(&brd_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 EBRDENABLE(brdp);
Alan Cox4ac43602006-06-28 04:26:52 -07001434 ap = (cdkasy_t __iomem *) EBRDGETMEMPTR(brdp, portp->addr);
1435 head = (unsigned int) readw(&ap->txq.head);
1436 tail = (unsigned int) readw(&ap->txq.tail);
1437 if (tail != ((unsigned int) readw(&ap->txq.tail)))
1438 tail = (unsigned int) readw(&ap->txq.tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 size = portp->txsize;
1440 if (head >= tail) {
1441 len = size - (head - tail) - 1;
1442 stlen = size - head;
1443 } else {
1444 len = tail - head - 1;
1445 stlen = len;
1446 }
1447
1448 len = MIN(len, count);
1449 count = 0;
Alan Cox4ac43602006-06-28 04:26:52 -07001450 shbuf = (char __iomem *) EBRDGETMEMPTR(brdp, portp->txoffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451
1452 while (len > 0) {
1453 stlen = MIN(len, stlen);
Alan Cox4ac43602006-06-28 04:26:52 -07001454 memcpy_toio(shbuf + head, chbuf, stlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 chbuf += stlen;
1456 len -= stlen;
1457 count += stlen;
1458 head += stlen;
1459 if (head >= size) {
1460 head = 0;
1461 stlen = tail;
1462 }
1463 }
1464
Alan Cox4ac43602006-06-28 04:26:52 -07001465 ap = (cdkasy_t __iomem *) EBRDGETMEMPTR(brdp, portp->addr);
1466 writew(head, &ap->txq.head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 if (test_bit(ST_TXBUSY, &portp->state)) {
Alan Cox4ac43602006-06-28 04:26:52 -07001468 if (readl(&ap->changed.data) & DT_TXEMPTY)
1469 writel(readl(&ap->changed.data) & ~DT_TXEMPTY, &ap->changed.data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 }
Alan Cox4ac43602006-06-28 04:26:52 -07001471 hdrp = (cdkhdr_t __iomem *) EBRDGETMEMPTR(brdp, CDK_CDKADDR);
1472 bits = ((unsigned char __iomem *) hdrp) + brdp->slaveoffset +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 portp->portidx;
Alan Cox4ac43602006-06-28 04:26:52 -07001474 writeb(readb(bits) | portp->portbit, bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 set_bit(ST_TXBUSY, &portp->state);
1476 EBRDDISABLE(brdp);
Alan Cox4ac43602006-06-28 04:26:52 -07001477 spin_unlock_irqrestore(&brd_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478
1479 return(count);
1480}
1481
1482/*****************************************************************************/
1483
1484/*
1485 * Output a single character. We put it into a temporary local buffer
1486 * (for speed) then write out that buffer when the flushchars routine
1487 * is called. There is a safety catch here so that if some other port
1488 * writes chars before the current buffer has been, then we write them
1489 * first them do the new ports.
1490 */
1491
1492static void stli_putchar(struct tty_struct *tty, unsigned char ch)
1493{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 if (tty != stli_txcooktty) {
Alan Cox4ac43602006-06-28 04:26:52 -07001495 if (stli_txcooktty != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 stli_flushchars(stli_txcooktty);
1497 stli_txcooktty = tty;
1498 }
1499
1500 stli_txcookbuf[stli_txcooksize++] = ch;
1501}
1502
1503/*****************************************************************************/
1504
1505/*
1506 * Transfer characters from the local TX cooking buffer to the board.
1507 * We sort of ignore the tty that gets passed in here. We rely on the
1508 * info stored with the TX cook buffer to tell us which port to flush
1509 * the data on. In any case we clean out the TX cook buffer, for re-use
1510 * by someone else.
1511 */
1512
1513static void stli_flushchars(struct tty_struct *tty)
1514{
Alan Cox4ac43602006-06-28 04:26:52 -07001515 cdkhdr_t __iomem *hdrp;
1516 unsigned char __iomem *bits;
1517 cdkasy_t __iomem *ap;
1518 struct tty_struct *cooktty;
1519 stliport_t *portp;
1520 stlibrd_t *brdp;
1521 unsigned int len, stlen, head, tail, size, count, cooksize;
1522 unsigned char *buf;
1523 unsigned char __iomem *shbuf;
1524 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525
1526 cooksize = stli_txcooksize;
1527 cooktty = stli_txcooktty;
1528 stli_txcooksize = 0;
1529 stli_txcookrealsize = 0;
Alan Cox4ac43602006-06-28 04:26:52 -07001530 stli_txcooktty = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531
Alan Cox4ac43602006-06-28 04:26:52 -07001532 if (tty == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 return;
Alan Cox4ac43602006-06-28 04:26:52 -07001534 if (cooktty == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 return;
1536 if (tty != cooktty)
1537 tty = cooktty;
1538 if (cooksize == 0)
1539 return;
1540
1541 portp = tty->driver_data;
Alan Cox4ac43602006-06-28 04:26:52 -07001542 if (portp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 return;
1544 if ((portp->brdnr < 0) || (portp->brdnr >= stli_nrbrds))
1545 return;
1546 brdp = stli_brds[portp->brdnr];
Alan Cox4ac43602006-06-28 04:26:52 -07001547 if (brdp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 return;
1549
Alan Cox4ac43602006-06-28 04:26:52 -07001550 spin_lock_irqsave(&brd_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 EBRDENABLE(brdp);
1552
Alan Cox4ac43602006-06-28 04:26:52 -07001553 ap = (cdkasy_t __iomem *) EBRDGETMEMPTR(brdp, portp->addr);
1554 head = (unsigned int) readw(&ap->txq.head);
1555 tail = (unsigned int) readw(&ap->txq.tail);
1556 if (tail != ((unsigned int) readw(&ap->txq.tail)))
1557 tail = (unsigned int) readw(&ap->txq.tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 size = portp->txsize;
1559 if (head >= tail) {
1560 len = size - (head - tail) - 1;
1561 stlen = size - head;
1562 } else {
1563 len = tail - head - 1;
1564 stlen = len;
1565 }
1566
1567 len = MIN(len, cooksize);
1568 count = 0;
Al Viro29756fa2006-10-10 22:47:27 +01001569 shbuf = EBRDGETMEMPTR(brdp, portp->txoffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 buf = stli_txcookbuf;
1571
1572 while (len > 0) {
1573 stlen = MIN(len, stlen);
Alan Cox4ac43602006-06-28 04:26:52 -07001574 memcpy_toio(shbuf + head, buf, stlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 buf += stlen;
1576 len -= stlen;
1577 count += stlen;
1578 head += stlen;
1579 if (head >= size) {
1580 head = 0;
1581 stlen = tail;
1582 }
1583 }
1584
Alan Cox4ac43602006-06-28 04:26:52 -07001585 ap = (cdkasy_t __iomem *) EBRDGETMEMPTR(brdp, portp->addr);
1586 writew(head, &ap->txq.head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587
1588 if (test_bit(ST_TXBUSY, &portp->state)) {
Alan Cox4ac43602006-06-28 04:26:52 -07001589 if (readl(&ap->changed.data) & DT_TXEMPTY)
1590 writel(readl(&ap->changed.data) & ~DT_TXEMPTY, &ap->changed.data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 }
Alan Cox4ac43602006-06-28 04:26:52 -07001592 hdrp = (cdkhdr_t __iomem *) EBRDGETMEMPTR(brdp, CDK_CDKADDR);
1593 bits = ((unsigned char __iomem *) hdrp) + brdp->slaveoffset +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 portp->portidx;
Alan Cox4ac43602006-06-28 04:26:52 -07001595 writeb(readb(bits) | portp->portbit, bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 set_bit(ST_TXBUSY, &portp->state);
1597
1598 EBRDDISABLE(brdp);
Alan Cox4ac43602006-06-28 04:26:52 -07001599 spin_unlock_irqrestore(&brd_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600}
1601
1602/*****************************************************************************/
1603
1604static int stli_writeroom(struct tty_struct *tty)
1605{
Alan Cox4ac43602006-06-28 04:26:52 -07001606 cdkasyrq_t __iomem *rp;
1607 stliport_t *portp;
1608 stlibrd_t *brdp;
1609 unsigned int head, tail, len;
1610 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 if (tty == stli_txcooktty) {
1613 if (stli_txcookrealsize != 0) {
1614 len = stli_txcookrealsize - stli_txcooksize;
Alan Cox4ac43602006-06-28 04:26:52 -07001615 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 }
1617 }
1618
1619 portp = tty->driver_data;
Alan Cox4ac43602006-06-28 04:26:52 -07001620 if (portp == NULL)
1621 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 if ((portp->brdnr < 0) || (portp->brdnr >= stli_nrbrds))
Alan Cox4ac43602006-06-28 04:26:52 -07001623 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 brdp = stli_brds[portp->brdnr];
Alan Cox4ac43602006-06-28 04:26:52 -07001625 if (brdp == NULL)
1626 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627
Alan Cox4ac43602006-06-28 04:26:52 -07001628 spin_lock_irqsave(&brd_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 EBRDENABLE(brdp);
Alan Cox4ac43602006-06-28 04:26:52 -07001630 rp = &((cdkasy_t __iomem *) EBRDGETMEMPTR(brdp, portp->addr))->txq;
1631 head = (unsigned int) readw(&rp->head);
1632 tail = (unsigned int) readw(&rp->tail);
1633 if (tail != ((unsigned int) readw(&rp->tail)))
1634 tail = (unsigned int) readw(&rp->tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 len = (head >= tail) ? (portp->txsize - (head - tail)) : (tail - head);
1636 len--;
1637 EBRDDISABLE(brdp);
Alan Cox4ac43602006-06-28 04:26:52 -07001638 spin_unlock_irqrestore(&brd_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639
1640 if (tty == stli_txcooktty) {
1641 stli_txcookrealsize = len;
1642 len -= stli_txcooksize;
1643 }
Alan Cox4ac43602006-06-28 04:26:52 -07001644 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645}
1646
1647/*****************************************************************************/
1648
1649/*
1650 * Return the number of characters in the transmit buffer. Normally we
1651 * will return the number of chars in the shared memory ring queue.
1652 * We need to kludge around the case where the shared memory buffer is
1653 * empty but not all characters have drained yet, for this case just
1654 * return that there is 1 character in the buffer!
1655 */
1656
1657static int stli_charsinbuffer(struct tty_struct *tty)
1658{
Alan Cox4ac43602006-06-28 04:26:52 -07001659 cdkasyrq_t __iomem *rp;
1660 stliport_t *portp;
1661 stlibrd_t *brdp;
1662 unsigned int head, tail, len;
1663 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 if (tty == stli_txcooktty)
1666 stli_flushchars(tty);
1667 portp = tty->driver_data;
Alan Cox4ac43602006-06-28 04:26:52 -07001668 if (portp == NULL)
1669 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 if ((portp->brdnr < 0) || (portp->brdnr >= stli_nrbrds))
Alan Cox4ac43602006-06-28 04:26:52 -07001671 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 brdp = stli_brds[portp->brdnr];
Alan Cox4ac43602006-06-28 04:26:52 -07001673 if (brdp == NULL)
1674 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675
Alan Cox4ac43602006-06-28 04:26:52 -07001676 spin_lock_irqsave(&brd_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 EBRDENABLE(brdp);
Alan Cox4ac43602006-06-28 04:26:52 -07001678 rp = &((cdkasy_t __iomem *) EBRDGETMEMPTR(brdp, portp->addr))->txq;
1679 head = (unsigned int) readw(&rp->head);
1680 tail = (unsigned int) readw(&rp->tail);
1681 if (tail != ((unsigned int) readw(&rp->tail)))
1682 tail = (unsigned int) readw(&rp->tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 len = (head >= tail) ? (head - tail) : (portp->txsize - (tail - head));
1684 if ((len == 0) && test_bit(ST_TXBUSY, &portp->state))
1685 len = 1;
1686 EBRDDISABLE(brdp);
Alan Cox4ac43602006-06-28 04:26:52 -07001687 spin_unlock_irqrestore(&brd_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688
Alan Cox4ac43602006-06-28 04:26:52 -07001689 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690}
1691
1692/*****************************************************************************/
1693
1694/*
1695 * Generate the serial struct info.
1696 */
1697
1698static int stli_getserial(stliport_t *portp, struct serial_struct __user *sp)
1699{
Alan Cox4ac43602006-06-28 04:26:52 -07001700 struct serial_struct sio;
1701 stlibrd_t *brdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702
1703 memset(&sio, 0, sizeof(struct serial_struct));
1704 sio.type = PORT_UNKNOWN;
1705 sio.line = portp->portnr;
1706 sio.irq = 0;
1707 sio.flags = portp->flags;
1708 sio.baud_base = portp->baud_base;
1709 sio.close_delay = portp->close_delay;
1710 sio.closing_wait = portp->closing_wait;
1711 sio.custom_divisor = portp->custom_divisor;
1712 sio.xmit_fifo_size = 0;
1713 sio.hub6 = 0;
1714
1715 brdp = stli_brds[portp->brdnr];
Alan Cox4ac43602006-06-28 04:26:52 -07001716 if (brdp != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 sio.port = brdp->iobase;
1718
1719 return copy_to_user(sp, &sio, sizeof(struct serial_struct)) ?
1720 -EFAULT : 0;
1721}
1722
1723/*****************************************************************************/
1724
1725/*
1726 * Set port according to the serial struct info.
1727 * At this point we do not do any auto-configure stuff, so we will
1728 * just quietly ignore any requests to change irq, etc.
1729 */
1730
1731static int stli_setserial(stliport_t *portp, struct serial_struct __user *sp)
1732{
Alan Cox4ac43602006-06-28 04:26:52 -07001733 struct serial_struct sio;
1734 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735
1736 if (copy_from_user(&sio, sp, sizeof(struct serial_struct)))
1737 return -EFAULT;
1738 if (!capable(CAP_SYS_ADMIN)) {
1739 if ((sio.baud_base != portp->baud_base) ||
1740 (sio.close_delay != portp->close_delay) ||
1741 ((sio.flags & ~ASYNC_USR_MASK) !=
1742 (portp->flags & ~ASYNC_USR_MASK)))
Alan Cox4ac43602006-06-28 04:26:52 -07001743 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 }
1745
1746 portp->flags = (portp->flags & ~ASYNC_USR_MASK) |
1747 (sio.flags & ASYNC_USR_MASK);
1748 portp->baud_base = sio.baud_base;
1749 portp->close_delay = sio.close_delay;
1750 portp->closing_wait = sio.closing_wait;
1751 portp->custom_divisor = sio.custom_divisor;
1752
1753 if ((rc = stli_setport(portp)) < 0)
Alan Cox4ac43602006-06-28 04:26:52 -07001754 return rc;
1755 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756}
1757
1758/*****************************************************************************/
1759
1760static int stli_tiocmget(struct tty_struct *tty, struct file *file)
1761{
1762 stliport_t *portp = tty->driver_data;
1763 stlibrd_t *brdp;
1764 int rc;
1765
Alan Cox4ac43602006-06-28 04:26:52 -07001766 if (portp == NULL)
1767 return -ENODEV;
1768 if (portp->brdnr < 0 || portp->brdnr >= stli_nrbrds)
1769 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 brdp = stli_brds[portp->brdnr];
Alan Cox4ac43602006-06-28 04:26:52 -07001771 if (brdp == NULL)
1772 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 if (tty->flags & (1 << TTY_IO_ERROR))
Alan Cox4ac43602006-06-28 04:26:52 -07001774 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775
1776 if ((rc = stli_cmdwait(brdp, portp, A_GETSIGNALS,
1777 &portp->asig, sizeof(asysigs_t), 1)) < 0)
Alan Cox4ac43602006-06-28 04:26:52 -07001778 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779
1780 return stli_mktiocm(portp->asig.sigvalue);
1781}
1782
1783static int stli_tiocmset(struct tty_struct *tty, struct file *file,
1784 unsigned int set, unsigned int clear)
1785{
1786 stliport_t *portp = tty->driver_data;
1787 stlibrd_t *brdp;
1788 int rts = -1, dtr = -1;
1789
Alan Cox4ac43602006-06-28 04:26:52 -07001790 if (portp == NULL)
1791 return -ENODEV;
1792 if (portp->brdnr < 0 || portp->brdnr >= stli_nrbrds)
1793 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 brdp = stli_brds[portp->brdnr];
Alan Cox4ac43602006-06-28 04:26:52 -07001795 if (brdp == NULL)
1796 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 if (tty->flags & (1 << TTY_IO_ERROR))
Alan Cox4ac43602006-06-28 04:26:52 -07001798 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799
1800 if (set & TIOCM_RTS)
1801 rts = 1;
1802 if (set & TIOCM_DTR)
1803 dtr = 1;
1804 if (clear & TIOCM_RTS)
1805 rts = 0;
1806 if (clear & TIOCM_DTR)
1807 dtr = 0;
1808
1809 stli_mkasysigs(&portp->asig, dtr, rts);
1810
1811 return stli_cmdwait(brdp, portp, A_SETSIGNALS, &portp->asig,
1812 sizeof(asysigs_t), 0);
1813}
1814
1815static int stli_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg)
1816{
Alan Cox4ac43602006-06-28 04:26:52 -07001817 stliport_t *portp;
1818 stlibrd_t *brdp;
1819 unsigned int ival;
1820 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 void __user *argp = (void __user *)arg;
1822
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 portp = tty->driver_data;
Alan Cox4ac43602006-06-28 04:26:52 -07001824 if (portp == NULL)
1825 return -ENODEV;
1826 if (portp->brdnr < 0 || portp->brdnr >= stli_nrbrds)
1827 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 brdp = stli_brds[portp->brdnr];
Alan Cox4ac43602006-06-28 04:26:52 -07001829 if (brdp == NULL)
1830 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831
1832 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1833 (cmd != COM_GETPORTSTATS) && (cmd != COM_CLRPORTSTATS)) {
1834 if (tty->flags & (1 << TTY_IO_ERROR))
Alan Cox4ac43602006-06-28 04:26:52 -07001835 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 }
1837
1838 rc = 0;
1839
1840 switch (cmd) {
1841 case TIOCGSOFTCAR:
1842 rc = put_user(((tty->termios->c_cflag & CLOCAL) ? 1 : 0),
1843 (unsigned __user *) arg);
1844 break;
1845 case TIOCSSOFTCAR:
1846 if ((rc = get_user(ival, (unsigned __user *) arg)) == 0)
1847 tty->termios->c_cflag =
1848 (tty->termios->c_cflag & ~CLOCAL) |
1849 (ival ? CLOCAL : 0);
1850 break;
1851 case TIOCGSERIAL:
1852 rc = stli_getserial(portp, argp);
1853 break;
1854 case TIOCSSERIAL:
1855 rc = stli_setserial(portp, argp);
1856 break;
1857 case STL_GETPFLAG:
1858 rc = put_user(portp->pflag, (unsigned __user *)argp);
1859 break;
1860 case STL_SETPFLAG:
1861 if ((rc = get_user(portp->pflag, (unsigned __user *)argp)) == 0)
1862 stli_setport(portp);
1863 break;
1864 case COM_GETPORTSTATS:
1865 rc = stli_getportstats(portp, argp);
1866 break;
1867 case COM_CLRPORTSTATS:
1868 rc = stli_clrportstats(portp, argp);
1869 break;
1870 case TIOCSERCONFIG:
1871 case TIOCSERGWILD:
1872 case TIOCSERSWILD:
1873 case TIOCSERGETLSR:
1874 case TIOCSERGSTRUCT:
1875 case TIOCSERGETMULTI:
1876 case TIOCSERSETMULTI:
1877 default:
1878 rc = -ENOIOCTLCMD;
1879 break;
1880 }
1881
Alan Cox4ac43602006-06-28 04:26:52 -07001882 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883}
1884
1885/*****************************************************************************/
1886
1887/*
1888 * This routine assumes that we have user context and can sleep.
1889 * Looks like it is true for the current ttys implementation..!!
1890 */
1891
1892static void stli_settermios(struct tty_struct *tty, struct termios *old)
1893{
Alan Cox4ac43602006-06-28 04:26:52 -07001894 stliport_t *portp;
1895 stlibrd_t *brdp;
1896 struct termios *tiosp;
1897 asyport_t aport;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898
Alan Cox4ac43602006-06-28 04:26:52 -07001899 if (tty == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 return;
1901 portp = tty->driver_data;
Alan Cox4ac43602006-06-28 04:26:52 -07001902 if (portp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 return;
Alan Cox4ac43602006-06-28 04:26:52 -07001904 if (portp->brdnr < 0 || portp->brdnr >= stli_nrbrds)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 return;
1906 brdp = stli_brds[portp->brdnr];
Alan Cox4ac43602006-06-28 04:26:52 -07001907 if (brdp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 return;
1909
1910 tiosp = tty->termios;
1911 if ((tiosp->c_cflag == old->c_cflag) &&
1912 (tiosp->c_iflag == old->c_iflag))
1913 return;
1914
1915 stli_mkasyport(portp, &aport, tiosp);
1916 stli_cmdwait(brdp, portp, A_SETPORT, &aport, sizeof(asyport_t), 0);
1917 stli_mkasysigs(&portp->asig, ((tiosp->c_cflag & CBAUD) ? 1 : 0), -1);
1918 stli_cmdwait(brdp, portp, A_SETSIGNALS, &portp->asig,
1919 sizeof(asysigs_t), 0);
1920 if ((old->c_cflag & CRTSCTS) && ((tiosp->c_cflag & CRTSCTS) == 0))
1921 tty->hw_stopped = 0;
1922 if (((old->c_cflag & CLOCAL) == 0) && (tiosp->c_cflag & CLOCAL))
1923 wake_up_interruptible(&portp->open_wait);
1924}
1925
1926/*****************************************************************************/
1927
1928/*
1929 * Attempt to flow control who ever is sending us data. We won't really
1930 * do any flow control action here. We can't directly, and even if we
1931 * wanted to we would have to send a command to the slave. The slave
1932 * knows how to flow control, and will do so when its buffers reach its
1933 * internal high water marks. So what we will do is set a local state
1934 * bit that will stop us sending any RX data up from the poll routine
1935 * (which is the place where RX data from the slave is handled).
1936 */
1937
1938static void stli_throttle(struct tty_struct *tty)
1939{
Alan Cox4ac43602006-06-28 04:26:52 -07001940 stliport_t *portp = tty->driver_data;
1941 if (portp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 set_bit(ST_RXSTOP, &portp->state);
1944}
1945
1946/*****************************************************************************/
1947
1948/*
1949 * Unflow control the device sending us data... That means that all
1950 * we have to do is clear the RXSTOP state bit. The next poll call
1951 * will then be able to pass the RX data back up.
1952 */
1953
1954static void stli_unthrottle(struct tty_struct *tty)
1955{
Alan Cox4ac43602006-06-28 04:26:52 -07001956 stliport_t *portp = tty->driver_data;
1957 if (portp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 clear_bit(ST_RXSTOP, &portp->state);
1960}
1961
1962/*****************************************************************************/
1963
1964/*
Alan Cox4ac43602006-06-28 04:26:52 -07001965 * Stop the transmitter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 */
1967
1968static void stli_stop(struct tty_struct *tty)
1969{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970}
1971
1972/*****************************************************************************/
1973
1974/*
Alan Cox4ac43602006-06-28 04:26:52 -07001975 * Start the transmitter again.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 */
1977
1978static void stli_start(struct tty_struct *tty)
1979{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980}
1981
1982/*****************************************************************************/
1983
1984/*
1985 * Scheduler called hang up routine. This is called from the scheduler,
1986 * not direct from the driver "poll" routine. We can't call it there
1987 * since the real local hangup code will enable/disable the board and
1988 * other things that we can't do while handling the poll. Much easier
1989 * to deal with it some time later (don't really care when, hangups
1990 * aren't that time critical).
1991 */
1992
Al Viro3e577a82006-12-06 18:41:45 +00001993static void stli_dohangup(struct work_struct *ugly_api)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994{
Al Viro3e577a82006-12-06 18:41:45 +00001995 stliport_t *portp = container_of(ugly_api, stliport_t, tqhangup);
Alan Cox4ac43602006-06-28 04:26:52 -07001996 if (portp->tty != NULL) {
1997 tty_hangup(portp->tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 }
1999}
2000
2001/*****************************************************************************/
2002
2003/*
2004 * Hangup this port. This is pretty much like closing the port, only
2005 * a little more brutal. No waiting for data to drain. Shutdown the
2006 * port and maybe drop signals. This is rather tricky really. We want
2007 * to close the port as well.
2008 */
2009
2010static void stli_hangup(struct tty_struct *tty)
2011{
Alan Cox4ac43602006-06-28 04:26:52 -07002012 stliport_t *portp;
2013 stlibrd_t *brdp;
2014 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 portp = tty->driver_data;
Alan Cox4ac43602006-06-28 04:26:52 -07002017 if (portp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 return;
Alan Cox4ac43602006-06-28 04:26:52 -07002019 if (portp->brdnr < 0 || portp->brdnr >= stli_nrbrds)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 return;
2021 brdp = stli_brds[portp->brdnr];
Alan Cox4ac43602006-06-28 04:26:52 -07002022 if (brdp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 return;
2024
2025 portp->flags &= ~ASYNC_INITIALIZED;
2026
Alan Cox4ac43602006-06-28 04:26:52 -07002027 if (!test_bit(ST_CLOSING, &portp->state))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 stli_rawclose(brdp, portp, 0, 0);
Alan Cox4ac43602006-06-28 04:26:52 -07002029
2030 spin_lock_irqsave(&stli_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 if (tty->termios->c_cflag & HUPCL) {
2032 stli_mkasysigs(&portp->asig, 0, 0);
2033 if (test_bit(ST_CMDING, &portp->state)) {
2034 set_bit(ST_DOSIGS, &portp->state);
2035 set_bit(ST_DOFLUSHTX, &portp->state);
2036 set_bit(ST_DOFLUSHRX, &portp->state);
2037 } else {
2038 stli_sendcmd(brdp, portp, A_SETSIGNALSF,
2039 &portp->asig, sizeof(asysigs_t), 0);
2040 }
2041 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042
2043 clear_bit(ST_TXBUSY, &portp->state);
2044 clear_bit(ST_RXSTOP, &portp->state);
2045 set_bit(TTY_IO_ERROR, &tty->flags);
Alan Cox4ac43602006-06-28 04:26:52 -07002046 portp->tty = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 portp->flags &= ~ASYNC_NORMAL_ACTIVE;
2048 portp->refcount = 0;
Alan Cox4ac43602006-06-28 04:26:52 -07002049 spin_unlock_irqrestore(&stli_lock, flags);
2050
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 wake_up_interruptible(&portp->open_wait);
2052}
2053
2054/*****************************************************************************/
2055
2056/*
2057 * Flush characters from the lower buffer. We may not have user context
2058 * so we cannot sleep waiting for it to complete. Also we need to check
2059 * if there is chars for this port in the TX cook buffer, and flush them
2060 * as well.
2061 */
2062
2063static void stli_flushbuffer(struct tty_struct *tty)
2064{
Alan Cox4ac43602006-06-28 04:26:52 -07002065 stliport_t *portp;
2066 stlibrd_t *brdp;
2067 unsigned long ftype, flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 portp = tty->driver_data;
Alan Cox4ac43602006-06-28 04:26:52 -07002070 if (portp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 return;
Alan Cox4ac43602006-06-28 04:26:52 -07002072 if (portp->brdnr < 0 || portp->brdnr >= stli_nrbrds)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 return;
2074 brdp = stli_brds[portp->brdnr];
Alan Cox4ac43602006-06-28 04:26:52 -07002075 if (brdp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 return;
2077
Alan Cox4ac43602006-06-28 04:26:52 -07002078 spin_lock_irqsave(&brd_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 if (tty == stli_txcooktty) {
Alan Cox4ac43602006-06-28 04:26:52 -07002080 stli_txcooktty = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 stli_txcooksize = 0;
2082 stli_txcookrealsize = 0;
2083 }
2084 if (test_bit(ST_CMDING, &portp->state)) {
2085 set_bit(ST_DOFLUSHTX, &portp->state);
2086 } else {
2087 ftype = FLUSHTX;
2088 if (test_bit(ST_DOFLUSHRX, &portp->state)) {
2089 ftype |= FLUSHRX;
2090 clear_bit(ST_DOFLUSHRX, &portp->state);
2091 }
Alan Cox4ac43602006-06-28 04:26:52 -07002092 __stli_sendcmd(brdp, portp, A_FLUSH, &ftype, sizeof(u32), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 }
Alan Cox4ac43602006-06-28 04:26:52 -07002094 spin_unlock_irqrestore(&brd_lock, flags);
2095 tty_wakeup(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096}
2097
2098/*****************************************************************************/
2099
2100static void stli_breakctl(struct tty_struct *tty, int state)
2101{
2102 stlibrd_t *brdp;
2103 stliport_t *portp;
2104 long arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 portp = tty->driver_data;
Alan Cox4ac43602006-06-28 04:26:52 -07002107 if (portp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 return;
Alan Cox4ac43602006-06-28 04:26:52 -07002109 if (portp->brdnr < 0 || portp->brdnr >= stli_nrbrds)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 return;
2111 brdp = stli_brds[portp->brdnr];
Alan Cox4ac43602006-06-28 04:26:52 -07002112 if (brdp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 return;
2114
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 arg = (state == -1) ? BREAKON : BREAKOFF;
2116 stli_cmdwait(brdp, portp, A_BREAK, &arg, sizeof(long), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117}
2118
2119/*****************************************************************************/
2120
2121static void stli_waituntilsent(struct tty_struct *tty, int timeout)
2122{
Alan Cox4ac43602006-06-28 04:26:52 -07002123 stliport_t *portp;
2124 unsigned long tend;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125
Alan Cox4ac43602006-06-28 04:26:52 -07002126 if (tty == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 return;
2128 portp = tty->driver_data;
Alan Cox4ac43602006-06-28 04:26:52 -07002129 if (portp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 return;
2131
2132 if (timeout == 0)
2133 timeout = HZ;
2134 tend = jiffies + timeout;
2135
2136 while (test_bit(ST_TXBUSY, &portp->state)) {
2137 if (signal_pending(current))
2138 break;
2139 msleep_interruptible(20);
2140 if (time_after_eq(jiffies, tend))
2141 break;
2142 }
2143}
2144
2145/*****************************************************************************/
2146
2147static void stli_sendxchar(struct tty_struct *tty, char ch)
2148{
2149 stlibrd_t *brdp;
2150 stliport_t *portp;
2151 asyctrl_t actrl;
2152
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 portp = tty->driver_data;
Alan Cox4ac43602006-06-28 04:26:52 -07002154 if (portp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 return;
Alan Cox4ac43602006-06-28 04:26:52 -07002156 if (portp->brdnr < 0 || portp->brdnr >= stli_nrbrds)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 return;
2158 brdp = stli_brds[portp->brdnr];
Alan Cox4ac43602006-06-28 04:26:52 -07002159 if (brdp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 return;
2161
2162 memset(&actrl, 0, sizeof(asyctrl_t));
2163 if (ch == STOP_CHAR(tty)) {
2164 actrl.rxctrl = CT_STOPFLOW;
2165 } else if (ch == START_CHAR(tty)) {
2166 actrl.rxctrl = CT_STARTFLOW;
2167 } else {
2168 actrl.txctrl = CT_SENDCHR;
2169 actrl.tximdch = ch;
2170 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 stli_cmdwait(brdp, portp, A_PORTCTRL, &actrl, sizeof(asyctrl_t), 0);
2172}
2173
2174/*****************************************************************************/
2175
2176#define MAXLINE 80
2177
2178/*
2179 * Format info for a specified port. The line is deliberately limited
2180 * to 80 characters. (If it is too long it will be truncated, if too
2181 * short then padded with spaces).
2182 */
2183
2184static int stli_portinfo(stlibrd_t *brdp, stliport_t *portp, int portnr, char *pos)
2185{
Alan Cox4ac43602006-06-28 04:26:52 -07002186 char *sp, *uart;
2187 int rc, cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188
2189 rc = stli_portcmdstats(portp);
2190
2191 uart = "UNKNOWN";
2192 if (brdp->state & BST_STARTED) {
2193 switch (stli_comstats.hwid) {
Alan Cox4ac43602006-06-28 04:26:52 -07002194 case 0: uart = "2681"; break;
2195 case 1: uart = "SC26198"; break;
2196 default:uart = "CD1400"; break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 }
2198 }
2199
2200 sp = pos;
2201 sp += sprintf(sp, "%d: uart:%s ", portnr, uart);
2202
2203 if ((brdp->state & BST_STARTED) && (rc >= 0)) {
2204 sp += sprintf(sp, "tx:%d rx:%d", (int) stli_comstats.txtotal,
2205 (int) stli_comstats.rxtotal);
2206
2207 if (stli_comstats.rxframing)
2208 sp += sprintf(sp, " fe:%d",
2209 (int) stli_comstats.rxframing);
2210 if (stli_comstats.rxparity)
2211 sp += sprintf(sp, " pe:%d",
2212 (int) stli_comstats.rxparity);
2213 if (stli_comstats.rxbreaks)
2214 sp += sprintf(sp, " brk:%d",
2215 (int) stli_comstats.rxbreaks);
2216 if (stli_comstats.rxoverrun)
2217 sp += sprintf(sp, " oe:%d",
2218 (int) stli_comstats.rxoverrun);
2219
2220 cnt = sprintf(sp, "%s%s%s%s%s ",
2221 (stli_comstats.signals & TIOCM_RTS) ? "|RTS" : "",
2222 (stli_comstats.signals & TIOCM_CTS) ? "|CTS" : "",
2223 (stli_comstats.signals & TIOCM_DTR) ? "|DTR" : "",
2224 (stli_comstats.signals & TIOCM_CD) ? "|DCD" : "",
2225 (stli_comstats.signals & TIOCM_DSR) ? "|DSR" : "");
2226 *sp = ' ';
2227 sp += cnt;
2228 }
2229
2230 for (cnt = (sp - pos); (cnt < (MAXLINE - 1)); cnt++)
2231 *sp++ = ' ';
2232 if (cnt >= MAXLINE)
2233 pos[(MAXLINE - 2)] = '+';
2234 pos[(MAXLINE - 1)] = '\n';
2235
2236 return(MAXLINE);
2237}
2238
2239/*****************************************************************************/
2240
2241/*
2242 * Port info, read from the /proc file system.
2243 */
2244
2245static int stli_readproc(char *page, char **start, off_t off, int count, int *eof, void *data)
2246{
Alan Cox4ac43602006-06-28 04:26:52 -07002247 stlibrd_t *brdp;
2248 stliport_t *portp;
2249 int brdnr, portnr, totalport;
2250 int curoff, maxoff;
2251 char *pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252
2253 pos = page;
2254 totalport = 0;
2255 curoff = 0;
2256
2257 if (off == 0) {
2258 pos += sprintf(pos, "%s: version %s", stli_drvtitle,
2259 stli_drvversion);
2260 while (pos < (page + MAXLINE - 1))
2261 *pos++ = ' ';
2262 *pos++ = '\n';
2263 }
2264 curoff = MAXLINE;
2265
2266/*
2267 * We scan through for each board, panel and port. The offset is
2268 * calculated on the fly, and irrelevant ports are skipped.
2269 */
2270 for (brdnr = 0; (brdnr < stli_nrbrds); brdnr++) {
2271 brdp = stli_brds[brdnr];
Alan Cox4ac43602006-06-28 04:26:52 -07002272 if (brdp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 continue;
2274 if (brdp->state == 0)
2275 continue;
2276
2277 maxoff = curoff + (brdp->nrports * MAXLINE);
2278 if (off >= maxoff) {
2279 curoff = maxoff;
2280 continue;
2281 }
2282
2283 totalport = brdnr * STL_MAXPORTS;
2284 for (portnr = 0; (portnr < brdp->nrports); portnr++,
2285 totalport++) {
2286 portp = brdp->ports[portnr];
Alan Cox4ac43602006-06-28 04:26:52 -07002287 if (portp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 continue;
2289 if (off >= (curoff += MAXLINE))
2290 continue;
2291 if ((pos - page + MAXLINE) > count)
2292 goto stli_readdone;
2293 pos += stli_portinfo(brdp, portp, totalport, pos);
2294 }
2295 }
2296
2297 *eof = 1;
2298
2299stli_readdone:
2300 *start = page;
2301 return(pos - page);
2302}
2303
2304/*****************************************************************************/
2305
2306/*
2307 * Generic send command routine. This will send a message to the slave,
2308 * of the specified type with the specified argument. Must be very
2309 * careful of data that will be copied out from shared memory -
2310 * containing command results. The command completion is all done from
2311 * a poll routine that does not have user context. Therefore you cannot
2312 * copy back directly into user space, or to the kernel stack of a
2313 * process. This routine does not sleep, so can be called from anywhere.
Alan Cox4ac43602006-06-28 04:26:52 -07002314 *
2315 * The caller must hold the brd_lock (see also stli_sendcmd the usual
2316 * entry point)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 */
2318
Alan Cox4ac43602006-06-28 04:26:52 -07002319static void __stli_sendcmd(stlibrd_t *brdp, stliport_t *portp, unsigned long cmd, void *arg, int size, int copyback)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320{
Alan Cox4ac43602006-06-28 04:26:52 -07002321 cdkhdr_t __iomem *hdrp;
2322 cdkctrl_t __iomem *cp;
2323 unsigned char __iomem *bits;
2324 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325
Alan Cox4ac43602006-06-28 04:26:52 -07002326 spin_lock_irqsave(&brd_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327
2328 if (test_bit(ST_CMDING, &portp->state)) {
2329 printk(KERN_ERR "STALLION: command already busy, cmd=%x!\n",
2330 (int) cmd);
Alan Cox4ac43602006-06-28 04:26:52 -07002331 spin_unlock_irqrestore(&brd_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 return;
2333 }
2334
2335 EBRDENABLE(brdp);
Alan Cox4ac43602006-06-28 04:26:52 -07002336 cp = &((cdkasy_t __iomem *) EBRDGETMEMPTR(brdp, portp->addr))->ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 if (size > 0) {
Alan Cox4ac43602006-06-28 04:26:52 -07002338 memcpy_toio((void __iomem *) &(cp->args[0]), arg, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 if (copyback) {
2340 portp->argp = arg;
2341 portp->argsize = size;
2342 }
2343 }
Alan Cox4ac43602006-06-28 04:26:52 -07002344 writel(0, &cp->status);
2345 writel(cmd, &cp->cmd);
2346 hdrp = (cdkhdr_t __iomem *) EBRDGETMEMPTR(brdp, CDK_CDKADDR);
2347 bits = ((unsigned char __iomem *) hdrp) + brdp->slaveoffset +
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 portp->portidx;
Alan Cox4ac43602006-06-28 04:26:52 -07002349 writeb(readb(bits) | portp->portbit, bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 set_bit(ST_CMDING, &portp->state);
2351 EBRDDISABLE(brdp);
Alan Cox4ac43602006-06-28 04:26:52 -07002352 spin_unlock_irqrestore(&brd_lock, flags);
2353}
2354
2355static void stli_sendcmd(stlibrd_t *brdp, stliport_t *portp, unsigned long cmd, void *arg, int size, int copyback)
2356{
2357 unsigned long flags;
2358
2359 spin_lock_irqsave(&brd_lock, flags);
2360 __stli_sendcmd(brdp, portp, cmd, arg, size, copyback);
2361 spin_unlock_irqrestore(&brd_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362}
2363
2364/*****************************************************************************/
2365
2366/*
2367 * Read data from shared memory. This assumes that the shared memory
2368 * is enabled and that interrupts are off. Basically we just empty out
2369 * the shared memory buffer into the tty buffer. Must be careful to
2370 * handle the case where we fill up the tty buffer, but still have
2371 * more chars to unload.
2372 */
2373
2374static void stli_read(stlibrd_t *brdp, stliport_t *portp)
2375{
Alan Cox4ac43602006-06-28 04:26:52 -07002376 cdkasyrq_t __iomem *rp;
2377 char __iomem *shbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 struct tty_struct *tty;
Alan Cox4ac43602006-06-28 04:26:52 -07002379 unsigned int head, tail, size;
2380 unsigned int len, stlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381
2382 if (test_bit(ST_RXSTOP, &portp->state))
2383 return;
2384 tty = portp->tty;
Alan Cox4ac43602006-06-28 04:26:52 -07002385 if (tty == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 return;
2387
Alan Cox4ac43602006-06-28 04:26:52 -07002388 rp = &((cdkasy_t __iomem *) EBRDGETMEMPTR(brdp, portp->addr))->rxq;
2389 head = (unsigned int) readw(&rp->head);
2390 if (head != ((unsigned int) readw(&rp->head)))
2391 head = (unsigned int) readw(&rp->head);
2392 tail = (unsigned int) readw(&rp->tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 size = portp->rxsize;
2394 if (head >= tail) {
2395 len = head - tail;
2396 stlen = len;
2397 } else {
2398 len = size - (tail - head);
2399 stlen = size - tail;
2400 }
2401
Alan Cox33f0f882006-01-09 20:54:13 -08002402 len = tty_buffer_request_room(tty, len);
Alan Cox4ac43602006-06-28 04:26:52 -07002403
2404 shbuf = (char __iomem *) EBRDGETMEMPTR(brdp, portp->rxoffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405
2406 while (len > 0) {
Alan Cox4ac43602006-06-28 04:26:52 -07002407 unsigned char *cptr;
2408
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 stlen = MIN(len, stlen);
Alan Cox4ac43602006-06-28 04:26:52 -07002410 tty_prepare_flip_string(tty, &cptr, stlen);
2411 memcpy_fromio(cptr, shbuf + tail, stlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 len -= stlen;
2413 tail += stlen;
2414 if (tail >= size) {
2415 tail = 0;
2416 stlen = head;
2417 }
2418 }
Alan Cox4ac43602006-06-28 04:26:52 -07002419 rp = &((cdkasy_t __iomem *) EBRDGETMEMPTR(brdp, portp->addr))->rxq;
2420 writew(tail, &rp->tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421
2422 if (head != tail)
2423 set_bit(ST_RXING, &portp->state);
2424
2425 tty_schedule_flip(tty);
2426}
2427
2428/*****************************************************************************/
2429
2430/*
2431 * Set up and carry out any delayed commands. There is only a small set
2432 * of slave commands that can be done "off-level". So it is not too
2433 * difficult to deal with them here.
2434 */
2435
Alan Cox4ac43602006-06-28 04:26:52 -07002436static void stli_dodelaycmd(stliport_t *portp, cdkctrl_t __iomem *cp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437{
Alan Cox4ac43602006-06-28 04:26:52 -07002438 int cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439
2440 if (test_bit(ST_DOSIGS, &portp->state)) {
2441 if (test_bit(ST_DOFLUSHTX, &portp->state) &&
2442 test_bit(ST_DOFLUSHRX, &portp->state))
2443 cmd = A_SETSIGNALSF;
2444 else if (test_bit(ST_DOFLUSHTX, &portp->state))
2445 cmd = A_SETSIGNALSFTX;
2446 else if (test_bit(ST_DOFLUSHRX, &portp->state))
2447 cmd = A_SETSIGNALSFRX;
2448 else
2449 cmd = A_SETSIGNALS;
2450 clear_bit(ST_DOFLUSHTX, &portp->state);
2451 clear_bit(ST_DOFLUSHRX, &portp->state);
2452 clear_bit(ST_DOSIGS, &portp->state);
Alan Cox4ac43602006-06-28 04:26:52 -07002453 memcpy_toio((void __iomem *) &(cp->args[0]), (void *) &portp->asig,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 sizeof(asysigs_t));
Alan Cox4ac43602006-06-28 04:26:52 -07002455 writel(0, &cp->status);
2456 writel(cmd, &cp->cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 set_bit(ST_CMDING, &portp->state);
2458 } else if (test_bit(ST_DOFLUSHTX, &portp->state) ||
2459 test_bit(ST_DOFLUSHRX, &portp->state)) {
2460 cmd = ((test_bit(ST_DOFLUSHTX, &portp->state)) ? FLUSHTX : 0);
2461 cmd |= ((test_bit(ST_DOFLUSHRX, &portp->state)) ? FLUSHRX : 0);
2462 clear_bit(ST_DOFLUSHTX, &portp->state);
2463 clear_bit(ST_DOFLUSHRX, &portp->state);
Alan Cox4ac43602006-06-28 04:26:52 -07002464 memcpy_toio((void __iomem *) &(cp->args[0]), (void *) &cmd, sizeof(int));
2465 writel(0, &cp->status);
2466 writel(A_FLUSH, &cp->cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 set_bit(ST_CMDING, &portp->state);
2468 }
2469}
2470
2471/*****************************************************************************/
2472
2473/*
2474 * Host command service checking. This handles commands or messages
2475 * coming from the slave to the host. Must have board shared memory
2476 * enabled and interrupts off when called. Notice that by servicing the
2477 * read data last we don't need to change the shared memory pointer
2478 * during processing (which is a slow IO operation).
2479 * Return value indicates if this port is still awaiting actions from
2480 * the slave (like open, command, or even TX data being sent). If 0
2481 * then port is still busy, otherwise no longer busy.
2482 */
2483
2484static int stli_hostcmd(stlibrd_t *brdp, stliport_t *portp)
2485{
Alan Cox4ac43602006-06-28 04:26:52 -07002486 cdkasy_t __iomem *ap;
2487 cdkctrl_t __iomem *cp;
2488 struct tty_struct *tty;
2489 asynotify_t nt;
2490 unsigned long oldsigs;
2491 int rc, donerx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492
Alan Cox4ac43602006-06-28 04:26:52 -07002493 ap = (cdkasy_t __iomem *) EBRDGETMEMPTR(brdp, portp->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 cp = &ap->ctrl;
2495
2496/*
2497 * Check if we are waiting for an open completion message.
2498 */
2499 if (test_bit(ST_OPENING, &portp->state)) {
Alan Cox4ac43602006-06-28 04:26:52 -07002500 rc = readl(&cp->openarg);
2501 if (readb(&cp->open) == 0 && rc != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 if (rc > 0)
2503 rc--;
Alan Cox4ac43602006-06-28 04:26:52 -07002504 writel(0, &cp->openarg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 portp->rc = rc;
2506 clear_bit(ST_OPENING, &portp->state);
2507 wake_up_interruptible(&portp->raw_wait);
2508 }
2509 }
2510
2511/*
2512 * Check if we are waiting for a close completion message.
2513 */
2514 if (test_bit(ST_CLOSING, &portp->state)) {
Alan Cox4ac43602006-06-28 04:26:52 -07002515 rc = (int) readl(&cp->closearg);
2516 if (readb(&cp->close) == 0 && rc != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 if (rc > 0)
2518 rc--;
Alan Cox4ac43602006-06-28 04:26:52 -07002519 writel(0, &cp->closearg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 portp->rc = rc;
2521 clear_bit(ST_CLOSING, &portp->state);
2522 wake_up_interruptible(&portp->raw_wait);
2523 }
2524 }
2525
2526/*
2527 * Check if we are waiting for a command completion message. We may
2528 * need to copy out the command results associated with this command.
2529 */
2530 if (test_bit(ST_CMDING, &portp->state)) {
Alan Cox4ac43602006-06-28 04:26:52 -07002531 rc = readl(&cp->status);
2532 if (readl(&cp->cmd) == 0 && rc != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 if (rc > 0)
2534 rc--;
Alan Cox4ac43602006-06-28 04:26:52 -07002535 if (portp->argp != NULL) {
2536 memcpy_fromio(portp->argp, (void __iomem *) &(cp->args[0]),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 portp->argsize);
Alan Cox4ac43602006-06-28 04:26:52 -07002538 portp->argp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 }
Alan Cox4ac43602006-06-28 04:26:52 -07002540 writel(0, &cp->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541 portp->rc = rc;
2542 clear_bit(ST_CMDING, &portp->state);
2543 stli_dodelaycmd(portp, cp);
2544 wake_up_interruptible(&portp->raw_wait);
2545 }
2546 }
2547
2548/*
2549 * Check for any notification messages ready. This includes lots of
2550 * different types of events - RX chars ready, RX break received,
2551 * TX data low or empty in the slave, modem signals changed state.
2552 */
2553 donerx = 0;
2554
2555 if (ap->notify) {
2556 nt = ap->changed;
2557 ap->notify = 0;
2558 tty = portp->tty;
2559
2560 if (nt.signal & SG_DCD) {
2561 oldsigs = portp->sigs;
2562 portp->sigs = stli_mktiocm(nt.sigvalue);
2563 clear_bit(ST_GETSIGS, &portp->state);
2564 if ((portp->sigs & TIOCM_CD) &&
2565 ((oldsigs & TIOCM_CD) == 0))
2566 wake_up_interruptible(&portp->open_wait);
2567 if ((oldsigs & TIOCM_CD) &&
2568 ((portp->sigs & TIOCM_CD) == 0)) {
2569 if (portp->flags & ASYNC_CHECK_CD) {
2570 if (tty)
2571 schedule_work(&portp->tqhangup);
2572 }
2573 }
2574 }
2575
2576 if (nt.data & DT_TXEMPTY)
2577 clear_bit(ST_TXBUSY, &portp->state);
2578 if (nt.data & (DT_TXEMPTY | DT_TXLOW)) {
Alan Cox4ac43602006-06-28 04:26:52 -07002579 if (tty != NULL) {
2580 tty_wakeup(tty);
2581 EBRDENABLE(brdp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 wake_up_interruptible(&tty->write_wait);
2583 }
2584 }
2585
2586 if ((nt.data & DT_RXBREAK) && (portp->rxmarkmsk & BRKINT)) {
Alan Cox4ac43602006-06-28 04:26:52 -07002587 if (tty != NULL) {
Alan Cox33f0f882006-01-09 20:54:13 -08002588 tty_insert_flip_char(tty, 0, TTY_BREAK);
2589 if (portp->flags & ASYNC_SAK) {
2590 do_SAK(tty);
2591 EBRDENABLE(brdp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 }
Alan Cox33f0f882006-01-09 20:54:13 -08002593 tty_schedule_flip(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 }
2595 }
2596
2597 if (nt.data & DT_RXBUSY) {
2598 donerx++;
2599 stli_read(brdp, portp);
2600 }
2601 }
2602
2603/*
2604 * It might seem odd that we are checking for more RX chars here.
2605 * But, we need to handle the case where the tty buffer was previously
2606 * filled, but we had more characters to pass up. The slave will not
2607 * send any more RX notify messages until the RX buffer has been emptied.
2608 * But it will leave the service bits on (since the buffer is not empty).
2609 * So from here we can try to process more RX chars.
2610 */
2611 if ((!donerx) && test_bit(ST_RXING, &portp->state)) {
2612 clear_bit(ST_RXING, &portp->state);
2613 stli_read(brdp, portp);
2614 }
2615
2616 return((test_bit(ST_OPENING, &portp->state) ||
2617 test_bit(ST_CLOSING, &portp->state) ||
2618 test_bit(ST_CMDING, &portp->state) ||
2619 test_bit(ST_TXBUSY, &portp->state) ||
2620 test_bit(ST_RXING, &portp->state)) ? 0 : 1);
2621}
2622
2623/*****************************************************************************/
2624
2625/*
2626 * Service all ports on a particular board. Assumes that the boards
2627 * shared memory is enabled, and that the page pointer is pointed
2628 * at the cdk header structure.
2629 */
2630
Alan Cox4ac43602006-06-28 04:26:52 -07002631static void stli_brdpoll(stlibrd_t *brdp, cdkhdr_t __iomem *hdrp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632{
Alan Cox4ac43602006-06-28 04:26:52 -07002633 stliport_t *portp;
2634 unsigned char hostbits[(STL_MAXCHANS / 8) + 1];
2635 unsigned char slavebits[(STL_MAXCHANS / 8) + 1];
2636 unsigned char __iomem *slavep;
2637 int bitpos, bitat, bitsize;
2638 int channr, nrdevs, slavebitchange;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639
2640 bitsize = brdp->bitsize;
2641 nrdevs = brdp->nrdevs;
2642
2643/*
2644 * Check if slave wants any service. Basically we try to do as
2645 * little work as possible here. There are 2 levels of service
2646 * bits. So if there is nothing to do we bail early. We check
2647 * 8 service bits at a time in the inner loop, so we can bypass
2648 * the lot if none of them want service.
2649 */
Alan Cox4ac43602006-06-28 04:26:52 -07002650 memcpy_fromio(&hostbits[0], (((unsigned char __iomem *) hdrp) + brdp->hostoffset),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651 bitsize);
2652
2653 memset(&slavebits[0], 0, bitsize);
2654 slavebitchange = 0;
2655
2656 for (bitpos = 0; (bitpos < bitsize); bitpos++) {
2657 if (hostbits[bitpos] == 0)
2658 continue;
2659 channr = bitpos * 8;
2660 for (bitat = 0x1; (channr < nrdevs); channr++, bitat <<= 1) {
2661 if (hostbits[bitpos] & bitat) {
2662 portp = brdp->ports[(channr - 1)];
2663 if (stli_hostcmd(brdp, portp)) {
2664 slavebitchange++;
2665 slavebits[bitpos] |= bitat;
2666 }
2667 }
2668 }
2669 }
2670
2671/*
2672 * If any of the ports are no longer busy then update them in the
2673 * slave request bits. We need to do this after, since a host port
2674 * service may initiate more slave requests.
2675 */
2676 if (slavebitchange) {
Alan Cox4ac43602006-06-28 04:26:52 -07002677 hdrp = (cdkhdr_t __iomem *) EBRDGETMEMPTR(brdp, CDK_CDKADDR);
2678 slavep = ((unsigned char __iomem *) hdrp) + brdp->slaveoffset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 for (bitpos = 0; (bitpos < bitsize); bitpos++) {
Alan Cox4ac43602006-06-28 04:26:52 -07002680 if (readb(slavebits + bitpos))
2681 writeb(readb(slavep + bitpos) & ~slavebits[bitpos], slavebits + bitpos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682 }
2683 }
2684}
2685
2686/*****************************************************************************/
2687
2688/*
2689 * Driver poll routine. This routine polls the boards in use and passes
2690 * messages back up to host when necessary. This is actually very
2691 * CPU efficient, since we will always have the kernel poll clock, it
2692 * adds only a few cycles when idle (since board service can be
2693 * determined very easily), but when loaded generates no interrupts
2694 * (with their expensive associated context change).
2695 */
2696
2697static void stli_poll(unsigned long arg)
2698{
Alan Cox4ac43602006-06-28 04:26:52 -07002699 cdkhdr_t __iomem *hdrp;
2700 stlibrd_t *brdp;
2701 int brdnr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702
2703 stli_timerlist.expires = STLI_TIMEOUT;
2704 add_timer(&stli_timerlist);
2705
2706/*
2707 * Check each board and do any servicing required.
2708 */
2709 for (brdnr = 0; (brdnr < stli_nrbrds); brdnr++) {
2710 brdp = stli_brds[brdnr];
Alan Cox4ac43602006-06-28 04:26:52 -07002711 if (brdp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 continue;
2713 if ((brdp->state & BST_STARTED) == 0)
2714 continue;
2715
Alan Cox4ac43602006-06-28 04:26:52 -07002716 spin_lock(&brd_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717 EBRDENABLE(brdp);
Alan Cox4ac43602006-06-28 04:26:52 -07002718 hdrp = (cdkhdr_t __iomem *) EBRDGETMEMPTR(brdp, CDK_CDKADDR);
2719 if (readb(&hdrp->hostreq))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 stli_brdpoll(brdp, hdrp);
2721 EBRDDISABLE(brdp);
Alan Cox4ac43602006-06-28 04:26:52 -07002722 spin_unlock(&brd_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 }
2724}
2725
2726/*****************************************************************************/
2727
2728/*
2729 * Translate the termios settings into the port setting structure of
2730 * the slave.
2731 */
2732
2733static void stli_mkasyport(stliport_t *portp, asyport_t *pp, struct termios *tiosp)
2734{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735 memset(pp, 0, sizeof(asyport_t));
2736
2737/*
2738 * Start of by setting the baud, char size, parity and stop bit info.
2739 */
Alan Cox1db27c12006-09-29 02:01:38 -07002740 pp->baudout = tty_get_baud_rate(portp->tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 if ((tiosp->c_cflag & CBAUD) == B38400) {
2742 if ((portp->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
2743 pp->baudout = 57600;
2744 else if ((portp->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
2745 pp->baudout = 115200;
2746 else if ((portp->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
2747 pp->baudout = 230400;
2748 else if ((portp->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
2749 pp->baudout = 460800;
2750 else if ((portp->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST)
2751 pp->baudout = (portp->baud_base / portp->custom_divisor);
2752 }
2753 if (pp->baudout > STL_MAXBAUD)
2754 pp->baudout = STL_MAXBAUD;
2755 pp->baudin = pp->baudout;
2756
2757 switch (tiosp->c_cflag & CSIZE) {
2758 case CS5:
2759 pp->csize = 5;
2760 break;
2761 case CS6:
2762 pp->csize = 6;
2763 break;
2764 case CS7:
2765 pp->csize = 7;
2766 break;
2767 default:
2768 pp->csize = 8;
2769 break;
2770 }
2771
2772 if (tiosp->c_cflag & CSTOPB)
2773 pp->stopbs = PT_STOP2;
2774 else
2775 pp->stopbs = PT_STOP1;
2776
2777 if (tiosp->c_cflag & PARENB) {
2778 if (tiosp->c_cflag & PARODD)
2779 pp->parity = PT_ODDPARITY;
2780 else
2781 pp->parity = PT_EVENPARITY;
2782 } else {
2783 pp->parity = PT_NOPARITY;
2784 }
2785
2786/*
2787 * Set up any flow control options enabled.
2788 */
2789 if (tiosp->c_iflag & IXON) {
2790 pp->flow |= F_IXON;
2791 if (tiosp->c_iflag & IXANY)
2792 pp->flow |= F_IXANY;
2793 }
2794 if (tiosp->c_cflag & CRTSCTS)
2795 pp->flow |= (F_RTSFLOW | F_CTSFLOW);
2796
2797 pp->startin = tiosp->c_cc[VSTART];
2798 pp->stopin = tiosp->c_cc[VSTOP];
2799 pp->startout = tiosp->c_cc[VSTART];
2800 pp->stopout = tiosp->c_cc[VSTOP];
2801
2802/*
2803 * Set up the RX char marking mask with those RX error types we must
2804 * catch. We can get the slave to help us out a little here, it will
2805 * ignore parity errors and breaks for us, and mark parity errors in
2806 * the data stream.
2807 */
2808 if (tiosp->c_iflag & IGNPAR)
2809 pp->iflag |= FI_IGNRXERRS;
2810 if (tiosp->c_iflag & IGNBRK)
2811 pp->iflag |= FI_IGNBREAK;
2812
2813 portp->rxmarkmsk = 0;
2814 if (tiosp->c_iflag & (INPCK | PARMRK))
2815 pp->iflag |= FI_1MARKRXERRS;
2816 if (tiosp->c_iflag & BRKINT)
2817 portp->rxmarkmsk |= BRKINT;
2818
2819/*
2820 * Set up clocal processing as required.
2821 */
2822 if (tiosp->c_cflag & CLOCAL)
2823 portp->flags &= ~ASYNC_CHECK_CD;
2824 else
2825 portp->flags |= ASYNC_CHECK_CD;
2826
2827/*
2828 * Transfer any persistent flags into the asyport structure.
2829 */
2830 pp->pflag = (portp->pflag & 0xffff);
2831 pp->vmin = (portp->pflag & P_RXIMIN) ? 1 : 0;
2832 pp->vtime = (portp->pflag & P_RXITIME) ? 1 : 0;
2833 pp->cc[1] = (portp->pflag & P_RXTHOLD) ? 1 : 0;
2834}
2835
2836/*****************************************************************************/
2837
2838/*
2839 * Construct a slave signals structure for setting the DTR and RTS
2840 * signals as specified.
2841 */
2842
2843static void stli_mkasysigs(asysigs_t *sp, int dtr, int rts)
2844{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 memset(sp, 0, sizeof(asysigs_t));
2846 if (dtr >= 0) {
2847 sp->signal |= SG_DTR;
2848 sp->sigvalue |= ((dtr > 0) ? SG_DTR : 0);
2849 }
2850 if (rts >= 0) {
2851 sp->signal |= SG_RTS;
2852 sp->sigvalue |= ((rts > 0) ? SG_RTS : 0);
2853 }
2854}
2855
2856/*****************************************************************************/
2857
2858/*
2859 * Convert the signals returned from the slave into a local TIOCM type
2860 * signals value. We keep them locally in TIOCM format.
2861 */
2862
2863static long stli_mktiocm(unsigned long sigvalue)
2864{
Alan Cox4ac43602006-06-28 04:26:52 -07002865 long tiocm = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 tiocm |= ((sigvalue & SG_DCD) ? TIOCM_CD : 0);
2867 tiocm |= ((sigvalue & SG_CTS) ? TIOCM_CTS : 0);
2868 tiocm |= ((sigvalue & SG_RI) ? TIOCM_RI : 0);
2869 tiocm |= ((sigvalue & SG_DSR) ? TIOCM_DSR : 0);
2870 tiocm |= ((sigvalue & SG_DTR) ? TIOCM_DTR : 0);
2871 tiocm |= ((sigvalue & SG_RTS) ? TIOCM_RTS : 0);
2872 return(tiocm);
2873}
2874
2875/*****************************************************************************/
2876
2877/*
2878 * All panels and ports actually attached have been worked out. All
2879 * we need to do here is set up the appropriate per port data structures.
2880 */
2881
2882static int stli_initports(stlibrd_t *brdp)
2883{
2884 stliport_t *portp;
2885 int i, panelnr, panelport;
2886
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 for (i = 0, panelnr = 0, panelport = 0; (i < brdp->nrports); i++) {
Tobias Klauserb0b4ed72006-03-31 02:30:56 -08002888 portp = kzalloc(sizeof(stliport_t), GFP_KERNEL);
2889 if (!portp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 printk("STALLION: failed to allocate port structure\n");
2891 continue;
2892 }
2893
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 portp->magic = STLI_PORTMAGIC;
2895 portp->portnr = i;
2896 portp->brdnr = brdp->brdnr;
2897 portp->panelnr = panelnr;
2898 portp->baud_base = STL_BAUDBASE;
2899 portp->close_delay = STL_CLOSEDELAY;
2900 portp->closing_wait = 30 * HZ;
Al Viro3e577a82006-12-06 18:41:45 +00002901 INIT_WORK(&portp->tqhangup, stli_dohangup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902 init_waitqueue_head(&portp->open_wait);
2903 init_waitqueue_head(&portp->close_wait);
2904 init_waitqueue_head(&portp->raw_wait);
2905 panelport++;
2906 if (panelport >= brdp->panels[panelnr]) {
2907 panelport = 0;
2908 panelnr++;
2909 }
2910 brdp->ports[i] = portp;
2911 }
2912
Alan Cox4ac43602006-06-28 04:26:52 -07002913 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914}
2915
2916/*****************************************************************************/
2917
2918/*
2919 * All the following routines are board specific hardware operations.
2920 */
2921
2922static void stli_ecpinit(stlibrd_t *brdp)
2923{
2924 unsigned long memconf;
2925
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926 outb(ECP_ATSTOP, (brdp->iobase + ECP_ATCONFR));
2927 udelay(10);
2928 outb(ECP_ATDISABLE, (brdp->iobase + ECP_ATCONFR));
2929 udelay(100);
2930
2931 memconf = (brdp->memaddr & ECP_ATADDRMASK) >> ECP_ATADDRSHFT;
2932 outb(memconf, (brdp->iobase + ECP_ATMEMAR));
2933}
2934
2935/*****************************************************************************/
2936
2937static void stli_ecpenable(stlibrd_t *brdp)
2938{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939 outb(ECP_ATENABLE, (brdp->iobase + ECP_ATCONFR));
2940}
2941
2942/*****************************************************************************/
2943
2944static void stli_ecpdisable(stlibrd_t *brdp)
2945{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946 outb(ECP_ATDISABLE, (brdp->iobase + ECP_ATCONFR));
2947}
2948
2949/*****************************************************************************/
2950
Al Viro29756fa2006-10-10 22:47:27 +01002951static void __iomem *stli_ecpgetmemptr(stlibrd_t *brdp, unsigned long offset, int line)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952{
Al Viro29756fa2006-10-10 22:47:27 +01002953 void __iomem *ptr;
Alan Cox4ac43602006-06-28 04:26:52 -07002954 unsigned char val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955
2956 if (offset > brdp->memsize) {
2957 printk(KERN_ERR "STALLION: shared memory pointer=%x out of "
2958 "range at line=%d(%d), brd=%d\n",
2959 (int) offset, line, __LINE__, brdp->brdnr);
2960 ptr = NULL;
2961 val = 0;
2962 } else {
2963 ptr = brdp->membase + (offset % ECP_ATPAGESIZE);
2964 val = (unsigned char) (offset / ECP_ATPAGESIZE);
2965 }
2966 outb(val, (brdp->iobase + ECP_ATMEMPR));
2967 return(ptr);
2968}
2969
2970/*****************************************************************************/
2971
2972static void stli_ecpreset(stlibrd_t *brdp)
2973{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974 outb(ECP_ATSTOP, (brdp->iobase + ECP_ATCONFR));
2975 udelay(10);
2976 outb(ECP_ATDISABLE, (brdp->iobase + ECP_ATCONFR));
2977 udelay(500);
2978}
2979
2980/*****************************************************************************/
2981
2982static void stli_ecpintr(stlibrd_t *brdp)
2983{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984 outb(0x1, brdp->iobase);
2985}
2986
2987/*****************************************************************************/
2988
2989/*
2990 * The following set of functions act on ECP EISA boards.
2991 */
2992
2993static void stli_ecpeiinit(stlibrd_t *brdp)
2994{
2995 unsigned long memconf;
2996
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997 outb(0x1, (brdp->iobase + ECP_EIBRDENAB));
2998 outb(ECP_EISTOP, (brdp->iobase + ECP_EICONFR));
2999 udelay(10);
3000 outb(ECP_EIDISABLE, (brdp->iobase + ECP_EICONFR));
3001 udelay(500);
3002
3003 memconf = (brdp->memaddr & ECP_EIADDRMASKL) >> ECP_EIADDRSHFTL;
3004 outb(memconf, (brdp->iobase + ECP_EIMEMARL));
3005 memconf = (brdp->memaddr & ECP_EIADDRMASKH) >> ECP_EIADDRSHFTH;
3006 outb(memconf, (brdp->iobase + ECP_EIMEMARH));
3007}
3008
3009/*****************************************************************************/
3010
3011static void stli_ecpeienable(stlibrd_t *brdp)
3012{
3013 outb(ECP_EIENABLE, (brdp->iobase + ECP_EICONFR));
3014}
3015
3016/*****************************************************************************/
3017
3018static void stli_ecpeidisable(stlibrd_t *brdp)
3019{
3020 outb(ECP_EIDISABLE, (brdp->iobase + ECP_EICONFR));
3021}
3022
3023/*****************************************************************************/
3024
Al Viro29756fa2006-10-10 22:47:27 +01003025static void __iomem *stli_ecpeigetmemptr(stlibrd_t *brdp, unsigned long offset, int line)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026{
Al Viro29756fa2006-10-10 22:47:27 +01003027 void __iomem *ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028 unsigned char val;
3029
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030 if (offset > brdp->memsize) {
3031 printk(KERN_ERR "STALLION: shared memory pointer=%x out of "
3032 "range at line=%d(%d), brd=%d\n",
3033 (int) offset, line, __LINE__, brdp->brdnr);
3034 ptr = NULL;
3035 val = 0;
3036 } else {
3037 ptr = brdp->membase + (offset % ECP_EIPAGESIZE);
3038 if (offset < ECP_EIPAGESIZE)
3039 val = ECP_EIENABLE;
3040 else
3041 val = ECP_EIENABLE | 0x40;
3042 }
3043 outb(val, (brdp->iobase + ECP_EICONFR));
3044 return(ptr);
3045}
3046
3047/*****************************************************************************/
3048
3049static void stli_ecpeireset(stlibrd_t *brdp)
3050{
3051 outb(ECP_EISTOP, (brdp->iobase + ECP_EICONFR));
3052 udelay(10);
3053 outb(ECP_EIDISABLE, (brdp->iobase + ECP_EICONFR));
3054 udelay(500);
3055}
3056
3057/*****************************************************************************/
3058
3059/*
3060 * The following set of functions act on ECP MCA boards.
3061 */
3062
3063static void stli_ecpmcenable(stlibrd_t *brdp)
3064{
3065 outb(ECP_MCENABLE, (brdp->iobase + ECP_MCCONFR));
3066}
3067
3068/*****************************************************************************/
3069
3070static void stli_ecpmcdisable(stlibrd_t *brdp)
3071{
3072 outb(ECP_MCDISABLE, (brdp->iobase + ECP_MCCONFR));
3073}
3074
3075/*****************************************************************************/
3076
Al Viro29756fa2006-10-10 22:47:27 +01003077static void __iomem *stli_ecpmcgetmemptr(stlibrd_t *brdp, unsigned long offset, int line)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078{
Al Viro29756fa2006-10-10 22:47:27 +01003079 void __iomem *ptr;
Alan Cox4ac43602006-06-28 04:26:52 -07003080 unsigned char val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081
3082 if (offset > brdp->memsize) {
3083 printk(KERN_ERR "STALLION: shared memory pointer=%x out of "
3084 "range at line=%d(%d), brd=%d\n",
3085 (int) offset, line, __LINE__, brdp->brdnr);
3086 ptr = NULL;
3087 val = 0;
3088 } else {
3089 ptr = brdp->membase + (offset % ECP_MCPAGESIZE);
3090 val = ((unsigned char) (offset / ECP_MCPAGESIZE)) | ECP_MCENABLE;
3091 }
3092 outb(val, (brdp->iobase + ECP_MCCONFR));
3093 return(ptr);
3094}
3095
3096/*****************************************************************************/
3097
3098static void stli_ecpmcreset(stlibrd_t *brdp)
3099{
3100 outb(ECP_MCSTOP, (brdp->iobase + ECP_MCCONFR));
3101 udelay(10);
3102 outb(ECP_MCDISABLE, (brdp->iobase + ECP_MCCONFR));
3103 udelay(500);
3104}
3105
3106/*****************************************************************************/
3107
3108/*
3109 * The following set of functions act on ECP PCI boards.
3110 */
3111
3112static void stli_ecppciinit(stlibrd_t *brdp)
3113{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114 outb(ECP_PCISTOP, (brdp->iobase + ECP_PCICONFR));
3115 udelay(10);
3116 outb(0, (brdp->iobase + ECP_PCICONFR));
3117 udelay(500);
3118}
3119
3120/*****************************************************************************/
3121
Al Viro29756fa2006-10-10 22:47:27 +01003122static void __iomem *stli_ecppcigetmemptr(stlibrd_t *brdp, unsigned long offset, int line)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123{
Al Viro29756fa2006-10-10 22:47:27 +01003124 void __iomem *ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125 unsigned char val;
3126
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127 if (offset > brdp->memsize) {
3128 printk(KERN_ERR "STALLION: shared memory pointer=%x out of "
3129 "range at line=%d(%d), board=%d\n",
3130 (int) offset, line, __LINE__, brdp->brdnr);
3131 ptr = NULL;
3132 val = 0;
3133 } else {
3134 ptr = brdp->membase + (offset % ECP_PCIPAGESIZE);
3135 val = (offset / ECP_PCIPAGESIZE) << 1;
3136 }
3137 outb(val, (brdp->iobase + ECP_PCICONFR));
3138 return(ptr);
3139}
3140
3141/*****************************************************************************/
3142
3143static void stli_ecppcireset(stlibrd_t *brdp)
3144{
3145 outb(ECP_PCISTOP, (brdp->iobase + ECP_PCICONFR));
3146 udelay(10);
3147 outb(0, (brdp->iobase + ECP_PCICONFR));
3148 udelay(500);
3149}
3150
3151/*****************************************************************************/
3152
3153/*
3154 * The following routines act on ONboards.
3155 */
3156
3157static void stli_onbinit(stlibrd_t *brdp)
3158{
3159 unsigned long memconf;
3160
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161 outb(ONB_ATSTOP, (brdp->iobase + ONB_ATCONFR));
3162 udelay(10);
3163 outb(ONB_ATDISABLE, (brdp->iobase + ONB_ATCONFR));
3164 mdelay(1000);
3165
3166 memconf = (brdp->memaddr & ONB_ATADDRMASK) >> ONB_ATADDRSHFT;
3167 outb(memconf, (brdp->iobase + ONB_ATMEMAR));
3168 outb(0x1, brdp->iobase);
3169 mdelay(1);
3170}
3171
3172/*****************************************************************************/
3173
3174static void stli_onbenable(stlibrd_t *brdp)
3175{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176 outb((brdp->enabval | ONB_ATENABLE), (brdp->iobase + ONB_ATCONFR));
3177}
3178
3179/*****************************************************************************/
3180
3181static void stli_onbdisable(stlibrd_t *brdp)
3182{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183 outb((brdp->enabval | ONB_ATDISABLE), (brdp->iobase + ONB_ATCONFR));
3184}
3185
3186/*****************************************************************************/
3187
Al Viro29756fa2006-10-10 22:47:27 +01003188static void __iomem *stli_onbgetmemptr(stlibrd_t *brdp, unsigned long offset, int line)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189{
Al Viro29756fa2006-10-10 22:47:27 +01003190 void __iomem *ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192 if (offset > brdp->memsize) {
3193 printk(KERN_ERR "STALLION: shared memory pointer=%x out of "
3194 "range at line=%d(%d), brd=%d\n",
3195 (int) offset, line, __LINE__, brdp->brdnr);
3196 ptr = NULL;
3197 } else {
3198 ptr = brdp->membase + (offset % ONB_ATPAGESIZE);
3199 }
3200 return(ptr);
3201}
3202
3203/*****************************************************************************/
3204
3205static void stli_onbreset(stlibrd_t *brdp)
3206{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207 outb(ONB_ATSTOP, (brdp->iobase + ONB_ATCONFR));
3208 udelay(10);
3209 outb(ONB_ATDISABLE, (brdp->iobase + ONB_ATCONFR));
3210 mdelay(1000);
3211}
3212
3213/*****************************************************************************/
3214
3215/*
3216 * The following routines act on ONboard EISA.
3217 */
3218
3219static void stli_onbeinit(stlibrd_t *brdp)
3220{
3221 unsigned long memconf;
3222
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223 outb(0x1, (brdp->iobase + ONB_EIBRDENAB));
3224 outb(ONB_EISTOP, (brdp->iobase + ONB_EICONFR));
3225 udelay(10);
3226 outb(ONB_EIDISABLE, (brdp->iobase + ONB_EICONFR));
3227 mdelay(1000);
3228
3229 memconf = (brdp->memaddr & ONB_EIADDRMASKL) >> ONB_EIADDRSHFTL;
3230 outb(memconf, (brdp->iobase + ONB_EIMEMARL));
3231 memconf = (brdp->memaddr & ONB_EIADDRMASKH) >> ONB_EIADDRSHFTH;
3232 outb(memconf, (brdp->iobase + ONB_EIMEMARH));
3233 outb(0x1, brdp->iobase);
3234 mdelay(1);
3235}
3236
3237/*****************************************************************************/
3238
3239static void stli_onbeenable(stlibrd_t *brdp)
3240{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241 outb(ONB_EIENABLE, (brdp->iobase + ONB_EICONFR));
3242}
3243
3244/*****************************************************************************/
3245
3246static void stli_onbedisable(stlibrd_t *brdp)
3247{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248 outb(ONB_EIDISABLE, (brdp->iobase + ONB_EICONFR));
3249}
3250
3251/*****************************************************************************/
3252
Al Viro29756fa2006-10-10 22:47:27 +01003253static void __iomem *stli_onbegetmemptr(stlibrd_t *brdp, unsigned long offset, int line)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254{
Al Viro29756fa2006-10-10 22:47:27 +01003255 void __iomem *ptr;
Alan Cox4ac43602006-06-28 04:26:52 -07003256 unsigned char val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257
3258 if (offset > brdp->memsize) {
3259 printk(KERN_ERR "STALLION: shared memory pointer=%x out of "
3260 "range at line=%d(%d), brd=%d\n",
3261 (int) offset, line, __LINE__, brdp->brdnr);
3262 ptr = NULL;
3263 val = 0;
3264 } else {
3265 ptr = brdp->membase + (offset % ONB_EIPAGESIZE);
3266 if (offset < ONB_EIPAGESIZE)
3267 val = ONB_EIENABLE;
3268 else
3269 val = ONB_EIENABLE | 0x40;
3270 }
3271 outb(val, (brdp->iobase + ONB_EICONFR));
3272 return(ptr);
3273}
3274
3275/*****************************************************************************/
3276
3277static void stli_onbereset(stlibrd_t *brdp)
3278{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279 outb(ONB_EISTOP, (brdp->iobase + ONB_EICONFR));
3280 udelay(10);
3281 outb(ONB_EIDISABLE, (brdp->iobase + ONB_EICONFR));
3282 mdelay(1000);
3283}
3284
3285/*****************************************************************************/
3286
3287/*
3288 * The following routines act on Brumby boards.
3289 */
3290
3291static void stli_bbyinit(stlibrd_t *brdp)
3292{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293 outb(BBY_ATSTOP, (brdp->iobase + BBY_ATCONFR));
3294 udelay(10);
3295 outb(0, (brdp->iobase + BBY_ATCONFR));
3296 mdelay(1000);
3297 outb(0x1, brdp->iobase);
3298 mdelay(1);
3299}
3300
3301/*****************************************************************************/
3302
Al Viro29756fa2006-10-10 22:47:27 +01003303static void __iomem *stli_bbygetmemptr(stlibrd_t *brdp, unsigned long offset, int line)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304{
Al Viro29756fa2006-10-10 22:47:27 +01003305 void __iomem *ptr;
Alan Cox4ac43602006-06-28 04:26:52 -07003306 unsigned char val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307
Alan Cox4ac43602006-06-28 04:26:52 -07003308 BUG_ON(offset > brdp->memsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309
Alan Cox4ac43602006-06-28 04:26:52 -07003310 ptr = brdp->membase + (offset % BBY_PAGESIZE);
3311 val = (unsigned char) (offset / BBY_PAGESIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312 outb(val, (brdp->iobase + BBY_ATCONFR));
3313 return(ptr);
3314}
3315
3316/*****************************************************************************/
3317
3318static void stli_bbyreset(stlibrd_t *brdp)
3319{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320 outb(BBY_ATSTOP, (brdp->iobase + BBY_ATCONFR));
3321 udelay(10);
3322 outb(0, (brdp->iobase + BBY_ATCONFR));
3323 mdelay(1000);
3324}
3325
3326/*****************************************************************************/
3327
3328/*
3329 * The following routines act on original old Stallion boards.
3330 */
3331
3332static void stli_stalinit(stlibrd_t *brdp)
3333{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334 outb(0x1, brdp->iobase);
3335 mdelay(1000);
3336}
3337
3338/*****************************************************************************/
3339
Al Viro29756fa2006-10-10 22:47:27 +01003340static void __iomem *stli_stalgetmemptr(stlibrd_t *brdp, unsigned long offset, int line)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341{
Alan Cox4ac43602006-06-28 04:26:52 -07003342 BUG_ON(offset > brdp->memsize);
3343 return brdp->membase + (offset % STAL_PAGESIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344}
3345
3346/*****************************************************************************/
3347
3348static void stli_stalreset(stlibrd_t *brdp)
3349{
Alan Cox4ac43602006-06-28 04:26:52 -07003350 u32 __iomem *vecp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351
Alan Cox4ac43602006-06-28 04:26:52 -07003352 vecp = (u32 __iomem *) (brdp->membase + 0x30);
3353 writel(0xffff0000, vecp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354 outb(0, brdp->iobase);
3355 mdelay(1000);
3356}
3357
3358/*****************************************************************************/
3359
3360/*
3361 * Try to find an ECP board and initialize it. This handles only ECP
3362 * board types.
3363 */
3364
3365static int stli_initecp(stlibrd_t *brdp)
3366{
Alan Cox4ac43602006-06-28 04:26:52 -07003367 cdkecpsig_t sig;
3368 cdkecpsig_t __iomem *sigsp;
3369 unsigned int status, nxtid;
3370 char *name;
3371 int panelnr, nrports;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372
3373 if (!request_region(brdp->iobase, brdp->iosize, "istallion"))
3374 return -EIO;
3375
3376 if ((brdp->iobase == 0) || (brdp->memaddr == 0))
3377 {
3378 release_region(brdp->iobase, brdp->iosize);
Alan Cox4ac43602006-06-28 04:26:52 -07003379 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380 }
3381
3382 brdp->iosize = ECP_IOSIZE;
3383
3384/*
3385 * Based on the specific board type setup the common vars to access
3386 * and enable shared memory. Set all board specific information now
3387 * as well.
3388 */
3389 switch (brdp->brdtype) {
3390 case BRD_ECP:
3391 brdp->membase = (void *) brdp->memaddr;
3392 brdp->memsize = ECP_MEMSIZE;
3393 brdp->pagesize = ECP_ATPAGESIZE;
3394 brdp->init = stli_ecpinit;
3395 brdp->enable = stli_ecpenable;
3396 brdp->reenable = stli_ecpenable;
3397 brdp->disable = stli_ecpdisable;
3398 brdp->getmemptr = stli_ecpgetmemptr;
3399 brdp->intr = stli_ecpintr;
3400 brdp->reset = stli_ecpreset;
3401 name = "serial(EC8/64)";
3402 break;
3403
3404 case BRD_ECPE:
3405 brdp->membase = (void *) brdp->memaddr;
3406 brdp->memsize = ECP_MEMSIZE;
3407 brdp->pagesize = ECP_EIPAGESIZE;
3408 brdp->init = stli_ecpeiinit;
3409 brdp->enable = stli_ecpeienable;
3410 brdp->reenable = stli_ecpeienable;
3411 brdp->disable = stli_ecpeidisable;
3412 brdp->getmemptr = stli_ecpeigetmemptr;
3413 brdp->intr = stli_ecpintr;
3414 brdp->reset = stli_ecpeireset;
3415 name = "serial(EC8/64-EI)";
3416 break;
3417
3418 case BRD_ECPMC:
3419 brdp->membase = (void *) brdp->memaddr;
3420 brdp->memsize = ECP_MEMSIZE;
3421 brdp->pagesize = ECP_MCPAGESIZE;
3422 brdp->init = NULL;
3423 brdp->enable = stli_ecpmcenable;
3424 brdp->reenable = stli_ecpmcenable;
3425 brdp->disable = stli_ecpmcdisable;
3426 brdp->getmemptr = stli_ecpmcgetmemptr;
3427 brdp->intr = stli_ecpintr;
3428 brdp->reset = stli_ecpmcreset;
3429 name = "serial(EC8/64-MCA)";
3430 break;
3431
3432 case BRD_ECPPCI:
3433 brdp->membase = (void *) brdp->memaddr;
3434 brdp->memsize = ECP_PCIMEMSIZE;
3435 brdp->pagesize = ECP_PCIPAGESIZE;
3436 brdp->init = stli_ecppciinit;
3437 brdp->enable = NULL;
3438 brdp->reenable = NULL;
3439 brdp->disable = NULL;
3440 brdp->getmemptr = stli_ecppcigetmemptr;
3441 brdp->intr = stli_ecpintr;
3442 brdp->reset = stli_ecppcireset;
3443 name = "serial(EC/RA-PCI)";
3444 break;
3445
3446 default:
3447 release_region(brdp->iobase, brdp->iosize);
Alan Cox4ac43602006-06-28 04:26:52 -07003448 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449 }
3450
3451/*
3452 * The per-board operations structure is all set up, so now let's go
3453 * and get the board operational. Firstly initialize board configuration
3454 * registers. Set the memory mapping info so we can get at the boards
3455 * shared memory.
3456 */
3457 EBRDINIT(brdp);
3458
3459 brdp->membase = ioremap(brdp->memaddr, brdp->memsize);
Alan Cox4ac43602006-06-28 04:26:52 -07003460 if (brdp->membase == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461 {
3462 release_region(brdp->iobase, brdp->iosize);
Alan Cox4ac43602006-06-28 04:26:52 -07003463 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464 }
3465
3466/*
3467 * Now that all specific code is set up, enable the shared memory and
3468 * look for the a signature area that will tell us exactly what board
3469 * this is, and what it is connected to it.
3470 */
3471 EBRDENABLE(brdp);
Alan Cox4ac43602006-06-28 04:26:52 -07003472 sigsp = (cdkecpsig_t __iomem *) EBRDGETMEMPTR(brdp, CDK_SIGADDR);
Al Viro634965f2006-09-23 01:20:31 +01003473 memcpy_fromio(&sig, sigsp, sizeof(cdkecpsig_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474 EBRDDISABLE(brdp);
3475
Alan Cox4ac43602006-06-28 04:26:52 -07003476 if (sig.magic != cpu_to_le32(ECP_MAGIC))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477 {
3478 release_region(brdp->iobase, brdp->iosize);
Amol Ladaa8a8d62006-12-06 20:35:22 -08003479 iounmap(brdp->membase);
3480 brdp->membase = NULL;
Alan Cox4ac43602006-06-28 04:26:52 -07003481 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003482 }
3483
3484/*
3485 * Scan through the signature looking at the panels connected to the
3486 * board. Calculate the total number of ports as we go.
3487 */
3488 for (panelnr = 0, nxtid = 0; (panelnr < STL_MAXPANELS); panelnr++) {
3489 status = sig.panelid[nxtid];
3490 if ((status & ECH_PNLIDMASK) != nxtid)
3491 break;
3492
3493 brdp->panelids[panelnr] = status;
3494 nrports = (status & ECH_PNL16PORT) ? 16 : 8;
3495 if ((nrports == 16) && ((status & ECH_PNLXPID) == 0))
3496 nxtid++;
3497 brdp->panels[panelnr] = nrports;
3498 brdp->nrports += nrports;
3499 nxtid++;
3500 brdp->nrpanels++;
3501 }
3502
3503
3504 brdp->state |= BST_FOUND;
Alan Cox4ac43602006-06-28 04:26:52 -07003505 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506}
3507
3508/*****************************************************************************/
3509
3510/*
3511 * Try to find an ONboard, Brumby or Stallion board and initialize it.
3512 * This handles only these board types.
3513 */
3514
3515static int stli_initonb(stlibrd_t *brdp)
3516{
Alan Cox4ac43602006-06-28 04:26:52 -07003517 cdkonbsig_t sig;
3518 cdkonbsig_t __iomem *sigsp;
3519 char *name;
3520 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521
3522/*
3523 * Do a basic sanity check on the IO and memory addresses.
3524 */
Alan Cox4ac43602006-06-28 04:26:52 -07003525 if (brdp->iobase == 0 || brdp->memaddr == 0)
3526 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003527
3528 brdp->iosize = ONB_IOSIZE;
3529
3530 if (!request_region(brdp->iobase, brdp->iosize, "istallion"))
3531 return -EIO;
3532
3533/*
3534 * Based on the specific board type setup the common vars to access
3535 * and enable shared memory. Set all board specific information now
3536 * as well.
3537 */
3538 switch (brdp->brdtype) {
3539 case BRD_ONBOARD:
3540 case BRD_ONBOARD32:
3541 case BRD_ONBOARD2:
3542 case BRD_ONBOARD2_32:
3543 case BRD_ONBOARDRS:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544 brdp->memsize = ONB_MEMSIZE;
3545 brdp->pagesize = ONB_ATPAGESIZE;
3546 brdp->init = stli_onbinit;
3547 brdp->enable = stli_onbenable;
3548 brdp->reenable = stli_onbenable;
3549 brdp->disable = stli_onbdisable;
3550 brdp->getmemptr = stli_onbgetmemptr;
3551 brdp->intr = stli_ecpintr;
3552 brdp->reset = stli_onbreset;
3553 if (brdp->memaddr > 0x100000)
3554 brdp->enabval = ONB_MEMENABHI;
3555 else
3556 brdp->enabval = ONB_MEMENABLO;
3557 name = "serial(ONBoard)";
3558 break;
3559
3560 case BRD_ONBOARDE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561 brdp->memsize = ONB_EIMEMSIZE;
3562 brdp->pagesize = ONB_EIPAGESIZE;
3563 brdp->init = stli_onbeinit;
3564 brdp->enable = stli_onbeenable;
3565 brdp->reenable = stli_onbeenable;
3566 brdp->disable = stli_onbedisable;
3567 brdp->getmemptr = stli_onbegetmemptr;
3568 brdp->intr = stli_ecpintr;
3569 brdp->reset = stli_onbereset;
3570 name = "serial(ONBoard/E)";
3571 break;
3572
3573 case BRD_BRUMBY4:
3574 case BRD_BRUMBY8:
3575 case BRD_BRUMBY16:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003576 brdp->memsize = BBY_MEMSIZE;
3577 brdp->pagesize = BBY_PAGESIZE;
3578 brdp->init = stli_bbyinit;
3579 brdp->enable = NULL;
3580 brdp->reenable = NULL;
3581 brdp->disable = NULL;
3582 brdp->getmemptr = stli_bbygetmemptr;
3583 brdp->intr = stli_ecpintr;
3584 brdp->reset = stli_bbyreset;
3585 name = "serial(Brumby)";
3586 break;
3587
3588 case BRD_STALLION:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589 brdp->memsize = STAL_MEMSIZE;
3590 brdp->pagesize = STAL_PAGESIZE;
3591 brdp->init = stli_stalinit;
3592 brdp->enable = NULL;
3593 brdp->reenable = NULL;
3594 brdp->disable = NULL;
3595 brdp->getmemptr = stli_stalgetmemptr;
3596 brdp->intr = stli_ecpintr;
3597 brdp->reset = stli_stalreset;
3598 name = "serial(Stallion)";
3599 break;
3600
3601 default:
3602 release_region(brdp->iobase, brdp->iosize);
Alan Cox4ac43602006-06-28 04:26:52 -07003603 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604 }
3605
3606/*
3607 * The per-board operations structure is all set up, so now let's go
3608 * and get the board operational. Firstly initialize board configuration
3609 * registers. Set the memory mapping info so we can get at the boards
3610 * shared memory.
3611 */
3612 EBRDINIT(brdp);
3613
3614 brdp->membase = ioremap(brdp->memaddr, brdp->memsize);
Alan Cox4ac43602006-06-28 04:26:52 -07003615 if (brdp->membase == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616 {
3617 release_region(brdp->iobase, brdp->iosize);
Alan Cox4ac43602006-06-28 04:26:52 -07003618 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619 }
3620
3621/*
3622 * Now that all specific code is set up, enable the shared memory and
3623 * look for the a signature area that will tell us exactly what board
3624 * this is, and how many ports.
3625 */
3626 EBRDENABLE(brdp);
Alan Cox4ac43602006-06-28 04:26:52 -07003627 sigsp = (cdkonbsig_t __iomem *) EBRDGETMEMPTR(brdp, CDK_SIGADDR);
3628 memcpy_fromio(&sig, sigsp, sizeof(cdkonbsig_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003629 EBRDDISABLE(brdp);
3630
Alan Cox4ac43602006-06-28 04:26:52 -07003631 if (sig.magic0 != cpu_to_le16(ONB_MAGIC0) ||
3632 sig.magic1 != cpu_to_le16(ONB_MAGIC1) ||
3633 sig.magic2 != cpu_to_le16(ONB_MAGIC2) ||
3634 sig.magic3 != cpu_to_le16(ONB_MAGIC3))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635 {
3636 release_region(brdp->iobase, brdp->iosize);
Amol Ladaa8a8d62006-12-06 20:35:22 -08003637 iounmap(brdp->membase);
3638 brdp->membase = NULL;
Alan Cox4ac43602006-06-28 04:26:52 -07003639 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640 }
3641
3642/*
3643 * Scan through the signature alive mask and calculate how many ports
3644 * there are on this board.
3645 */
3646 brdp->nrpanels = 1;
3647 if (sig.amask1) {
3648 brdp->nrports = 32;
3649 } else {
3650 for (i = 0; (i < 16); i++) {
3651 if (((sig.amask0 << i) & 0x8000) == 0)
3652 break;
3653 }
3654 brdp->nrports = i;
3655 }
3656 brdp->panels[0] = brdp->nrports;
3657
3658
3659 brdp->state |= BST_FOUND;
Alan Cox4ac43602006-06-28 04:26:52 -07003660 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003661}
3662
3663/*****************************************************************************/
3664
3665/*
3666 * Start up a running board. This routine is only called after the
3667 * code has been down loaded to the board and is operational. It will
3668 * read in the memory map, and get the show on the road...
3669 */
3670
3671static int stli_startbrd(stlibrd_t *brdp)
3672{
Alan Cox4ac43602006-06-28 04:26:52 -07003673 cdkhdr_t __iomem *hdrp;
3674 cdkmem_t __iomem *memp;
3675 cdkasy_t __iomem *ap;
3676 unsigned long flags;
3677 stliport_t *portp;
3678 int portnr, nrdevs, i, rc = 0;
3679 u32 memoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680
Alan Cox4ac43602006-06-28 04:26:52 -07003681 spin_lock_irqsave(&brd_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682 EBRDENABLE(brdp);
Alan Cox4ac43602006-06-28 04:26:52 -07003683 hdrp = (cdkhdr_t __iomem *) EBRDGETMEMPTR(brdp, CDK_CDKADDR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684 nrdevs = hdrp->nrdevs;
3685
3686#if 0
3687 printk("%s(%d): CDK version %d.%d.%d --> "
3688 "nrdevs=%d memp=%x hostp=%x slavep=%x\n",
Alan Cox4ac43602006-06-28 04:26:52 -07003689 __FILE__, __LINE__, readb(&hdrp->ver_release), readb(&hdrp->ver_modification),
3690 readb(&hdrp->ver_fix), nrdevs, (int) readl(&hdrp->memp), readl(&hdrp->hostp),
3691 readl(&hdrp->slavep));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003692#endif
3693
3694 if (nrdevs < (brdp->nrports + 1)) {
3695 printk(KERN_ERR "STALLION: slave failed to allocate memory for "
3696 "all devices, devices=%d\n", nrdevs);
3697 brdp->nrports = nrdevs - 1;
3698 }
3699 brdp->nrdevs = nrdevs;
3700 brdp->hostoffset = hdrp->hostp - CDK_CDKADDR;
3701 brdp->slaveoffset = hdrp->slavep - CDK_CDKADDR;
3702 brdp->bitsize = (nrdevs + 7) / 8;
Alan Cox4ac43602006-06-28 04:26:52 -07003703 memoff = readl(&hdrp->memp);
3704 if (memoff > brdp->memsize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705 printk(KERN_ERR "STALLION: corrupted shared memory region?\n");
3706 rc = -EIO;
3707 goto stli_donestartup;
3708 }
Alan Cox4ac43602006-06-28 04:26:52 -07003709 memp = (cdkmem_t __iomem *) EBRDGETMEMPTR(brdp, memoff);
3710 if (readw(&memp->dtype) != TYP_ASYNCTRL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003711 printk(KERN_ERR "STALLION: no slave control device found\n");
3712 goto stli_donestartup;
3713 }
3714 memp++;
3715
3716/*
3717 * Cycle through memory allocation of each port. We are guaranteed to
3718 * have all ports inside the first page of slave window, so no need to
3719 * change pages while reading memory map.
3720 */
3721 for (i = 1, portnr = 0; (i < nrdevs); i++, portnr++, memp++) {
Alan Cox4ac43602006-06-28 04:26:52 -07003722 if (readw(&memp->dtype) != TYP_ASYNC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003723 break;
3724 portp = brdp->ports[portnr];
Alan Cox4ac43602006-06-28 04:26:52 -07003725 if (portp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726 break;
3727 portp->devnr = i;
Alan Cox4ac43602006-06-28 04:26:52 -07003728 portp->addr = readl(&memp->offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729 portp->reqbit = (unsigned char) (0x1 << (i * 8 / nrdevs));
3730 portp->portidx = (unsigned char) (i / 8);
3731 portp->portbit = (unsigned char) (0x1 << (i % 8));
3732 }
3733
Alan Cox4ac43602006-06-28 04:26:52 -07003734 writeb(0xff, &hdrp->slavereq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003735
3736/*
3737 * For each port setup a local copy of the RX and TX buffer offsets
3738 * and sizes. We do this separate from the above, because we need to
3739 * move the shared memory page...
3740 */
3741 for (i = 1, portnr = 0; (i < nrdevs); i++, portnr++) {
3742 portp = brdp->ports[portnr];
Alan Cox4ac43602006-06-28 04:26:52 -07003743 if (portp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003744 break;
3745 if (portp->addr == 0)
3746 break;
Alan Cox4ac43602006-06-28 04:26:52 -07003747 ap = (cdkasy_t __iomem *) EBRDGETMEMPTR(brdp, portp->addr);
3748 if (ap != NULL) {
3749 portp->rxsize = readw(&ap->rxq.size);
3750 portp->txsize = readw(&ap->txq.size);
3751 portp->rxoffset = readl(&ap->rxq.offset);
3752 portp->txoffset = readl(&ap->txq.offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753 }
3754 }
3755
3756stli_donestartup:
3757 EBRDDISABLE(brdp);
Alan Cox4ac43602006-06-28 04:26:52 -07003758 spin_unlock_irqrestore(&brd_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003759
3760 if (rc == 0)
3761 brdp->state |= BST_STARTED;
3762
3763 if (! stli_timeron) {
3764 stli_timeron++;
3765 stli_timerlist.expires = STLI_TIMEOUT;
3766 add_timer(&stli_timerlist);
3767 }
3768
Alan Cox4ac43602006-06-28 04:26:52 -07003769 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003770}
3771
3772/*****************************************************************************/
3773
3774/*
3775 * Probe and initialize the specified board.
3776 */
3777
3778static int __init stli_brdinit(stlibrd_t *brdp)
3779{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003780 stli_brds[brdp->brdnr] = brdp;
3781
3782 switch (brdp->brdtype) {
3783 case BRD_ECP:
3784 case BRD_ECPE:
3785 case BRD_ECPMC:
3786 case BRD_ECPPCI:
3787 stli_initecp(brdp);
3788 break;
3789 case BRD_ONBOARD:
3790 case BRD_ONBOARDE:
3791 case BRD_ONBOARD2:
3792 case BRD_ONBOARD32:
3793 case BRD_ONBOARD2_32:
3794 case BRD_ONBOARDRS:
3795 case BRD_BRUMBY4:
3796 case BRD_BRUMBY8:
3797 case BRD_BRUMBY16:
3798 case BRD_STALLION:
3799 stli_initonb(brdp);
3800 break;
3801 case BRD_EASYIO:
3802 case BRD_ECH:
3803 case BRD_ECHMC:
3804 case BRD_ECHPCI:
3805 printk(KERN_ERR "STALLION: %s board type not supported in "
3806 "this driver\n", stli_brdnames[brdp->brdtype]);
Alan Cox4ac43602006-06-28 04:26:52 -07003807 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808 default:
3809 printk(KERN_ERR "STALLION: board=%d is unknown board "
3810 "type=%d\n", brdp->brdnr, brdp->brdtype);
Alan Cox4ac43602006-06-28 04:26:52 -07003811 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003812 }
3813
3814 if ((brdp->state & BST_FOUND) == 0) {
3815 printk(KERN_ERR "STALLION: %s board not found, board=%d "
3816 "io=%x mem=%x\n",
3817 stli_brdnames[brdp->brdtype], brdp->brdnr,
3818 brdp->iobase, (int) brdp->memaddr);
Alan Cox4ac43602006-06-28 04:26:52 -07003819 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003820 }
3821
3822 stli_initports(brdp);
3823 printk(KERN_INFO "STALLION: %s found, board=%d io=%x mem=%x "
3824 "nrpanels=%d nrports=%d\n", stli_brdnames[brdp->brdtype],
3825 brdp->brdnr, brdp->iobase, (int) brdp->memaddr,
3826 brdp->nrpanels, brdp->nrports);
Alan Cox4ac43602006-06-28 04:26:52 -07003827 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003828}
3829
3830/*****************************************************************************/
3831
3832/*
3833 * Probe around trying to find where the EISA boards shared memory
3834 * might be. This is a bit if hack, but it is the best we can do.
3835 */
3836
3837static int stli_eisamemprobe(stlibrd_t *brdp)
3838{
Alan Cox4ac43602006-06-28 04:26:52 -07003839 cdkecpsig_t ecpsig, __iomem *ecpsigp;
3840 cdkonbsig_t onbsig, __iomem *onbsigp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003841 int i, foundit;
3842
Linus Torvalds1da177e2005-04-16 15:20:36 -07003843/*
3844 * First up we reset the board, to get it into a known state. There
3845 * is only 2 board types here we need to worry about. Don;t use the
3846 * standard board init routine here, it programs up the shared
3847 * memory address, and we don't know it yet...
3848 */
3849 if (brdp->brdtype == BRD_ECPE) {
3850 outb(0x1, (brdp->iobase + ECP_EIBRDENAB));
3851 outb(ECP_EISTOP, (brdp->iobase + ECP_EICONFR));
3852 udelay(10);
3853 outb(ECP_EIDISABLE, (brdp->iobase + ECP_EICONFR));
3854 udelay(500);
3855 stli_ecpeienable(brdp);
3856 } else if (brdp->brdtype == BRD_ONBOARDE) {
3857 outb(0x1, (brdp->iobase + ONB_EIBRDENAB));
3858 outb(ONB_EISTOP, (brdp->iobase + ONB_EICONFR));
3859 udelay(10);
3860 outb(ONB_EIDISABLE, (brdp->iobase + ONB_EICONFR));
3861 mdelay(100);
3862 outb(0x1, brdp->iobase);
3863 mdelay(1);
3864 stli_onbeenable(brdp);
3865 } else {
Alan Cox4ac43602006-06-28 04:26:52 -07003866 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003867 }
3868
3869 foundit = 0;
3870 brdp->memsize = ECP_MEMSIZE;
3871
3872/*
3873 * Board shared memory is enabled, so now we have a poke around and
3874 * see if we can find it.
3875 */
3876 for (i = 0; (i < stli_eisamempsize); i++) {
3877 brdp->memaddr = stli_eisamemprobeaddrs[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003878 brdp->membase = ioremap(brdp->memaddr, brdp->memsize);
Alan Cox4ac43602006-06-28 04:26:52 -07003879 if (brdp->membase == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003880 continue;
3881
3882 if (brdp->brdtype == BRD_ECPE) {
Al Viro29756fa2006-10-10 22:47:27 +01003883 ecpsigp = stli_ecpeigetmemptr(brdp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003884 CDK_SIGADDR, __LINE__);
Alan Cox4ac43602006-06-28 04:26:52 -07003885 memcpy_fromio(&ecpsig, ecpsigp, sizeof(cdkecpsig_t));
3886 if (ecpsig.magic == cpu_to_le32(ECP_MAGIC))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003887 foundit = 1;
3888 } else {
Alan Cox4ac43602006-06-28 04:26:52 -07003889 onbsigp = (cdkonbsig_t __iomem *) stli_onbegetmemptr(brdp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003890 CDK_SIGADDR, __LINE__);
Alan Cox4ac43602006-06-28 04:26:52 -07003891 memcpy_fromio(&onbsig, onbsigp, sizeof(cdkonbsig_t));
3892 if ((onbsig.magic0 == cpu_to_le16(ONB_MAGIC0)) &&
3893 (onbsig.magic1 == cpu_to_le16(ONB_MAGIC1)) &&
3894 (onbsig.magic2 == cpu_to_le16(ONB_MAGIC2)) &&
3895 (onbsig.magic3 == cpu_to_le16(ONB_MAGIC3)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003896 foundit = 1;
3897 }
3898
3899 iounmap(brdp->membase);
3900 if (foundit)
3901 break;
3902 }
3903
3904/*
3905 * Regardless of whether we found the shared memory or not we must
3906 * disable the region. After that return success or failure.
3907 */
3908 if (brdp->brdtype == BRD_ECPE)
3909 stli_ecpeidisable(brdp);
3910 else
3911 stli_onbedisable(brdp);
3912
3913 if (! foundit) {
3914 brdp->memaddr = 0;
3915 brdp->membase = NULL;
3916 printk(KERN_ERR "STALLION: failed to probe shared memory "
3917 "region for %s in EISA slot=%d\n",
3918 stli_brdnames[brdp->brdtype], (brdp->iobase >> 12));
Alan Cox4ac43602006-06-28 04:26:52 -07003919 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003920 }
Alan Cox4ac43602006-06-28 04:26:52 -07003921 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003922}
3923
3924static int stli_getbrdnr(void)
3925{
3926 int i;
3927
3928 for (i = 0; i < STL_MAXBRDS; i++) {
3929 if (!stli_brds[i]) {
3930 if (i >= stli_nrbrds)
3931 stli_nrbrds = i + 1;
3932 return i;
3933 }
3934 }
3935 return -1;
3936}
3937
3938/*****************************************************************************/
3939
3940/*
3941 * Probe around and try to find any EISA boards in system. The biggest
3942 * problem here is finding out what memory address is associated with
3943 * an EISA board after it is found. The registers of the ECPE and
3944 * ONboardE are not readable - so we can't read them from there. We
3945 * don't have access to the EISA CMOS (or EISA BIOS) so we don't
3946 * actually have any way to find out the real value. The best we can
3947 * do is go probing around in the usual places hoping we can find it.
3948 */
3949
3950static int stli_findeisabrds(void)
3951{
Alan Cox4ac43602006-06-28 04:26:52 -07003952 stlibrd_t *brdp;
3953 unsigned int iobase, eid;
3954 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955
3956/*
Alan Cox4ac43602006-06-28 04:26:52 -07003957 * Firstly check if this is an EISA system. If this is not an EISA system then
Linus Torvalds1da177e2005-04-16 15:20:36 -07003958 * don't bother going any further!
3959 */
Alan Cox4ac43602006-06-28 04:26:52 -07003960 if (EISA_bus)
3961 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003962
3963/*
3964 * Looks like an EISA system, so go searching for EISA boards.
3965 */
3966 for (iobase = 0x1000; (iobase <= 0xc000); iobase += 0x1000) {
3967 outb(0xff, (iobase + 0xc80));
3968 eid = inb(iobase + 0xc80);
3969 eid |= inb(iobase + 0xc81) << 8;
3970 if (eid != STL_EISAID)
3971 continue;
3972
3973/*
3974 * We have found a board. Need to check if this board was
3975 * statically configured already (just in case!).
3976 */
3977 for (i = 0; (i < STL_MAXBRDS); i++) {
3978 brdp = stli_brds[i];
Alan Cox4ac43602006-06-28 04:26:52 -07003979 if (brdp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003980 continue;
3981 if (brdp->iobase == iobase)
3982 break;
3983 }
3984 if (i < STL_MAXBRDS)
3985 continue;
3986
3987/*
3988 * We have found a Stallion board and it is not configured already.
3989 * Allocate a board structure and initialize it.
3990 */
Alan Cox4ac43602006-06-28 04:26:52 -07003991 if ((brdp = stli_allocbrd()) == NULL)
3992 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003993 if ((brdp->brdnr = stli_getbrdnr()) < 0)
Alan Cox4ac43602006-06-28 04:26:52 -07003994 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003995 eid = inb(iobase + 0xc82);
3996 if (eid == ECP_EISAID)
3997 brdp->brdtype = BRD_ECPE;
3998 else if (eid == ONB_EISAID)
3999 brdp->brdtype = BRD_ONBOARDE;
4000 else
4001 brdp->brdtype = BRD_UNKNOWN;
4002 brdp->iobase = iobase;
4003 outb(0x1, (iobase + 0xc84));
4004 if (stli_eisamemprobe(brdp))
4005 outb(0, (iobase + 0xc84));
4006 stli_brdinit(brdp);
4007 }
4008
Alan Cox4ac43602006-06-28 04:26:52 -07004009 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010}
4011
4012/*****************************************************************************/
4013
4014/*
4015 * Find the next available board number that is free.
4016 */
4017
4018/*****************************************************************************/
4019
4020#ifdef CONFIG_PCI
4021
4022/*
4023 * We have a Stallion board. Allocate a board structure and
4024 * initialize it. Read its IO and MEMORY resources from PCI
4025 * configuration space.
4026 */
4027
4028static int stli_initpcibrd(int brdtype, struct pci_dev *devp)
4029{
Alan Cox4ac43602006-06-28 04:26:52 -07004030 stlibrd_t *brdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031
4032 if (pci_enable_device(devp))
Alan Cox4ac43602006-06-28 04:26:52 -07004033 return -EIO;
4034 if ((brdp = stli_allocbrd()) == NULL)
4035 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004036 if ((brdp->brdnr = stli_getbrdnr()) < 0) {
4037 printk(KERN_INFO "STALLION: too many boards found, "
4038 "maximum supported %d\n", STL_MAXBRDS);
Alan Cox4ac43602006-06-28 04:26:52 -07004039 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004040 }
4041 brdp->brdtype = brdtype;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004042/*
4043 * We have all resources from the board, so lets setup the actual
4044 * board structure now.
4045 */
4046 brdp->iobase = pci_resource_start(devp, 3);
4047 brdp->memaddr = pci_resource_start(devp, 2);
4048 stli_brdinit(brdp);
4049
Alan Cox4ac43602006-06-28 04:26:52 -07004050 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004051}
4052
4053/*****************************************************************************/
4054
4055/*
4056 * Find all Stallion PCI boards that might be installed. Initialize each
4057 * one as it is found.
4058 */
4059
4060static int stli_findpcibrds(void)
4061{
Alan Cox4ac43602006-06-28 04:26:52 -07004062 struct pci_dev *dev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004063
Alan Cox4ac43602006-06-28 04:26:52 -07004064 while ((dev = pci_get_device(PCI_VENDOR_ID_STALLION, PCI_DEVICE_ID_ECRA, dev))) {
4065 stli_initpcibrd(BRD_ECPPCI, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004066 }
Alan Cox4ac43602006-06-28 04:26:52 -07004067 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004068}
4069
4070#endif
4071
4072/*****************************************************************************/
4073
4074/*
4075 * Allocate a new board structure. Fill out the basic info in it.
4076 */
4077
4078static stlibrd_t *stli_allocbrd(void)
4079{
Alan Cox4ac43602006-06-28 04:26:52 -07004080 stlibrd_t *brdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004081
Tobias Klauserb0b4ed72006-03-31 02:30:56 -08004082 brdp = kzalloc(sizeof(stlibrd_t), GFP_KERNEL);
4083 if (!brdp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004084 printk(KERN_ERR "STALLION: failed to allocate memory "
Alan Cox4ac43602006-06-28 04:26:52 -07004085 "(size=%Zd)\n", sizeof(stlibrd_t));
Tobias Klauserb0b4ed72006-03-31 02:30:56 -08004086 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004087 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004088 brdp->magic = STLI_BOARDMAGIC;
Alan Cox4ac43602006-06-28 04:26:52 -07004089 return brdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004090}
4091
4092/*****************************************************************************/
4093
4094/*
4095 * Scan through all the boards in the configuration and see what we
4096 * can find.
4097 */
4098
4099static int stli_initbrds(void)
4100{
Alan Cox4ac43602006-06-28 04:26:52 -07004101 stlibrd_t *brdp, *nxtbrdp;
4102 stlconf_t *confp;
4103 int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004104
4105 if (stli_nrbrds > STL_MAXBRDS) {
4106 printk(KERN_INFO "STALLION: too many boards in configuration "
4107 "table, truncating to %d\n", STL_MAXBRDS);
4108 stli_nrbrds = STL_MAXBRDS;
4109 }
4110
4111/*
4112 * Firstly scan the list of static boards configured. Allocate
4113 * resources and initialize the boards as found. If this is a
4114 * module then let the module args override static configuration.
4115 */
4116 for (i = 0; (i < stli_nrbrds); i++) {
4117 confp = &stli_brdconf[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118 stli_parsebrd(confp, stli_brdsp[i]);
Alan Cox4ac43602006-06-28 04:26:52 -07004119 if ((brdp = stli_allocbrd()) == NULL)
4120 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004121 brdp->brdnr = i;
4122 brdp->brdtype = confp->brdtype;
4123 brdp->iobase = confp->ioaddr1;
4124 brdp->memaddr = confp->memaddr;
4125 stli_brdinit(brdp);
4126 }
4127
4128/*
4129 * Static configuration table done, so now use dynamic methods to
4130 * see if any more boards should be configured.
4131 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004132 stli_argbrds();
Adrian Bunkdbc6b5f2005-06-25 14:59:03 -07004133 if (STLI_EISAPROBE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004134 stli_findeisabrds();
4135#ifdef CONFIG_PCI
4136 stli_findpcibrds();
4137#endif
4138
4139/*
4140 * All found boards are initialized. Now for a little optimization, if
4141 * no boards are sharing the "shared memory" regions then we can just
4142 * leave them all enabled. This is in fact the usual case.
4143 */
4144 stli_shared = 0;
4145 if (stli_nrbrds > 1) {
4146 for (i = 0; (i < stli_nrbrds); i++) {
4147 brdp = stli_brds[i];
Alan Cox4ac43602006-06-28 04:26:52 -07004148 if (brdp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004149 continue;
4150 for (j = i + 1; (j < stli_nrbrds); j++) {
4151 nxtbrdp = stli_brds[j];
Alan Cox4ac43602006-06-28 04:26:52 -07004152 if (nxtbrdp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153 continue;
4154 if ((brdp->membase >= nxtbrdp->membase) &&
4155 (brdp->membase <= (nxtbrdp->membase +
4156 nxtbrdp->memsize - 1))) {
4157 stli_shared++;
4158 break;
4159 }
4160 }
4161 }
4162 }
4163
4164 if (stli_shared == 0) {
4165 for (i = 0; (i < stli_nrbrds); i++) {
4166 brdp = stli_brds[i];
Alan Cox4ac43602006-06-28 04:26:52 -07004167 if (brdp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004168 continue;
4169 if (brdp->state & BST_FOUND) {
4170 EBRDENABLE(brdp);
4171 brdp->enable = NULL;
4172 brdp->disable = NULL;
4173 }
4174 }
4175 }
4176
Alan Cox4ac43602006-06-28 04:26:52 -07004177 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004178}
4179
4180/*****************************************************************************/
4181
4182/*
4183 * Code to handle an "staliomem" read operation. This device is the
4184 * contents of the board shared memory. It is used for down loading
4185 * the slave image (and debugging :-)
4186 */
4187
4188static ssize_t stli_memread(struct file *fp, char __user *buf, size_t count, loff_t *offp)
4189{
Alan Cox4ac43602006-06-28 04:26:52 -07004190 unsigned long flags;
Al Viro29756fa2006-10-10 22:47:27 +01004191 void __iomem *memptr;
Alan Cox4ac43602006-06-28 04:26:52 -07004192 stlibrd_t *brdp;
4193 int brdnr, size, n;
4194 void *p;
4195 loff_t off = *offp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004196
4197 brdnr = iminor(fp->f_dentry->d_inode);
4198 if (brdnr >= stli_nrbrds)
Alan Cox4ac43602006-06-28 04:26:52 -07004199 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004200 brdp = stli_brds[brdnr];
Alan Cox4ac43602006-06-28 04:26:52 -07004201 if (brdp == NULL)
4202 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203 if (brdp->state == 0)
Alan Cox4ac43602006-06-28 04:26:52 -07004204 return -ENODEV;
4205 if (off >= brdp->memsize || off + count < off)
4206 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004207
Alan Cox4ac43602006-06-28 04:26:52 -07004208 size = MIN(count, (brdp->memsize - off));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004209
Alan Cox4ac43602006-06-28 04:26:52 -07004210 /*
4211 * Copy the data a page at a time
4212 */
4213
4214 p = (void *)__get_free_page(GFP_KERNEL);
4215 if(p == NULL)
4216 return -ENOMEM;
4217
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218 while (size > 0) {
Alan Cox4ac43602006-06-28 04:26:52 -07004219 spin_lock_irqsave(&brd_lock, flags);
4220 EBRDENABLE(brdp);
Al Viro29756fa2006-10-10 22:47:27 +01004221 memptr = EBRDGETMEMPTR(brdp, off);
Alan Cox4ac43602006-06-28 04:26:52 -07004222 n = MIN(size, (brdp->pagesize - (((unsigned long) off) % brdp->pagesize)));
4223 n = MIN(n, PAGE_SIZE);
4224 memcpy_fromio(p, memptr, n);
4225 EBRDDISABLE(brdp);
4226 spin_unlock_irqrestore(&brd_lock, flags);
4227 if (copy_to_user(buf, p, n)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004228 count = -EFAULT;
4229 goto out;
4230 }
Alan Cox4ac43602006-06-28 04:26:52 -07004231 off += n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232 buf += n;
4233 size -= n;
4234 }
4235out:
Alan Cox4ac43602006-06-28 04:26:52 -07004236 *offp = off;
4237 free_page((unsigned long)p);
4238 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004239}
4240
4241/*****************************************************************************/
4242
4243/*
4244 * Code to handle an "staliomem" write operation. This device is the
4245 * contents of the board shared memory. It is used for down loading
4246 * the slave image (and debugging :-)
Alan Cox4ac43602006-06-28 04:26:52 -07004247 *
4248 * FIXME: copy under lock
Linus Torvalds1da177e2005-04-16 15:20:36 -07004249 */
4250
4251static ssize_t stli_memwrite(struct file *fp, const char __user *buf, size_t count, loff_t *offp)
4252{
Alan Cox4ac43602006-06-28 04:26:52 -07004253 unsigned long flags;
Al Viro29756fa2006-10-10 22:47:27 +01004254 void __iomem *memptr;
Alan Cox4ac43602006-06-28 04:26:52 -07004255 stlibrd_t *brdp;
4256 char __user *chbuf;
4257 int brdnr, size, n;
4258 void *p;
4259 loff_t off = *offp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004260
4261 brdnr = iminor(fp->f_dentry->d_inode);
Alan Cox4ac43602006-06-28 04:26:52 -07004262
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263 if (brdnr >= stli_nrbrds)
Alan Cox4ac43602006-06-28 04:26:52 -07004264 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004265 brdp = stli_brds[brdnr];
Alan Cox4ac43602006-06-28 04:26:52 -07004266 if (brdp == NULL)
4267 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004268 if (brdp->state == 0)
Alan Cox4ac43602006-06-28 04:26:52 -07004269 return -ENODEV;
4270 if (off >= brdp->memsize || off + count < off)
4271 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004272
4273 chbuf = (char __user *) buf;
Alan Cox4ac43602006-06-28 04:26:52 -07004274 size = MIN(count, (brdp->memsize - off));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275
Alan Cox4ac43602006-06-28 04:26:52 -07004276 /*
4277 * Copy the data a page at a time
4278 */
4279
4280 p = (void *)__get_free_page(GFP_KERNEL);
4281 if(p == NULL)
4282 return -ENOMEM;
4283
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284 while (size > 0) {
Alan Cox4ac43602006-06-28 04:26:52 -07004285 n = MIN(size, (brdp->pagesize - (((unsigned long) off) % brdp->pagesize)));
4286 n = MIN(n, PAGE_SIZE);
4287 if (copy_from_user(p, chbuf, n)) {
4288 if (count == 0)
4289 count = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004290 goto out;
4291 }
Alan Cox4ac43602006-06-28 04:26:52 -07004292 spin_lock_irqsave(&brd_lock, flags);
4293 EBRDENABLE(brdp);
Al Viro29756fa2006-10-10 22:47:27 +01004294 memptr = EBRDGETMEMPTR(brdp, off);
Alan Cox4ac43602006-06-28 04:26:52 -07004295 memcpy_toio(memptr, p, n);
4296 EBRDDISABLE(brdp);
4297 spin_unlock_irqrestore(&brd_lock, flags);
4298 off += n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004299 chbuf += n;
4300 size -= n;
4301 }
4302out:
Alan Cox4ac43602006-06-28 04:26:52 -07004303 free_page((unsigned long) p);
4304 *offp = off;
4305 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004306}
4307
4308/*****************************************************************************/
4309
4310/*
4311 * Return the board stats structure to user app.
4312 */
4313
4314static int stli_getbrdstats(combrd_t __user *bp)
4315{
Alan Cox4ac43602006-06-28 04:26:52 -07004316 stlibrd_t *brdp;
4317 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004318
4319 if (copy_from_user(&stli_brdstats, bp, sizeof(combrd_t)))
4320 return -EFAULT;
4321 if (stli_brdstats.brd >= STL_MAXBRDS)
Alan Cox4ac43602006-06-28 04:26:52 -07004322 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004323 brdp = stli_brds[stli_brdstats.brd];
Alan Cox4ac43602006-06-28 04:26:52 -07004324 if (brdp == NULL)
4325 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004326
4327 memset(&stli_brdstats, 0, sizeof(combrd_t));
4328 stli_brdstats.brd = brdp->brdnr;
4329 stli_brdstats.type = brdp->brdtype;
4330 stli_brdstats.hwid = 0;
4331 stli_brdstats.state = brdp->state;
4332 stli_brdstats.ioaddr = brdp->iobase;
4333 stli_brdstats.memaddr = brdp->memaddr;
4334 stli_brdstats.nrpanels = brdp->nrpanels;
4335 stli_brdstats.nrports = brdp->nrports;
4336 for (i = 0; (i < brdp->nrpanels); i++) {
4337 stli_brdstats.panels[i].panel = i;
4338 stli_brdstats.panels[i].hwid = brdp->panelids[i];
4339 stli_brdstats.panels[i].nrports = brdp->panels[i];
4340 }
4341
4342 if (copy_to_user(bp, &stli_brdstats, sizeof(combrd_t)))
4343 return -EFAULT;
Alan Cox4ac43602006-06-28 04:26:52 -07004344 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345}
4346
4347/*****************************************************************************/
4348
4349/*
4350 * Resolve the referenced port number into a port struct pointer.
4351 */
4352
4353static stliport_t *stli_getport(int brdnr, int panelnr, int portnr)
4354{
Alan Cox4ac43602006-06-28 04:26:52 -07004355 stlibrd_t *brdp;
4356 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004357
Alan Cox4ac43602006-06-28 04:26:52 -07004358 if (brdnr < 0 || brdnr >= STL_MAXBRDS)
4359 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004360 brdp = stli_brds[brdnr];
Alan Cox4ac43602006-06-28 04:26:52 -07004361 if (brdp == NULL)
4362 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004363 for (i = 0; (i < panelnr); i++)
4364 portnr += brdp->panels[i];
4365 if ((portnr < 0) || (portnr >= brdp->nrports))
Alan Cox4ac43602006-06-28 04:26:52 -07004366 return NULL;
4367 return brdp->ports[portnr];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004368}
4369
4370/*****************************************************************************/
4371
4372/*
4373 * Return the port stats structure to user app. A NULL port struct
4374 * pointer passed in means that we need to find out from the app
4375 * what port to get stats for (used through board control device).
4376 */
4377
4378static int stli_portcmdstats(stliport_t *portp)
4379{
4380 unsigned long flags;
4381 stlibrd_t *brdp;
4382 int rc;
4383
4384 memset(&stli_comstats, 0, sizeof(comstats_t));
4385
Alan Cox4ac43602006-06-28 04:26:52 -07004386 if (portp == NULL)
4387 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004388 brdp = stli_brds[portp->brdnr];
Alan Cox4ac43602006-06-28 04:26:52 -07004389 if (brdp == NULL)
4390 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004391
4392 if (brdp->state & BST_STARTED) {
4393 if ((rc = stli_cmdwait(brdp, portp, A_GETSTATS,
4394 &stli_cdkstats, sizeof(asystats_t), 1)) < 0)
Alan Cox4ac43602006-06-28 04:26:52 -07004395 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004396 } else {
4397 memset(&stli_cdkstats, 0, sizeof(asystats_t));
4398 }
4399
4400 stli_comstats.brd = portp->brdnr;
4401 stli_comstats.panel = portp->panelnr;
4402 stli_comstats.port = portp->portnr;
4403 stli_comstats.state = portp->state;
4404 stli_comstats.flags = portp->flags;
4405
Alan Cox4ac43602006-06-28 04:26:52 -07004406 spin_lock_irqsave(&brd_lock, flags);
4407 if (portp->tty != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004408 if (portp->tty->driver_data == portp) {
4409 stli_comstats.ttystate = portp->tty->flags;
Alan Cox4ac43602006-06-28 04:26:52 -07004410 stli_comstats.rxbuffered = -1;
4411 if (portp->tty->termios != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004412 stli_comstats.cflags = portp->tty->termios->c_cflag;
4413 stli_comstats.iflags = portp->tty->termios->c_iflag;
4414 stli_comstats.oflags = portp->tty->termios->c_oflag;
4415 stli_comstats.lflags = portp->tty->termios->c_lflag;
4416 }
4417 }
4418 }
Alan Cox4ac43602006-06-28 04:26:52 -07004419 spin_unlock_irqrestore(&brd_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004420
4421 stli_comstats.txtotal = stli_cdkstats.txchars;
4422 stli_comstats.rxtotal = stli_cdkstats.rxchars + stli_cdkstats.ringover;
4423 stli_comstats.txbuffered = stli_cdkstats.txringq;
4424 stli_comstats.rxbuffered += stli_cdkstats.rxringq;
4425 stli_comstats.rxoverrun = stli_cdkstats.overruns;
4426 stli_comstats.rxparity = stli_cdkstats.parity;
4427 stli_comstats.rxframing = stli_cdkstats.framing;
4428 stli_comstats.rxlost = stli_cdkstats.ringover;
4429 stli_comstats.rxbreaks = stli_cdkstats.rxbreaks;
4430 stli_comstats.txbreaks = stli_cdkstats.txbreaks;
4431 stli_comstats.txxon = stli_cdkstats.txstart;
4432 stli_comstats.txxoff = stli_cdkstats.txstop;
4433 stli_comstats.rxxon = stli_cdkstats.rxstart;
4434 stli_comstats.rxxoff = stli_cdkstats.rxstop;
4435 stli_comstats.rxrtsoff = stli_cdkstats.rtscnt / 2;
4436 stli_comstats.rxrtson = stli_cdkstats.rtscnt - stli_comstats.rxrtsoff;
4437 stli_comstats.modem = stli_cdkstats.dcdcnt;
4438 stli_comstats.hwid = stli_cdkstats.hwid;
4439 stli_comstats.signals = stli_mktiocm(stli_cdkstats.signals);
4440
Alan Cox4ac43602006-06-28 04:26:52 -07004441 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004442}
4443
4444/*****************************************************************************/
4445
4446/*
4447 * Return the port stats structure to user app. A NULL port struct
4448 * pointer passed in means that we need to find out from the app
4449 * what port to get stats for (used through board control device).
4450 */
4451
4452static int stli_getportstats(stliport_t *portp, comstats_t __user *cp)
4453{
Alan Cox4ac43602006-06-28 04:26:52 -07004454 stlibrd_t *brdp;
4455 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004456
4457 if (!portp) {
4458 if (copy_from_user(&stli_comstats, cp, sizeof(comstats_t)))
4459 return -EFAULT;
4460 portp = stli_getport(stli_comstats.brd, stli_comstats.panel,
4461 stli_comstats.port);
4462 if (!portp)
4463 return -ENODEV;
4464 }
4465
4466 brdp = stli_brds[portp->brdnr];
4467 if (!brdp)
4468 return -ENODEV;
4469
4470 if ((rc = stli_portcmdstats(portp)) < 0)
4471 return rc;
4472
4473 return copy_to_user(cp, &stli_comstats, sizeof(comstats_t)) ?
4474 -EFAULT : 0;
4475}
4476
4477/*****************************************************************************/
4478
4479/*
4480 * Clear the port stats structure. We also return it zeroed out...
4481 */
4482
4483static int stli_clrportstats(stliport_t *portp, comstats_t __user *cp)
4484{
Alan Cox4ac43602006-06-28 04:26:52 -07004485 stlibrd_t *brdp;
4486 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004487
4488 if (!portp) {
4489 if (copy_from_user(&stli_comstats, cp, sizeof(comstats_t)))
4490 return -EFAULT;
4491 portp = stli_getport(stli_comstats.brd, stli_comstats.panel,
4492 stli_comstats.port);
4493 if (!portp)
4494 return -ENODEV;
4495 }
4496
4497 brdp = stli_brds[portp->brdnr];
4498 if (!brdp)
4499 return -ENODEV;
4500
4501 if (brdp->state & BST_STARTED) {
4502 if ((rc = stli_cmdwait(brdp, portp, A_CLEARSTATS, NULL, 0, 0)) < 0)
4503 return rc;
4504 }
4505
4506 memset(&stli_comstats, 0, sizeof(comstats_t));
4507 stli_comstats.brd = portp->brdnr;
4508 stli_comstats.panel = portp->panelnr;
4509 stli_comstats.port = portp->portnr;
4510
4511 if (copy_to_user(cp, &stli_comstats, sizeof(comstats_t)))
4512 return -EFAULT;
4513 return 0;
4514}
4515
4516/*****************************************************************************/
4517
4518/*
4519 * Return the entire driver ports structure to a user app.
4520 */
4521
4522static int stli_getportstruct(stliport_t __user *arg)
4523{
Alan Cox4ac43602006-06-28 04:26:52 -07004524 stliport_t *portp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004525
4526 if (copy_from_user(&stli_dummyport, arg, sizeof(stliport_t)))
4527 return -EFAULT;
4528 portp = stli_getport(stli_dummyport.brdnr, stli_dummyport.panelnr,
4529 stli_dummyport.portnr);
4530 if (!portp)
4531 return -ENODEV;
4532 if (copy_to_user(arg, portp, sizeof(stliport_t)))
4533 return -EFAULT;
4534 return 0;
4535}
4536
4537/*****************************************************************************/
4538
4539/*
4540 * Return the entire driver board structure to a user app.
4541 */
4542
4543static int stli_getbrdstruct(stlibrd_t __user *arg)
4544{
Alan Cox4ac43602006-06-28 04:26:52 -07004545 stlibrd_t *brdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004546
4547 if (copy_from_user(&stli_dummybrd, arg, sizeof(stlibrd_t)))
4548 return -EFAULT;
4549 if ((stli_dummybrd.brdnr < 0) || (stli_dummybrd.brdnr >= STL_MAXBRDS))
4550 return -ENODEV;
4551 brdp = stli_brds[stli_dummybrd.brdnr];
4552 if (!brdp)
4553 return -ENODEV;
4554 if (copy_to_user(arg, brdp, sizeof(stlibrd_t)))
4555 return -EFAULT;
4556 return 0;
4557}
4558
4559/*****************************************************************************/
4560
4561/*
4562 * The "staliomem" device is also required to do some special operations on
4563 * the board. We need to be able to send an interrupt to the board,
4564 * reset it, and start/stop it.
4565 */
4566
4567static int stli_memioctl(struct inode *ip, struct file *fp, unsigned int cmd, unsigned long arg)
4568{
Alan Cox4ac43602006-06-28 04:26:52 -07004569 stlibrd_t *brdp;
4570 int brdnr, rc, done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004571 void __user *argp = (void __user *)arg;
4572
Linus Torvalds1da177e2005-04-16 15:20:36 -07004573/*
4574 * First up handle the board independent ioctls.
4575 */
4576 done = 0;
4577 rc = 0;
4578
4579 switch (cmd) {
4580 case COM_GETPORTSTATS:
4581 rc = stli_getportstats(NULL, argp);
4582 done++;
4583 break;
4584 case COM_CLRPORTSTATS:
4585 rc = stli_clrportstats(NULL, argp);
4586 done++;
4587 break;
4588 case COM_GETBRDSTATS:
4589 rc = stli_getbrdstats(argp);
4590 done++;
4591 break;
4592 case COM_READPORT:
4593 rc = stli_getportstruct(argp);
4594 done++;
4595 break;
4596 case COM_READBOARD:
4597 rc = stli_getbrdstruct(argp);
4598 done++;
4599 break;
4600 }
4601
4602 if (done)
Alan Cox4ac43602006-06-28 04:26:52 -07004603 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004604
4605/*
4606 * Now handle the board specific ioctls. These all depend on the
4607 * minor number of the device they were called from.
4608 */
4609 brdnr = iminor(ip);
4610 if (brdnr >= STL_MAXBRDS)
Alan Cox4ac43602006-06-28 04:26:52 -07004611 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004612 brdp = stli_brds[brdnr];
4613 if (!brdp)
Alan Cox4ac43602006-06-28 04:26:52 -07004614 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004615 if (brdp->state == 0)
Alan Cox4ac43602006-06-28 04:26:52 -07004616 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004617
4618 switch (cmd) {
4619 case STL_BINTR:
4620 EBRDINTR(brdp);
4621 break;
4622 case STL_BSTART:
4623 rc = stli_startbrd(brdp);
4624 break;
4625 case STL_BSTOP:
4626 brdp->state &= ~BST_STARTED;
4627 break;
4628 case STL_BRESET:
4629 brdp->state &= ~BST_STARTED;
4630 EBRDRESET(brdp);
4631 if (stli_shared == 0) {
4632 if (brdp->reenable != NULL)
4633 (* brdp->reenable)(brdp);
4634 }
4635 break;
4636 default:
4637 rc = -ENOIOCTLCMD;
4638 break;
4639 }
Alan Cox4ac43602006-06-28 04:26:52 -07004640 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004641}
4642
Jeff Dikeb68e31d2006-10-02 02:17:18 -07004643static const struct tty_operations stli_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004644 .open = stli_open,
4645 .close = stli_close,
4646 .write = stli_write,
4647 .put_char = stli_putchar,
4648 .flush_chars = stli_flushchars,
4649 .write_room = stli_writeroom,
4650 .chars_in_buffer = stli_charsinbuffer,
4651 .ioctl = stli_ioctl,
4652 .set_termios = stli_settermios,
4653 .throttle = stli_throttle,
4654 .unthrottle = stli_unthrottle,
4655 .stop = stli_stop,
4656 .start = stli_start,
4657 .hangup = stli_hangup,
4658 .flush_buffer = stli_flushbuffer,
4659 .break_ctl = stli_breakctl,
4660 .wait_until_sent = stli_waituntilsent,
4661 .send_xchar = stli_sendxchar,
4662 .read_proc = stli_readproc,
4663 .tiocmget = stli_tiocmget,
4664 .tiocmset = stli_tiocmset,
4665};
4666
4667/*****************************************************************************/
4668
Adrian Bunk672b2712006-06-30 01:55:30 -07004669static int __init stli_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004670{
4671 int i;
4672 printk(KERN_INFO "%s: version %s\n", stli_drvtitle, stli_drvversion);
4673
Alan Cox4ac43602006-06-28 04:26:52 -07004674 spin_lock_init(&stli_lock);
4675 spin_lock_init(&brd_lock);
4676
Linus Torvalds1da177e2005-04-16 15:20:36 -07004677 stli_initbrds();
4678
4679 stli_serial = alloc_tty_driver(STL_MAXBRDS * STL_MAXPORTS);
4680 if (!stli_serial)
4681 return -ENOMEM;
4682
4683/*
4684 * Allocate a temporary write buffer.
4685 */
Tobias Klauserb0b4ed72006-03-31 02:30:56 -08004686 stli_txcookbuf = kmalloc(STLI_TXBUFSIZE, GFP_KERNEL);
4687 if (!stli_txcookbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004688 printk(KERN_ERR "STALLION: failed to allocate memory "
4689 "(size=%d)\n", STLI_TXBUFSIZE);
4690
4691/*
4692 * Set up a character driver for the shared memory region. We need this
4693 * to down load the slave code image. Also it is a useful debugging tool.
4694 */
4695 if (register_chrdev(STL_SIOMEMMAJOR, "staliomem", &stli_fsiomem))
4696 printk(KERN_ERR "STALLION: failed to register serial memory "
4697 "device\n");
4698
gregkh@suse.deca8eca62005-03-23 09:53:09 -08004699 istallion_class = class_create(THIS_MODULE, "staliomem");
Greg Kroah-Hartman7c69ef72005-06-20 21:15:16 -07004700 for (i = 0; i < 4; i++)
Greg Kroah-Hartman53f46542005-10-27 22:25:43 -07004701 class_device_create(istallion_class, NULL,
4702 MKDEV(STL_SIOMEMMAJOR, i),
Linus Torvalds1da177e2005-04-16 15:20:36 -07004703 NULL, "staliomem%d", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004704
4705/*
4706 * Set up the tty driver structure and register us as a driver.
4707 */
4708 stli_serial->owner = THIS_MODULE;
4709 stli_serial->driver_name = stli_drvname;
4710 stli_serial->name = stli_serialname;
4711 stli_serial->major = STL_SERIALMAJOR;
4712 stli_serial->minor_start = 0;
4713 stli_serial->type = TTY_DRIVER_TYPE_SERIAL;
4714 stli_serial->subtype = SERIAL_TYPE_NORMAL;
4715 stli_serial->init_termios = stli_deftermios;
4716 stli_serial->flags = TTY_DRIVER_REAL_RAW;
4717 tty_set_operations(stli_serial, &stli_ops);
4718
4719 if (tty_register_driver(stli_serial)) {
4720 put_tty_driver(stli_serial);
4721 printk(KERN_ERR "STALLION: failed to register serial driver\n");
4722 return -EBUSY;
4723 }
Alan Cox4ac43602006-06-28 04:26:52 -07004724 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004725}
4726
4727/*****************************************************************************/