blob: 2f56bc7d147b17c84d82bb4c6c241fd832b60e93 [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 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
18
19/*****************************************************************************/
20
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/module.h>
22#include <linux/slab.h>
23#include <linux/interrupt.h>
24#include <linux/tty.h>
25#include <linux/tty_flip.h>
26#include <linux/serial.h>
27#include <linux/cdk.h>
28#include <linux/comstats.h>
29#include <linux/istallion.h>
30#include <linux/ioport.h>
31#include <linux/delay.h>
32#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/device.h>
34#include <linux/wait.h>
Alan Cox4ac43602006-06-28 04:26:52 -070035#include <linux/eisa.h>
Jiri Slabya3f8d9d2006-12-08 02:39:18 -080036#include <linux/ctype.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38#include <asm/io.h>
39#include <asm/uaccess.h>
40
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/pci.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43/*****************************************************************************/
44
45/*
46 * Define different board types. Not all of the following board types
47 * are supported by this driver. But I will use the standard "assigned"
48 * board numbers. Currently supported boards are abbreviated as:
49 * ECP = EasyConnection 8/64, ONB = ONboard, BBY = Brumby and
50 * STAL = Stallion.
51 */
52#define BRD_UNKNOWN 0
53#define BRD_STALLION 1
54#define BRD_BRUMBY4 2
55#define BRD_ONBOARD2 3
56#define BRD_ONBOARD 4
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#define BRD_ONBOARDE 7
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#define BRD_ECP 23
59#define BRD_ECPE 24
60#define BRD_ECPMC 25
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#define BRD_ECPPCI 29
62
63#define BRD_BRUMBY BRD_BRUMBY4
64
65/*
66 * Define a configuration structure to hold the board configuration.
67 * Need to set this up in the code (for now) with the boards that are
68 * to be configured into the system. This is what needs to be modified
69 * when adding/removing/modifying boards. Each line entry in the
70 * stli_brdconf[] array is a board. Each line contains io/irq/memory
71 * ranges for that board (as well as what type of board it is).
72 * Some examples:
73 * { BRD_ECP, 0x2a0, 0, 0xcc000, 0, 0 },
74 * This line will configure an EasyConnection 8/64 at io address 2a0,
75 * and shared memory address of cc000. Multiple EasyConnection 8/64
76 * boards can share the same shared memory address space. No interrupt
77 * is required for this board type.
78 * Another example:
79 * { BRD_ECPE, 0x5000, 0, 0x80000000, 0, 0 },
80 * This line will configure an EasyConnection 8/64 EISA in slot 5 and
81 * shared memory address of 0x80000000 (2 GByte). Multiple
82 * EasyConnection 8/64 EISA boards can share the same shared memory
83 * address space. No interrupt is required for this board type.
84 * Another example:
85 * { BRD_ONBOARD, 0x240, 0, 0xd0000, 0, 0 },
86 * This line will configure an ONboard (ISA type) at io address 240,
87 * and shared memory address of d0000. Multiple ONboards can share
88 * the same shared memory address space. No interrupt required.
89 * Another example:
90 * { BRD_BRUMBY4, 0x360, 0, 0xc8000, 0, 0 },
91 * This line will configure a Brumby board (any number of ports!) at
92 * io address 360 and shared memory address of c8000. All Brumby boards
93 * configured into a system must have their own separate io and memory
94 * addresses. No interrupt is required.
95 * Another example:
96 * { BRD_STALLION, 0x330, 0, 0xd0000, 0, 0 },
97 * This line will configure an original Stallion board at io address 330
98 * and shared memory address d0000 (this would only be valid for a "V4.0"
99 * or Rev.O Stallion board). All Stallion boards configured into the
100 * system must have their own separate io and memory addresses. No
101 * interrupt is required.
102 */
103
104typedef struct {
105 int brdtype;
106 int ioaddr1;
107 int ioaddr2;
108 unsigned long memaddr;
109 int irq;
110 int irqtype;
111} stlconf_t;
112
Jiri Slabya3f8d9d2006-12-08 02:39:18 -0800113static int stli_nrbrds;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Alan Cox4ac43602006-06-28 04:26:52 -0700115/* stli_lock must NOT be taken holding brd_lock */
116static spinlock_t stli_lock; /* TTY logic lock */
117static spinlock_t brd_lock; /* Board logic lock */
118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119/*
120 * There is some experimental EISA board detection code in this driver.
121 * By default it is disabled, but for those that want to try it out,
122 * then set the define below to be 1.
123 */
124#define STLI_EISAPROBE 0
125
126/*****************************************************************************/
127
128/*
129 * Define some important driver characteristics. Device major numbers
130 * allocated as per Linux Device Registry.
131 */
132#ifndef STL_SIOMEMMAJOR
133#define STL_SIOMEMMAJOR 28
134#endif
135#ifndef STL_SERIALMAJOR
136#define STL_SERIALMAJOR 24
137#endif
138#ifndef STL_CALLOUTMAJOR
139#define STL_CALLOUTMAJOR 25
140#endif
141
142/*****************************************************************************/
143
144/*
145 * Define our local driver identity first. Set up stuff to deal with
146 * all the local structures required by a serial tty driver.
147 */
148static char *stli_drvtitle = "Stallion Intelligent Multiport Serial Driver";
149static char *stli_drvname = "istallion";
150static char *stli_drvversion = "5.6.0";
151static char *stli_serialname = "ttyE";
152
153static struct tty_driver *stli_serial;
154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
156#define STLI_TXBUFSIZE 4096
157
158/*
159 * Use a fast local buffer for cooked characters. Typically a whole
160 * bunch of cooked characters come in for a port, 1 at a time. So we
161 * save those up into a local buffer, then write out the whole lot
162 * with a large memcpy. Just use 1 buffer for all ports, since its
163 * use it is only need for short periods of time by each port.
164 */
165static char *stli_txcookbuf;
166static int stli_txcooksize;
167static int stli_txcookrealsize;
168static struct tty_struct *stli_txcooktty;
169
170/*
171 * Define a local default termios struct. All ports will be created
172 * with this termios initially. Basically all it defines is a raw port
173 * at 9600 baud, 8 data bits, no parity, 1 stop bit.
174 */
Alan Cox606d0992006-12-08 02:38:45 -0800175static struct ktermios stli_deftermios = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 .c_cflag = (B9600 | CS8 | CREAD | HUPCL | CLOCAL),
177 .c_cc = INIT_C_CC,
Alan Cox606d0992006-12-08 02:38:45 -0800178 .c_ispeed = 9600,
179 .c_ospeed = 9600,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180};
181
182/*
183 * Define global stats structures. Not used often, and can be
184 * re-used for each stats call.
185 */
186static comstats_t stli_comstats;
187static combrd_t stli_brdstats;
188static asystats_t stli_cdkstats;
189static stlibrd_t stli_dummybrd;
190static stliport_t stli_dummyport;
191
192/*****************************************************************************/
193
194static stlibrd_t *stli_brds[STL_MAXBRDS];
195
196static int stli_shared;
197
198/*
199 * Per board state flags. Used with the state field of the board struct.
200 * Not really much here... All we need to do is keep track of whether
201 * the board has been detected, and whether it is actually running a slave
202 * or not.
203 */
204#define BST_FOUND 0x1
205#define BST_STARTED 0x2
206
207/*
208 * Define the set of port state flags. These are marked for internal
209 * state purposes only, usually to do with the state of communications
210 * with the slave. Most of them need to be updated atomically, so always
211 * use the bit setting operations (unless protected by cli/sti).
212 */
213#define ST_INITIALIZING 1
214#define ST_OPENING 2
215#define ST_CLOSING 3
216#define ST_CMDING 4
217#define ST_TXBUSY 5
218#define ST_RXING 6
219#define ST_DOFLUSHRX 7
220#define ST_DOFLUSHTX 8
221#define ST_DOSIGS 9
222#define ST_RXSTOP 10
223#define ST_GETSIGS 11
224
225/*
226 * Define an array of board names as printable strings. Handy for
227 * referencing boards when printing trace and stuff.
228 */
229static char *stli_brdnames[] = {
230 "Unknown",
231 "Stallion",
232 "Brumby",
233 "ONboard-MC",
234 "ONboard",
235 "Brumby",
236 "Brumby",
237 "ONboard-EI",
Jiri Slabya3f8d9d2006-12-08 02:39:18 -0800238 NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 "ONboard",
240 "ONboard-MC",
241 "ONboard-MC",
Jiri Slabya3f8d9d2006-12-08 02:39:18 -0800242 NULL,
243 NULL,
244 NULL,
245 NULL,
246 NULL,
247 NULL,
248 NULL,
249 NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 "EasyIO",
251 "EC8/32-AT",
252 "EC8/32-MC",
253 "EC8/64-AT",
254 "EC8/64-EI",
255 "EC8/64-MC",
256 "EC8/32-PCI",
257 "EC8/64-PCI",
258 "EasyIO-PCI",
259 "EC/RA-PCI",
260};
261
262/*****************************************************************************/
263
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264/*
265 * Define some string labels for arguments passed from the module
266 * load line. These allow for easy board definitions, and easy
267 * modification of the io, memory and irq resoucres.
268 */
269
270static char *board0[8];
271static char *board1[8];
272static char *board2[8];
273static char *board3[8];
274
275static char **stli_brdsp[] = {
276 (char **) &board0,
277 (char **) &board1,
278 (char **) &board2,
279 (char **) &board3
280};
281
282/*
283 * Define a set of common board names, and types. This is used to
284 * parse any module arguments.
285 */
286
287typedef struct stlibrdtype {
288 char *name;
289 int type;
290} stlibrdtype_t;
291
292static stlibrdtype_t stli_brdstr[] = {
293 { "stallion", BRD_STALLION },
294 { "1", BRD_STALLION },
295 { "brumby", BRD_BRUMBY },
296 { "brumby4", BRD_BRUMBY },
297 { "brumby/4", BRD_BRUMBY },
298 { "brumby-4", BRD_BRUMBY },
299 { "brumby8", BRD_BRUMBY },
300 { "brumby/8", BRD_BRUMBY },
301 { "brumby-8", BRD_BRUMBY },
302 { "brumby16", BRD_BRUMBY },
303 { "brumby/16", BRD_BRUMBY },
304 { "brumby-16", BRD_BRUMBY },
305 { "2", BRD_BRUMBY },
306 { "onboard2", BRD_ONBOARD2 },
307 { "onboard-2", BRD_ONBOARD2 },
308 { "onboard/2", BRD_ONBOARD2 },
309 { "onboard-mc", BRD_ONBOARD2 },
310 { "onboard/mc", BRD_ONBOARD2 },
311 { "onboard-mca", BRD_ONBOARD2 },
312 { "onboard/mca", BRD_ONBOARD2 },
313 { "3", BRD_ONBOARD2 },
314 { "onboard", BRD_ONBOARD },
315 { "onboardat", BRD_ONBOARD },
316 { "4", BRD_ONBOARD },
317 { "onboarde", BRD_ONBOARDE },
318 { "onboard-e", BRD_ONBOARDE },
319 { "onboard/e", BRD_ONBOARDE },
320 { "onboard-ei", BRD_ONBOARDE },
321 { "onboard/ei", BRD_ONBOARDE },
322 { "7", BRD_ONBOARDE },
323 { "ecp", BRD_ECP },
324 { "ecpat", BRD_ECP },
325 { "ec8/64", BRD_ECP },
326 { "ec8/64-at", BRD_ECP },
327 { "ec8/64-isa", BRD_ECP },
328 { "23", BRD_ECP },
329 { "ecpe", BRD_ECPE },
330 { "ecpei", BRD_ECPE },
331 { "ec8/64-e", BRD_ECPE },
332 { "ec8/64-ei", BRD_ECPE },
333 { "24", BRD_ECPE },
334 { "ecpmc", BRD_ECPMC },
335 { "ec8/64-mc", BRD_ECPMC },
336 { "ec8/64-mca", BRD_ECPMC },
337 { "25", BRD_ECPMC },
338 { "ecppci", BRD_ECPPCI },
339 { "ec/ra", BRD_ECPPCI },
340 { "ec/ra-pc", BRD_ECPPCI },
341 { "ec/ra-pci", BRD_ECPPCI },
342 { "29", BRD_ECPPCI },
343};
344
345/*
346 * Define the module agruments.
347 */
348MODULE_AUTHOR("Greg Ungerer");
349MODULE_DESCRIPTION("Stallion Intelligent Multiport Serial Driver");
350MODULE_LICENSE("GPL");
351
352
Rusty Russell8d3b33f2006-03-25 03:07:05 -0800353module_param_array(board0, charp, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354MODULE_PARM_DESC(board0, "Board 0 config -> name[,ioaddr[,memaddr]");
Rusty Russell8d3b33f2006-03-25 03:07:05 -0800355module_param_array(board1, charp, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356MODULE_PARM_DESC(board1, "Board 1 config -> name[,ioaddr[,memaddr]");
Rusty Russell8d3b33f2006-03-25 03:07:05 -0800357module_param_array(board2, charp, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358MODULE_PARM_DESC(board2, "Board 2 config -> name[,ioaddr[,memaddr]");
Rusty Russell8d3b33f2006-03-25 03:07:05 -0800359module_param_array(board3, charp, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360MODULE_PARM_DESC(board3, "Board 3 config -> name[,ioaddr[,memaddr]");
361
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362/*
363 * Set up a default memory address table for EISA board probing.
364 * The default addresses are all bellow 1Mbyte, which has to be the
365 * case anyway. They should be safe, since we only read values from
366 * them, and interrupts are disabled while we do it. If the higher
367 * memory support is compiled in then we also try probing around
368 * the 1Gb, 2Gb and 3Gb areas as well...
369 */
370static unsigned long stli_eisamemprobeaddrs[] = {
371 0xc0000, 0xd0000, 0xe0000, 0xf0000,
372 0x80000000, 0x80010000, 0x80020000, 0x80030000,
373 0x40000000, 0x40010000, 0x40020000, 0x40030000,
374 0xc0000000, 0xc0010000, 0xc0020000, 0xc0030000,
375 0xff000000, 0xff010000, 0xff020000, 0xff030000,
376};
377
Tobias Klauserfe971072006-01-09 20:54:02 -0800378static int stli_eisamempsize = ARRAY_SIZE(stli_eisamemprobeaddrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
380/*
381 * Define the Stallion PCI vendor and device IDs.
382 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383#ifndef PCI_DEVICE_ID_ECRA
384#define PCI_DEVICE_ID_ECRA 0x0004
385#endif
386
387static struct pci_device_id istallion_pci_tbl[] = {
Alan Cox4ac43602006-06-28 04:26:52 -0700388 { PCI_DEVICE(PCI_VENDOR_ID_STALLION, PCI_DEVICE_ID_ECRA), },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 { 0 }
390};
391MODULE_DEVICE_TABLE(pci, istallion_pci_tbl);
392
Jiri Slaby845bead2006-12-08 02:39:17 -0800393static struct pci_driver stli_pcidriver;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
395/*****************************************************************************/
396
397/*
398 * Hardware configuration info for ECP boards. These defines apply
399 * to the directly accessible io ports of the ECP. There is a set of
400 * defines for each ECP board type, ISA, EISA, MCA and PCI.
401 */
402#define ECP_IOSIZE 4
403
404#define ECP_MEMSIZE (128 * 1024)
405#define ECP_PCIMEMSIZE (256 * 1024)
406
407#define ECP_ATPAGESIZE (4 * 1024)
408#define ECP_MCPAGESIZE (4 * 1024)
409#define ECP_EIPAGESIZE (64 * 1024)
410#define ECP_PCIPAGESIZE (64 * 1024)
411
412#define STL_EISAID 0x8c4e
413
414/*
415 * Important defines for the ISA class of ECP board.
416 */
417#define ECP_ATIREG 0
418#define ECP_ATCONFR 1
419#define ECP_ATMEMAR 2
420#define ECP_ATMEMPR 3
421#define ECP_ATSTOP 0x1
422#define ECP_ATINTENAB 0x10
423#define ECP_ATENABLE 0x20
424#define ECP_ATDISABLE 0x00
425#define ECP_ATADDRMASK 0x3f000
426#define ECP_ATADDRSHFT 12
427
428/*
429 * Important defines for the EISA class of ECP board.
430 */
431#define ECP_EIIREG 0
432#define ECP_EIMEMARL 1
433#define ECP_EICONFR 2
434#define ECP_EIMEMARH 3
435#define ECP_EIENABLE 0x1
436#define ECP_EIDISABLE 0x0
437#define ECP_EISTOP 0x4
438#define ECP_EIEDGE 0x00
439#define ECP_EILEVEL 0x80
440#define ECP_EIADDRMASKL 0x00ff0000
441#define ECP_EIADDRSHFTL 16
442#define ECP_EIADDRMASKH 0xff000000
443#define ECP_EIADDRSHFTH 24
444#define ECP_EIBRDENAB 0xc84
445
446#define ECP_EISAID 0x4
447
448/*
449 * Important defines for the Micro-channel class of ECP board.
450 * (It has a lot in common with the ISA boards.)
451 */
452#define ECP_MCIREG 0
453#define ECP_MCCONFR 1
454#define ECP_MCSTOP 0x20
455#define ECP_MCENABLE 0x80
456#define ECP_MCDISABLE 0x00
457
458/*
459 * Important defines for the PCI class of ECP board.
460 * (It has a lot in common with the other ECP boards.)
461 */
462#define ECP_PCIIREG 0
463#define ECP_PCICONFR 1
464#define ECP_PCISTOP 0x01
465
466/*
467 * Hardware configuration info for ONboard and Brumby boards. These
468 * defines apply to the directly accessible io ports of these boards.
469 */
470#define ONB_IOSIZE 16
471#define ONB_MEMSIZE (64 * 1024)
472#define ONB_ATPAGESIZE (64 * 1024)
473#define ONB_MCPAGESIZE (64 * 1024)
474#define ONB_EIMEMSIZE (128 * 1024)
475#define ONB_EIPAGESIZE (64 * 1024)
476
477/*
478 * Important defines for the ISA class of ONboard board.
479 */
480#define ONB_ATIREG 0
481#define ONB_ATMEMAR 1
482#define ONB_ATCONFR 2
483#define ONB_ATSTOP 0x4
484#define ONB_ATENABLE 0x01
485#define ONB_ATDISABLE 0x00
486#define ONB_ATADDRMASK 0xff0000
487#define ONB_ATADDRSHFT 16
488
489#define ONB_MEMENABLO 0
490#define ONB_MEMENABHI 0x02
491
492/*
493 * Important defines for the EISA class of ONboard board.
494 */
495#define ONB_EIIREG 0
496#define ONB_EIMEMARL 1
497#define ONB_EICONFR 2
498#define ONB_EIMEMARH 3
499#define ONB_EIENABLE 0x1
500#define ONB_EIDISABLE 0x0
501#define ONB_EISTOP 0x4
502#define ONB_EIEDGE 0x00
503#define ONB_EILEVEL 0x80
504#define ONB_EIADDRMASKL 0x00ff0000
505#define ONB_EIADDRSHFTL 16
506#define ONB_EIADDRMASKH 0xff000000
507#define ONB_EIADDRSHFTH 24
508#define ONB_EIBRDENAB 0xc84
509
510#define ONB_EISAID 0x1
511
512/*
513 * Important defines for the Brumby boards. They are pretty simple,
514 * there is not much that is programmably configurable.
515 */
516#define BBY_IOSIZE 16
517#define BBY_MEMSIZE (64 * 1024)
518#define BBY_PAGESIZE (16 * 1024)
519
520#define BBY_ATIREG 0
521#define BBY_ATCONFR 1
522#define BBY_ATSTOP 0x4
523
524/*
525 * Important defines for the Stallion boards. They are pretty simple,
526 * there is not much that is programmably configurable.
527 */
528#define STAL_IOSIZE 16
529#define STAL_MEMSIZE (64 * 1024)
530#define STAL_PAGESIZE (64 * 1024)
531
532/*
533 * Define the set of status register values for EasyConnection panels.
534 * The signature will return with the status value for each panel. From
535 * this we can determine what is attached to the board - before we have
536 * actually down loaded any code to it.
537 */
538#define ECH_PNLSTATUS 2
539#define ECH_PNL16PORT 0x20
540#define ECH_PNLIDMASK 0x07
541#define ECH_PNLXPID 0x40
542#define ECH_PNLINTRPEND 0x80
543
544/*
545 * Define some macros to do things to the board. Even those these boards
546 * are somewhat related there is often significantly different ways of
547 * doing some operation on it (like enable, paging, reset, etc). So each
548 * board class has a set of functions which do the commonly required
549 * operations. The macros below basically just call these functions,
550 * generally checking for a NULL function - which means that the board
551 * needs nothing done to it to achieve this operation!
552 */
553#define EBRDINIT(brdp) \
554 if (brdp->init != NULL) \
555 (* brdp->init)(brdp)
556
557#define EBRDENABLE(brdp) \
558 if (brdp->enable != NULL) \
559 (* brdp->enable)(brdp);
560
561#define EBRDDISABLE(brdp) \
562 if (brdp->disable != NULL) \
563 (* brdp->disable)(brdp);
564
565#define EBRDINTR(brdp) \
566 if (brdp->intr != NULL) \
567 (* brdp->intr)(brdp);
568
569#define EBRDRESET(brdp) \
570 if (brdp->reset != NULL) \
571 (* brdp->reset)(brdp);
572
573#define EBRDGETMEMPTR(brdp,offset) \
574 (* brdp->getmemptr)(brdp, offset, __LINE__)
575
576/*
577 * Define the maximal baud rate, and the default baud base for ports.
578 */
579#define STL_MAXBAUD 460800
580#define STL_BAUDBASE 115200
581#define STL_CLOSEDELAY (5 * HZ / 10)
582
583/*****************************************************************************/
584
585/*
586 * Define macros to extract a brd or port number from a minor number.
587 */
588#define MINOR2BRD(min) (((min) & 0xc0) >> 6)
589#define MINOR2PORT(min) ((min) & 0x3f)
590
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591/*****************************************************************************/
592
593/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 * Prototype all functions in this driver!
595 */
596
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597static int stli_parsebrd(stlconf_t *confp, char **argp);
Adrian Bunk672b2712006-06-30 01:55:30 -0700598static int stli_init(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599static int stli_open(struct tty_struct *tty, struct file *filp);
600static void stli_close(struct tty_struct *tty, struct file *filp);
601static int stli_write(struct tty_struct *tty, const unsigned char *buf, int count);
602static void stli_putchar(struct tty_struct *tty, unsigned char ch);
603static void stli_flushchars(struct tty_struct *tty);
604static int stli_writeroom(struct tty_struct *tty);
605static int stli_charsinbuffer(struct tty_struct *tty);
606static int stli_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg);
Alan Cox606d0992006-12-08 02:38:45 -0800607static void stli_settermios(struct tty_struct *tty, struct ktermios *old);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608static void stli_throttle(struct tty_struct *tty);
609static void stli_unthrottle(struct tty_struct *tty);
610static void stli_stop(struct tty_struct *tty);
611static void stli_start(struct tty_struct *tty);
612static void stli_flushbuffer(struct tty_struct *tty);
613static void stli_breakctl(struct tty_struct *tty, int state);
614static void stli_waituntilsent(struct tty_struct *tty, int timeout);
615static void stli_sendxchar(struct tty_struct *tty, char ch);
616static void stli_hangup(struct tty_struct *tty);
617static int stli_portinfo(stlibrd_t *brdp, stliport_t *portp, int portnr, char *pos);
618
619static int stli_brdinit(stlibrd_t *brdp);
620static int stli_startbrd(stlibrd_t *brdp);
621static ssize_t stli_memread(struct file *fp, char __user *buf, size_t count, loff_t *offp);
622static ssize_t stli_memwrite(struct file *fp, const char __user *buf, size_t count, loff_t *offp);
623static int stli_memioctl(struct inode *ip, struct file *fp, unsigned int cmd, unsigned long arg);
Alan Cox4ac43602006-06-28 04:26:52 -0700624static void stli_brdpoll(stlibrd_t *brdp, cdkhdr_t __iomem *hdrp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625static void stli_poll(unsigned long arg);
626static int stli_hostcmd(stlibrd_t *brdp, stliport_t *portp);
627static int stli_initopen(stlibrd_t *brdp, stliport_t *portp);
628static int stli_rawopen(stlibrd_t *brdp, stliport_t *portp, unsigned long arg, int wait);
629static int stli_rawclose(stlibrd_t *brdp, stliport_t *portp, unsigned long arg, int wait);
630static int stli_waitcarrier(stlibrd_t *brdp, stliport_t *portp, struct file *filp);
Al Viro3e577a82006-12-06 18:41:45 +0000631static void stli_dohangup(struct work_struct *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632static int stli_setport(stliport_t *portp);
633static int stli_cmdwait(stlibrd_t *brdp, stliport_t *portp, unsigned long cmd, void *arg, int size, int copyback);
634static 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 -0700635static void __stli_sendcmd(stlibrd_t *brdp, stliport_t *portp, unsigned long cmd, void *arg, int size, int copyback);
636static void stli_dodelaycmd(stliport_t *portp, cdkctrl_t __iomem *cp);
Alan Cox606d0992006-12-08 02:38:45 -0800637static void stli_mkasyport(stliport_t *portp, asyport_t *pp, struct ktermios *tiosp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638static void stli_mkasysigs(asysigs_t *sp, int dtr, int rts);
639static long stli_mktiocm(unsigned long sigvalue);
640static void stli_read(stlibrd_t *brdp, stliport_t *portp);
641static int stli_getserial(stliport_t *portp, struct serial_struct __user *sp);
642static int stli_setserial(stliport_t *portp, struct serial_struct __user *sp);
643static int stli_getbrdstats(combrd_t __user *bp);
644static int stli_getportstats(stliport_t *portp, comstats_t __user *cp);
645static int stli_portcmdstats(stliport_t *portp);
646static int stli_clrportstats(stliport_t *portp, comstats_t __user *cp);
647static int stli_getportstruct(stliport_t __user *arg);
648static int stli_getbrdstruct(stlibrd_t __user *arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649static stlibrd_t *stli_allocbrd(void);
650
651static void stli_ecpinit(stlibrd_t *brdp);
652static void stli_ecpenable(stlibrd_t *brdp);
653static void stli_ecpdisable(stlibrd_t *brdp);
Al Viro29756fa2006-10-10 22:47:27 +0100654static void __iomem *stli_ecpgetmemptr(stlibrd_t *brdp, unsigned long offset, int line);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655static void stli_ecpreset(stlibrd_t *brdp);
656static void stli_ecpintr(stlibrd_t *brdp);
657static void stli_ecpeiinit(stlibrd_t *brdp);
658static void stli_ecpeienable(stlibrd_t *brdp);
659static void stli_ecpeidisable(stlibrd_t *brdp);
Al Viro29756fa2006-10-10 22:47:27 +0100660static void __iomem *stli_ecpeigetmemptr(stlibrd_t *brdp, unsigned long offset, int line);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661static void stli_ecpeireset(stlibrd_t *brdp);
662static void stli_ecpmcenable(stlibrd_t *brdp);
663static void stli_ecpmcdisable(stlibrd_t *brdp);
Al Viro29756fa2006-10-10 22:47:27 +0100664static void __iomem *stli_ecpmcgetmemptr(stlibrd_t *brdp, unsigned long offset, int line);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665static void stli_ecpmcreset(stlibrd_t *brdp);
666static void stli_ecppciinit(stlibrd_t *brdp);
Al Viro29756fa2006-10-10 22:47:27 +0100667static void __iomem *stli_ecppcigetmemptr(stlibrd_t *brdp, unsigned long offset, int line);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668static void stli_ecppcireset(stlibrd_t *brdp);
669
670static void stli_onbinit(stlibrd_t *brdp);
671static void stli_onbenable(stlibrd_t *brdp);
672static void stli_onbdisable(stlibrd_t *brdp);
Al Viro29756fa2006-10-10 22:47:27 +0100673static void __iomem *stli_onbgetmemptr(stlibrd_t *brdp, unsigned long offset, int line);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674static void stli_onbreset(stlibrd_t *brdp);
675static void stli_onbeinit(stlibrd_t *brdp);
676static void stli_onbeenable(stlibrd_t *brdp);
677static void stli_onbedisable(stlibrd_t *brdp);
Al Viro29756fa2006-10-10 22:47:27 +0100678static void __iomem *stli_onbegetmemptr(stlibrd_t *brdp, unsigned long offset, int line);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679static void stli_onbereset(stlibrd_t *brdp);
680static void stli_bbyinit(stlibrd_t *brdp);
Al Viro29756fa2006-10-10 22:47:27 +0100681static void __iomem *stli_bbygetmemptr(stlibrd_t *brdp, unsigned long offset, int line);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682static void stli_bbyreset(stlibrd_t *brdp);
683static void stli_stalinit(stlibrd_t *brdp);
Al Viro29756fa2006-10-10 22:47:27 +0100684static void __iomem *stli_stalgetmemptr(stlibrd_t *brdp, unsigned long offset, int line);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685static void stli_stalreset(stlibrd_t *brdp);
686
687static stliport_t *stli_getport(int brdnr, int panelnr, int portnr);
688
689static int stli_initecp(stlibrd_t *brdp);
690static int stli_initonb(stlibrd_t *brdp);
691static int stli_eisamemprobe(stlibrd_t *brdp);
692static int stli_initports(stlibrd_t *brdp);
693
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694/*****************************************************************************/
695
696/*
697 * Define the driver info for a user level shared memory device. This
698 * device will work sort of like the /dev/kmem device - except that it
699 * will give access to the shared memory on the Stallion intelligent
700 * board. This is also a very useful debugging tool.
701 */
Arjan van de Ven62322d22006-07-03 00:24:21 -0700702static const struct file_operations stli_fsiomem = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 .owner = THIS_MODULE,
704 .read = stli_memread,
705 .write = stli_memwrite,
706 .ioctl = stli_memioctl,
707};
708
709/*****************************************************************************/
710
711/*
712 * Define a timer_list entry for our poll routine. The slave board
713 * is polled every so often to see if anything needs doing. This is
714 * much cheaper on host cpu than using interrupts. It turns out to
715 * not increase character latency by much either...
716 */
Ingo Molnar8d06afa2005-09-09 13:10:40 -0700717static DEFINE_TIMER(stli_timerlist, stli_poll, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
719static int stli_timeron;
720
721/*
722 * Define the calculation for the timeout routine.
723 */
724#define STLI_TIMEOUT (jiffies + 1)
725
726/*****************************************************************************/
727
gregkh@suse.deca8eca62005-03-23 09:53:09 -0800728static struct class *istallion_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
Jiri Slaby845bead2006-12-08 02:39:17 -0800730static void stli_cleanup_ports(stlibrd_t *brdp)
731{
732 stliport_t *portp;
733 unsigned int j;
734
735 for (j = 0; j < STL_MAXPORTS; j++) {
736 portp = brdp->ports[j];
737 if (portp != NULL) {
738 if (portp->tty != NULL)
739 tty_hangup(portp->tty);
740 kfree(portp);
741 }
742 }
743}
744
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745/*
746 * Loadable module initialization stuff.
747 */
748
749static int __init istallion_module_init(void)
750{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 stli_init();
Alan Cox4ac43602006-06-28 04:26:52 -0700752 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753}
754
755/*****************************************************************************/
756
757static void __exit istallion_module_exit(void)
758{
759 stlibrd_t *brdp;
Jiri Slaby845bead2006-12-08 02:39:17 -0800760 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 printk(KERN_INFO "Unloading %s: version %s\n", stli_drvtitle,
763 stli_drvversion);
764
Jiri Slaby845bead2006-12-08 02:39:17 -0800765 pci_unregister_driver(&stli_pcidriver);
Alan Cox4ac43602006-06-28 04:26:52 -0700766 /*
767 * Free up all allocated resources used by the ports. This includes
768 * memory and interrupts.
769 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 if (stli_timeron) {
771 stli_timeron = 0;
Alan Cox4ac43602006-06-28 04:26:52 -0700772 del_timer_sync(&stli_timerlist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 }
774
775 i = tty_unregister_driver(stli_serial);
776 if (i) {
777 printk("STALLION: failed to un-register tty driver, "
778 "errno=%d\n", -i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 return;
780 }
781 put_tty_driver(stli_serial);
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -0700782 for (i = 0; i < 4; i++)
gregkh@suse.deca8eca62005-03-23 09:53:09 -0800783 class_device_destroy(istallion_class, MKDEV(STL_SIOMEMMAJOR, i));
gregkh@suse.deca8eca62005-03-23 09:53:09 -0800784 class_destroy(istallion_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 if ((i = unregister_chrdev(STL_SIOMEMMAJOR, "staliomem")))
786 printk("STALLION: failed to un-register serial memory device, "
787 "errno=%d\n", -i);
Jesper Juhl735d5662005-11-07 01:01:29 -0800788
Jesper Juhl735d5662005-11-07 01:01:29 -0800789 kfree(stli_txcookbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
791 for (i = 0; (i < stli_nrbrds); i++) {
Alan Cox4ac43602006-06-28 04:26:52 -0700792 if ((brdp = stli_brds[i]) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 continue;
Jiri Slaby845bead2006-12-08 02:39:17 -0800794
795 stli_cleanup_ports(brdp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
797 iounmap(brdp->membase);
798 if (brdp->iosize > 0)
799 release_region(brdp->iobase, brdp->iosize);
800 kfree(brdp);
Alan Cox4ac43602006-06-28 04:26:52 -0700801 stli_brds[i] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803}
804
805module_init(istallion_module_init);
806module_exit(istallion_module_exit);
807
808/*****************************************************************************/
809
810/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 * Parse the supplied argument string, into the board conf struct.
812 */
813
814static int stli_parsebrd(stlconf_t *confp, char **argp)
815{
Alan Cox4ac43602006-06-28 04:26:52 -0700816 char *sp;
817 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
Alan Cox4ac43602006-06-28 04:26:52 -0700819 if (argp[0] == NULL || *argp[0] == 0)
820 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
822 for (sp = argp[0], i = 0; ((*sp != 0) && (i < 25)); sp++, i++)
Jiri Slabya3f8d9d2006-12-08 02:39:18 -0800823 *sp = tolower(*sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
Tobias Klauserfe971072006-01-09 20:54:02 -0800825 for (i = 0; i < ARRAY_SIZE(stli_brdstr); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 if (strcmp(stli_brdstr[i].name, argp[0]) == 0)
827 break;
828 }
Tobias Klauserfe971072006-01-09 20:54:02 -0800829 if (i == ARRAY_SIZE(stli_brdstr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 printk("STALLION: unknown board name, %s?\n", argp[0]);
Tobias Klauserfe971072006-01-09 20:54:02 -0800831 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 }
833
834 confp->brdtype = stli_brdstr[i].type;
Alan Cox4ac43602006-06-28 04:26:52 -0700835 if (argp[1] != NULL && *argp[1] != 0)
Jiri Slabya3f8d9d2006-12-08 02:39:18 -0800836 confp->ioaddr1 = simple_strtoul(argp[1], NULL, 0);
Alan Cox4ac43602006-06-28 04:26:52 -0700837 if (argp[2] != NULL && *argp[2] != 0)
Jiri Slabya3f8d9d2006-12-08 02:39:18 -0800838 confp->memaddr = simple_strtoul(argp[2], NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 return(1);
840}
841
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842/*****************************************************************************/
843
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844static int stli_open(struct tty_struct *tty, struct file *filp)
845{
Alan Cox4ac43602006-06-28 04:26:52 -0700846 stlibrd_t *brdp;
847 stliport_t *portp;
848 unsigned int minordev;
849 int brdnr, portnr, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
851 minordev = tty->index;
852 brdnr = MINOR2BRD(minordev);
853 if (brdnr >= stli_nrbrds)
Alan Cox4ac43602006-06-28 04:26:52 -0700854 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 brdp = stli_brds[brdnr];
Alan Cox4ac43602006-06-28 04:26:52 -0700856 if (brdp == NULL)
857 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 if ((brdp->state & BST_STARTED) == 0)
Alan Cox4ac43602006-06-28 04:26:52 -0700859 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 portnr = MINOR2PORT(minordev);
861 if ((portnr < 0) || (portnr > brdp->nrports))
Alan Cox4ac43602006-06-28 04:26:52 -0700862 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
864 portp = brdp->ports[portnr];
Alan Cox4ac43602006-06-28 04:26:52 -0700865 if (portp == NULL)
866 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 if (portp->devnr < 1)
Alan Cox4ac43602006-06-28 04:26:52 -0700868 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
870
871/*
872 * Check if this port is in the middle of closing. If so then wait
873 * until it is closed then return error status based on flag settings.
874 * The sleep here does not need interrupt protection since the wakeup
875 * for it is done with the same context.
876 */
877 if (portp->flags & ASYNC_CLOSING) {
878 interruptible_sleep_on(&portp->close_wait);
879 if (portp->flags & ASYNC_HUP_NOTIFY)
Alan Cox4ac43602006-06-28 04:26:52 -0700880 return -EAGAIN;
881 return -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 }
883
884/*
885 * On the first open of the device setup the port hardware, and
886 * initialize the per port data structure. Since initializing the port
887 * requires several commands to the board we will need to wait for any
888 * other open that is already initializing the port.
889 */
890 portp->tty = tty;
891 tty->driver_data = portp;
892 portp->refcount++;
893
894 wait_event_interruptible(portp->raw_wait,
895 !test_bit(ST_INITIALIZING, &portp->state));
896 if (signal_pending(current))
Alan Cox4ac43602006-06-28 04:26:52 -0700897 return -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
899 if ((portp->flags & ASYNC_INITIALIZED) == 0) {
900 set_bit(ST_INITIALIZING, &portp->state);
901 if ((rc = stli_initopen(brdp, portp)) >= 0) {
902 portp->flags |= ASYNC_INITIALIZED;
903 clear_bit(TTY_IO_ERROR, &tty->flags);
904 }
905 clear_bit(ST_INITIALIZING, &portp->state);
906 wake_up_interruptible(&portp->raw_wait);
907 if (rc < 0)
Alan Cox4ac43602006-06-28 04:26:52 -0700908 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 }
910
911/*
912 * Check if this port is in the middle of closing. If so then wait
913 * until it is closed then return error status, based on flag settings.
914 * The sleep here does not need interrupt protection since the wakeup
915 * for it is done with the same context.
916 */
917 if (portp->flags & ASYNC_CLOSING) {
918 interruptible_sleep_on(&portp->close_wait);
919 if (portp->flags & ASYNC_HUP_NOTIFY)
Alan Cox4ac43602006-06-28 04:26:52 -0700920 return -EAGAIN;
921 return -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 }
923
924/*
925 * Based on type of open being done check if it can overlap with any
926 * previous opens still in effect. If we are a normal serial device
927 * then also we might have to wait for carrier.
928 */
929 if (!(filp->f_flags & O_NONBLOCK)) {
930 if ((rc = stli_waitcarrier(brdp, portp, filp)) != 0)
Alan Cox4ac43602006-06-28 04:26:52 -0700931 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 }
933 portp->flags |= ASYNC_NORMAL_ACTIVE;
Alan Cox4ac43602006-06-28 04:26:52 -0700934 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935}
936
937/*****************************************************************************/
938
939static void stli_close(struct tty_struct *tty, struct file *filp)
940{
Alan Cox4ac43602006-06-28 04:26:52 -0700941 stlibrd_t *brdp;
942 stliport_t *portp;
943 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
945 portp = tty->driver_data;
Alan Cox4ac43602006-06-28 04:26:52 -0700946 if (portp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 return;
948
Alan Cox4ac43602006-06-28 04:26:52 -0700949 spin_lock_irqsave(&stli_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 if (tty_hung_up_p(filp)) {
Alan Cox4ac43602006-06-28 04:26:52 -0700951 spin_unlock_irqrestore(&stli_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 return;
953 }
954 if ((tty->count == 1) && (portp->refcount != 1))
955 portp->refcount = 1;
956 if (portp->refcount-- > 1) {
Alan Cox4ac43602006-06-28 04:26:52 -0700957 spin_unlock_irqrestore(&stli_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 return;
959 }
960
961 portp->flags |= ASYNC_CLOSING;
962
963/*
964 * May want to wait for data to drain before closing. The BUSY flag
965 * keeps track of whether we are still transmitting or not. It is
966 * updated by messages from the slave - indicating when all chars
967 * really have drained.
968 */
969 if (tty == stli_txcooktty)
970 stli_flushchars(tty);
971 tty->closing = 1;
Alan Cox4ac43602006-06-28 04:26:52 -0700972 spin_unlock_irqrestore(&stli_lock, flags);
973
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 if (portp->closing_wait != ASYNC_CLOSING_WAIT_NONE)
975 tty_wait_until_sent(tty, portp->closing_wait);
976
977 portp->flags &= ~ASYNC_INITIALIZED;
978 brdp = stli_brds[portp->brdnr];
979 stli_rawclose(brdp, portp, 0, 0);
980 if (tty->termios->c_cflag & HUPCL) {
981 stli_mkasysigs(&portp->asig, 0, 0);
982 if (test_bit(ST_CMDING, &portp->state))
983 set_bit(ST_DOSIGS, &portp->state);
984 else
985 stli_sendcmd(brdp, portp, A_SETSIGNALS, &portp->asig,
986 sizeof(asysigs_t), 0);
987 }
988 clear_bit(ST_TXBUSY, &portp->state);
989 clear_bit(ST_RXSTOP, &portp->state);
990 set_bit(TTY_IO_ERROR, &tty->flags);
991 if (tty->ldisc.flush_buffer)
992 (tty->ldisc.flush_buffer)(tty);
993 set_bit(ST_DOFLUSHRX, &portp->state);
994 stli_flushbuffer(tty);
995
996 tty->closing = 0;
Alan Cox4ac43602006-06-28 04:26:52 -0700997 portp->tty = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
999 if (portp->openwaitcnt) {
1000 if (portp->close_delay)
1001 msleep_interruptible(jiffies_to_msecs(portp->close_delay));
1002 wake_up_interruptible(&portp->open_wait);
1003 }
1004
1005 portp->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
1006 wake_up_interruptible(&portp->close_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007}
1008
1009/*****************************************************************************/
1010
1011/*
1012 * Carry out first open operations on a port. This involves a number of
1013 * commands to be sent to the slave. We need to open the port, set the
1014 * notification events, set the initial port settings, get and set the
1015 * initial signal values. We sleep and wait in between each one. But
1016 * this still all happens pretty quickly.
1017 */
1018
1019static int stli_initopen(stlibrd_t *brdp, stliport_t *portp)
1020{
Alan Cox4ac43602006-06-28 04:26:52 -07001021 struct tty_struct *tty;
1022 asynotify_t nt;
1023 asyport_t aport;
1024 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
1026 if ((rc = stli_rawopen(brdp, portp, 0, 1)) < 0)
Alan Cox4ac43602006-06-28 04:26:52 -07001027 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
1029 memset(&nt, 0, sizeof(asynotify_t));
1030 nt.data = (DT_TXLOW | DT_TXEMPTY | DT_RXBUSY | DT_RXBREAK);
1031 nt.signal = SG_DCD;
1032 if ((rc = stli_cmdwait(brdp, portp, A_SETNOTIFY, &nt,
1033 sizeof(asynotify_t), 0)) < 0)
Alan Cox4ac43602006-06-28 04:26:52 -07001034 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
1036 tty = portp->tty;
Alan Cox4ac43602006-06-28 04:26:52 -07001037 if (tty == NULL)
1038 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 stli_mkasyport(portp, &aport, tty->termios);
1040 if ((rc = stli_cmdwait(brdp, portp, A_SETPORT, &aport,
1041 sizeof(asyport_t), 0)) < 0)
Alan Cox4ac43602006-06-28 04:26:52 -07001042 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
1044 set_bit(ST_GETSIGS, &portp->state);
1045 if ((rc = stli_cmdwait(brdp, portp, A_GETSIGNALS, &portp->asig,
1046 sizeof(asysigs_t), 1)) < 0)
Alan Cox4ac43602006-06-28 04:26:52 -07001047 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 if (test_and_clear_bit(ST_GETSIGS, &portp->state))
1049 portp->sigs = stli_mktiocm(portp->asig.sigvalue);
1050 stli_mkasysigs(&portp->asig, 1, 1);
1051 if ((rc = stli_cmdwait(brdp, portp, A_SETSIGNALS, &portp->asig,
1052 sizeof(asysigs_t), 0)) < 0)
Alan Cox4ac43602006-06-28 04:26:52 -07001053 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054
Alan Cox4ac43602006-06-28 04:26:52 -07001055 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056}
1057
1058/*****************************************************************************/
1059
1060/*
1061 * Send an open message to the slave. This will sleep waiting for the
1062 * acknowledgement, so must have user context. We need to co-ordinate
1063 * with close events here, since we don't want open and close events
1064 * to overlap.
1065 */
1066
1067static int stli_rawopen(stlibrd_t *brdp, stliport_t *portp, unsigned long arg, int wait)
1068{
Alan Cox4ac43602006-06-28 04:26:52 -07001069 cdkhdr_t __iomem *hdrp;
1070 cdkctrl_t __iomem *cp;
1071 unsigned char __iomem *bits;
1072 unsigned long flags;
1073 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074
1075/*
1076 * Send a message to the slave to open this port.
1077 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
1079/*
1080 * Slave is already closing this port. This can happen if a hangup
1081 * occurs on this port. So we must wait until it is complete. The
1082 * order of opens and closes may not be preserved across shared
1083 * memory, so we must wait until it is complete.
1084 */
1085 wait_event_interruptible(portp->raw_wait,
1086 !test_bit(ST_CLOSING, &portp->state));
1087 if (signal_pending(current)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 return -ERESTARTSYS;
1089 }
1090
1091/*
1092 * Everything is ready now, so write the open message into shared
1093 * memory. Once the message is in set the service bits to say that
1094 * this port wants service.
1095 */
Alan Cox4ac43602006-06-28 04:26:52 -07001096 spin_lock_irqsave(&brd_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 EBRDENABLE(brdp);
Alan Cox4ac43602006-06-28 04:26:52 -07001098 cp = &((cdkasy_t __iomem *) EBRDGETMEMPTR(brdp, portp->addr))->ctrl;
1099 writel(arg, &cp->openarg);
1100 writeb(1, &cp->open);
1101 hdrp = (cdkhdr_t __iomem *) EBRDGETMEMPTR(brdp, CDK_CDKADDR);
1102 bits = ((unsigned char __iomem *) hdrp) + brdp->slaveoffset +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 portp->portidx;
Alan Cox4ac43602006-06-28 04:26:52 -07001104 writeb(readb(bits) | portp->portbit, bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 EBRDDISABLE(brdp);
1106
1107 if (wait == 0) {
Alan Cox4ac43602006-06-28 04:26:52 -07001108 spin_unlock_irqrestore(&brd_lock, flags);
1109 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 }
1111
1112/*
1113 * Slave is in action, so now we must wait for the open acknowledgment
1114 * to come back.
1115 */
1116 rc = 0;
1117 set_bit(ST_OPENING, &portp->state);
Alan Cox4ac43602006-06-28 04:26:52 -07001118 spin_unlock_irqrestore(&brd_lock, flags);
1119
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 wait_event_interruptible(portp->raw_wait,
1121 !test_bit(ST_OPENING, &portp->state));
1122 if (signal_pending(current))
1123 rc = -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124
1125 if ((rc == 0) && (portp->rc != 0))
1126 rc = -EIO;
Alan Cox4ac43602006-06-28 04:26:52 -07001127 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128}
1129
1130/*****************************************************************************/
1131
1132/*
1133 * Send a close message to the slave. Normally this will sleep waiting
1134 * for the acknowledgement, but if wait parameter is 0 it will not. If
1135 * wait is true then must have user context (to sleep).
1136 */
1137
1138static int stli_rawclose(stlibrd_t *brdp, stliport_t *portp, unsigned long arg, int wait)
1139{
Alan Cox4ac43602006-06-28 04:26:52 -07001140 cdkhdr_t __iomem *hdrp;
1141 cdkctrl_t __iomem *cp;
1142 unsigned char __iomem *bits;
1143 unsigned long flags;
1144 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145
1146/*
1147 * Slave is already closing this port. This can happen if a hangup
1148 * occurs on this port.
1149 */
1150 if (wait) {
1151 wait_event_interruptible(portp->raw_wait,
1152 !test_bit(ST_CLOSING, &portp->state));
1153 if (signal_pending(current)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 return -ERESTARTSYS;
1155 }
1156 }
1157
1158/*
1159 * Write the close command into shared memory.
1160 */
Alan Cox4ac43602006-06-28 04:26:52 -07001161 spin_lock_irqsave(&brd_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 EBRDENABLE(brdp);
Alan Cox4ac43602006-06-28 04:26:52 -07001163 cp = &((cdkasy_t __iomem *) EBRDGETMEMPTR(brdp, portp->addr))->ctrl;
1164 writel(arg, &cp->closearg);
1165 writeb(1, &cp->close);
1166 hdrp = (cdkhdr_t __iomem *) EBRDGETMEMPTR(brdp, CDK_CDKADDR);
1167 bits = ((unsigned char __iomem *) hdrp) + brdp->slaveoffset +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 portp->portidx;
Alan Cox4ac43602006-06-28 04:26:52 -07001169 writeb(readb(bits) |portp->portbit, bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 EBRDDISABLE(brdp);
1171
1172 set_bit(ST_CLOSING, &portp->state);
Alan Cox4ac43602006-06-28 04:26:52 -07001173 spin_unlock_irqrestore(&brd_lock, flags);
1174
1175 if (wait == 0)
1176 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177
1178/*
1179 * Slave is in action, so now we must wait for the open acknowledgment
1180 * to come back.
1181 */
1182 rc = 0;
1183 wait_event_interruptible(portp->raw_wait,
1184 !test_bit(ST_CLOSING, &portp->state));
1185 if (signal_pending(current))
1186 rc = -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187
1188 if ((rc == 0) && (portp->rc != 0))
1189 rc = -EIO;
Alan Cox4ac43602006-06-28 04:26:52 -07001190 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191}
1192
1193/*****************************************************************************/
1194
1195/*
1196 * Send a command to the slave and wait for the response. This must
1197 * have user context (it sleeps). This routine is generic in that it
1198 * can send any type of command. Its purpose is to wait for that command
1199 * to complete (as opposed to initiating the command then returning).
1200 */
1201
1202static int stli_cmdwait(stlibrd_t *brdp, stliport_t *portp, unsigned long cmd, void *arg, int size, int copyback)
1203{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 wait_event_interruptible(portp->raw_wait,
1205 !test_bit(ST_CMDING, &portp->state));
Alan Cox4ac43602006-06-28 04:26:52 -07001206 if (signal_pending(current))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 return -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
1209 stli_sendcmd(brdp, portp, cmd, arg, size, copyback);
1210
1211 wait_event_interruptible(portp->raw_wait,
1212 !test_bit(ST_CMDING, &portp->state));
Alan Cox4ac43602006-06-28 04:26:52 -07001213 if (signal_pending(current))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 return -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215
1216 if (portp->rc != 0)
Alan Cox4ac43602006-06-28 04:26:52 -07001217 return -EIO;
1218 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219}
1220
1221/*****************************************************************************/
1222
1223/*
1224 * Send the termios settings for this port to the slave. This sleeps
1225 * waiting for the command to complete - so must have user context.
1226 */
1227
1228static int stli_setport(stliport_t *portp)
1229{
Alan Cox4ac43602006-06-28 04:26:52 -07001230 stlibrd_t *brdp;
1231 asyport_t aport;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232
Alan Cox4ac43602006-06-28 04:26:52 -07001233 if (portp == NULL)
1234 return -ENODEV;
1235 if (portp->tty == NULL)
1236 return -ENODEV;
1237 if (portp->brdnr < 0 && portp->brdnr >= stli_nrbrds)
1238 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 brdp = stli_brds[portp->brdnr];
Alan Cox4ac43602006-06-28 04:26:52 -07001240 if (brdp == NULL)
1241 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242
1243 stli_mkasyport(portp, &aport, portp->tty->termios);
1244 return(stli_cmdwait(brdp, portp, A_SETPORT, &aport, sizeof(asyport_t), 0));
1245}
1246
1247/*****************************************************************************/
1248
1249/*
1250 * Possibly need to wait for carrier (DCD signal) to come high. Say
1251 * maybe because if we are clocal then we don't need to wait...
1252 */
1253
1254static int stli_waitcarrier(stlibrd_t *brdp, stliport_t *portp, struct file *filp)
1255{
Alan Cox4ac43602006-06-28 04:26:52 -07001256 unsigned long flags;
1257 int rc, doclocal;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
1259 rc = 0;
1260 doclocal = 0;
1261
1262 if (portp->tty->termios->c_cflag & CLOCAL)
1263 doclocal++;
1264
Alan Cox4ac43602006-06-28 04:26:52 -07001265 spin_lock_irqsave(&stli_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 portp->openwaitcnt++;
1267 if (! tty_hung_up_p(filp))
1268 portp->refcount--;
Alan Cox4ac43602006-06-28 04:26:52 -07001269 spin_unlock_irqrestore(&stli_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270
1271 for (;;) {
1272 stli_mkasysigs(&portp->asig, 1, 1);
1273 if ((rc = stli_cmdwait(brdp, portp, A_SETSIGNALS,
1274 &portp->asig, sizeof(asysigs_t), 0)) < 0)
1275 break;
1276 if (tty_hung_up_p(filp) ||
1277 ((portp->flags & ASYNC_INITIALIZED) == 0)) {
1278 if (portp->flags & ASYNC_HUP_NOTIFY)
1279 rc = -EBUSY;
1280 else
1281 rc = -ERESTARTSYS;
1282 break;
1283 }
1284 if (((portp->flags & ASYNC_CLOSING) == 0) &&
1285 (doclocal || (portp->sigs & TIOCM_CD))) {
1286 break;
1287 }
1288 if (signal_pending(current)) {
1289 rc = -ERESTARTSYS;
1290 break;
1291 }
1292 interruptible_sleep_on(&portp->open_wait);
1293 }
1294
Alan Cox4ac43602006-06-28 04:26:52 -07001295 spin_lock_irqsave(&stli_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 if (! tty_hung_up_p(filp))
1297 portp->refcount++;
1298 portp->openwaitcnt--;
Alan Cox4ac43602006-06-28 04:26:52 -07001299 spin_unlock_irqrestore(&stli_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300
Alan Cox4ac43602006-06-28 04:26:52 -07001301 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302}
1303
1304/*****************************************************************************/
1305
1306/*
1307 * Write routine. Take the data and put it in the shared memory ring
1308 * queue. If port is not already sending chars then need to mark the
1309 * service bits for this port.
1310 */
1311
1312static int stli_write(struct tty_struct *tty, const unsigned char *buf, int count)
1313{
Alan Cox4ac43602006-06-28 04:26:52 -07001314 cdkasy_t __iomem *ap;
1315 cdkhdr_t __iomem *hdrp;
1316 unsigned char __iomem *bits;
1317 unsigned char __iomem *shbuf;
1318 unsigned char *chbuf;
1319 stliport_t *portp;
1320 stlibrd_t *brdp;
1321 unsigned int len, stlen, head, tail, size;
1322 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 if (tty == stli_txcooktty)
1325 stli_flushchars(tty);
1326 portp = tty->driver_data;
Alan Cox4ac43602006-06-28 04:26:52 -07001327 if (portp == NULL)
1328 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 if ((portp->brdnr < 0) || (portp->brdnr >= stli_nrbrds))
Alan Cox4ac43602006-06-28 04:26:52 -07001330 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 brdp = stli_brds[portp->brdnr];
Alan Cox4ac43602006-06-28 04:26:52 -07001332 if (brdp == NULL)
1333 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 chbuf = (unsigned char *) buf;
1335
1336/*
1337 * All data is now local, shove as much as possible into shared memory.
1338 */
Alan Cox4ac43602006-06-28 04:26:52 -07001339 spin_lock_irqsave(&brd_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 EBRDENABLE(brdp);
Alan Cox4ac43602006-06-28 04:26:52 -07001341 ap = (cdkasy_t __iomem *) EBRDGETMEMPTR(brdp, portp->addr);
1342 head = (unsigned int) readw(&ap->txq.head);
1343 tail = (unsigned int) readw(&ap->txq.tail);
1344 if (tail != ((unsigned int) readw(&ap->txq.tail)))
1345 tail = (unsigned int) readw(&ap->txq.tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 size = portp->txsize;
1347 if (head >= tail) {
1348 len = size - (head - tail) - 1;
1349 stlen = size - head;
1350 } else {
1351 len = tail - head - 1;
1352 stlen = len;
1353 }
1354
Jiri Slabya3f8d9d2006-12-08 02:39:18 -08001355 len = min(len, (unsigned int)count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 count = 0;
Alan Cox4ac43602006-06-28 04:26:52 -07001357 shbuf = (char __iomem *) EBRDGETMEMPTR(brdp, portp->txoffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358
1359 while (len > 0) {
Jiri Slabya3f8d9d2006-12-08 02:39:18 -08001360 stlen = min(len, stlen);
Alan Cox4ac43602006-06-28 04:26:52 -07001361 memcpy_toio(shbuf + head, chbuf, stlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 chbuf += stlen;
1363 len -= stlen;
1364 count += stlen;
1365 head += stlen;
1366 if (head >= size) {
1367 head = 0;
1368 stlen = tail;
1369 }
1370 }
1371
Alan Cox4ac43602006-06-28 04:26:52 -07001372 ap = (cdkasy_t __iomem *) EBRDGETMEMPTR(brdp, portp->addr);
1373 writew(head, &ap->txq.head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 if (test_bit(ST_TXBUSY, &portp->state)) {
Alan Cox4ac43602006-06-28 04:26:52 -07001375 if (readl(&ap->changed.data) & DT_TXEMPTY)
1376 writel(readl(&ap->changed.data) & ~DT_TXEMPTY, &ap->changed.data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 }
Alan Cox4ac43602006-06-28 04:26:52 -07001378 hdrp = (cdkhdr_t __iomem *) EBRDGETMEMPTR(brdp, CDK_CDKADDR);
1379 bits = ((unsigned char __iomem *) hdrp) + brdp->slaveoffset +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 portp->portidx;
Alan Cox4ac43602006-06-28 04:26:52 -07001381 writeb(readb(bits) | portp->portbit, bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 set_bit(ST_TXBUSY, &portp->state);
1383 EBRDDISABLE(brdp);
Alan Cox4ac43602006-06-28 04:26:52 -07001384 spin_unlock_irqrestore(&brd_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385
1386 return(count);
1387}
1388
1389/*****************************************************************************/
1390
1391/*
1392 * Output a single character. We put it into a temporary local buffer
1393 * (for speed) then write out that buffer when the flushchars routine
1394 * is called. There is a safety catch here so that if some other port
1395 * writes chars before the current buffer has been, then we write them
1396 * first them do the new ports.
1397 */
1398
1399static void stli_putchar(struct tty_struct *tty, unsigned char ch)
1400{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 if (tty != stli_txcooktty) {
Alan Cox4ac43602006-06-28 04:26:52 -07001402 if (stli_txcooktty != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 stli_flushchars(stli_txcooktty);
1404 stli_txcooktty = tty;
1405 }
1406
1407 stli_txcookbuf[stli_txcooksize++] = ch;
1408}
1409
1410/*****************************************************************************/
1411
1412/*
1413 * Transfer characters from the local TX cooking buffer to the board.
1414 * We sort of ignore the tty that gets passed in here. We rely on the
1415 * info stored with the TX cook buffer to tell us which port to flush
1416 * the data on. In any case we clean out the TX cook buffer, for re-use
1417 * by someone else.
1418 */
1419
1420static void stli_flushchars(struct tty_struct *tty)
1421{
Alan Cox4ac43602006-06-28 04:26:52 -07001422 cdkhdr_t __iomem *hdrp;
1423 unsigned char __iomem *bits;
1424 cdkasy_t __iomem *ap;
1425 struct tty_struct *cooktty;
1426 stliport_t *portp;
1427 stlibrd_t *brdp;
1428 unsigned int len, stlen, head, tail, size, count, cooksize;
1429 unsigned char *buf;
1430 unsigned char __iomem *shbuf;
1431 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432
1433 cooksize = stli_txcooksize;
1434 cooktty = stli_txcooktty;
1435 stli_txcooksize = 0;
1436 stli_txcookrealsize = 0;
Alan Cox4ac43602006-06-28 04:26:52 -07001437 stli_txcooktty = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438
Alan Cox4ac43602006-06-28 04:26:52 -07001439 if (tty == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 return;
Alan Cox4ac43602006-06-28 04:26:52 -07001441 if (cooktty == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 return;
1443 if (tty != cooktty)
1444 tty = cooktty;
1445 if (cooksize == 0)
1446 return;
1447
1448 portp = tty->driver_data;
Alan Cox4ac43602006-06-28 04:26:52 -07001449 if (portp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 return;
1451 if ((portp->brdnr < 0) || (portp->brdnr >= stli_nrbrds))
1452 return;
1453 brdp = stli_brds[portp->brdnr];
Alan Cox4ac43602006-06-28 04:26:52 -07001454 if (brdp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 return;
1456
Alan Cox4ac43602006-06-28 04:26:52 -07001457 spin_lock_irqsave(&brd_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 EBRDENABLE(brdp);
1459
Alan Cox4ac43602006-06-28 04:26:52 -07001460 ap = (cdkasy_t __iomem *) EBRDGETMEMPTR(brdp, portp->addr);
1461 head = (unsigned int) readw(&ap->txq.head);
1462 tail = (unsigned int) readw(&ap->txq.tail);
1463 if (tail != ((unsigned int) readw(&ap->txq.tail)))
1464 tail = (unsigned int) readw(&ap->txq.tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 size = portp->txsize;
1466 if (head >= tail) {
1467 len = size - (head - tail) - 1;
1468 stlen = size - head;
1469 } else {
1470 len = tail - head - 1;
1471 stlen = len;
1472 }
1473
Jiri Slabya3f8d9d2006-12-08 02:39:18 -08001474 len = min(len, cooksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 count = 0;
Al Viro29756fa2006-10-10 22:47:27 +01001476 shbuf = EBRDGETMEMPTR(brdp, portp->txoffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 buf = stli_txcookbuf;
1478
1479 while (len > 0) {
Jiri Slabya3f8d9d2006-12-08 02:39:18 -08001480 stlen = min(len, stlen);
Alan Cox4ac43602006-06-28 04:26:52 -07001481 memcpy_toio(shbuf + head, buf, stlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 buf += stlen;
1483 len -= stlen;
1484 count += stlen;
1485 head += stlen;
1486 if (head >= size) {
1487 head = 0;
1488 stlen = tail;
1489 }
1490 }
1491
Alan Cox4ac43602006-06-28 04:26:52 -07001492 ap = (cdkasy_t __iomem *) EBRDGETMEMPTR(brdp, portp->addr);
1493 writew(head, &ap->txq.head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494
1495 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
1505 EBRDDISABLE(brdp);
Alan Cox4ac43602006-06-28 04:26:52 -07001506 spin_unlock_irqrestore(&brd_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507}
1508
1509/*****************************************************************************/
1510
1511static int stli_writeroom(struct tty_struct *tty)
1512{
Alan Cox4ac43602006-06-28 04:26:52 -07001513 cdkasyrq_t __iomem *rp;
1514 stliport_t *portp;
1515 stlibrd_t *brdp;
1516 unsigned int head, tail, len;
1517 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 if (tty == stli_txcooktty) {
1520 if (stli_txcookrealsize != 0) {
1521 len = stli_txcookrealsize - stli_txcooksize;
Alan Cox4ac43602006-06-28 04:26:52 -07001522 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 }
1524 }
1525
1526 portp = tty->driver_data;
Alan Cox4ac43602006-06-28 04:26:52 -07001527 if (portp == NULL)
1528 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 if ((portp->brdnr < 0) || (portp->brdnr >= stli_nrbrds))
Alan Cox4ac43602006-06-28 04:26:52 -07001530 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 brdp = stli_brds[portp->brdnr];
Alan Cox4ac43602006-06-28 04:26:52 -07001532 if (brdp == NULL)
1533 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534
Alan Cox4ac43602006-06-28 04:26:52 -07001535 spin_lock_irqsave(&brd_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 EBRDENABLE(brdp);
Alan Cox4ac43602006-06-28 04:26:52 -07001537 rp = &((cdkasy_t __iomem *) EBRDGETMEMPTR(brdp, portp->addr))->txq;
1538 head = (unsigned int) readw(&rp->head);
1539 tail = (unsigned int) readw(&rp->tail);
1540 if (tail != ((unsigned int) readw(&rp->tail)))
1541 tail = (unsigned int) readw(&rp->tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 len = (head >= tail) ? (portp->txsize - (head - tail)) : (tail - head);
1543 len--;
1544 EBRDDISABLE(brdp);
Alan Cox4ac43602006-06-28 04:26:52 -07001545 spin_unlock_irqrestore(&brd_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546
1547 if (tty == stli_txcooktty) {
1548 stli_txcookrealsize = len;
1549 len -= stli_txcooksize;
1550 }
Alan Cox4ac43602006-06-28 04:26:52 -07001551 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552}
1553
1554/*****************************************************************************/
1555
1556/*
1557 * Return the number of characters in the transmit buffer. Normally we
1558 * will return the number of chars in the shared memory ring queue.
1559 * We need to kludge around the case where the shared memory buffer is
1560 * empty but not all characters have drained yet, for this case just
1561 * return that there is 1 character in the buffer!
1562 */
1563
1564static int stli_charsinbuffer(struct tty_struct *tty)
1565{
Alan Cox4ac43602006-06-28 04:26:52 -07001566 cdkasyrq_t __iomem *rp;
1567 stliport_t *portp;
1568 stlibrd_t *brdp;
1569 unsigned int head, tail, len;
1570 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 if (tty == stli_txcooktty)
1573 stli_flushchars(tty);
1574 portp = tty->driver_data;
Alan Cox4ac43602006-06-28 04:26:52 -07001575 if (portp == NULL)
1576 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 if ((portp->brdnr < 0) || (portp->brdnr >= stli_nrbrds))
Alan Cox4ac43602006-06-28 04:26:52 -07001578 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 brdp = stli_brds[portp->brdnr];
Alan Cox4ac43602006-06-28 04:26:52 -07001580 if (brdp == NULL)
1581 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582
Alan Cox4ac43602006-06-28 04:26:52 -07001583 spin_lock_irqsave(&brd_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 EBRDENABLE(brdp);
Alan Cox4ac43602006-06-28 04:26:52 -07001585 rp = &((cdkasy_t __iomem *) EBRDGETMEMPTR(brdp, portp->addr))->txq;
1586 head = (unsigned int) readw(&rp->head);
1587 tail = (unsigned int) readw(&rp->tail);
1588 if (tail != ((unsigned int) readw(&rp->tail)))
1589 tail = (unsigned int) readw(&rp->tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 len = (head >= tail) ? (head - tail) : (portp->txsize - (tail - head));
1591 if ((len == 0) && test_bit(ST_TXBUSY, &portp->state))
1592 len = 1;
1593 EBRDDISABLE(brdp);
Alan Cox4ac43602006-06-28 04:26:52 -07001594 spin_unlock_irqrestore(&brd_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595
Alan Cox4ac43602006-06-28 04:26:52 -07001596 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597}
1598
1599/*****************************************************************************/
1600
1601/*
1602 * Generate the serial struct info.
1603 */
1604
1605static int stli_getserial(stliport_t *portp, struct serial_struct __user *sp)
1606{
Alan Cox4ac43602006-06-28 04:26:52 -07001607 struct serial_struct sio;
1608 stlibrd_t *brdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609
1610 memset(&sio, 0, sizeof(struct serial_struct));
1611 sio.type = PORT_UNKNOWN;
1612 sio.line = portp->portnr;
1613 sio.irq = 0;
1614 sio.flags = portp->flags;
1615 sio.baud_base = portp->baud_base;
1616 sio.close_delay = portp->close_delay;
1617 sio.closing_wait = portp->closing_wait;
1618 sio.custom_divisor = portp->custom_divisor;
1619 sio.xmit_fifo_size = 0;
1620 sio.hub6 = 0;
1621
1622 brdp = stli_brds[portp->brdnr];
Alan Cox4ac43602006-06-28 04:26:52 -07001623 if (brdp != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 sio.port = brdp->iobase;
1625
1626 return copy_to_user(sp, &sio, sizeof(struct serial_struct)) ?
1627 -EFAULT : 0;
1628}
1629
1630/*****************************************************************************/
1631
1632/*
1633 * Set port according to the serial struct info.
1634 * At this point we do not do any auto-configure stuff, so we will
1635 * just quietly ignore any requests to change irq, etc.
1636 */
1637
1638static int stli_setserial(stliport_t *portp, struct serial_struct __user *sp)
1639{
Alan Cox4ac43602006-06-28 04:26:52 -07001640 struct serial_struct sio;
1641 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642
1643 if (copy_from_user(&sio, sp, sizeof(struct serial_struct)))
1644 return -EFAULT;
1645 if (!capable(CAP_SYS_ADMIN)) {
1646 if ((sio.baud_base != portp->baud_base) ||
1647 (sio.close_delay != portp->close_delay) ||
1648 ((sio.flags & ~ASYNC_USR_MASK) !=
1649 (portp->flags & ~ASYNC_USR_MASK)))
Alan Cox4ac43602006-06-28 04:26:52 -07001650 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 }
1652
1653 portp->flags = (portp->flags & ~ASYNC_USR_MASK) |
1654 (sio.flags & ASYNC_USR_MASK);
1655 portp->baud_base = sio.baud_base;
1656 portp->close_delay = sio.close_delay;
1657 portp->closing_wait = sio.closing_wait;
1658 portp->custom_divisor = sio.custom_divisor;
1659
1660 if ((rc = stli_setport(portp)) < 0)
Alan Cox4ac43602006-06-28 04:26:52 -07001661 return rc;
1662 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663}
1664
1665/*****************************************************************************/
1666
1667static int stli_tiocmget(struct tty_struct *tty, struct file *file)
1668{
1669 stliport_t *portp = tty->driver_data;
1670 stlibrd_t *brdp;
1671 int rc;
1672
Alan Cox4ac43602006-06-28 04:26:52 -07001673 if (portp == NULL)
1674 return -ENODEV;
1675 if (portp->brdnr < 0 || portp->brdnr >= stli_nrbrds)
1676 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 brdp = stli_brds[portp->brdnr];
Alan Cox4ac43602006-06-28 04:26:52 -07001678 if (brdp == NULL)
1679 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 if (tty->flags & (1 << TTY_IO_ERROR))
Alan Cox4ac43602006-06-28 04:26:52 -07001681 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682
1683 if ((rc = stli_cmdwait(brdp, portp, A_GETSIGNALS,
1684 &portp->asig, sizeof(asysigs_t), 1)) < 0)
Alan Cox4ac43602006-06-28 04:26:52 -07001685 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686
1687 return stli_mktiocm(portp->asig.sigvalue);
1688}
1689
1690static int stli_tiocmset(struct tty_struct *tty, struct file *file,
1691 unsigned int set, unsigned int clear)
1692{
1693 stliport_t *portp = tty->driver_data;
1694 stlibrd_t *brdp;
1695 int rts = -1, dtr = -1;
1696
Alan Cox4ac43602006-06-28 04:26:52 -07001697 if (portp == NULL)
1698 return -ENODEV;
1699 if (portp->brdnr < 0 || portp->brdnr >= stli_nrbrds)
1700 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 brdp = stli_brds[portp->brdnr];
Alan Cox4ac43602006-06-28 04:26:52 -07001702 if (brdp == NULL)
1703 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 if (tty->flags & (1 << TTY_IO_ERROR))
Alan Cox4ac43602006-06-28 04:26:52 -07001705 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706
1707 if (set & TIOCM_RTS)
1708 rts = 1;
1709 if (set & TIOCM_DTR)
1710 dtr = 1;
1711 if (clear & TIOCM_RTS)
1712 rts = 0;
1713 if (clear & TIOCM_DTR)
1714 dtr = 0;
1715
1716 stli_mkasysigs(&portp->asig, dtr, rts);
1717
1718 return stli_cmdwait(brdp, portp, A_SETSIGNALS, &portp->asig,
1719 sizeof(asysigs_t), 0);
1720}
1721
1722static int stli_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg)
1723{
Alan Cox4ac43602006-06-28 04:26:52 -07001724 stliport_t *portp;
1725 stlibrd_t *brdp;
1726 unsigned int ival;
1727 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 void __user *argp = (void __user *)arg;
1729
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 portp = tty->driver_data;
Alan Cox4ac43602006-06-28 04:26:52 -07001731 if (portp == NULL)
1732 return -ENODEV;
1733 if (portp->brdnr < 0 || portp->brdnr >= stli_nrbrds)
1734 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 brdp = stli_brds[portp->brdnr];
Alan Cox4ac43602006-06-28 04:26:52 -07001736 if (brdp == NULL)
1737 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738
1739 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1740 (cmd != COM_GETPORTSTATS) && (cmd != COM_CLRPORTSTATS)) {
1741 if (tty->flags & (1 << TTY_IO_ERROR))
Alan Cox4ac43602006-06-28 04:26:52 -07001742 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 }
1744
1745 rc = 0;
1746
1747 switch (cmd) {
1748 case TIOCGSOFTCAR:
1749 rc = put_user(((tty->termios->c_cflag & CLOCAL) ? 1 : 0),
1750 (unsigned __user *) arg);
1751 break;
1752 case TIOCSSOFTCAR:
1753 if ((rc = get_user(ival, (unsigned __user *) arg)) == 0)
1754 tty->termios->c_cflag =
1755 (tty->termios->c_cflag & ~CLOCAL) |
1756 (ival ? CLOCAL : 0);
1757 break;
1758 case TIOCGSERIAL:
1759 rc = stli_getserial(portp, argp);
1760 break;
1761 case TIOCSSERIAL:
1762 rc = stli_setserial(portp, argp);
1763 break;
1764 case STL_GETPFLAG:
1765 rc = put_user(portp->pflag, (unsigned __user *)argp);
1766 break;
1767 case STL_SETPFLAG:
1768 if ((rc = get_user(portp->pflag, (unsigned __user *)argp)) == 0)
1769 stli_setport(portp);
1770 break;
1771 case COM_GETPORTSTATS:
1772 rc = stli_getportstats(portp, argp);
1773 break;
1774 case COM_CLRPORTSTATS:
1775 rc = stli_clrportstats(portp, argp);
1776 break;
1777 case TIOCSERCONFIG:
1778 case TIOCSERGWILD:
1779 case TIOCSERSWILD:
1780 case TIOCSERGETLSR:
1781 case TIOCSERGSTRUCT:
1782 case TIOCSERGETMULTI:
1783 case TIOCSERSETMULTI:
1784 default:
1785 rc = -ENOIOCTLCMD;
1786 break;
1787 }
1788
Alan Cox4ac43602006-06-28 04:26:52 -07001789 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790}
1791
1792/*****************************************************************************/
1793
1794/*
1795 * This routine assumes that we have user context and can sleep.
1796 * Looks like it is true for the current ttys implementation..!!
1797 */
1798
Alan Cox606d0992006-12-08 02:38:45 -08001799static void stli_settermios(struct tty_struct *tty, struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800{
Alan Cox4ac43602006-06-28 04:26:52 -07001801 stliport_t *portp;
1802 stlibrd_t *brdp;
Alan Cox606d0992006-12-08 02:38:45 -08001803 struct ktermios *tiosp;
Alan Cox4ac43602006-06-28 04:26:52 -07001804 asyport_t aport;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805
Alan Cox4ac43602006-06-28 04:26:52 -07001806 if (tty == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 return;
1808 portp = tty->driver_data;
Alan Cox4ac43602006-06-28 04:26:52 -07001809 if (portp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 return;
Alan Cox4ac43602006-06-28 04:26:52 -07001811 if (portp->brdnr < 0 || portp->brdnr >= stli_nrbrds)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 return;
1813 brdp = stli_brds[portp->brdnr];
Alan Cox4ac43602006-06-28 04:26:52 -07001814 if (brdp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 return;
1816
1817 tiosp = tty->termios;
1818 if ((tiosp->c_cflag == old->c_cflag) &&
1819 (tiosp->c_iflag == old->c_iflag))
1820 return;
1821
1822 stli_mkasyport(portp, &aport, tiosp);
1823 stli_cmdwait(brdp, portp, A_SETPORT, &aport, sizeof(asyport_t), 0);
1824 stli_mkasysigs(&portp->asig, ((tiosp->c_cflag & CBAUD) ? 1 : 0), -1);
1825 stli_cmdwait(brdp, portp, A_SETSIGNALS, &portp->asig,
1826 sizeof(asysigs_t), 0);
1827 if ((old->c_cflag & CRTSCTS) && ((tiosp->c_cflag & CRTSCTS) == 0))
1828 tty->hw_stopped = 0;
1829 if (((old->c_cflag & CLOCAL) == 0) && (tiosp->c_cflag & CLOCAL))
1830 wake_up_interruptible(&portp->open_wait);
1831}
1832
1833/*****************************************************************************/
1834
1835/*
1836 * Attempt to flow control who ever is sending us data. We won't really
1837 * do any flow control action here. We can't directly, and even if we
1838 * wanted to we would have to send a command to the slave. The slave
1839 * knows how to flow control, and will do so when its buffers reach its
1840 * internal high water marks. So what we will do is set a local state
1841 * bit that will stop us sending any RX data up from the poll routine
1842 * (which is the place where RX data from the slave is handled).
1843 */
1844
1845static void stli_throttle(struct tty_struct *tty)
1846{
Alan Cox4ac43602006-06-28 04:26:52 -07001847 stliport_t *portp = tty->driver_data;
1848 if (portp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 set_bit(ST_RXSTOP, &portp->state);
1851}
1852
1853/*****************************************************************************/
1854
1855/*
1856 * Unflow control the device sending us data... That means that all
1857 * we have to do is clear the RXSTOP state bit. The next poll call
1858 * will then be able to pass the RX data back up.
1859 */
1860
1861static void stli_unthrottle(struct tty_struct *tty)
1862{
Alan Cox4ac43602006-06-28 04:26:52 -07001863 stliport_t *portp = tty->driver_data;
1864 if (portp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 clear_bit(ST_RXSTOP, &portp->state);
1867}
1868
1869/*****************************************************************************/
1870
1871/*
Alan Cox4ac43602006-06-28 04:26:52 -07001872 * Stop the transmitter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 */
1874
1875static void stli_stop(struct tty_struct *tty)
1876{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877}
1878
1879/*****************************************************************************/
1880
1881/*
Alan Cox4ac43602006-06-28 04:26:52 -07001882 * Start the transmitter again.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 */
1884
1885static void stli_start(struct tty_struct *tty)
1886{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887}
1888
1889/*****************************************************************************/
1890
1891/*
1892 * Scheduler called hang up routine. This is called from the scheduler,
1893 * not direct from the driver "poll" routine. We can't call it there
1894 * since the real local hangup code will enable/disable the board and
1895 * other things that we can't do while handling the poll. Much easier
1896 * to deal with it some time later (don't really care when, hangups
1897 * aren't that time critical).
1898 */
1899
Al Viro3e577a82006-12-06 18:41:45 +00001900static void stli_dohangup(struct work_struct *ugly_api)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901{
Al Viro3e577a82006-12-06 18:41:45 +00001902 stliport_t *portp = container_of(ugly_api, stliport_t, tqhangup);
Alan Cox4ac43602006-06-28 04:26:52 -07001903 if (portp->tty != NULL) {
1904 tty_hangup(portp->tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 }
1906}
1907
1908/*****************************************************************************/
1909
1910/*
1911 * Hangup this port. This is pretty much like closing the port, only
1912 * a little more brutal. No waiting for data to drain. Shutdown the
1913 * port and maybe drop signals. This is rather tricky really. We want
1914 * to close the port as well.
1915 */
1916
1917static void stli_hangup(struct tty_struct *tty)
1918{
Alan Cox4ac43602006-06-28 04:26:52 -07001919 stliport_t *portp;
1920 stlibrd_t *brdp;
1921 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 portp = tty->driver_data;
Alan Cox4ac43602006-06-28 04:26:52 -07001924 if (portp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 return;
Alan Cox4ac43602006-06-28 04:26:52 -07001926 if (portp->brdnr < 0 || portp->brdnr >= stli_nrbrds)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 return;
1928 brdp = stli_brds[portp->brdnr];
Alan Cox4ac43602006-06-28 04:26:52 -07001929 if (brdp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 return;
1931
1932 portp->flags &= ~ASYNC_INITIALIZED;
1933
Alan Cox4ac43602006-06-28 04:26:52 -07001934 if (!test_bit(ST_CLOSING, &portp->state))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 stli_rawclose(brdp, portp, 0, 0);
Alan Cox4ac43602006-06-28 04:26:52 -07001936
1937 spin_lock_irqsave(&stli_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 if (tty->termios->c_cflag & HUPCL) {
1939 stli_mkasysigs(&portp->asig, 0, 0);
1940 if (test_bit(ST_CMDING, &portp->state)) {
1941 set_bit(ST_DOSIGS, &portp->state);
1942 set_bit(ST_DOFLUSHTX, &portp->state);
1943 set_bit(ST_DOFLUSHRX, &portp->state);
1944 } else {
1945 stli_sendcmd(brdp, portp, A_SETSIGNALSF,
1946 &portp->asig, sizeof(asysigs_t), 0);
1947 }
1948 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949
1950 clear_bit(ST_TXBUSY, &portp->state);
1951 clear_bit(ST_RXSTOP, &portp->state);
1952 set_bit(TTY_IO_ERROR, &tty->flags);
Alan Cox4ac43602006-06-28 04:26:52 -07001953 portp->tty = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 portp->flags &= ~ASYNC_NORMAL_ACTIVE;
1955 portp->refcount = 0;
Alan Cox4ac43602006-06-28 04:26:52 -07001956 spin_unlock_irqrestore(&stli_lock, flags);
1957
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 wake_up_interruptible(&portp->open_wait);
1959}
1960
1961/*****************************************************************************/
1962
1963/*
1964 * Flush characters from the lower buffer. We may not have user context
1965 * so we cannot sleep waiting for it to complete. Also we need to check
1966 * if there is chars for this port in the TX cook buffer, and flush them
1967 * as well.
1968 */
1969
1970static void stli_flushbuffer(struct tty_struct *tty)
1971{
Alan Cox4ac43602006-06-28 04:26:52 -07001972 stliport_t *portp;
1973 stlibrd_t *brdp;
1974 unsigned long ftype, flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 portp = tty->driver_data;
Alan Cox4ac43602006-06-28 04:26:52 -07001977 if (portp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 return;
Alan Cox4ac43602006-06-28 04:26:52 -07001979 if (portp->brdnr < 0 || portp->brdnr >= stli_nrbrds)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 return;
1981 brdp = stli_brds[portp->brdnr];
Alan Cox4ac43602006-06-28 04:26:52 -07001982 if (brdp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 return;
1984
Alan Cox4ac43602006-06-28 04:26:52 -07001985 spin_lock_irqsave(&brd_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 if (tty == stli_txcooktty) {
Alan Cox4ac43602006-06-28 04:26:52 -07001987 stli_txcooktty = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 stli_txcooksize = 0;
1989 stli_txcookrealsize = 0;
1990 }
1991 if (test_bit(ST_CMDING, &portp->state)) {
1992 set_bit(ST_DOFLUSHTX, &portp->state);
1993 } else {
1994 ftype = FLUSHTX;
1995 if (test_bit(ST_DOFLUSHRX, &portp->state)) {
1996 ftype |= FLUSHRX;
1997 clear_bit(ST_DOFLUSHRX, &portp->state);
1998 }
Alan Cox4ac43602006-06-28 04:26:52 -07001999 __stli_sendcmd(brdp, portp, A_FLUSH, &ftype, sizeof(u32), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 }
Alan Cox4ac43602006-06-28 04:26:52 -07002001 spin_unlock_irqrestore(&brd_lock, flags);
2002 tty_wakeup(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003}
2004
2005/*****************************************************************************/
2006
2007static void stli_breakctl(struct tty_struct *tty, int state)
2008{
2009 stlibrd_t *brdp;
2010 stliport_t *portp;
2011 long arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 portp = tty->driver_data;
Alan Cox4ac43602006-06-28 04:26:52 -07002014 if (portp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 return;
Alan Cox4ac43602006-06-28 04:26:52 -07002016 if (portp->brdnr < 0 || portp->brdnr >= stli_nrbrds)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 return;
2018 brdp = stli_brds[portp->brdnr];
Alan Cox4ac43602006-06-28 04:26:52 -07002019 if (brdp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 return;
2021
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 arg = (state == -1) ? BREAKON : BREAKOFF;
2023 stli_cmdwait(brdp, portp, A_BREAK, &arg, sizeof(long), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024}
2025
2026/*****************************************************************************/
2027
2028static void stli_waituntilsent(struct tty_struct *tty, int timeout)
2029{
Alan Cox4ac43602006-06-28 04:26:52 -07002030 stliport_t *portp;
2031 unsigned long tend;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032
Alan Cox4ac43602006-06-28 04:26:52 -07002033 if (tty == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 return;
2035 portp = tty->driver_data;
Alan Cox4ac43602006-06-28 04:26:52 -07002036 if (portp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 return;
2038
2039 if (timeout == 0)
2040 timeout = HZ;
2041 tend = jiffies + timeout;
2042
2043 while (test_bit(ST_TXBUSY, &portp->state)) {
2044 if (signal_pending(current))
2045 break;
2046 msleep_interruptible(20);
2047 if (time_after_eq(jiffies, tend))
2048 break;
2049 }
2050}
2051
2052/*****************************************************************************/
2053
2054static void stli_sendxchar(struct tty_struct *tty, char ch)
2055{
2056 stlibrd_t *brdp;
2057 stliport_t *portp;
2058 asyctrl_t actrl;
2059
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 portp = tty->driver_data;
Alan Cox4ac43602006-06-28 04:26:52 -07002061 if (portp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 return;
Alan Cox4ac43602006-06-28 04:26:52 -07002063 if (portp->brdnr < 0 || portp->brdnr >= stli_nrbrds)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 return;
2065 brdp = stli_brds[portp->brdnr];
Alan Cox4ac43602006-06-28 04:26:52 -07002066 if (brdp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 return;
2068
2069 memset(&actrl, 0, sizeof(asyctrl_t));
2070 if (ch == STOP_CHAR(tty)) {
2071 actrl.rxctrl = CT_STOPFLOW;
2072 } else if (ch == START_CHAR(tty)) {
2073 actrl.rxctrl = CT_STARTFLOW;
2074 } else {
2075 actrl.txctrl = CT_SENDCHR;
2076 actrl.tximdch = ch;
2077 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 stli_cmdwait(brdp, portp, A_PORTCTRL, &actrl, sizeof(asyctrl_t), 0);
2079}
2080
2081/*****************************************************************************/
2082
2083#define MAXLINE 80
2084
2085/*
2086 * Format info for a specified port. The line is deliberately limited
2087 * to 80 characters. (If it is too long it will be truncated, if too
2088 * short then padded with spaces).
2089 */
2090
2091static int stli_portinfo(stlibrd_t *brdp, stliport_t *portp, int portnr, char *pos)
2092{
Alan Cox4ac43602006-06-28 04:26:52 -07002093 char *sp, *uart;
2094 int rc, cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095
2096 rc = stli_portcmdstats(portp);
2097
2098 uart = "UNKNOWN";
2099 if (brdp->state & BST_STARTED) {
2100 switch (stli_comstats.hwid) {
Alan Cox4ac43602006-06-28 04:26:52 -07002101 case 0: uart = "2681"; break;
2102 case 1: uart = "SC26198"; break;
2103 default:uart = "CD1400"; break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 }
2105 }
2106
2107 sp = pos;
2108 sp += sprintf(sp, "%d: uart:%s ", portnr, uart);
2109
2110 if ((brdp->state & BST_STARTED) && (rc >= 0)) {
2111 sp += sprintf(sp, "tx:%d rx:%d", (int) stli_comstats.txtotal,
2112 (int) stli_comstats.rxtotal);
2113
2114 if (stli_comstats.rxframing)
2115 sp += sprintf(sp, " fe:%d",
2116 (int) stli_comstats.rxframing);
2117 if (stli_comstats.rxparity)
2118 sp += sprintf(sp, " pe:%d",
2119 (int) stli_comstats.rxparity);
2120 if (stli_comstats.rxbreaks)
2121 sp += sprintf(sp, " brk:%d",
2122 (int) stli_comstats.rxbreaks);
2123 if (stli_comstats.rxoverrun)
2124 sp += sprintf(sp, " oe:%d",
2125 (int) stli_comstats.rxoverrun);
2126
2127 cnt = sprintf(sp, "%s%s%s%s%s ",
2128 (stli_comstats.signals & TIOCM_RTS) ? "|RTS" : "",
2129 (stli_comstats.signals & TIOCM_CTS) ? "|CTS" : "",
2130 (stli_comstats.signals & TIOCM_DTR) ? "|DTR" : "",
2131 (stli_comstats.signals & TIOCM_CD) ? "|DCD" : "",
2132 (stli_comstats.signals & TIOCM_DSR) ? "|DSR" : "");
2133 *sp = ' ';
2134 sp += cnt;
2135 }
2136
2137 for (cnt = (sp - pos); (cnt < (MAXLINE - 1)); cnt++)
2138 *sp++ = ' ';
2139 if (cnt >= MAXLINE)
2140 pos[(MAXLINE - 2)] = '+';
2141 pos[(MAXLINE - 1)] = '\n';
2142
2143 return(MAXLINE);
2144}
2145
2146/*****************************************************************************/
2147
2148/*
2149 * Port info, read from the /proc file system.
2150 */
2151
2152static int stli_readproc(char *page, char **start, off_t off, int count, int *eof, void *data)
2153{
Alan Cox4ac43602006-06-28 04:26:52 -07002154 stlibrd_t *brdp;
2155 stliport_t *portp;
2156 int brdnr, portnr, totalport;
2157 int curoff, maxoff;
2158 char *pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159
2160 pos = page;
2161 totalport = 0;
2162 curoff = 0;
2163
2164 if (off == 0) {
2165 pos += sprintf(pos, "%s: version %s", stli_drvtitle,
2166 stli_drvversion);
2167 while (pos < (page + MAXLINE - 1))
2168 *pos++ = ' ';
2169 *pos++ = '\n';
2170 }
2171 curoff = MAXLINE;
2172
2173/*
2174 * We scan through for each board, panel and port. The offset is
2175 * calculated on the fly, and irrelevant ports are skipped.
2176 */
2177 for (brdnr = 0; (brdnr < stli_nrbrds); brdnr++) {
2178 brdp = stli_brds[brdnr];
Alan Cox4ac43602006-06-28 04:26:52 -07002179 if (brdp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 continue;
2181 if (brdp->state == 0)
2182 continue;
2183
2184 maxoff = curoff + (brdp->nrports * MAXLINE);
2185 if (off >= maxoff) {
2186 curoff = maxoff;
2187 continue;
2188 }
2189
2190 totalport = brdnr * STL_MAXPORTS;
2191 for (portnr = 0; (portnr < brdp->nrports); portnr++,
2192 totalport++) {
2193 portp = brdp->ports[portnr];
Alan Cox4ac43602006-06-28 04:26:52 -07002194 if (portp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 continue;
2196 if (off >= (curoff += MAXLINE))
2197 continue;
2198 if ((pos - page + MAXLINE) > count)
2199 goto stli_readdone;
2200 pos += stli_portinfo(brdp, portp, totalport, pos);
2201 }
2202 }
2203
2204 *eof = 1;
2205
2206stli_readdone:
2207 *start = page;
2208 return(pos - page);
2209}
2210
2211/*****************************************************************************/
2212
2213/*
2214 * Generic send command routine. This will send a message to the slave,
2215 * of the specified type with the specified argument. Must be very
2216 * careful of data that will be copied out from shared memory -
2217 * containing command results. The command completion is all done from
2218 * a poll routine that does not have user context. Therefore you cannot
2219 * copy back directly into user space, or to the kernel stack of a
2220 * process. This routine does not sleep, so can be called from anywhere.
Alan Cox4ac43602006-06-28 04:26:52 -07002221 *
2222 * The caller must hold the brd_lock (see also stli_sendcmd the usual
2223 * entry point)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 */
2225
Alan Cox4ac43602006-06-28 04:26:52 -07002226static 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 -07002227{
Alan Cox4ac43602006-06-28 04:26:52 -07002228 cdkhdr_t __iomem *hdrp;
2229 cdkctrl_t __iomem *cp;
2230 unsigned char __iomem *bits;
2231 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232
Alan Cox4ac43602006-06-28 04:26:52 -07002233 spin_lock_irqsave(&brd_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234
2235 if (test_bit(ST_CMDING, &portp->state)) {
2236 printk(KERN_ERR "STALLION: command already busy, cmd=%x!\n",
2237 (int) cmd);
Alan Cox4ac43602006-06-28 04:26:52 -07002238 spin_unlock_irqrestore(&brd_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 return;
2240 }
2241
2242 EBRDENABLE(brdp);
Alan Cox4ac43602006-06-28 04:26:52 -07002243 cp = &((cdkasy_t __iomem *) EBRDGETMEMPTR(brdp, portp->addr))->ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 if (size > 0) {
Alan Cox4ac43602006-06-28 04:26:52 -07002245 memcpy_toio((void __iomem *) &(cp->args[0]), arg, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 if (copyback) {
2247 portp->argp = arg;
2248 portp->argsize = size;
2249 }
2250 }
Alan Cox4ac43602006-06-28 04:26:52 -07002251 writel(0, &cp->status);
2252 writel(cmd, &cp->cmd);
2253 hdrp = (cdkhdr_t __iomem *) EBRDGETMEMPTR(brdp, CDK_CDKADDR);
2254 bits = ((unsigned char __iomem *) hdrp) + brdp->slaveoffset +
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 portp->portidx;
Alan Cox4ac43602006-06-28 04:26:52 -07002256 writeb(readb(bits) | portp->portbit, bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 set_bit(ST_CMDING, &portp->state);
2258 EBRDDISABLE(brdp);
Alan Cox4ac43602006-06-28 04:26:52 -07002259 spin_unlock_irqrestore(&brd_lock, flags);
2260}
2261
2262static void stli_sendcmd(stlibrd_t *brdp, stliport_t *portp, unsigned long cmd, void *arg, int size, int copyback)
2263{
2264 unsigned long flags;
2265
2266 spin_lock_irqsave(&brd_lock, flags);
2267 __stli_sendcmd(brdp, portp, cmd, arg, size, copyback);
2268 spin_unlock_irqrestore(&brd_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269}
2270
2271/*****************************************************************************/
2272
2273/*
2274 * Read data from shared memory. This assumes that the shared memory
2275 * is enabled and that interrupts are off. Basically we just empty out
2276 * the shared memory buffer into the tty buffer. Must be careful to
2277 * handle the case where we fill up the tty buffer, but still have
2278 * more chars to unload.
2279 */
2280
2281static void stli_read(stlibrd_t *brdp, stliport_t *portp)
2282{
Alan Cox4ac43602006-06-28 04:26:52 -07002283 cdkasyrq_t __iomem *rp;
2284 char __iomem *shbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 struct tty_struct *tty;
Alan Cox4ac43602006-06-28 04:26:52 -07002286 unsigned int head, tail, size;
2287 unsigned int len, stlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288
2289 if (test_bit(ST_RXSTOP, &portp->state))
2290 return;
2291 tty = portp->tty;
Alan Cox4ac43602006-06-28 04:26:52 -07002292 if (tty == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 return;
2294
Alan Cox4ac43602006-06-28 04:26:52 -07002295 rp = &((cdkasy_t __iomem *) EBRDGETMEMPTR(brdp, portp->addr))->rxq;
2296 head = (unsigned int) readw(&rp->head);
2297 if (head != ((unsigned int) readw(&rp->head)))
2298 head = (unsigned int) readw(&rp->head);
2299 tail = (unsigned int) readw(&rp->tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 size = portp->rxsize;
2301 if (head >= tail) {
2302 len = head - tail;
2303 stlen = len;
2304 } else {
2305 len = size - (tail - head);
2306 stlen = size - tail;
2307 }
2308
Alan Cox33f0f882006-01-09 20:54:13 -08002309 len = tty_buffer_request_room(tty, len);
Alan Cox4ac43602006-06-28 04:26:52 -07002310
2311 shbuf = (char __iomem *) EBRDGETMEMPTR(brdp, portp->rxoffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312
2313 while (len > 0) {
Alan Cox4ac43602006-06-28 04:26:52 -07002314 unsigned char *cptr;
2315
Jiri Slabya3f8d9d2006-12-08 02:39:18 -08002316 stlen = min(len, stlen);
Alan Cox4ac43602006-06-28 04:26:52 -07002317 tty_prepare_flip_string(tty, &cptr, stlen);
2318 memcpy_fromio(cptr, shbuf + tail, stlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 len -= stlen;
2320 tail += stlen;
2321 if (tail >= size) {
2322 tail = 0;
2323 stlen = head;
2324 }
2325 }
Alan Cox4ac43602006-06-28 04:26:52 -07002326 rp = &((cdkasy_t __iomem *) EBRDGETMEMPTR(brdp, portp->addr))->rxq;
2327 writew(tail, &rp->tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328
2329 if (head != tail)
2330 set_bit(ST_RXING, &portp->state);
2331
2332 tty_schedule_flip(tty);
2333}
2334
2335/*****************************************************************************/
2336
2337/*
2338 * Set up and carry out any delayed commands. There is only a small set
2339 * of slave commands that can be done "off-level". So it is not too
2340 * difficult to deal with them here.
2341 */
2342
Alan Cox4ac43602006-06-28 04:26:52 -07002343static void stli_dodelaycmd(stliport_t *portp, cdkctrl_t __iomem *cp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344{
Alan Cox4ac43602006-06-28 04:26:52 -07002345 int cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346
2347 if (test_bit(ST_DOSIGS, &portp->state)) {
2348 if (test_bit(ST_DOFLUSHTX, &portp->state) &&
2349 test_bit(ST_DOFLUSHRX, &portp->state))
2350 cmd = A_SETSIGNALSF;
2351 else if (test_bit(ST_DOFLUSHTX, &portp->state))
2352 cmd = A_SETSIGNALSFTX;
2353 else if (test_bit(ST_DOFLUSHRX, &portp->state))
2354 cmd = A_SETSIGNALSFRX;
2355 else
2356 cmd = A_SETSIGNALS;
2357 clear_bit(ST_DOFLUSHTX, &portp->state);
2358 clear_bit(ST_DOFLUSHRX, &portp->state);
2359 clear_bit(ST_DOSIGS, &portp->state);
Alan Cox4ac43602006-06-28 04:26:52 -07002360 memcpy_toio((void __iomem *) &(cp->args[0]), (void *) &portp->asig,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 sizeof(asysigs_t));
Alan Cox4ac43602006-06-28 04:26:52 -07002362 writel(0, &cp->status);
2363 writel(cmd, &cp->cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 set_bit(ST_CMDING, &portp->state);
2365 } else if (test_bit(ST_DOFLUSHTX, &portp->state) ||
2366 test_bit(ST_DOFLUSHRX, &portp->state)) {
2367 cmd = ((test_bit(ST_DOFLUSHTX, &portp->state)) ? FLUSHTX : 0);
2368 cmd |= ((test_bit(ST_DOFLUSHRX, &portp->state)) ? FLUSHRX : 0);
2369 clear_bit(ST_DOFLUSHTX, &portp->state);
2370 clear_bit(ST_DOFLUSHRX, &portp->state);
Alan Cox4ac43602006-06-28 04:26:52 -07002371 memcpy_toio((void __iomem *) &(cp->args[0]), (void *) &cmd, sizeof(int));
2372 writel(0, &cp->status);
2373 writel(A_FLUSH, &cp->cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 set_bit(ST_CMDING, &portp->state);
2375 }
2376}
2377
2378/*****************************************************************************/
2379
2380/*
2381 * Host command service checking. This handles commands or messages
2382 * coming from the slave to the host. Must have board shared memory
2383 * enabled and interrupts off when called. Notice that by servicing the
2384 * read data last we don't need to change the shared memory pointer
2385 * during processing (which is a slow IO operation).
2386 * Return value indicates if this port is still awaiting actions from
2387 * the slave (like open, command, or even TX data being sent). If 0
2388 * then port is still busy, otherwise no longer busy.
2389 */
2390
2391static int stli_hostcmd(stlibrd_t *brdp, stliport_t *portp)
2392{
Alan Cox4ac43602006-06-28 04:26:52 -07002393 cdkasy_t __iomem *ap;
2394 cdkctrl_t __iomem *cp;
2395 struct tty_struct *tty;
2396 asynotify_t nt;
2397 unsigned long oldsigs;
2398 int rc, donerx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399
Alan Cox4ac43602006-06-28 04:26:52 -07002400 ap = (cdkasy_t __iomem *) EBRDGETMEMPTR(brdp, portp->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 cp = &ap->ctrl;
2402
2403/*
2404 * Check if we are waiting for an open completion message.
2405 */
2406 if (test_bit(ST_OPENING, &portp->state)) {
Alan Cox4ac43602006-06-28 04:26:52 -07002407 rc = readl(&cp->openarg);
2408 if (readb(&cp->open) == 0 && rc != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 if (rc > 0)
2410 rc--;
Alan Cox4ac43602006-06-28 04:26:52 -07002411 writel(0, &cp->openarg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 portp->rc = rc;
2413 clear_bit(ST_OPENING, &portp->state);
2414 wake_up_interruptible(&portp->raw_wait);
2415 }
2416 }
2417
2418/*
2419 * Check if we are waiting for a close completion message.
2420 */
2421 if (test_bit(ST_CLOSING, &portp->state)) {
Alan Cox4ac43602006-06-28 04:26:52 -07002422 rc = (int) readl(&cp->closearg);
2423 if (readb(&cp->close) == 0 && rc != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 if (rc > 0)
2425 rc--;
Alan Cox4ac43602006-06-28 04:26:52 -07002426 writel(0, &cp->closearg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 portp->rc = rc;
2428 clear_bit(ST_CLOSING, &portp->state);
2429 wake_up_interruptible(&portp->raw_wait);
2430 }
2431 }
2432
2433/*
2434 * Check if we are waiting for a command completion message. We may
2435 * need to copy out the command results associated with this command.
2436 */
2437 if (test_bit(ST_CMDING, &portp->state)) {
Alan Cox4ac43602006-06-28 04:26:52 -07002438 rc = readl(&cp->status);
2439 if (readl(&cp->cmd) == 0 && rc != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 if (rc > 0)
2441 rc--;
Alan Cox4ac43602006-06-28 04:26:52 -07002442 if (portp->argp != NULL) {
2443 memcpy_fromio(portp->argp, (void __iomem *) &(cp->args[0]),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 portp->argsize);
Alan Cox4ac43602006-06-28 04:26:52 -07002445 portp->argp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 }
Alan Cox4ac43602006-06-28 04:26:52 -07002447 writel(0, &cp->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 portp->rc = rc;
2449 clear_bit(ST_CMDING, &portp->state);
2450 stli_dodelaycmd(portp, cp);
2451 wake_up_interruptible(&portp->raw_wait);
2452 }
2453 }
2454
2455/*
2456 * Check for any notification messages ready. This includes lots of
2457 * different types of events - RX chars ready, RX break received,
2458 * TX data low or empty in the slave, modem signals changed state.
2459 */
2460 donerx = 0;
2461
2462 if (ap->notify) {
2463 nt = ap->changed;
2464 ap->notify = 0;
2465 tty = portp->tty;
2466
2467 if (nt.signal & SG_DCD) {
2468 oldsigs = portp->sigs;
2469 portp->sigs = stli_mktiocm(nt.sigvalue);
2470 clear_bit(ST_GETSIGS, &portp->state);
2471 if ((portp->sigs & TIOCM_CD) &&
2472 ((oldsigs & TIOCM_CD) == 0))
2473 wake_up_interruptible(&portp->open_wait);
2474 if ((oldsigs & TIOCM_CD) &&
2475 ((portp->sigs & TIOCM_CD) == 0)) {
2476 if (portp->flags & ASYNC_CHECK_CD) {
2477 if (tty)
2478 schedule_work(&portp->tqhangup);
2479 }
2480 }
2481 }
2482
2483 if (nt.data & DT_TXEMPTY)
2484 clear_bit(ST_TXBUSY, &portp->state);
2485 if (nt.data & (DT_TXEMPTY | DT_TXLOW)) {
Alan Cox4ac43602006-06-28 04:26:52 -07002486 if (tty != NULL) {
2487 tty_wakeup(tty);
2488 EBRDENABLE(brdp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 wake_up_interruptible(&tty->write_wait);
2490 }
2491 }
2492
2493 if ((nt.data & DT_RXBREAK) && (portp->rxmarkmsk & BRKINT)) {
Alan Cox4ac43602006-06-28 04:26:52 -07002494 if (tty != NULL) {
Alan Cox33f0f882006-01-09 20:54:13 -08002495 tty_insert_flip_char(tty, 0, TTY_BREAK);
2496 if (portp->flags & ASYNC_SAK) {
2497 do_SAK(tty);
2498 EBRDENABLE(brdp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 }
Alan Cox33f0f882006-01-09 20:54:13 -08002500 tty_schedule_flip(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 }
2502 }
2503
2504 if (nt.data & DT_RXBUSY) {
2505 donerx++;
2506 stli_read(brdp, portp);
2507 }
2508 }
2509
2510/*
2511 * It might seem odd that we are checking for more RX chars here.
2512 * But, we need to handle the case where the tty buffer was previously
2513 * filled, but we had more characters to pass up. The slave will not
2514 * send any more RX notify messages until the RX buffer has been emptied.
2515 * But it will leave the service bits on (since the buffer is not empty).
2516 * So from here we can try to process more RX chars.
2517 */
2518 if ((!donerx) && test_bit(ST_RXING, &portp->state)) {
2519 clear_bit(ST_RXING, &portp->state);
2520 stli_read(brdp, portp);
2521 }
2522
2523 return((test_bit(ST_OPENING, &portp->state) ||
2524 test_bit(ST_CLOSING, &portp->state) ||
2525 test_bit(ST_CMDING, &portp->state) ||
2526 test_bit(ST_TXBUSY, &portp->state) ||
2527 test_bit(ST_RXING, &portp->state)) ? 0 : 1);
2528}
2529
2530/*****************************************************************************/
2531
2532/*
2533 * Service all ports on a particular board. Assumes that the boards
2534 * shared memory is enabled, and that the page pointer is pointed
2535 * at the cdk header structure.
2536 */
2537
Alan Cox4ac43602006-06-28 04:26:52 -07002538static void stli_brdpoll(stlibrd_t *brdp, cdkhdr_t __iomem *hdrp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539{
Alan Cox4ac43602006-06-28 04:26:52 -07002540 stliport_t *portp;
2541 unsigned char hostbits[(STL_MAXCHANS / 8) + 1];
2542 unsigned char slavebits[(STL_MAXCHANS / 8) + 1];
2543 unsigned char __iomem *slavep;
2544 int bitpos, bitat, bitsize;
2545 int channr, nrdevs, slavebitchange;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546
2547 bitsize = brdp->bitsize;
2548 nrdevs = brdp->nrdevs;
2549
2550/*
2551 * Check if slave wants any service. Basically we try to do as
2552 * little work as possible here. There are 2 levels of service
2553 * bits. So if there is nothing to do we bail early. We check
2554 * 8 service bits at a time in the inner loop, so we can bypass
2555 * the lot if none of them want service.
2556 */
Alan Cox4ac43602006-06-28 04:26:52 -07002557 memcpy_fromio(&hostbits[0], (((unsigned char __iomem *) hdrp) + brdp->hostoffset),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558 bitsize);
2559
2560 memset(&slavebits[0], 0, bitsize);
2561 slavebitchange = 0;
2562
2563 for (bitpos = 0; (bitpos < bitsize); bitpos++) {
2564 if (hostbits[bitpos] == 0)
2565 continue;
2566 channr = bitpos * 8;
2567 for (bitat = 0x1; (channr < nrdevs); channr++, bitat <<= 1) {
2568 if (hostbits[bitpos] & bitat) {
2569 portp = brdp->ports[(channr - 1)];
2570 if (stli_hostcmd(brdp, portp)) {
2571 slavebitchange++;
2572 slavebits[bitpos] |= bitat;
2573 }
2574 }
2575 }
2576 }
2577
2578/*
2579 * If any of the ports are no longer busy then update them in the
2580 * slave request bits. We need to do this after, since a host port
2581 * service may initiate more slave requests.
2582 */
2583 if (slavebitchange) {
Alan Cox4ac43602006-06-28 04:26:52 -07002584 hdrp = (cdkhdr_t __iomem *) EBRDGETMEMPTR(brdp, CDK_CDKADDR);
2585 slavep = ((unsigned char __iomem *) hdrp) + brdp->slaveoffset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 for (bitpos = 0; (bitpos < bitsize); bitpos++) {
Alan Cox4ac43602006-06-28 04:26:52 -07002587 if (readb(slavebits + bitpos))
2588 writeb(readb(slavep + bitpos) & ~slavebits[bitpos], slavebits + bitpos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 }
2590 }
2591}
2592
2593/*****************************************************************************/
2594
2595/*
2596 * Driver poll routine. This routine polls the boards in use and passes
2597 * messages back up to host when necessary. This is actually very
2598 * CPU efficient, since we will always have the kernel poll clock, it
2599 * adds only a few cycles when idle (since board service can be
2600 * determined very easily), but when loaded generates no interrupts
2601 * (with their expensive associated context change).
2602 */
2603
2604static void stli_poll(unsigned long arg)
2605{
Alan Cox4ac43602006-06-28 04:26:52 -07002606 cdkhdr_t __iomem *hdrp;
2607 stlibrd_t *brdp;
2608 int brdnr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609
2610 stli_timerlist.expires = STLI_TIMEOUT;
2611 add_timer(&stli_timerlist);
2612
2613/*
2614 * Check each board and do any servicing required.
2615 */
2616 for (brdnr = 0; (brdnr < stli_nrbrds); brdnr++) {
2617 brdp = stli_brds[brdnr];
Alan Cox4ac43602006-06-28 04:26:52 -07002618 if (brdp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619 continue;
2620 if ((brdp->state & BST_STARTED) == 0)
2621 continue;
2622
Alan Cox4ac43602006-06-28 04:26:52 -07002623 spin_lock(&brd_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 EBRDENABLE(brdp);
Alan Cox4ac43602006-06-28 04:26:52 -07002625 hdrp = (cdkhdr_t __iomem *) EBRDGETMEMPTR(brdp, CDK_CDKADDR);
2626 if (readb(&hdrp->hostreq))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 stli_brdpoll(brdp, hdrp);
2628 EBRDDISABLE(brdp);
Alan Cox4ac43602006-06-28 04:26:52 -07002629 spin_unlock(&brd_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 }
2631}
2632
2633/*****************************************************************************/
2634
2635/*
2636 * Translate the termios settings into the port setting structure of
2637 * the slave.
2638 */
2639
Alan Cox606d0992006-12-08 02:38:45 -08002640static void stli_mkasyport(stliport_t *portp, asyport_t *pp, struct ktermios *tiosp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 memset(pp, 0, sizeof(asyport_t));
2643
2644/*
2645 * Start of by setting the baud, char size, parity and stop bit info.
2646 */
Alan Cox1db27c12006-09-29 02:01:38 -07002647 pp->baudout = tty_get_baud_rate(portp->tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 if ((tiosp->c_cflag & CBAUD) == B38400) {
2649 if ((portp->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
2650 pp->baudout = 57600;
2651 else if ((portp->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
2652 pp->baudout = 115200;
2653 else if ((portp->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
2654 pp->baudout = 230400;
2655 else if ((portp->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
2656 pp->baudout = 460800;
2657 else if ((portp->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST)
2658 pp->baudout = (portp->baud_base / portp->custom_divisor);
2659 }
2660 if (pp->baudout > STL_MAXBAUD)
2661 pp->baudout = STL_MAXBAUD;
2662 pp->baudin = pp->baudout;
2663
2664 switch (tiosp->c_cflag & CSIZE) {
2665 case CS5:
2666 pp->csize = 5;
2667 break;
2668 case CS6:
2669 pp->csize = 6;
2670 break;
2671 case CS7:
2672 pp->csize = 7;
2673 break;
2674 default:
2675 pp->csize = 8;
2676 break;
2677 }
2678
2679 if (tiosp->c_cflag & CSTOPB)
2680 pp->stopbs = PT_STOP2;
2681 else
2682 pp->stopbs = PT_STOP1;
2683
2684 if (tiosp->c_cflag & PARENB) {
2685 if (tiosp->c_cflag & PARODD)
2686 pp->parity = PT_ODDPARITY;
2687 else
2688 pp->parity = PT_EVENPARITY;
2689 } else {
2690 pp->parity = PT_NOPARITY;
2691 }
2692
2693/*
2694 * Set up any flow control options enabled.
2695 */
2696 if (tiosp->c_iflag & IXON) {
2697 pp->flow |= F_IXON;
2698 if (tiosp->c_iflag & IXANY)
2699 pp->flow |= F_IXANY;
2700 }
2701 if (tiosp->c_cflag & CRTSCTS)
2702 pp->flow |= (F_RTSFLOW | F_CTSFLOW);
2703
2704 pp->startin = tiosp->c_cc[VSTART];
2705 pp->stopin = tiosp->c_cc[VSTOP];
2706 pp->startout = tiosp->c_cc[VSTART];
2707 pp->stopout = tiosp->c_cc[VSTOP];
2708
2709/*
2710 * Set up the RX char marking mask with those RX error types we must
2711 * catch. We can get the slave to help us out a little here, it will
2712 * ignore parity errors and breaks for us, and mark parity errors in
2713 * the data stream.
2714 */
2715 if (tiosp->c_iflag & IGNPAR)
2716 pp->iflag |= FI_IGNRXERRS;
2717 if (tiosp->c_iflag & IGNBRK)
2718 pp->iflag |= FI_IGNBREAK;
2719
2720 portp->rxmarkmsk = 0;
2721 if (tiosp->c_iflag & (INPCK | PARMRK))
2722 pp->iflag |= FI_1MARKRXERRS;
2723 if (tiosp->c_iflag & BRKINT)
2724 portp->rxmarkmsk |= BRKINT;
2725
2726/*
2727 * Set up clocal processing as required.
2728 */
2729 if (tiosp->c_cflag & CLOCAL)
2730 portp->flags &= ~ASYNC_CHECK_CD;
2731 else
2732 portp->flags |= ASYNC_CHECK_CD;
2733
2734/*
2735 * Transfer any persistent flags into the asyport structure.
2736 */
2737 pp->pflag = (portp->pflag & 0xffff);
2738 pp->vmin = (portp->pflag & P_RXIMIN) ? 1 : 0;
2739 pp->vtime = (portp->pflag & P_RXITIME) ? 1 : 0;
2740 pp->cc[1] = (portp->pflag & P_RXTHOLD) ? 1 : 0;
2741}
2742
2743/*****************************************************************************/
2744
2745/*
2746 * Construct a slave signals structure for setting the DTR and RTS
2747 * signals as specified.
2748 */
2749
2750static void stli_mkasysigs(asysigs_t *sp, int dtr, int rts)
2751{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 memset(sp, 0, sizeof(asysigs_t));
2753 if (dtr >= 0) {
2754 sp->signal |= SG_DTR;
2755 sp->sigvalue |= ((dtr > 0) ? SG_DTR : 0);
2756 }
2757 if (rts >= 0) {
2758 sp->signal |= SG_RTS;
2759 sp->sigvalue |= ((rts > 0) ? SG_RTS : 0);
2760 }
2761}
2762
2763/*****************************************************************************/
2764
2765/*
2766 * Convert the signals returned from the slave into a local TIOCM type
2767 * signals value. We keep them locally in TIOCM format.
2768 */
2769
2770static long stli_mktiocm(unsigned long sigvalue)
2771{
Alan Cox4ac43602006-06-28 04:26:52 -07002772 long tiocm = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 tiocm |= ((sigvalue & SG_DCD) ? TIOCM_CD : 0);
2774 tiocm |= ((sigvalue & SG_CTS) ? TIOCM_CTS : 0);
2775 tiocm |= ((sigvalue & SG_RI) ? TIOCM_RI : 0);
2776 tiocm |= ((sigvalue & SG_DSR) ? TIOCM_DSR : 0);
2777 tiocm |= ((sigvalue & SG_DTR) ? TIOCM_DTR : 0);
2778 tiocm |= ((sigvalue & SG_RTS) ? TIOCM_RTS : 0);
2779 return(tiocm);
2780}
2781
2782/*****************************************************************************/
2783
2784/*
2785 * All panels and ports actually attached have been worked out. All
2786 * we need to do here is set up the appropriate per port data structures.
2787 */
2788
2789static int stli_initports(stlibrd_t *brdp)
2790{
2791 stliport_t *portp;
2792 int i, panelnr, panelport;
2793
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 for (i = 0, panelnr = 0, panelport = 0; (i < brdp->nrports); i++) {
Tobias Klauserb0b4ed72006-03-31 02:30:56 -08002795 portp = kzalloc(sizeof(stliport_t), GFP_KERNEL);
2796 if (!portp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 printk("STALLION: failed to allocate port structure\n");
2798 continue;
2799 }
2800
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801 portp->magic = STLI_PORTMAGIC;
2802 portp->portnr = i;
2803 portp->brdnr = brdp->brdnr;
2804 portp->panelnr = panelnr;
2805 portp->baud_base = STL_BAUDBASE;
2806 portp->close_delay = STL_CLOSEDELAY;
2807 portp->closing_wait = 30 * HZ;
Al Viro3e577a82006-12-06 18:41:45 +00002808 INIT_WORK(&portp->tqhangup, stli_dohangup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809 init_waitqueue_head(&portp->open_wait);
2810 init_waitqueue_head(&portp->close_wait);
2811 init_waitqueue_head(&portp->raw_wait);
2812 panelport++;
2813 if (panelport >= brdp->panels[panelnr]) {
2814 panelport = 0;
2815 panelnr++;
2816 }
2817 brdp->ports[i] = portp;
2818 }
2819
Alan Cox4ac43602006-06-28 04:26:52 -07002820 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821}
2822
2823/*****************************************************************************/
2824
2825/*
2826 * All the following routines are board specific hardware operations.
2827 */
2828
2829static void stli_ecpinit(stlibrd_t *brdp)
2830{
2831 unsigned long memconf;
2832
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 outb(ECP_ATSTOP, (brdp->iobase + ECP_ATCONFR));
2834 udelay(10);
2835 outb(ECP_ATDISABLE, (brdp->iobase + ECP_ATCONFR));
2836 udelay(100);
2837
2838 memconf = (brdp->memaddr & ECP_ATADDRMASK) >> ECP_ATADDRSHFT;
2839 outb(memconf, (brdp->iobase + ECP_ATMEMAR));
2840}
2841
2842/*****************************************************************************/
2843
2844static void stli_ecpenable(stlibrd_t *brdp)
2845{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 outb(ECP_ATENABLE, (brdp->iobase + ECP_ATCONFR));
2847}
2848
2849/*****************************************************************************/
2850
2851static void stli_ecpdisable(stlibrd_t *brdp)
2852{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 outb(ECP_ATDISABLE, (brdp->iobase + ECP_ATCONFR));
2854}
2855
2856/*****************************************************************************/
2857
Al Viro29756fa2006-10-10 22:47:27 +01002858static void __iomem *stli_ecpgetmemptr(stlibrd_t *brdp, unsigned long offset, int line)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859{
Al Viro29756fa2006-10-10 22:47:27 +01002860 void __iomem *ptr;
Alan Cox4ac43602006-06-28 04:26:52 -07002861 unsigned char val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862
2863 if (offset > brdp->memsize) {
2864 printk(KERN_ERR "STALLION: shared memory pointer=%x out of "
2865 "range at line=%d(%d), brd=%d\n",
2866 (int) offset, line, __LINE__, brdp->brdnr);
2867 ptr = NULL;
2868 val = 0;
2869 } else {
2870 ptr = brdp->membase + (offset % ECP_ATPAGESIZE);
2871 val = (unsigned char) (offset / ECP_ATPAGESIZE);
2872 }
2873 outb(val, (brdp->iobase + ECP_ATMEMPR));
2874 return(ptr);
2875}
2876
2877/*****************************************************************************/
2878
2879static void stli_ecpreset(stlibrd_t *brdp)
2880{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881 outb(ECP_ATSTOP, (brdp->iobase + ECP_ATCONFR));
2882 udelay(10);
2883 outb(ECP_ATDISABLE, (brdp->iobase + ECP_ATCONFR));
2884 udelay(500);
2885}
2886
2887/*****************************************************************************/
2888
2889static void stli_ecpintr(stlibrd_t *brdp)
2890{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 outb(0x1, brdp->iobase);
2892}
2893
2894/*****************************************************************************/
2895
2896/*
2897 * The following set of functions act on ECP EISA boards.
2898 */
2899
2900static void stli_ecpeiinit(stlibrd_t *brdp)
2901{
2902 unsigned long memconf;
2903
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904 outb(0x1, (brdp->iobase + ECP_EIBRDENAB));
2905 outb(ECP_EISTOP, (brdp->iobase + ECP_EICONFR));
2906 udelay(10);
2907 outb(ECP_EIDISABLE, (brdp->iobase + ECP_EICONFR));
2908 udelay(500);
2909
2910 memconf = (brdp->memaddr & ECP_EIADDRMASKL) >> ECP_EIADDRSHFTL;
2911 outb(memconf, (brdp->iobase + ECP_EIMEMARL));
2912 memconf = (brdp->memaddr & ECP_EIADDRMASKH) >> ECP_EIADDRSHFTH;
2913 outb(memconf, (brdp->iobase + ECP_EIMEMARH));
2914}
2915
2916/*****************************************************************************/
2917
2918static void stli_ecpeienable(stlibrd_t *brdp)
2919{
2920 outb(ECP_EIENABLE, (brdp->iobase + ECP_EICONFR));
2921}
2922
2923/*****************************************************************************/
2924
2925static void stli_ecpeidisable(stlibrd_t *brdp)
2926{
2927 outb(ECP_EIDISABLE, (brdp->iobase + ECP_EICONFR));
2928}
2929
2930/*****************************************************************************/
2931
Al Viro29756fa2006-10-10 22:47:27 +01002932static void __iomem *stli_ecpeigetmemptr(stlibrd_t *brdp, unsigned long offset, int line)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933{
Al Viro29756fa2006-10-10 22:47:27 +01002934 void __iomem *ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935 unsigned char val;
2936
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937 if (offset > brdp->memsize) {
2938 printk(KERN_ERR "STALLION: shared memory pointer=%x out of "
2939 "range at line=%d(%d), brd=%d\n",
2940 (int) offset, line, __LINE__, brdp->brdnr);
2941 ptr = NULL;
2942 val = 0;
2943 } else {
2944 ptr = brdp->membase + (offset % ECP_EIPAGESIZE);
2945 if (offset < ECP_EIPAGESIZE)
2946 val = ECP_EIENABLE;
2947 else
2948 val = ECP_EIENABLE | 0x40;
2949 }
2950 outb(val, (brdp->iobase + ECP_EICONFR));
2951 return(ptr);
2952}
2953
2954/*****************************************************************************/
2955
2956static void stli_ecpeireset(stlibrd_t *brdp)
2957{
2958 outb(ECP_EISTOP, (brdp->iobase + ECP_EICONFR));
2959 udelay(10);
2960 outb(ECP_EIDISABLE, (brdp->iobase + ECP_EICONFR));
2961 udelay(500);
2962}
2963
2964/*****************************************************************************/
2965
2966/*
2967 * The following set of functions act on ECP MCA boards.
2968 */
2969
2970static void stli_ecpmcenable(stlibrd_t *brdp)
2971{
2972 outb(ECP_MCENABLE, (brdp->iobase + ECP_MCCONFR));
2973}
2974
2975/*****************************************************************************/
2976
2977static void stli_ecpmcdisable(stlibrd_t *brdp)
2978{
2979 outb(ECP_MCDISABLE, (brdp->iobase + ECP_MCCONFR));
2980}
2981
2982/*****************************************************************************/
2983
Al Viro29756fa2006-10-10 22:47:27 +01002984static void __iomem *stli_ecpmcgetmemptr(stlibrd_t *brdp, unsigned long offset, int line)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985{
Al Viro29756fa2006-10-10 22:47:27 +01002986 void __iomem *ptr;
Alan Cox4ac43602006-06-28 04:26:52 -07002987 unsigned char val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988
2989 if (offset > brdp->memsize) {
2990 printk(KERN_ERR "STALLION: shared memory pointer=%x out of "
2991 "range at line=%d(%d), brd=%d\n",
2992 (int) offset, line, __LINE__, brdp->brdnr);
2993 ptr = NULL;
2994 val = 0;
2995 } else {
2996 ptr = brdp->membase + (offset % ECP_MCPAGESIZE);
2997 val = ((unsigned char) (offset / ECP_MCPAGESIZE)) | ECP_MCENABLE;
2998 }
2999 outb(val, (brdp->iobase + ECP_MCCONFR));
3000 return(ptr);
3001}
3002
3003/*****************************************************************************/
3004
3005static void stli_ecpmcreset(stlibrd_t *brdp)
3006{
3007 outb(ECP_MCSTOP, (brdp->iobase + ECP_MCCONFR));
3008 udelay(10);
3009 outb(ECP_MCDISABLE, (brdp->iobase + ECP_MCCONFR));
3010 udelay(500);
3011}
3012
3013/*****************************************************************************/
3014
3015/*
3016 * The following set of functions act on ECP PCI boards.
3017 */
3018
3019static void stli_ecppciinit(stlibrd_t *brdp)
3020{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021 outb(ECP_PCISTOP, (brdp->iobase + ECP_PCICONFR));
3022 udelay(10);
3023 outb(0, (brdp->iobase + ECP_PCICONFR));
3024 udelay(500);
3025}
3026
3027/*****************************************************************************/
3028
Al Viro29756fa2006-10-10 22:47:27 +01003029static void __iomem *stli_ecppcigetmemptr(stlibrd_t *brdp, unsigned long offset, int line)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030{
Al Viro29756fa2006-10-10 22:47:27 +01003031 void __iomem *ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032 unsigned char val;
3033
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 if (offset > brdp->memsize) {
3035 printk(KERN_ERR "STALLION: shared memory pointer=%x out of "
3036 "range at line=%d(%d), board=%d\n",
3037 (int) offset, line, __LINE__, brdp->brdnr);
3038 ptr = NULL;
3039 val = 0;
3040 } else {
3041 ptr = brdp->membase + (offset % ECP_PCIPAGESIZE);
3042 val = (offset / ECP_PCIPAGESIZE) << 1;
3043 }
3044 outb(val, (brdp->iobase + ECP_PCICONFR));
3045 return(ptr);
3046}
3047
3048/*****************************************************************************/
3049
3050static void stli_ecppcireset(stlibrd_t *brdp)
3051{
3052 outb(ECP_PCISTOP, (brdp->iobase + ECP_PCICONFR));
3053 udelay(10);
3054 outb(0, (brdp->iobase + ECP_PCICONFR));
3055 udelay(500);
3056}
3057
3058/*****************************************************************************/
3059
3060/*
3061 * The following routines act on ONboards.
3062 */
3063
3064static void stli_onbinit(stlibrd_t *brdp)
3065{
3066 unsigned long memconf;
3067
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068 outb(ONB_ATSTOP, (brdp->iobase + ONB_ATCONFR));
3069 udelay(10);
3070 outb(ONB_ATDISABLE, (brdp->iobase + ONB_ATCONFR));
3071 mdelay(1000);
3072
3073 memconf = (brdp->memaddr & ONB_ATADDRMASK) >> ONB_ATADDRSHFT;
3074 outb(memconf, (brdp->iobase + ONB_ATMEMAR));
3075 outb(0x1, brdp->iobase);
3076 mdelay(1);
3077}
3078
3079/*****************************************************************************/
3080
3081static void stli_onbenable(stlibrd_t *brdp)
3082{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083 outb((brdp->enabval | ONB_ATENABLE), (brdp->iobase + ONB_ATCONFR));
3084}
3085
3086/*****************************************************************************/
3087
3088static void stli_onbdisable(stlibrd_t *brdp)
3089{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090 outb((brdp->enabval | ONB_ATDISABLE), (brdp->iobase + ONB_ATCONFR));
3091}
3092
3093/*****************************************************************************/
3094
Al Viro29756fa2006-10-10 22:47:27 +01003095static void __iomem *stli_onbgetmemptr(stlibrd_t *brdp, unsigned long offset, int line)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096{
Al Viro29756fa2006-10-10 22:47:27 +01003097 void __iomem *ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099 if (offset > brdp->memsize) {
3100 printk(KERN_ERR "STALLION: shared memory pointer=%x out of "
3101 "range at line=%d(%d), brd=%d\n",
3102 (int) offset, line, __LINE__, brdp->brdnr);
3103 ptr = NULL;
3104 } else {
3105 ptr = brdp->membase + (offset % ONB_ATPAGESIZE);
3106 }
3107 return(ptr);
3108}
3109
3110/*****************************************************************************/
3111
3112static void stli_onbreset(stlibrd_t *brdp)
3113{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114 outb(ONB_ATSTOP, (brdp->iobase + ONB_ATCONFR));
3115 udelay(10);
3116 outb(ONB_ATDISABLE, (brdp->iobase + ONB_ATCONFR));
3117 mdelay(1000);
3118}
3119
3120/*****************************************************************************/
3121
3122/*
3123 * The following routines act on ONboard EISA.
3124 */
3125
3126static void stli_onbeinit(stlibrd_t *brdp)
3127{
3128 unsigned long memconf;
3129
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130 outb(0x1, (brdp->iobase + ONB_EIBRDENAB));
3131 outb(ONB_EISTOP, (brdp->iobase + ONB_EICONFR));
3132 udelay(10);
3133 outb(ONB_EIDISABLE, (brdp->iobase + ONB_EICONFR));
3134 mdelay(1000);
3135
3136 memconf = (brdp->memaddr & ONB_EIADDRMASKL) >> ONB_EIADDRSHFTL;
3137 outb(memconf, (brdp->iobase + ONB_EIMEMARL));
3138 memconf = (brdp->memaddr & ONB_EIADDRMASKH) >> ONB_EIADDRSHFTH;
3139 outb(memconf, (brdp->iobase + ONB_EIMEMARH));
3140 outb(0x1, brdp->iobase);
3141 mdelay(1);
3142}
3143
3144/*****************************************************************************/
3145
3146static void stli_onbeenable(stlibrd_t *brdp)
3147{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148 outb(ONB_EIENABLE, (brdp->iobase + ONB_EICONFR));
3149}
3150
3151/*****************************************************************************/
3152
3153static void stli_onbedisable(stlibrd_t *brdp)
3154{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155 outb(ONB_EIDISABLE, (brdp->iobase + ONB_EICONFR));
3156}
3157
3158/*****************************************************************************/
3159
Al Viro29756fa2006-10-10 22:47:27 +01003160static void __iomem *stli_onbegetmemptr(stlibrd_t *brdp, unsigned long offset, int line)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161{
Al Viro29756fa2006-10-10 22:47:27 +01003162 void __iomem *ptr;
Alan Cox4ac43602006-06-28 04:26:52 -07003163 unsigned char val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164
3165 if (offset > brdp->memsize) {
3166 printk(KERN_ERR "STALLION: shared memory pointer=%x out of "
3167 "range at line=%d(%d), brd=%d\n",
3168 (int) offset, line, __LINE__, brdp->brdnr);
3169 ptr = NULL;
3170 val = 0;
3171 } else {
3172 ptr = brdp->membase + (offset % ONB_EIPAGESIZE);
3173 if (offset < ONB_EIPAGESIZE)
3174 val = ONB_EIENABLE;
3175 else
3176 val = ONB_EIENABLE | 0x40;
3177 }
3178 outb(val, (brdp->iobase + ONB_EICONFR));
3179 return(ptr);
3180}
3181
3182/*****************************************************************************/
3183
3184static void stli_onbereset(stlibrd_t *brdp)
3185{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186 outb(ONB_EISTOP, (brdp->iobase + ONB_EICONFR));
3187 udelay(10);
3188 outb(ONB_EIDISABLE, (brdp->iobase + ONB_EICONFR));
3189 mdelay(1000);
3190}
3191
3192/*****************************************************************************/
3193
3194/*
3195 * The following routines act on Brumby boards.
3196 */
3197
3198static void stli_bbyinit(stlibrd_t *brdp)
3199{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200 outb(BBY_ATSTOP, (brdp->iobase + BBY_ATCONFR));
3201 udelay(10);
3202 outb(0, (brdp->iobase + BBY_ATCONFR));
3203 mdelay(1000);
3204 outb(0x1, brdp->iobase);
3205 mdelay(1);
3206}
3207
3208/*****************************************************************************/
3209
Al Viro29756fa2006-10-10 22:47:27 +01003210static void __iomem *stli_bbygetmemptr(stlibrd_t *brdp, unsigned long offset, int line)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211{
Al Viro29756fa2006-10-10 22:47:27 +01003212 void __iomem *ptr;
Alan Cox4ac43602006-06-28 04:26:52 -07003213 unsigned char val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214
Alan Cox4ac43602006-06-28 04:26:52 -07003215 BUG_ON(offset > brdp->memsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216
Alan Cox4ac43602006-06-28 04:26:52 -07003217 ptr = brdp->membase + (offset % BBY_PAGESIZE);
3218 val = (unsigned char) (offset / BBY_PAGESIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219 outb(val, (brdp->iobase + BBY_ATCONFR));
3220 return(ptr);
3221}
3222
3223/*****************************************************************************/
3224
3225static void stli_bbyreset(stlibrd_t *brdp)
3226{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227 outb(BBY_ATSTOP, (brdp->iobase + BBY_ATCONFR));
3228 udelay(10);
3229 outb(0, (brdp->iobase + BBY_ATCONFR));
3230 mdelay(1000);
3231}
3232
3233/*****************************************************************************/
3234
3235/*
3236 * The following routines act on original old Stallion boards.
3237 */
3238
3239static void stli_stalinit(stlibrd_t *brdp)
3240{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241 outb(0x1, brdp->iobase);
3242 mdelay(1000);
3243}
3244
3245/*****************************************************************************/
3246
Al Viro29756fa2006-10-10 22:47:27 +01003247static void __iomem *stli_stalgetmemptr(stlibrd_t *brdp, unsigned long offset, int line)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248{
Alan Cox4ac43602006-06-28 04:26:52 -07003249 BUG_ON(offset > brdp->memsize);
3250 return brdp->membase + (offset % STAL_PAGESIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251}
3252
3253/*****************************************************************************/
3254
3255static void stli_stalreset(stlibrd_t *brdp)
3256{
Alan Cox4ac43602006-06-28 04:26:52 -07003257 u32 __iomem *vecp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258
Alan Cox4ac43602006-06-28 04:26:52 -07003259 vecp = (u32 __iomem *) (brdp->membase + 0x30);
3260 writel(0xffff0000, vecp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261 outb(0, brdp->iobase);
3262 mdelay(1000);
3263}
3264
3265/*****************************************************************************/
3266
3267/*
3268 * Try to find an ECP board and initialize it. This handles only ECP
3269 * board types.
3270 */
3271
3272static int stli_initecp(stlibrd_t *brdp)
3273{
Alan Cox4ac43602006-06-28 04:26:52 -07003274 cdkecpsig_t sig;
3275 cdkecpsig_t __iomem *sigsp;
3276 unsigned int status, nxtid;
3277 char *name;
3278 int panelnr, nrports;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279
3280 if (!request_region(brdp->iobase, brdp->iosize, "istallion"))
3281 return -EIO;
3282
3283 if ((brdp->iobase == 0) || (brdp->memaddr == 0))
3284 {
3285 release_region(brdp->iobase, brdp->iosize);
Alan Cox4ac43602006-06-28 04:26:52 -07003286 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287 }
3288
3289 brdp->iosize = ECP_IOSIZE;
3290
3291/*
3292 * Based on the specific board type setup the common vars to access
3293 * and enable shared memory. Set all board specific information now
3294 * as well.
3295 */
3296 switch (brdp->brdtype) {
3297 case BRD_ECP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298 brdp->memsize = ECP_MEMSIZE;
3299 brdp->pagesize = ECP_ATPAGESIZE;
3300 brdp->init = stli_ecpinit;
3301 brdp->enable = stli_ecpenable;
3302 brdp->reenable = stli_ecpenable;
3303 brdp->disable = stli_ecpdisable;
3304 brdp->getmemptr = stli_ecpgetmemptr;
3305 brdp->intr = stli_ecpintr;
3306 brdp->reset = stli_ecpreset;
3307 name = "serial(EC8/64)";
3308 break;
3309
3310 case BRD_ECPE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311 brdp->memsize = ECP_MEMSIZE;
3312 brdp->pagesize = ECP_EIPAGESIZE;
3313 brdp->init = stli_ecpeiinit;
3314 brdp->enable = stli_ecpeienable;
3315 brdp->reenable = stli_ecpeienable;
3316 brdp->disable = stli_ecpeidisable;
3317 brdp->getmemptr = stli_ecpeigetmemptr;
3318 brdp->intr = stli_ecpintr;
3319 brdp->reset = stli_ecpeireset;
3320 name = "serial(EC8/64-EI)";
3321 break;
3322
3323 case BRD_ECPMC:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324 brdp->memsize = ECP_MEMSIZE;
3325 brdp->pagesize = ECP_MCPAGESIZE;
3326 brdp->init = NULL;
3327 brdp->enable = stli_ecpmcenable;
3328 brdp->reenable = stli_ecpmcenable;
3329 brdp->disable = stli_ecpmcdisable;
3330 brdp->getmemptr = stli_ecpmcgetmemptr;
3331 brdp->intr = stli_ecpintr;
3332 brdp->reset = stli_ecpmcreset;
3333 name = "serial(EC8/64-MCA)";
3334 break;
3335
3336 case BRD_ECPPCI:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337 brdp->memsize = ECP_PCIMEMSIZE;
3338 brdp->pagesize = ECP_PCIPAGESIZE;
3339 brdp->init = stli_ecppciinit;
3340 brdp->enable = NULL;
3341 brdp->reenable = NULL;
3342 brdp->disable = NULL;
3343 brdp->getmemptr = stli_ecppcigetmemptr;
3344 brdp->intr = stli_ecpintr;
3345 brdp->reset = stli_ecppcireset;
3346 name = "serial(EC/RA-PCI)";
3347 break;
3348
3349 default:
3350 release_region(brdp->iobase, brdp->iosize);
Alan Cox4ac43602006-06-28 04:26:52 -07003351 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352 }
3353
3354/*
3355 * The per-board operations structure is all set up, so now let's go
3356 * and get the board operational. Firstly initialize board configuration
3357 * registers. Set the memory mapping info so we can get at the boards
3358 * shared memory.
3359 */
3360 EBRDINIT(brdp);
3361
3362 brdp->membase = ioremap(brdp->memaddr, brdp->memsize);
Alan Cox4ac43602006-06-28 04:26:52 -07003363 if (brdp->membase == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364 {
3365 release_region(brdp->iobase, brdp->iosize);
Alan Cox4ac43602006-06-28 04:26:52 -07003366 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367 }
3368
3369/*
3370 * Now that all specific code is set up, enable the shared memory and
3371 * look for the a signature area that will tell us exactly what board
3372 * this is, and what it is connected to it.
3373 */
3374 EBRDENABLE(brdp);
Alan Cox4ac43602006-06-28 04:26:52 -07003375 sigsp = (cdkecpsig_t __iomem *) EBRDGETMEMPTR(brdp, CDK_SIGADDR);
Al Viro634965f2006-09-23 01:20:31 +01003376 memcpy_fromio(&sig, sigsp, sizeof(cdkecpsig_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377 EBRDDISABLE(brdp);
3378
Alan Cox4ac43602006-06-28 04:26:52 -07003379 if (sig.magic != cpu_to_le32(ECP_MAGIC))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380 {
3381 release_region(brdp->iobase, brdp->iosize);
Amol Ladaa8a8d62006-12-06 20:35:22 -08003382 iounmap(brdp->membase);
3383 brdp->membase = NULL;
Alan Cox4ac43602006-06-28 04:26:52 -07003384 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385 }
3386
3387/*
3388 * Scan through the signature looking at the panels connected to the
3389 * board. Calculate the total number of ports as we go.
3390 */
3391 for (panelnr = 0, nxtid = 0; (panelnr < STL_MAXPANELS); panelnr++) {
3392 status = sig.panelid[nxtid];
3393 if ((status & ECH_PNLIDMASK) != nxtid)
3394 break;
3395
3396 brdp->panelids[panelnr] = status;
3397 nrports = (status & ECH_PNL16PORT) ? 16 : 8;
3398 if ((nrports == 16) && ((status & ECH_PNLXPID) == 0))
3399 nxtid++;
3400 brdp->panels[panelnr] = nrports;
3401 brdp->nrports += nrports;
3402 nxtid++;
3403 brdp->nrpanels++;
3404 }
3405
3406
3407 brdp->state |= BST_FOUND;
Alan Cox4ac43602006-06-28 04:26:52 -07003408 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409}
3410
3411/*****************************************************************************/
3412
3413/*
3414 * Try to find an ONboard, Brumby or Stallion board and initialize it.
3415 * This handles only these board types.
3416 */
3417
3418static int stli_initonb(stlibrd_t *brdp)
3419{
Alan Cox4ac43602006-06-28 04:26:52 -07003420 cdkonbsig_t sig;
3421 cdkonbsig_t __iomem *sigsp;
3422 char *name;
3423 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424
3425/*
3426 * Do a basic sanity check on the IO and memory addresses.
3427 */
Alan Cox4ac43602006-06-28 04:26:52 -07003428 if (brdp->iobase == 0 || brdp->memaddr == 0)
3429 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430
3431 brdp->iosize = ONB_IOSIZE;
3432
3433 if (!request_region(brdp->iobase, brdp->iosize, "istallion"))
3434 return -EIO;
3435
3436/*
3437 * Based on the specific board type setup the common vars to access
3438 * and enable shared memory. Set all board specific information now
3439 * as well.
3440 */
3441 switch (brdp->brdtype) {
3442 case BRD_ONBOARD:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443 case BRD_ONBOARD2:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444 brdp->memsize = ONB_MEMSIZE;
3445 brdp->pagesize = ONB_ATPAGESIZE;
3446 brdp->init = stli_onbinit;
3447 brdp->enable = stli_onbenable;
3448 brdp->reenable = stli_onbenable;
3449 brdp->disable = stli_onbdisable;
3450 brdp->getmemptr = stli_onbgetmemptr;
3451 brdp->intr = stli_ecpintr;
3452 brdp->reset = stli_onbreset;
3453 if (brdp->memaddr > 0x100000)
3454 brdp->enabval = ONB_MEMENABHI;
3455 else
3456 brdp->enabval = ONB_MEMENABLO;
3457 name = "serial(ONBoard)";
3458 break;
3459
3460 case BRD_ONBOARDE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461 brdp->memsize = ONB_EIMEMSIZE;
3462 brdp->pagesize = ONB_EIPAGESIZE;
3463 brdp->init = stli_onbeinit;
3464 brdp->enable = stli_onbeenable;
3465 brdp->reenable = stli_onbeenable;
3466 brdp->disable = stli_onbedisable;
3467 brdp->getmemptr = stli_onbegetmemptr;
3468 brdp->intr = stli_ecpintr;
3469 brdp->reset = stli_onbereset;
3470 name = "serial(ONBoard/E)";
3471 break;
3472
3473 case BRD_BRUMBY4:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474 brdp->memsize = BBY_MEMSIZE;
3475 brdp->pagesize = BBY_PAGESIZE;
3476 brdp->init = stli_bbyinit;
3477 brdp->enable = NULL;
3478 brdp->reenable = NULL;
3479 brdp->disable = NULL;
3480 brdp->getmemptr = stli_bbygetmemptr;
3481 brdp->intr = stli_ecpintr;
3482 brdp->reset = stli_bbyreset;
3483 name = "serial(Brumby)";
3484 break;
3485
3486 case BRD_STALLION:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487 brdp->memsize = STAL_MEMSIZE;
3488 brdp->pagesize = STAL_PAGESIZE;
3489 brdp->init = stli_stalinit;
3490 brdp->enable = NULL;
3491 brdp->reenable = NULL;
3492 brdp->disable = NULL;
3493 brdp->getmemptr = stli_stalgetmemptr;
3494 brdp->intr = stli_ecpintr;
3495 brdp->reset = stli_stalreset;
3496 name = "serial(Stallion)";
3497 break;
3498
3499 default:
3500 release_region(brdp->iobase, brdp->iosize);
Alan Cox4ac43602006-06-28 04:26:52 -07003501 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502 }
3503
3504/*
3505 * The per-board operations structure is all set up, so now let's go
3506 * and get the board operational. Firstly initialize board configuration
3507 * registers. Set the memory mapping info so we can get at the boards
3508 * shared memory.
3509 */
3510 EBRDINIT(brdp);
3511
3512 brdp->membase = ioremap(brdp->memaddr, brdp->memsize);
Alan Cox4ac43602006-06-28 04:26:52 -07003513 if (brdp->membase == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514 {
3515 release_region(brdp->iobase, brdp->iosize);
Alan Cox4ac43602006-06-28 04:26:52 -07003516 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003517 }
3518
3519/*
3520 * Now that all specific code is set up, enable the shared memory and
3521 * look for the a signature area that will tell us exactly what board
3522 * this is, and how many ports.
3523 */
3524 EBRDENABLE(brdp);
Alan Cox4ac43602006-06-28 04:26:52 -07003525 sigsp = (cdkonbsig_t __iomem *) EBRDGETMEMPTR(brdp, CDK_SIGADDR);
3526 memcpy_fromio(&sig, sigsp, sizeof(cdkonbsig_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003527 EBRDDISABLE(brdp);
3528
Alan Cox4ac43602006-06-28 04:26:52 -07003529 if (sig.magic0 != cpu_to_le16(ONB_MAGIC0) ||
3530 sig.magic1 != cpu_to_le16(ONB_MAGIC1) ||
3531 sig.magic2 != cpu_to_le16(ONB_MAGIC2) ||
3532 sig.magic3 != cpu_to_le16(ONB_MAGIC3))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533 {
3534 release_region(brdp->iobase, brdp->iosize);
Amol Ladaa8a8d62006-12-06 20:35:22 -08003535 iounmap(brdp->membase);
3536 brdp->membase = NULL;
Alan Cox4ac43602006-06-28 04:26:52 -07003537 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538 }
3539
3540/*
3541 * Scan through the signature alive mask and calculate how many ports
3542 * there are on this board.
3543 */
3544 brdp->nrpanels = 1;
3545 if (sig.amask1) {
3546 brdp->nrports = 32;
3547 } else {
3548 for (i = 0; (i < 16); i++) {
3549 if (((sig.amask0 << i) & 0x8000) == 0)
3550 break;
3551 }
3552 brdp->nrports = i;
3553 }
3554 brdp->panels[0] = brdp->nrports;
3555
3556
3557 brdp->state |= BST_FOUND;
Alan Cox4ac43602006-06-28 04:26:52 -07003558 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559}
3560
3561/*****************************************************************************/
3562
3563/*
3564 * Start up a running board. This routine is only called after the
3565 * code has been down loaded to the board and is operational. It will
3566 * read in the memory map, and get the show on the road...
3567 */
3568
3569static int stli_startbrd(stlibrd_t *brdp)
3570{
Alan Cox4ac43602006-06-28 04:26:52 -07003571 cdkhdr_t __iomem *hdrp;
3572 cdkmem_t __iomem *memp;
3573 cdkasy_t __iomem *ap;
3574 unsigned long flags;
3575 stliport_t *portp;
3576 int portnr, nrdevs, i, rc = 0;
3577 u32 memoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578
Alan Cox4ac43602006-06-28 04:26:52 -07003579 spin_lock_irqsave(&brd_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580 EBRDENABLE(brdp);
Alan Cox4ac43602006-06-28 04:26:52 -07003581 hdrp = (cdkhdr_t __iomem *) EBRDGETMEMPTR(brdp, CDK_CDKADDR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582 nrdevs = hdrp->nrdevs;
3583
3584#if 0
3585 printk("%s(%d): CDK version %d.%d.%d --> "
3586 "nrdevs=%d memp=%x hostp=%x slavep=%x\n",
Alan Cox4ac43602006-06-28 04:26:52 -07003587 __FILE__, __LINE__, readb(&hdrp->ver_release), readb(&hdrp->ver_modification),
3588 readb(&hdrp->ver_fix), nrdevs, (int) readl(&hdrp->memp), readl(&hdrp->hostp),
3589 readl(&hdrp->slavep));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590#endif
3591
3592 if (nrdevs < (brdp->nrports + 1)) {
3593 printk(KERN_ERR "STALLION: slave failed to allocate memory for "
3594 "all devices, devices=%d\n", nrdevs);
3595 brdp->nrports = nrdevs - 1;
3596 }
3597 brdp->nrdevs = nrdevs;
3598 brdp->hostoffset = hdrp->hostp - CDK_CDKADDR;
3599 brdp->slaveoffset = hdrp->slavep - CDK_CDKADDR;
3600 brdp->bitsize = (nrdevs + 7) / 8;
Alan Cox4ac43602006-06-28 04:26:52 -07003601 memoff = readl(&hdrp->memp);
3602 if (memoff > brdp->memsize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603 printk(KERN_ERR "STALLION: corrupted shared memory region?\n");
3604 rc = -EIO;
3605 goto stli_donestartup;
3606 }
Alan Cox4ac43602006-06-28 04:26:52 -07003607 memp = (cdkmem_t __iomem *) EBRDGETMEMPTR(brdp, memoff);
3608 if (readw(&memp->dtype) != TYP_ASYNCTRL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609 printk(KERN_ERR "STALLION: no slave control device found\n");
3610 goto stli_donestartup;
3611 }
3612 memp++;
3613
3614/*
3615 * Cycle through memory allocation of each port. We are guaranteed to
3616 * have all ports inside the first page of slave window, so no need to
3617 * change pages while reading memory map.
3618 */
3619 for (i = 1, portnr = 0; (i < nrdevs); i++, portnr++, memp++) {
Alan Cox4ac43602006-06-28 04:26:52 -07003620 if (readw(&memp->dtype) != TYP_ASYNC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621 break;
3622 portp = brdp->ports[portnr];
Alan Cox4ac43602006-06-28 04:26:52 -07003623 if (portp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624 break;
3625 portp->devnr = i;
Alan Cox4ac43602006-06-28 04:26:52 -07003626 portp->addr = readl(&memp->offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627 portp->reqbit = (unsigned char) (0x1 << (i * 8 / nrdevs));
3628 portp->portidx = (unsigned char) (i / 8);
3629 portp->portbit = (unsigned char) (0x1 << (i % 8));
3630 }
3631
Alan Cox4ac43602006-06-28 04:26:52 -07003632 writeb(0xff, &hdrp->slavereq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633
3634/*
3635 * For each port setup a local copy of the RX and TX buffer offsets
3636 * and sizes. We do this separate from the above, because we need to
3637 * move the shared memory page...
3638 */
3639 for (i = 1, portnr = 0; (i < nrdevs); i++, portnr++) {
3640 portp = brdp->ports[portnr];
Alan Cox4ac43602006-06-28 04:26:52 -07003641 if (portp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003642 break;
3643 if (portp->addr == 0)
3644 break;
Alan Cox4ac43602006-06-28 04:26:52 -07003645 ap = (cdkasy_t __iomem *) EBRDGETMEMPTR(brdp, portp->addr);
3646 if (ap != NULL) {
3647 portp->rxsize = readw(&ap->rxq.size);
3648 portp->txsize = readw(&ap->txq.size);
3649 portp->rxoffset = readl(&ap->rxq.offset);
3650 portp->txoffset = readl(&ap->txq.offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003651 }
3652 }
3653
3654stli_donestartup:
3655 EBRDDISABLE(brdp);
Alan Cox4ac43602006-06-28 04:26:52 -07003656 spin_unlock_irqrestore(&brd_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657
3658 if (rc == 0)
3659 brdp->state |= BST_STARTED;
3660
3661 if (! stli_timeron) {
3662 stli_timeron++;
3663 stli_timerlist.expires = STLI_TIMEOUT;
3664 add_timer(&stli_timerlist);
3665 }
3666
Alan Cox4ac43602006-06-28 04:26:52 -07003667 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668}
3669
3670/*****************************************************************************/
3671
3672/*
3673 * Probe and initialize the specified board.
3674 */
3675
Jiri Slaby845bead2006-12-08 02:39:17 -08003676static int __devinit stli_brdinit(stlibrd_t *brdp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678 stli_brds[brdp->brdnr] = brdp;
3679
3680 switch (brdp->brdtype) {
3681 case BRD_ECP:
3682 case BRD_ECPE:
3683 case BRD_ECPMC:
3684 case BRD_ECPPCI:
3685 stli_initecp(brdp);
3686 break;
3687 case BRD_ONBOARD:
3688 case BRD_ONBOARDE:
3689 case BRD_ONBOARD2:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003690 case BRD_BRUMBY4:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003691 case BRD_STALLION:
3692 stli_initonb(brdp);
3693 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003694 default:
3695 printk(KERN_ERR "STALLION: board=%d is unknown board "
3696 "type=%d\n", brdp->brdnr, brdp->brdtype);
Alan Cox4ac43602006-06-28 04:26:52 -07003697 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003698 }
3699
3700 if ((brdp->state & BST_FOUND) == 0) {
3701 printk(KERN_ERR "STALLION: %s board not found, board=%d "
3702 "io=%x mem=%x\n",
3703 stli_brdnames[brdp->brdtype], brdp->brdnr,
3704 brdp->iobase, (int) brdp->memaddr);
Alan Cox4ac43602006-06-28 04:26:52 -07003705 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706 }
3707
3708 stli_initports(brdp);
3709 printk(KERN_INFO "STALLION: %s found, board=%d io=%x mem=%x "
3710 "nrpanels=%d nrports=%d\n", stli_brdnames[brdp->brdtype],
3711 brdp->brdnr, brdp->iobase, (int) brdp->memaddr,
3712 brdp->nrpanels, brdp->nrports);
Alan Cox4ac43602006-06-28 04:26:52 -07003713 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003714}
3715
3716/*****************************************************************************/
3717
3718/*
3719 * Probe around trying to find where the EISA boards shared memory
3720 * might be. This is a bit if hack, but it is the best we can do.
3721 */
3722
3723static int stli_eisamemprobe(stlibrd_t *brdp)
3724{
Alan Cox4ac43602006-06-28 04:26:52 -07003725 cdkecpsig_t ecpsig, __iomem *ecpsigp;
3726 cdkonbsig_t onbsig, __iomem *onbsigp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727 int i, foundit;
3728
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729/*
3730 * First up we reset the board, to get it into a known state. There
3731 * is only 2 board types here we need to worry about. Don;t use the
3732 * standard board init routine here, it programs up the shared
3733 * memory address, and we don't know it yet...
3734 */
3735 if (brdp->brdtype == BRD_ECPE) {
3736 outb(0x1, (brdp->iobase + ECP_EIBRDENAB));
3737 outb(ECP_EISTOP, (brdp->iobase + ECP_EICONFR));
3738 udelay(10);
3739 outb(ECP_EIDISABLE, (brdp->iobase + ECP_EICONFR));
3740 udelay(500);
3741 stli_ecpeienable(brdp);
3742 } else if (brdp->brdtype == BRD_ONBOARDE) {
3743 outb(0x1, (brdp->iobase + ONB_EIBRDENAB));
3744 outb(ONB_EISTOP, (brdp->iobase + ONB_EICONFR));
3745 udelay(10);
3746 outb(ONB_EIDISABLE, (brdp->iobase + ONB_EICONFR));
3747 mdelay(100);
3748 outb(0x1, brdp->iobase);
3749 mdelay(1);
3750 stli_onbeenable(brdp);
3751 } else {
Alan Cox4ac43602006-06-28 04:26:52 -07003752 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753 }
3754
3755 foundit = 0;
3756 brdp->memsize = ECP_MEMSIZE;
3757
3758/*
3759 * Board shared memory is enabled, so now we have a poke around and
3760 * see if we can find it.
3761 */
3762 for (i = 0; (i < stli_eisamempsize); i++) {
3763 brdp->memaddr = stli_eisamemprobeaddrs[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003764 brdp->membase = ioremap(brdp->memaddr, brdp->memsize);
Alan Cox4ac43602006-06-28 04:26:52 -07003765 if (brdp->membase == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766 continue;
3767
3768 if (brdp->brdtype == BRD_ECPE) {
Al Viro29756fa2006-10-10 22:47:27 +01003769 ecpsigp = stli_ecpeigetmemptr(brdp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003770 CDK_SIGADDR, __LINE__);
Alan Cox4ac43602006-06-28 04:26:52 -07003771 memcpy_fromio(&ecpsig, ecpsigp, sizeof(cdkecpsig_t));
3772 if (ecpsig.magic == cpu_to_le32(ECP_MAGIC))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003773 foundit = 1;
3774 } else {
Alan Cox4ac43602006-06-28 04:26:52 -07003775 onbsigp = (cdkonbsig_t __iomem *) stli_onbegetmemptr(brdp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776 CDK_SIGADDR, __LINE__);
Alan Cox4ac43602006-06-28 04:26:52 -07003777 memcpy_fromio(&onbsig, onbsigp, sizeof(cdkonbsig_t));
3778 if ((onbsig.magic0 == cpu_to_le16(ONB_MAGIC0)) &&
3779 (onbsig.magic1 == cpu_to_le16(ONB_MAGIC1)) &&
3780 (onbsig.magic2 == cpu_to_le16(ONB_MAGIC2)) &&
3781 (onbsig.magic3 == cpu_to_le16(ONB_MAGIC3)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782 foundit = 1;
3783 }
3784
3785 iounmap(brdp->membase);
3786 if (foundit)
3787 break;
3788 }
3789
3790/*
3791 * Regardless of whether we found the shared memory or not we must
3792 * disable the region. After that return success or failure.
3793 */
3794 if (brdp->brdtype == BRD_ECPE)
3795 stli_ecpeidisable(brdp);
3796 else
3797 stli_onbedisable(brdp);
3798
3799 if (! foundit) {
3800 brdp->memaddr = 0;
3801 brdp->membase = NULL;
3802 printk(KERN_ERR "STALLION: failed to probe shared memory "
3803 "region for %s in EISA slot=%d\n",
3804 stli_brdnames[brdp->brdtype], (brdp->iobase >> 12));
Alan Cox4ac43602006-06-28 04:26:52 -07003805 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003806 }
Alan Cox4ac43602006-06-28 04:26:52 -07003807 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808}
3809
3810static int stli_getbrdnr(void)
3811{
3812 int i;
3813
3814 for (i = 0; i < STL_MAXBRDS; i++) {
3815 if (!stli_brds[i]) {
3816 if (i >= stli_nrbrds)
3817 stli_nrbrds = i + 1;
3818 return i;
3819 }
3820 }
3821 return -1;
3822}
3823
3824/*****************************************************************************/
3825
3826/*
3827 * Probe around and try to find any EISA boards in system. The biggest
3828 * problem here is finding out what memory address is associated with
3829 * an EISA board after it is found. The registers of the ECPE and
3830 * ONboardE are not readable - so we can't read them from there. We
3831 * don't have access to the EISA CMOS (or EISA BIOS) so we don't
3832 * actually have any way to find out the real value. The best we can
3833 * do is go probing around in the usual places hoping we can find it.
3834 */
3835
3836static int stli_findeisabrds(void)
3837{
Alan Cox4ac43602006-06-28 04:26:52 -07003838 stlibrd_t *brdp;
3839 unsigned int iobase, eid;
3840 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003841
3842/*
Alan Cox4ac43602006-06-28 04:26:52 -07003843 * Firstly check if this is an EISA system. If this is not an EISA system then
Linus Torvalds1da177e2005-04-16 15:20:36 -07003844 * don't bother going any further!
3845 */
Alan Cox4ac43602006-06-28 04:26:52 -07003846 if (EISA_bus)
3847 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003848
3849/*
3850 * Looks like an EISA system, so go searching for EISA boards.
3851 */
3852 for (iobase = 0x1000; (iobase <= 0xc000); iobase += 0x1000) {
3853 outb(0xff, (iobase + 0xc80));
3854 eid = inb(iobase + 0xc80);
3855 eid |= inb(iobase + 0xc81) << 8;
3856 if (eid != STL_EISAID)
3857 continue;
3858
3859/*
3860 * We have found a board. Need to check if this board was
3861 * statically configured already (just in case!).
3862 */
3863 for (i = 0; (i < STL_MAXBRDS); i++) {
3864 brdp = stli_brds[i];
Alan Cox4ac43602006-06-28 04:26:52 -07003865 if (brdp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003866 continue;
3867 if (brdp->iobase == iobase)
3868 break;
3869 }
3870 if (i < STL_MAXBRDS)
3871 continue;
3872
3873/*
3874 * We have found a Stallion board and it is not configured already.
3875 * Allocate a board structure and initialize it.
3876 */
Alan Cox4ac43602006-06-28 04:26:52 -07003877 if ((brdp = stli_allocbrd()) == NULL)
3878 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003879 if ((brdp->brdnr = stli_getbrdnr()) < 0)
Alan Cox4ac43602006-06-28 04:26:52 -07003880 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003881 eid = inb(iobase + 0xc82);
3882 if (eid == ECP_EISAID)
3883 brdp->brdtype = BRD_ECPE;
3884 else if (eid == ONB_EISAID)
3885 brdp->brdtype = BRD_ONBOARDE;
3886 else
3887 brdp->brdtype = BRD_UNKNOWN;
3888 brdp->iobase = iobase;
3889 outb(0x1, (iobase + 0xc84));
3890 if (stli_eisamemprobe(brdp))
3891 outb(0, (iobase + 0xc84));
3892 stli_brdinit(brdp);
3893 }
3894
Alan Cox4ac43602006-06-28 04:26:52 -07003895 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003896}
3897
3898/*****************************************************************************/
3899
3900/*
3901 * Find the next available board number that is free.
3902 */
3903
3904/*****************************************************************************/
3905
Linus Torvalds1da177e2005-04-16 15:20:36 -07003906/*
3907 * We have a Stallion board. Allocate a board structure and
3908 * initialize it. Read its IO and MEMORY resources from PCI
3909 * configuration space.
3910 */
3911
Jiri Slaby845bead2006-12-08 02:39:17 -08003912static int __devinit stli_pciprobe(struct pci_dev *pdev,
3913 const struct pci_device_id *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003914{
Alan Cox4ac43602006-06-28 04:26:52 -07003915 stlibrd_t *brdp;
Jiri Slaby845bead2006-12-08 02:39:17 -08003916 int retval = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003917
Jiri Slaby845bead2006-12-08 02:39:17 -08003918 retval = pci_enable_device(pdev);
3919 if (retval)
3920 goto err;
3921 brdp = stli_allocbrd();
3922 if (brdp == NULL) {
3923 retval = -ENOMEM;
3924 goto err;
3925 }
3926 if ((brdp->brdnr = stli_getbrdnr()) < 0) { /* TODO: locking */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003927 printk(KERN_INFO "STALLION: too many boards found, "
3928 "maximum supported %d\n", STL_MAXBRDS);
Jiri Slaby845bead2006-12-08 02:39:17 -08003929 retval = -EIO;
3930 goto err_fr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003931 }
Jiri Slaby845bead2006-12-08 02:39:17 -08003932 brdp->brdtype = BRD_ECPPCI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003933/*
3934 * We have all resources from the board, so lets setup the actual
3935 * board structure now.
3936 */
Jiri Slaby845bead2006-12-08 02:39:17 -08003937 brdp->iobase = pci_resource_start(pdev, 3);
3938 brdp->memaddr = pci_resource_start(pdev, 2);
3939 retval = stli_brdinit(brdp);
3940 if (retval)
3941 goto err_fr;
3942
3943 pci_set_drvdata(pdev, brdp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003944
Alan Cox4ac43602006-06-28 04:26:52 -07003945 return 0;
Jiri Slaby845bead2006-12-08 02:39:17 -08003946err_fr:
3947 kfree(brdp);
3948err:
3949 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950}
3951
Jiri Slaby845bead2006-12-08 02:39:17 -08003952static void stli_pciremove(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003953{
Jiri Slaby845bead2006-12-08 02:39:17 -08003954 stlibrd_t *brdp = pci_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955
Jiri Slaby845bead2006-12-08 02:39:17 -08003956 stli_cleanup_ports(brdp);
3957
3958 iounmap(brdp->membase);
3959 if (brdp->iosize > 0)
3960 release_region(brdp->iobase, brdp->iosize);
3961
3962 stli_brds[brdp->brdnr] = NULL;
3963 kfree(brdp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003964}
3965
Jiri Slaby845bead2006-12-08 02:39:17 -08003966static struct pci_driver stli_pcidriver = {
3967 .name = "istallion",
3968 .id_table = istallion_pci_tbl,
3969 .probe = stli_pciprobe,
3970 .remove = __devexit_p(stli_pciremove)
3971};
Linus Torvalds1da177e2005-04-16 15:20:36 -07003972/*****************************************************************************/
3973
3974/*
3975 * Allocate a new board structure. Fill out the basic info in it.
3976 */
3977
3978static stlibrd_t *stli_allocbrd(void)
3979{
Alan Cox4ac43602006-06-28 04:26:52 -07003980 stlibrd_t *brdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981
Tobias Klauserb0b4ed72006-03-31 02:30:56 -08003982 brdp = kzalloc(sizeof(stlibrd_t), GFP_KERNEL);
3983 if (!brdp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003984 printk(KERN_ERR "STALLION: failed to allocate memory "
Alan Cox4ac43602006-06-28 04:26:52 -07003985 "(size=%Zd)\n", sizeof(stlibrd_t));
Tobias Klauserb0b4ed72006-03-31 02:30:56 -08003986 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003987 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988 brdp->magic = STLI_BOARDMAGIC;
Alan Cox4ac43602006-06-28 04:26:52 -07003989 return brdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003990}
3991
3992/*****************************************************************************/
3993
3994/*
3995 * Scan through all the boards in the configuration and see what we
3996 * can find.
3997 */
3998
3999static int stli_initbrds(void)
4000{
Alan Cox4ac43602006-06-28 04:26:52 -07004001 stlibrd_t *brdp, *nxtbrdp;
Jiri Slabya3f8d9d2006-12-08 02:39:18 -08004002 stlconf_t conf;
Jiri Slaby845bead2006-12-08 02:39:17 -08004003 int i, j, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004004
Jiri Slabya3f8d9d2006-12-08 02:39:18 -08004005 for (stli_nrbrds = 0; stli_nrbrds < ARRAY_SIZE(stli_brdsp);
4006 stli_nrbrds++) {
4007 memset(&conf, 0, sizeof(conf));
4008 if (stli_parsebrd(&conf, stli_brdsp[stli_nrbrds]) == 0)
4009 continue;
Alan Cox4ac43602006-06-28 04:26:52 -07004010 if ((brdp = stli_allocbrd()) == NULL)
Jiri Slabya3f8d9d2006-12-08 02:39:18 -08004011 continue;
4012 brdp->brdnr = stli_nrbrds;
4013 brdp->brdtype = conf.brdtype;
4014 brdp->iobase = conf.ioaddr1;
4015 brdp->memaddr = conf.memaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004016 stli_brdinit(brdp);
4017 }
4018
Adrian Bunkdbc6b5f2005-06-25 14:59:03 -07004019 if (STLI_EISAPROBE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004020 stli_findeisabrds();
Jiri Slaby845bead2006-12-08 02:39:17 -08004021
4022 retval = pci_register_driver(&stli_pcidriver);
4023 /* TODO: check retval and do something */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004024
4025/*
4026 * All found boards are initialized. Now for a little optimization, if
4027 * no boards are sharing the "shared memory" regions then we can just
4028 * leave them all enabled. This is in fact the usual case.
4029 */
4030 stli_shared = 0;
4031 if (stli_nrbrds > 1) {
4032 for (i = 0; (i < stli_nrbrds); i++) {
4033 brdp = stli_brds[i];
Alan Cox4ac43602006-06-28 04:26:52 -07004034 if (brdp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004035 continue;
4036 for (j = i + 1; (j < stli_nrbrds); j++) {
4037 nxtbrdp = stli_brds[j];
Alan Cox4ac43602006-06-28 04:26:52 -07004038 if (nxtbrdp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004039 continue;
4040 if ((brdp->membase >= nxtbrdp->membase) &&
4041 (brdp->membase <= (nxtbrdp->membase +
4042 nxtbrdp->memsize - 1))) {
4043 stli_shared++;
4044 break;
4045 }
4046 }
4047 }
4048 }
4049
4050 if (stli_shared == 0) {
4051 for (i = 0; (i < stli_nrbrds); i++) {
4052 brdp = stli_brds[i];
Alan Cox4ac43602006-06-28 04:26:52 -07004053 if (brdp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054 continue;
4055 if (brdp->state & BST_FOUND) {
4056 EBRDENABLE(brdp);
4057 brdp->enable = NULL;
4058 brdp->disable = NULL;
4059 }
4060 }
4061 }
4062
Alan Cox4ac43602006-06-28 04:26:52 -07004063 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004064}
4065
4066/*****************************************************************************/
4067
4068/*
4069 * Code to handle an "staliomem" read operation. This device is the
4070 * contents of the board shared memory. It is used for down loading
4071 * the slave image (and debugging :-)
4072 */
4073
4074static ssize_t stli_memread(struct file *fp, char __user *buf, size_t count, loff_t *offp)
4075{
Alan Cox4ac43602006-06-28 04:26:52 -07004076 unsigned long flags;
Al Viro29756fa2006-10-10 22:47:27 +01004077 void __iomem *memptr;
Alan Cox4ac43602006-06-28 04:26:52 -07004078 stlibrd_t *brdp;
4079 int brdnr, size, n;
4080 void *p;
4081 loff_t off = *offp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004082
Josef Sipeka7113a92006-12-08 02:36:55 -08004083 brdnr = iminor(fp->f_path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004084 if (brdnr >= stli_nrbrds)
Alan Cox4ac43602006-06-28 04:26:52 -07004085 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004086 brdp = stli_brds[brdnr];
Alan Cox4ac43602006-06-28 04:26:52 -07004087 if (brdp == NULL)
4088 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004089 if (brdp->state == 0)
Alan Cox4ac43602006-06-28 04:26:52 -07004090 return -ENODEV;
4091 if (off >= brdp->memsize || off + count < off)
4092 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004093
Jiri Slabya3f8d9d2006-12-08 02:39:18 -08004094 size = min(count, (size_t)(brdp->memsize - off));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004095
Alan Cox4ac43602006-06-28 04:26:52 -07004096 /*
4097 * Copy the data a page at a time
4098 */
4099
4100 p = (void *)__get_free_page(GFP_KERNEL);
4101 if(p == NULL)
4102 return -ENOMEM;
4103
Linus Torvalds1da177e2005-04-16 15:20:36 -07004104 while (size > 0) {
Alan Cox4ac43602006-06-28 04:26:52 -07004105 spin_lock_irqsave(&brd_lock, flags);
4106 EBRDENABLE(brdp);
Al Viro29756fa2006-10-10 22:47:27 +01004107 memptr = EBRDGETMEMPTR(brdp, off);
Jiri Slabya3f8d9d2006-12-08 02:39:18 -08004108 n = min(size, (int)(brdp->pagesize - (((unsigned long) off) % brdp->pagesize)));
4109 n = min(n, (int)PAGE_SIZE);
Alan Cox4ac43602006-06-28 04:26:52 -07004110 memcpy_fromio(p, memptr, n);
4111 EBRDDISABLE(brdp);
4112 spin_unlock_irqrestore(&brd_lock, flags);
4113 if (copy_to_user(buf, p, n)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114 count = -EFAULT;
4115 goto out;
4116 }
Alan Cox4ac43602006-06-28 04:26:52 -07004117 off += n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118 buf += n;
4119 size -= n;
4120 }
4121out:
Alan Cox4ac43602006-06-28 04:26:52 -07004122 *offp = off;
4123 free_page((unsigned long)p);
4124 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004125}
4126
4127/*****************************************************************************/
4128
4129/*
4130 * Code to handle an "staliomem" write operation. This device is the
4131 * contents of the board shared memory. It is used for down loading
4132 * the slave image (and debugging :-)
Alan Cox4ac43602006-06-28 04:26:52 -07004133 *
4134 * FIXME: copy under lock
Linus Torvalds1da177e2005-04-16 15:20:36 -07004135 */
4136
4137static ssize_t stli_memwrite(struct file *fp, const char __user *buf, size_t count, loff_t *offp)
4138{
Alan Cox4ac43602006-06-28 04:26:52 -07004139 unsigned long flags;
Al Viro29756fa2006-10-10 22:47:27 +01004140 void __iomem *memptr;
Alan Cox4ac43602006-06-28 04:26:52 -07004141 stlibrd_t *brdp;
4142 char __user *chbuf;
4143 int brdnr, size, n;
4144 void *p;
4145 loff_t off = *offp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146
Josef Sipeka7113a92006-12-08 02:36:55 -08004147 brdnr = iminor(fp->f_path.dentry->d_inode);
Alan Cox4ac43602006-06-28 04:26:52 -07004148
Linus Torvalds1da177e2005-04-16 15:20:36 -07004149 if (brdnr >= stli_nrbrds)
Alan Cox4ac43602006-06-28 04:26:52 -07004150 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004151 brdp = stli_brds[brdnr];
Alan Cox4ac43602006-06-28 04:26:52 -07004152 if (brdp == NULL)
4153 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004154 if (brdp->state == 0)
Alan Cox4ac43602006-06-28 04:26:52 -07004155 return -ENODEV;
4156 if (off >= brdp->memsize || off + count < off)
4157 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158
4159 chbuf = (char __user *) buf;
Jiri Slabya3f8d9d2006-12-08 02:39:18 -08004160 size = min(count, (size_t)(brdp->memsize - off));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161
Alan Cox4ac43602006-06-28 04:26:52 -07004162 /*
4163 * Copy the data a page at a time
4164 */
4165
4166 p = (void *)__get_free_page(GFP_KERNEL);
4167 if(p == NULL)
4168 return -ENOMEM;
4169
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170 while (size > 0) {
Jiri Slabya3f8d9d2006-12-08 02:39:18 -08004171 n = min(size, (int)(brdp->pagesize - (((unsigned long) off) % brdp->pagesize)));
4172 n = min(n, (int)PAGE_SIZE);
Alan Cox4ac43602006-06-28 04:26:52 -07004173 if (copy_from_user(p, chbuf, n)) {
4174 if (count == 0)
4175 count = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176 goto out;
4177 }
Alan Cox4ac43602006-06-28 04:26:52 -07004178 spin_lock_irqsave(&brd_lock, flags);
4179 EBRDENABLE(brdp);
Al Viro29756fa2006-10-10 22:47:27 +01004180 memptr = EBRDGETMEMPTR(brdp, off);
Alan Cox4ac43602006-06-28 04:26:52 -07004181 memcpy_toio(memptr, p, n);
4182 EBRDDISABLE(brdp);
4183 spin_unlock_irqrestore(&brd_lock, flags);
4184 off += n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004185 chbuf += n;
4186 size -= n;
4187 }
4188out:
Alan Cox4ac43602006-06-28 04:26:52 -07004189 free_page((unsigned long) p);
4190 *offp = off;
4191 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192}
4193
4194/*****************************************************************************/
4195
4196/*
4197 * Return the board stats structure to user app.
4198 */
4199
4200static int stli_getbrdstats(combrd_t __user *bp)
4201{
Alan Cox4ac43602006-06-28 04:26:52 -07004202 stlibrd_t *brdp;
4203 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004204
4205 if (copy_from_user(&stli_brdstats, bp, sizeof(combrd_t)))
4206 return -EFAULT;
4207 if (stli_brdstats.brd >= STL_MAXBRDS)
Alan Cox4ac43602006-06-28 04:26:52 -07004208 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004209 brdp = stli_brds[stli_brdstats.brd];
Alan Cox4ac43602006-06-28 04:26:52 -07004210 if (brdp == NULL)
4211 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004212
4213 memset(&stli_brdstats, 0, sizeof(combrd_t));
4214 stli_brdstats.brd = brdp->brdnr;
4215 stli_brdstats.type = brdp->brdtype;
4216 stli_brdstats.hwid = 0;
4217 stli_brdstats.state = brdp->state;
4218 stli_brdstats.ioaddr = brdp->iobase;
4219 stli_brdstats.memaddr = brdp->memaddr;
4220 stli_brdstats.nrpanels = brdp->nrpanels;
4221 stli_brdstats.nrports = brdp->nrports;
4222 for (i = 0; (i < brdp->nrpanels); i++) {
4223 stli_brdstats.panels[i].panel = i;
4224 stli_brdstats.panels[i].hwid = brdp->panelids[i];
4225 stli_brdstats.panels[i].nrports = brdp->panels[i];
4226 }
4227
4228 if (copy_to_user(bp, &stli_brdstats, sizeof(combrd_t)))
4229 return -EFAULT;
Alan Cox4ac43602006-06-28 04:26:52 -07004230 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231}
4232
4233/*****************************************************************************/
4234
4235/*
4236 * Resolve the referenced port number into a port struct pointer.
4237 */
4238
4239static stliport_t *stli_getport(int brdnr, int panelnr, int portnr)
4240{
Alan Cox4ac43602006-06-28 04:26:52 -07004241 stlibrd_t *brdp;
4242 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243
Alan Cox4ac43602006-06-28 04:26:52 -07004244 if (brdnr < 0 || brdnr >= STL_MAXBRDS)
4245 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246 brdp = stli_brds[brdnr];
Alan Cox4ac43602006-06-28 04:26:52 -07004247 if (brdp == NULL)
4248 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004249 for (i = 0; (i < panelnr); i++)
4250 portnr += brdp->panels[i];
4251 if ((portnr < 0) || (portnr >= brdp->nrports))
Alan Cox4ac43602006-06-28 04:26:52 -07004252 return NULL;
4253 return brdp->ports[portnr];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004254}
4255
4256/*****************************************************************************/
4257
4258/*
4259 * Return the port stats structure to user app. A NULL port struct
4260 * pointer passed in means that we need to find out from the app
4261 * what port to get stats for (used through board control device).
4262 */
4263
4264static int stli_portcmdstats(stliport_t *portp)
4265{
4266 unsigned long flags;
4267 stlibrd_t *brdp;
4268 int rc;
4269
4270 memset(&stli_comstats, 0, sizeof(comstats_t));
4271
Alan Cox4ac43602006-06-28 04:26:52 -07004272 if (portp == NULL)
4273 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274 brdp = stli_brds[portp->brdnr];
Alan Cox4ac43602006-06-28 04:26:52 -07004275 if (brdp == NULL)
4276 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004277
4278 if (brdp->state & BST_STARTED) {
4279 if ((rc = stli_cmdwait(brdp, portp, A_GETSTATS,
4280 &stli_cdkstats, sizeof(asystats_t), 1)) < 0)
Alan Cox4ac43602006-06-28 04:26:52 -07004281 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004282 } else {
4283 memset(&stli_cdkstats, 0, sizeof(asystats_t));
4284 }
4285
4286 stli_comstats.brd = portp->brdnr;
4287 stli_comstats.panel = portp->panelnr;
4288 stli_comstats.port = portp->portnr;
4289 stli_comstats.state = portp->state;
4290 stli_comstats.flags = portp->flags;
4291
Alan Cox4ac43602006-06-28 04:26:52 -07004292 spin_lock_irqsave(&brd_lock, flags);
4293 if (portp->tty != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294 if (portp->tty->driver_data == portp) {
4295 stli_comstats.ttystate = portp->tty->flags;
Alan Cox4ac43602006-06-28 04:26:52 -07004296 stli_comstats.rxbuffered = -1;
4297 if (portp->tty->termios != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004298 stli_comstats.cflags = portp->tty->termios->c_cflag;
4299 stli_comstats.iflags = portp->tty->termios->c_iflag;
4300 stli_comstats.oflags = portp->tty->termios->c_oflag;
4301 stli_comstats.lflags = portp->tty->termios->c_lflag;
4302 }
4303 }
4304 }
Alan Cox4ac43602006-06-28 04:26:52 -07004305 spin_unlock_irqrestore(&brd_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004306
4307 stli_comstats.txtotal = stli_cdkstats.txchars;
4308 stli_comstats.rxtotal = stli_cdkstats.rxchars + stli_cdkstats.ringover;
4309 stli_comstats.txbuffered = stli_cdkstats.txringq;
4310 stli_comstats.rxbuffered += stli_cdkstats.rxringq;
4311 stli_comstats.rxoverrun = stli_cdkstats.overruns;
4312 stli_comstats.rxparity = stli_cdkstats.parity;
4313 stli_comstats.rxframing = stli_cdkstats.framing;
4314 stli_comstats.rxlost = stli_cdkstats.ringover;
4315 stli_comstats.rxbreaks = stli_cdkstats.rxbreaks;
4316 stli_comstats.txbreaks = stli_cdkstats.txbreaks;
4317 stli_comstats.txxon = stli_cdkstats.txstart;
4318 stli_comstats.txxoff = stli_cdkstats.txstop;
4319 stli_comstats.rxxon = stli_cdkstats.rxstart;
4320 stli_comstats.rxxoff = stli_cdkstats.rxstop;
4321 stli_comstats.rxrtsoff = stli_cdkstats.rtscnt / 2;
4322 stli_comstats.rxrtson = stli_cdkstats.rtscnt - stli_comstats.rxrtsoff;
4323 stli_comstats.modem = stli_cdkstats.dcdcnt;
4324 stli_comstats.hwid = stli_cdkstats.hwid;
4325 stli_comstats.signals = stli_mktiocm(stli_cdkstats.signals);
4326
Alan Cox4ac43602006-06-28 04:26:52 -07004327 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004328}
4329
4330/*****************************************************************************/
4331
4332/*
4333 * Return the port stats structure to user app. A NULL port struct
4334 * pointer passed in means that we need to find out from the app
4335 * what port to get stats for (used through board control device).
4336 */
4337
4338static int stli_getportstats(stliport_t *portp, comstats_t __user *cp)
4339{
Alan Cox4ac43602006-06-28 04:26:52 -07004340 stlibrd_t *brdp;
4341 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342
4343 if (!portp) {
4344 if (copy_from_user(&stli_comstats, cp, sizeof(comstats_t)))
4345 return -EFAULT;
4346 portp = stli_getport(stli_comstats.brd, stli_comstats.panel,
4347 stli_comstats.port);
4348 if (!portp)
4349 return -ENODEV;
4350 }
4351
4352 brdp = stli_brds[portp->brdnr];
4353 if (!brdp)
4354 return -ENODEV;
4355
4356 if ((rc = stli_portcmdstats(portp)) < 0)
4357 return rc;
4358
4359 return copy_to_user(cp, &stli_comstats, sizeof(comstats_t)) ?
4360 -EFAULT : 0;
4361}
4362
4363/*****************************************************************************/
4364
4365/*
4366 * Clear the port stats structure. We also return it zeroed out...
4367 */
4368
4369static int stli_clrportstats(stliport_t *portp, comstats_t __user *cp)
4370{
Alan Cox4ac43602006-06-28 04:26:52 -07004371 stlibrd_t *brdp;
4372 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004373
4374 if (!portp) {
4375 if (copy_from_user(&stli_comstats, cp, sizeof(comstats_t)))
4376 return -EFAULT;
4377 portp = stli_getport(stli_comstats.brd, stli_comstats.panel,
4378 stli_comstats.port);
4379 if (!portp)
4380 return -ENODEV;
4381 }
4382
4383 brdp = stli_brds[portp->brdnr];
4384 if (!brdp)
4385 return -ENODEV;
4386
4387 if (brdp->state & BST_STARTED) {
4388 if ((rc = stli_cmdwait(brdp, portp, A_CLEARSTATS, NULL, 0, 0)) < 0)
4389 return rc;
4390 }
4391
4392 memset(&stli_comstats, 0, sizeof(comstats_t));
4393 stli_comstats.brd = portp->brdnr;
4394 stli_comstats.panel = portp->panelnr;
4395 stli_comstats.port = portp->portnr;
4396
4397 if (copy_to_user(cp, &stli_comstats, sizeof(comstats_t)))
4398 return -EFAULT;
4399 return 0;
4400}
4401
4402/*****************************************************************************/
4403
4404/*
4405 * Return the entire driver ports structure to a user app.
4406 */
4407
4408static int stli_getportstruct(stliport_t __user *arg)
4409{
Alan Cox4ac43602006-06-28 04:26:52 -07004410 stliport_t *portp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004411
4412 if (copy_from_user(&stli_dummyport, arg, sizeof(stliport_t)))
4413 return -EFAULT;
4414 portp = stli_getport(stli_dummyport.brdnr, stli_dummyport.panelnr,
4415 stli_dummyport.portnr);
4416 if (!portp)
4417 return -ENODEV;
4418 if (copy_to_user(arg, portp, sizeof(stliport_t)))
4419 return -EFAULT;
4420 return 0;
4421}
4422
4423/*****************************************************************************/
4424
4425/*
4426 * Return the entire driver board structure to a user app.
4427 */
4428
4429static int stli_getbrdstruct(stlibrd_t __user *arg)
4430{
Alan Cox4ac43602006-06-28 04:26:52 -07004431 stlibrd_t *brdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004432
4433 if (copy_from_user(&stli_dummybrd, arg, sizeof(stlibrd_t)))
4434 return -EFAULT;
4435 if ((stli_dummybrd.brdnr < 0) || (stli_dummybrd.brdnr >= STL_MAXBRDS))
4436 return -ENODEV;
4437 brdp = stli_brds[stli_dummybrd.brdnr];
4438 if (!brdp)
4439 return -ENODEV;
4440 if (copy_to_user(arg, brdp, sizeof(stlibrd_t)))
4441 return -EFAULT;
4442 return 0;
4443}
4444
4445/*****************************************************************************/
4446
4447/*
4448 * The "staliomem" device is also required to do some special operations on
4449 * the board. We need to be able to send an interrupt to the board,
4450 * reset it, and start/stop it.
4451 */
4452
4453static int stli_memioctl(struct inode *ip, struct file *fp, unsigned int cmd, unsigned long arg)
4454{
Alan Cox4ac43602006-06-28 04:26:52 -07004455 stlibrd_t *brdp;
4456 int brdnr, rc, done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004457 void __user *argp = (void __user *)arg;
4458
Linus Torvalds1da177e2005-04-16 15:20:36 -07004459/*
4460 * First up handle the board independent ioctls.
4461 */
4462 done = 0;
4463 rc = 0;
4464
4465 switch (cmd) {
4466 case COM_GETPORTSTATS:
4467 rc = stli_getportstats(NULL, argp);
4468 done++;
4469 break;
4470 case COM_CLRPORTSTATS:
4471 rc = stli_clrportstats(NULL, argp);
4472 done++;
4473 break;
4474 case COM_GETBRDSTATS:
4475 rc = stli_getbrdstats(argp);
4476 done++;
4477 break;
4478 case COM_READPORT:
4479 rc = stli_getportstruct(argp);
4480 done++;
4481 break;
4482 case COM_READBOARD:
4483 rc = stli_getbrdstruct(argp);
4484 done++;
4485 break;
4486 }
4487
4488 if (done)
Alan Cox4ac43602006-06-28 04:26:52 -07004489 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004490
4491/*
4492 * Now handle the board specific ioctls. These all depend on the
4493 * minor number of the device they were called from.
4494 */
4495 brdnr = iminor(ip);
4496 if (brdnr >= STL_MAXBRDS)
Alan Cox4ac43602006-06-28 04:26:52 -07004497 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004498 brdp = stli_brds[brdnr];
4499 if (!brdp)
Alan Cox4ac43602006-06-28 04:26:52 -07004500 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004501 if (brdp->state == 0)
Alan Cox4ac43602006-06-28 04:26:52 -07004502 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004503
4504 switch (cmd) {
4505 case STL_BINTR:
4506 EBRDINTR(brdp);
4507 break;
4508 case STL_BSTART:
4509 rc = stli_startbrd(brdp);
4510 break;
4511 case STL_BSTOP:
4512 brdp->state &= ~BST_STARTED;
4513 break;
4514 case STL_BRESET:
4515 brdp->state &= ~BST_STARTED;
4516 EBRDRESET(brdp);
4517 if (stli_shared == 0) {
4518 if (brdp->reenable != NULL)
4519 (* brdp->reenable)(brdp);
4520 }
4521 break;
4522 default:
4523 rc = -ENOIOCTLCMD;
4524 break;
4525 }
Alan Cox4ac43602006-06-28 04:26:52 -07004526 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004527}
4528
Jeff Dikeb68e31d2006-10-02 02:17:18 -07004529static const struct tty_operations stli_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004530 .open = stli_open,
4531 .close = stli_close,
4532 .write = stli_write,
4533 .put_char = stli_putchar,
4534 .flush_chars = stli_flushchars,
4535 .write_room = stli_writeroom,
4536 .chars_in_buffer = stli_charsinbuffer,
4537 .ioctl = stli_ioctl,
4538 .set_termios = stli_settermios,
4539 .throttle = stli_throttle,
4540 .unthrottle = stli_unthrottle,
4541 .stop = stli_stop,
4542 .start = stli_start,
4543 .hangup = stli_hangup,
4544 .flush_buffer = stli_flushbuffer,
4545 .break_ctl = stli_breakctl,
4546 .wait_until_sent = stli_waituntilsent,
4547 .send_xchar = stli_sendxchar,
4548 .read_proc = stli_readproc,
4549 .tiocmget = stli_tiocmget,
4550 .tiocmset = stli_tiocmset,
4551};
4552
4553/*****************************************************************************/
4554
Adrian Bunk672b2712006-06-30 01:55:30 -07004555static int __init stli_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004556{
4557 int i;
4558 printk(KERN_INFO "%s: version %s\n", stli_drvtitle, stli_drvversion);
4559
Alan Cox4ac43602006-06-28 04:26:52 -07004560 spin_lock_init(&stli_lock);
4561 spin_lock_init(&brd_lock);
4562
Linus Torvalds1da177e2005-04-16 15:20:36 -07004563 stli_initbrds();
4564
4565 stli_serial = alloc_tty_driver(STL_MAXBRDS * STL_MAXPORTS);
4566 if (!stli_serial)
4567 return -ENOMEM;
4568
4569/*
4570 * Allocate a temporary write buffer.
4571 */
Tobias Klauserb0b4ed72006-03-31 02:30:56 -08004572 stli_txcookbuf = kmalloc(STLI_TXBUFSIZE, GFP_KERNEL);
4573 if (!stli_txcookbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004574 printk(KERN_ERR "STALLION: failed to allocate memory "
4575 "(size=%d)\n", STLI_TXBUFSIZE);
4576
4577/*
4578 * Set up a character driver for the shared memory region. We need this
4579 * to down load the slave code image. Also it is a useful debugging tool.
4580 */
4581 if (register_chrdev(STL_SIOMEMMAJOR, "staliomem", &stli_fsiomem))
4582 printk(KERN_ERR "STALLION: failed to register serial memory "
4583 "device\n");
4584
gregkh@suse.deca8eca62005-03-23 09:53:09 -08004585 istallion_class = class_create(THIS_MODULE, "staliomem");
Greg Kroah-Hartman7c69ef72005-06-20 21:15:16 -07004586 for (i = 0; i < 4; i++)
Greg Kroah-Hartman53f46542005-10-27 22:25:43 -07004587 class_device_create(istallion_class, NULL,
4588 MKDEV(STL_SIOMEMMAJOR, i),
Linus Torvalds1da177e2005-04-16 15:20:36 -07004589 NULL, "staliomem%d", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004590
4591/*
4592 * Set up the tty driver structure and register us as a driver.
4593 */
4594 stli_serial->owner = THIS_MODULE;
4595 stli_serial->driver_name = stli_drvname;
4596 stli_serial->name = stli_serialname;
4597 stli_serial->major = STL_SERIALMAJOR;
4598 stli_serial->minor_start = 0;
4599 stli_serial->type = TTY_DRIVER_TYPE_SERIAL;
4600 stli_serial->subtype = SERIAL_TYPE_NORMAL;
4601 stli_serial->init_termios = stli_deftermios;
4602 stli_serial->flags = TTY_DRIVER_REAL_RAW;
4603 tty_set_operations(stli_serial, &stli_ops);
4604
4605 if (tty_register_driver(stli_serial)) {
4606 put_tty_driver(stli_serial);
4607 printk(KERN_ERR "STALLION: failed to register serial driver\n");
4608 return -EBUSY;
4609 }
Alan Cox4ac43602006-06-28 04:26:52 -07004610 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004611}
4612
4613/*****************************************************************************/