blob: 216c79256de3e36566bc3bb2df61d9c7c5a6b269 [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
29#include <linux/config.h>
30#include <linux/module.h>
31#include <linux/slab.h>
32#include <linux/interrupt.h>
33#include <linux/tty.h>
34#include <linux/tty_flip.h>
35#include <linux/serial.h>
36#include <linux/cdk.h>
37#include <linux/comstats.h>
38#include <linux/istallion.h>
39#include <linux/ioport.h>
40#include <linux/delay.h>
41#include <linux/init.h>
42#include <linux/devfs_fs_kernel.h>
43#include <linux/device.h>
44#include <linux/wait.h>
Alan Cox4ac43602006-06-28 04:26:52 -070045#include <linux/eisa.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47#include <asm/io.h>
48#include <asm/uaccess.h>
49
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <linux/pci.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52/*****************************************************************************/
53
54/*
55 * Define different board types. Not all of the following board types
56 * are supported by this driver. But I will use the standard "assigned"
57 * board numbers. Currently supported boards are abbreviated as:
58 * ECP = EasyConnection 8/64, ONB = ONboard, BBY = Brumby and
59 * STAL = Stallion.
60 */
61#define BRD_UNKNOWN 0
62#define BRD_STALLION 1
63#define BRD_BRUMBY4 2
64#define BRD_ONBOARD2 3
65#define BRD_ONBOARD 4
66#define BRD_BRUMBY8 5
67#define BRD_BRUMBY16 6
68#define BRD_ONBOARDE 7
69#define BRD_ONBOARD32 9
70#define BRD_ONBOARD2_32 10
71#define BRD_ONBOARDRS 11
72#define BRD_EASYIO 20
73#define BRD_ECH 21
74#define BRD_ECHMC 22
75#define BRD_ECP 23
76#define BRD_ECPE 24
77#define BRD_ECPMC 25
78#define BRD_ECHPCI 26
79#define BRD_ECH64PCI 27
80#define BRD_EASYIOPCI 28
81#define BRD_ECPPCI 29
82
83#define BRD_BRUMBY BRD_BRUMBY4
84
85/*
86 * Define a configuration structure to hold the board configuration.
87 * Need to set this up in the code (for now) with the boards that are
88 * to be configured into the system. This is what needs to be modified
89 * when adding/removing/modifying boards. Each line entry in the
90 * stli_brdconf[] array is a board. Each line contains io/irq/memory
91 * ranges for that board (as well as what type of board it is).
92 * Some examples:
93 * { BRD_ECP, 0x2a0, 0, 0xcc000, 0, 0 },
94 * This line will configure an EasyConnection 8/64 at io address 2a0,
95 * and shared memory address of cc000. Multiple EasyConnection 8/64
96 * boards can share the same shared memory address space. No interrupt
97 * is required for this board type.
98 * Another example:
99 * { BRD_ECPE, 0x5000, 0, 0x80000000, 0, 0 },
100 * This line will configure an EasyConnection 8/64 EISA in slot 5 and
101 * shared memory address of 0x80000000 (2 GByte). Multiple
102 * EasyConnection 8/64 EISA boards can share the same shared memory
103 * address space. No interrupt is required for this board type.
104 * Another example:
105 * { BRD_ONBOARD, 0x240, 0, 0xd0000, 0, 0 },
106 * This line will configure an ONboard (ISA type) at io address 240,
107 * and shared memory address of d0000. Multiple ONboards can share
108 * the same shared memory address space. No interrupt required.
109 * Another example:
110 * { BRD_BRUMBY4, 0x360, 0, 0xc8000, 0, 0 },
111 * This line will configure a Brumby board (any number of ports!) at
112 * io address 360 and shared memory address of c8000. All Brumby boards
113 * configured into a system must have their own separate io and memory
114 * addresses. No interrupt is required.
115 * Another example:
116 * { BRD_STALLION, 0x330, 0, 0xd0000, 0, 0 },
117 * This line will configure an original Stallion board at io address 330
118 * and shared memory address d0000 (this would only be valid for a "V4.0"
119 * or Rev.O Stallion board). All Stallion boards configured into the
120 * system must have their own separate io and memory addresses. No
121 * interrupt is required.
122 */
123
124typedef struct {
125 int brdtype;
126 int ioaddr1;
127 int ioaddr2;
128 unsigned long memaddr;
129 int irq;
130 int irqtype;
131} stlconf_t;
132
133static stlconf_t stli_brdconf[] = {
134 /*{ BRD_ECP, 0x2a0, 0, 0xcc000, 0, 0 },*/
135};
136
Tobias Klauserfe971072006-01-09 20:54:02 -0800137static int stli_nrbrds = ARRAY_SIZE(stli_brdconf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Alan Cox4ac43602006-06-28 04:26:52 -0700139/* stli_lock must NOT be taken holding brd_lock */
140static spinlock_t stli_lock; /* TTY logic lock */
141static spinlock_t brd_lock; /* Board logic lock */
142
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143/*
144 * There is some experimental EISA board detection code in this driver.
145 * By default it is disabled, but for those that want to try it out,
146 * then set the define below to be 1.
147 */
148#define STLI_EISAPROBE 0
149
150/*****************************************************************************/
151
152/*
153 * Define some important driver characteristics. Device major numbers
154 * allocated as per Linux Device Registry.
155 */
156#ifndef STL_SIOMEMMAJOR
157#define STL_SIOMEMMAJOR 28
158#endif
159#ifndef STL_SERIALMAJOR
160#define STL_SERIALMAJOR 24
161#endif
162#ifndef STL_CALLOUTMAJOR
163#define STL_CALLOUTMAJOR 25
164#endif
165
166/*****************************************************************************/
167
168/*
169 * Define our local driver identity first. Set up stuff to deal with
170 * all the local structures required by a serial tty driver.
171 */
172static char *stli_drvtitle = "Stallion Intelligent Multiport Serial Driver";
173static char *stli_drvname = "istallion";
174static char *stli_drvversion = "5.6.0";
175static char *stli_serialname = "ttyE";
176
177static struct tty_driver *stli_serial;
178
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
180#define STLI_TXBUFSIZE 4096
181
182/*
183 * Use a fast local buffer for cooked characters. Typically a whole
184 * bunch of cooked characters come in for a port, 1 at a time. So we
185 * save those up into a local buffer, then write out the whole lot
186 * with a large memcpy. Just use 1 buffer for all ports, since its
187 * use it is only need for short periods of time by each port.
188 */
189static char *stli_txcookbuf;
190static int stli_txcooksize;
191static int stli_txcookrealsize;
192static struct tty_struct *stli_txcooktty;
193
194/*
195 * Define a local default termios struct. All ports will be created
196 * with this termios initially. Basically all it defines is a raw port
197 * at 9600 baud, 8 data bits, no parity, 1 stop bit.
198 */
199static struct termios stli_deftermios = {
200 .c_cflag = (B9600 | CS8 | CREAD | HUPCL | CLOCAL),
201 .c_cc = INIT_C_CC,
202};
203
204/*
205 * Define global stats structures. Not used often, and can be
206 * re-used for each stats call.
207 */
208static comstats_t stli_comstats;
209static combrd_t stli_brdstats;
210static asystats_t stli_cdkstats;
211static stlibrd_t stli_dummybrd;
212static stliport_t stli_dummyport;
213
214/*****************************************************************************/
215
216static stlibrd_t *stli_brds[STL_MAXBRDS];
217
218static int stli_shared;
219
220/*
221 * Per board state flags. Used with the state field of the board struct.
222 * Not really much here... All we need to do is keep track of whether
223 * the board has been detected, and whether it is actually running a slave
224 * or not.
225 */
226#define BST_FOUND 0x1
227#define BST_STARTED 0x2
228
229/*
230 * Define the set of port state flags. These are marked for internal
231 * state purposes only, usually to do with the state of communications
232 * with the slave. Most of them need to be updated atomically, so always
233 * use the bit setting operations (unless protected by cli/sti).
234 */
235#define ST_INITIALIZING 1
236#define ST_OPENING 2
237#define ST_CLOSING 3
238#define ST_CMDING 4
239#define ST_TXBUSY 5
240#define ST_RXING 6
241#define ST_DOFLUSHRX 7
242#define ST_DOFLUSHTX 8
243#define ST_DOSIGS 9
244#define ST_RXSTOP 10
245#define ST_GETSIGS 11
246
247/*
248 * Define an array of board names as printable strings. Handy for
249 * referencing boards when printing trace and stuff.
250 */
251static char *stli_brdnames[] = {
252 "Unknown",
253 "Stallion",
254 "Brumby",
255 "ONboard-MC",
256 "ONboard",
257 "Brumby",
258 "Brumby",
259 "ONboard-EI",
260 (char *) NULL,
261 "ONboard",
262 "ONboard-MC",
263 "ONboard-MC",
264 (char *) NULL,
265 (char *) NULL,
266 (char *) NULL,
267 (char *) NULL,
268 (char *) NULL,
269 (char *) NULL,
270 (char *) NULL,
271 (char *) NULL,
272 "EasyIO",
273 "EC8/32-AT",
274 "EC8/32-MC",
275 "EC8/64-AT",
276 "EC8/64-EI",
277 "EC8/64-MC",
278 "EC8/32-PCI",
279 "EC8/64-PCI",
280 "EasyIO-PCI",
281 "EC/RA-PCI",
282};
283
284/*****************************************************************************/
285
286#ifdef MODULE
287/*
288 * Define some string labels for arguments passed from the module
289 * load line. These allow for easy board definitions, and easy
290 * modification of the io, memory and irq resoucres.
291 */
292
293static char *board0[8];
294static char *board1[8];
295static char *board2[8];
296static char *board3[8];
297
298static char **stli_brdsp[] = {
299 (char **) &board0,
300 (char **) &board1,
301 (char **) &board2,
302 (char **) &board3
303};
304
305/*
306 * Define a set of common board names, and types. This is used to
307 * parse any module arguments.
308 */
309
310typedef struct stlibrdtype {
311 char *name;
312 int type;
313} stlibrdtype_t;
314
315static stlibrdtype_t stli_brdstr[] = {
316 { "stallion", BRD_STALLION },
317 { "1", BRD_STALLION },
318 { "brumby", BRD_BRUMBY },
319 { "brumby4", BRD_BRUMBY },
320 { "brumby/4", BRD_BRUMBY },
321 { "brumby-4", BRD_BRUMBY },
322 { "brumby8", BRD_BRUMBY },
323 { "brumby/8", BRD_BRUMBY },
324 { "brumby-8", BRD_BRUMBY },
325 { "brumby16", BRD_BRUMBY },
326 { "brumby/16", BRD_BRUMBY },
327 { "brumby-16", BRD_BRUMBY },
328 { "2", BRD_BRUMBY },
329 { "onboard2", BRD_ONBOARD2 },
330 { "onboard-2", BRD_ONBOARD2 },
331 { "onboard/2", BRD_ONBOARD2 },
332 { "onboard-mc", BRD_ONBOARD2 },
333 { "onboard/mc", BRD_ONBOARD2 },
334 { "onboard-mca", BRD_ONBOARD2 },
335 { "onboard/mca", BRD_ONBOARD2 },
336 { "3", BRD_ONBOARD2 },
337 { "onboard", BRD_ONBOARD },
338 { "onboardat", BRD_ONBOARD },
339 { "4", BRD_ONBOARD },
340 { "onboarde", BRD_ONBOARDE },
341 { "onboard-e", BRD_ONBOARDE },
342 { "onboard/e", BRD_ONBOARDE },
343 { "onboard-ei", BRD_ONBOARDE },
344 { "onboard/ei", BRD_ONBOARDE },
345 { "7", BRD_ONBOARDE },
346 { "ecp", BRD_ECP },
347 { "ecpat", BRD_ECP },
348 { "ec8/64", BRD_ECP },
349 { "ec8/64-at", BRD_ECP },
350 { "ec8/64-isa", BRD_ECP },
351 { "23", BRD_ECP },
352 { "ecpe", BRD_ECPE },
353 { "ecpei", BRD_ECPE },
354 { "ec8/64-e", BRD_ECPE },
355 { "ec8/64-ei", BRD_ECPE },
356 { "24", BRD_ECPE },
357 { "ecpmc", BRD_ECPMC },
358 { "ec8/64-mc", BRD_ECPMC },
359 { "ec8/64-mca", BRD_ECPMC },
360 { "25", BRD_ECPMC },
361 { "ecppci", BRD_ECPPCI },
362 { "ec/ra", BRD_ECPPCI },
363 { "ec/ra-pc", BRD_ECPPCI },
364 { "ec/ra-pci", BRD_ECPPCI },
365 { "29", BRD_ECPPCI },
366};
367
368/*
369 * Define the module agruments.
370 */
371MODULE_AUTHOR("Greg Ungerer");
372MODULE_DESCRIPTION("Stallion Intelligent Multiport Serial Driver");
373MODULE_LICENSE("GPL");
374
375
Rusty Russell8d3b33f2006-03-25 03:07:05 -0800376module_param_array(board0, charp, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377MODULE_PARM_DESC(board0, "Board 0 config -> name[,ioaddr[,memaddr]");
Rusty Russell8d3b33f2006-03-25 03:07:05 -0800378module_param_array(board1, charp, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379MODULE_PARM_DESC(board1, "Board 1 config -> name[,ioaddr[,memaddr]");
Rusty Russell8d3b33f2006-03-25 03:07:05 -0800380module_param_array(board2, charp, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381MODULE_PARM_DESC(board2, "Board 2 config -> name[,ioaddr[,memaddr]");
Rusty Russell8d3b33f2006-03-25 03:07:05 -0800382module_param_array(board3, charp, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383MODULE_PARM_DESC(board3, "Board 3 config -> name[,ioaddr[,memaddr]");
384
385#endif
386
387/*
388 * Set up a default memory address table for EISA board probing.
389 * The default addresses are all bellow 1Mbyte, which has to be the
390 * case anyway. They should be safe, since we only read values from
391 * them, and interrupts are disabled while we do it. If the higher
392 * memory support is compiled in then we also try probing around
393 * the 1Gb, 2Gb and 3Gb areas as well...
394 */
395static unsigned long stli_eisamemprobeaddrs[] = {
396 0xc0000, 0xd0000, 0xe0000, 0xf0000,
397 0x80000000, 0x80010000, 0x80020000, 0x80030000,
398 0x40000000, 0x40010000, 0x40020000, 0x40030000,
399 0xc0000000, 0xc0010000, 0xc0020000, 0xc0030000,
400 0xff000000, 0xff010000, 0xff020000, 0xff030000,
401};
402
Tobias Klauserfe971072006-01-09 20:54:02 -0800403static int stli_eisamempsize = ARRAY_SIZE(stli_eisamemprobeaddrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
405/*
406 * Define the Stallion PCI vendor and device IDs.
407 */
408#ifdef CONFIG_PCI
409#ifndef PCI_VENDOR_ID_STALLION
410#define PCI_VENDOR_ID_STALLION 0x124d
411#endif
412#ifndef PCI_DEVICE_ID_ECRA
413#define PCI_DEVICE_ID_ECRA 0x0004
414#endif
415
416static struct pci_device_id istallion_pci_tbl[] = {
Alan Cox4ac43602006-06-28 04:26:52 -0700417 { PCI_DEVICE(PCI_VENDOR_ID_STALLION, PCI_DEVICE_ID_ECRA), },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 { 0 }
419};
420MODULE_DEVICE_TABLE(pci, istallion_pci_tbl);
421
422#endif /* CONFIG_PCI */
423
424/*****************************************************************************/
425
426/*
427 * Hardware configuration info for ECP boards. These defines apply
428 * to the directly accessible io ports of the ECP. There is a set of
429 * defines for each ECP board type, ISA, EISA, MCA and PCI.
430 */
431#define ECP_IOSIZE 4
432
433#define ECP_MEMSIZE (128 * 1024)
434#define ECP_PCIMEMSIZE (256 * 1024)
435
436#define ECP_ATPAGESIZE (4 * 1024)
437#define ECP_MCPAGESIZE (4 * 1024)
438#define ECP_EIPAGESIZE (64 * 1024)
439#define ECP_PCIPAGESIZE (64 * 1024)
440
441#define STL_EISAID 0x8c4e
442
443/*
444 * Important defines for the ISA class of ECP board.
445 */
446#define ECP_ATIREG 0
447#define ECP_ATCONFR 1
448#define ECP_ATMEMAR 2
449#define ECP_ATMEMPR 3
450#define ECP_ATSTOP 0x1
451#define ECP_ATINTENAB 0x10
452#define ECP_ATENABLE 0x20
453#define ECP_ATDISABLE 0x00
454#define ECP_ATADDRMASK 0x3f000
455#define ECP_ATADDRSHFT 12
456
457/*
458 * Important defines for the EISA class of ECP board.
459 */
460#define ECP_EIIREG 0
461#define ECP_EIMEMARL 1
462#define ECP_EICONFR 2
463#define ECP_EIMEMARH 3
464#define ECP_EIENABLE 0x1
465#define ECP_EIDISABLE 0x0
466#define ECP_EISTOP 0x4
467#define ECP_EIEDGE 0x00
468#define ECP_EILEVEL 0x80
469#define ECP_EIADDRMASKL 0x00ff0000
470#define ECP_EIADDRSHFTL 16
471#define ECP_EIADDRMASKH 0xff000000
472#define ECP_EIADDRSHFTH 24
473#define ECP_EIBRDENAB 0xc84
474
475#define ECP_EISAID 0x4
476
477/*
478 * Important defines for the Micro-channel class of ECP board.
479 * (It has a lot in common with the ISA boards.)
480 */
481#define ECP_MCIREG 0
482#define ECP_MCCONFR 1
483#define ECP_MCSTOP 0x20
484#define ECP_MCENABLE 0x80
485#define ECP_MCDISABLE 0x00
486
487/*
488 * Important defines for the PCI class of ECP board.
489 * (It has a lot in common with the other ECP boards.)
490 */
491#define ECP_PCIIREG 0
492#define ECP_PCICONFR 1
493#define ECP_PCISTOP 0x01
494
495/*
496 * Hardware configuration info for ONboard and Brumby boards. These
497 * defines apply to the directly accessible io ports of these boards.
498 */
499#define ONB_IOSIZE 16
500#define ONB_MEMSIZE (64 * 1024)
501#define ONB_ATPAGESIZE (64 * 1024)
502#define ONB_MCPAGESIZE (64 * 1024)
503#define ONB_EIMEMSIZE (128 * 1024)
504#define ONB_EIPAGESIZE (64 * 1024)
505
506/*
507 * Important defines for the ISA class of ONboard board.
508 */
509#define ONB_ATIREG 0
510#define ONB_ATMEMAR 1
511#define ONB_ATCONFR 2
512#define ONB_ATSTOP 0x4
513#define ONB_ATENABLE 0x01
514#define ONB_ATDISABLE 0x00
515#define ONB_ATADDRMASK 0xff0000
516#define ONB_ATADDRSHFT 16
517
518#define ONB_MEMENABLO 0
519#define ONB_MEMENABHI 0x02
520
521/*
522 * Important defines for the EISA class of ONboard board.
523 */
524#define ONB_EIIREG 0
525#define ONB_EIMEMARL 1
526#define ONB_EICONFR 2
527#define ONB_EIMEMARH 3
528#define ONB_EIENABLE 0x1
529#define ONB_EIDISABLE 0x0
530#define ONB_EISTOP 0x4
531#define ONB_EIEDGE 0x00
532#define ONB_EILEVEL 0x80
533#define ONB_EIADDRMASKL 0x00ff0000
534#define ONB_EIADDRSHFTL 16
535#define ONB_EIADDRMASKH 0xff000000
536#define ONB_EIADDRSHFTH 24
537#define ONB_EIBRDENAB 0xc84
538
539#define ONB_EISAID 0x1
540
541/*
542 * Important defines for the Brumby boards. They are pretty simple,
543 * there is not much that is programmably configurable.
544 */
545#define BBY_IOSIZE 16
546#define BBY_MEMSIZE (64 * 1024)
547#define BBY_PAGESIZE (16 * 1024)
548
549#define BBY_ATIREG 0
550#define BBY_ATCONFR 1
551#define BBY_ATSTOP 0x4
552
553/*
554 * Important defines for the Stallion boards. They are pretty simple,
555 * there is not much that is programmably configurable.
556 */
557#define STAL_IOSIZE 16
558#define STAL_MEMSIZE (64 * 1024)
559#define STAL_PAGESIZE (64 * 1024)
560
561/*
562 * Define the set of status register values for EasyConnection panels.
563 * The signature will return with the status value for each panel. From
564 * this we can determine what is attached to the board - before we have
565 * actually down loaded any code to it.
566 */
567#define ECH_PNLSTATUS 2
568#define ECH_PNL16PORT 0x20
569#define ECH_PNLIDMASK 0x07
570#define ECH_PNLXPID 0x40
571#define ECH_PNLINTRPEND 0x80
572
573/*
574 * Define some macros to do things to the board. Even those these boards
575 * are somewhat related there is often significantly different ways of
576 * doing some operation on it (like enable, paging, reset, etc). So each
577 * board class has a set of functions which do the commonly required
578 * operations. The macros below basically just call these functions,
579 * generally checking for a NULL function - which means that the board
580 * needs nothing done to it to achieve this operation!
581 */
582#define EBRDINIT(brdp) \
583 if (brdp->init != NULL) \
584 (* brdp->init)(brdp)
585
586#define EBRDENABLE(brdp) \
587 if (brdp->enable != NULL) \
588 (* brdp->enable)(brdp);
589
590#define EBRDDISABLE(brdp) \
591 if (brdp->disable != NULL) \
592 (* brdp->disable)(brdp);
593
594#define EBRDINTR(brdp) \
595 if (brdp->intr != NULL) \
596 (* brdp->intr)(brdp);
597
598#define EBRDRESET(brdp) \
599 if (brdp->reset != NULL) \
600 (* brdp->reset)(brdp);
601
602#define EBRDGETMEMPTR(brdp,offset) \
603 (* brdp->getmemptr)(brdp, offset, __LINE__)
604
605/*
606 * Define the maximal baud rate, and the default baud base for ports.
607 */
608#define STL_MAXBAUD 460800
609#define STL_BAUDBASE 115200
610#define STL_CLOSEDELAY (5 * HZ / 10)
611
612/*****************************************************************************/
613
614/*
615 * Define macros to extract a brd or port number from a minor number.
616 */
617#define MINOR2BRD(min) (((min) & 0xc0) >> 6)
618#define MINOR2PORT(min) ((min) & 0x3f)
619
620/*
621 * Define a baud rate table that converts termios baud rate selector
622 * into the actual baud rate value. All baud rate calculations are based
623 * on the actual baud rate required.
624 */
625static unsigned int stli_baudrates[] = {
626 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
627 9600, 19200, 38400, 57600, 115200, 230400, 460800, 921600
628};
629
630/*****************************************************************************/
631
632/*
633 * Define some handy local macros...
634 */
635#undef MIN
636#define MIN(a,b) (((a) <= (b)) ? (a) : (b))
637
638#undef TOLOWER
639#define TOLOWER(x) ((((x) >= 'A') && ((x) <= 'Z')) ? ((x) + 0x20) : (x))
640
641/*****************************************************************************/
642
643/*
644 * Prototype all functions in this driver!
645 */
646
647#ifdef MODULE
648static void stli_argbrds(void);
649static int stli_parsebrd(stlconf_t *confp, char **argp);
650
651static unsigned long stli_atol(char *str);
652#endif
653
654int stli_init(void);
655static int stli_open(struct tty_struct *tty, struct file *filp);
656static void stli_close(struct tty_struct *tty, struct file *filp);
657static int stli_write(struct tty_struct *tty, const unsigned char *buf, int count);
658static void stli_putchar(struct tty_struct *tty, unsigned char ch);
659static void stli_flushchars(struct tty_struct *tty);
660static int stli_writeroom(struct tty_struct *tty);
661static int stli_charsinbuffer(struct tty_struct *tty);
662static int stli_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg);
663static void stli_settermios(struct tty_struct *tty, struct termios *old);
664static void stli_throttle(struct tty_struct *tty);
665static void stli_unthrottle(struct tty_struct *tty);
666static void stli_stop(struct tty_struct *tty);
667static void stli_start(struct tty_struct *tty);
668static void stli_flushbuffer(struct tty_struct *tty);
669static void stli_breakctl(struct tty_struct *tty, int state);
670static void stli_waituntilsent(struct tty_struct *tty, int timeout);
671static void stli_sendxchar(struct tty_struct *tty, char ch);
672static void stli_hangup(struct tty_struct *tty);
673static int stli_portinfo(stlibrd_t *brdp, stliport_t *portp, int portnr, char *pos);
674
675static int stli_brdinit(stlibrd_t *brdp);
676static int stli_startbrd(stlibrd_t *brdp);
677static ssize_t stli_memread(struct file *fp, char __user *buf, size_t count, loff_t *offp);
678static ssize_t stli_memwrite(struct file *fp, const char __user *buf, size_t count, loff_t *offp);
679static int stli_memioctl(struct inode *ip, struct file *fp, unsigned int cmd, unsigned long arg);
Alan Cox4ac43602006-06-28 04:26:52 -0700680static void stli_brdpoll(stlibrd_t *brdp, cdkhdr_t __iomem *hdrp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681static void stli_poll(unsigned long arg);
682static int stli_hostcmd(stlibrd_t *brdp, stliport_t *portp);
683static int stli_initopen(stlibrd_t *brdp, stliport_t *portp);
684static int stli_rawopen(stlibrd_t *brdp, stliport_t *portp, unsigned long arg, int wait);
685static int stli_rawclose(stlibrd_t *brdp, stliport_t *portp, unsigned long arg, int wait);
686static int stli_waitcarrier(stlibrd_t *brdp, stliport_t *portp, struct file *filp);
687static void stli_dohangup(void *arg);
688static int stli_setport(stliport_t *portp);
689static int stli_cmdwait(stlibrd_t *brdp, stliport_t *portp, unsigned long cmd, void *arg, int size, int copyback);
690static 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 -0700691static void __stli_sendcmd(stlibrd_t *brdp, stliport_t *portp, unsigned long cmd, void *arg, int size, int copyback);
692static void stli_dodelaycmd(stliport_t *portp, cdkctrl_t __iomem *cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693static void stli_mkasyport(stliport_t *portp, asyport_t *pp, struct termios *tiosp);
694static void stli_mkasysigs(asysigs_t *sp, int dtr, int rts);
695static long stli_mktiocm(unsigned long sigvalue);
696static void stli_read(stlibrd_t *brdp, stliport_t *portp);
697static int stli_getserial(stliport_t *portp, struct serial_struct __user *sp);
698static int stli_setserial(stliport_t *portp, struct serial_struct __user *sp);
699static int stli_getbrdstats(combrd_t __user *bp);
700static int stli_getportstats(stliport_t *portp, comstats_t __user *cp);
701static int stli_portcmdstats(stliport_t *portp);
702static int stli_clrportstats(stliport_t *portp, comstats_t __user *cp);
703static int stli_getportstruct(stliport_t __user *arg);
704static int stli_getbrdstruct(stlibrd_t __user *arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705static stlibrd_t *stli_allocbrd(void);
706
707static void stli_ecpinit(stlibrd_t *brdp);
708static void stli_ecpenable(stlibrd_t *brdp);
709static void stli_ecpdisable(stlibrd_t *brdp);
710static char *stli_ecpgetmemptr(stlibrd_t *brdp, unsigned long offset, int line);
711static void stli_ecpreset(stlibrd_t *brdp);
712static void stli_ecpintr(stlibrd_t *brdp);
713static void stli_ecpeiinit(stlibrd_t *brdp);
714static void stli_ecpeienable(stlibrd_t *brdp);
715static void stli_ecpeidisable(stlibrd_t *brdp);
716static char *stli_ecpeigetmemptr(stlibrd_t *brdp, unsigned long offset, int line);
717static void stli_ecpeireset(stlibrd_t *brdp);
718static void stli_ecpmcenable(stlibrd_t *brdp);
719static void stli_ecpmcdisable(stlibrd_t *brdp);
720static char *stli_ecpmcgetmemptr(stlibrd_t *brdp, unsigned long offset, int line);
721static void stli_ecpmcreset(stlibrd_t *brdp);
722static void stli_ecppciinit(stlibrd_t *brdp);
723static char *stli_ecppcigetmemptr(stlibrd_t *brdp, unsigned long offset, int line);
724static void stli_ecppcireset(stlibrd_t *brdp);
725
726static void stli_onbinit(stlibrd_t *brdp);
727static void stli_onbenable(stlibrd_t *brdp);
728static void stli_onbdisable(stlibrd_t *brdp);
729static char *stli_onbgetmemptr(stlibrd_t *brdp, unsigned long offset, int line);
730static void stli_onbreset(stlibrd_t *brdp);
731static void stli_onbeinit(stlibrd_t *brdp);
732static void stli_onbeenable(stlibrd_t *brdp);
733static void stli_onbedisable(stlibrd_t *brdp);
734static char *stli_onbegetmemptr(stlibrd_t *brdp, unsigned long offset, int line);
735static void stli_onbereset(stlibrd_t *brdp);
736static void stli_bbyinit(stlibrd_t *brdp);
737static char *stli_bbygetmemptr(stlibrd_t *brdp, unsigned long offset, int line);
738static void stli_bbyreset(stlibrd_t *brdp);
739static void stli_stalinit(stlibrd_t *brdp);
740static char *stli_stalgetmemptr(stlibrd_t *brdp, unsigned long offset, int line);
741static void stli_stalreset(stlibrd_t *brdp);
742
743static stliport_t *stli_getport(int brdnr, int panelnr, int portnr);
744
745static int stli_initecp(stlibrd_t *brdp);
746static int stli_initonb(stlibrd_t *brdp);
747static int stli_eisamemprobe(stlibrd_t *brdp);
748static int stli_initports(stlibrd_t *brdp);
749
750#ifdef CONFIG_PCI
751static int stli_initpcibrd(int brdtype, struct pci_dev *devp);
752#endif
753
754/*****************************************************************************/
755
756/*
757 * Define the driver info for a user level shared memory device. This
758 * device will work sort of like the /dev/kmem device - except that it
759 * will give access to the shared memory on the Stallion intelligent
760 * board. This is also a very useful debugging tool.
761 */
762static struct file_operations stli_fsiomem = {
763 .owner = THIS_MODULE,
764 .read = stli_memread,
765 .write = stli_memwrite,
766 .ioctl = stli_memioctl,
767};
768
769/*****************************************************************************/
770
771/*
772 * Define a timer_list entry for our poll routine. The slave board
773 * is polled every so often to see if anything needs doing. This is
774 * much cheaper on host cpu than using interrupts. It turns out to
775 * not increase character latency by much either...
776 */
Ingo Molnar8d06afa2005-09-09 13:10:40 -0700777static DEFINE_TIMER(stli_timerlist, stli_poll, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
779static int stli_timeron;
780
781/*
782 * Define the calculation for the timeout routine.
783 */
784#define STLI_TIMEOUT (jiffies + 1)
785
786/*****************************************************************************/
787
gregkh@suse.deca8eca62005-03-23 09:53:09 -0800788static struct class *istallion_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
790#ifdef MODULE
791
792/*
793 * Loadable module initialization stuff.
794 */
795
796static int __init istallion_module_init(void)
797{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 stli_init();
Alan Cox4ac43602006-06-28 04:26:52 -0700799 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800}
801
802/*****************************************************************************/
803
804static void __exit istallion_module_exit(void)
805{
806 stlibrd_t *brdp;
807 stliport_t *portp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 int i, j;
809
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 printk(KERN_INFO "Unloading %s: version %s\n", stli_drvtitle,
811 stli_drvversion);
812
Alan Cox4ac43602006-06-28 04:26:52 -0700813 /*
814 * Free up all allocated resources used by the ports. This includes
815 * memory and interrupts.
816 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 if (stli_timeron) {
818 stli_timeron = 0;
Alan Cox4ac43602006-06-28 04:26:52 -0700819 del_timer_sync(&stli_timerlist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 }
821
822 i = tty_unregister_driver(stli_serial);
823 if (i) {
824 printk("STALLION: failed to un-register tty driver, "
825 "errno=%d\n", -i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 return;
827 }
828 put_tty_driver(stli_serial);
829 for (i = 0; i < 4; i++) {
830 devfs_remove("staliomem/%d", i);
gregkh@suse.deca8eca62005-03-23 09:53:09 -0800831 class_device_destroy(istallion_class, MKDEV(STL_SIOMEMMAJOR, i));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 }
833 devfs_remove("staliomem");
gregkh@suse.deca8eca62005-03-23 09:53:09 -0800834 class_destroy(istallion_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 if ((i = unregister_chrdev(STL_SIOMEMMAJOR, "staliomem")))
836 printk("STALLION: failed to un-register serial memory device, "
837 "errno=%d\n", -i);
Jesper Juhl735d5662005-11-07 01:01:29 -0800838
Jesper Juhl735d5662005-11-07 01:01:29 -0800839 kfree(stli_txcookbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
841 for (i = 0; (i < stli_nrbrds); i++) {
Alan Cox4ac43602006-06-28 04:26:52 -0700842 if ((brdp = stli_brds[i]) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 continue;
844 for (j = 0; (j < STL_MAXPORTS); j++) {
845 portp = brdp->ports[j];
Alan Cox4ac43602006-06-28 04:26:52 -0700846 if (portp != NULL) {
847 if (portp->tty != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 tty_hangup(portp->tty);
849 kfree(portp);
850 }
851 }
852
853 iounmap(brdp->membase);
854 if (brdp->iosize > 0)
855 release_region(brdp->iobase, brdp->iosize);
856 kfree(brdp);
Alan Cox4ac43602006-06-28 04:26:52 -0700857 stli_brds[i] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859}
860
861module_init(istallion_module_init);
862module_exit(istallion_module_exit);
863
864/*****************************************************************************/
865
866/*
867 * Check for any arguments passed in on the module load command line.
868 */
869
870static void stli_argbrds(void)
871{
Alan Cox4ac43602006-06-28 04:26:52 -0700872 stlconf_t conf;
873 stlibrd_t *brdp;
874 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
Tobias Klauserfe971072006-01-09 20:54:02 -0800876 for (i = stli_nrbrds; i < ARRAY_SIZE(stli_brdsp); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 memset(&conf, 0, sizeof(conf));
878 if (stli_parsebrd(&conf, stli_brdsp[i]) == 0)
879 continue;
Alan Cox4ac43602006-06-28 04:26:52 -0700880 if ((brdp = stli_allocbrd()) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 continue;
882 stli_nrbrds = i + 1;
883 brdp->brdnr = i;
884 brdp->brdtype = conf.brdtype;
885 brdp->iobase = conf.ioaddr1;
886 brdp->memaddr = conf.memaddr;
887 stli_brdinit(brdp);
888 }
889}
890
891/*****************************************************************************/
892
893/*
894 * Convert an ascii string number into an unsigned long.
895 */
896
897static unsigned long stli_atol(char *str)
898{
Alan Cox4ac43602006-06-28 04:26:52 -0700899 unsigned long val;
900 int base, c;
901 char *sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
903 val = 0;
904 sp = str;
905 if ((*sp == '0') && (*(sp+1) == 'x')) {
906 base = 16;
907 sp += 2;
908 } else if (*sp == '0') {
909 base = 8;
910 sp++;
911 } else {
912 base = 10;
913 }
914
915 for (; (*sp != 0); sp++) {
916 c = (*sp > '9') ? (TOLOWER(*sp) - 'a' + 10) : (*sp - '0');
917 if ((c < 0) || (c >= base)) {
918 printk("STALLION: invalid argument %s\n", str);
919 val = 0;
920 break;
921 }
922 val = (val * base) + c;
923 }
924 return(val);
925}
926
927/*****************************************************************************/
928
929/*
930 * Parse the supplied argument string, into the board conf struct.
931 */
932
933static int stli_parsebrd(stlconf_t *confp, char **argp)
934{
Alan Cox4ac43602006-06-28 04:26:52 -0700935 char *sp;
936 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
Alan Cox4ac43602006-06-28 04:26:52 -0700938 if (argp[0] == NULL || *argp[0] == 0)
939 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
941 for (sp = argp[0], i = 0; ((*sp != 0) && (i < 25)); sp++, i++)
942 *sp = TOLOWER(*sp);
943
Tobias Klauserfe971072006-01-09 20:54:02 -0800944 for (i = 0; i < ARRAY_SIZE(stli_brdstr); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 if (strcmp(stli_brdstr[i].name, argp[0]) == 0)
946 break;
947 }
Tobias Klauserfe971072006-01-09 20:54:02 -0800948 if (i == ARRAY_SIZE(stli_brdstr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 printk("STALLION: unknown board name, %s?\n", argp[0]);
Tobias Klauserfe971072006-01-09 20:54:02 -0800950 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 }
952
953 confp->brdtype = stli_brdstr[i].type;
Alan Cox4ac43602006-06-28 04:26:52 -0700954 if (argp[1] != NULL && *argp[1] != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 confp->ioaddr1 = stli_atol(argp[1]);
Alan Cox4ac43602006-06-28 04:26:52 -0700956 if (argp[2] != NULL && *argp[2] != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 confp->memaddr = stli_atol(argp[2]);
958 return(1);
959}
960
961#endif
962
963/*****************************************************************************/
964
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965static int stli_open(struct tty_struct *tty, struct file *filp)
966{
Alan Cox4ac43602006-06-28 04:26:52 -0700967 stlibrd_t *brdp;
968 stliport_t *portp;
969 unsigned int minordev;
970 int brdnr, portnr, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
972 minordev = tty->index;
973 brdnr = MINOR2BRD(minordev);
974 if (brdnr >= stli_nrbrds)
Alan Cox4ac43602006-06-28 04:26:52 -0700975 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 brdp = stli_brds[brdnr];
Alan Cox4ac43602006-06-28 04:26:52 -0700977 if (brdp == NULL)
978 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 if ((brdp->state & BST_STARTED) == 0)
Alan Cox4ac43602006-06-28 04:26:52 -0700980 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 portnr = MINOR2PORT(minordev);
982 if ((portnr < 0) || (portnr > brdp->nrports))
Alan Cox4ac43602006-06-28 04:26:52 -0700983 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
985 portp = brdp->ports[portnr];
Alan Cox4ac43602006-06-28 04:26:52 -0700986 if (portp == NULL)
987 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 if (portp->devnr < 1)
Alan Cox4ac43602006-06-28 04:26:52 -0700989 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
991
992/*
993 * Check if this port is in the middle of closing. If so then wait
994 * until it is closed then return error status based on flag settings.
995 * The sleep here does not need interrupt protection since the wakeup
996 * for it is done with the same context.
997 */
998 if (portp->flags & ASYNC_CLOSING) {
999 interruptible_sleep_on(&portp->close_wait);
1000 if (portp->flags & ASYNC_HUP_NOTIFY)
Alan Cox4ac43602006-06-28 04:26:52 -07001001 return -EAGAIN;
1002 return -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 }
1004
1005/*
1006 * On the first open of the device setup the port hardware, and
1007 * initialize the per port data structure. Since initializing the port
1008 * requires several commands to the board we will need to wait for any
1009 * other open that is already initializing the port.
1010 */
1011 portp->tty = tty;
1012 tty->driver_data = portp;
1013 portp->refcount++;
1014
1015 wait_event_interruptible(portp->raw_wait,
1016 !test_bit(ST_INITIALIZING, &portp->state));
1017 if (signal_pending(current))
Alan Cox4ac43602006-06-28 04:26:52 -07001018 return -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
1020 if ((portp->flags & ASYNC_INITIALIZED) == 0) {
1021 set_bit(ST_INITIALIZING, &portp->state);
1022 if ((rc = stli_initopen(brdp, portp)) >= 0) {
1023 portp->flags |= ASYNC_INITIALIZED;
1024 clear_bit(TTY_IO_ERROR, &tty->flags);
1025 }
1026 clear_bit(ST_INITIALIZING, &portp->state);
1027 wake_up_interruptible(&portp->raw_wait);
1028 if (rc < 0)
Alan Cox4ac43602006-06-28 04:26:52 -07001029 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 }
1031
1032/*
1033 * Check if this port is in the middle of closing. If so then wait
1034 * until it is closed then return error status, based on flag settings.
1035 * The sleep here does not need interrupt protection since the wakeup
1036 * for it is done with the same context.
1037 */
1038 if (portp->flags & ASYNC_CLOSING) {
1039 interruptible_sleep_on(&portp->close_wait);
1040 if (portp->flags & ASYNC_HUP_NOTIFY)
Alan Cox4ac43602006-06-28 04:26:52 -07001041 return -EAGAIN;
1042 return -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 }
1044
1045/*
1046 * Based on type of open being done check if it can overlap with any
1047 * previous opens still in effect. If we are a normal serial device
1048 * then also we might have to wait for carrier.
1049 */
1050 if (!(filp->f_flags & O_NONBLOCK)) {
1051 if ((rc = stli_waitcarrier(brdp, portp, filp)) != 0)
Alan Cox4ac43602006-06-28 04:26:52 -07001052 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 }
1054 portp->flags |= ASYNC_NORMAL_ACTIVE;
Alan Cox4ac43602006-06-28 04:26:52 -07001055 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056}
1057
1058/*****************************************************************************/
1059
1060static void stli_close(struct tty_struct *tty, struct file *filp)
1061{
Alan Cox4ac43602006-06-28 04:26:52 -07001062 stlibrd_t *brdp;
1063 stliport_t *portp;
1064 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065
1066 portp = tty->driver_data;
Alan Cox4ac43602006-06-28 04:26:52 -07001067 if (portp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 return;
1069
Alan Cox4ac43602006-06-28 04:26:52 -07001070 spin_lock_irqsave(&stli_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 if (tty_hung_up_p(filp)) {
Alan Cox4ac43602006-06-28 04:26:52 -07001072 spin_unlock_irqrestore(&stli_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 return;
1074 }
1075 if ((tty->count == 1) && (portp->refcount != 1))
1076 portp->refcount = 1;
1077 if (portp->refcount-- > 1) {
Alan Cox4ac43602006-06-28 04:26:52 -07001078 spin_unlock_irqrestore(&stli_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 return;
1080 }
1081
1082 portp->flags |= ASYNC_CLOSING;
1083
1084/*
1085 * May want to wait for data to drain before closing. The BUSY flag
1086 * keeps track of whether we are still transmitting or not. It is
1087 * updated by messages from the slave - indicating when all chars
1088 * really have drained.
1089 */
1090 if (tty == stli_txcooktty)
1091 stli_flushchars(tty);
1092 tty->closing = 1;
Alan Cox4ac43602006-06-28 04:26:52 -07001093 spin_unlock_irqrestore(&stli_lock, flags);
1094
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 if (portp->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1096 tty_wait_until_sent(tty, portp->closing_wait);
1097
1098 portp->flags &= ~ASYNC_INITIALIZED;
1099 brdp = stli_brds[portp->brdnr];
1100 stli_rawclose(brdp, portp, 0, 0);
1101 if (tty->termios->c_cflag & HUPCL) {
1102 stli_mkasysigs(&portp->asig, 0, 0);
1103 if (test_bit(ST_CMDING, &portp->state))
1104 set_bit(ST_DOSIGS, &portp->state);
1105 else
1106 stli_sendcmd(brdp, portp, A_SETSIGNALS, &portp->asig,
1107 sizeof(asysigs_t), 0);
1108 }
1109 clear_bit(ST_TXBUSY, &portp->state);
1110 clear_bit(ST_RXSTOP, &portp->state);
1111 set_bit(TTY_IO_ERROR, &tty->flags);
1112 if (tty->ldisc.flush_buffer)
1113 (tty->ldisc.flush_buffer)(tty);
1114 set_bit(ST_DOFLUSHRX, &portp->state);
1115 stli_flushbuffer(tty);
1116
1117 tty->closing = 0;
Alan Cox4ac43602006-06-28 04:26:52 -07001118 portp->tty = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119
1120 if (portp->openwaitcnt) {
1121 if (portp->close_delay)
1122 msleep_interruptible(jiffies_to_msecs(portp->close_delay));
1123 wake_up_interruptible(&portp->open_wait);
1124 }
1125
1126 portp->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
1127 wake_up_interruptible(&portp->close_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128}
1129
1130/*****************************************************************************/
1131
1132/*
1133 * Carry out first open operations on a port. This involves a number of
1134 * commands to be sent to the slave. We need to open the port, set the
1135 * notification events, set the initial port settings, get and set the
1136 * initial signal values. We sleep and wait in between each one. But
1137 * this still all happens pretty quickly.
1138 */
1139
1140static int stli_initopen(stlibrd_t *brdp, stliport_t *portp)
1141{
Alan Cox4ac43602006-06-28 04:26:52 -07001142 struct tty_struct *tty;
1143 asynotify_t nt;
1144 asyport_t aport;
1145 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146
1147 if ((rc = stli_rawopen(brdp, portp, 0, 1)) < 0)
Alan Cox4ac43602006-06-28 04:26:52 -07001148 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149
1150 memset(&nt, 0, sizeof(asynotify_t));
1151 nt.data = (DT_TXLOW | DT_TXEMPTY | DT_RXBUSY | DT_RXBREAK);
1152 nt.signal = SG_DCD;
1153 if ((rc = stli_cmdwait(brdp, portp, A_SETNOTIFY, &nt,
1154 sizeof(asynotify_t), 0)) < 0)
Alan Cox4ac43602006-06-28 04:26:52 -07001155 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156
1157 tty = portp->tty;
Alan Cox4ac43602006-06-28 04:26:52 -07001158 if (tty == NULL)
1159 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 stli_mkasyport(portp, &aport, tty->termios);
1161 if ((rc = stli_cmdwait(brdp, portp, A_SETPORT, &aport,
1162 sizeof(asyport_t), 0)) < 0)
Alan Cox4ac43602006-06-28 04:26:52 -07001163 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
1165 set_bit(ST_GETSIGS, &portp->state);
1166 if ((rc = stli_cmdwait(brdp, portp, A_GETSIGNALS, &portp->asig,
1167 sizeof(asysigs_t), 1)) < 0)
Alan Cox4ac43602006-06-28 04:26:52 -07001168 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 if (test_and_clear_bit(ST_GETSIGS, &portp->state))
1170 portp->sigs = stli_mktiocm(portp->asig.sigvalue);
1171 stli_mkasysigs(&portp->asig, 1, 1);
1172 if ((rc = stli_cmdwait(brdp, portp, A_SETSIGNALS, &portp->asig,
1173 sizeof(asysigs_t), 0)) < 0)
Alan Cox4ac43602006-06-28 04:26:52 -07001174 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
Alan Cox4ac43602006-06-28 04:26:52 -07001176 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177}
1178
1179/*****************************************************************************/
1180
1181/*
1182 * Send an open message to the slave. This will sleep waiting for the
1183 * acknowledgement, so must have user context. We need to co-ordinate
1184 * with close events here, since we don't want open and close events
1185 * to overlap.
1186 */
1187
1188static int stli_rawopen(stlibrd_t *brdp, stliport_t *portp, unsigned long arg, int wait)
1189{
Alan Cox4ac43602006-06-28 04:26:52 -07001190 cdkhdr_t __iomem *hdrp;
1191 cdkctrl_t __iomem *cp;
1192 unsigned char __iomem *bits;
1193 unsigned long flags;
1194 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195
1196/*
1197 * Send a message to the slave to open this port.
1198 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199
1200/*
1201 * Slave is already closing this port. This can happen if a hangup
1202 * occurs on this port. So we must wait until it is complete. The
1203 * order of opens and closes may not be preserved across shared
1204 * memory, so we must wait until it is complete.
1205 */
1206 wait_event_interruptible(portp->raw_wait,
1207 !test_bit(ST_CLOSING, &portp->state));
1208 if (signal_pending(current)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 return -ERESTARTSYS;
1210 }
1211
1212/*
1213 * Everything is ready now, so write the open message into shared
1214 * memory. Once the message is in set the service bits to say that
1215 * this port wants service.
1216 */
Alan Cox4ac43602006-06-28 04:26:52 -07001217 spin_lock_irqsave(&brd_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 EBRDENABLE(brdp);
Alan Cox4ac43602006-06-28 04:26:52 -07001219 cp = &((cdkasy_t __iomem *) EBRDGETMEMPTR(brdp, portp->addr))->ctrl;
1220 writel(arg, &cp->openarg);
1221 writeb(1, &cp->open);
1222 hdrp = (cdkhdr_t __iomem *) EBRDGETMEMPTR(brdp, CDK_CDKADDR);
1223 bits = ((unsigned char __iomem *) hdrp) + brdp->slaveoffset +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 portp->portidx;
Alan Cox4ac43602006-06-28 04:26:52 -07001225 writeb(readb(bits) | portp->portbit, bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 EBRDDISABLE(brdp);
1227
1228 if (wait == 0) {
Alan Cox4ac43602006-06-28 04:26:52 -07001229 spin_unlock_irqrestore(&brd_lock, flags);
1230 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 }
1232
1233/*
1234 * Slave is in action, so now we must wait for the open acknowledgment
1235 * to come back.
1236 */
1237 rc = 0;
1238 set_bit(ST_OPENING, &portp->state);
Alan Cox4ac43602006-06-28 04:26:52 -07001239 spin_unlock_irqrestore(&brd_lock, flags);
1240
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 wait_event_interruptible(portp->raw_wait,
1242 !test_bit(ST_OPENING, &portp->state));
1243 if (signal_pending(current))
1244 rc = -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245
1246 if ((rc == 0) && (portp->rc != 0))
1247 rc = -EIO;
Alan Cox4ac43602006-06-28 04:26:52 -07001248 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249}
1250
1251/*****************************************************************************/
1252
1253/*
1254 * Send a close message to the slave. Normally this will sleep waiting
1255 * for the acknowledgement, but if wait parameter is 0 it will not. If
1256 * wait is true then must have user context (to sleep).
1257 */
1258
1259static int stli_rawclose(stlibrd_t *brdp, stliport_t *portp, unsigned long arg, int wait)
1260{
Alan Cox4ac43602006-06-28 04:26:52 -07001261 cdkhdr_t __iomem *hdrp;
1262 cdkctrl_t __iomem *cp;
1263 unsigned char __iomem *bits;
1264 unsigned long flags;
1265 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
1267/*
1268 * Slave is already closing this port. This can happen if a hangup
1269 * occurs on this port.
1270 */
1271 if (wait) {
1272 wait_event_interruptible(portp->raw_wait,
1273 !test_bit(ST_CLOSING, &portp->state));
1274 if (signal_pending(current)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 return -ERESTARTSYS;
1276 }
1277 }
1278
1279/*
1280 * Write the close command into shared memory.
1281 */
Alan Cox4ac43602006-06-28 04:26:52 -07001282 spin_lock_irqsave(&brd_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 EBRDENABLE(brdp);
Alan Cox4ac43602006-06-28 04:26:52 -07001284 cp = &((cdkasy_t __iomem *) EBRDGETMEMPTR(brdp, portp->addr))->ctrl;
1285 writel(arg, &cp->closearg);
1286 writeb(1, &cp->close);
1287 hdrp = (cdkhdr_t __iomem *) EBRDGETMEMPTR(brdp, CDK_CDKADDR);
1288 bits = ((unsigned char __iomem *) hdrp) + brdp->slaveoffset +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 portp->portidx;
Alan Cox4ac43602006-06-28 04:26:52 -07001290 writeb(readb(bits) |portp->portbit, bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 EBRDDISABLE(brdp);
1292
1293 set_bit(ST_CLOSING, &portp->state);
Alan Cox4ac43602006-06-28 04:26:52 -07001294 spin_unlock_irqrestore(&brd_lock, flags);
1295
1296 if (wait == 0)
1297 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298
1299/*
1300 * Slave is in action, so now we must wait for the open acknowledgment
1301 * to come back.
1302 */
1303 rc = 0;
1304 wait_event_interruptible(portp->raw_wait,
1305 !test_bit(ST_CLOSING, &portp->state));
1306 if (signal_pending(current))
1307 rc = -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308
1309 if ((rc == 0) && (portp->rc != 0))
1310 rc = -EIO;
Alan Cox4ac43602006-06-28 04:26:52 -07001311 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312}
1313
1314/*****************************************************************************/
1315
1316/*
1317 * Send a command to the slave and wait for the response. This must
1318 * have user context (it sleeps). This routine is generic in that it
1319 * can send any type of command. Its purpose is to wait for that command
1320 * to complete (as opposed to initiating the command then returning).
1321 */
1322
1323static int stli_cmdwait(stlibrd_t *brdp, stliport_t *portp, unsigned long cmd, void *arg, int size, int copyback)
1324{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 wait_event_interruptible(portp->raw_wait,
1326 !test_bit(ST_CMDING, &portp->state));
Alan Cox4ac43602006-06-28 04:26:52 -07001327 if (signal_pending(current))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 return -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329
1330 stli_sendcmd(brdp, portp, cmd, arg, size, copyback);
1331
1332 wait_event_interruptible(portp->raw_wait,
1333 !test_bit(ST_CMDING, &portp->state));
Alan Cox4ac43602006-06-28 04:26:52 -07001334 if (signal_pending(current))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 return -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336
1337 if (portp->rc != 0)
Alan Cox4ac43602006-06-28 04:26:52 -07001338 return -EIO;
1339 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340}
1341
1342/*****************************************************************************/
1343
1344/*
1345 * Send the termios settings for this port to the slave. This sleeps
1346 * waiting for the command to complete - so must have user context.
1347 */
1348
1349static int stli_setport(stliport_t *portp)
1350{
Alan Cox4ac43602006-06-28 04:26:52 -07001351 stlibrd_t *brdp;
1352 asyport_t aport;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353
Alan Cox4ac43602006-06-28 04:26:52 -07001354 if (portp == NULL)
1355 return -ENODEV;
1356 if (portp->tty == NULL)
1357 return -ENODEV;
1358 if (portp->brdnr < 0 && portp->brdnr >= stli_nrbrds)
1359 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 brdp = stli_brds[portp->brdnr];
Alan Cox4ac43602006-06-28 04:26:52 -07001361 if (brdp == NULL)
1362 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
1364 stli_mkasyport(portp, &aport, portp->tty->termios);
1365 return(stli_cmdwait(brdp, portp, A_SETPORT, &aport, sizeof(asyport_t), 0));
1366}
1367
1368/*****************************************************************************/
1369
1370/*
1371 * Possibly need to wait for carrier (DCD signal) to come high. Say
1372 * maybe because if we are clocal then we don't need to wait...
1373 */
1374
1375static int stli_waitcarrier(stlibrd_t *brdp, stliport_t *portp, struct file *filp)
1376{
Alan Cox4ac43602006-06-28 04:26:52 -07001377 unsigned long flags;
1378 int rc, doclocal;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379
1380 rc = 0;
1381 doclocal = 0;
1382
1383 if (portp->tty->termios->c_cflag & CLOCAL)
1384 doclocal++;
1385
Alan Cox4ac43602006-06-28 04:26:52 -07001386 spin_lock_irqsave(&stli_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 portp->openwaitcnt++;
1388 if (! tty_hung_up_p(filp))
1389 portp->refcount--;
Alan Cox4ac43602006-06-28 04:26:52 -07001390 spin_unlock_irqrestore(&stli_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391
1392 for (;;) {
1393 stli_mkasysigs(&portp->asig, 1, 1);
1394 if ((rc = stli_cmdwait(brdp, portp, A_SETSIGNALS,
1395 &portp->asig, sizeof(asysigs_t), 0)) < 0)
1396 break;
1397 if (tty_hung_up_p(filp) ||
1398 ((portp->flags & ASYNC_INITIALIZED) == 0)) {
1399 if (portp->flags & ASYNC_HUP_NOTIFY)
1400 rc = -EBUSY;
1401 else
1402 rc = -ERESTARTSYS;
1403 break;
1404 }
1405 if (((portp->flags & ASYNC_CLOSING) == 0) &&
1406 (doclocal || (portp->sigs & TIOCM_CD))) {
1407 break;
1408 }
1409 if (signal_pending(current)) {
1410 rc = -ERESTARTSYS;
1411 break;
1412 }
1413 interruptible_sleep_on(&portp->open_wait);
1414 }
1415
Alan Cox4ac43602006-06-28 04:26:52 -07001416 spin_lock_irqsave(&stli_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 if (! tty_hung_up_p(filp))
1418 portp->refcount++;
1419 portp->openwaitcnt--;
Alan Cox4ac43602006-06-28 04:26:52 -07001420 spin_unlock_irqrestore(&stli_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421
Alan Cox4ac43602006-06-28 04:26:52 -07001422 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423}
1424
1425/*****************************************************************************/
1426
1427/*
1428 * Write routine. Take the data and put it in the shared memory ring
1429 * queue. If port is not already sending chars then need to mark the
1430 * service bits for this port.
1431 */
1432
1433static int stli_write(struct tty_struct *tty, const unsigned char *buf, int count)
1434{
Alan Cox4ac43602006-06-28 04:26:52 -07001435 cdkasy_t __iomem *ap;
1436 cdkhdr_t __iomem *hdrp;
1437 unsigned char __iomem *bits;
1438 unsigned char __iomem *shbuf;
1439 unsigned char *chbuf;
1440 stliport_t *portp;
1441 stlibrd_t *brdp;
1442 unsigned int len, stlen, head, tail, size;
1443 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 if (tty == stli_txcooktty)
1446 stli_flushchars(tty);
1447 portp = tty->driver_data;
Alan Cox4ac43602006-06-28 04:26:52 -07001448 if (portp == NULL)
1449 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 if ((portp->brdnr < 0) || (portp->brdnr >= stli_nrbrds))
Alan Cox4ac43602006-06-28 04:26:52 -07001451 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 brdp = stli_brds[portp->brdnr];
Alan Cox4ac43602006-06-28 04:26:52 -07001453 if (brdp == NULL)
1454 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 chbuf = (unsigned char *) buf;
1456
1457/*
1458 * All data is now local, shove as much as possible into shared memory.
1459 */
Alan Cox4ac43602006-06-28 04:26:52 -07001460 spin_lock_irqsave(&brd_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 EBRDENABLE(brdp);
Alan Cox4ac43602006-06-28 04:26:52 -07001462 ap = (cdkasy_t __iomem *) EBRDGETMEMPTR(brdp, portp->addr);
1463 head = (unsigned int) readw(&ap->txq.head);
1464 tail = (unsigned int) readw(&ap->txq.tail);
1465 if (tail != ((unsigned int) readw(&ap->txq.tail)))
1466 tail = (unsigned int) readw(&ap->txq.tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 size = portp->txsize;
1468 if (head >= tail) {
1469 len = size - (head - tail) - 1;
1470 stlen = size - head;
1471 } else {
1472 len = tail - head - 1;
1473 stlen = len;
1474 }
1475
1476 len = MIN(len, count);
1477 count = 0;
Alan Cox4ac43602006-06-28 04:26:52 -07001478 shbuf = (char __iomem *) EBRDGETMEMPTR(brdp, portp->txoffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479
1480 while (len > 0) {
1481 stlen = MIN(len, stlen);
Alan Cox4ac43602006-06-28 04:26:52 -07001482 memcpy_toio(shbuf + head, chbuf, stlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 chbuf += stlen;
1484 len -= stlen;
1485 count += stlen;
1486 head += stlen;
1487 if (head >= size) {
1488 head = 0;
1489 stlen = tail;
1490 }
1491 }
1492
Alan Cox4ac43602006-06-28 04:26:52 -07001493 ap = (cdkasy_t __iomem *) EBRDGETMEMPTR(brdp, portp->addr);
1494 writew(head, &ap->txq.head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 if (test_bit(ST_TXBUSY, &portp->state)) {
Alan Cox4ac43602006-06-28 04:26:52 -07001496 if (readl(&ap->changed.data) & DT_TXEMPTY)
1497 writel(readl(&ap->changed.data) & ~DT_TXEMPTY, &ap->changed.data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 }
Alan Cox4ac43602006-06-28 04:26:52 -07001499 hdrp = (cdkhdr_t __iomem *) EBRDGETMEMPTR(brdp, CDK_CDKADDR);
1500 bits = ((unsigned char __iomem *) hdrp) + brdp->slaveoffset +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 portp->portidx;
Alan Cox4ac43602006-06-28 04:26:52 -07001502 writeb(readb(bits) | portp->portbit, bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 set_bit(ST_TXBUSY, &portp->state);
1504 EBRDDISABLE(brdp);
Alan Cox4ac43602006-06-28 04:26:52 -07001505 spin_unlock_irqrestore(&brd_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506
1507 return(count);
1508}
1509
1510/*****************************************************************************/
1511
1512/*
1513 * Output a single character. We put it into a temporary local buffer
1514 * (for speed) then write out that buffer when the flushchars routine
1515 * is called. There is a safety catch here so that if some other port
1516 * writes chars before the current buffer has been, then we write them
1517 * first them do the new ports.
1518 */
1519
1520static void stli_putchar(struct tty_struct *tty, unsigned char ch)
1521{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 if (tty != stli_txcooktty) {
Alan Cox4ac43602006-06-28 04:26:52 -07001523 if (stli_txcooktty != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 stli_flushchars(stli_txcooktty);
1525 stli_txcooktty = tty;
1526 }
1527
1528 stli_txcookbuf[stli_txcooksize++] = ch;
1529}
1530
1531/*****************************************************************************/
1532
1533/*
1534 * Transfer characters from the local TX cooking buffer to the board.
1535 * We sort of ignore the tty that gets passed in here. We rely on the
1536 * info stored with the TX cook buffer to tell us which port to flush
1537 * the data on. In any case we clean out the TX cook buffer, for re-use
1538 * by someone else.
1539 */
1540
1541static void stli_flushchars(struct tty_struct *tty)
1542{
Alan Cox4ac43602006-06-28 04:26:52 -07001543 cdkhdr_t __iomem *hdrp;
1544 unsigned char __iomem *bits;
1545 cdkasy_t __iomem *ap;
1546 struct tty_struct *cooktty;
1547 stliport_t *portp;
1548 stlibrd_t *brdp;
1549 unsigned int len, stlen, head, tail, size, count, cooksize;
1550 unsigned char *buf;
1551 unsigned char __iomem *shbuf;
1552 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553
1554 cooksize = stli_txcooksize;
1555 cooktty = stli_txcooktty;
1556 stli_txcooksize = 0;
1557 stli_txcookrealsize = 0;
Alan Cox4ac43602006-06-28 04:26:52 -07001558 stli_txcooktty = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559
Alan Cox4ac43602006-06-28 04:26:52 -07001560 if (tty == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 return;
Alan Cox4ac43602006-06-28 04:26:52 -07001562 if (cooktty == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 return;
1564 if (tty != cooktty)
1565 tty = cooktty;
1566 if (cooksize == 0)
1567 return;
1568
1569 portp = tty->driver_data;
Alan Cox4ac43602006-06-28 04:26:52 -07001570 if (portp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 return;
1572 if ((portp->brdnr < 0) || (portp->brdnr >= stli_nrbrds))
1573 return;
1574 brdp = stli_brds[portp->brdnr];
Alan Cox4ac43602006-06-28 04:26:52 -07001575 if (brdp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 return;
1577
Alan Cox4ac43602006-06-28 04:26:52 -07001578 spin_lock_irqsave(&brd_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 EBRDENABLE(brdp);
1580
Alan Cox4ac43602006-06-28 04:26:52 -07001581 ap = (cdkasy_t __iomem *) EBRDGETMEMPTR(brdp, portp->addr);
1582 head = (unsigned int) readw(&ap->txq.head);
1583 tail = (unsigned int) readw(&ap->txq.tail);
1584 if (tail != ((unsigned int) readw(&ap->txq.tail)))
1585 tail = (unsigned int) readw(&ap->txq.tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 size = portp->txsize;
1587 if (head >= tail) {
1588 len = size - (head - tail) - 1;
1589 stlen = size - head;
1590 } else {
1591 len = tail - head - 1;
1592 stlen = len;
1593 }
1594
1595 len = MIN(len, cooksize);
1596 count = 0;
1597 shbuf = (char *) EBRDGETMEMPTR(brdp, portp->txoffset);
1598 buf = stli_txcookbuf;
1599
1600 while (len > 0) {
1601 stlen = MIN(len, stlen);
Alan Cox4ac43602006-06-28 04:26:52 -07001602 memcpy_toio(shbuf + head, buf, stlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 buf += stlen;
1604 len -= stlen;
1605 count += stlen;
1606 head += stlen;
1607 if (head >= size) {
1608 head = 0;
1609 stlen = tail;
1610 }
1611 }
1612
Alan Cox4ac43602006-06-28 04:26:52 -07001613 ap = (cdkasy_t __iomem *) EBRDGETMEMPTR(brdp, portp->addr);
1614 writew(head, &ap->txq.head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615
1616 if (test_bit(ST_TXBUSY, &portp->state)) {
Alan Cox4ac43602006-06-28 04:26:52 -07001617 if (readl(&ap->changed.data) & DT_TXEMPTY)
1618 writel(readl(&ap->changed.data) & ~DT_TXEMPTY, &ap->changed.data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 }
Alan Cox4ac43602006-06-28 04:26:52 -07001620 hdrp = (cdkhdr_t __iomem *) EBRDGETMEMPTR(brdp, CDK_CDKADDR);
1621 bits = ((unsigned char __iomem *) hdrp) + brdp->slaveoffset +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 portp->portidx;
Alan Cox4ac43602006-06-28 04:26:52 -07001623 writeb(readb(bits) | portp->portbit, bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 set_bit(ST_TXBUSY, &portp->state);
1625
1626 EBRDDISABLE(brdp);
Alan Cox4ac43602006-06-28 04:26:52 -07001627 spin_unlock_irqrestore(&brd_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628}
1629
1630/*****************************************************************************/
1631
1632static int stli_writeroom(struct tty_struct *tty)
1633{
Alan Cox4ac43602006-06-28 04:26:52 -07001634 cdkasyrq_t __iomem *rp;
1635 stliport_t *portp;
1636 stlibrd_t *brdp;
1637 unsigned int head, tail, len;
1638 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 if (tty == stli_txcooktty) {
1641 if (stli_txcookrealsize != 0) {
1642 len = stli_txcookrealsize - stli_txcooksize;
Alan Cox4ac43602006-06-28 04:26:52 -07001643 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 }
1645 }
1646
1647 portp = tty->driver_data;
Alan Cox4ac43602006-06-28 04:26:52 -07001648 if (portp == NULL)
1649 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 if ((portp->brdnr < 0) || (portp->brdnr >= stli_nrbrds))
Alan Cox4ac43602006-06-28 04:26:52 -07001651 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 brdp = stli_brds[portp->brdnr];
Alan Cox4ac43602006-06-28 04:26:52 -07001653 if (brdp == NULL)
1654 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655
Alan Cox4ac43602006-06-28 04:26:52 -07001656 spin_lock_irqsave(&brd_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 EBRDENABLE(brdp);
Alan Cox4ac43602006-06-28 04:26:52 -07001658 rp = &((cdkasy_t __iomem *) EBRDGETMEMPTR(brdp, portp->addr))->txq;
1659 head = (unsigned int) readw(&rp->head);
1660 tail = (unsigned int) readw(&rp->tail);
1661 if (tail != ((unsigned int) readw(&rp->tail)))
1662 tail = (unsigned int) readw(&rp->tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 len = (head >= tail) ? (portp->txsize - (head - tail)) : (tail - head);
1664 len--;
1665 EBRDDISABLE(brdp);
Alan Cox4ac43602006-06-28 04:26:52 -07001666 spin_unlock_irqrestore(&brd_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667
1668 if (tty == stli_txcooktty) {
1669 stli_txcookrealsize = len;
1670 len -= stli_txcooksize;
1671 }
Alan Cox4ac43602006-06-28 04:26:52 -07001672 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673}
1674
1675/*****************************************************************************/
1676
1677/*
1678 * Return the number of characters in the transmit buffer. Normally we
1679 * will return the number of chars in the shared memory ring queue.
1680 * We need to kludge around the case where the shared memory buffer is
1681 * empty but not all characters have drained yet, for this case just
1682 * return that there is 1 character in the buffer!
1683 */
1684
1685static int stli_charsinbuffer(struct tty_struct *tty)
1686{
Alan Cox4ac43602006-06-28 04:26:52 -07001687 cdkasyrq_t __iomem *rp;
1688 stliport_t *portp;
1689 stlibrd_t *brdp;
1690 unsigned int head, tail, len;
1691 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 if (tty == stli_txcooktty)
1694 stli_flushchars(tty);
1695 portp = tty->driver_data;
Alan Cox4ac43602006-06-28 04:26:52 -07001696 if (portp == NULL)
1697 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 if ((portp->brdnr < 0) || (portp->brdnr >= stli_nrbrds))
Alan Cox4ac43602006-06-28 04:26:52 -07001699 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 brdp = stli_brds[portp->brdnr];
Alan Cox4ac43602006-06-28 04:26:52 -07001701 if (brdp == NULL)
1702 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703
Alan Cox4ac43602006-06-28 04:26:52 -07001704 spin_lock_irqsave(&brd_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 EBRDENABLE(brdp);
Alan Cox4ac43602006-06-28 04:26:52 -07001706 rp = &((cdkasy_t __iomem *) EBRDGETMEMPTR(brdp, portp->addr))->txq;
1707 head = (unsigned int) readw(&rp->head);
1708 tail = (unsigned int) readw(&rp->tail);
1709 if (tail != ((unsigned int) readw(&rp->tail)))
1710 tail = (unsigned int) readw(&rp->tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 len = (head >= tail) ? (head - tail) : (portp->txsize - (tail - head));
1712 if ((len == 0) && test_bit(ST_TXBUSY, &portp->state))
1713 len = 1;
1714 EBRDDISABLE(brdp);
Alan Cox4ac43602006-06-28 04:26:52 -07001715 spin_unlock_irqrestore(&brd_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716
Alan Cox4ac43602006-06-28 04:26:52 -07001717 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718}
1719
1720/*****************************************************************************/
1721
1722/*
1723 * Generate the serial struct info.
1724 */
1725
1726static int stli_getserial(stliport_t *portp, struct serial_struct __user *sp)
1727{
Alan Cox4ac43602006-06-28 04:26:52 -07001728 struct serial_struct sio;
1729 stlibrd_t *brdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730
1731 memset(&sio, 0, sizeof(struct serial_struct));
1732 sio.type = PORT_UNKNOWN;
1733 sio.line = portp->portnr;
1734 sio.irq = 0;
1735 sio.flags = portp->flags;
1736 sio.baud_base = portp->baud_base;
1737 sio.close_delay = portp->close_delay;
1738 sio.closing_wait = portp->closing_wait;
1739 sio.custom_divisor = portp->custom_divisor;
1740 sio.xmit_fifo_size = 0;
1741 sio.hub6 = 0;
1742
1743 brdp = stli_brds[portp->brdnr];
Alan Cox4ac43602006-06-28 04:26:52 -07001744 if (brdp != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 sio.port = brdp->iobase;
1746
1747 return copy_to_user(sp, &sio, sizeof(struct serial_struct)) ?
1748 -EFAULT : 0;
1749}
1750
1751/*****************************************************************************/
1752
1753/*
1754 * Set port according to the serial struct info.
1755 * At this point we do not do any auto-configure stuff, so we will
1756 * just quietly ignore any requests to change irq, etc.
1757 */
1758
1759static int stli_setserial(stliport_t *portp, struct serial_struct __user *sp)
1760{
Alan Cox4ac43602006-06-28 04:26:52 -07001761 struct serial_struct sio;
1762 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763
1764 if (copy_from_user(&sio, sp, sizeof(struct serial_struct)))
1765 return -EFAULT;
1766 if (!capable(CAP_SYS_ADMIN)) {
1767 if ((sio.baud_base != portp->baud_base) ||
1768 (sio.close_delay != portp->close_delay) ||
1769 ((sio.flags & ~ASYNC_USR_MASK) !=
1770 (portp->flags & ~ASYNC_USR_MASK)))
Alan Cox4ac43602006-06-28 04:26:52 -07001771 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 }
1773
1774 portp->flags = (portp->flags & ~ASYNC_USR_MASK) |
1775 (sio.flags & ASYNC_USR_MASK);
1776 portp->baud_base = sio.baud_base;
1777 portp->close_delay = sio.close_delay;
1778 portp->closing_wait = sio.closing_wait;
1779 portp->custom_divisor = sio.custom_divisor;
1780
1781 if ((rc = stli_setport(portp)) < 0)
Alan Cox4ac43602006-06-28 04:26:52 -07001782 return rc;
1783 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784}
1785
1786/*****************************************************************************/
1787
1788static int stli_tiocmget(struct tty_struct *tty, struct file *file)
1789{
1790 stliport_t *portp = tty->driver_data;
1791 stlibrd_t *brdp;
1792 int rc;
1793
Alan Cox4ac43602006-06-28 04:26:52 -07001794 if (portp == NULL)
1795 return -ENODEV;
1796 if (portp->brdnr < 0 || portp->brdnr >= stli_nrbrds)
1797 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 brdp = stli_brds[portp->brdnr];
Alan Cox4ac43602006-06-28 04:26:52 -07001799 if (brdp == NULL)
1800 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 if (tty->flags & (1 << TTY_IO_ERROR))
Alan Cox4ac43602006-06-28 04:26:52 -07001802 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803
1804 if ((rc = stli_cmdwait(brdp, portp, A_GETSIGNALS,
1805 &portp->asig, sizeof(asysigs_t), 1)) < 0)
Alan Cox4ac43602006-06-28 04:26:52 -07001806 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807
1808 return stli_mktiocm(portp->asig.sigvalue);
1809}
1810
1811static int stli_tiocmset(struct tty_struct *tty, struct file *file,
1812 unsigned int set, unsigned int clear)
1813{
1814 stliport_t *portp = tty->driver_data;
1815 stlibrd_t *brdp;
1816 int rts = -1, dtr = -1;
1817
Alan Cox4ac43602006-06-28 04:26:52 -07001818 if (portp == NULL)
1819 return -ENODEV;
1820 if (portp->brdnr < 0 || portp->brdnr >= stli_nrbrds)
1821 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 brdp = stli_brds[portp->brdnr];
Alan Cox4ac43602006-06-28 04:26:52 -07001823 if (brdp == NULL)
1824 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 if (tty->flags & (1 << TTY_IO_ERROR))
Alan Cox4ac43602006-06-28 04:26:52 -07001826 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827
1828 if (set & TIOCM_RTS)
1829 rts = 1;
1830 if (set & TIOCM_DTR)
1831 dtr = 1;
1832 if (clear & TIOCM_RTS)
1833 rts = 0;
1834 if (clear & TIOCM_DTR)
1835 dtr = 0;
1836
1837 stli_mkasysigs(&portp->asig, dtr, rts);
1838
1839 return stli_cmdwait(brdp, portp, A_SETSIGNALS, &portp->asig,
1840 sizeof(asysigs_t), 0);
1841}
1842
1843static int stli_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg)
1844{
Alan Cox4ac43602006-06-28 04:26:52 -07001845 stliport_t *portp;
1846 stlibrd_t *brdp;
1847 unsigned int ival;
1848 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 void __user *argp = (void __user *)arg;
1850
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 portp = tty->driver_data;
Alan Cox4ac43602006-06-28 04:26:52 -07001852 if (portp == NULL)
1853 return -ENODEV;
1854 if (portp->brdnr < 0 || portp->brdnr >= stli_nrbrds)
1855 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 brdp = stli_brds[portp->brdnr];
Alan Cox4ac43602006-06-28 04:26:52 -07001857 if (brdp == NULL)
1858 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859
1860 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1861 (cmd != COM_GETPORTSTATS) && (cmd != COM_CLRPORTSTATS)) {
1862 if (tty->flags & (1 << TTY_IO_ERROR))
Alan Cox4ac43602006-06-28 04:26:52 -07001863 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 }
1865
1866 rc = 0;
1867
1868 switch (cmd) {
1869 case TIOCGSOFTCAR:
1870 rc = put_user(((tty->termios->c_cflag & CLOCAL) ? 1 : 0),
1871 (unsigned __user *) arg);
1872 break;
1873 case TIOCSSOFTCAR:
1874 if ((rc = get_user(ival, (unsigned __user *) arg)) == 0)
1875 tty->termios->c_cflag =
1876 (tty->termios->c_cflag & ~CLOCAL) |
1877 (ival ? CLOCAL : 0);
1878 break;
1879 case TIOCGSERIAL:
1880 rc = stli_getserial(portp, argp);
1881 break;
1882 case TIOCSSERIAL:
1883 rc = stli_setserial(portp, argp);
1884 break;
1885 case STL_GETPFLAG:
1886 rc = put_user(portp->pflag, (unsigned __user *)argp);
1887 break;
1888 case STL_SETPFLAG:
1889 if ((rc = get_user(portp->pflag, (unsigned __user *)argp)) == 0)
1890 stli_setport(portp);
1891 break;
1892 case COM_GETPORTSTATS:
1893 rc = stli_getportstats(portp, argp);
1894 break;
1895 case COM_CLRPORTSTATS:
1896 rc = stli_clrportstats(portp, argp);
1897 break;
1898 case TIOCSERCONFIG:
1899 case TIOCSERGWILD:
1900 case TIOCSERSWILD:
1901 case TIOCSERGETLSR:
1902 case TIOCSERGSTRUCT:
1903 case TIOCSERGETMULTI:
1904 case TIOCSERSETMULTI:
1905 default:
1906 rc = -ENOIOCTLCMD;
1907 break;
1908 }
1909
Alan Cox4ac43602006-06-28 04:26:52 -07001910 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911}
1912
1913/*****************************************************************************/
1914
1915/*
1916 * This routine assumes that we have user context and can sleep.
1917 * Looks like it is true for the current ttys implementation..!!
1918 */
1919
1920static void stli_settermios(struct tty_struct *tty, struct termios *old)
1921{
Alan Cox4ac43602006-06-28 04:26:52 -07001922 stliport_t *portp;
1923 stlibrd_t *brdp;
1924 struct termios *tiosp;
1925 asyport_t aport;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926
Alan Cox4ac43602006-06-28 04:26:52 -07001927 if (tty == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 return;
1929 portp = tty->driver_data;
Alan Cox4ac43602006-06-28 04:26:52 -07001930 if (portp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 return;
Alan Cox4ac43602006-06-28 04:26:52 -07001932 if (portp->brdnr < 0 || portp->brdnr >= stli_nrbrds)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 return;
1934 brdp = stli_brds[portp->brdnr];
Alan Cox4ac43602006-06-28 04:26:52 -07001935 if (brdp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 return;
1937
1938 tiosp = tty->termios;
1939 if ((tiosp->c_cflag == old->c_cflag) &&
1940 (tiosp->c_iflag == old->c_iflag))
1941 return;
1942
1943 stli_mkasyport(portp, &aport, tiosp);
1944 stli_cmdwait(brdp, portp, A_SETPORT, &aport, sizeof(asyport_t), 0);
1945 stli_mkasysigs(&portp->asig, ((tiosp->c_cflag & CBAUD) ? 1 : 0), -1);
1946 stli_cmdwait(brdp, portp, A_SETSIGNALS, &portp->asig,
1947 sizeof(asysigs_t), 0);
1948 if ((old->c_cflag & CRTSCTS) && ((tiosp->c_cflag & CRTSCTS) == 0))
1949 tty->hw_stopped = 0;
1950 if (((old->c_cflag & CLOCAL) == 0) && (tiosp->c_cflag & CLOCAL))
1951 wake_up_interruptible(&portp->open_wait);
1952}
1953
1954/*****************************************************************************/
1955
1956/*
1957 * Attempt to flow control who ever is sending us data. We won't really
1958 * do any flow control action here. We can't directly, and even if we
1959 * wanted to we would have to send a command to the slave. The slave
1960 * knows how to flow control, and will do so when its buffers reach its
1961 * internal high water marks. So what we will do is set a local state
1962 * bit that will stop us sending any RX data up from the poll routine
1963 * (which is the place where RX data from the slave is handled).
1964 */
1965
1966static void stli_throttle(struct tty_struct *tty)
1967{
Alan Cox4ac43602006-06-28 04:26:52 -07001968 stliport_t *portp = tty->driver_data;
1969 if (portp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 set_bit(ST_RXSTOP, &portp->state);
1972}
1973
1974/*****************************************************************************/
1975
1976/*
1977 * Unflow control the device sending us data... That means that all
1978 * we have to do is clear the RXSTOP state bit. The next poll call
1979 * will then be able to pass the RX data back up.
1980 */
1981
1982static void stli_unthrottle(struct tty_struct *tty)
1983{
Alan Cox4ac43602006-06-28 04:26:52 -07001984 stliport_t *portp = tty->driver_data;
1985 if (portp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 clear_bit(ST_RXSTOP, &portp->state);
1988}
1989
1990/*****************************************************************************/
1991
1992/*
Alan Cox4ac43602006-06-28 04:26:52 -07001993 * Stop the transmitter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 */
1995
1996static void stli_stop(struct tty_struct *tty)
1997{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998}
1999
2000/*****************************************************************************/
2001
2002/*
Alan Cox4ac43602006-06-28 04:26:52 -07002003 * Start the transmitter again.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 */
2005
2006static void stli_start(struct tty_struct *tty)
2007{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008}
2009
2010/*****************************************************************************/
2011
2012/*
2013 * Scheduler called hang up routine. This is called from the scheduler,
2014 * not direct from the driver "poll" routine. We can't call it there
2015 * since the real local hangup code will enable/disable the board and
2016 * other things that we can't do while handling the poll. Much easier
2017 * to deal with it some time later (don't really care when, hangups
2018 * aren't that time critical).
2019 */
2020
2021static void stli_dohangup(void *arg)
2022{
Alan Cox4ac43602006-06-28 04:26:52 -07002023 stliport_t *portp = (stliport_t *) arg;
2024 if (portp->tty != NULL) {
2025 tty_hangup(portp->tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 }
2027}
2028
2029/*****************************************************************************/
2030
2031/*
2032 * Hangup this port. This is pretty much like closing the port, only
2033 * a little more brutal. No waiting for data to drain. Shutdown the
2034 * port and maybe drop signals. This is rather tricky really. We want
2035 * to close the port as well.
2036 */
2037
2038static void stli_hangup(struct tty_struct *tty)
2039{
Alan Cox4ac43602006-06-28 04:26:52 -07002040 stliport_t *portp;
2041 stlibrd_t *brdp;
2042 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 portp = tty->driver_data;
Alan Cox4ac43602006-06-28 04:26:52 -07002045 if (portp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 return;
Alan Cox4ac43602006-06-28 04:26:52 -07002047 if (portp->brdnr < 0 || portp->brdnr >= stli_nrbrds)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 return;
2049 brdp = stli_brds[portp->brdnr];
Alan Cox4ac43602006-06-28 04:26:52 -07002050 if (brdp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 return;
2052
2053 portp->flags &= ~ASYNC_INITIALIZED;
2054
Alan Cox4ac43602006-06-28 04:26:52 -07002055 if (!test_bit(ST_CLOSING, &portp->state))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 stli_rawclose(brdp, portp, 0, 0);
Alan Cox4ac43602006-06-28 04:26:52 -07002057
2058 spin_lock_irqsave(&stli_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 if (tty->termios->c_cflag & HUPCL) {
2060 stli_mkasysigs(&portp->asig, 0, 0);
2061 if (test_bit(ST_CMDING, &portp->state)) {
2062 set_bit(ST_DOSIGS, &portp->state);
2063 set_bit(ST_DOFLUSHTX, &portp->state);
2064 set_bit(ST_DOFLUSHRX, &portp->state);
2065 } else {
2066 stli_sendcmd(brdp, portp, A_SETSIGNALSF,
2067 &portp->asig, sizeof(asysigs_t), 0);
2068 }
2069 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070
2071 clear_bit(ST_TXBUSY, &portp->state);
2072 clear_bit(ST_RXSTOP, &portp->state);
2073 set_bit(TTY_IO_ERROR, &tty->flags);
Alan Cox4ac43602006-06-28 04:26:52 -07002074 portp->tty = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 portp->flags &= ~ASYNC_NORMAL_ACTIVE;
2076 portp->refcount = 0;
Alan Cox4ac43602006-06-28 04:26:52 -07002077 spin_unlock_irqrestore(&stli_lock, flags);
2078
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 wake_up_interruptible(&portp->open_wait);
2080}
2081
2082/*****************************************************************************/
2083
2084/*
2085 * Flush characters from the lower buffer. We may not have user context
2086 * so we cannot sleep waiting for it to complete. Also we need to check
2087 * if there is chars for this port in the TX cook buffer, and flush them
2088 * as well.
2089 */
2090
2091static void stli_flushbuffer(struct tty_struct *tty)
2092{
Alan Cox4ac43602006-06-28 04:26:52 -07002093 stliport_t *portp;
2094 stlibrd_t *brdp;
2095 unsigned long ftype, flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 portp = tty->driver_data;
Alan Cox4ac43602006-06-28 04:26:52 -07002098 if (portp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 return;
Alan Cox4ac43602006-06-28 04:26:52 -07002100 if (portp->brdnr < 0 || portp->brdnr >= stli_nrbrds)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 return;
2102 brdp = stli_brds[portp->brdnr];
Alan Cox4ac43602006-06-28 04:26:52 -07002103 if (brdp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 return;
2105
Alan Cox4ac43602006-06-28 04:26:52 -07002106 spin_lock_irqsave(&brd_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 if (tty == stli_txcooktty) {
Alan Cox4ac43602006-06-28 04:26:52 -07002108 stli_txcooktty = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 stli_txcooksize = 0;
2110 stli_txcookrealsize = 0;
2111 }
2112 if (test_bit(ST_CMDING, &portp->state)) {
2113 set_bit(ST_DOFLUSHTX, &portp->state);
2114 } else {
2115 ftype = FLUSHTX;
2116 if (test_bit(ST_DOFLUSHRX, &portp->state)) {
2117 ftype |= FLUSHRX;
2118 clear_bit(ST_DOFLUSHRX, &portp->state);
2119 }
Alan Cox4ac43602006-06-28 04:26:52 -07002120 __stli_sendcmd(brdp, portp, A_FLUSH, &ftype, sizeof(u32), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 }
Alan Cox4ac43602006-06-28 04:26:52 -07002122 spin_unlock_irqrestore(&brd_lock, flags);
2123 tty_wakeup(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124}
2125
2126/*****************************************************************************/
2127
2128static void stli_breakctl(struct tty_struct *tty, int state)
2129{
2130 stlibrd_t *brdp;
2131 stliport_t *portp;
2132 long arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 portp = tty->driver_data;
Alan Cox4ac43602006-06-28 04:26:52 -07002135 if (portp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 return;
Alan Cox4ac43602006-06-28 04:26:52 -07002137 if (portp->brdnr < 0 || portp->brdnr >= stli_nrbrds)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 return;
2139 brdp = stli_brds[portp->brdnr];
Alan Cox4ac43602006-06-28 04:26:52 -07002140 if (brdp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 return;
2142
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 arg = (state == -1) ? BREAKON : BREAKOFF;
2144 stli_cmdwait(brdp, portp, A_BREAK, &arg, sizeof(long), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145}
2146
2147/*****************************************************************************/
2148
2149static void stli_waituntilsent(struct tty_struct *tty, int timeout)
2150{
Alan Cox4ac43602006-06-28 04:26:52 -07002151 stliport_t *portp;
2152 unsigned long tend;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153
Alan Cox4ac43602006-06-28 04:26:52 -07002154 if (tty == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 return;
2156 portp = tty->driver_data;
Alan Cox4ac43602006-06-28 04:26:52 -07002157 if (portp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 return;
2159
2160 if (timeout == 0)
2161 timeout = HZ;
2162 tend = jiffies + timeout;
2163
2164 while (test_bit(ST_TXBUSY, &portp->state)) {
2165 if (signal_pending(current))
2166 break;
2167 msleep_interruptible(20);
2168 if (time_after_eq(jiffies, tend))
2169 break;
2170 }
2171}
2172
2173/*****************************************************************************/
2174
2175static void stli_sendxchar(struct tty_struct *tty, char ch)
2176{
2177 stlibrd_t *brdp;
2178 stliport_t *portp;
2179 asyctrl_t actrl;
2180
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 portp = tty->driver_data;
Alan Cox4ac43602006-06-28 04:26:52 -07002182 if (portp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 return;
Alan Cox4ac43602006-06-28 04:26:52 -07002184 if (portp->brdnr < 0 || portp->brdnr >= stli_nrbrds)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 return;
2186 brdp = stli_brds[portp->brdnr];
Alan Cox4ac43602006-06-28 04:26:52 -07002187 if (brdp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 return;
2189
2190 memset(&actrl, 0, sizeof(asyctrl_t));
2191 if (ch == STOP_CHAR(tty)) {
2192 actrl.rxctrl = CT_STOPFLOW;
2193 } else if (ch == START_CHAR(tty)) {
2194 actrl.rxctrl = CT_STARTFLOW;
2195 } else {
2196 actrl.txctrl = CT_SENDCHR;
2197 actrl.tximdch = ch;
2198 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 stli_cmdwait(brdp, portp, A_PORTCTRL, &actrl, sizeof(asyctrl_t), 0);
2200}
2201
2202/*****************************************************************************/
2203
2204#define MAXLINE 80
2205
2206/*
2207 * Format info for a specified port. The line is deliberately limited
2208 * to 80 characters. (If it is too long it will be truncated, if too
2209 * short then padded with spaces).
2210 */
2211
2212static int stli_portinfo(stlibrd_t *brdp, stliport_t *portp, int portnr, char *pos)
2213{
Alan Cox4ac43602006-06-28 04:26:52 -07002214 char *sp, *uart;
2215 int rc, cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216
2217 rc = stli_portcmdstats(portp);
2218
2219 uart = "UNKNOWN";
2220 if (brdp->state & BST_STARTED) {
2221 switch (stli_comstats.hwid) {
Alan Cox4ac43602006-06-28 04:26:52 -07002222 case 0: uart = "2681"; break;
2223 case 1: uart = "SC26198"; break;
2224 default:uart = "CD1400"; break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 }
2226 }
2227
2228 sp = pos;
2229 sp += sprintf(sp, "%d: uart:%s ", portnr, uart);
2230
2231 if ((brdp->state & BST_STARTED) && (rc >= 0)) {
2232 sp += sprintf(sp, "tx:%d rx:%d", (int) stli_comstats.txtotal,
2233 (int) stli_comstats.rxtotal);
2234
2235 if (stli_comstats.rxframing)
2236 sp += sprintf(sp, " fe:%d",
2237 (int) stli_comstats.rxframing);
2238 if (stli_comstats.rxparity)
2239 sp += sprintf(sp, " pe:%d",
2240 (int) stli_comstats.rxparity);
2241 if (stli_comstats.rxbreaks)
2242 sp += sprintf(sp, " brk:%d",
2243 (int) stli_comstats.rxbreaks);
2244 if (stli_comstats.rxoverrun)
2245 sp += sprintf(sp, " oe:%d",
2246 (int) stli_comstats.rxoverrun);
2247
2248 cnt = sprintf(sp, "%s%s%s%s%s ",
2249 (stli_comstats.signals & TIOCM_RTS) ? "|RTS" : "",
2250 (stli_comstats.signals & TIOCM_CTS) ? "|CTS" : "",
2251 (stli_comstats.signals & TIOCM_DTR) ? "|DTR" : "",
2252 (stli_comstats.signals & TIOCM_CD) ? "|DCD" : "",
2253 (stli_comstats.signals & TIOCM_DSR) ? "|DSR" : "");
2254 *sp = ' ';
2255 sp += cnt;
2256 }
2257
2258 for (cnt = (sp - pos); (cnt < (MAXLINE - 1)); cnt++)
2259 *sp++ = ' ';
2260 if (cnt >= MAXLINE)
2261 pos[(MAXLINE - 2)] = '+';
2262 pos[(MAXLINE - 1)] = '\n';
2263
2264 return(MAXLINE);
2265}
2266
2267/*****************************************************************************/
2268
2269/*
2270 * Port info, read from the /proc file system.
2271 */
2272
2273static int stli_readproc(char *page, char **start, off_t off, int count, int *eof, void *data)
2274{
Alan Cox4ac43602006-06-28 04:26:52 -07002275 stlibrd_t *brdp;
2276 stliport_t *portp;
2277 int brdnr, portnr, totalport;
2278 int curoff, maxoff;
2279 char *pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280
2281 pos = page;
2282 totalport = 0;
2283 curoff = 0;
2284
2285 if (off == 0) {
2286 pos += sprintf(pos, "%s: version %s", stli_drvtitle,
2287 stli_drvversion);
2288 while (pos < (page + MAXLINE - 1))
2289 *pos++ = ' ';
2290 *pos++ = '\n';
2291 }
2292 curoff = MAXLINE;
2293
2294/*
2295 * We scan through for each board, panel and port. The offset is
2296 * calculated on the fly, and irrelevant ports are skipped.
2297 */
2298 for (brdnr = 0; (brdnr < stli_nrbrds); brdnr++) {
2299 brdp = stli_brds[brdnr];
Alan Cox4ac43602006-06-28 04:26:52 -07002300 if (brdp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 continue;
2302 if (brdp->state == 0)
2303 continue;
2304
2305 maxoff = curoff + (brdp->nrports * MAXLINE);
2306 if (off >= maxoff) {
2307 curoff = maxoff;
2308 continue;
2309 }
2310
2311 totalport = brdnr * STL_MAXPORTS;
2312 for (portnr = 0; (portnr < brdp->nrports); portnr++,
2313 totalport++) {
2314 portp = brdp->ports[portnr];
Alan Cox4ac43602006-06-28 04:26:52 -07002315 if (portp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 continue;
2317 if (off >= (curoff += MAXLINE))
2318 continue;
2319 if ((pos - page + MAXLINE) > count)
2320 goto stli_readdone;
2321 pos += stli_portinfo(brdp, portp, totalport, pos);
2322 }
2323 }
2324
2325 *eof = 1;
2326
2327stli_readdone:
2328 *start = page;
2329 return(pos - page);
2330}
2331
2332/*****************************************************************************/
2333
2334/*
2335 * Generic send command routine. This will send a message to the slave,
2336 * of the specified type with the specified argument. Must be very
2337 * careful of data that will be copied out from shared memory -
2338 * containing command results. The command completion is all done from
2339 * a poll routine that does not have user context. Therefore you cannot
2340 * copy back directly into user space, or to the kernel stack of a
2341 * process. This routine does not sleep, so can be called from anywhere.
Alan Cox4ac43602006-06-28 04:26:52 -07002342 *
2343 * The caller must hold the brd_lock (see also stli_sendcmd the usual
2344 * entry point)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 */
2346
Alan Cox4ac43602006-06-28 04:26:52 -07002347static 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 -07002348{
Alan Cox4ac43602006-06-28 04:26:52 -07002349 cdkhdr_t __iomem *hdrp;
2350 cdkctrl_t __iomem *cp;
2351 unsigned char __iomem *bits;
2352 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353
Alan Cox4ac43602006-06-28 04:26:52 -07002354 spin_lock_irqsave(&brd_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355
2356 if (test_bit(ST_CMDING, &portp->state)) {
2357 printk(KERN_ERR "STALLION: command already busy, cmd=%x!\n",
2358 (int) cmd);
Alan Cox4ac43602006-06-28 04:26:52 -07002359 spin_unlock_irqrestore(&brd_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 return;
2361 }
2362
2363 EBRDENABLE(brdp);
Alan Cox4ac43602006-06-28 04:26:52 -07002364 cp = &((cdkasy_t __iomem *) EBRDGETMEMPTR(brdp, portp->addr))->ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 if (size > 0) {
Alan Cox4ac43602006-06-28 04:26:52 -07002366 memcpy_toio((void __iomem *) &(cp->args[0]), arg, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 if (copyback) {
2368 portp->argp = arg;
2369 portp->argsize = size;
2370 }
2371 }
Alan Cox4ac43602006-06-28 04:26:52 -07002372 writel(0, &cp->status);
2373 writel(cmd, &cp->cmd);
2374 hdrp = (cdkhdr_t __iomem *) EBRDGETMEMPTR(brdp, CDK_CDKADDR);
2375 bits = ((unsigned char __iomem *) hdrp) + brdp->slaveoffset +
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 portp->portidx;
Alan Cox4ac43602006-06-28 04:26:52 -07002377 writeb(readb(bits) | portp->portbit, bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 set_bit(ST_CMDING, &portp->state);
2379 EBRDDISABLE(brdp);
Alan Cox4ac43602006-06-28 04:26:52 -07002380 spin_unlock_irqrestore(&brd_lock, flags);
2381}
2382
2383static void stli_sendcmd(stlibrd_t *brdp, stliport_t *portp, unsigned long cmd, void *arg, int size, int copyback)
2384{
2385 unsigned long flags;
2386
2387 spin_lock_irqsave(&brd_lock, flags);
2388 __stli_sendcmd(brdp, portp, cmd, arg, size, copyback);
2389 spin_unlock_irqrestore(&brd_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390}
2391
2392/*****************************************************************************/
2393
2394/*
2395 * Read data from shared memory. This assumes that the shared memory
2396 * is enabled and that interrupts are off. Basically we just empty out
2397 * the shared memory buffer into the tty buffer. Must be careful to
2398 * handle the case where we fill up the tty buffer, but still have
2399 * more chars to unload.
2400 */
2401
2402static void stli_read(stlibrd_t *brdp, stliport_t *portp)
2403{
Alan Cox4ac43602006-06-28 04:26:52 -07002404 cdkasyrq_t __iomem *rp;
2405 char __iomem *shbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 struct tty_struct *tty;
Alan Cox4ac43602006-06-28 04:26:52 -07002407 unsigned int head, tail, size;
2408 unsigned int len, stlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409
2410 if (test_bit(ST_RXSTOP, &portp->state))
2411 return;
2412 tty = portp->tty;
Alan Cox4ac43602006-06-28 04:26:52 -07002413 if (tty == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 return;
2415
Alan Cox4ac43602006-06-28 04:26:52 -07002416 rp = &((cdkasy_t __iomem *) EBRDGETMEMPTR(brdp, portp->addr))->rxq;
2417 head = (unsigned int) readw(&rp->head);
2418 if (head != ((unsigned int) readw(&rp->head)))
2419 head = (unsigned int) readw(&rp->head);
2420 tail = (unsigned int) readw(&rp->tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 size = portp->rxsize;
2422 if (head >= tail) {
2423 len = head - tail;
2424 stlen = len;
2425 } else {
2426 len = size - (tail - head);
2427 stlen = size - tail;
2428 }
2429
Alan Cox33f0f882006-01-09 20:54:13 -08002430 len = tty_buffer_request_room(tty, len);
Alan Cox4ac43602006-06-28 04:26:52 -07002431
2432 shbuf = (char __iomem *) EBRDGETMEMPTR(brdp, portp->rxoffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433
2434 while (len > 0) {
Alan Cox4ac43602006-06-28 04:26:52 -07002435 unsigned char *cptr;
2436
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 stlen = MIN(len, stlen);
Alan Cox4ac43602006-06-28 04:26:52 -07002438 tty_prepare_flip_string(tty, &cptr, stlen);
2439 memcpy_fromio(cptr, shbuf + tail, stlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 len -= stlen;
2441 tail += stlen;
2442 if (tail >= size) {
2443 tail = 0;
2444 stlen = head;
2445 }
2446 }
Alan Cox4ac43602006-06-28 04:26:52 -07002447 rp = &((cdkasy_t __iomem *) EBRDGETMEMPTR(brdp, portp->addr))->rxq;
2448 writew(tail, &rp->tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449
2450 if (head != tail)
2451 set_bit(ST_RXING, &portp->state);
2452
2453 tty_schedule_flip(tty);
2454}
2455
2456/*****************************************************************************/
2457
2458/*
2459 * Set up and carry out any delayed commands. There is only a small set
2460 * of slave commands that can be done "off-level". So it is not too
2461 * difficult to deal with them here.
2462 */
2463
Alan Cox4ac43602006-06-28 04:26:52 -07002464static void stli_dodelaycmd(stliport_t *portp, cdkctrl_t __iomem *cp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465{
Alan Cox4ac43602006-06-28 04:26:52 -07002466 int cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467
2468 if (test_bit(ST_DOSIGS, &portp->state)) {
2469 if (test_bit(ST_DOFLUSHTX, &portp->state) &&
2470 test_bit(ST_DOFLUSHRX, &portp->state))
2471 cmd = A_SETSIGNALSF;
2472 else if (test_bit(ST_DOFLUSHTX, &portp->state))
2473 cmd = A_SETSIGNALSFTX;
2474 else if (test_bit(ST_DOFLUSHRX, &portp->state))
2475 cmd = A_SETSIGNALSFRX;
2476 else
2477 cmd = A_SETSIGNALS;
2478 clear_bit(ST_DOFLUSHTX, &portp->state);
2479 clear_bit(ST_DOFLUSHRX, &portp->state);
2480 clear_bit(ST_DOSIGS, &portp->state);
Alan Cox4ac43602006-06-28 04:26:52 -07002481 memcpy_toio((void __iomem *) &(cp->args[0]), (void *) &portp->asig,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 sizeof(asysigs_t));
Alan Cox4ac43602006-06-28 04:26:52 -07002483 writel(0, &cp->status);
2484 writel(cmd, &cp->cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 set_bit(ST_CMDING, &portp->state);
2486 } else if (test_bit(ST_DOFLUSHTX, &portp->state) ||
2487 test_bit(ST_DOFLUSHRX, &portp->state)) {
2488 cmd = ((test_bit(ST_DOFLUSHTX, &portp->state)) ? FLUSHTX : 0);
2489 cmd |= ((test_bit(ST_DOFLUSHRX, &portp->state)) ? FLUSHRX : 0);
2490 clear_bit(ST_DOFLUSHTX, &portp->state);
2491 clear_bit(ST_DOFLUSHRX, &portp->state);
Alan Cox4ac43602006-06-28 04:26:52 -07002492 memcpy_toio((void __iomem *) &(cp->args[0]), (void *) &cmd, sizeof(int));
2493 writel(0, &cp->status);
2494 writel(A_FLUSH, &cp->cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 set_bit(ST_CMDING, &portp->state);
2496 }
2497}
2498
2499/*****************************************************************************/
2500
2501/*
2502 * Host command service checking. This handles commands or messages
2503 * coming from the slave to the host. Must have board shared memory
2504 * enabled and interrupts off when called. Notice that by servicing the
2505 * read data last we don't need to change the shared memory pointer
2506 * during processing (which is a slow IO operation).
2507 * Return value indicates if this port is still awaiting actions from
2508 * the slave (like open, command, or even TX data being sent). If 0
2509 * then port is still busy, otherwise no longer busy.
2510 */
2511
2512static int stli_hostcmd(stlibrd_t *brdp, stliport_t *portp)
2513{
Alan Cox4ac43602006-06-28 04:26:52 -07002514 cdkasy_t __iomem *ap;
2515 cdkctrl_t __iomem *cp;
2516 struct tty_struct *tty;
2517 asynotify_t nt;
2518 unsigned long oldsigs;
2519 int rc, donerx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520
Alan Cox4ac43602006-06-28 04:26:52 -07002521 ap = (cdkasy_t __iomem *) EBRDGETMEMPTR(brdp, portp->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 cp = &ap->ctrl;
2523
2524/*
2525 * Check if we are waiting for an open completion message.
2526 */
2527 if (test_bit(ST_OPENING, &portp->state)) {
Alan Cox4ac43602006-06-28 04:26:52 -07002528 rc = readl(&cp->openarg);
2529 if (readb(&cp->open) == 0 && rc != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 if (rc > 0)
2531 rc--;
Alan Cox4ac43602006-06-28 04:26:52 -07002532 writel(0, &cp->openarg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 portp->rc = rc;
2534 clear_bit(ST_OPENING, &portp->state);
2535 wake_up_interruptible(&portp->raw_wait);
2536 }
2537 }
2538
2539/*
2540 * Check if we are waiting for a close completion message.
2541 */
2542 if (test_bit(ST_CLOSING, &portp->state)) {
Alan Cox4ac43602006-06-28 04:26:52 -07002543 rc = (int) readl(&cp->closearg);
2544 if (readb(&cp->close) == 0 && rc != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 if (rc > 0)
2546 rc--;
Alan Cox4ac43602006-06-28 04:26:52 -07002547 writel(0, &cp->closearg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 portp->rc = rc;
2549 clear_bit(ST_CLOSING, &portp->state);
2550 wake_up_interruptible(&portp->raw_wait);
2551 }
2552 }
2553
2554/*
2555 * Check if we are waiting for a command completion message. We may
2556 * need to copy out the command results associated with this command.
2557 */
2558 if (test_bit(ST_CMDING, &portp->state)) {
Alan Cox4ac43602006-06-28 04:26:52 -07002559 rc = readl(&cp->status);
2560 if (readl(&cp->cmd) == 0 && rc != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 if (rc > 0)
2562 rc--;
Alan Cox4ac43602006-06-28 04:26:52 -07002563 if (portp->argp != NULL) {
2564 memcpy_fromio(portp->argp, (void __iomem *) &(cp->args[0]),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 portp->argsize);
Alan Cox4ac43602006-06-28 04:26:52 -07002566 portp->argp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 }
Alan Cox4ac43602006-06-28 04:26:52 -07002568 writel(0, &cp->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 portp->rc = rc;
2570 clear_bit(ST_CMDING, &portp->state);
2571 stli_dodelaycmd(portp, cp);
2572 wake_up_interruptible(&portp->raw_wait);
2573 }
2574 }
2575
2576/*
2577 * Check for any notification messages ready. This includes lots of
2578 * different types of events - RX chars ready, RX break received,
2579 * TX data low or empty in the slave, modem signals changed state.
2580 */
2581 donerx = 0;
2582
2583 if (ap->notify) {
2584 nt = ap->changed;
2585 ap->notify = 0;
2586 tty = portp->tty;
2587
2588 if (nt.signal & SG_DCD) {
2589 oldsigs = portp->sigs;
2590 portp->sigs = stli_mktiocm(nt.sigvalue);
2591 clear_bit(ST_GETSIGS, &portp->state);
2592 if ((portp->sigs & TIOCM_CD) &&
2593 ((oldsigs & TIOCM_CD) == 0))
2594 wake_up_interruptible(&portp->open_wait);
2595 if ((oldsigs & TIOCM_CD) &&
2596 ((portp->sigs & TIOCM_CD) == 0)) {
2597 if (portp->flags & ASYNC_CHECK_CD) {
2598 if (tty)
2599 schedule_work(&portp->tqhangup);
2600 }
2601 }
2602 }
2603
2604 if (nt.data & DT_TXEMPTY)
2605 clear_bit(ST_TXBUSY, &portp->state);
2606 if (nt.data & (DT_TXEMPTY | DT_TXLOW)) {
Alan Cox4ac43602006-06-28 04:26:52 -07002607 if (tty != NULL) {
2608 tty_wakeup(tty);
2609 EBRDENABLE(brdp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 wake_up_interruptible(&tty->write_wait);
2611 }
2612 }
2613
2614 if ((nt.data & DT_RXBREAK) && (portp->rxmarkmsk & BRKINT)) {
Alan Cox4ac43602006-06-28 04:26:52 -07002615 if (tty != NULL) {
Alan Cox33f0f882006-01-09 20:54:13 -08002616 tty_insert_flip_char(tty, 0, TTY_BREAK);
2617 if (portp->flags & ASYNC_SAK) {
2618 do_SAK(tty);
2619 EBRDENABLE(brdp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620 }
Alan Cox33f0f882006-01-09 20:54:13 -08002621 tty_schedule_flip(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 }
2623 }
2624
2625 if (nt.data & DT_RXBUSY) {
2626 donerx++;
2627 stli_read(brdp, portp);
2628 }
2629 }
2630
2631/*
2632 * It might seem odd that we are checking for more RX chars here.
2633 * But, we need to handle the case where the tty buffer was previously
2634 * filled, but we had more characters to pass up. The slave will not
2635 * send any more RX notify messages until the RX buffer has been emptied.
2636 * But it will leave the service bits on (since the buffer is not empty).
2637 * So from here we can try to process more RX chars.
2638 */
2639 if ((!donerx) && test_bit(ST_RXING, &portp->state)) {
2640 clear_bit(ST_RXING, &portp->state);
2641 stli_read(brdp, portp);
2642 }
2643
2644 return((test_bit(ST_OPENING, &portp->state) ||
2645 test_bit(ST_CLOSING, &portp->state) ||
2646 test_bit(ST_CMDING, &portp->state) ||
2647 test_bit(ST_TXBUSY, &portp->state) ||
2648 test_bit(ST_RXING, &portp->state)) ? 0 : 1);
2649}
2650
2651/*****************************************************************************/
2652
2653/*
2654 * Service all ports on a particular board. Assumes that the boards
2655 * shared memory is enabled, and that the page pointer is pointed
2656 * at the cdk header structure.
2657 */
2658
Alan Cox4ac43602006-06-28 04:26:52 -07002659static void stli_brdpoll(stlibrd_t *brdp, cdkhdr_t __iomem *hdrp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660{
Alan Cox4ac43602006-06-28 04:26:52 -07002661 stliport_t *portp;
2662 unsigned char hostbits[(STL_MAXCHANS / 8) + 1];
2663 unsigned char slavebits[(STL_MAXCHANS / 8) + 1];
2664 unsigned char __iomem *slavep;
2665 int bitpos, bitat, bitsize;
2666 int channr, nrdevs, slavebitchange;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667
2668 bitsize = brdp->bitsize;
2669 nrdevs = brdp->nrdevs;
2670
2671/*
2672 * Check if slave wants any service. Basically we try to do as
2673 * little work as possible here. There are 2 levels of service
2674 * bits. So if there is nothing to do we bail early. We check
2675 * 8 service bits at a time in the inner loop, so we can bypass
2676 * the lot if none of them want service.
2677 */
Alan Cox4ac43602006-06-28 04:26:52 -07002678 memcpy_fromio(&hostbits[0], (((unsigned char __iomem *) hdrp) + brdp->hostoffset),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 bitsize);
2680
2681 memset(&slavebits[0], 0, bitsize);
2682 slavebitchange = 0;
2683
2684 for (bitpos = 0; (bitpos < bitsize); bitpos++) {
2685 if (hostbits[bitpos] == 0)
2686 continue;
2687 channr = bitpos * 8;
2688 for (bitat = 0x1; (channr < nrdevs); channr++, bitat <<= 1) {
2689 if (hostbits[bitpos] & bitat) {
2690 portp = brdp->ports[(channr - 1)];
2691 if (stli_hostcmd(brdp, portp)) {
2692 slavebitchange++;
2693 slavebits[bitpos] |= bitat;
2694 }
2695 }
2696 }
2697 }
2698
2699/*
2700 * If any of the ports are no longer busy then update them in the
2701 * slave request bits. We need to do this after, since a host port
2702 * service may initiate more slave requests.
2703 */
2704 if (slavebitchange) {
Alan Cox4ac43602006-06-28 04:26:52 -07002705 hdrp = (cdkhdr_t __iomem *) EBRDGETMEMPTR(brdp, CDK_CDKADDR);
2706 slavep = ((unsigned char __iomem *) hdrp) + brdp->slaveoffset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 for (bitpos = 0; (bitpos < bitsize); bitpos++) {
Alan Cox4ac43602006-06-28 04:26:52 -07002708 if (readb(slavebits + bitpos))
2709 writeb(readb(slavep + bitpos) & ~slavebits[bitpos], slavebits + bitpos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 }
2711 }
2712}
2713
2714/*****************************************************************************/
2715
2716/*
2717 * Driver poll routine. This routine polls the boards in use and passes
2718 * messages back up to host when necessary. This is actually very
2719 * CPU efficient, since we will always have the kernel poll clock, it
2720 * adds only a few cycles when idle (since board service can be
2721 * determined very easily), but when loaded generates no interrupts
2722 * (with their expensive associated context change).
2723 */
2724
2725static void stli_poll(unsigned long arg)
2726{
Alan Cox4ac43602006-06-28 04:26:52 -07002727 cdkhdr_t __iomem *hdrp;
2728 stlibrd_t *brdp;
2729 int brdnr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730
2731 stli_timerlist.expires = STLI_TIMEOUT;
2732 add_timer(&stli_timerlist);
2733
2734/*
2735 * Check each board and do any servicing required.
2736 */
2737 for (brdnr = 0; (brdnr < stli_nrbrds); brdnr++) {
2738 brdp = stli_brds[brdnr];
Alan Cox4ac43602006-06-28 04:26:52 -07002739 if (brdp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740 continue;
2741 if ((brdp->state & BST_STARTED) == 0)
2742 continue;
2743
Alan Cox4ac43602006-06-28 04:26:52 -07002744 spin_lock(&brd_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 EBRDENABLE(brdp);
Alan Cox4ac43602006-06-28 04:26:52 -07002746 hdrp = (cdkhdr_t __iomem *) EBRDGETMEMPTR(brdp, CDK_CDKADDR);
2747 if (readb(&hdrp->hostreq))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748 stli_brdpoll(brdp, hdrp);
2749 EBRDDISABLE(brdp);
Alan Cox4ac43602006-06-28 04:26:52 -07002750 spin_unlock(&brd_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751 }
2752}
2753
2754/*****************************************************************************/
2755
2756/*
2757 * Translate the termios settings into the port setting structure of
2758 * the slave.
2759 */
2760
2761static void stli_mkasyport(stliport_t *portp, asyport_t *pp, struct termios *tiosp)
2762{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763 memset(pp, 0, sizeof(asyport_t));
2764
2765/*
2766 * Start of by setting the baud, char size, parity and stop bit info.
2767 */
2768 pp->baudout = tiosp->c_cflag & CBAUD;
2769 if (pp->baudout & CBAUDEX) {
2770 pp->baudout &= ~CBAUDEX;
2771 if ((pp->baudout < 1) || (pp->baudout > 4))
2772 tiosp->c_cflag &= ~CBAUDEX;
2773 else
2774 pp->baudout += 15;
2775 }
2776 pp->baudout = stli_baudrates[pp->baudout];
2777 if ((tiosp->c_cflag & CBAUD) == B38400) {
2778 if ((portp->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
2779 pp->baudout = 57600;
2780 else if ((portp->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
2781 pp->baudout = 115200;
2782 else if ((portp->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
2783 pp->baudout = 230400;
2784 else if ((portp->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
2785 pp->baudout = 460800;
2786 else if ((portp->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST)
2787 pp->baudout = (portp->baud_base / portp->custom_divisor);
2788 }
2789 if (pp->baudout > STL_MAXBAUD)
2790 pp->baudout = STL_MAXBAUD;
2791 pp->baudin = pp->baudout;
2792
2793 switch (tiosp->c_cflag & CSIZE) {
2794 case CS5:
2795 pp->csize = 5;
2796 break;
2797 case CS6:
2798 pp->csize = 6;
2799 break;
2800 case CS7:
2801 pp->csize = 7;
2802 break;
2803 default:
2804 pp->csize = 8;
2805 break;
2806 }
2807
2808 if (tiosp->c_cflag & CSTOPB)
2809 pp->stopbs = PT_STOP2;
2810 else
2811 pp->stopbs = PT_STOP1;
2812
2813 if (tiosp->c_cflag & PARENB) {
2814 if (tiosp->c_cflag & PARODD)
2815 pp->parity = PT_ODDPARITY;
2816 else
2817 pp->parity = PT_EVENPARITY;
2818 } else {
2819 pp->parity = PT_NOPARITY;
2820 }
2821
2822/*
2823 * Set up any flow control options enabled.
2824 */
2825 if (tiosp->c_iflag & IXON) {
2826 pp->flow |= F_IXON;
2827 if (tiosp->c_iflag & IXANY)
2828 pp->flow |= F_IXANY;
2829 }
2830 if (tiosp->c_cflag & CRTSCTS)
2831 pp->flow |= (F_RTSFLOW | F_CTSFLOW);
2832
2833 pp->startin = tiosp->c_cc[VSTART];
2834 pp->stopin = tiosp->c_cc[VSTOP];
2835 pp->startout = tiosp->c_cc[VSTART];
2836 pp->stopout = tiosp->c_cc[VSTOP];
2837
2838/*
2839 * Set up the RX char marking mask with those RX error types we must
2840 * catch. We can get the slave to help us out a little here, it will
2841 * ignore parity errors and breaks for us, and mark parity errors in
2842 * the data stream.
2843 */
2844 if (tiosp->c_iflag & IGNPAR)
2845 pp->iflag |= FI_IGNRXERRS;
2846 if (tiosp->c_iflag & IGNBRK)
2847 pp->iflag |= FI_IGNBREAK;
2848
2849 portp->rxmarkmsk = 0;
2850 if (tiosp->c_iflag & (INPCK | PARMRK))
2851 pp->iflag |= FI_1MARKRXERRS;
2852 if (tiosp->c_iflag & BRKINT)
2853 portp->rxmarkmsk |= BRKINT;
2854
2855/*
2856 * Set up clocal processing as required.
2857 */
2858 if (tiosp->c_cflag & CLOCAL)
2859 portp->flags &= ~ASYNC_CHECK_CD;
2860 else
2861 portp->flags |= ASYNC_CHECK_CD;
2862
2863/*
2864 * Transfer any persistent flags into the asyport structure.
2865 */
2866 pp->pflag = (portp->pflag & 0xffff);
2867 pp->vmin = (portp->pflag & P_RXIMIN) ? 1 : 0;
2868 pp->vtime = (portp->pflag & P_RXITIME) ? 1 : 0;
2869 pp->cc[1] = (portp->pflag & P_RXTHOLD) ? 1 : 0;
2870}
2871
2872/*****************************************************************************/
2873
2874/*
2875 * Construct a slave signals structure for setting the DTR and RTS
2876 * signals as specified.
2877 */
2878
2879static void stli_mkasysigs(asysigs_t *sp, int dtr, int rts)
2880{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881 memset(sp, 0, sizeof(asysigs_t));
2882 if (dtr >= 0) {
2883 sp->signal |= SG_DTR;
2884 sp->sigvalue |= ((dtr > 0) ? SG_DTR : 0);
2885 }
2886 if (rts >= 0) {
2887 sp->signal |= SG_RTS;
2888 sp->sigvalue |= ((rts > 0) ? SG_RTS : 0);
2889 }
2890}
2891
2892/*****************************************************************************/
2893
2894/*
2895 * Convert the signals returned from the slave into a local TIOCM type
2896 * signals value. We keep them locally in TIOCM format.
2897 */
2898
2899static long stli_mktiocm(unsigned long sigvalue)
2900{
Alan Cox4ac43602006-06-28 04:26:52 -07002901 long tiocm = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902 tiocm |= ((sigvalue & SG_DCD) ? TIOCM_CD : 0);
2903 tiocm |= ((sigvalue & SG_CTS) ? TIOCM_CTS : 0);
2904 tiocm |= ((sigvalue & SG_RI) ? TIOCM_RI : 0);
2905 tiocm |= ((sigvalue & SG_DSR) ? TIOCM_DSR : 0);
2906 tiocm |= ((sigvalue & SG_DTR) ? TIOCM_DTR : 0);
2907 tiocm |= ((sigvalue & SG_RTS) ? TIOCM_RTS : 0);
2908 return(tiocm);
2909}
2910
2911/*****************************************************************************/
2912
2913/*
2914 * All panels and ports actually attached have been worked out. All
2915 * we need to do here is set up the appropriate per port data structures.
2916 */
2917
2918static int stli_initports(stlibrd_t *brdp)
2919{
2920 stliport_t *portp;
2921 int i, panelnr, panelport;
2922
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 for (i = 0, panelnr = 0, panelport = 0; (i < brdp->nrports); i++) {
Tobias Klauserb0b4ed72006-03-31 02:30:56 -08002924 portp = kzalloc(sizeof(stliport_t), GFP_KERNEL);
2925 if (!portp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926 printk("STALLION: failed to allocate port structure\n");
2927 continue;
2928 }
2929
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930 portp->magic = STLI_PORTMAGIC;
2931 portp->portnr = i;
2932 portp->brdnr = brdp->brdnr;
2933 portp->panelnr = panelnr;
2934 portp->baud_base = STL_BAUDBASE;
2935 portp->close_delay = STL_CLOSEDELAY;
2936 portp->closing_wait = 30 * HZ;
2937 INIT_WORK(&portp->tqhangup, stli_dohangup, portp);
2938 init_waitqueue_head(&portp->open_wait);
2939 init_waitqueue_head(&portp->close_wait);
2940 init_waitqueue_head(&portp->raw_wait);
2941 panelport++;
2942 if (panelport >= brdp->panels[panelnr]) {
2943 panelport = 0;
2944 panelnr++;
2945 }
2946 brdp->ports[i] = portp;
2947 }
2948
Alan Cox4ac43602006-06-28 04:26:52 -07002949 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950}
2951
2952/*****************************************************************************/
2953
2954/*
2955 * All the following routines are board specific hardware operations.
2956 */
2957
2958static void stli_ecpinit(stlibrd_t *brdp)
2959{
2960 unsigned long memconf;
2961
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962 outb(ECP_ATSTOP, (brdp->iobase + ECP_ATCONFR));
2963 udelay(10);
2964 outb(ECP_ATDISABLE, (brdp->iobase + ECP_ATCONFR));
2965 udelay(100);
2966
2967 memconf = (brdp->memaddr & ECP_ATADDRMASK) >> ECP_ATADDRSHFT;
2968 outb(memconf, (brdp->iobase + ECP_ATMEMAR));
2969}
2970
2971/*****************************************************************************/
2972
2973static void stli_ecpenable(stlibrd_t *brdp)
2974{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975 outb(ECP_ATENABLE, (brdp->iobase + ECP_ATCONFR));
2976}
2977
2978/*****************************************************************************/
2979
2980static void stli_ecpdisable(stlibrd_t *brdp)
2981{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982 outb(ECP_ATDISABLE, (brdp->iobase + ECP_ATCONFR));
2983}
2984
2985/*****************************************************************************/
2986
2987static char *stli_ecpgetmemptr(stlibrd_t *brdp, unsigned long offset, int line)
2988{
Alan Cox4ac43602006-06-28 04:26:52 -07002989 void *ptr;
2990 unsigned char val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991
2992 if (offset > brdp->memsize) {
2993 printk(KERN_ERR "STALLION: shared memory pointer=%x out of "
2994 "range at line=%d(%d), brd=%d\n",
2995 (int) offset, line, __LINE__, brdp->brdnr);
2996 ptr = NULL;
2997 val = 0;
2998 } else {
2999 ptr = brdp->membase + (offset % ECP_ATPAGESIZE);
3000 val = (unsigned char) (offset / ECP_ATPAGESIZE);
3001 }
3002 outb(val, (brdp->iobase + ECP_ATMEMPR));
3003 return(ptr);
3004}
3005
3006/*****************************************************************************/
3007
3008static void stli_ecpreset(stlibrd_t *brdp)
3009{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010 outb(ECP_ATSTOP, (brdp->iobase + ECP_ATCONFR));
3011 udelay(10);
3012 outb(ECP_ATDISABLE, (brdp->iobase + ECP_ATCONFR));
3013 udelay(500);
3014}
3015
3016/*****************************************************************************/
3017
3018static void stli_ecpintr(stlibrd_t *brdp)
3019{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 outb(0x1, brdp->iobase);
3021}
3022
3023/*****************************************************************************/
3024
3025/*
3026 * The following set of functions act on ECP EISA boards.
3027 */
3028
3029static void stli_ecpeiinit(stlibrd_t *brdp)
3030{
3031 unsigned long memconf;
3032
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 outb(0x1, (brdp->iobase + ECP_EIBRDENAB));
3034 outb(ECP_EISTOP, (brdp->iobase + ECP_EICONFR));
3035 udelay(10);
3036 outb(ECP_EIDISABLE, (brdp->iobase + ECP_EICONFR));
3037 udelay(500);
3038
3039 memconf = (brdp->memaddr & ECP_EIADDRMASKL) >> ECP_EIADDRSHFTL;
3040 outb(memconf, (brdp->iobase + ECP_EIMEMARL));
3041 memconf = (brdp->memaddr & ECP_EIADDRMASKH) >> ECP_EIADDRSHFTH;
3042 outb(memconf, (brdp->iobase + ECP_EIMEMARH));
3043}
3044
3045/*****************************************************************************/
3046
3047static void stli_ecpeienable(stlibrd_t *brdp)
3048{
3049 outb(ECP_EIENABLE, (brdp->iobase + ECP_EICONFR));
3050}
3051
3052/*****************************************************************************/
3053
3054static void stli_ecpeidisable(stlibrd_t *brdp)
3055{
3056 outb(ECP_EIDISABLE, (brdp->iobase + ECP_EICONFR));
3057}
3058
3059/*****************************************************************************/
3060
3061static char *stli_ecpeigetmemptr(stlibrd_t *brdp, unsigned long offset, int line)
3062{
3063 void *ptr;
3064 unsigned char val;
3065
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066 if (offset > brdp->memsize) {
3067 printk(KERN_ERR "STALLION: shared memory pointer=%x out of "
3068 "range at line=%d(%d), brd=%d\n",
3069 (int) offset, line, __LINE__, brdp->brdnr);
3070 ptr = NULL;
3071 val = 0;
3072 } else {
3073 ptr = brdp->membase + (offset % ECP_EIPAGESIZE);
3074 if (offset < ECP_EIPAGESIZE)
3075 val = ECP_EIENABLE;
3076 else
3077 val = ECP_EIENABLE | 0x40;
3078 }
3079 outb(val, (brdp->iobase + ECP_EICONFR));
3080 return(ptr);
3081}
3082
3083/*****************************************************************************/
3084
3085static void stli_ecpeireset(stlibrd_t *brdp)
3086{
3087 outb(ECP_EISTOP, (brdp->iobase + ECP_EICONFR));
3088 udelay(10);
3089 outb(ECP_EIDISABLE, (brdp->iobase + ECP_EICONFR));
3090 udelay(500);
3091}
3092
3093/*****************************************************************************/
3094
3095/*
3096 * The following set of functions act on ECP MCA boards.
3097 */
3098
3099static void stli_ecpmcenable(stlibrd_t *brdp)
3100{
3101 outb(ECP_MCENABLE, (brdp->iobase + ECP_MCCONFR));
3102}
3103
3104/*****************************************************************************/
3105
3106static void stli_ecpmcdisable(stlibrd_t *brdp)
3107{
3108 outb(ECP_MCDISABLE, (brdp->iobase + ECP_MCCONFR));
3109}
3110
3111/*****************************************************************************/
3112
3113static char *stli_ecpmcgetmemptr(stlibrd_t *brdp, unsigned long offset, int line)
3114{
Alan Cox4ac43602006-06-28 04:26:52 -07003115 void *ptr;
3116 unsigned char val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117
3118 if (offset > brdp->memsize) {
3119 printk(KERN_ERR "STALLION: shared memory pointer=%x out of "
3120 "range at line=%d(%d), brd=%d\n",
3121 (int) offset, line, __LINE__, brdp->brdnr);
3122 ptr = NULL;
3123 val = 0;
3124 } else {
3125 ptr = brdp->membase + (offset % ECP_MCPAGESIZE);
3126 val = ((unsigned char) (offset / ECP_MCPAGESIZE)) | ECP_MCENABLE;
3127 }
3128 outb(val, (brdp->iobase + ECP_MCCONFR));
3129 return(ptr);
3130}
3131
3132/*****************************************************************************/
3133
3134static void stli_ecpmcreset(stlibrd_t *brdp)
3135{
3136 outb(ECP_MCSTOP, (brdp->iobase + ECP_MCCONFR));
3137 udelay(10);
3138 outb(ECP_MCDISABLE, (brdp->iobase + ECP_MCCONFR));
3139 udelay(500);
3140}
3141
3142/*****************************************************************************/
3143
3144/*
3145 * The following set of functions act on ECP PCI boards.
3146 */
3147
3148static void stli_ecppciinit(stlibrd_t *brdp)
3149{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150 outb(ECP_PCISTOP, (brdp->iobase + ECP_PCICONFR));
3151 udelay(10);
3152 outb(0, (brdp->iobase + ECP_PCICONFR));
3153 udelay(500);
3154}
3155
3156/*****************************************************************************/
3157
3158static char *stli_ecppcigetmemptr(stlibrd_t *brdp, unsigned long offset, int line)
3159{
3160 void *ptr;
3161 unsigned char val;
3162
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163 if (offset > brdp->memsize) {
3164 printk(KERN_ERR "STALLION: shared memory pointer=%x out of "
3165 "range at line=%d(%d), board=%d\n",
3166 (int) offset, line, __LINE__, brdp->brdnr);
3167 ptr = NULL;
3168 val = 0;
3169 } else {
3170 ptr = brdp->membase + (offset % ECP_PCIPAGESIZE);
3171 val = (offset / ECP_PCIPAGESIZE) << 1;
3172 }
3173 outb(val, (brdp->iobase + ECP_PCICONFR));
3174 return(ptr);
3175}
3176
3177/*****************************************************************************/
3178
3179static void stli_ecppcireset(stlibrd_t *brdp)
3180{
3181 outb(ECP_PCISTOP, (brdp->iobase + ECP_PCICONFR));
3182 udelay(10);
3183 outb(0, (brdp->iobase + ECP_PCICONFR));
3184 udelay(500);
3185}
3186
3187/*****************************************************************************/
3188
3189/*
3190 * The following routines act on ONboards.
3191 */
3192
3193static void stli_onbinit(stlibrd_t *brdp)
3194{
3195 unsigned long memconf;
3196
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197 outb(ONB_ATSTOP, (brdp->iobase + ONB_ATCONFR));
3198 udelay(10);
3199 outb(ONB_ATDISABLE, (brdp->iobase + ONB_ATCONFR));
3200 mdelay(1000);
3201
3202 memconf = (brdp->memaddr & ONB_ATADDRMASK) >> ONB_ATADDRSHFT;
3203 outb(memconf, (brdp->iobase + ONB_ATMEMAR));
3204 outb(0x1, brdp->iobase);
3205 mdelay(1);
3206}
3207
3208/*****************************************************************************/
3209
3210static void stli_onbenable(stlibrd_t *brdp)
3211{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212 outb((brdp->enabval | ONB_ATENABLE), (brdp->iobase + ONB_ATCONFR));
3213}
3214
3215/*****************************************************************************/
3216
3217static void stli_onbdisable(stlibrd_t *brdp)
3218{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219 outb((brdp->enabval | ONB_ATDISABLE), (brdp->iobase + ONB_ATCONFR));
3220}
3221
3222/*****************************************************************************/
3223
3224static char *stli_onbgetmemptr(stlibrd_t *brdp, unsigned long offset, int line)
3225{
3226 void *ptr;
3227
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228 if (offset > brdp->memsize) {
3229 printk(KERN_ERR "STALLION: shared memory pointer=%x out of "
3230 "range at line=%d(%d), brd=%d\n",
3231 (int) offset, line, __LINE__, brdp->brdnr);
3232 ptr = NULL;
3233 } else {
3234 ptr = brdp->membase + (offset % ONB_ATPAGESIZE);
3235 }
3236 return(ptr);
3237}
3238
3239/*****************************************************************************/
3240
3241static void stli_onbreset(stlibrd_t *brdp)
3242{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243 outb(ONB_ATSTOP, (brdp->iobase + ONB_ATCONFR));
3244 udelay(10);
3245 outb(ONB_ATDISABLE, (brdp->iobase + ONB_ATCONFR));
3246 mdelay(1000);
3247}
3248
3249/*****************************************************************************/
3250
3251/*
3252 * The following routines act on ONboard EISA.
3253 */
3254
3255static void stli_onbeinit(stlibrd_t *brdp)
3256{
3257 unsigned long memconf;
3258
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259 outb(0x1, (brdp->iobase + ONB_EIBRDENAB));
3260 outb(ONB_EISTOP, (brdp->iobase + ONB_EICONFR));
3261 udelay(10);
3262 outb(ONB_EIDISABLE, (brdp->iobase + ONB_EICONFR));
3263 mdelay(1000);
3264
3265 memconf = (brdp->memaddr & ONB_EIADDRMASKL) >> ONB_EIADDRSHFTL;
3266 outb(memconf, (brdp->iobase + ONB_EIMEMARL));
3267 memconf = (brdp->memaddr & ONB_EIADDRMASKH) >> ONB_EIADDRSHFTH;
3268 outb(memconf, (brdp->iobase + ONB_EIMEMARH));
3269 outb(0x1, brdp->iobase);
3270 mdelay(1);
3271}
3272
3273/*****************************************************************************/
3274
3275static void stli_onbeenable(stlibrd_t *brdp)
3276{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277 outb(ONB_EIENABLE, (brdp->iobase + ONB_EICONFR));
3278}
3279
3280/*****************************************************************************/
3281
3282static void stli_onbedisable(stlibrd_t *brdp)
3283{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284 outb(ONB_EIDISABLE, (brdp->iobase + ONB_EICONFR));
3285}
3286
3287/*****************************************************************************/
3288
3289static char *stli_onbegetmemptr(stlibrd_t *brdp, unsigned long offset, int line)
3290{
Alan Cox4ac43602006-06-28 04:26:52 -07003291 void *ptr;
3292 unsigned char val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293
3294 if (offset > brdp->memsize) {
3295 printk(KERN_ERR "STALLION: shared memory pointer=%x out of "
3296 "range at line=%d(%d), brd=%d\n",
3297 (int) offset, line, __LINE__, brdp->brdnr);
3298 ptr = NULL;
3299 val = 0;
3300 } else {
3301 ptr = brdp->membase + (offset % ONB_EIPAGESIZE);
3302 if (offset < ONB_EIPAGESIZE)
3303 val = ONB_EIENABLE;
3304 else
3305 val = ONB_EIENABLE | 0x40;
3306 }
3307 outb(val, (brdp->iobase + ONB_EICONFR));
3308 return(ptr);
3309}
3310
3311/*****************************************************************************/
3312
3313static void stli_onbereset(stlibrd_t *brdp)
3314{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315 outb(ONB_EISTOP, (brdp->iobase + ONB_EICONFR));
3316 udelay(10);
3317 outb(ONB_EIDISABLE, (brdp->iobase + ONB_EICONFR));
3318 mdelay(1000);
3319}
3320
3321/*****************************************************************************/
3322
3323/*
3324 * The following routines act on Brumby boards.
3325 */
3326
3327static void stli_bbyinit(stlibrd_t *brdp)
3328{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329 outb(BBY_ATSTOP, (brdp->iobase + BBY_ATCONFR));
3330 udelay(10);
3331 outb(0, (brdp->iobase + BBY_ATCONFR));
3332 mdelay(1000);
3333 outb(0x1, brdp->iobase);
3334 mdelay(1);
3335}
3336
3337/*****************************************************************************/
3338
3339static char *stli_bbygetmemptr(stlibrd_t *brdp, unsigned long offset, int line)
3340{
Alan Cox4ac43602006-06-28 04:26:52 -07003341 void *ptr;
3342 unsigned char val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343
Alan Cox4ac43602006-06-28 04:26:52 -07003344 BUG_ON(offset > brdp->memsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345
Alan Cox4ac43602006-06-28 04:26:52 -07003346 ptr = brdp->membase + (offset % BBY_PAGESIZE);
3347 val = (unsigned char) (offset / BBY_PAGESIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348 outb(val, (brdp->iobase + BBY_ATCONFR));
3349 return(ptr);
3350}
3351
3352/*****************************************************************************/
3353
3354static void stli_bbyreset(stlibrd_t *brdp)
3355{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356 outb(BBY_ATSTOP, (brdp->iobase + BBY_ATCONFR));
3357 udelay(10);
3358 outb(0, (brdp->iobase + BBY_ATCONFR));
3359 mdelay(1000);
3360}
3361
3362/*****************************************************************************/
3363
3364/*
3365 * The following routines act on original old Stallion boards.
3366 */
3367
3368static void stli_stalinit(stlibrd_t *brdp)
3369{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370 outb(0x1, brdp->iobase);
3371 mdelay(1000);
3372}
3373
3374/*****************************************************************************/
3375
3376static char *stli_stalgetmemptr(stlibrd_t *brdp, unsigned long offset, int line)
3377{
Alan Cox4ac43602006-06-28 04:26:52 -07003378 BUG_ON(offset > brdp->memsize);
3379 return brdp->membase + (offset % STAL_PAGESIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380}
3381
3382/*****************************************************************************/
3383
3384static void stli_stalreset(stlibrd_t *brdp)
3385{
Alan Cox4ac43602006-06-28 04:26:52 -07003386 u32 __iomem *vecp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387
Alan Cox4ac43602006-06-28 04:26:52 -07003388 vecp = (u32 __iomem *) (brdp->membase + 0x30);
3389 writel(0xffff0000, vecp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390 outb(0, brdp->iobase);
3391 mdelay(1000);
3392}
3393
3394/*****************************************************************************/
3395
3396/*
3397 * Try to find an ECP board and initialize it. This handles only ECP
3398 * board types.
3399 */
3400
3401static int stli_initecp(stlibrd_t *brdp)
3402{
Alan Cox4ac43602006-06-28 04:26:52 -07003403 cdkecpsig_t sig;
3404 cdkecpsig_t __iomem *sigsp;
3405 unsigned int status, nxtid;
3406 char *name;
3407 int panelnr, nrports;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408
3409 if (!request_region(brdp->iobase, brdp->iosize, "istallion"))
3410 return -EIO;
3411
3412 if ((brdp->iobase == 0) || (brdp->memaddr == 0))
3413 {
3414 release_region(brdp->iobase, brdp->iosize);
Alan Cox4ac43602006-06-28 04:26:52 -07003415 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416 }
3417
3418 brdp->iosize = ECP_IOSIZE;
3419
3420/*
3421 * Based on the specific board type setup the common vars to access
3422 * and enable shared memory. Set all board specific information now
3423 * as well.
3424 */
3425 switch (brdp->brdtype) {
3426 case BRD_ECP:
3427 brdp->membase = (void *) brdp->memaddr;
3428 brdp->memsize = ECP_MEMSIZE;
3429 brdp->pagesize = ECP_ATPAGESIZE;
3430 brdp->init = stli_ecpinit;
3431 brdp->enable = stli_ecpenable;
3432 brdp->reenable = stli_ecpenable;
3433 brdp->disable = stli_ecpdisable;
3434 brdp->getmemptr = stli_ecpgetmemptr;
3435 brdp->intr = stli_ecpintr;
3436 brdp->reset = stli_ecpreset;
3437 name = "serial(EC8/64)";
3438 break;
3439
3440 case BRD_ECPE:
3441 brdp->membase = (void *) brdp->memaddr;
3442 brdp->memsize = ECP_MEMSIZE;
3443 brdp->pagesize = ECP_EIPAGESIZE;
3444 brdp->init = stli_ecpeiinit;
3445 brdp->enable = stli_ecpeienable;
3446 brdp->reenable = stli_ecpeienable;
3447 brdp->disable = stli_ecpeidisable;
3448 brdp->getmemptr = stli_ecpeigetmemptr;
3449 brdp->intr = stli_ecpintr;
3450 brdp->reset = stli_ecpeireset;
3451 name = "serial(EC8/64-EI)";
3452 break;
3453
3454 case BRD_ECPMC:
3455 brdp->membase = (void *) brdp->memaddr;
3456 brdp->memsize = ECP_MEMSIZE;
3457 brdp->pagesize = ECP_MCPAGESIZE;
3458 brdp->init = NULL;
3459 brdp->enable = stli_ecpmcenable;
3460 brdp->reenable = stli_ecpmcenable;
3461 brdp->disable = stli_ecpmcdisable;
3462 brdp->getmemptr = stli_ecpmcgetmemptr;
3463 brdp->intr = stli_ecpintr;
3464 brdp->reset = stli_ecpmcreset;
3465 name = "serial(EC8/64-MCA)";
3466 break;
3467
3468 case BRD_ECPPCI:
3469 brdp->membase = (void *) brdp->memaddr;
3470 brdp->memsize = ECP_PCIMEMSIZE;
3471 brdp->pagesize = ECP_PCIPAGESIZE;
3472 brdp->init = stli_ecppciinit;
3473 brdp->enable = NULL;
3474 brdp->reenable = NULL;
3475 brdp->disable = NULL;
3476 brdp->getmemptr = stli_ecppcigetmemptr;
3477 brdp->intr = stli_ecpintr;
3478 brdp->reset = stli_ecppcireset;
3479 name = "serial(EC/RA-PCI)";
3480 break;
3481
3482 default:
3483 release_region(brdp->iobase, brdp->iosize);
Alan Cox4ac43602006-06-28 04:26:52 -07003484 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485 }
3486
3487/*
3488 * The per-board operations structure is all set up, so now let's go
3489 * and get the board operational. Firstly initialize board configuration
3490 * registers. Set the memory mapping info so we can get at the boards
3491 * shared memory.
3492 */
3493 EBRDINIT(brdp);
3494
3495 brdp->membase = ioremap(brdp->memaddr, brdp->memsize);
Alan Cox4ac43602006-06-28 04:26:52 -07003496 if (brdp->membase == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497 {
3498 release_region(brdp->iobase, brdp->iosize);
Alan Cox4ac43602006-06-28 04:26:52 -07003499 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500 }
3501
3502/*
3503 * Now that all specific code is set up, enable the shared memory and
3504 * look for the a signature area that will tell us exactly what board
3505 * this is, and what it is connected to it.
3506 */
3507 EBRDENABLE(brdp);
Alan Cox4ac43602006-06-28 04:26:52 -07003508 sigsp = (cdkecpsig_t __iomem *) EBRDGETMEMPTR(brdp, CDK_SIGADDR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509 memcpy(&sig, sigsp, sizeof(cdkecpsig_t));
3510 EBRDDISABLE(brdp);
3511
Alan Cox4ac43602006-06-28 04:26:52 -07003512 if (sig.magic != cpu_to_le32(ECP_MAGIC))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513 {
3514 release_region(brdp->iobase, brdp->iosize);
Alan Cox4ac43602006-06-28 04:26:52 -07003515 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516 }
3517
3518/*
3519 * Scan through the signature looking at the panels connected to the
3520 * board. Calculate the total number of ports as we go.
3521 */
3522 for (panelnr = 0, nxtid = 0; (panelnr < STL_MAXPANELS); panelnr++) {
3523 status = sig.panelid[nxtid];
3524 if ((status & ECH_PNLIDMASK) != nxtid)
3525 break;
3526
3527 brdp->panelids[panelnr] = status;
3528 nrports = (status & ECH_PNL16PORT) ? 16 : 8;
3529 if ((nrports == 16) && ((status & ECH_PNLXPID) == 0))
3530 nxtid++;
3531 brdp->panels[panelnr] = nrports;
3532 brdp->nrports += nrports;
3533 nxtid++;
3534 brdp->nrpanels++;
3535 }
3536
3537
3538 brdp->state |= BST_FOUND;
Alan Cox4ac43602006-06-28 04:26:52 -07003539 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540}
3541
3542/*****************************************************************************/
3543
3544/*
3545 * Try to find an ONboard, Brumby or Stallion board and initialize it.
3546 * This handles only these board types.
3547 */
3548
3549static int stli_initonb(stlibrd_t *brdp)
3550{
Alan Cox4ac43602006-06-28 04:26:52 -07003551 cdkonbsig_t sig;
3552 cdkonbsig_t __iomem *sigsp;
3553 char *name;
3554 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555
3556/*
3557 * Do a basic sanity check on the IO and memory addresses.
3558 */
Alan Cox4ac43602006-06-28 04:26:52 -07003559 if (brdp->iobase == 0 || brdp->memaddr == 0)
3560 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561
3562 brdp->iosize = ONB_IOSIZE;
3563
3564 if (!request_region(brdp->iobase, brdp->iosize, "istallion"))
3565 return -EIO;
3566
3567/*
3568 * Based on the specific board type setup the common vars to access
3569 * and enable shared memory. Set all board specific information now
3570 * as well.
3571 */
3572 switch (brdp->brdtype) {
3573 case BRD_ONBOARD:
3574 case BRD_ONBOARD32:
3575 case BRD_ONBOARD2:
3576 case BRD_ONBOARD2_32:
3577 case BRD_ONBOARDRS:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578 brdp->memsize = ONB_MEMSIZE;
3579 brdp->pagesize = ONB_ATPAGESIZE;
3580 brdp->init = stli_onbinit;
3581 brdp->enable = stli_onbenable;
3582 brdp->reenable = stli_onbenable;
3583 brdp->disable = stli_onbdisable;
3584 brdp->getmemptr = stli_onbgetmemptr;
3585 brdp->intr = stli_ecpintr;
3586 brdp->reset = stli_onbreset;
3587 if (brdp->memaddr > 0x100000)
3588 brdp->enabval = ONB_MEMENABHI;
3589 else
3590 brdp->enabval = ONB_MEMENABLO;
3591 name = "serial(ONBoard)";
3592 break;
3593
3594 case BRD_ONBOARDE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595 brdp->memsize = ONB_EIMEMSIZE;
3596 brdp->pagesize = ONB_EIPAGESIZE;
3597 brdp->init = stli_onbeinit;
3598 brdp->enable = stli_onbeenable;
3599 brdp->reenable = stli_onbeenable;
3600 brdp->disable = stli_onbedisable;
3601 brdp->getmemptr = stli_onbegetmemptr;
3602 brdp->intr = stli_ecpintr;
3603 brdp->reset = stli_onbereset;
3604 name = "serial(ONBoard/E)";
3605 break;
3606
3607 case BRD_BRUMBY4:
3608 case BRD_BRUMBY8:
3609 case BRD_BRUMBY16:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610 brdp->memsize = BBY_MEMSIZE;
3611 brdp->pagesize = BBY_PAGESIZE;
3612 brdp->init = stli_bbyinit;
3613 brdp->enable = NULL;
3614 brdp->reenable = NULL;
3615 brdp->disable = NULL;
3616 brdp->getmemptr = stli_bbygetmemptr;
3617 brdp->intr = stli_ecpintr;
3618 brdp->reset = stli_bbyreset;
3619 name = "serial(Brumby)";
3620 break;
3621
3622 case BRD_STALLION:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623 brdp->memsize = STAL_MEMSIZE;
3624 brdp->pagesize = STAL_PAGESIZE;
3625 brdp->init = stli_stalinit;
3626 brdp->enable = NULL;
3627 brdp->reenable = NULL;
3628 brdp->disable = NULL;
3629 brdp->getmemptr = stli_stalgetmemptr;
3630 brdp->intr = stli_ecpintr;
3631 brdp->reset = stli_stalreset;
3632 name = "serial(Stallion)";
3633 break;
3634
3635 default:
3636 release_region(brdp->iobase, brdp->iosize);
Alan Cox4ac43602006-06-28 04:26:52 -07003637 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003638 }
3639
3640/*
3641 * The per-board operations structure is all set up, so now let's go
3642 * and get the board operational. Firstly initialize board configuration
3643 * registers. Set the memory mapping info so we can get at the boards
3644 * shared memory.
3645 */
3646 EBRDINIT(brdp);
3647
3648 brdp->membase = ioremap(brdp->memaddr, brdp->memsize);
Alan Cox4ac43602006-06-28 04:26:52 -07003649 if (brdp->membase == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650 {
3651 release_region(brdp->iobase, brdp->iosize);
Alan Cox4ac43602006-06-28 04:26:52 -07003652 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003653 }
3654
3655/*
3656 * Now that all specific code is set up, enable the shared memory and
3657 * look for the a signature area that will tell us exactly what board
3658 * this is, and how many ports.
3659 */
3660 EBRDENABLE(brdp);
Alan Cox4ac43602006-06-28 04:26:52 -07003661 sigsp = (cdkonbsig_t __iomem *) EBRDGETMEMPTR(brdp, CDK_SIGADDR);
3662 memcpy_fromio(&sig, sigsp, sizeof(cdkonbsig_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663 EBRDDISABLE(brdp);
3664
Alan Cox4ac43602006-06-28 04:26:52 -07003665 if (sig.magic0 != cpu_to_le16(ONB_MAGIC0) ||
3666 sig.magic1 != cpu_to_le16(ONB_MAGIC1) ||
3667 sig.magic2 != cpu_to_le16(ONB_MAGIC2) ||
3668 sig.magic3 != cpu_to_le16(ONB_MAGIC3))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669 {
3670 release_region(brdp->iobase, brdp->iosize);
Alan Cox4ac43602006-06-28 04:26:52 -07003671 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672 }
3673
3674/*
3675 * Scan through the signature alive mask and calculate how many ports
3676 * there are on this board.
3677 */
3678 brdp->nrpanels = 1;
3679 if (sig.amask1) {
3680 brdp->nrports = 32;
3681 } else {
3682 for (i = 0; (i < 16); i++) {
3683 if (((sig.amask0 << i) & 0x8000) == 0)
3684 break;
3685 }
3686 brdp->nrports = i;
3687 }
3688 brdp->panels[0] = brdp->nrports;
3689
3690
3691 brdp->state |= BST_FOUND;
Alan Cox4ac43602006-06-28 04:26:52 -07003692 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003693}
3694
3695/*****************************************************************************/
3696
3697/*
3698 * Start up a running board. This routine is only called after the
3699 * code has been down loaded to the board and is operational. It will
3700 * read in the memory map, and get the show on the road...
3701 */
3702
3703static int stli_startbrd(stlibrd_t *brdp)
3704{
Alan Cox4ac43602006-06-28 04:26:52 -07003705 cdkhdr_t __iomem *hdrp;
3706 cdkmem_t __iomem *memp;
3707 cdkasy_t __iomem *ap;
3708 unsigned long flags;
3709 stliport_t *portp;
3710 int portnr, nrdevs, i, rc = 0;
3711 u32 memoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003712
Alan Cox4ac43602006-06-28 04:26:52 -07003713 spin_lock_irqsave(&brd_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003714 EBRDENABLE(brdp);
Alan Cox4ac43602006-06-28 04:26:52 -07003715 hdrp = (cdkhdr_t __iomem *) EBRDGETMEMPTR(brdp, CDK_CDKADDR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716 nrdevs = hdrp->nrdevs;
3717
3718#if 0
3719 printk("%s(%d): CDK version %d.%d.%d --> "
3720 "nrdevs=%d memp=%x hostp=%x slavep=%x\n",
Alan Cox4ac43602006-06-28 04:26:52 -07003721 __FILE__, __LINE__, readb(&hdrp->ver_release), readb(&hdrp->ver_modification),
3722 readb(&hdrp->ver_fix), nrdevs, (int) readl(&hdrp->memp), readl(&hdrp->hostp),
3723 readl(&hdrp->slavep));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724#endif
3725
3726 if (nrdevs < (brdp->nrports + 1)) {
3727 printk(KERN_ERR "STALLION: slave failed to allocate memory for "
3728 "all devices, devices=%d\n", nrdevs);
3729 brdp->nrports = nrdevs - 1;
3730 }
3731 brdp->nrdevs = nrdevs;
3732 brdp->hostoffset = hdrp->hostp - CDK_CDKADDR;
3733 brdp->slaveoffset = hdrp->slavep - CDK_CDKADDR;
3734 brdp->bitsize = (nrdevs + 7) / 8;
Alan Cox4ac43602006-06-28 04:26:52 -07003735 memoff = readl(&hdrp->memp);
3736 if (memoff > brdp->memsize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003737 printk(KERN_ERR "STALLION: corrupted shared memory region?\n");
3738 rc = -EIO;
3739 goto stli_donestartup;
3740 }
Alan Cox4ac43602006-06-28 04:26:52 -07003741 memp = (cdkmem_t __iomem *) EBRDGETMEMPTR(brdp, memoff);
3742 if (readw(&memp->dtype) != TYP_ASYNCTRL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743 printk(KERN_ERR "STALLION: no slave control device found\n");
3744 goto stli_donestartup;
3745 }
3746 memp++;
3747
3748/*
3749 * Cycle through memory allocation of each port. We are guaranteed to
3750 * have all ports inside the first page of slave window, so no need to
3751 * change pages while reading memory map.
3752 */
3753 for (i = 1, portnr = 0; (i < nrdevs); i++, portnr++, memp++) {
Alan Cox4ac43602006-06-28 04:26:52 -07003754 if (readw(&memp->dtype) != TYP_ASYNC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003755 break;
3756 portp = brdp->ports[portnr];
Alan Cox4ac43602006-06-28 04:26:52 -07003757 if (portp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003758 break;
3759 portp->devnr = i;
Alan Cox4ac43602006-06-28 04:26:52 -07003760 portp->addr = readl(&memp->offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761 portp->reqbit = (unsigned char) (0x1 << (i * 8 / nrdevs));
3762 portp->portidx = (unsigned char) (i / 8);
3763 portp->portbit = (unsigned char) (0x1 << (i % 8));
3764 }
3765
Alan Cox4ac43602006-06-28 04:26:52 -07003766 writeb(0xff, &hdrp->slavereq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003767
3768/*
3769 * For each port setup a local copy of the RX and TX buffer offsets
3770 * and sizes. We do this separate from the above, because we need to
3771 * move the shared memory page...
3772 */
3773 for (i = 1, portnr = 0; (i < nrdevs); i++, portnr++) {
3774 portp = brdp->ports[portnr];
Alan Cox4ac43602006-06-28 04:26:52 -07003775 if (portp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776 break;
3777 if (portp->addr == 0)
3778 break;
Alan Cox4ac43602006-06-28 04:26:52 -07003779 ap = (cdkasy_t __iomem *) EBRDGETMEMPTR(brdp, portp->addr);
3780 if (ap != NULL) {
3781 portp->rxsize = readw(&ap->rxq.size);
3782 portp->txsize = readw(&ap->txq.size);
3783 portp->rxoffset = readl(&ap->rxq.offset);
3784 portp->txoffset = readl(&ap->txq.offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003785 }
3786 }
3787
3788stli_donestartup:
3789 EBRDDISABLE(brdp);
Alan Cox4ac43602006-06-28 04:26:52 -07003790 spin_unlock_irqrestore(&brd_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003791
3792 if (rc == 0)
3793 brdp->state |= BST_STARTED;
3794
3795 if (! stli_timeron) {
3796 stli_timeron++;
3797 stli_timerlist.expires = STLI_TIMEOUT;
3798 add_timer(&stli_timerlist);
3799 }
3800
Alan Cox4ac43602006-06-28 04:26:52 -07003801 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802}
3803
3804/*****************************************************************************/
3805
3806/*
3807 * Probe and initialize the specified board.
3808 */
3809
3810static int __init stli_brdinit(stlibrd_t *brdp)
3811{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003812 stli_brds[brdp->brdnr] = brdp;
3813
3814 switch (brdp->brdtype) {
3815 case BRD_ECP:
3816 case BRD_ECPE:
3817 case BRD_ECPMC:
3818 case BRD_ECPPCI:
3819 stli_initecp(brdp);
3820 break;
3821 case BRD_ONBOARD:
3822 case BRD_ONBOARDE:
3823 case BRD_ONBOARD2:
3824 case BRD_ONBOARD32:
3825 case BRD_ONBOARD2_32:
3826 case BRD_ONBOARDRS:
3827 case BRD_BRUMBY4:
3828 case BRD_BRUMBY8:
3829 case BRD_BRUMBY16:
3830 case BRD_STALLION:
3831 stli_initonb(brdp);
3832 break;
3833 case BRD_EASYIO:
3834 case BRD_ECH:
3835 case BRD_ECHMC:
3836 case BRD_ECHPCI:
3837 printk(KERN_ERR "STALLION: %s board type not supported in "
3838 "this driver\n", stli_brdnames[brdp->brdtype]);
Alan Cox4ac43602006-06-28 04:26:52 -07003839 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003840 default:
3841 printk(KERN_ERR "STALLION: board=%d is unknown board "
3842 "type=%d\n", brdp->brdnr, brdp->brdtype);
Alan Cox4ac43602006-06-28 04:26:52 -07003843 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003844 }
3845
3846 if ((brdp->state & BST_FOUND) == 0) {
3847 printk(KERN_ERR "STALLION: %s board not found, board=%d "
3848 "io=%x mem=%x\n",
3849 stli_brdnames[brdp->brdtype], brdp->brdnr,
3850 brdp->iobase, (int) brdp->memaddr);
Alan Cox4ac43602006-06-28 04:26:52 -07003851 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003852 }
3853
3854 stli_initports(brdp);
3855 printk(KERN_INFO "STALLION: %s found, board=%d io=%x mem=%x "
3856 "nrpanels=%d nrports=%d\n", stli_brdnames[brdp->brdtype],
3857 brdp->brdnr, brdp->iobase, (int) brdp->memaddr,
3858 brdp->nrpanels, brdp->nrports);
Alan Cox4ac43602006-06-28 04:26:52 -07003859 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003860}
3861
3862/*****************************************************************************/
3863
3864/*
3865 * Probe around trying to find where the EISA boards shared memory
3866 * might be. This is a bit if hack, but it is the best we can do.
3867 */
3868
3869static int stli_eisamemprobe(stlibrd_t *brdp)
3870{
Alan Cox4ac43602006-06-28 04:26:52 -07003871 cdkecpsig_t ecpsig, __iomem *ecpsigp;
3872 cdkonbsig_t onbsig, __iomem *onbsigp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003873 int i, foundit;
3874
Linus Torvalds1da177e2005-04-16 15:20:36 -07003875/*
3876 * First up we reset the board, to get it into a known state. There
3877 * is only 2 board types here we need to worry about. Don;t use the
3878 * standard board init routine here, it programs up the shared
3879 * memory address, and we don't know it yet...
3880 */
3881 if (brdp->brdtype == BRD_ECPE) {
3882 outb(0x1, (brdp->iobase + ECP_EIBRDENAB));
3883 outb(ECP_EISTOP, (brdp->iobase + ECP_EICONFR));
3884 udelay(10);
3885 outb(ECP_EIDISABLE, (brdp->iobase + ECP_EICONFR));
3886 udelay(500);
3887 stli_ecpeienable(brdp);
3888 } else if (brdp->brdtype == BRD_ONBOARDE) {
3889 outb(0x1, (brdp->iobase + ONB_EIBRDENAB));
3890 outb(ONB_EISTOP, (brdp->iobase + ONB_EICONFR));
3891 udelay(10);
3892 outb(ONB_EIDISABLE, (brdp->iobase + ONB_EICONFR));
3893 mdelay(100);
3894 outb(0x1, brdp->iobase);
3895 mdelay(1);
3896 stli_onbeenable(brdp);
3897 } else {
Alan Cox4ac43602006-06-28 04:26:52 -07003898 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003899 }
3900
3901 foundit = 0;
3902 brdp->memsize = ECP_MEMSIZE;
3903
3904/*
3905 * Board shared memory is enabled, so now we have a poke around and
3906 * see if we can find it.
3907 */
3908 for (i = 0; (i < stli_eisamempsize); i++) {
3909 brdp->memaddr = stli_eisamemprobeaddrs[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003910 brdp->membase = ioremap(brdp->memaddr, brdp->memsize);
Alan Cox4ac43602006-06-28 04:26:52 -07003911 if (brdp->membase == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003912 continue;
3913
3914 if (brdp->brdtype == BRD_ECPE) {
Alan Cox4ac43602006-06-28 04:26:52 -07003915 ecpsigp = (cdkecpsig_t __iomem *) stli_ecpeigetmemptr(brdp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003916 CDK_SIGADDR, __LINE__);
Alan Cox4ac43602006-06-28 04:26:52 -07003917 memcpy_fromio(&ecpsig, ecpsigp, sizeof(cdkecpsig_t));
3918 if (ecpsig.magic == cpu_to_le32(ECP_MAGIC))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919 foundit = 1;
3920 } else {
Alan Cox4ac43602006-06-28 04:26:52 -07003921 onbsigp = (cdkonbsig_t __iomem *) stli_onbegetmemptr(brdp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003922 CDK_SIGADDR, __LINE__);
Alan Cox4ac43602006-06-28 04:26:52 -07003923 memcpy_fromio(&onbsig, onbsigp, sizeof(cdkonbsig_t));
3924 if ((onbsig.magic0 == cpu_to_le16(ONB_MAGIC0)) &&
3925 (onbsig.magic1 == cpu_to_le16(ONB_MAGIC1)) &&
3926 (onbsig.magic2 == cpu_to_le16(ONB_MAGIC2)) &&
3927 (onbsig.magic3 == cpu_to_le16(ONB_MAGIC3)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003928 foundit = 1;
3929 }
3930
3931 iounmap(brdp->membase);
3932 if (foundit)
3933 break;
3934 }
3935
3936/*
3937 * Regardless of whether we found the shared memory or not we must
3938 * disable the region. After that return success or failure.
3939 */
3940 if (brdp->brdtype == BRD_ECPE)
3941 stli_ecpeidisable(brdp);
3942 else
3943 stli_onbedisable(brdp);
3944
3945 if (! foundit) {
3946 brdp->memaddr = 0;
3947 brdp->membase = NULL;
3948 printk(KERN_ERR "STALLION: failed to probe shared memory "
3949 "region for %s in EISA slot=%d\n",
3950 stli_brdnames[brdp->brdtype], (brdp->iobase >> 12));
Alan Cox4ac43602006-06-28 04:26:52 -07003951 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003952 }
Alan Cox4ac43602006-06-28 04:26:52 -07003953 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003954}
3955
3956static int stli_getbrdnr(void)
3957{
3958 int i;
3959
3960 for (i = 0; i < STL_MAXBRDS; i++) {
3961 if (!stli_brds[i]) {
3962 if (i >= stli_nrbrds)
3963 stli_nrbrds = i + 1;
3964 return i;
3965 }
3966 }
3967 return -1;
3968}
3969
3970/*****************************************************************************/
3971
3972/*
3973 * Probe around and try to find any EISA boards in system. The biggest
3974 * problem here is finding out what memory address is associated with
3975 * an EISA board after it is found. The registers of the ECPE and
3976 * ONboardE are not readable - so we can't read them from there. We
3977 * don't have access to the EISA CMOS (or EISA BIOS) so we don't
3978 * actually have any way to find out the real value. The best we can
3979 * do is go probing around in the usual places hoping we can find it.
3980 */
3981
3982static int stli_findeisabrds(void)
3983{
Alan Cox4ac43602006-06-28 04:26:52 -07003984 stlibrd_t *brdp;
3985 unsigned int iobase, eid;
3986 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003987
3988/*
Alan Cox4ac43602006-06-28 04:26:52 -07003989 * Firstly check if this is an EISA system. If this is not an EISA system then
Linus Torvalds1da177e2005-04-16 15:20:36 -07003990 * don't bother going any further!
3991 */
Alan Cox4ac43602006-06-28 04:26:52 -07003992 if (EISA_bus)
3993 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003994
3995/*
3996 * Looks like an EISA system, so go searching for EISA boards.
3997 */
3998 for (iobase = 0x1000; (iobase <= 0xc000); iobase += 0x1000) {
3999 outb(0xff, (iobase + 0xc80));
4000 eid = inb(iobase + 0xc80);
4001 eid |= inb(iobase + 0xc81) << 8;
4002 if (eid != STL_EISAID)
4003 continue;
4004
4005/*
4006 * We have found a board. Need to check if this board was
4007 * statically configured already (just in case!).
4008 */
4009 for (i = 0; (i < STL_MAXBRDS); i++) {
4010 brdp = stli_brds[i];
Alan Cox4ac43602006-06-28 04:26:52 -07004011 if (brdp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004012 continue;
4013 if (brdp->iobase == iobase)
4014 break;
4015 }
4016 if (i < STL_MAXBRDS)
4017 continue;
4018
4019/*
4020 * We have found a Stallion board and it is not configured already.
4021 * Allocate a board structure and initialize it.
4022 */
Alan Cox4ac43602006-06-28 04:26:52 -07004023 if ((brdp = stli_allocbrd()) == NULL)
4024 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004025 if ((brdp->brdnr = stli_getbrdnr()) < 0)
Alan Cox4ac43602006-06-28 04:26:52 -07004026 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027 eid = inb(iobase + 0xc82);
4028 if (eid == ECP_EISAID)
4029 brdp->brdtype = BRD_ECPE;
4030 else if (eid == ONB_EISAID)
4031 brdp->brdtype = BRD_ONBOARDE;
4032 else
4033 brdp->brdtype = BRD_UNKNOWN;
4034 brdp->iobase = iobase;
4035 outb(0x1, (iobase + 0xc84));
4036 if (stli_eisamemprobe(brdp))
4037 outb(0, (iobase + 0xc84));
4038 stli_brdinit(brdp);
4039 }
4040
Alan Cox4ac43602006-06-28 04:26:52 -07004041 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004042}
4043
4044/*****************************************************************************/
4045
4046/*
4047 * Find the next available board number that is free.
4048 */
4049
4050/*****************************************************************************/
4051
4052#ifdef CONFIG_PCI
4053
4054/*
4055 * We have a Stallion board. Allocate a board structure and
4056 * initialize it. Read its IO and MEMORY resources from PCI
4057 * configuration space.
4058 */
4059
4060static int stli_initpcibrd(int brdtype, struct pci_dev *devp)
4061{
Alan Cox4ac43602006-06-28 04:26:52 -07004062 stlibrd_t *brdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004063
4064 if (pci_enable_device(devp))
Alan Cox4ac43602006-06-28 04:26:52 -07004065 return -EIO;
4066 if ((brdp = stli_allocbrd()) == NULL)
4067 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004068 if ((brdp->brdnr = stli_getbrdnr()) < 0) {
4069 printk(KERN_INFO "STALLION: too many boards found, "
4070 "maximum supported %d\n", STL_MAXBRDS);
Alan Cox4ac43602006-06-28 04:26:52 -07004071 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004072 }
4073 brdp->brdtype = brdtype;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004074/*
4075 * We have all resources from the board, so lets setup the actual
4076 * board structure now.
4077 */
4078 brdp->iobase = pci_resource_start(devp, 3);
4079 brdp->memaddr = pci_resource_start(devp, 2);
4080 stli_brdinit(brdp);
4081
Alan Cox4ac43602006-06-28 04:26:52 -07004082 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004083}
4084
4085/*****************************************************************************/
4086
4087/*
4088 * Find all Stallion PCI boards that might be installed. Initialize each
4089 * one as it is found.
4090 */
4091
4092static int stli_findpcibrds(void)
4093{
Alan Cox4ac43602006-06-28 04:26:52 -07004094 struct pci_dev *dev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004095
Alan Cox4ac43602006-06-28 04:26:52 -07004096 while ((dev = pci_get_device(PCI_VENDOR_ID_STALLION, PCI_DEVICE_ID_ECRA, dev))) {
4097 stli_initpcibrd(BRD_ECPPCI, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004098 }
Alan Cox4ac43602006-06-28 04:26:52 -07004099 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004100}
4101
4102#endif
4103
4104/*****************************************************************************/
4105
4106/*
4107 * Allocate a new board structure. Fill out the basic info in it.
4108 */
4109
4110static stlibrd_t *stli_allocbrd(void)
4111{
Alan Cox4ac43602006-06-28 04:26:52 -07004112 stlibrd_t *brdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004113
Tobias Klauserb0b4ed72006-03-31 02:30:56 -08004114 brdp = kzalloc(sizeof(stlibrd_t), GFP_KERNEL);
4115 if (!brdp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004116 printk(KERN_ERR "STALLION: failed to allocate memory "
Alan Cox4ac43602006-06-28 04:26:52 -07004117 "(size=%Zd)\n", sizeof(stlibrd_t));
Tobias Klauserb0b4ed72006-03-31 02:30:56 -08004118 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004119 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004120 brdp->magic = STLI_BOARDMAGIC;
Alan Cox4ac43602006-06-28 04:26:52 -07004121 return brdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004122}
4123
4124/*****************************************************************************/
4125
4126/*
4127 * Scan through all the boards in the configuration and see what we
4128 * can find.
4129 */
4130
4131static int stli_initbrds(void)
4132{
Alan Cox4ac43602006-06-28 04:26:52 -07004133 stlibrd_t *brdp, *nxtbrdp;
4134 stlconf_t *confp;
4135 int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004136
4137 if (stli_nrbrds > STL_MAXBRDS) {
4138 printk(KERN_INFO "STALLION: too many boards in configuration "
4139 "table, truncating to %d\n", STL_MAXBRDS);
4140 stli_nrbrds = STL_MAXBRDS;
4141 }
4142
4143/*
4144 * Firstly scan the list of static boards configured. Allocate
4145 * resources and initialize the boards as found. If this is a
4146 * module then let the module args override static configuration.
4147 */
4148 for (i = 0; (i < stli_nrbrds); i++) {
4149 confp = &stli_brdconf[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004150 stli_parsebrd(confp, stli_brdsp[i]);
Alan Cox4ac43602006-06-28 04:26:52 -07004151 if ((brdp = stli_allocbrd()) == NULL)
4152 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153 brdp->brdnr = i;
4154 brdp->brdtype = confp->brdtype;
4155 brdp->iobase = confp->ioaddr1;
4156 brdp->memaddr = confp->memaddr;
4157 stli_brdinit(brdp);
4158 }
4159
4160/*
4161 * Static configuration table done, so now use dynamic methods to
4162 * see if any more boards should be configured.
4163 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164 stli_argbrds();
Adrian Bunkdbc6b5f2005-06-25 14:59:03 -07004165 if (STLI_EISAPROBE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004166 stli_findeisabrds();
4167#ifdef CONFIG_PCI
4168 stli_findpcibrds();
4169#endif
4170
4171/*
4172 * All found boards are initialized. Now for a little optimization, if
4173 * no boards are sharing the "shared memory" regions then we can just
4174 * leave them all enabled. This is in fact the usual case.
4175 */
4176 stli_shared = 0;
4177 if (stli_nrbrds > 1) {
4178 for (i = 0; (i < stli_nrbrds); i++) {
4179 brdp = stli_brds[i];
Alan Cox4ac43602006-06-28 04:26:52 -07004180 if (brdp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004181 continue;
4182 for (j = i + 1; (j < stli_nrbrds); j++) {
4183 nxtbrdp = stli_brds[j];
Alan Cox4ac43602006-06-28 04:26:52 -07004184 if (nxtbrdp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004185 continue;
4186 if ((brdp->membase >= nxtbrdp->membase) &&
4187 (brdp->membase <= (nxtbrdp->membase +
4188 nxtbrdp->memsize - 1))) {
4189 stli_shared++;
4190 break;
4191 }
4192 }
4193 }
4194 }
4195
4196 if (stli_shared == 0) {
4197 for (i = 0; (i < stli_nrbrds); i++) {
4198 brdp = stli_brds[i];
Alan Cox4ac43602006-06-28 04:26:52 -07004199 if (brdp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004200 continue;
4201 if (brdp->state & BST_FOUND) {
4202 EBRDENABLE(brdp);
4203 brdp->enable = NULL;
4204 brdp->disable = NULL;
4205 }
4206 }
4207 }
4208
Alan Cox4ac43602006-06-28 04:26:52 -07004209 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004210}
4211
4212/*****************************************************************************/
4213
4214/*
4215 * Code to handle an "staliomem" read operation. This device is the
4216 * contents of the board shared memory. It is used for down loading
4217 * the slave image (and debugging :-)
4218 */
4219
4220static ssize_t stli_memread(struct file *fp, char __user *buf, size_t count, loff_t *offp)
4221{
Alan Cox4ac43602006-06-28 04:26:52 -07004222 unsigned long flags;
4223 void *memptr;
4224 stlibrd_t *brdp;
4225 int brdnr, size, n;
4226 void *p;
4227 loff_t off = *offp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004228
4229 brdnr = iminor(fp->f_dentry->d_inode);
4230 if (brdnr >= stli_nrbrds)
Alan Cox4ac43602006-06-28 04:26:52 -07004231 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232 brdp = stli_brds[brdnr];
Alan Cox4ac43602006-06-28 04:26:52 -07004233 if (brdp == NULL)
4234 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004235 if (brdp->state == 0)
Alan Cox4ac43602006-06-28 04:26:52 -07004236 return -ENODEV;
4237 if (off >= brdp->memsize || off + count < off)
4238 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004239
Alan Cox4ac43602006-06-28 04:26:52 -07004240 size = MIN(count, (brdp->memsize - off));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241
Alan Cox4ac43602006-06-28 04:26:52 -07004242 /*
4243 * Copy the data a page at a time
4244 */
4245
4246 p = (void *)__get_free_page(GFP_KERNEL);
4247 if(p == NULL)
4248 return -ENOMEM;
4249
Linus Torvalds1da177e2005-04-16 15:20:36 -07004250 while (size > 0) {
Alan Cox4ac43602006-06-28 04:26:52 -07004251 spin_lock_irqsave(&brd_lock, flags);
4252 EBRDENABLE(brdp);
4253 memptr = (void *) EBRDGETMEMPTR(brdp, off);
4254 n = MIN(size, (brdp->pagesize - (((unsigned long) off) % brdp->pagesize)));
4255 n = MIN(n, PAGE_SIZE);
4256 memcpy_fromio(p, memptr, n);
4257 EBRDDISABLE(brdp);
4258 spin_unlock_irqrestore(&brd_lock, flags);
4259 if (copy_to_user(buf, p, n)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004260 count = -EFAULT;
4261 goto out;
4262 }
Alan Cox4ac43602006-06-28 04:26:52 -07004263 off += n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004264 buf += n;
4265 size -= n;
4266 }
4267out:
Alan Cox4ac43602006-06-28 04:26:52 -07004268 *offp = off;
4269 free_page((unsigned long)p);
4270 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271}
4272
4273/*****************************************************************************/
4274
4275/*
4276 * Code to handle an "staliomem" write operation. This device is the
4277 * contents of the board shared memory. It is used for down loading
4278 * the slave image (and debugging :-)
Alan Cox4ac43602006-06-28 04:26:52 -07004279 *
4280 * FIXME: copy under lock
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281 */
4282
4283static ssize_t stli_memwrite(struct file *fp, const char __user *buf, size_t count, loff_t *offp)
4284{
Alan Cox4ac43602006-06-28 04:26:52 -07004285 unsigned long flags;
4286 void *memptr;
4287 stlibrd_t *brdp;
4288 char __user *chbuf;
4289 int brdnr, size, n;
4290 void *p;
4291 loff_t off = *offp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292
4293 brdnr = iminor(fp->f_dentry->d_inode);
Alan Cox4ac43602006-06-28 04:26:52 -07004294
Linus Torvalds1da177e2005-04-16 15:20:36 -07004295 if (brdnr >= stli_nrbrds)
Alan Cox4ac43602006-06-28 04:26:52 -07004296 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297 brdp = stli_brds[brdnr];
Alan Cox4ac43602006-06-28 04:26:52 -07004298 if (brdp == NULL)
4299 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300 if (brdp->state == 0)
Alan Cox4ac43602006-06-28 04:26:52 -07004301 return -ENODEV;
4302 if (off >= brdp->memsize || off + count < off)
4303 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004304
4305 chbuf = (char __user *) buf;
Alan Cox4ac43602006-06-28 04:26:52 -07004306 size = MIN(count, (brdp->memsize - off));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307
Alan Cox4ac43602006-06-28 04:26:52 -07004308 /*
4309 * Copy the data a page at a time
4310 */
4311
4312 p = (void *)__get_free_page(GFP_KERNEL);
4313 if(p == NULL)
4314 return -ENOMEM;
4315
Linus Torvalds1da177e2005-04-16 15:20:36 -07004316 while (size > 0) {
Alan Cox4ac43602006-06-28 04:26:52 -07004317 n = MIN(size, (brdp->pagesize - (((unsigned long) off) % brdp->pagesize)));
4318 n = MIN(n, PAGE_SIZE);
4319 if (copy_from_user(p, chbuf, n)) {
4320 if (count == 0)
4321 count = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004322 goto out;
4323 }
Alan Cox4ac43602006-06-28 04:26:52 -07004324 spin_lock_irqsave(&brd_lock, flags);
4325 EBRDENABLE(brdp);
4326 memptr = (void *) EBRDGETMEMPTR(brdp, off);
4327 memcpy_toio(memptr, p, n);
4328 EBRDDISABLE(brdp);
4329 spin_unlock_irqrestore(&brd_lock, flags);
4330 off += n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004331 chbuf += n;
4332 size -= n;
4333 }
4334out:
Alan Cox4ac43602006-06-28 04:26:52 -07004335 free_page((unsigned long) p);
4336 *offp = off;
4337 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004338}
4339
4340/*****************************************************************************/
4341
4342/*
4343 * Return the board stats structure to user app.
4344 */
4345
4346static int stli_getbrdstats(combrd_t __user *bp)
4347{
Alan Cox4ac43602006-06-28 04:26:52 -07004348 stlibrd_t *brdp;
4349 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004350
4351 if (copy_from_user(&stli_brdstats, bp, sizeof(combrd_t)))
4352 return -EFAULT;
4353 if (stli_brdstats.brd >= STL_MAXBRDS)
Alan Cox4ac43602006-06-28 04:26:52 -07004354 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004355 brdp = stli_brds[stli_brdstats.brd];
Alan Cox4ac43602006-06-28 04:26:52 -07004356 if (brdp == NULL)
4357 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004358
4359 memset(&stli_brdstats, 0, sizeof(combrd_t));
4360 stli_brdstats.brd = brdp->brdnr;
4361 stli_brdstats.type = brdp->brdtype;
4362 stli_brdstats.hwid = 0;
4363 stli_brdstats.state = brdp->state;
4364 stli_brdstats.ioaddr = brdp->iobase;
4365 stli_brdstats.memaddr = brdp->memaddr;
4366 stli_brdstats.nrpanels = brdp->nrpanels;
4367 stli_brdstats.nrports = brdp->nrports;
4368 for (i = 0; (i < brdp->nrpanels); i++) {
4369 stli_brdstats.panels[i].panel = i;
4370 stli_brdstats.panels[i].hwid = brdp->panelids[i];
4371 stli_brdstats.panels[i].nrports = brdp->panels[i];
4372 }
4373
4374 if (copy_to_user(bp, &stli_brdstats, sizeof(combrd_t)))
4375 return -EFAULT;
Alan Cox4ac43602006-06-28 04:26:52 -07004376 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004377}
4378
4379/*****************************************************************************/
4380
4381/*
4382 * Resolve the referenced port number into a port struct pointer.
4383 */
4384
4385static stliport_t *stli_getport(int brdnr, int panelnr, int portnr)
4386{
Alan Cox4ac43602006-06-28 04:26:52 -07004387 stlibrd_t *brdp;
4388 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004389
Alan Cox4ac43602006-06-28 04:26:52 -07004390 if (brdnr < 0 || brdnr >= STL_MAXBRDS)
4391 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004392 brdp = stli_brds[brdnr];
Alan Cox4ac43602006-06-28 04:26:52 -07004393 if (brdp == NULL)
4394 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004395 for (i = 0; (i < panelnr); i++)
4396 portnr += brdp->panels[i];
4397 if ((portnr < 0) || (portnr >= brdp->nrports))
Alan Cox4ac43602006-06-28 04:26:52 -07004398 return NULL;
4399 return brdp->ports[portnr];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004400}
4401
4402/*****************************************************************************/
4403
4404/*
4405 * Return the port stats structure to user app. A NULL port struct
4406 * pointer passed in means that we need to find out from the app
4407 * what port to get stats for (used through board control device).
4408 */
4409
4410static int stli_portcmdstats(stliport_t *portp)
4411{
4412 unsigned long flags;
4413 stlibrd_t *brdp;
4414 int rc;
4415
4416 memset(&stli_comstats, 0, sizeof(comstats_t));
4417
Alan Cox4ac43602006-06-28 04:26:52 -07004418 if (portp == NULL)
4419 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004420 brdp = stli_brds[portp->brdnr];
Alan Cox4ac43602006-06-28 04:26:52 -07004421 if (brdp == NULL)
4422 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423
4424 if (brdp->state & BST_STARTED) {
4425 if ((rc = stli_cmdwait(brdp, portp, A_GETSTATS,
4426 &stli_cdkstats, sizeof(asystats_t), 1)) < 0)
Alan Cox4ac43602006-06-28 04:26:52 -07004427 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004428 } else {
4429 memset(&stli_cdkstats, 0, sizeof(asystats_t));
4430 }
4431
4432 stli_comstats.brd = portp->brdnr;
4433 stli_comstats.panel = portp->panelnr;
4434 stli_comstats.port = portp->portnr;
4435 stli_comstats.state = portp->state;
4436 stli_comstats.flags = portp->flags;
4437
Alan Cox4ac43602006-06-28 04:26:52 -07004438 spin_lock_irqsave(&brd_lock, flags);
4439 if (portp->tty != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004440 if (portp->tty->driver_data == portp) {
4441 stli_comstats.ttystate = portp->tty->flags;
Alan Cox4ac43602006-06-28 04:26:52 -07004442 stli_comstats.rxbuffered = -1;
4443 if (portp->tty->termios != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004444 stli_comstats.cflags = portp->tty->termios->c_cflag;
4445 stli_comstats.iflags = portp->tty->termios->c_iflag;
4446 stli_comstats.oflags = portp->tty->termios->c_oflag;
4447 stli_comstats.lflags = portp->tty->termios->c_lflag;
4448 }
4449 }
4450 }
Alan Cox4ac43602006-06-28 04:26:52 -07004451 spin_unlock_irqrestore(&brd_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004452
4453 stli_comstats.txtotal = stli_cdkstats.txchars;
4454 stli_comstats.rxtotal = stli_cdkstats.rxchars + stli_cdkstats.ringover;
4455 stli_comstats.txbuffered = stli_cdkstats.txringq;
4456 stli_comstats.rxbuffered += stli_cdkstats.rxringq;
4457 stli_comstats.rxoverrun = stli_cdkstats.overruns;
4458 stli_comstats.rxparity = stli_cdkstats.parity;
4459 stli_comstats.rxframing = stli_cdkstats.framing;
4460 stli_comstats.rxlost = stli_cdkstats.ringover;
4461 stli_comstats.rxbreaks = stli_cdkstats.rxbreaks;
4462 stli_comstats.txbreaks = stli_cdkstats.txbreaks;
4463 stli_comstats.txxon = stli_cdkstats.txstart;
4464 stli_comstats.txxoff = stli_cdkstats.txstop;
4465 stli_comstats.rxxon = stli_cdkstats.rxstart;
4466 stli_comstats.rxxoff = stli_cdkstats.rxstop;
4467 stli_comstats.rxrtsoff = stli_cdkstats.rtscnt / 2;
4468 stli_comstats.rxrtson = stli_cdkstats.rtscnt - stli_comstats.rxrtsoff;
4469 stli_comstats.modem = stli_cdkstats.dcdcnt;
4470 stli_comstats.hwid = stli_cdkstats.hwid;
4471 stli_comstats.signals = stli_mktiocm(stli_cdkstats.signals);
4472
Alan Cox4ac43602006-06-28 04:26:52 -07004473 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004474}
4475
4476/*****************************************************************************/
4477
4478/*
4479 * Return the port stats structure to user app. A NULL port struct
4480 * pointer passed in means that we need to find out from the app
4481 * what port to get stats for (used through board control device).
4482 */
4483
4484static int stli_getportstats(stliport_t *portp, comstats_t __user *cp)
4485{
Alan Cox4ac43602006-06-28 04:26:52 -07004486 stlibrd_t *brdp;
4487 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004488
4489 if (!portp) {
4490 if (copy_from_user(&stli_comstats, cp, sizeof(comstats_t)))
4491 return -EFAULT;
4492 portp = stli_getport(stli_comstats.brd, stli_comstats.panel,
4493 stli_comstats.port);
4494 if (!portp)
4495 return -ENODEV;
4496 }
4497
4498 brdp = stli_brds[portp->brdnr];
4499 if (!brdp)
4500 return -ENODEV;
4501
4502 if ((rc = stli_portcmdstats(portp)) < 0)
4503 return rc;
4504
4505 return copy_to_user(cp, &stli_comstats, sizeof(comstats_t)) ?
4506 -EFAULT : 0;
4507}
4508
4509/*****************************************************************************/
4510
4511/*
4512 * Clear the port stats structure. We also return it zeroed out...
4513 */
4514
4515static int stli_clrportstats(stliport_t *portp, comstats_t __user *cp)
4516{
Alan Cox4ac43602006-06-28 04:26:52 -07004517 stlibrd_t *brdp;
4518 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004519
4520 if (!portp) {
4521 if (copy_from_user(&stli_comstats, cp, sizeof(comstats_t)))
4522 return -EFAULT;
4523 portp = stli_getport(stli_comstats.brd, stli_comstats.panel,
4524 stli_comstats.port);
4525 if (!portp)
4526 return -ENODEV;
4527 }
4528
4529 brdp = stli_brds[portp->brdnr];
4530 if (!brdp)
4531 return -ENODEV;
4532
4533 if (brdp->state & BST_STARTED) {
4534 if ((rc = stli_cmdwait(brdp, portp, A_CLEARSTATS, NULL, 0, 0)) < 0)
4535 return rc;
4536 }
4537
4538 memset(&stli_comstats, 0, sizeof(comstats_t));
4539 stli_comstats.brd = portp->brdnr;
4540 stli_comstats.panel = portp->panelnr;
4541 stli_comstats.port = portp->portnr;
4542
4543 if (copy_to_user(cp, &stli_comstats, sizeof(comstats_t)))
4544 return -EFAULT;
4545 return 0;
4546}
4547
4548/*****************************************************************************/
4549
4550/*
4551 * Return the entire driver ports structure to a user app.
4552 */
4553
4554static int stli_getportstruct(stliport_t __user *arg)
4555{
Alan Cox4ac43602006-06-28 04:26:52 -07004556 stliport_t *portp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004557
4558 if (copy_from_user(&stli_dummyport, arg, sizeof(stliport_t)))
4559 return -EFAULT;
4560 portp = stli_getport(stli_dummyport.brdnr, stli_dummyport.panelnr,
4561 stli_dummyport.portnr);
4562 if (!portp)
4563 return -ENODEV;
4564 if (copy_to_user(arg, portp, sizeof(stliport_t)))
4565 return -EFAULT;
4566 return 0;
4567}
4568
4569/*****************************************************************************/
4570
4571/*
4572 * Return the entire driver board structure to a user app.
4573 */
4574
4575static int stli_getbrdstruct(stlibrd_t __user *arg)
4576{
Alan Cox4ac43602006-06-28 04:26:52 -07004577 stlibrd_t *brdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004578
4579 if (copy_from_user(&stli_dummybrd, arg, sizeof(stlibrd_t)))
4580 return -EFAULT;
4581 if ((stli_dummybrd.brdnr < 0) || (stli_dummybrd.brdnr >= STL_MAXBRDS))
4582 return -ENODEV;
4583 brdp = stli_brds[stli_dummybrd.brdnr];
4584 if (!brdp)
4585 return -ENODEV;
4586 if (copy_to_user(arg, brdp, sizeof(stlibrd_t)))
4587 return -EFAULT;
4588 return 0;
4589}
4590
4591/*****************************************************************************/
4592
4593/*
4594 * The "staliomem" device is also required to do some special operations on
4595 * the board. We need to be able to send an interrupt to the board,
4596 * reset it, and start/stop it.
4597 */
4598
4599static int stli_memioctl(struct inode *ip, struct file *fp, unsigned int cmd, unsigned long arg)
4600{
Alan Cox4ac43602006-06-28 04:26:52 -07004601 stlibrd_t *brdp;
4602 int brdnr, rc, done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004603 void __user *argp = (void __user *)arg;
4604
Linus Torvalds1da177e2005-04-16 15:20:36 -07004605/*
4606 * First up handle the board independent ioctls.
4607 */
4608 done = 0;
4609 rc = 0;
4610
4611 switch (cmd) {
4612 case COM_GETPORTSTATS:
4613 rc = stli_getportstats(NULL, argp);
4614 done++;
4615 break;
4616 case COM_CLRPORTSTATS:
4617 rc = stli_clrportstats(NULL, argp);
4618 done++;
4619 break;
4620 case COM_GETBRDSTATS:
4621 rc = stli_getbrdstats(argp);
4622 done++;
4623 break;
4624 case COM_READPORT:
4625 rc = stli_getportstruct(argp);
4626 done++;
4627 break;
4628 case COM_READBOARD:
4629 rc = stli_getbrdstruct(argp);
4630 done++;
4631 break;
4632 }
4633
4634 if (done)
Alan Cox4ac43602006-06-28 04:26:52 -07004635 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004636
4637/*
4638 * Now handle the board specific ioctls. These all depend on the
4639 * minor number of the device they were called from.
4640 */
4641 brdnr = iminor(ip);
4642 if (brdnr >= STL_MAXBRDS)
Alan Cox4ac43602006-06-28 04:26:52 -07004643 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004644 brdp = stli_brds[brdnr];
4645 if (!brdp)
Alan Cox4ac43602006-06-28 04:26:52 -07004646 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004647 if (brdp->state == 0)
Alan Cox4ac43602006-06-28 04:26:52 -07004648 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004649
4650 switch (cmd) {
4651 case STL_BINTR:
4652 EBRDINTR(brdp);
4653 break;
4654 case STL_BSTART:
4655 rc = stli_startbrd(brdp);
4656 break;
4657 case STL_BSTOP:
4658 brdp->state &= ~BST_STARTED;
4659 break;
4660 case STL_BRESET:
4661 brdp->state &= ~BST_STARTED;
4662 EBRDRESET(brdp);
4663 if (stli_shared == 0) {
4664 if (brdp->reenable != NULL)
4665 (* brdp->reenable)(brdp);
4666 }
4667 break;
4668 default:
4669 rc = -ENOIOCTLCMD;
4670 break;
4671 }
Alan Cox4ac43602006-06-28 04:26:52 -07004672 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004673}
4674
4675static struct tty_operations stli_ops = {
4676 .open = stli_open,
4677 .close = stli_close,
4678 .write = stli_write,
4679 .put_char = stli_putchar,
4680 .flush_chars = stli_flushchars,
4681 .write_room = stli_writeroom,
4682 .chars_in_buffer = stli_charsinbuffer,
4683 .ioctl = stli_ioctl,
4684 .set_termios = stli_settermios,
4685 .throttle = stli_throttle,
4686 .unthrottle = stli_unthrottle,
4687 .stop = stli_stop,
4688 .start = stli_start,
4689 .hangup = stli_hangup,
4690 .flush_buffer = stli_flushbuffer,
4691 .break_ctl = stli_breakctl,
4692 .wait_until_sent = stli_waituntilsent,
4693 .send_xchar = stli_sendxchar,
4694 .read_proc = stli_readproc,
4695 .tiocmget = stli_tiocmget,
4696 .tiocmset = stli_tiocmset,
4697};
4698
4699/*****************************************************************************/
4700
4701int __init stli_init(void)
4702{
4703 int i;
4704 printk(KERN_INFO "%s: version %s\n", stli_drvtitle, stli_drvversion);
4705
Alan Cox4ac43602006-06-28 04:26:52 -07004706 spin_lock_init(&stli_lock);
4707 spin_lock_init(&brd_lock);
4708
Linus Torvalds1da177e2005-04-16 15:20:36 -07004709 stli_initbrds();
4710
4711 stli_serial = alloc_tty_driver(STL_MAXBRDS * STL_MAXPORTS);
4712 if (!stli_serial)
4713 return -ENOMEM;
4714
4715/*
4716 * Allocate a temporary write buffer.
4717 */
Tobias Klauserb0b4ed72006-03-31 02:30:56 -08004718 stli_txcookbuf = kmalloc(STLI_TXBUFSIZE, GFP_KERNEL);
4719 if (!stli_txcookbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004720 printk(KERN_ERR "STALLION: failed to allocate memory "
4721 "(size=%d)\n", STLI_TXBUFSIZE);
4722
4723/*
4724 * Set up a character driver for the shared memory region. We need this
4725 * to down load the slave code image. Also it is a useful debugging tool.
4726 */
4727 if (register_chrdev(STL_SIOMEMMAJOR, "staliomem", &stli_fsiomem))
4728 printk(KERN_ERR "STALLION: failed to register serial memory "
4729 "device\n");
4730
4731 devfs_mk_dir("staliomem");
gregkh@suse.deca8eca62005-03-23 09:53:09 -08004732 istallion_class = class_create(THIS_MODULE, "staliomem");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004733 for (i = 0; i < 4; i++) {
4734 devfs_mk_cdev(MKDEV(STL_SIOMEMMAJOR, i),
4735 S_IFCHR | S_IRUSR | S_IWUSR,
4736 "staliomem/%d", i);
Greg Kroah-Hartman53f46542005-10-27 22:25:43 -07004737 class_device_create(istallion_class, NULL,
4738 MKDEV(STL_SIOMEMMAJOR, i),
Linus Torvalds1da177e2005-04-16 15:20:36 -07004739 NULL, "staliomem%d", i);
4740 }
4741
4742/*
4743 * Set up the tty driver structure and register us as a driver.
4744 */
4745 stli_serial->owner = THIS_MODULE;
4746 stli_serial->driver_name = stli_drvname;
4747 stli_serial->name = stli_serialname;
4748 stli_serial->major = STL_SERIALMAJOR;
4749 stli_serial->minor_start = 0;
4750 stli_serial->type = TTY_DRIVER_TYPE_SERIAL;
4751 stli_serial->subtype = SERIAL_TYPE_NORMAL;
4752 stli_serial->init_termios = stli_deftermios;
4753 stli_serial->flags = TTY_DRIVER_REAL_RAW;
4754 tty_set_operations(stli_serial, &stli_ops);
4755
4756 if (tty_register_driver(stli_serial)) {
4757 put_tty_driver(stli_serial);
4758 printk(KERN_ERR "STALLION: failed to register serial driver\n");
4759 return -EBUSY;
4760 }
Alan Cox4ac43602006-06-28 04:26:52 -07004761 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004762}
4763
4764/*****************************************************************************/