staging: dgap: remove unneeded status variables
[linux-2.6/btrfs-unstable.git] / drivers / staging / dgap / dgap.c
blobad5afbc1e69447e7d22443416f6d68fd73dc78bb
1 /*
2 * Copyright 2003 Digi International (www.digi.com)
3 * Scott H Kilau <Scott_Kilau at digi dot com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
8 * any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY, EXPRESS OR IMPLIED; without even the
12 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
13 * PURPOSE. See the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 * NOTE TO LINUX KERNEL HACKERS: DO NOT REFORMAT THIS CODE!
22 * This is shared code between Digi's CVS archive and the
23 * Linux Kernel sources.
24 * Changing the source just for reformatting needlessly breaks
25 * our CVS diff history.
27 * Send any bug fixes/changes to: Eng.Linux at digi dot com.
28 * Thank you.
33 * In the original out of kernel Digi dgap driver, firmware
34 * loading was done via user land to driver handshaking.
36 * For cards that support a concentrator (port expander),
37 * I believe the concentrator its self told the card which
38 * concentrator is actually attached and then that info
39 * was used to tell user land which concentrator firmware
40 * image was to be downloaded. I think even the BIOS or
41 * FEP images required could change with the connection
42 * of a particular concentrator.
44 * Since I have no access to any of these cards or
45 * concentrators, I cannot put the correct concentrator
46 * firmware file names into the firmware_info structure
47 * as is now done for the BIOS and FEP images.
49 * I think, but am not certain, that the cards supporting
50 * concentrators will function without them. So support
51 * of these cards has been left in this driver.
53 * In order to fully support those cards, they would
54 * either have to be acquired for dissection or maybe
55 * Digi International could provide some assistance.
57 #undef DIGI_CONCENTRATORS_SUPPORTED
59 #include <linux/kernel.h>
60 #include <linux/module.h>
61 #include <linux/pci.h>
62 #include <linux/delay.h> /* For udelay */
63 #include <linux/slab.h>
64 #include <linux/uaccess.h>
65 #include <linux/sched.h>
67 #include <linux/interrupt.h> /* For tasklet and interrupt structs/defines */
68 #include <linux/ctype.h>
69 #include <linux/tty.h>
70 #include <linux/tty_flip.h>
71 #include <linux/serial_reg.h>
72 #include <linux/io.h> /* For read[bwl]/write[bwl] */
74 #include <linux/string.h>
75 #include <linux/device.h>
76 #include <linux/kdev_t.h>
77 #include <linux/firmware.h>
79 #include "dgap.h"
81 #define init_MUTEX(sem) sema_init(sem, 1)
82 #define DECLARE_MUTEX(name) \
83 struct semaphore name = __SEMAPHORE_INITIALIZER(name, 1)
85 MODULE_LICENSE("GPL");
86 MODULE_AUTHOR("Digi International, http://www.digi.com");
87 MODULE_DESCRIPTION("Driver for the Digi International EPCA PCI based product line");
88 MODULE_SUPPORTED_DEVICE("dgap");
90 /**************************************************************************
92 * protos for this file
96 static int dgap_start(void);
97 static void dgap_init_globals(void);
98 static int dgap_found_board(struct pci_dev *pdev, int id);
99 static void dgap_cleanup_board(struct board_t *brd);
100 static void dgap_poll_handler(ulong dummy);
101 static int dgap_init_pci(void);
102 static int dgap_init_one(struct pci_dev *pdev, const struct pci_device_id *ent);
103 static void dgap_remove_one(struct pci_dev *dev);
104 static int dgap_probe1(struct pci_dev *pdev, int card_type);
105 static int dgap_do_remap(struct board_t *brd);
106 static irqreturn_t dgap_intr(int irq, void *voidbrd);
108 /* Our function prototypes */
109 static int dgap_tty_open(struct tty_struct *tty, struct file *file);
110 static void dgap_tty_close(struct tty_struct *tty, struct file *file);
111 static int dgap_block_til_ready(struct tty_struct *tty, struct file *file,
112 struct channel_t *ch);
113 static int dgap_tty_ioctl(struct tty_struct *tty, unsigned int cmd,
114 unsigned long arg);
115 static int dgap_tty_digigeta(struct tty_struct *tty,
116 struct digi_t __user *retinfo);
117 static int dgap_tty_digiseta(struct tty_struct *tty,
118 struct digi_t __user *new_info);
119 static int dgap_tty_digigetedelay(struct tty_struct *tty, int __user *retinfo);
120 static int dgap_tty_digisetedelay(struct tty_struct *tty, int __user *new_info);
121 static int dgap_tty_write_room(struct tty_struct *tty);
122 static int dgap_tty_chars_in_buffer(struct tty_struct *tty);
123 static void dgap_tty_start(struct tty_struct *tty);
124 static void dgap_tty_stop(struct tty_struct *tty);
125 static void dgap_tty_throttle(struct tty_struct *tty);
126 static void dgap_tty_unthrottle(struct tty_struct *tty);
127 static void dgap_tty_flush_chars(struct tty_struct *tty);
128 static void dgap_tty_flush_buffer(struct tty_struct *tty);
129 static void dgap_tty_hangup(struct tty_struct *tty);
130 static int dgap_wait_for_drain(struct tty_struct *tty);
131 static int dgap_set_modem_info(struct tty_struct *tty, unsigned int command,
132 unsigned int __user *value);
133 static int dgap_get_modem_info(struct channel_t *ch,
134 unsigned int __user *value);
135 static int dgap_tty_digisetcustombaud(struct tty_struct *tty,
136 int __user *new_info);
137 static int dgap_tty_digigetcustombaud(struct tty_struct *tty,
138 int __user *retinfo);
139 static int dgap_tty_tiocmget(struct tty_struct *tty);
140 static int dgap_tty_tiocmset(struct tty_struct *tty, unsigned int set,
141 unsigned int clear);
142 static int dgap_tty_send_break(struct tty_struct *tty, int msec);
143 static void dgap_tty_wait_until_sent(struct tty_struct *tty, int timeout);
144 static int dgap_tty_write(struct tty_struct *tty, const unsigned char *buf,
145 int count);
146 static void dgap_tty_set_termios(struct tty_struct *tty,
147 struct ktermios *old_termios);
148 static int dgap_tty_put_char(struct tty_struct *tty, unsigned char c);
149 static void dgap_tty_send_xchar(struct tty_struct *tty, char ch);
151 static int dgap_tty_register(struct board_t *brd);
152 static int dgap_tty_init(struct board_t *);
153 static void dgap_tty_uninit(struct board_t *);
154 static void dgap_carrier(struct channel_t *ch);
155 static void dgap_input(struct channel_t *ch);
158 * Our function prototypes from dgap_fep5
160 static void dgap_cmdw_ext(struct channel_t *ch, u16 cmd, u16 word, uint ncmds);
161 static int dgap_event(struct board_t *bd);
163 static void dgap_poll_tasklet(unsigned long data);
164 static void dgap_cmdb(struct channel_t *ch, uchar cmd, uchar byte1,
165 uchar byte2, uint ncmds);
166 static void dgap_cmdw(struct channel_t *ch, uchar cmd, u16 word, uint ncmds);
167 static void dgap_wmove(struct channel_t *ch, char *buf, uint cnt);
168 static int dgap_param(struct tty_struct *tty);
169 static void dgap_parity_scan(struct channel_t *ch, unsigned char *cbuf,
170 unsigned char *fbuf, int *len);
171 static uint dgap_get_custom_baud(struct channel_t *ch);
172 static void dgap_firmware_reset_port(struct channel_t *ch);
175 * Function prototypes from dgap_parse.c.
177 static int dgap_gettok(char **in, struct cnode *p);
178 static char *dgap_getword(char **in);
179 static char *dgap_savestring(char *s);
180 static struct cnode *dgap_newnode(int t);
181 static int dgap_checknode(struct cnode *p);
182 static void dgap_err(char *s);
185 * Function prototypes from dgap_sysfs.h
187 struct board_t;
188 struct channel_t;
189 struct un_t;
190 struct pci_driver;
191 struct class_device;
193 static void dgap_create_ports_sysfiles(struct board_t *bd);
194 static void dgap_remove_ports_sysfiles(struct board_t *bd);
196 static int dgap_create_driver_sysfiles(struct pci_driver *);
197 static void dgap_remove_driver_sysfiles(struct pci_driver *);
199 static void dgap_create_tty_sysfs(struct un_t *un, struct device *c);
200 static void dgap_remove_tty_sysfs(struct device *c);
203 * Function prototypes from dgap_parse.h
205 static int dgap_parsefile(char **in, int Remove);
206 static struct cnode *dgap_find_config(int type, int bus, int slot);
207 static uint dgap_config_get_number_of_ports(struct board_t *bd);
208 static char *dgap_create_config_string(struct board_t *bd, char *string);
209 static uint dgap_config_get_useintr(struct board_t *bd);
210 static uint dgap_config_get_altpin(struct board_t *bd);
212 static int dgap_ms_sleep(ulong ms);
213 static void dgap_do_bios_load(struct board_t *brd, uchar __user *ubios,
214 int len);
215 static void dgap_do_fep_load(struct board_t *brd, uchar __user *ufep, int len);
216 #ifdef DIGI_CONCENTRATORS_SUPPORTED
217 static void dgap_do_conc_load(struct board_t *brd, uchar *uaddr, int len);
218 #endif
219 static int dgap_after_config_loaded(int board);
220 static int dgap_finalize_board_init(struct board_t *brd);
222 static void dgap_get_vpd(struct board_t *brd);
223 static void dgap_do_reset_board(struct board_t *brd);
224 static void dgap_do_wait_for_bios(struct board_t *brd);
225 static void dgap_do_wait_for_fep(struct board_t *brd);
226 static int dgap_tty_register_ports(struct board_t *brd);
227 static int dgap_firmware_load(struct pci_dev *pdev, int card_type);
229 /* Driver unload function */
230 static void dgap_cleanup_module(void);
232 module_exit(dgap_cleanup_module);
235 * File operations permitted on Control/Management major.
237 static const struct file_operations DgapBoardFops = {
238 .owner = THIS_MODULE,
242 * Globals
244 static uint dgap_NumBoards;
245 static struct board_t *dgap_Board[MAXBOARDS];
246 static ulong dgap_poll_counter;
247 static char *dgap_config_buf;
248 static int dgap_driver_state = DRIVER_INITIALIZED;
249 DEFINE_SPINLOCK(dgap_dl_lock);
250 static wait_queue_head_t dgap_dl_wait;
251 static int dgap_dl_action;
252 static int dgap_poll_tick = 20; /* Poll interval - 20 ms */
255 * Static vars.
257 static struct class *dgap_class;
259 static struct board_t *dgap_BoardsByMajor[256];
260 static uint dgap_count = 500;
263 * Poller stuff
265 DEFINE_SPINLOCK(dgap_poll_lock); /* Poll scheduling lock */
266 static ulong dgap_poll_time; /* Time of next poll */
267 static uint dgap_poll_stop; /* Used to tell poller to stop */
268 static struct timer_list dgap_poll_timer;
271 SUPPORTED PRODUCTS
273 Card Model Number of Ports Interface
274 ----------------------------------------------------------------
275 Acceleport Xem 4 - 64 (EIA232 & EIA422)
276 Acceleport Xr 4 & 8 (EIA232)
277 Acceleport Xr 920 4 & 8 (EIA232)
278 Acceleport C/X 8 - 128 (EIA232)
279 Acceleport EPC/X 8 - 224 (EIA232)
280 Acceleport Xr/422 4 & 8 (EIA422)
281 Acceleport 2r/920 2 (EIA232)
282 Acceleport 4r/920 4 (EIA232)
283 Acceleport 8r/920 8 (EIA232)
285 IBM 8-Port Asynchronous PCI Adapter (EIA232)
286 IBM 128-Port Asynchronous PCI Adapter (EIA232 & EIA422)
289 static struct pci_device_id dgap_pci_tbl[] = {
290 { DIGI_VID, PCI_DEV_XEM_DID, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
291 { DIGI_VID, PCI_DEV_CX_DID, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 1 },
292 { DIGI_VID, PCI_DEV_CX_IBM_DID, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 2 },
293 { DIGI_VID, PCI_DEV_EPCJ_DID, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 3 },
294 { DIGI_VID, PCI_DEV_920_2_DID, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 4 },
295 { DIGI_VID, PCI_DEV_920_4_DID, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 5 },
296 { DIGI_VID, PCI_DEV_920_8_DID, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 6 },
297 { DIGI_VID, PCI_DEV_XR_DID, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 7 },
298 { DIGI_VID, PCI_DEV_XRJ_DID, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 8 },
299 { DIGI_VID, PCI_DEV_XR_422_DID, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 9 },
300 { DIGI_VID, PCI_DEV_XR_IBM_DID, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 10 },
301 { DIGI_VID, PCI_DEV_XR_SAIP_DID, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 11 },
302 { DIGI_VID, PCI_DEV_XR_BULL_DID, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 12 },
303 { DIGI_VID, PCI_DEV_920_8_HP_DID, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 13 },
304 { DIGI_VID, PCI_DEV_XEM_HP_DID, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 14 },
305 {0,} /* 0 terminated list. */
307 MODULE_DEVICE_TABLE(pci, dgap_pci_tbl);
310 * A generic list of Product names, PCI Vendor ID, and PCI Device ID.
312 struct board_id {
313 uint config_type;
314 uchar *name;
315 uint maxports;
316 uint dpatype;
319 static struct board_id dgap_Ids[] = {
320 { PPCM, PCI_DEV_XEM_NAME, 64, (T_PCXM|T_PCLITE|T_PCIBUS) },
321 { PCX, PCI_DEV_CX_NAME, 128, (T_CX|T_PCIBUS) },
322 { PCX, PCI_DEV_CX_IBM_NAME, 128, (T_CX|T_PCIBUS) },
323 { PEPC, PCI_DEV_EPCJ_NAME, 224, (T_EPC|T_PCIBUS) },
324 { APORT2_920P, PCI_DEV_920_2_NAME, 2, (T_PCXR|T_PCLITE|T_PCIBUS) },
325 { APORT4_920P, PCI_DEV_920_4_NAME, 4, (T_PCXR|T_PCLITE|T_PCIBUS) },
326 { APORT8_920P, PCI_DEV_920_8_NAME, 8, (T_PCXR|T_PCLITE|T_PCIBUS) },
327 { PAPORT8, PCI_DEV_XR_NAME, 8, (T_PCXR|T_PCLITE|T_PCIBUS) },
328 { PAPORT8, PCI_DEV_XRJ_NAME, 8, (T_PCXR|T_PCLITE|T_PCIBUS) },
329 { PAPORT8, PCI_DEV_XR_422_NAME, 8, (T_PCXR|T_PCLITE|T_PCIBUS) },
330 { PAPORT8, PCI_DEV_XR_IBM_NAME, 8, (T_PCXR|T_PCLITE|T_PCIBUS) },
331 { PAPORT8, PCI_DEV_XR_SAIP_NAME, 8, (T_PCXR|T_PCLITE|T_PCIBUS) },
332 { PAPORT8, PCI_DEV_XR_BULL_NAME, 8, (T_PCXR|T_PCLITE|T_PCIBUS) },
333 { APORT8_920P, PCI_DEV_920_8_HP_NAME, 8, (T_PCXR|T_PCLITE|T_PCIBUS) },
334 { PPCM, PCI_DEV_XEM_HP_NAME, 64, (T_PCXM|T_PCLITE|T_PCIBUS) },
335 {0,} /* 0 terminated list. */
338 static struct pci_driver dgap_driver = {
339 .name = "dgap",
340 .probe = dgap_init_one,
341 .id_table = dgap_pci_tbl,
342 .remove = dgap_remove_one,
345 struct firmware_info {
346 uchar *conf_name; /* dgap.conf */
347 uchar *bios_name; /* BIOS filename */
348 uchar *fep_name; /* FEP filename */
349 uchar *con_name; /* Concentrator filename FIXME*/
350 int num; /* sequence number */
354 * Firmware - BIOS, FEP, and CONC filenames
356 static struct firmware_info fw_info[] = {
357 { "dgap/dgap.conf", "dgap/sxbios.bin", "dgap/sxfep.bin", 0, 0 },
358 { "dgap/dgap.conf", "dgap/cxpbios.bin", "dgap/cxpfep.bin", 0, 1 },
359 { "dgap/dgap.conf", "dgap/cxpbios.bin", "dgap/cxpfep.bin", 0, 2 },
360 { "dgap/dgap.conf", "dgap/pcibios.bin", "dgap/pcifep.bin", 0, 3 },
361 { "dgap/dgap.conf", "dgap/xrbios.bin", "dgap/xrfep.bin", 0, 4 },
362 { "dgap/dgap.conf", "dgap/xrbios.bin", "dgap/xrfep.bin", 0, 5 },
363 { "dgap/dgap.conf", "dgap/xrbios.bin", "dgap/xrfep.bin", 0, 6 },
364 { "dgap/dgap.conf", "dgap/xrbios.bin", "dgap/xrfep.bin", 0, 7 },
365 { "dgap/dgap.conf", "dgap/xrbios.bin", "dgap/xrfep.bin", 0, 8 },
366 { "dgap/dgap.conf", "dgap/xrbios.bin", "dgap/xrfep.bin", 0, 9 },
367 { "dgap/dgap.conf", "dgap/xrbios.bin", "dgap/xrfep.bin", 0, 10 },
368 { "dgap/dgap.conf", "dgap/xrbios.bin", "dgap/xrfep.bin", 0, 11 },
369 { "dgap/dgap.conf", "dgap/xrbios.bin", "dgap/xrfep.bin", 0, 12 },
370 { "dgap/dgap.conf", "dgap/xrbios.bin", "dgap/xrfep.bin", 0, 13 },
371 { "dgap/dgap.conf", "dgap/sxbios.bin", "dgap/sxfep.bin", 0, 14 },
372 {0,}
375 static char *dgap_driver_state_text[] = {
376 "Driver Initialized",
377 "Driver needs configuration load.",
378 "Driver requested configuration from download daemon.",
379 "Driver Ready."
383 * Default transparent print information.
385 static struct digi_t dgap_digi_init = {
386 .digi_flags = DIGI_COOK, /* Flags */
387 .digi_maxcps = 100, /* Max CPS */
388 .digi_maxchar = 50, /* Max chars in print queue */
389 .digi_bufsize = 100, /* Printer buffer size */
390 .digi_onlen = 4, /* size of printer on string */
391 .digi_offlen = 4, /* size of printer off string */
392 .digi_onstr = "\033[5i", /* ANSI printer on string ] */
393 .digi_offstr = "\033[4i", /* ANSI printer off string ] */
394 .digi_term = "ansi" /* default terminal type */
398 * Define a local default termios struct. All ports will be created
399 * with this termios initially.
401 * This defines a raw port at 9600 baud, 8 data bits, no parity,
402 * 1 stop bit.
405 static struct ktermios DgapDefaultTermios = {
406 .c_iflag = (DEFAULT_IFLAGS), /* iflags */
407 .c_oflag = (DEFAULT_OFLAGS), /* oflags */
408 .c_cflag = (DEFAULT_CFLAGS), /* cflags */
409 .c_lflag = (DEFAULT_LFLAGS), /* lflags */
410 .c_cc = INIT_C_CC,
411 .c_line = 0,
414 static const struct tty_operations dgap_tty_ops = {
415 .open = dgap_tty_open,
416 .close = dgap_tty_close,
417 .write = dgap_tty_write,
418 .write_room = dgap_tty_write_room,
419 .flush_buffer = dgap_tty_flush_buffer,
420 .chars_in_buffer = dgap_tty_chars_in_buffer,
421 .flush_chars = dgap_tty_flush_chars,
422 .ioctl = dgap_tty_ioctl,
423 .set_termios = dgap_tty_set_termios,
424 .stop = dgap_tty_stop,
425 .start = dgap_tty_start,
426 .throttle = dgap_tty_throttle,
427 .unthrottle = dgap_tty_unthrottle,
428 .hangup = dgap_tty_hangup,
429 .put_char = dgap_tty_put_char,
430 .tiocmget = dgap_tty_tiocmget,
431 .tiocmset = dgap_tty_tiocmset,
432 .break_ctl = dgap_tty_send_break,
433 .wait_until_sent = dgap_tty_wait_until_sent,
434 .send_xchar = dgap_tty_send_xchar
438 * Our needed internal static variables from dgap_parse.c
440 static struct cnode dgap_head;
441 #define MAXCWORD 200
442 static char dgap_cword[MAXCWORD];
444 struct toklist {
445 int token;
446 char *string;
449 static struct toklist dgap_tlist[] = {
450 { BEGIN, "config_begin" },
451 { END, "config_end" },
452 { BOARD, "board" },
453 { PCX, "Digi_AccelePort_C/X_PCI" },
454 { PEPC, "Digi_AccelePort_EPC/X_PCI" },
455 { PPCM, "Digi_AccelePort_Xem_PCI" },
456 { APORT2_920P, "Digi_AccelePort_2r_920_PCI" },
457 { APORT4_920P, "Digi_AccelePort_4r_920_PCI" },
458 { APORT8_920P, "Digi_AccelePort_8r_920_PCI" },
459 { PAPORT4, "Digi_AccelePort_4r_PCI(EIA-232/RS-422)" },
460 { PAPORT8, "Digi_AccelePort_8r_PCI(EIA-232/RS-422)" },
461 { IO, "io" },
462 { PCIINFO, "pciinfo" },
463 { LINE, "line" },
464 { CONC, "conc" },
465 { CONC, "concentrator" },
466 { CX, "cx" },
467 { CX, "ccon" },
468 { EPC, "epccon" },
469 { EPC, "epc" },
470 { MOD, "module" },
471 { ID, "id" },
472 { STARTO, "start" },
473 { SPEED, "speed" },
474 { CABLE, "cable" },
475 { CONNECT, "connect" },
476 { METHOD, "method" },
477 { STATUS, "status" },
478 { CUSTOM, "Custom" },
479 { BASIC, "Basic" },
480 { MEM, "mem" },
481 { MEM, "memory" },
482 { PORTS, "ports" },
483 { MODEM, "modem" },
484 { NPORTS, "nports" },
485 { TTYN, "ttyname" },
486 { CU, "cuname" },
487 { PRINT, "prname" },
488 { CMAJOR, "major" },
489 { ALTPIN, "altpin" },
490 { USEINTR, "useintr" },
491 { TTSIZ, "ttysize" },
492 { CHSIZ, "chsize" },
493 { BSSIZ, "boardsize" },
494 { UNTSIZ, "schedsize" },
495 { F2SIZ, "f2200size" },
496 { VPSIZ, "vpixsize" },
497 { 0, NULL }
500 /************************************************************************
502 * Driver load/unload functions
504 ************************************************************************/
507 * init_module()
509 * Module load. This is where it all starts.
511 static int dgap_init_module(void)
513 int rc = 0;
515 pr_info("%s, Digi International Part Number %s\n", DG_NAME, DG_PART);
517 dgap_driver_state = DRIVER_NEED_CONFIG_LOAD;
519 rc = dgap_start();
520 if (rc)
521 return rc;
523 rc = dgap_init_pci();
524 if (rc)
525 goto err_cleanup;
527 rc = dgap_create_driver_sysfiles(&dgap_driver);
528 if (rc)
529 goto err_cleanup;
531 dgap_driver_state = DRIVER_READY;
533 return 0;
535 err_cleanup:
537 dgap_cleanup_module();
539 return rc;
541 module_init(dgap_init_module);
544 * Start of driver.
546 static int dgap_start(void)
548 int rc = 0;
549 unsigned long flags;
552 * make sure that the globals are
553 * init'd before we do anything else
555 dgap_init_globals();
557 dgap_NumBoards = 0;
559 pr_info("For the tools package please visit http://www.digi.com\n");
562 * Register our base character device into the kernel.
566 * Register management/dpa devices
568 rc = register_chrdev(DIGI_DGAP_MAJOR, "dgap", &DgapBoardFops);
569 if (rc < 0)
570 return rc;
572 dgap_class = class_create(THIS_MODULE, "dgap_mgmt");
573 device_create(dgap_class, NULL,
574 MKDEV(DIGI_DGAP_MAJOR, 0),
575 NULL, "dgap_mgmt");
577 /* Start the poller */
578 DGAP_LOCK(dgap_poll_lock, flags);
579 init_timer(&dgap_poll_timer);
580 dgap_poll_timer.function = dgap_poll_handler;
581 dgap_poll_timer.data = 0;
582 dgap_poll_time = jiffies + dgap_jiffies_from_ms(dgap_poll_tick);
583 dgap_poll_timer.expires = dgap_poll_time;
584 DGAP_UNLOCK(dgap_poll_lock, flags);
586 add_timer(&dgap_poll_timer);
588 dgap_driver_state = DRIVER_NEED_CONFIG_LOAD;
590 return rc;
594 * Register pci driver, and return how many boards we have.
596 static int dgap_init_pci(void)
598 return pci_register_driver(&dgap_driver);
601 /* returns count (>= 0), or negative on error */
602 static int dgap_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
604 int rc;
606 /* wake up and enable device */
607 rc = pci_enable_device(pdev);
609 if (rc < 0) {
610 rc = -EIO;
611 } else {
612 rc = dgap_probe1(pdev, ent->driver_data);
613 if (rc == 0) {
614 dgap_NumBoards++;
615 rc = dgap_firmware_load(pdev, ent->driver_data);
618 return rc;
621 static int dgap_probe1(struct pci_dev *pdev, int card_type)
623 return dgap_found_board(pdev, card_type);
626 static void dgap_remove_one(struct pci_dev *dev)
628 /* Do Nothing */
632 * dgap_cleanup_module()
634 * Module unload. This is where it all ends.
636 static void dgap_cleanup_module(void)
638 int i;
639 ulong lock_flags;
641 DGAP_LOCK(dgap_poll_lock, lock_flags);
642 dgap_poll_stop = 1;
643 DGAP_UNLOCK(dgap_poll_lock, lock_flags);
645 /* Turn off poller right away. */
646 del_timer_sync(&dgap_poll_timer);
648 dgap_remove_driver_sysfiles(&dgap_driver);
650 device_destroy(dgap_class, MKDEV(DIGI_DGAP_MAJOR, 0));
651 class_destroy(dgap_class);
652 unregister_chrdev(DIGI_DGAP_MAJOR, "dgap");
654 kfree(dgap_config_buf);
656 for (i = 0; i < dgap_NumBoards; ++i) {
657 dgap_remove_ports_sysfiles(dgap_Board[i]);
658 dgap_tty_uninit(dgap_Board[i]);
659 dgap_cleanup_board(dgap_Board[i]);
662 if (dgap_NumBoards)
663 pci_unregister_driver(&dgap_driver);
667 * dgap_cleanup_board()
669 * Free all the memory associated with a board
671 static void dgap_cleanup_board(struct board_t *brd)
673 int i = 0;
675 if (!brd || brd->magic != DGAP_BOARD_MAGIC)
676 return;
678 if (brd->intr_used && brd->irq)
679 free_irq(brd->irq, brd);
681 tasklet_kill(&brd->helper_tasklet);
683 if (brd->re_map_port) {
684 release_mem_region(brd->membase + 0x200000, 0x200000);
685 iounmap(brd->re_map_port);
686 brd->re_map_port = NULL;
689 if (brd->re_map_membase) {
690 release_mem_region(brd->membase, 0x200000);
691 iounmap(brd->re_map_membase);
692 brd->re_map_membase = NULL;
695 /* Free all allocated channels structs */
696 for (i = 0; i < MAXPORTS ; i++)
697 kfree(brd->channels[i]);
699 kfree(brd->flipbuf);
700 kfree(brd->flipflagbuf);
702 dgap_Board[brd->boardnum] = NULL;
704 kfree(brd);
708 * dgap_found_board()
710 * A board has been found, init it.
712 static int dgap_found_board(struct pci_dev *pdev, int id)
714 struct board_t *brd;
715 unsigned int pci_irq;
716 int i = 0;
718 /* get the board structure and prep it */
719 brd = kzalloc(sizeof(struct board_t), GFP_KERNEL);
720 if (!brd)
721 return -ENOMEM;
723 dgap_Board[dgap_NumBoards] = brd;
725 /* store the info for the board we've found */
726 brd->magic = DGAP_BOARD_MAGIC;
727 brd->boardnum = dgap_NumBoards;
728 brd->firstminor = 0;
729 brd->vendor = dgap_pci_tbl[id].vendor;
730 brd->device = dgap_pci_tbl[id].device;
731 brd->pdev = pdev;
732 brd->pci_bus = pdev->bus->number;
733 brd->pci_slot = PCI_SLOT(pdev->devfn);
734 brd->name = dgap_Ids[id].name;
735 brd->maxports = dgap_Ids[id].maxports;
736 brd->type = dgap_Ids[id].config_type;
737 brd->dpatype = dgap_Ids[id].dpatype;
738 brd->dpastatus = BD_NOFEP;
739 init_waitqueue_head(&brd->state_wait);
741 DGAP_SPINLOCK_INIT(brd->bd_lock);
743 brd->state = BOARD_FOUND;
744 brd->runwait = 0;
745 brd->inhibit_poller = FALSE;
746 brd->wait_for_bios = 0;
747 brd->wait_for_fep = 0;
749 for (i = 0; i < MAXPORTS; i++)
750 brd->channels[i] = NULL;
752 /* store which card & revision we have */
753 pci_read_config_word(pdev, PCI_SUBSYSTEM_VENDOR_ID, &brd->subvendor);
754 pci_read_config_word(pdev, PCI_SUBSYSTEM_ID, &brd->subdevice);
755 pci_read_config_byte(pdev, PCI_REVISION_ID, &brd->rev);
757 pci_irq = pdev->irq;
758 brd->irq = pci_irq;
760 /* get the PCI Base Address Registers */
762 /* Xr Jupiter and EPC use BAR 2 */
763 if (brd->device == PCI_DEV_XRJ_DID || brd->device == PCI_DEV_EPCJ_DID) {
764 brd->membase = pci_resource_start(pdev, 2);
765 brd->membase_end = pci_resource_end(pdev, 2);
767 /* Everyone else uses BAR 0 */
768 else {
769 brd->membase = pci_resource_start(pdev, 0);
770 brd->membase_end = pci_resource_end(pdev, 0);
773 if (!brd->membase)
774 return -ENODEV;
776 if (brd->membase & 1)
777 brd->membase &= ~3;
778 else
779 brd->membase &= ~15;
782 * On the PCI boards, there is no IO space allocated
783 * The I/O registers will be in the first 3 bytes of the
784 * upper 2MB of the 4MB memory space. The board memory
785 * will be mapped into the low 2MB of the 4MB memory space
787 brd->port = brd->membase + PCI_IO_OFFSET;
788 brd->port_end = brd->port + PCI_IO_SIZE;
791 * Special initialization for non-PLX boards
793 if (brd->device != PCI_DEV_XRJ_DID && brd->device != PCI_DEV_EPCJ_DID) {
794 unsigned short cmd;
796 pci_write_config_byte(pdev, 0x40, 0);
797 pci_write_config_byte(pdev, 0x46, 0);
799 /* Limit burst length to 2 doubleword transactions */
800 pci_write_config_byte(pdev, 0x42, 1);
803 * Enable IO and mem if not already done.
804 * This was needed for support on Itanium.
806 pci_read_config_word(pdev, PCI_COMMAND, &cmd);
807 cmd |= (PCI_COMMAND_IO | PCI_COMMAND_MEMORY);
808 pci_write_config_word(pdev, PCI_COMMAND, cmd);
811 /* init our poll helper tasklet */
812 tasklet_init(&brd->helper_tasklet, dgap_poll_tasklet,
813 (unsigned long) brd);
815 i = dgap_do_remap(brd);
816 if (i)
817 brd->state = BOARD_FAILED;
818 else
819 brd->state = NEED_RESET;
821 return 0;
825 static int dgap_finalize_board_init(struct board_t *brd)
827 int rc;
829 if (!brd || brd->magic != DGAP_BOARD_MAGIC)
830 return -ENODEV;
832 brd->use_interrupts = dgap_config_get_useintr(brd);
835 * Set up our interrupt handler if we are set to do interrupts.
837 if (brd->use_interrupts && brd->irq) {
839 rc = request_irq(brd->irq, dgap_intr, IRQF_SHARED, "DGAP", brd);
841 if (rc)
842 brd->intr_used = 0;
843 else
844 brd->intr_used = 1;
845 } else {
846 brd->intr_used = 0;
849 return 0;
852 static int dgap_firmware_load(struct pci_dev *pdev, int card_type)
854 struct board_t *brd = dgap_Board[dgap_NumBoards - 1];
855 const struct firmware *fw;
856 int ret;
858 dgap_get_vpd(brd);
859 dgap_do_reset_board(brd);
861 if ((fw_info[card_type].conf_name) &&
862 (dgap_driver_state == DRIVER_NEED_CONFIG_LOAD)) {
863 ret = request_firmware(&fw, fw_info[card_type].conf_name,
864 &pdev->dev);
865 if (ret) {
866 pr_err("dgap: config file %s not found\n",
867 fw_info[card_type].conf_name);
868 return ret;
870 if (!dgap_config_buf) {
871 dgap_config_buf = kmalloc(fw->size + 1, GFP_ATOMIC);
872 if (!dgap_config_buf) {
873 release_firmware(fw);
874 return -ENOMEM;
878 memcpy(dgap_config_buf, fw->data, fw->size);
879 release_firmware(fw);
880 dgap_config_buf[fw->size + 1] = '\0';
882 if (dgap_parsefile(&dgap_config_buf, TRUE) != 0)
883 return -EINVAL;
885 dgap_driver_state = -1;
888 ret = dgap_after_config_loaded(brd->boardnum);
889 if (ret)
890 return ret;
892 * Match this board to a config the user created for us.
894 brd->bd_config =
895 dgap_find_config(brd->type, brd->pci_bus, brd->pci_slot);
898 * Because the 4 port Xr products share the same PCI ID
899 * as the 8 port Xr products, if we receive a NULL config
900 * back, and this is a PAPORT8 board, retry with a
901 * PAPORT4 attempt as well.
903 if (brd->type == PAPORT8 && !brd->bd_config)
904 brd->bd_config =
905 dgap_find_config(PAPORT4, brd->pci_bus, brd->pci_slot);
907 if (!brd->bd_config) {
908 pr_err("dgap: No valid configuration found\n");
909 return -EINVAL;
912 dgap_tty_register(brd);
913 dgap_finalize_board_init(brd);
915 if (fw_info[card_type].bios_name) {
916 ret = request_firmware(&fw, fw_info[card_type].bios_name,
917 &pdev->dev);
918 if (ret) {
919 pr_err("dgap: bios file %s not found\n",
920 fw_info[card_type].bios_name);
921 return ret;
923 dgap_do_bios_load(brd, (char *)fw->data, fw->size);
924 release_firmware(fw);
926 /* Wait for BIOS to test board... */
927 dgap_do_wait_for_bios(brd);
929 if (brd->state != FINISHED_BIOS_LOAD)
930 return -ENXIO;
933 if (fw_info[card_type].fep_name) {
934 ret = request_firmware(&fw, fw_info[card_type].fep_name,
935 &pdev->dev);
936 if (ret) {
937 pr_err("dgap: fep file %s not found\n",
938 fw_info[card_type].fep_name);
939 return ret;
941 dgap_do_fep_load(brd, (char *)fw->data, fw->size);
942 release_firmware(fw);
944 /* Wait for FEP to load on board... */
945 dgap_do_wait_for_fep(brd);
947 if (brd->state != FINISHED_FEP_LOAD)
948 return -ENXIO;
951 #ifdef DIGI_CONCENTRATORS_SUPPORTED
953 * If this is a CX or EPCX, we need to see if the firmware
954 * is requesting a concentrator image from us.
956 if ((bd->type == PCX) || (bd->type == PEPC)) {
957 chk_addr = (u16 *) (vaddr + DOWNREQ);
958 /* Nonzero if FEP is requesting concentrator image. */
959 check = readw(chk_addr);
960 vaddr = brd->re_map_membase;
963 if (fw_info[card_type].con_name && check && vaddr) {
964 ret = request_firmware(&fw, fw_info[card_type].con_name,
965 &pdev->dev);
966 if (ret) {
967 pr_err("dgap: conc file %s not found\n",
968 fw_info[card_type].con_name);
969 return ret;
971 /* Put concentrator firmware loading code here */
972 offset = readw((u16 *) (vaddr + DOWNREQ));
973 memcpy_toio(offset, fw->data, fw->size);
975 dgap_do_conc_load(brd, (char *)fw->data, fw->size)
976 release_firmware(fw);
978 #endif
980 * Do tty device initialization.
982 ret = dgap_tty_init(brd);
983 if (ret < 0) {
984 dgap_tty_uninit(brd);
985 return ret;
988 ret = dgap_tty_register_ports(brd);
989 if (ret)
990 return ret;
992 brd->state = BOARD_READY;
993 brd->dpastatus = BD_RUNNING;
995 return 0;
999 * Remap PCI memory.
1001 static int dgap_do_remap(struct board_t *brd)
1003 if (!brd || brd->magic != DGAP_BOARD_MAGIC)
1004 return -ENXIO;
1006 if (!request_mem_region(brd->membase, 0x200000, "dgap"))
1007 return -ENOMEM;
1009 if (!request_mem_region(brd->membase + PCI_IO_OFFSET, 0x200000,
1010 "dgap")) {
1011 release_mem_region(brd->membase, 0x200000);
1012 return -ENOMEM;
1015 brd->re_map_membase = ioremap(brd->membase, 0x200000);
1016 if (!brd->re_map_membase) {
1017 release_mem_region(brd->membase, 0x200000);
1018 release_mem_region(brd->membase + PCI_IO_OFFSET, 0x200000);
1019 return -ENOMEM;
1022 brd->re_map_port = ioremap((brd->membase + PCI_IO_OFFSET), 0x200000);
1023 if (!brd->re_map_port) {
1024 release_mem_region(brd->membase, 0x200000);
1025 release_mem_region(brd->membase + PCI_IO_OFFSET, 0x200000);
1026 iounmap(brd->re_map_membase);
1027 return -ENOMEM;
1030 return 0;
1033 /*****************************************************************************
1035 * Function:
1037 * dgap_poll_handler
1039 * Author:
1041 * Scott H Kilau
1043 * Parameters:
1045 * dummy -- ignored
1047 * Return Values:
1049 * none
1051 * Description:
1053 * As each timer expires, it determines (a) whether the "transmit"
1054 * waiter needs to be woken up, and (b) whether the poller needs to
1055 * be rescheduled.
1057 ******************************************************************************/
1059 static void dgap_poll_handler(ulong dummy)
1061 int i;
1062 struct board_t *brd;
1063 unsigned long lock_flags;
1064 ulong new_time;
1066 dgap_poll_counter++;
1069 * Do not start the board state machine until
1070 * driver tells us its up and running, and has
1071 * everything it needs.
1073 if (dgap_driver_state != DRIVER_READY)
1074 goto schedule_poller;
1077 * If we have just 1 board, or the system is not SMP,
1078 * then use the typical old style poller.
1079 * Otherwise, use our new tasklet based poller, which should
1080 * speed things up for multiple boards.
1082 if ((dgap_NumBoards == 1) || (num_online_cpus() <= 1)) {
1083 for (i = 0; i < dgap_NumBoards; i++) {
1085 brd = dgap_Board[i];
1087 if (brd->state == BOARD_FAILED)
1088 continue;
1089 if (!brd->intr_running)
1090 /* Call the real board poller directly */
1091 dgap_poll_tasklet((unsigned long) brd);
1093 } else {
1095 * Go thru each board, kicking off a
1096 * tasklet for each if needed
1098 for (i = 0; i < dgap_NumBoards; i++) {
1099 brd = dgap_Board[i];
1102 * Attempt to grab the board lock.
1104 * If we can't get it, no big deal, the next poll
1105 * will get it. Basically, I just really don't want
1106 * to spin in here, because I want to kick off my
1107 * tasklets as fast as I can, and then get out the
1108 * poller.
1110 if (!spin_trylock(&brd->bd_lock))
1111 continue;
1114 * If board is in a failed state, don't bother
1115 * scheduling a tasklet
1117 if (brd->state == BOARD_FAILED) {
1118 spin_unlock(&brd->bd_lock);
1119 continue;
1122 /* Schedule a poll helper task */
1123 if (!brd->intr_running)
1124 tasklet_schedule(&brd->helper_tasklet);
1127 * Can't do DGAP_UNLOCK here, as we don't have
1128 * lock_flags because we did a trylock above.
1130 spin_unlock(&brd->bd_lock);
1134 schedule_poller:
1137 * Schedule ourself back at the nominal wakeup interval.
1139 DGAP_LOCK(dgap_poll_lock, lock_flags);
1140 dgap_poll_time += dgap_jiffies_from_ms(dgap_poll_tick);
1142 new_time = dgap_poll_time - jiffies;
1144 if ((ulong) new_time >= 2 * dgap_poll_tick) {
1145 dgap_poll_time =
1146 jiffies + dgap_jiffies_from_ms(dgap_poll_tick);
1149 dgap_poll_timer.function = dgap_poll_handler;
1150 dgap_poll_timer.data = 0;
1151 dgap_poll_timer.expires = dgap_poll_time;
1152 DGAP_UNLOCK(dgap_poll_lock, lock_flags);
1154 if (!dgap_poll_stop)
1155 add_timer(&dgap_poll_timer);
1159 * dgap_intr()
1161 * Driver interrupt handler.
1163 static irqreturn_t dgap_intr(int irq, void *voidbrd)
1165 struct board_t *brd = (struct board_t *) voidbrd;
1167 if (!brd)
1168 return IRQ_NONE;
1171 * Check to make sure its for us.
1173 if (brd->magic != DGAP_BOARD_MAGIC)
1174 return IRQ_NONE;
1176 brd->intr_count++;
1179 * Schedule tasklet to run at a better time.
1181 tasklet_schedule(&brd->helper_tasklet);
1182 return IRQ_HANDLED;
1186 * dgap_init_globals()
1188 * This is where we initialize the globals from the static insmod
1189 * configuration variables. These are declared near the head of
1190 * this file.
1192 static void dgap_init_globals(void)
1194 int i = 0;
1196 for (i = 0; i < MAXBOARDS; i++)
1197 dgap_Board[i] = NULL;
1199 init_timer(&dgap_poll_timer);
1201 init_waitqueue_head(&dgap_dl_wait);
1202 dgap_dl_action = 0;
1205 /************************************************************************
1207 * Utility functions
1209 ************************************************************************/
1212 * dgap_ms_sleep()
1214 * Put the driver to sleep for x ms's
1216 * Returns 0 if timed out, !0 (showing signal) if interrupted by a signal.
1218 static int dgap_ms_sleep(ulong ms)
1220 current->state = TASK_INTERRUPTIBLE;
1221 schedule_timeout((ms * HZ) / 1000);
1222 return signal_pending(current);
1225 /************************************************************************
1227 * TTY Initialization/Cleanup Functions
1229 ************************************************************************/
1232 * dgap_tty_register()
1234 * Init the tty subsystem for this board.
1236 static int dgap_tty_register(struct board_t *brd)
1238 int rc = 0;
1240 brd->SerialDriver = alloc_tty_driver(MAXPORTS);
1242 snprintf(brd->SerialName, MAXTTYNAMELEN, "tty_dgap_%d_", brd->boardnum);
1243 brd->SerialDriver->name = brd->SerialName;
1244 brd->SerialDriver->name_base = 0;
1245 brd->SerialDriver->major = 0;
1246 brd->SerialDriver->minor_start = 0;
1247 brd->SerialDriver->type = TTY_DRIVER_TYPE_SERIAL;
1248 brd->SerialDriver->subtype = SERIAL_TYPE_NORMAL;
1249 brd->SerialDriver->init_termios = DgapDefaultTermios;
1250 brd->SerialDriver->driver_name = DRVSTR;
1251 brd->SerialDriver->flags = (TTY_DRIVER_REAL_RAW |
1252 TTY_DRIVER_DYNAMIC_DEV |
1253 TTY_DRIVER_HARDWARE_BREAK);
1255 /* The kernel wants space to store pointers to tty_structs */
1256 brd->SerialDriver->ttys =
1257 kzalloc(MAXPORTS * sizeof(struct tty_struct *), GFP_KERNEL);
1258 if (!brd->SerialDriver->ttys)
1259 return -ENOMEM;
1262 * Entry points for driver. Called by the kernel from
1263 * tty_io.c and n_tty.c.
1265 tty_set_operations(brd->SerialDriver, &dgap_tty_ops);
1268 * If we're doing transparent print, we have to do all of the above
1269 * again, separately so we don't get the LD confused about what major
1270 * we are when we get into the dgap_tty_open() routine.
1272 brd->PrintDriver = alloc_tty_driver(MAXPORTS);
1274 snprintf(brd->PrintName, MAXTTYNAMELEN, "pr_dgap_%d_", brd->boardnum);
1275 brd->PrintDriver->name = brd->PrintName;
1276 brd->PrintDriver->name_base = 0;
1277 brd->PrintDriver->major = 0;
1278 brd->PrintDriver->minor_start = 0;
1279 brd->PrintDriver->type = TTY_DRIVER_TYPE_SERIAL;
1280 brd->PrintDriver->subtype = SERIAL_TYPE_NORMAL;
1281 brd->PrintDriver->init_termios = DgapDefaultTermios;
1282 brd->PrintDriver->driver_name = DRVSTR;
1283 brd->PrintDriver->flags = (TTY_DRIVER_REAL_RAW |
1284 TTY_DRIVER_DYNAMIC_DEV |
1285 TTY_DRIVER_HARDWARE_BREAK);
1287 /* The kernel wants space to store pointers to tty_structs */
1288 brd->PrintDriver->ttys =
1289 kzalloc(MAXPORTS * sizeof(struct tty_struct *), GFP_KERNEL);
1290 if (!brd->PrintDriver->ttys)
1291 return -ENOMEM;
1294 * Entry points for driver. Called by the kernel from
1295 * tty_io.c and n_tty.c.
1297 tty_set_operations(brd->PrintDriver, &dgap_tty_ops);
1299 if (!brd->dgap_Major_Serial_Registered) {
1300 /* Register tty devices */
1301 rc = tty_register_driver(brd->SerialDriver);
1302 if (rc < 0)
1303 return rc;
1304 brd->dgap_Major_Serial_Registered = TRUE;
1305 dgap_BoardsByMajor[brd->SerialDriver->major] = brd;
1306 brd->dgap_Serial_Major = brd->SerialDriver->major;
1309 if (!brd->dgap_Major_TransparentPrint_Registered) {
1310 /* Register Transparent Print devices */
1311 rc = tty_register_driver(brd->PrintDriver);
1312 if (rc < 0)
1313 return rc;
1314 brd->dgap_Major_TransparentPrint_Registered = TRUE;
1315 dgap_BoardsByMajor[brd->PrintDriver->major] = brd;
1316 brd->dgap_TransparentPrint_Major = brd->PrintDriver->major;
1319 return rc;
1323 * dgap_tty_init()
1325 * Init the tty subsystem. Called once per board after board has been
1326 * downloaded and init'ed.
1328 static int dgap_tty_init(struct board_t *brd)
1330 int i;
1331 int tlw;
1332 uint true_count = 0;
1333 uchar *vaddr;
1334 uchar modem = 0;
1335 struct channel_t *ch;
1336 struct bs_t *bs;
1337 struct cm_t *cm;
1339 if (!brd)
1340 return -ENXIO;
1343 * Initialize board structure elements.
1346 vaddr = brd->re_map_membase;
1347 true_count = readw((vaddr + NCHAN));
1349 brd->nasync = dgap_config_get_number_of_ports(brd);
1351 if (!brd->nasync)
1352 brd->nasync = brd->maxports;
1354 if (brd->nasync > brd->maxports)
1355 brd->nasync = brd->maxports;
1357 if (true_count != brd->nasync) {
1358 if ((brd->type == PPCM) && (true_count == 64)) {
1359 pr_warn("dgap: %s configured for %d ports, has %d ports.\n",
1360 brd->name, brd->nasync, true_count);
1361 pr_warn("dgap: Please make SURE the EBI cable running from the card\n");
1362 pr_warn("dgap: to each EM module is plugged into EBI IN!\n");
1363 } else if ((brd->type == PPCM) && (true_count == 0)) {
1364 pr_warn("dgap: %s configured for %d ports, has %d ports.\n",
1365 brd->name, brd->nasync, true_count);
1366 pr_warn("dgap: Please make SURE the EBI cable running from the card\n");
1367 pr_warn("dgap: to each EM module is plugged into EBI IN!\n");
1368 } else
1369 pr_warn("dgap: %s configured for %d ports, has %d ports.\n",
1370 brd->name, brd->nasync, true_count);
1372 brd->nasync = true_count;
1374 /* If no ports, don't bother going any further */
1375 if (!brd->nasync) {
1376 brd->state = BOARD_FAILED;
1377 brd->dpastatus = BD_NOFEP;
1378 return -ENXIO;
1383 * Allocate channel memory that might not have been allocated
1384 * when the driver was first loaded.
1386 for (i = 0; i < brd->nasync; i++) {
1387 if (!brd->channels[i]) {
1388 brd->channels[i] =
1389 kzalloc(sizeof(struct channel_t), GFP_ATOMIC);
1390 if (!brd->channels[i])
1391 return -ENOMEM;
1395 ch = brd->channels[0];
1396 vaddr = brd->re_map_membase;
1398 bs = (struct bs_t *) ((ulong) vaddr + CHANBUF);
1399 cm = (struct cm_t *) ((ulong) vaddr + CMDBUF);
1401 brd->bd_bs = bs;
1403 /* Set up channel variables */
1404 for (i = 0; i < brd->nasync; i++, ch = brd->channels[i], bs++) {
1406 if (!brd->channels[i])
1407 continue;
1409 DGAP_SPINLOCK_INIT(ch->ch_lock);
1411 /* Store all our magic numbers */
1412 ch->magic = DGAP_CHANNEL_MAGIC;
1413 ch->ch_tun.magic = DGAP_UNIT_MAGIC;
1414 ch->ch_tun.un_type = DGAP_SERIAL;
1415 ch->ch_tun.un_ch = ch;
1416 ch->ch_tun.un_dev = i;
1418 ch->ch_pun.magic = DGAP_UNIT_MAGIC;
1419 ch->ch_pun.un_type = DGAP_PRINT;
1420 ch->ch_pun.un_ch = ch;
1421 ch->ch_pun.un_dev = i;
1423 ch->ch_vaddr = vaddr;
1424 ch->ch_bs = bs;
1425 ch->ch_cm = cm;
1426 ch->ch_bd = brd;
1427 ch->ch_portnum = i;
1428 ch->ch_digi = dgap_digi_init;
1431 * Set up digi dsr and dcd bits based on altpin flag.
1433 if (dgap_config_get_altpin(brd)) {
1434 ch->ch_dsr = DM_CD;
1435 ch->ch_cd = DM_DSR;
1436 ch->ch_digi.digi_flags |= DIGI_ALTPIN;
1437 } else {
1438 ch->ch_cd = DM_CD;
1439 ch->ch_dsr = DM_DSR;
1442 ch->ch_taddr = vaddr + ((ch->ch_bs->tx_seg) << 4);
1443 ch->ch_raddr = vaddr + ((ch->ch_bs->rx_seg) << 4);
1444 ch->ch_tx_win = 0;
1445 ch->ch_rx_win = 0;
1446 ch->ch_tsize = readw(&(ch->ch_bs->tx_max)) + 1;
1447 ch->ch_rsize = readw(&(ch->ch_bs->rx_max)) + 1;
1448 ch->ch_tstart = 0;
1449 ch->ch_rstart = 0;
1451 /* .25 second delay */
1452 ch->ch_close_delay = 250;
1455 * Set queue water marks, interrupt mask,
1456 * and general tty parameters.
1458 tlw = ch->ch_tsize >= 2000 ? ((ch->ch_tsize * 5) / 8) :
1459 ch->ch_tsize / 2;
1460 ch->ch_tlw = tlw;
1462 dgap_cmdw(ch, STLOW, tlw, 0);
1464 dgap_cmdw(ch, SRLOW, ch->ch_rsize / 2, 0);
1466 dgap_cmdw(ch, SRHIGH, 7 * ch->ch_rsize / 8, 0);
1468 ch->ch_mistat = readb(&(ch->ch_bs->m_stat));
1470 init_waitqueue_head(&ch->ch_flags_wait);
1471 init_waitqueue_head(&ch->ch_tun.un_flags_wait);
1472 init_waitqueue_head(&ch->ch_pun.un_flags_wait);
1473 init_waitqueue_head(&ch->ch_sniff_wait);
1475 /* Turn on all modem interrupts for now */
1476 modem = (DM_CD | DM_DSR | DM_CTS | DM_RI);
1477 writeb(modem, &(ch->ch_bs->m_int));
1480 * Set edelay to 0 if interrupts are turned on,
1481 * otherwise set edelay to the usual 100.
1483 if (brd->intr_used)
1484 writew(0, &(ch->ch_bs->edelay));
1485 else
1486 writew(100, &(ch->ch_bs->edelay));
1488 writeb(1, &(ch->ch_bs->idata));
1491 return 0;
1495 * dgap_tty_uninit()
1497 * Uninitialize the TTY portion of this driver. Free all memory and
1498 * resources.
1500 static void dgap_tty_uninit(struct board_t *brd)
1502 struct device *dev;
1503 int i = 0;
1505 if (brd->dgap_Major_Serial_Registered) {
1506 dgap_BoardsByMajor[brd->SerialDriver->major] = NULL;
1507 brd->dgap_Serial_Major = 0;
1508 for (i = 0; i < brd->nasync; i++) {
1509 tty_port_destroy(&brd->SerialPorts[i]);
1510 dev = brd->channels[i]->ch_tun.un_sysfs;
1511 dgap_remove_tty_sysfs(dev);
1512 tty_unregister_device(brd->SerialDriver, i);
1514 tty_unregister_driver(brd->SerialDriver);
1515 kfree(brd->SerialDriver->ttys);
1516 brd->SerialDriver->ttys = NULL;
1517 put_tty_driver(brd->SerialDriver);
1518 kfree(brd->SerialPorts);
1519 brd->dgap_Major_Serial_Registered = FALSE;
1522 if (brd->dgap_Major_TransparentPrint_Registered) {
1523 dgap_BoardsByMajor[brd->PrintDriver->major] = NULL;
1524 brd->dgap_TransparentPrint_Major = 0;
1525 for (i = 0; i < brd->nasync; i++) {
1526 tty_port_destroy(&brd->PrinterPorts[i]);
1527 dev = brd->channels[i]->ch_pun.un_sysfs;
1528 dgap_remove_tty_sysfs(dev);
1529 tty_unregister_device(brd->PrintDriver, i);
1531 tty_unregister_driver(brd->PrintDriver);
1532 kfree(brd->PrintDriver->ttys);
1533 brd->PrintDriver->ttys = NULL;
1534 put_tty_driver(brd->PrintDriver);
1535 kfree(brd->PrinterPorts);
1536 brd->dgap_Major_TransparentPrint_Registered = FALSE;
1540 #define TMPBUFLEN (1024)
1542 * dgap_sniff - Dump data out to the "sniff" buffer if the
1543 * proc sniff file is opened...
1545 static void dgap_sniff_nowait_nolock(struct channel_t *ch, uchar *text,
1546 uchar *buf, int len)
1548 struct timeval tv;
1549 int n;
1550 int r;
1551 int nbuf;
1552 int i;
1553 int tmpbuflen;
1554 char tmpbuf[TMPBUFLEN];
1555 char *p = tmpbuf;
1556 int too_much_data;
1558 /* Leave if sniff not open */
1559 if (!(ch->ch_sniff_flags & SNIFF_OPEN))
1560 return;
1562 do_gettimeofday(&tv);
1564 /* Create our header for data dump */
1565 p += sprintf(p, "<%ld %ld><%s><", tv.tv_sec, tv.tv_usec, text);
1566 tmpbuflen = p - tmpbuf;
1568 do {
1569 too_much_data = 0;
1571 for (i = 0; i < len && tmpbuflen < (TMPBUFLEN - 4); i++) {
1572 p += sprintf(p, "%02x ", *buf);
1573 buf++;
1574 tmpbuflen = p - tmpbuf;
1577 if (tmpbuflen < (TMPBUFLEN - 4)) {
1578 if (i > 0)
1579 p += sprintf(p - 1, "%s\n", ">");
1580 else
1581 p += sprintf(p, "%s\n", ">");
1582 } else {
1583 too_much_data = 1;
1584 len -= i;
1587 nbuf = strlen(tmpbuf);
1588 p = tmpbuf;
1591 * Loop while data remains.
1593 while (nbuf > 0 && ch->ch_sniff_buf) {
1595 * Determine the amount of available space left in the
1596 * buffer. If there's none, wait until some appears.
1598 n = (ch->ch_sniff_out - ch->ch_sniff_in - 1) &
1599 SNIFF_MASK;
1602 * If there is no space left to write to in our sniff
1603 * buffer, we have no choice but to drop the data.
1604 * We *cannot* sleep here waiting for space, because
1605 * this function was probably called by the
1606 * interrupt/timer routines!
1608 if (n == 0)
1609 return;
1612 * Copy as much data as will fit.
1615 if (n > nbuf)
1616 n = nbuf;
1618 r = SNIFF_MAX - ch->ch_sniff_in;
1620 if (r <= n) {
1621 memcpy(ch->ch_sniff_buf + ch->ch_sniff_in, p, r);
1623 n -= r;
1624 ch->ch_sniff_in = 0;
1625 p += r;
1626 nbuf -= r;
1629 memcpy(ch->ch_sniff_buf + ch->ch_sniff_in, p, n);
1631 ch->ch_sniff_in += n;
1632 p += n;
1633 nbuf -= n;
1636 * Wakeup any thread waiting for data
1638 if (ch->ch_sniff_flags & SNIFF_WAIT_DATA) {
1639 ch->ch_sniff_flags &= ~SNIFF_WAIT_DATA;
1640 wake_up_interruptible(&ch->ch_sniff_wait);
1645 * If the user sent us too much data to push into our tmpbuf,
1646 * we need to keep looping around on all the data.
1648 if (too_much_data) {
1649 p = tmpbuf;
1650 tmpbuflen = 0;
1653 } while (too_much_data);
1656 /*=======================================================================
1658 * dgap_input - Process received data.
1660 * ch - Pointer to channel structure.
1662 *=======================================================================*/
1664 static void dgap_input(struct channel_t *ch)
1666 struct board_t *bd;
1667 struct bs_t *bs;
1668 struct tty_struct *tp;
1669 struct tty_ldisc *ld;
1670 uint rmask;
1671 uint head;
1672 uint tail;
1673 int data_len;
1674 ulong lock_flags;
1675 ulong lock_flags2;
1676 int flip_len;
1677 int len = 0;
1678 int n = 0;
1679 uchar *buf;
1680 uchar tmpchar;
1681 int s = 0;
1683 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
1684 return;
1686 tp = ch->ch_tun.un_tty;
1688 bs = ch->ch_bs;
1689 if (!bs)
1690 return;
1692 bd = ch->ch_bd;
1693 if (!bd || bd->magic != DGAP_BOARD_MAGIC)
1694 return;
1696 DGAP_LOCK(bd->bd_lock, lock_flags);
1697 DGAP_LOCK(ch->ch_lock, lock_flags2);
1700 * Figure the number of characters in the buffer.
1701 * Exit immediately if none.
1704 rmask = ch->ch_rsize - 1;
1706 head = readw(&(bs->rx_head));
1707 head &= rmask;
1708 tail = readw(&(bs->rx_tail));
1709 tail &= rmask;
1711 data_len = (head - tail) & rmask;
1713 if (data_len == 0) {
1714 writeb(1, &(bs->idata));
1715 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
1716 DGAP_UNLOCK(bd->bd_lock, lock_flags);
1717 return;
1721 * If the device is not open, or CREAD is off, flush
1722 * input data and return immediately.
1724 if ((bd->state != BOARD_READY) || !tp ||
1725 (tp->magic != TTY_MAGIC) ||
1726 !(ch->ch_tun.un_flags & UN_ISOPEN) ||
1727 !(tp->termios.c_cflag & CREAD) ||
1728 (ch->ch_tun.un_flags & UN_CLOSING)) {
1730 writew(head, &(bs->rx_tail));
1731 writeb(1, &(bs->idata));
1732 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
1733 DGAP_UNLOCK(bd->bd_lock, lock_flags);
1734 return;
1738 * If we are throttled, simply don't read any data.
1740 if (ch->ch_flags & CH_RXBLOCK) {
1741 writeb(1, &(bs->idata));
1742 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
1743 DGAP_UNLOCK(bd->bd_lock, lock_flags);
1744 return;
1748 * Ignore oruns.
1750 tmpchar = readb(&(bs->orun));
1751 if (tmpchar) {
1752 ch->ch_err_overrun++;
1753 writeb(0, &(bs->orun));
1756 /* Decide how much data we can send into the tty layer */
1757 flip_len = TTY_FLIPBUF_SIZE;
1759 /* Chop down the length, if needed */
1760 len = min(data_len, flip_len);
1761 len = min(len, (N_TTY_BUF_SIZE - 1));
1763 ld = tty_ldisc_ref(tp);
1765 #ifdef TTY_DONT_FLIP
1767 * If the DONT_FLIP flag is on, don't flush our buffer, and act
1768 * like the ld doesn't have any space to put the data right now.
1770 if (test_bit(TTY_DONT_FLIP, &tp->flags))
1771 len = 0;
1772 #endif
1775 * If we were unable to get a reference to the ld,
1776 * don't flush our buffer, and act like the ld doesn't
1777 * have any space to put the data right now.
1779 if (!ld) {
1780 len = 0;
1781 } else {
1783 * If ld doesn't have a pointer to a receive_buf function,
1784 * flush the data, then act like the ld doesn't have any
1785 * space to put the data right now.
1787 if (!ld->ops->receive_buf) {
1788 writew(head, &(bs->rx_tail));
1789 len = 0;
1793 if (len <= 0) {
1794 writeb(1, &(bs->idata));
1795 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
1796 DGAP_UNLOCK(bd->bd_lock, lock_flags);
1797 if (ld)
1798 tty_ldisc_deref(ld);
1799 return;
1802 buf = ch->ch_bd->flipbuf;
1803 n = len;
1806 * n now contains the most amount of data we can copy,
1807 * bounded either by our buffer size or the amount
1808 * of data the card actually has pending...
1810 while (n) {
1812 s = ((head >= tail) ? head : ch->ch_rsize) - tail;
1813 s = min(s, n);
1815 if (s <= 0)
1816 break;
1818 memcpy_fromio(buf, (char *) ch->ch_raddr + tail, s);
1819 dgap_sniff_nowait_nolock(ch, "USER READ", buf, s);
1821 tail += s;
1822 buf += s;
1824 n -= s;
1825 /* Flip queue if needed */
1826 tail &= rmask;
1829 writew(tail, &(bs->rx_tail));
1830 writeb(1, &(bs->idata));
1831 ch->ch_rxcount += len;
1834 * If we are completely raw, we don't need to go through a lot
1835 * of the tty layers that exist.
1836 * In this case, we take the shortest and fastest route we
1837 * can to relay the data to the user.
1839 * On the other hand, if we are not raw, we need to go through
1840 * the tty layer, which has its API more well defined.
1842 if (I_PARMRK(tp) || I_BRKINT(tp) || I_INPCK(tp)) {
1843 dgap_parity_scan(ch, ch->ch_bd->flipbuf,
1844 ch->ch_bd->flipflagbuf, &len);
1846 len = tty_buffer_request_room(tp->port, len);
1847 tty_insert_flip_string_flags(tp->port, ch->ch_bd->flipbuf,
1848 ch->ch_bd->flipflagbuf, len);
1849 } else {
1850 len = tty_buffer_request_room(tp->port, len);
1851 tty_insert_flip_string(tp->port, ch->ch_bd->flipbuf, len);
1854 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
1855 DGAP_UNLOCK(bd->bd_lock, lock_flags);
1857 /* Tell the tty layer its okay to "eat" the data now */
1858 tty_flip_buffer_push(tp->port);
1860 if (ld)
1861 tty_ldisc_deref(ld);
1865 /************************************************************************
1866 * Determines when CARRIER changes state and takes appropriate
1867 * action.
1868 ************************************************************************/
1869 static void dgap_carrier(struct channel_t *ch)
1871 struct board_t *bd;
1873 int virt_carrier = 0;
1874 int phys_carrier = 0;
1876 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
1877 return;
1879 bd = ch->ch_bd;
1881 if (!bd || bd->magic != DGAP_BOARD_MAGIC)
1882 return;
1884 /* Make sure altpin is always set correctly */
1885 if (ch->ch_digi.digi_flags & DIGI_ALTPIN) {
1886 ch->ch_dsr = DM_CD;
1887 ch->ch_cd = DM_DSR;
1888 } else {
1889 ch->ch_dsr = DM_DSR;
1890 ch->ch_cd = DM_CD;
1893 if (ch->ch_mistat & D_CD(ch))
1894 phys_carrier = 1;
1896 if (ch->ch_digi.digi_flags & DIGI_FORCEDCD)
1897 virt_carrier = 1;
1899 if (ch->ch_c_cflag & CLOCAL)
1900 virt_carrier = 1;
1903 * Test for a VIRTUAL carrier transition to HIGH.
1905 if (((ch->ch_flags & CH_FCAR) == 0) && (virt_carrier == 1)) {
1908 * When carrier rises, wake any threads waiting
1909 * for carrier in the open routine.
1912 if (waitqueue_active(&(ch->ch_flags_wait)))
1913 wake_up_interruptible(&ch->ch_flags_wait);
1917 * Test for a PHYSICAL carrier transition to HIGH.
1919 if (((ch->ch_flags & CH_CD) == 0) && (phys_carrier == 1)) {
1922 * When carrier rises, wake any threads waiting
1923 * for carrier in the open routine.
1926 if (waitqueue_active(&(ch->ch_flags_wait)))
1927 wake_up_interruptible(&ch->ch_flags_wait);
1931 * Test for a PHYSICAL transition to low, so long as we aren't
1932 * currently ignoring physical transitions (which is what "virtual
1933 * carrier" indicates).
1935 * The transition of the virtual carrier to low really doesn't
1936 * matter... it really only means "ignore carrier state", not
1937 * "make pretend that carrier is there".
1939 if ((virt_carrier == 0) &&
1940 ((ch->ch_flags & CH_CD) != 0) &&
1941 (phys_carrier == 0)) {
1944 * When carrier drops:
1946 * Drop carrier on all open units.
1948 * Flush queues, waking up any task waiting in the
1949 * line discipline.
1951 * Send a hangup to the control terminal.
1953 * Enable all select calls.
1955 if (waitqueue_active(&(ch->ch_flags_wait)))
1956 wake_up_interruptible(&ch->ch_flags_wait);
1958 if (ch->ch_tun.un_open_count > 0)
1959 tty_hangup(ch->ch_tun.un_tty);
1961 if (ch->ch_pun.un_open_count > 0)
1962 tty_hangup(ch->ch_pun.un_tty);
1966 * Make sure that our cached values reflect the current reality.
1968 if (virt_carrier == 1)
1969 ch->ch_flags |= CH_FCAR;
1970 else
1971 ch->ch_flags &= ~CH_FCAR;
1973 if (phys_carrier == 1)
1974 ch->ch_flags |= CH_CD;
1975 else
1976 ch->ch_flags &= ~CH_CD;
1979 /************************************************************************
1981 * TTY Entry points and helper functions
1983 ************************************************************************/
1986 * dgap_tty_open()
1989 static int dgap_tty_open(struct tty_struct *tty, struct file *file)
1991 struct board_t *brd;
1992 struct channel_t *ch;
1993 struct un_t *un;
1994 struct bs_t *bs;
1995 uint major = 0;
1996 uint minor = 0;
1997 int rc = 0;
1998 ulong lock_flags;
1999 ulong lock_flags2;
2000 u16 head;
2002 rc = 0;
2004 major = MAJOR(tty_devnum(tty));
2005 minor = MINOR(tty_devnum(tty));
2007 if (major > 255)
2008 return -ENXIO;
2010 /* Get board pointer from our array of majors we have allocated */
2011 brd = dgap_BoardsByMajor[major];
2012 if (!brd)
2013 return -ENXIO;
2016 * If board is not yet up to a state of READY, go to
2017 * sleep waiting for it to happen or they cancel the open.
2019 rc = wait_event_interruptible(brd->state_wait,
2020 (brd->state & BOARD_READY));
2022 if (rc)
2023 return rc;
2025 DGAP_LOCK(brd->bd_lock, lock_flags);
2027 /* The wait above should guarantee this cannot happen */
2028 if (brd->state != BOARD_READY) {
2029 DGAP_UNLOCK(brd->bd_lock, lock_flags);
2030 return -ENXIO;
2033 /* If opened device is greater than our number of ports, bail. */
2034 if (MINOR(tty_devnum(tty)) > brd->nasync) {
2035 DGAP_UNLOCK(brd->bd_lock, lock_flags);
2036 return -ENXIO;
2039 ch = brd->channels[minor];
2040 if (!ch) {
2041 DGAP_UNLOCK(brd->bd_lock, lock_flags);
2042 return -ENXIO;
2045 /* Grab channel lock */
2046 DGAP_LOCK(ch->ch_lock, lock_flags2);
2048 /* Figure out our type */
2049 if (major == brd->dgap_Serial_Major) {
2050 un = &brd->channels[minor]->ch_tun;
2051 un->un_type = DGAP_SERIAL;
2052 } else if (major == brd->dgap_TransparentPrint_Major) {
2053 un = &brd->channels[minor]->ch_pun;
2054 un->un_type = DGAP_PRINT;
2055 } else {
2056 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
2057 DGAP_UNLOCK(brd->bd_lock, lock_flags);
2058 return -ENXIO;
2061 /* Store our unit into driver_data, so we always have it available. */
2062 tty->driver_data = un;
2065 * Error if channel info pointer is NULL.
2067 bs = ch->ch_bs;
2068 if (!bs) {
2069 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
2070 DGAP_UNLOCK(brd->bd_lock, lock_flags);
2071 return -ENXIO;
2075 * Initialize tty's
2077 if (!(un->un_flags & UN_ISOPEN)) {
2078 /* Store important variables. */
2079 un->un_tty = tty;
2081 /* Maybe do something here to the TTY struct as well? */
2085 * Initialize if neither terminal or printer is open.
2087 if (!((ch->ch_tun.un_flags | ch->ch_pun.un_flags) & UN_ISOPEN)) {
2089 ch->ch_mforce = 0;
2090 ch->ch_mval = 0;
2093 * Flush input queue.
2095 head = readw(&(bs->rx_head));
2096 writew(head, &(bs->rx_tail));
2098 ch->ch_flags = 0;
2099 ch->pscan_state = 0;
2100 ch->pscan_savechar = 0;
2102 ch->ch_c_cflag = tty->termios.c_cflag;
2103 ch->ch_c_iflag = tty->termios.c_iflag;
2104 ch->ch_c_oflag = tty->termios.c_oflag;
2105 ch->ch_c_lflag = tty->termios.c_lflag;
2106 ch->ch_startc = tty->termios.c_cc[VSTART];
2107 ch->ch_stopc = tty->termios.c_cc[VSTOP];
2109 /* TODO: flush our TTY struct here? */
2112 dgap_carrier(ch);
2114 * Run param in case we changed anything
2116 dgap_param(tty);
2119 * follow protocol for opening port
2122 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
2123 DGAP_UNLOCK(brd->bd_lock, lock_flags);
2125 rc = dgap_block_til_ready(tty, file, ch);
2127 if (!un->un_tty)
2128 return -ENODEV;
2130 /* No going back now, increment our unit and channel counters */
2131 DGAP_LOCK(ch->ch_lock, lock_flags);
2132 ch->ch_open_count++;
2133 un->un_open_count++;
2134 un->un_flags |= (UN_ISOPEN);
2135 DGAP_UNLOCK(ch->ch_lock, lock_flags);
2137 return rc;
2141 * dgap_block_til_ready()
2143 * Wait for DCD, if needed.
2145 static int dgap_block_til_ready(struct tty_struct *tty, struct file *file,
2146 struct channel_t *ch)
2148 int retval = 0;
2149 struct un_t *un = NULL;
2150 ulong lock_flags;
2151 uint old_flags = 0;
2152 int sleep_on_un_flags = 0;
2154 if (!tty || tty->magic != TTY_MAGIC || !file || !ch ||
2155 ch->magic != DGAP_CHANNEL_MAGIC)
2156 return -ENXIO;
2158 un = tty->driver_data;
2159 if (!un || un->magic != DGAP_UNIT_MAGIC)
2160 return -ENXIO;
2162 DGAP_LOCK(ch->ch_lock, lock_flags);
2164 ch->ch_wopen++;
2166 /* Loop forever */
2167 while (1) {
2169 sleep_on_un_flags = 0;
2172 * If board has failed somehow during our sleep,
2173 * bail with error.
2175 if (ch->ch_bd->state == BOARD_FAILED) {
2176 retval = -ENXIO;
2177 break;
2180 /* If tty was hung up, break out of loop and set error. */
2181 if (tty_hung_up_p(file)) {
2182 retval = -EAGAIN;
2183 break;
2187 * If either unit is in the middle of the fragile part of close,
2188 * we just cannot touch the channel safely.
2189 * Go back to sleep, knowing that when the channel can be
2190 * touched safely, the close routine will signal the
2191 * ch_wait_flags to wake us back up.
2193 if (!((ch->ch_tun.un_flags | ch->ch_pun.un_flags) &
2194 UN_CLOSING)) {
2197 * Our conditions to leave cleanly and happily:
2198 * 1) NONBLOCKING on the tty is set.
2199 * 2) CLOCAL is set.
2200 * 3) DCD (fake or real) is active.
2203 if (file->f_flags & O_NONBLOCK)
2204 break;
2206 if (tty->flags & (1 << TTY_IO_ERROR))
2207 break;
2209 if (ch->ch_flags & CH_CD)
2210 break;
2212 if (ch->ch_flags & CH_FCAR)
2213 break;
2214 } else {
2215 sleep_on_un_flags = 1;
2219 * If there is a signal pending, the user probably
2220 * interrupted (ctrl-c) us.
2221 * Leave loop with error set.
2223 if (signal_pending(current)) {
2224 retval = -ERESTARTSYS;
2225 break;
2229 * Store the flags before we let go of channel lock
2231 if (sleep_on_un_flags)
2232 old_flags = ch->ch_tun.un_flags | ch->ch_pun.un_flags;
2233 else
2234 old_flags = ch->ch_flags;
2237 * Let go of channel lock before calling schedule.
2238 * Our poller will get any FEP events and wake us up when DCD
2239 * eventually goes active.
2242 DGAP_UNLOCK(ch->ch_lock, lock_flags);
2245 * Wait for something in the flags to change
2246 * from the current value.
2248 if (sleep_on_un_flags) {
2249 retval = wait_event_interruptible(un->un_flags_wait,
2250 (old_flags != (ch->ch_tun.un_flags |
2251 ch->ch_pun.un_flags)));
2252 } else {
2253 retval = wait_event_interruptible(ch->ch_flags_wait,
2254 (old_flags != ch->ch_flags));
2258 * We got woken up for some reason.
2259 * Before looping around, grab our channel lock.
2261 DGAP_LOCK(ch->ch_lock, lock_flags);
2264 ch->ch_wopen--;
2266 DGAP_UNLOCK(ch->ch_lock, lock_flags);
2268 if (retval)
2269 return retval;
2271 return 0;
2275 * dgap_tty_hangup()
2277 * Hangup the port. Like a close, but don't wait for output to drain.
2279 static void dgap_tty_hangup(struct tty_struct *tty)
2281 struct board_t *bd;
2282 struct channel_t *ch;
2283 struct un_t *un;
2285 if (!tty || tty->magic != TTY_MAGIC)
2286 return;
2288 un = tty->driver_data;
2289 if (!un || un->magic != DGAP_UNIT_MAGIC)
2290 return;
2292 ch = un->un_ch;
2293 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
2294 return;
2296 bd = ch->ch_bd;
2297 if (!bd || bd->magic != DGAP_BOARD_MAGIC)
2298 return;
2300 /* flush the transmit queues */
2301 dgap_tty_flush_buffer(tty);
2306 * dgap_tty_close()
2309 static void dgap_tty_close(struct tty_struct *tty, struct file *file)
2311 struct ktermios *ts;
2312 struct board_t *bd;
2313 struct channel_t *ch;
2314 struct un_t *un;
2315 ulong lock_flags;
2316 int rc = 0;
2318 if (!tty || tty->magic != TTY_MAGIC)
2319 return;
2321 un = tty->driver_data;
2322 if (!un || un->magic != DGAP_UNIT_MAGIC)
2323 return;
2325 ch = un->un_ch;
2326 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
2327 return;
2329 bd = ch->ch_bd;
2330 if (!bd || bd->magic != DGAP_BOARD_MAGIC)
2331 return;
2333 ts = &tty->termios;
2335 DGAP_LOCK(ch->ch_lock, lock_flags);
2338 * Determine if this is the last close or not - and if we agree about
2339 * which type of close it is with the Line Discipline
2341 if ((tty->count == 1) && (un->un_open_count != 1)) {
2343 * Uh, oh. tty->count is 1, which means that the tty
2344 * structure will be freed. un_open_count should always
2345 * be one in these conditions. If it's greater than
2346 * one, we've got real problems, since it means the
2347 * serial port won't be shutdown.
2349 un->un_open_count = 1;
2352 if (--un->un_open_count < 0)
2353 un->un_open_count = 0;
2355 ch->ch_open_count--;
2357 if (ch->ch_open_count && un->un_open_count) {
2358 DGAP_UNLOCK(ch->ch_lock, lock_flags);
2359 return;
2362 /* OK, its the last close on the unit */
2364 un->un_flags |= UN_CLOSING;
2366 tty->closing = 1;
2369 * Only officially close channel if count is 0 and
2370 * DIGI_PRINTER bit is not set.
2372 if ((ch->ch_open_count == 0) &&
2373 !(ch->ch_digi.digi_flags & DIGI_PRINTER)) {
2375 ch->ch_flags &= ~(CH_RXBLOCK);
2377 DGAP_UNLOCK(ch->ch_lock, lock_flags);
2379 /* wait for output to drain */
2380 /* This will also return if we take an interrupt */
2382 rc = dgap_wait_for_drain(tty);
2384 dgap_tty_flush_buffer(tty);
2385 tty_ldisc_flush(tty);
2387 DGAP_LOCK(ch->ch_lock, lock_flags);
2389 tty->closing = 0;
2392 * If we have HUPCL set, lower DTR and RTS
2394 if (ch->ch_c_cflag & HUPCL) {
2395 ch->ch_mostat &= ~(D_RTS(ch)|D_DTR(ch));
2396 dgap_cmdb(ch, SMODEM, 0, D_DTR(ch)|D_RTS(ch), 0);
2399 * Go to sleep to ensure RTS/DTR
2400 * have been dropped for modems to see it.
2402 if (ch->ch_close_delay) {
2403 DGAP_UNLOCK(ch->ch_lock, lock_flags);
2404 dgap_ms_sleep(ch->ch_close_delay);
2405 DGAP_LOCK(ch->ch_lock, lock_flags);
2409 ch->pscan_state = 0;
2410 ch->pscan_savechar = 0;
2411 ch->ch_baud_info = 0;
2416 * turn off print device when closing print device.
2418 if ((un->un_type == DGAP_PRINT) && (ch->ch_flags & CH_PRON)) {
2419 dgap_wmove(ch, ch->ch_digi.digi_offstr,
2420 (int) ch->ch_digi.digi_offlen);
2421 ch->ch_flags &= ~CH_PRON;
2424 un->un_tty = NULL;
2425 un->un_flags &= ~(UN_ISOPEN | UN_CLOSING);
2426 tty->driver_data = NULL;
2428 wake_up_interruptible(&ch->ch_flags_wait);
2429 wake_up_interruptible(&un->un_flags_wait);
2431 DGAP_UNLOCK(ch->ch_lock, lock_flags);
2435 * dgap_tty_chars_in_buffer()
2437 * Return number of characters that have not been transmitted yet.
2439 * This routine is used by the line discipline to determine if there
2440 * is data waiting to be transmitted/drained/flushed or not.
2442 static int dgap_tty_chars_in_buffer(struct tty_struct *tty)
2444 struct board_t *bd = NULL;
2445 struct channel_t *ch = NULL;
2446 struct un_t *un = NULL;
2447 struct bs_t *bs = NULL;
2448 uchar tbusy;
2449 uint chars = 0;
2450 u16 thead, ttail, tmask, chead, ctail;
2451 ulong lock_flags = 0;
2452 ulong lock_flags2 = 0;
2454 if (tty == NULL)
2455 return 0;
2457 un = tty->driver_data;
2458 if (!un || un->magic != DGAP_UNIT_MAGIC)
2459 return 0;
2461 ch = un->un_ch;
2462 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
2463 return 0;
2465 bd = ch->ch_bd;
2466 if (!bd || bd->magic != DGAP_BOARD_MAGIC)
2467 return 0;
2469 bs = ch->ch_bs;
2470 if (!bs)
2471 return 0;
2473 DGAP_LOCK(bd->bd_lock, lock_flags);
2474 DGAP_LOCK(ch->ch_lock, lock_flags2);
2476 tmask = (ch->ch_tsize - 1);
2478 /* Get Transmit queue pointers */
2479 thead = readw(&(bs->tx_head)) & tmask;
2480 ttail = readw(&(bs->tx_tail)) & tmask;
2482 /* Get tbusy flag */
2483 tbusy = readb(&(bs->tbusy));
2485 /* Get Command queue pointers */
2486 chead = readw(&(ch->ch_cm->cm_head));
2487 ctail = readw(&(ch->ch_cm->cm_tail));
2489 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
2490 DGAP_UNLOCK(bd->bd_lock, lock_flags);
2493 * The only way we know for sure if there is no pending
2494 * data left to be transferred, is if:
2495 * 1) Transmit head and tail are equal (empty).
2496 * 2) Command queue head and tail are equal (empty).
2497 * 3) The "TBUSY" flag is 0. (Transmitter not busy).
2500 if ((ttail == thead) && (tbusy == 0) && (chead == ctail)) {
2501 chars = 0;
2502 } else {
2503 if (thead >= ttail)
2504 chars = thead - ttail;
2505 else
2506 chars = thead - ttail + ch->ch_tsize;
2508 * Fudge factor here.
2509 * If chars is zero, we know that the command queue had
2510 * something in it or tbusy was set. Because we cannot
2511 * be sure if there is still some data to be transmitted,
2512 * lets lie, and tell ld we have 1 byte left.
2514 if (chars == 0) {
2516 * If TBUSY is still set, and our tx buffers are empty,
2517 * force the firmware to send me another wakeup after
2518 * TBUSY has been cleared.
2520 if (tbusy != 0) {
2521 DGAP_LOCK(ch->ch_lock, lock_flags);
2522 un->un_flags |= UN_EMPTY;
2523 writeb(1, &(bs->iempty));
2524 DGAP_UNLOCK(ch->ch_lock, lock_flags);
2526 chars = 1;
2530 return chars;
2533 static int dgap_wait_for_drain(struct tty_struct *tty)
2535 struct channel_t *ch;
2536 struct un_t *un;
2537 struct bs_t *bs;
2538 int ret = -EIO;
2539 uint count = 1;
2540 ulong lock_flags = 0;
2542 if (!tty || tty->magic != TTY_MAGIC)
2543 return ret;
2545 un = tty->driver_data;
2546 if (!un || un->magic != DGAP_UNIT_MAGIC)
2547 return ret;
2549 ch = un->un_ch;
2550 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
2551 return ret;
2553 bs = ch->ch_bs;
2554 if (!bs)
2555 return ret;
2557 ret = 0;
2559 /* Loop until data is drained */
2560 while (count != 0) {
2562 count = dgap_tty_chars_in_buffer(tty);
2564 if (count == 0)
2565 break;
2567 /* Set flag waiting for drain */
2568 DGAP_LOCK(ch->ch_lock, lock_flags);
2569 un->un_flags |= UN_EMPTY;
2570 writeb(1, &(bs->iempty));
2571 DGAP_UNLOCK(ch->ch_lock, lock_flags);
2573 /* Go to sleep till we get woken up */
2574 ret = wait_event_interruptible(un->un_flags_wait,
2575 ((un->un_flags & UN_EMPTY) == 0));
2576 /* If ret is non-zero, user ctrl-c'ed us */
2577 if (ret)
2578 break;
2581 DGAP_LOCK(ch->ch_lock, lock_flags);
2582 un->un_flags &= ~(UN_EMPTY);
2583 DGAP_UNLOCK(ch->ch_lock, lock_flags);
2585 return ret;
2589 * dgap_maxcps_room
2591 * Reduces bytes_available to the max number of characters
2592 * that can be sent currently given the maxcps value, and
2593 * returns the new bytes_available. This only affects printer
2594 * output.
2596 static int dgap_maxcps_room(struct tty_struct *tty, int bytes_available)
2598 struct channel_t *ch = NULL;
2599 struct un_t *un = NULL;
2601 if (tty == NULL)
2602 return bytes_available;
2604 un = tty->driver_data;
2605 if (!un || un->magic != DGAP_UNIT_MAGIC)
2606 return bytes_available;
2608 ch = un->un_ch;
2609 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
2610 return bytes_available;
2613 * If its not the Transparent print device, return
2614 * the full data amount.
2616 if (un->un_type != DGAP_PRINT)
2617 return bytes_available;
2619 if (ch->ch_digi.digi_maxcps > 0 && ch->ch_digi.digi_bufsize > 0) {
2620 int cps_limit = 0;
2621 unsigned long current_time = jiffies;
2622 unsigned long buffer_time = current_time +
2623 (HZ * ch->ch_digi.digi_bufsize) /
2624 ch->ch_digi.digi_maxcps;
2626 if (ch->ch_cpstime < current_time) {
2627 /* buffer is empty */
2628 ch->ch_cpstime = current_time; /* reset ch_cpstime */
2629 cps_limit = ch->ch_digi.digi_bufsize;
2630 } else if (ch->ch_cpstime < buffer_time) {
2631 /* still room in the buffer */
2632 cps_limit = ((buffer_time - ch->ch_cpstime) *
2633 ch->ch_digi.digi_maxcps) / HZ;
2634 } else {
2635 /* no room in the buffer */
2636 cps_limit = 0;
2639 bytes_available = min(cps_limit, bytes_available);
2642 return bytes_available;
2645 static inline void dgap_set_firmware_event(struct un_t *un, unsigned int event)
2647 struct channel_t *ch = NULL;
2648 struct bs_t *bs = NULL;
2650 if (!un || un->magic != DGAP_UNIT_MAGIC)
2651 return;
2652 ch = un->un_ch;
2653 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
2654 return;
2655 bs = ch->ch_bs;
2656 if (!bs)
2657 return;
2659 if ((event & UN_LOW) != 0) {
2660 if ((un->un_flags & UN_LOW) == 0) {
2661 un->un_flags |= UN_LOW;
2662 writeb(1, &(bs->ilow));
2665 if ((event & UN_LOW) != 0) {
2666 if ((un->un_flags & UN_EMPTY) == 0) {
2667 un->un_flags |= UN_EMPTY;
2668 writeb(1, &(bs->iempty));
2674 * dgap_tty_write_room()
2676 * Return space available in Tx buffer
2678 static int dgap_tty_write_room(struct tty_struct *tty)
2680 struct channel_t *ch = NULL;
2681 struct un_t *un = NULL;
2682 struct bs_t *bs = NULL;
2683 u16 head, tail, tmask;
2684 int ret = 0;
2685 ulong lock_flags = 0;
2687 if (!tty)
2688 return 0;
2690 un = tty->driver_data;
2691 if (!un || un->magic != DGAP_UNIT_MAGIC)
2692 return 0;
2694 ch = un->un_ch;
2695 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
2696 return 0;
2698 bs = ch->ch_bs;
2699 if (!bs)
2700 return 0;
2702 DGAP_LOCK(ch->ch_lock, lock_flags);
2704 tmask = ch->ch_tsize - 1;
2705 head = readw(&(bs->tx_head)) & tmask;
2706 tail = readw(&(bs->tx_tail)) & tmask;
2708 ret = tail - head - 1;
2709 if (ret < 0)
2710 ret += ch->ch_tsize;
2712 /* Limit printer to maxcps */
2713 ret = dgap_maxcps_room(tty, ret);
2716 * If we are printer device, leave space for
2717 * possibly both the on and off strings.
2719 if (un->un_type == DGAP_PRINT) {
2720 if (!(ch->ch_flags & CH_PRON))
2721 ret -= ch->ch_digi.digi_onlen;
2722 ret -= ch->ch_digi.digi_offlen;
2723 } else {
2724 if (ch->ch_flags & CH_PRON)
2725 ret -= ch->ch_digi.digi_offlen;
2728 if (ret < 0)
2729 ret = 0;
2732 * Schedule FEP to wake us up if needed.
2734 * TODO: This might be overkill...
2735 * Do we really need to schedule callbacks from the FEP
2736 * in every case? Can we get smarter based on ret?
2738 dgap_set_firmware_event(un, UN_LOW | UN_EMPTY);
2739 DGAP_UNLOCK(ch->ch_lock, lock_flags);
2741 return ret;
2745 * dgap_tty_put_char()
2747 * Put a character into ch->ch_buf
2749 * - used by the line discipline for OPOST processing
2751 static int dgap_tty_put_char(struct tty_struct *tty, unsigned char c)
2754 * Simply call tty_write.
2756 dgap_tty_write(tty, &c, 1);
2757 return 1;
2761 * dgap_tty_write()
2763 * Take data from the user or kernel and send it out to the FEP.
2764 * In here exists all the Transparent Print magic as well.
2766 static int dgap_tty_write(struct tty_struct *tty, const unsigned char *buf,
2767 int count)
2769 struct channel_t *ch = NULL;
2770 struct un_t *un = NULL;
2771 struct bs_t *bs = NULL;
2772 char *vaddr = NULL;
2773 u16 head, tail, tmask, remain;
2774 int bufcount = 0, n = 0;
2775 int orig_count = 0;
2776 ulong lock_flags;
2778 if (!tty)
2779 return 0;
2781 un = tty->driver_data;
2782 if (!un || un->magic != DGAP_UNIT_MAGIC)
2783 return 0;
2785 ch = un->un_ch;
2786 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
2787 return 0;
2789 bs = ch->ch_bs;
2790 if (!bs)
2791 return 0;
2793 if (!count)
2794 return 0;
2797 * Store original amount of characters passed in.
2798 * This helps to figure out if we should ask the FEP
2799 * to send us an event when it has more space available.
2801 orig_count = count;
2803 DGAP_LOCK(ch->ch_lock, lock_flags);
2805 /* Get our space available for the channel from the board */
2806 tmask = ch->ch_tsize - 1;
2807 head = readw(&(bs->tx_head)) & tmask;
2808 tail = readw(&(bs->tx_tail)) & tmask;
2810 bufcount = tail - head - 1;
2811 if (bufcount < 0)
2812 bufcount += ch->ch_tsize;
2815 * Limit printer output to maxcps overall, with bursts allowed
2816 * up to bufsize characters.
2818 bufcount = dgap_maxcps_room(tty, bufcount);
2821 * Take minimum of what the user wants to send, and the
2822 * space available in the FEP buffer.
2824 count = min(count, bufcount);
2827 * Bail if no space left.
2829 if (count <= 0) {
2830 dgap_set_firmware_event(un, UN_LOW | UN_EMPTY);
2831 DGAP_UNLOCK(ch->ch_lock, lock_flags);
2832 return 0;
2836 * Output the printer ON string, if we are in terminal mode, but
2837 * need to be in printer mode.
2839 if ((un->un_type == DGAP_PRINT) && !(ch->ch_flags & CH_PRON)) {
2840 dgap_wmove(ch, ch->ch_digi.digi_onstr,
2841 (int) ch->ch_digi.digi_onlen);
2842 head = readw(&(bs->tx_head)) & tmask;
2843 ch->ch_flags |= CH_PRON;
2847 * On the other hand, output the printer OFF string, if we are
2848 * currently in printer mode, but need to output to the terminal.
2850 if ((un->un_type != DGAP_PRINT) && (ch->ch_flags & CH_PRON)) {
2851 dgap_wmove(ch, ch->ch_digi.digi_offstr,
2852 (int) ch->ch_digi.digi_offlen);
2853 head = readw(&(bs->tx_head)) & tmask;
2854 ch->ch_flags &= ~CH_PRON;
2858 * If there is nothing left to copy, or
2859 * I can't handle any more data, leave.
2861 if (count <= 0) {
2862 dgap_set_firmware_event(un, UN_LOW | UN_EMPTY);
2863 DGAP_UNLOCK(ch->ch_lock, lock_flags);
2864 return 0;
2867 n = count;
2870 * If the write wraps over the top of the circular buffer,
2871 * move the portion up to the wrap point, and reset the
2872 * pointers to the bottom.
2874 remain = ch->ch_tstart + ch->ch_tsize - head;
2876 if (n >= remain) {
2877 n -= remain;
2878 vaddr = ch->ch_taddr + head;
2880 memcpy_toio(vaddr, (uchar *) buf, remain);
2881 dgap_sniff_nowait_nolock(ch, "USER WRITE", (uchar *) buf,
2882 remain);
2884 head = ch->ch_tstart;
2885 buf += remain;
2888 if (n > 0) {
2891 * Move rest of data.
2893 vaddr = ch->ch_taddr + head;
2894 remain = n;
2896 memcpy_toio(vaddr, (uchar *) buf, remain);
2897 dgap_sniff_nowait_nolock(ch, "USER WRITE", (uchar *)buf,
2898 remain);
2900 head += remain;
2904 if (count) {
2905 ch->ch_txcount += count;
2906 head &= tmask;
2907 writew(head, &(bs->tx_head));
2910 dgap_set_firmware_event(un, UN_LOW | UN_EMPTY);
2913 * If this is the print device, and the
2914 * printer is still on, we need to turn it
2915 * off before going idle. If the buffer is
2916 * non-empty, wait until it goes empty.
2917 * Otherwise turn it off right now.
2919 if ((un->un_type == DGAP_PRINT) && (ch->ch_flags & CH_PRON)) {
2920 tail = readw(&(bs->tx_tail)) & tmask;
2922 if (tail != head) {
2923 un->un_flags |= UN_EMPTY;
2924 writeb(1, &(bs->iempty));
2925 } else {
2926 dgap_wmove(ch, ch->ch_digi.digi_offstr,
2927 (int) ch->ch_digi.digi_offlen);
2928 head = readw(&(bs->tx_head)) & tmask;
2929 ch->ch_flags &= ~CH_PRON;
2933 /* Update printer buffer empty time. */
2934 if ((un->un_type == DGAP_PRINT) && (ch->ch_digi.digi_maxcps > 0)
2935 && (ch->ch_digi.digi_bufsize > 0)) {
2936 ch->ch_cpstime += (HZ * count) / ch->ch_digi.digi_maxcps;
2939 DGAP_UNLOCK(ch->ch_lock, lock_flags);
2941 return count;
2945 * Return modem signals to ld.
2947 static int dgap_tty_tiocmget(struct tty_struct *tty)
2949 struct channel_t *ch;
2950 struct un_t *un;
2951 int result = -EIO;
2952 uchar mstat = 0;
2953 ulong lock_flags;
2955 if (!tty || tty->magic != TTY_MAGIC)
2956 return result;
2958 un = tty->driver_data;
2959 if (!un || un->magic != DGAP_UNIT_MAGIC)
2960 return result;
2962 ch = un->un_ch;
2963 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
2964 return result;
2966 DGAP_LOCK(ch->ch_lock, lock_flags);
2968 mstat = readb(&(ch->ch_bs->m_stat));
2969 /* Append any outbound signals that might be pending... */
2970 mstat |= ch->ch_mostat;
2972 DGAP_UNLOCK(ch->ch_lock, lock_flags);
2974 result = 0;
2976 if (mstat & D_DTR(ch))
2977 result |= TIOCM_DTR;
2978 if (mstat & D_RTS(ch))
2979 result |= TIOCM_RTS;
2980 if (mstat & D_CTS(ch))
2981 result |= TIOCM_CTS;
2982 if (mstat & D_DSR(ch))
2983 result |= TIOCM_DSR;
2984 if (mstat & D_RI(ch))
2985 result |= TIOCM_RI;
2986 if (mstat & D_CD(ch))
2987 result |= TIOCM_CD;
2989 return result;
2993 * dgap_tty_tiocmset()
2995 * Set modem signals, called by ld.
2997 static int dgap_tty_tiocmset(struct tty_struct *tty,
2998 unsigned int set, unsigned int clear)
3000 struct board_t *bd;
3001 struct channel_t *ch;
3002 struct un_t *un;
3003 int ret = -EIO;
3004 ulong lock_flags;
3005 ulong lock_flags2;
3007 if (!tty || tty->magic != TTY_MAGIC)
3008 return ret;
3010 un = tty->driver_data;
3011 if (!un || un->magic != DGAP_UNIT_MAGIC)
3012 return ret;
3014 ch = un->un_ch;
3015 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
3016 return ret;
3018 bd = ch->ch_bd;
3019 if (!bd || bd->magic != DGAP_BOARD_MAGIC)
3020 return ret;
3022 DGAP_LOCK(bd->bd_lock, lock_flags);
3023 DGAP_LOCK(ch->ch_lock, lock_flags2);
3025 if (set & TIOCM_RTS) {
3026 ch->ch_mforce |= D_RTS(ch);
3027 ch->ch_mval |= D_RTS(ch);
3030 if (set & TIOCM_DTR) {
3031 ch->ch_mforce |= D_DTR(ch);
3032 ch->ch_mval |= D_DTR(ch);
3035 if (clear & TIOCM_RTS) {
3036 ch->ch_mforce |= D_RTS(ch);
3037 ch->ch_mval &= ~(D_RTS(ch));
3040 if (clear & TIOCM_DTR) {
3041 ch->ch_mforce |= D_DTR(ch);
3042 ch->ch_mval &= ~(D_DTR(ch));
3045 dgap_param(tty);
3047 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
3048 DGAP_UNLOCK(bd->bd_lock, lock_flags);
3050 return 0;
3054 * dgap_tty_send_break()
3056 * Send a Break, called by ld.
3058 static int dgap_tty_send_break(struct tty_struct *tty, int msec)
3060 struct board_t *bd;
3061 struct channel_t *ch;
3062 struct un_t *un;
3063 int ret = -EIO;
3064 ulong lock_flags;
3065 ulong lock_flags2;
3067 if (!tty || tty->magic != TTY_MAGIC)
3068 return ret;
3070 un = tty->driver_data;
3071 if (!un || un->magic != DGAP_UNIT_MAGIC)
3072 return ret;
3074 ch = un->un_ch;
3075 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
3076 return ret;
3078 bd = ch->ch_bd;
3079 if (!bd || bd->magic != DGAP_BOARD_MAGIC)
3080 return ret;
3082 switch (msec) {
3083 case -1:
3084 msec = 0xFFFF;
3085 break;
3086 case 0:
3087 msec = 1;
3088 break;
3089 default:
3090 msec /= 10;
3091 break;
3094 DGAP_LOCK(bd->bd_lock, lock_flags);
3095 DGAP_LOCK(ch->ch_lock, lock_flags2);
3096 #if 0
3097 dgap_cmdw(ch, SBREAK, (u16) SBREAK_TIME, 0);
3098 #endif
3099 dgap_cmdw(ch, SBREAK, (u16) msec, 0);
3101 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
3102 DGAP_UNLOCK(bd->bd_lock, lock_flags);
3104 return 0;
3108 * dgap_tty_wait_until_sent()
3110 * wait until data has been transmitted, called by ld.
3112 static void dgap_tty_wait_until_sent(struct tty_struct *tty, int timeout)
3114 dgap_wait_for_drain(tty);
3118 * dgap_send_xchar()
3120 * send a high priority character, called by ld.
3122 static void dgap_tty_send_xchar(struct tty_struct *tty, char c)
3124 struct board_t *bd;
3125 struct channel_t *ch;
3126 struct un_t *un;
3127 ulong lock_flags;
3128 ulong lock_flags2;
3130 if (!tty || tty->magic != TTY_MAGIC)
3131 return;
3133 un = tty->driver_data;
3134 if (!un || un->magic != DGAP_UNIT_MAGIC)
3135 return;
3137 ch = un->un_ch;
3138 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
3139 return;
3141 bd = ch->ch_bd;
3142 if (!bd || bd->magic != DGAP_BOARD_MAGIC)
3143 return;
3145 DGAP_LOCK(bd->bd_lock, lock_flags);
3146 DGAP_LOCK(ch->ch_lock, lock_flags2);
3149 * This is technically what we should do.
3150 * However, the NIST tests specifically want
3151 * to see each XON or XOFF character that it
3152 * sends, so lets just send each character
3153 * by hand...
3155 #if 0
3156 if (c == STOP_CHAR(tty))
3157 dgap_cmdw(ch, RPAUSE, 0, 0);
3158 else if (c == START_CHAR(tty))
3159 dgap_cmdw(ch, RRESUME, 0, 0);
3160 else
3161 dgap_wmove(ch, &c, 1);
3162 #else
3163 dgap_wmove(ch, &c, 1);
3164 #endif
3166 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
3167 DGAP_UNLOCK(bd->bd_lock, lock_flags);
3169 return;
3173 * Return modem signals to ld.
3175 static int dgap_get_modem_info(struct channel_t *ch, unsigned int __user *value)
3177 int result = 0;
3178 uchar mstat = 0;
3179 ulong lock_flags;
3180 int rc = 0;
3182 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
3183 return -ENXIO;
3185 DGAP_LOCK(ch->ch_lock, lock_flags);
3187 mstat = readb(&(ch->ch_bs->m_stat));
3188 /* Append any outbound signals that might be pending... */
3189 mstat |= ch->ch_mostat;
3191 DGAP_UNLOCK(ch->ch_lock, lock_flags);
3193 result = 0;
3195 if (mstat & D_DTR(ch))
3196 result |= TIOCM_DTR;
3197 if (mstat & D_RTS(ch))
3198 result |= TIOCM_RTS;
3199 if (mstat & D_CTS(ch))
3200 result |= TIOCM_CTS;
3201 if (mstat & D_DSR(ch))
3202 result |= TIOCM_DSR;
3203 if (mstat & D_RI(ch))
3204 result |= TIOCM_RI;
3205 if (mstat & D_CD(ch))
3206 result |= TIOCM_CD;
3208 rc = put_user(result, value);
3210 return rc;
3214 * dgap_set_modem_info()
3216 * Set modem signals, called by ld.
3218 static int dgap_set_modem_info(struct tty_struct *tty, unsigned int command,
3219 unsigned int __user *value)
3221 struct board_t *bd;
3222 struct channel_t *ch;
3223 struct un_t *un;
3224 int ret = -ENXIO;
3225 unsigned int arg = 0;
3226 ulong lock_flags;
3227 ulong lock_flags2;
3229 if (!tty || tty->magic != TTY_MAGIC)
3230 return ret;
3232 un = tty->driver_data;
3233 if (!un || un->magic != DGAP_UNIT_MAGIC)
3234 return ret;
3236 ch = un->un_ch;
3237 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
3238 return ret;
3240 bd = ch->ch_bd;
3241 if (!bd || bd->magic != DGAP_BOARD_MAGIC)
3242 return ret;
3244 ret = get_user(arg, value);
3245 if (ret)
3246 return ret;
3248 switch (command) {
3249 case TIOCMBIS:
3250 if (arg & TIOCM_RTS) {
3251 ch->ch_mforce |= D_RTS(ch);
3252 ch->ch_mval |= D_RTS(ch);
3255 if (arg & TIOCM_DTR) {
3256 ch->ch_mforce |= D_DTR(ch);
3257 ch->ch_mval |= D_DTR(ch);
3260 break;
3262 case TIOCMBIC:
3263 if (arg & TIOCM_RTS) {
3264 ch->ch_mforce |= D_RTS(ch);
3265 ch->ch_mval &= ~(D_RTS(ch));
3268 if (arg & TIOCM_DTR) {
3269 ch->ch_mforce |= D_DTR(ch);
3270 ch->ch_mval &= ~(D_DTR(ch));
3273 break;
3275 case TIOCMSET:
3276 ch->ch_mforce = D_DTR(ch)|D_RTS(ch);
3278 if (arg & TIOCM_RTS)
3279 ch->ch_mval |= D_RTS(ch);
3280 else
3281 ch->ch_mval &= ~(D_RTS(ch));
3283 if (arg & TIOCM_DTR)
3284 ch->ch_mval |= (D_DTR(ch));
3285 else
3286 ch->ch_mval &= ~(D_DTR(ch));
3288 break;
3290 default:
3291 return -EINVAL;
3294 DGAP_LOCK(bd->bd_lock, lock_flags);
3295 DGAP_LOCK(ch->ch_lock, lock_flags2);
3297 dgap_param(tty);
3299 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
3300 DGAP_UNLOCK(bd->bd_lock, lock_flags);
3302 return 0;
3306 * dgap_tty_digigeta()
3308 * Ioctl to get the information for ditty.
3313 static int dgap_tty_digigeta(struct tty_struct *tty,
3314 struct digi_t __user *retinfo)
3316 struct channel_t *ch;
3317 struct un_t *un;
3318 struct digi_t tmp;
3319 ulong lock_flags;
3321 if (!retinfo)
3322 return -EFAULT;
3324 if (!tty || tty->magic != TTY_MAGIC)
3325 return -EFAULT;
3327 un = tty->driver_data;
3328 if (!un || un->magic != DGAP_UNIT_MAGIC)
3329 return -EFAULT;
3331 ch = un->un_ch;
3332 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
3333 return -EFAULT;
3335 memset(&tmp, 0, sizeof(tmp));
3337 DGAP_LOCK(ch->ch_lock, lock_flags);
3338 memcpy(&tmp, &ch->ch_digi, sizeof(tmp));
3339 DGAP_UNLOCK(ch->ch_lock, lock_flags);
3341 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
3342 return -EFAULT;
3344 return 0;
3348 * dgap_tty_digiseta()
3350 * Ioctl to set the information for ditty.
3355 static int dgap_tty_digiseta(struct tty_struct *tty,
3356 struct digi_t __user *new_info)
3358 struct board_t *bd;
3359 struct channel_t *ch;
3360 struct un_t *un;
3361 struct digi_t new_digi;
3362 ulong lock_flags = 0;
3363 unsigned long lock_flags2;
3365 if (!tty || tty->magic != TTY_MAGIC)
3366 return -EFAULT;
3368 un = tty->driver_data;
3369 if (!un || un->magic != DGAP_UNIT_MAGIC)
3370 return -EFAULT;
3372 ch = un->un_ch;
3373 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
3374 return -EFAULT;
3376 bd = ch->ch_bd;
3377 if (!bd || bd->magic != DGAP_BOARD_MAGIC)
3378 return -EFAULT;
3380 if (copy_from_user(&new_digi, new_info, sizeof(struct digi_t)))
3381 return -EFAULT;
3383 DGAP_LOCK(bd->bd_lock, lock_flags);
3384 DGAP_LOCK(ch->ch_lock, lock_flags2);
3386 memcpy(&ch->ch_digi, &new_digi, sizeof(struct digi_t));
3388 if (ch->ch_digi.digi_maxcps < 1)
3389 ch->ch_digi.digi_maxcps = 1;
3391 if (ch->ch_digi.digi_maxcps > 10000)
3392 ch->ch_digi.digi_maxcps = 10000;
3394 if (ch->ch_digi.digi_bufsize < 10)
3395 ch->ch_digi.digi_bufsize = 10;
3397 if (ch->ch_digi.digi_maxchar < 1)
3398 ch->ch_digi.digi_maxchar = 1;
3400 if (ch->ch_digi.digi_maxchar > ch->ch_digi.digi_bufsize)
3401 ch->ch_digi.digi_maxchar = ch->ch_digi.digi_bufsize;
3403 if (ch->ch_digi.digi_onlen > DIGI_PLEN)
3404 ch->ch_digi.digi_onlen = DIGI_PLEN;
3406 if (ch->ch_digi.digi_offlen > DIGI_PLEN)
3407 ch->ch_digi.digi_offlen = DIGI_PLEN;
3409 dgap_param(tty);
3411 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
3412 DGAP_UNLOCK(bd->bd_lock, lock_flags);
3414 return 0;
3418 * dgap_tty_digigetedelay()
3420 * Ioctl to get the current edelay setting.
3425 static int dgap_tty_digigetedelay(struct tty_struct *tty, int __user *retinfo)
3427 struct channel_t *ch;
3428 struct un_t *un;
3429 int tmp;
3430 ulong lock_flags;
3432 if (!retinfo)
3433 return -EFAULT;
3435 if (!tty || tty->magic != TTY_MAGIC)
3436 return -EFAULT;
3438 un = tty->driver_data;
3439 if (!un || un->magic != DGAP_UNIT_MAGIC)
3440 return -EFAULT;
3442 ch = un->un_ch;
3443 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
3444 return -EFAULT;
3446 memset(&tmp, 0, sizeof(tmp));
3448 DGAP_LOCK(ch->ch_lock, lock_flags);
3449 tmp = readw(&(ch->ch_bs->edelay));
3450 DGAP_UNLOCK(ch->ch_lock, lock_flags);
3452 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
3453 return -EFAULT;
3455 return 0;
3459 * dgap_tty_digisetedelay()
3461 * Ioctl to set the EDELAY setting
3464 static int dgap_tty_digisetedelay(struct tty_struct *tty, int __user *new_info)
3466 struct board_t *bd;
3467 struct channel_t *ch;
3468 struct un_t *un;
3469 int new_digi;
3470 ulong lock_flags;
3471 ulong lock_flags2;
3473 if (!tty || tty->magic != TTY_MAGIC)
3474 return -EFAULT;
3476 un = tty->driver_data;
3477 if (!un || un->magic != DGAP_UNIT_MAGIC)
3478 return -EFAULT;
3480 ch = un->un_ch;
3481 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
3482 return -EFAULT;
3484 bd = ch->ch_bd;
3485 if (!bd || bd->magic != DGAP_BOARD_MAGIC)
3486 return -EFAULT;
3488 if (copy_from_user(&new_digi, new_info, sizeof(int)))
3489 return -EFAULT;
3491 DGAP_LOCK(bd->bd_lock, lock_flags);
3492 DGAP_LOCK(ch->ch_lock, lock_flags2);
3494 writew((u16) new_digi, &(ch->ch_bs->edelay));
3496 dgap_param(tty);
3498 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
3499 DGAP_UNLOCK(bd->bd_lock, lock_flags);
3501 return 0;
3505 * dgap_tty_digigetcustombaud()
3507 * Ioctl to get the current custom baud rate setting.
3509 static int dgap_tty_digigetcustombaud(struct tty_struct *tty,
3510 int __user *retinfo)
3512 struct channel_t *ch;
3513 struct un_t *un;
3514 int tmp;
3515 ulong lock_flags;
3517 if (!retinfo)
3518 return -EFAULT;
3520 if (!tty || tty->magic != TTY_MAGIC)
3521 return -EFAULT;
3523 un = tty->driver_data;
3524 if (!un || un->magic != DGAP_UNIT_MAGIC)
3525 return -EFAULT;
3527 ch = un->un_ch;
3528 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
3529 return -EFAULT;
3531 memset(&tmp, 0, sizeof(tmp));
3533 DGAP_LOCK(ch->ch_lock, lock_flags);
3534 tmp = dgap_get_custom_baud(ch);
3535 DGAP_UNLOCK(ch->ch_lock, lock_flags);
3537 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
3538 return -EFAULT;
3540 return 0;
3544 * dgap_tty_digisetcustombaud()
3546 * Ioctl to set the custom baud rate setting
3548 static int dgap_tty_digisetcustombaud(struct tty_struct *tty,
3549 int __user *new_info)
3551 struct board_t *bd;
3552 struct channel_t *ch;
3553 struct un_t *un;
3554 uint new_rate;
3555 ulong lock_flags;
3556 ulong lock_flags2;
3558 if (!tty || tty->magic != TTY_MAGIC)
3559 return -EFAULT;
3561 un = tty->driver_data;
3562 if (!un || un->magic != DGAP_UNIT_MAGIC)
3563 return -EFAULT;
3565 ch = un->un_ch;
3566 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
3567 return -EFAULT;
3569 bd = ch->ch_bd;
3570 if (!bd || bd->magic != DGAP_BOARD_MAGIC)
3571 return -EFAULT;
3574 if (copy_from_user(&new_rate, new_info, sizeof(unsigned int)))
3575 return -EFAULT;
3577 if (bd->bd_flags & BD_FEP5PLUS) {
3579 DGAP_LOCK(bd->bd_lock, lock_flags);
3580 DGAP_LOCK(ch->ch_lock, lock_flags2);
3582 ch->ch_custom_speed = new_rate;
3584 dgap_param(tty);
3586 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
3587 DGAP_UNLOCK(bd->bd_lock, lock_flags);
3590 return 0;
3594 * dgap_set_termios()
3596 static void dgap_tty_set_termios(struct tty_struct *tty,
3597 struct ktermios *old_termios)
3599 struct board_t *bd;
3600 struct channel_t *ch;
3601 struct un_t *un;
3602 unsigned long lock_flags;
3603 unsigned long lock_flags2;
3605 if (!tty || tty->magic != TTY_MAGIC)
3606 return;
3608 un = tty->driver_data;
3609 if (!un || un->magic != DGAP_UNIT_MAGIC)
3610 return;
3612 ch = un->un_ch;
3613 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
3614 return;
3616 bd = ch->ch_bd;
3617 if (!bd || bd->magic != DGAP_BOARD_MAGIC)
3618 return;
3620 DGAP_LOCK(bd->bd_lock, lock_flags);
3621 DGAP_LOCK(ch->ch_lock, lock_flags2);
3623 ch->ch_c_cflag = tty->termios.c_cflag;
3624 ch->ch_c_iflag = tty->termios.c_iflag;
3625 ch->ch_c_oflag = tty->termios.c_oflag;
3626 ch->ch_c_lflag = tty->termios.c_lflag;
3627 ch->ch_startc = tty->termios.c_cc[VSTART];
3628 ch->ch_stopc = tty->termios.c_cc[VSTOP];
3630 dgap_carrier(ch);
3631 dgap_param(tty);
3633 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
3634 DGAP_UNLOCK(bd->bd_lock, lock_flags);
3637 static void dgap_tty_throttle(struct tty_struct *tty)
3639 struct board_t *bd;
3640 struct channel_t *ch;
3641 struct un_t *un;
3642 ulong lock_flags;
3643 ulong lock_flags2;
3645 if (!tty || tty->magic != TTY_MAGIC)
3646 return;
3648 un = tty->driver_data;
3649 if (!un || un->magic != DGAP_UNIT_MAGIC)
3650 return;
3652 ch = un->un_ch;
3653 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
3654 return;
3656 bd = ch->ch_bd;
3657 if (!bd || bd->magic != DGAP_BOARD_MAGIC)
3658 return;
3660 DGAP_LOCK(bd->bd_lock, lock_flags);
3661 DGAP_LOCK(ch->ch_lock, lock_flags2);
3663 ch->ch_flags |= (CH_RXBLOCK);
3664 #if 1
3665 dgap_cmdw(ch, RPAUSE, 0, 0);
3666 #endif
3668 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
3669 DGAP_UNLOCK(bd->bd_lock, lock_flags);
3673 static void dgap_tty_unthrottle(struct tty_struct *tty)
3675 struct board_t *bd;
3676 struct channel_t *ch;
3677 struct un_t *un;
3678 ulong lock_flags;
3679 ulong lock_flags2;
3681 if (!tty || tty->magic != TTY_MAGIC)
3682 return;
3684 un = tty->driver_data;
3685 if (!un || un->magic != DGAP_UNIT_MAGIC)
3686 return;
3688 ch = un->un_ch;
3689 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
3690 return;
3692 bd = ch->ch_bd;
3693 if (!bd || bd->magic != DGAP_BOARD_MAGIC)
3694 return;
3696 DGAP_LOCK(bd->bd_lock, lock_flags);
3697 DGAP_LOCK(ch->ch_lock, lock_flags2);
3699 ch->ch_flags &= ~(CH_RXBLOCK);
3701 #if 1
3702 dgap_cmdw(ch, RRESUME, 0, 0);
3703 #endif
3705 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
3706 DGAP_UNLOCK(bd->bd_lock, lock_flags);
3709 static void dgap_tty_start(struct tty_struct *tty)
3711 struct board_t *bd;
3712 struct channel_t *ch;
3713 struct un_t *un;
3714 ulong lock_flags;
3715 ulong lock_flags2;
3717 if (!tty || tty->magic != TTY_MAGIC)
3718 return;
3720 un = tty->driver_data;
3721 if (!un || un->magic != DGAP_UNIT_MAGIC)
3722 return;
3724 ch = un->un_ch;
3725 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
3726 return;
3728 bd = ch->ch_bd;
3729 if (!bd || bd->magic != DGAP_BOARD_MAGIC)
3730 return;
3732 DGAP_LOCK(bd->bd_lock, lock_flags);
3733 DGAP_LOCK(ch->ch_lock, lock_flags2);
3735 dgap_cmdw(ch, RESUMETX, 0, 0);
3737 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
3738 DGAP_UNLOCK(bd->bd_lock, lock_flags);
3742 static void dgap_tty_stop(struct tty_struct *tty)
3744 struct board_t *bd;
3745 struct channel_t *ch;
3746 struct un_t *un;
3747 ulong lock_flags;
3748 ulong lock_flags2;
3750 if (!tty || tty->magic != TTY_MAGIC)
3751 return;
3753 un = tty->driver_data;
3754 if (!un || un->magic != DGAP_UNIT_MAGIC)
3755 return;
3757 ch = un->un_ch;
3758 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
3759 return;
3761 bd = ch->ch_bd;
3762 if (!bd || bd->magic != DGAP_BOARD_MAGIC)
3763 return;
3765 DGAP_LOCK(bd->bd_lock, lock_flags);
3766 DGAP_LOCK(ch->ch_lock, lock_flags2);
3768 dgap_cmdw(ch, PAUSETX, 0, 0);
3770 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
3771 DGAP_UNLOCK(bd->bd_lock, lock_flags);
3776 * dgap_tty_flush_chars()
3778 * Flush the cook buffer
3780 * Note to self, and any other poor souls who venture here:
3782 * flush in this case DOES NOT mean dispose of the data.
3783 * instead, it means "stop buffering and send it if you
3784 * haven't already." Just guess how I figured that out... SRW 2-Jun-98
3786 * It is also always called in interrupt context - JAR 8-Sept-99
3788 static void dgap_tty_flush_chars(struct tty_struct *tty)
3790 struct board_t *bd;
3791 struct channel_t *ch;
3792 struct un_t *un;
3793 ulong lock_flags;
3794 ulong lock_flags2;
3796 if (!tty || tty->magic != TTY_MAGIC)
3797 return;
3799 un = tty->driver_data;
3800 if (!un || un->magic != DGAP_UNIT_MAGIC)
3801 return;
3803 ch = un->un_ch;
3804 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
3805 return;
3807 bd = ch->ch_bd;
3808 if (!bd || bd->magic != DGAP_BOARD_MAGIC)
3809 return;
3811 DGAP_LOCK(bd->bd_lock, lock_flags);
3812 DGAP_LOCK(ch->ch_lock, lock_flags2);
3814 /* TODO: Do something here */
3816 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
3817 DGAP_UNLOCK(bd->bd_lock, lock_flags);
3821 * dgap_tty_flush_buffer()
3823 * Flush Tx buffer (make in == out)
3825 static void dgap_tty_flush_buffer(struct tty_struct *tty)
3827 struct board_t *bd;
3828 struct channel_t *ch;
3829 struct un_t *un;
3830 ulong lock_flags;
3831 ulong lock_flags2;
3832 u16 head = 0;
3834 if (!tty || tty->magic != TTY_MAGIC)
3835 return;
3837 un = tty->driver_data;
3838 if (!un || un->magic != DGAP_UNIT_MAGIC)
3839 return;
3841 ch = un->un_ch;
3842 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
3843 return;
3845 bd = ch->ch_bd;
3846 if (!bd || bd->magic != DGAP_BOARD_MAGIC)
3847 return;
3849 DGAP_LOCK(bd->bd_lock, lock_flags);
3850 DGAP_LOCK(ch->ch_lock, lock_flags2);
3852 ch->ch_flags &= ~CH_STOP;
3853 head = readw(&(ch->ch_bs->tx_head));
3854 dgap_cmdw(ch, FLUSHTX, (u16) head, 0);
3855 dgap_cmdw(ch, RESUMETX, 0, 0);
3856 if (ch->ch_tun.un_flags & (UN_LOW|UN_EMPTY)) {
3857 ch->ch_tun.un_flags &= ~(UN_LOW|UN_EMPTY);
3858 wake_up_interruptible(&ch->ch_tun.un_flags_wait);
3860 if (ch->ch_pun.un_flags & (UN_LOW|UN_EMPTY)) {
3861 ch->ch_pun.un_flags &= ~(UN_LOW|UN_EMPTY);
3862 wake_up_interruptible(&ch->ch_pun.un_flags_wait);
3865 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
3866 DGAP_UNLOCK(bd->bd_lock, lock_flags);
3867 if (waitqueue_active(&tty->write_wait))
3868 wake_up_interruptible(&tty->write_wait);
3869 tty_wakeup(tty);
3872 /*****************************************************************************
3874 * The IOCTL function and all of its helpers
3876 *****************************************************************************/
3879 * dgap_tty_ioctl()
3881 * The usual assortment of ioctl's
3883 static int dgap_tty_ioctl(struct tty_struct *tty, unsigned int cmd,
3884 unsigned long arg)
3886 struct board_t *bd;
3887 struct channel_t *ch;
3888 struct un_t *un;
3889 int rc;
3890 u16 head = 0;
3891 ulong lock_flags = 0;
3892 ulong lock_flags2 = 0;
3893 void __user *uarg = (void __user *) arg;
3895 if (!tty || tty->magic != TTY_MAGIC)
3896 return -ENODEV;
3898 un = tty->driver_data;
3899 if (!un || un->magic != DGAP_UNIT_MAGIC)
3900 return -ENODEV;
3902 ch = un->un_ch;
3903 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
3904 return -ENODEV;
3906 bd = ch->ch_bd;
3907 if (!bd || bd->magic != DGAP_BOARD_MAGIC)
3908 return -ENODEV;
3910 DGAP_LOCK(bd->bd_lock, lock_flags);
3911 DGAP_LOCK(ch->ch_lock, lock_flags2);
3913 if (un->un_open_count <= 0) {
3914 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
3915 DGAP_UNLOCK(bd->bd_lock, lock_flags);
3916 return -EIO;
3919 switch (cmd) {
3921 /* Here are all the standard ioctl's that we MUST implement */
3923 case TCSBRK:
3925 * TCSBRK is SVID version: non-zero arg --> no break
3926 * this behaviour is exploited by tcdrain().
3928 * According to POSIX.1 spec (7.2.2.1.2) breaks should be
3929 * between 0.25 and 0.5 seconds so we'll ask for something
3930 * in the middle: 0.375 seconds.
3932 rc = tty_check_change(tty);
3933 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
3934 DGAP_UNLOCK(bd->bd_lock, lock_flags);
3935 if (rc)
3936 return rc;
3938 rc = dgap_wait_for_drain(tty);
3940 if (rc)
3941 return -EINTR;
3943 DGAP_LOCK(bd->bd_lock, lock_flags);
3944 DGAP_LOCK(ch->ch_lock, lock_flags2);
3946 if (((cmd == TCSBRK) && (!arg)) || (cmd == TCSBRKP))
3947 dgap_cmdw(ch, SBREAK, (u16) SBREAK_TIME, 0);
3949 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
3950 DGAP_UNLOCK(bd->bd_lock, lock_flags);
3952 return 0;
3954 case TCSBRKP:
3955 /* support for POSIX tcsendbreak()
3957 * According to POSIX.1 spec (7.2.2.1.2) breaks should be
3958 * between 0.25 and 0.5 seconds so we'll ask for something
3959 * in the middle: 0.375 seconds.
3961 rc = tty_check_change(tty);
3962 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
3963 DGAP_UNLOCK(bd->bd_lock, lock_flags);
3964 if (rc)
3965 return rc;
3967 rc = dgap_wait_for_drain(tty);
3968 if (rc)
3969 return -EINTR;
3971 DGAP_LOCK(bd->bd_lock, lock_flags);
3972 DGAP_LOCK(ch->ch_lock, lock_flags2);
3974 dgap_cmdw(ch, SBREAK, (u16) SBREAK_TIME, 0);
3976 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
3977 DGAP_UNLOCK(bd->bd_lock, lock_flags);
3979 return 0;
3981 case TIOCSBRK:
3983 * FEP5 doesn't support turning on a break unconditionally.
3984 * The FEP5 device will stop sending a break automatically
3985 * after the specified time value that was sent when turning on
3986 * the break.
3988 rc = tty_check_change(tty);
3989 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
3990 DGAP_UNLOCK(bd->bd_lock, lock_flags);
3991 if (rc)
3992 return rc;
3994 rc = dgap_wait_for_drain(tty);
3995 if (rc)
3996 return -EINTR;
3998 DGAP_LOCK(bd->bd_lock, lock_flags);
3999 DGAP_LOCK(ch->ch_lock, lock_flags2);
4001 dgap_cmdw(ch, SBREAK, (u16) SBREAK_TIME, 0);
4003 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
4004 DGAP_UNLOCK(bd->bd_lock, lock_flags);
4006 return 0;
4008 case TIOCCBRK:
4010 * FEP5 doesn't support turning off a break unconditionally.
4011 * The FEP5 device will stop sending a break automatically
4012 * after the specified time value that was sent when turning on
4013 * the break.
4015 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
4016 DGAP_UNLOCK(bd->bd_lock, lock_flags);
4017 return 0;
4019 case TIOCGSOFTCAR:
4021 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
4022 DGAP_UNLOCK(bd->bd_lock, lock_flags);
4024 rc = put_user(C_CLOCAL(tty) ? 1 : 0,
4025 (unsigned long __user *) arg);
4026 return rc;
4028 case TIOCSSOFTCAR:
4029 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
4030 DGAP_UNLOCK(bd->bd_lock, lock_flags);
4032 rc = get_user(arg, (unsigned long __user *) arg);
4033 if (rc)
4034 return rc;
4036 DGAP_LOCK(bd->bd_lock, lock_flags);
4037 DGAP_LOCK(ch->ch_lock, lock_flags2);
4038 tty->termios.c_cflag = ((tty->termios.c_cflag & ~CLOCAL) |
4039 (arg ? CLOCAL : 0));
4040 dgap_param(tty);
4041 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
4042 DGAP_UNLOCK(bd->bd_lock, lock_flags);
4044 return 0;
4046 case TIOCMGET:
4047 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
4048 DGAP_UNLOCK(bd->bd_lock, lock_flags);
4049 return dgap_get_modem_info(ch, uarg);
4051 case TIOCMBIS:
4052 case TIOCMBIC:
4053 case TIOCMSET:
4054 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
4055 DGAP_UNLOCK(bd->bd_lock, lock_flags);
4056 return dgap_set_modem_info(tty, cmd, uarg);
4059 * Here are any additional ioctl's that we want to implement
4062 case TCFLSH:
4064 * The linux tty driver doesn't have a flush
4065 * input routine for the driver, assuming all backed
4066 * up data is in the line disc. buffers. However,
4067 * we all know that's not the case. Here, we
4068 * act on the ioctl, but then lie and say we didn't
4069 * so the line discipline will process the flush
4070 * also.
4072 rc = tty_check_change(tty);
4073 if (rc) {
4074 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
4075 DGAP_UNLOCK(bd->bd_lock, lock_flags);
4076 return rc;
4079 if ((arg == TCIFLUSH) || (arg == TCIOFLUSH)) {
4080 if (!(un->un_type == DGAP_PRINT)) {
4081 head = readw(&(ch->ch_bs->rx_head));
4082 writew(head, &(ch->ch_bs->rx_tail));
4083 writeb(0, &(ch->ch_bs->orun));
4087 if ((arg == TCOFLUSH) || (arg == TCIOFLUSH)) {
4088 ch->ch_flags &= ~CH_STOP;
4089 head = readw(&(ch->ch_bs->tx_head));
4090 dgap_cmdw(ch, FLUSHTX, (u16) head, 0);
4091 dgap_cmdw(ch, RESUMETX, 0, 0);
4092 if (ch->ch_tun.un_flags & (UN_LOW|UN_EMPTY)) {
4093 ch->ch_tun.un_flags &= ~(UN_LOW|UN_EMPTY);
4094 wake_up_interruptible(&ch->ch_tun.un_flags_wait);
4096 if (ch->ch_pun.un_flags & (UN_LOW|UN_EMPTY)) {
4097 ch->ch_pun.un_flags &= ~(UN_LOW|UN_EMPTY);
4098 wake_up_interruptible(&ch->ch_pun.un_flags_wait);
4100 if (waitqueue_active(&tty->write_wait))
4101 wake_up_interruptible(&tty->write_wait);
4103 /* Can't hold any locks when calling tty_wakeup! */
4104 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
4105 DGAP_UNLOCK(bd->bd_lock, lock_flags);
4106 tty_wakeup(tty);
4107 DGAP_LOCK(bd->bd_lock, lock_flags);
4108 DGAP_LOCK(ch->ch_lock, lock_flags2);
4111 /* pretend we didn't recognize this IOCTL */
4112 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
4113 DGAP_UNLOCK(bd->bd_lock, lock_flags);
4115 return -ENOIOCTLCMD;
4117 case TCSETSF:
4118 case TCSETSW:
4120 * The linux tty driver doesn't have a flush
4121 * input routine for the driver, assuming all backed
4122 * up data is in the line disc. buffers. However,
4123 * we all know that's not the case. Here, we
4124 * act on the ioctl, but then lie and say we didn't
4125 * so the line discipline will process the flush
4126 * also.
4128 if (cmd == TCSETSF) {
4129 /* flush rx */
4130 ch->ch_flags &= ~CH_STOP;
4131 head = readw(&(ch->ch_bs->rx_head));
4132 writew(head, &(ch->ch_bs->rx_tail));
4135 /* now wait for all the output to drain */
4136 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
4137 DGAP_UNLOCK(bd->bd_lock, lock_flags);
4138 rc = dgap_wait_for_drain(tty);
4139 if (rc)
4140 return -EINTR;
4142 /* pretend we didn't recognize this */
4143 return -ENOIOCTLCMD;
4145 case TCSETAW:
4147 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
4148 DGAP_UNLOCK(bd->bd_lock, lock_flags);
4149 rc = dgap_wait_for_drain(tty);
4150 if (rc)
4151 return -EINTR;
4153 /* pretend we didn't recognize this */
4154 return -ENOIOCTLCMD;
4156 case TCXONC:
4158 * The Linux Line Discipline (LD) would do this for us if we
4159 * let it, but we have the special firmware options to do this
4160 * the "right way" regardless of hardware or software flow
4161 * control so we'll do it outselves instead of letting the LD
4162 * do it.
4164 rc = tty_check_change(tty);
4165 if (rc) {
4166 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
4167 DGAP_UNLOCK(bd->bd_lock, lock_flags);
4168 return rc;
4171 switch (arg) {
4173 case TCOON:
4174 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
4175 DGAP_UNLOCK(bd->bd_lock, lock_flags);
4176 dgap_tty_start(tty);
4177 return 0;
4178 case TCOOFF:
4179 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
4180 DGAP_UNLOCK(bd->bd_lock, lock_flags);
4181 dgap_tty_stop(tty);
4182 return 0;
4183 case TCION:
4184 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
4185 DGAP_UNLOCK(bd->bd_lock, lock_flags);
4186 /* Make the ld do it */
4187 return -ENOIOCTLCMD;
4188 case TCIOFF:
4189 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
4190 DGAP_UNLOCK(bd->bd_lock, lock_flags);
4191 /* Make the ld do it */
4192 return -ENOIOCTLCMD;
4193 default:
4194 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
4195 DGAP_UNLOCK(bd->bd_lock, lock_flags);
4196 return -EINVAL;
4199 case DIGI_GETA:
4200 /* get information for ditty */
4201 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
4202 DGAP_UNLOCK(bd->bd_lock, lock_flags);
4203 return dgap_tty_digigeta(tty, uarg);
4205 case DIGI_SETAW:
4206 case DIGI_SETAF:
4208 /* set information for ditty */
4209 if (cmd == (DIGI_SETAW)) {
4211 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
4212 DGAP_UNLOCK(bd->bd_lock, lock_flags);
4213 rc = dgap_wait_for_drain(tty);
4214 if (rc)
4215 return -EINTR;
4216 DGAP_LOCK(bd->bd_lock, lock_flags);
4217 DGAP_LOCK(ch->ch_lock, lock_flags2);
4218 } else
4219 tty_ldisc_flush(tty);
4220 /* fall thru */
4222 case DIGI_SETA:
4223 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
4224 DGAP_UNLOCK(bd->bd_lock, lock_flags);
4225 return dgap_tty_digiseta(tty, uarg);
4227 case DIGI_GEDELAY:
4228 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
4229 DGAP_UNLOCK(bd->bd_lock, lock_flags);
4230 return dgap_tty_digigetedelay(tty, uarg);
4232 case DIGI_SEDELAY:
4233 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
4234 DGAP_UNLOCK(bd->bd_lock, lock_flags);
4235 return dgap_tty_digisetedelay(tty, uarg);
4237 case DIGI_GETCUSTOMBAUD:
4238 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
4239 DGAP_UNLOCK(bd->bd_lock, lock_flags);
4240 return dgap_tty_digigetcustombaud(tty, uarg);
4242 case DIGI_SETCUSTOMBAUD:
4243 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
4244 DGAP_UNLOCK(bd->bd_lock, lock_flags);
4245 return dgap_tty_digisetcustombaud(tty, uarg);
4247 case DIGI_RESET_PORT:
4248 dgap_firmware_reset_port(ch);
4249 dgap_param(tty);
4250 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
4251 DGAP_UNLOCK(bd->bd_lock, lock_flags);
4252 return 0;
4254 default:
4255 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
4256 DGAP_UNLOCK(bd->bd_lock, lock_flags);
4258 return -ENOIOCTLCMD;
4262 static int dgap_after_config_loaded(int board)
4265 * Initialize KME waitqueues...
4267 init_waitqueue_head(&(dgap_Board[board]->kme_wait));
4270 * allocate flip buffer for board.
4272 dgap_Board[board]->flipbuf = kmalloc(MYFLIPLEN, GFP_ATOMIC);
4273 if (!dgap_Board[board]->flipbuf)
4274 return -ENOMEM;
4276 dgap_Board[board]->flipflagbuf = kmalloc(MYFLIPLEN, GFP_ATOMIC);
4277 if (!dgap_Board[board]->flipflagbuf) {
4278 kfree(dgap_Board[board]->flipbuf);
4279 return -ENOMEM;
4282 return 0;
4286 * Create pr and tty device entries
4288 static int dgap_tty_register_ports(struct board_t *brd)
4290 struct channel_t *ch;
4291 int i;
4293 brd->SerialPorts = kcalloc(brd->nasync, sizeof(*brd->SerialPorts),
4294 GFP_KERNEL);
4295 if (brd->SerialPorts == NULL)
4296 return -ENOMEM;
4297 for (i = 0; i < brd->nasync; i++)
4298 tty_port_init(&brd->SerialPorts[i]);
4300 brd->PrinterPorts = kcalloc(brd->nasync, sizeof(*brd->PrinterPorts),
4301 GFP_KERNEL);
4302 if (brd->PrinterPorts == NULL) {
4303 kfree(brd->SerialPorts);
4304 return -ENOMEM;
4306 for (i = 0; i < brd->nasync; i++)
4307 tty_port_init(&brd->PrinterPorts[i]);
4309 ch = brd->channels[0];
4310 for (i = 0; i < brd->nasync; i++, ch = brd->channels[i]) {
4312 struct device *classp;
4314 classp = tty_port_register_device(&brd->SerialPorts[i],
4315 brd->SerialDriver,
4316 brd->firstminor + i, NULL);
4318 dgap_create_tty_sysfs(&ch->ch_tun, classp);
4319 ch->ch_tun.un_sysfs = classp;
4321 classp = tty_port_register_device(&brd->PrinterPorts[i],
4322 brd->PrintDriver,
4323 brd->firstminor + i, NULL);
4325 dgap_create_tty_sysfs(&ch->ch_pun, classp);
4326 ch->ch_pun.un_sysfs = classp;
4328 dgap_create_ports_sysfiles(brd);
4330 return 0;
4334 * Copies the BIOS code from the user to the board,
4335 * and starts the BIOS running.
4337 static void dgap_do_bios_load(struct board_t *brd, uchar __user *ubios, int len)
4339 uchar *addr;
4340 uint offset;
4341 int i;
4343 if (!brd || (brd->magic != DGAP_BOARD_MAGIC) || !brd->re_map_membase)
4344 return;
4346 addr = brd->re_map_membase;
4349 * clear POST area
4351 for (i = 0; i < 16; i++)
4352 writeb(0, addr + POSTAREA + i);
4355 * Download bios
4357 offset = 0x1000;
4358 memcpy_toio(addr + offset, ubios, len);
4360 writel(0x0bf00401, addr);
4361 writel(0, (addr + 4));
4363 /* Clear the reset, and change states. */
4364 writeb(FEPCLR, brd->re_map_port);
4368 * Checks to see if the BIOS completed running on the card.
4370 static void dgap_do_wait_for_bios(struct board_t *brd)
4372 uchar *addr;
4373 u16 word;
4374 u16 err1;
4375 u16 err2;
4377 if (!brd || (brd->magic != DGAP_BOARD_MAGIC) || !brd->re_map_membase)
4378 return;
4380 addr = brd->re_map_membase;
4381 word = readw(addr + POSTAREA);
4384 * It can take 5-6 seconds for a board to
4385 * pass the bios self test and post results.
4386 * Give it 10 seconds.
4388 brd->wait_for_bios = 0;
4389 while (brd->wait_for_bios < 1000) {
4390 /* Check to see if BIOS thinks board is good. (GD). */
4391 if (word == *(u16 *) "GD") {
4392 brd->state = FINISHED_BIOS_LOAD;
4393 return;
4395 msleep_interruptible(10);
4396 brd->wait_for_bios++;
4397 word = readw(addr + POSTAREA);
4400 /* Gave up on board after too long of time taken */
4401 err1 = readw(addr + SEQUENCE);
4402 err2 = readw(addr + ERROR);
4403 pr_warn("dgap: %s failed diagnostics. Error #(%x,%x).\n",
4404 brd->name, err1, err2);
4405 brd->state = BOARD_FAILED;
4406 brd->dpastatus = BD_NOBIOS;
4410 * Copies the FEP code from the user to the board,
4411 * and starts the FEP running.
4413 static void dgap_do_fep_load(struct board_t *brd, uchar *ufep, int len)
4415 uchar *addr;
4416 uint offset;
4418 if (!brd || (brd->magic != DGAP_BOARD_MAGIC) || !brd->re_map_membase)
4419 return;
4421 addr = brd->re_map_membase;
4424 * Download FEP
4426 offset = 0x1000;
4427 memcpy_toio(addr + offset, ufep, len);
4430 * If board is a concentrator product, we need to give
4431 * it its config string describing how the concentrators look.
4433 if ((brd->type == PCX) || (brd->type == PEPC)) {
4434 uchar string[100];
4435 uchar *config, *xconfig;
4436 int i = 0;
4438 xconfig = dgap_create_config_string(brd, string);
4440 /* Write string to board memory */
4441 config = addr + CONFIG;
4442 for (; i < CONFIGSIZE; i++, config++, xconfig++) {
4443 writeb(*xconfig, config);
4444 if ((*xconfig & 0xff) == 0xff)
4445 break;
4449 writel(0xbfc01004, (addr + 0xc34));
4450 writel(0x3, (addr + 0xc30));
4455 * Waits for the FEP to report thats its ready for us to use.
4457 static void dgap_do_wait_for_fep(struct board_t *brd)
4459 uchar *addr;
4460 u16 word;
4461 u16 err1;
4462 u16 err2;
4464 if (!brd || (brd->magic != DGAP_BOARD_MAGIC) || !brd->re_map_membase)
4465 return;
4467 addr = brd->re_map_membase;
4468 word = readw(addr + FEPSTAT);
4471 * It can take 2-3 seconds for the FEP to
4472 * be up and running. Give it 5 secs.
4474 brd->wait_for_fep = 0;
4475 while (brd->wait_for_fep < 500) {
4476 /* Check to see if FEP is up and running now. */
4477 if (word == *(u16 *) "OS") {
4478 brd->state = FINISHED_FEP_LOAD;
4480 * Check to see if the board can support FEP5+ commands.
4482 word = readw(addr + FEP5_PLUS);
4483 if (word == *(u16 *) "5A")
4484 brd->bd_flags |= BD_FEP5PLUS;
4486 return;
4488 msleep_interruptible(10);
4489 brd->wait_for_fep++;
4490 word = readw(addr + FEPSTAT);
4493 /* Gave up on board after too long of time taken */
4494 err1 = readw(addr + SEQUENCE);
4495 err2 = readw(addr + ERROR);
4496 pr_warn("dgap: FEPOS for %s not functioning. Error #(%x,%x).\n",
4497 brd->name, err1, err2);
4498 brd->state = BOARD_FAILED;
4499 brd->dpastatus = BD_NOFEP;
4503 * Physically forces the FEP5 card to reset itself.
4505 static void dgap_do_reset_board(struct board_t *brd)
4507 uchar check;
4508 u32 check1;
4509 u32 check2;
4510 int i = 0;
4512 if (!brd || (brd->magic != DGAP_BOARD_MAGIC) ||
4513 !brd->re_map_membase || !brd->re_map_port)
4514 return;
4516 /* FEPRST does not vary among supported boards */
4517 writeb(FEPRST, brd->re_map_port);
4519 for (i = 0; i <= 1000; i++) {
4520 check = readb(brd->re_map_port) & 0xe;
4521 if (check == FEPRST)
4522 break;
4523 udelay(10);
4526 if (i > 1000) {
4527 pr_warn("dgap: Board not resetting... Failing board.\n");
4528 brd->state = BOARD_FAILED;
4529 brd->dpastatus = BD_NOFEP;
4530 return;
4534 * Make sure there really is memory out there.
4536 writel(0xa55a3cc3, (brd->re_map_membase + LOWMEM));
4537 writel(0x5aa5c33c, (brd->re_map_membase + HIGHMEM));
4538 check1 = readl(brd->re_map_membase + LOWMEM);
4539 check2 = readl(brd->re_map_membase + HIGHMEM);
4541 if ((check1 != 0xa55a3cc3) || (check2 != 0x5aa5c33c)) {
4542 pr_warn("dgap: No memory at %p for board.\n",
4543 brd->re_map_membase);
4544 brd->state = BOARD_FAILED;
4545 brd->dpastatus = BD_NOFEP;
4546 return;
4549 if (brd->state != BOARD_FAILED)
4550 brd->state = FINISHED_RESET;
4553 #ifdef DIGI_CONCENTRATORS_SUPPORTED
4555 * Sends a concentrator image into the FEP5 board.
4557 static void dgap_do_conc_load(struct board_t *brd, uchar *uaddr, int len)
4559 char *vaddr;
4560 u16 offset = 0;
4561 struct downld_t *to_dp;
4563 if (!brd || (brd->magic != DGAP_BOARD_MAGIC) || !brd->re_map_membase)
4564 return;
4566 vaddr = brd->re_map_membase;
4568 offset = readw((u16 *) (vaddr + DOWNREQ));
4569 to_dp = (struct downld_t *) (vaddr + (int) offset);
4570 memcpy_toio(to_dp, uaddr, len);
4572 /* Tell card we have data for it */
4573 writew(0, vaddr + (DOWNREQ));
4575 brd->conc_dl_status = NO_PENDING_CONCENTRATOR_REQUESTS;
4577 #endif
4579 #define EXPANSION_ROM_SIZE (64 * 1024)
4580 #define FEP5_ROM_MAGIC (0xFEFFFFFF)
4582 static void dgap_get_vpd(struct board_t *brd)
4584 u32 magic;
4585 u32 base_offset;
4586 u16 rom_offset;
4587 u16 vpd_offset;
4588 u16 image_length;
4589 u16 i;
4590 uchar byte1;
4591 uchar byte2;
4594 * Poke the magic number at the PCI Rom Address location.
4595 * If VPD is supported, the value read from that address
4596 * will be non-zero.
4598 magic = FEP5_ROM_MAGIC;
4599 pci_write_config_dword(brd->pdev, PCI_ROM_ADDRESS, magic);
4600 pci_read_config_dword(brd->pdev, PCI_ROM_ADDRESS, &magic);
4602 /* VPD not supported, bail */
4603 if (!magic)
4604 return;
4607 * To get to the OTPROM memory, we have to send the boards base
4608 * address or'ed with 1 into the PCI Rom Address location.
4610 magic = brd->membase | 0x01;
4611 pci_write_config_dword(brd->pdev, PCI_ROM_ADDRESS, magic);
4612 pci_read_config_dword(brd->pdev, PCI_ROM_ADDRESS, &magic);
4614 byte1 = readb(brd->re_map_membase);
4615 byte2 = readb(brd->re_map_membase + 1);
4618 * If the board correctly swapped to the OTPROM memory,
4619 * the first 2 bytes (header) should be 0x55, 0xAA
4621 if (byte1 == 0x55 && byte2 == 0xAA) {
4623 base_offset = 0;
4626 * We have to run through all the OTPROM memory looking
4627 * for the VPD offset.
4629 while (base_offset <= EXPANSION_ROM_SIZE) {
4632 * Lots of magic numbers here.
4634 * The VPD offset is located inside the ROM Data
4635 * Structure.
4637 * We also have to remember the length of each
4638 * ROM Data Structure, so we can "hop" to the next
4639 * entry if the VPD isn't in the current
4640 * ROM Data Structure.
4642 rom_offset = readw(brd->re_map_membase +
4643 base_offset + 0x18);
4644 image_length = readw(brd->re_map_membase +
4645 rom_offset + 0x10) * 512;
4646 vpd_offset = readw(brd->re_map_membase +
4647 rom_offset + 0x08);
4649 /* Found the VPD entry */
4650 if (vpd_offset)
4651 break;
4653 /* We didn't find a VPD entry, go to next ROM entry. */
4654 base_offset += image_length;
4656 byte1 = readb(brd->re_map_membase + base_offset);
4657 byte2 = readb(brd->re_map_membase + base_offset + 1);
4660 * If the new ROM offset doesn't have 0x55, 0xAA
4661 * as its header, we have run out of ROM.
4663 if (byte1 != 0x55 || byte2 != 0xAA)
4664 break;
4668 * If we have a VPD offset, then mark the board
4669 * as having a valid VPD, and copy VPDSIZE (512) bytes of
4670 * that VPD to the buffer we have in our board structure.
4672 if (vpd_offset) {
4673 brd->bd_flags |= BD_HAS_VPD;
4674 for (i = 0; i < VPDSIZE; i++) {
4675 brd->vpd[i] = readb(brd->re_map_membase +
4676 vpd_offset + i);
4682 * We MUST poke the magic number at the PCI Rom Address location again.
4683 * This makes the card report the regular board memory back to us,
4684 * rather than the OTPROM memory.
4686 magic = FEP5_ROM_MAGIC;
4687 pci_write_config_dword(brd->pdev, PCI_ROM_ADDRESS, magic);
4691 * Our board poller function.
4693 static void dgap_poll_tasklet(unsigned long data)
4695 struct board_t *bd = (struct board_t *) data;
4696 ulong lock_flags;
4697 ulong lock_flags2;
4698 char *vaddr;
4699 u16 head, tail;
4701 if (!bd || (bd->magic != DGAP_BOARD_MAGIC))
4702 return;
4704 if (bd->inhibit_poller)
4705 return;
4707 DGAP_LOCK(bd->bd_lock, lock_flags);
4709 vaddr = bd->re_map_membase;
4712 * If board is ready, parse deeper to see if there is anything to do.
4714 if (bd->state == BOARD_READY) {
4716 struct ev_t *eaddr = NULL;
4718 if (!bd->re_map_membase) {
4719 DGAP_UNLOCK(bd->bd_lock, lock_flags);
4720 return;
4722 if (!bd->re_map_port) {
4723 DGAP_UNLOCK(bd->bd_lock, lock_flags);
4724 return;
4727 if (!bd->nasync)
4728 goto out;
4730 eaddr = (struct ev_t *) (vaddr + EVBUF);
4732 /* Get our head and tail */
4733 head = readw(&(eaddr->ev_head));
4734 tail = readw(&(eaddr->ev_tail));
4737 * If there is an event pending. Go service it.
4739 if (head != tail) {
4740 DGAP_UNLOCK(bd->bd_lock, lock_flags);
4741 dgap_event(bd);
4742 DGAP_LOCK(bd->bd_lock, lock_flags);
4745 out:
4747 * If board is doing interrupts, ACK the interrupt.
4749 if (bd && bd->intr_running)
4750 readb(bd->re_map_port + 2);
4752 DGAP_UNLOCK(bd->bd_lock, lock_flags);
4753 return;
4756 /* Our state machine to get the board up and running */
4758 /* Reset board */
4759 if (bd->state == NEED_RESET) {
4761 /* Get VPD info */
4762 dgap_get_vpd(bd);
4764 dgap_do_reset_board(bd);
4767 /* Move to next state */
4768 if (bd->state == FINISHED_RESET)
4769 bd->state = NEED_CONFIG;
4771 if (bd->state == NEED_CONFIG) {
4773 * Match this board to a config the user created for us.
4775 bd->bd_config = dgap_find_config(bd->type, bd->pci_bus,
4776 bd->pci_slot);
4779 * Because the 4 port Xr products share the same PCI ID
4780 * as the 8 port Xr products, if we receive a NULL config
4781 * back, and this is a PAPORT8 board, retry with a
4782 * PAPORT4 attempt as well.
4784 if (bd->type == PAPORT8 && !bd->bd_config)
4785 bd->bd_config = dgap_find_config(PAPORT4, bd->pci_bus,
4786 bd->pci_slot);
4789 * Register the ttys (if any) into the kernel.
4791 if (bd->bd_config)
4792 bd->state = FINISHED_CONFIG;
4793 else
4794 bd->state = CONFIG_NOT_FOUND;
4797 /* Move to next state */
4798 if (bd->state == FINISHED_CONFIG)
4799 bd->state = NEED_DEVICE_CREATION;
4801 /* Move to next state */
4802 if (bd->state == NEED_DEVICE_CREATION) {
4804 * Signal downloader, its got some work to do.
4806 DGAP_LOCK(dgap_dl_lock, lock_flags2);
4807 if (dgap_dl_action != 1) {
4808 dgap_dl_action = 1;
4809 wake_up_interruptible(&dgap_dl_wait);
4811 DGAP_UNLOCK(dgap_dl_lock, lock_flags2);
4814 /* Move to next state */
4815 if (bd->state == FINISHED_DEVICE_CREATION)
4816 bd->state = NEED_BIOS_LOAD;
4818 /* Move to next state */
4819 if (bd->state == NEED_BIOS_LOAD) {
4821 * Signal downloader, its got some work to do.
4823 DGAP_LOCK(dgap_dl_lock, lock_flags2);
4824 if (dgap_dl_action != 1) {
4825 dgap_dl_action = 1;
4826 wake_up_interruptible(&dgap_dl_wait);
4828 DGAP_UNLOCK(dgap_dl_lock, lock_flags2);
4831 /* Wait for BIOS to test board... */
4832 if (bd->state == WAIT_BIOS_LOAD)
4833 dgap_do_wait_for_bios(bd);
4835 /* Move to next state */
4836 if (bd->state == FINISHED_BIOS_LOAD) {
4837 bd->state = NEED_FEP_LOAD;
4840 * Signal downloader, its got some work to do.
4842 DGAP_LOCK(dgap_dl_lock, lock_flags2);
4843 if (dgap_dl_action != 1) {
4844 dgap_dl_action = 1;
4845 wake_up_interruptible(&dgap_dl_wait);
4847 DGAP_UNLOCK(dgap_dl_lock, lock_flags2);
4850 /* Wait for FEP to load on board... */
4851 if (bd->state == WAIT_FEP_LOAD)
4852 dgap_do_wait_for_fep(bd);
4854 /* Move to next state */
4855 if (bd->state == FINISHED_FEP_LOAD) {
4858 * Do tty device initialization.
4860 int rc = dgap_tty_init(bd);
4862 if (rc < 0) {
4863 dgap_tty_uninit(bd);
4864 bd->state = BOARD_FAILED;
4865 bd->dpastatus = BD_NOFEP;
4866 } else {
4867 bd->state = NEED_PROC_CREATION;
4870 * Signal downloader, its got some work to do.
4872 DGAP_LOCK(dgap_dl_lock, lock_flags2);
4873 if (dgap_dl_action != 1) {
4874 dgap_dl_action = 1;
4875 wake_up_interruptible(&dgap_dl_wait);
4877 DGAP_UNLOCK(dgap_dl_lock, lock_flags2);
4881 /* Move to next state */
4882 if (bd->state == FINISHED_PROC_CREATION) {
4884 bd->state = BOARD_READY;
4885 bd->dpastatus = BD_RUNNING;
4888 * If user requested the board to run in interrupt mode,
4889 * go and set it up on the board.
4891 if (bd->intr_used) {
4892 writew(1, (bd->re_map_membase + ENABLE_INTR));
4894 * Tell the board to poll the UARTS as fast as possible.
4896 writew(FEPPOLL_MIN, (bd->re_map_membase + FEPPOLL));
4897 bd->intr_running = 1;
4900 /* Wake up anyone waiting for board state to change to ready */
4901 wake_up_interruptible(&bd->state_wait);
4904 DGAP_UNLOCK(bd->bd_lock, lock_flags);
4907 /*=======================================================================
4909 * dgap_cmdb - Sends a 2 byte command to the FEP.
4911 * ch - Pointer to channel structure.
4912 * cmd - Command to be sent.
4913 * byte1 - Integer containing first byte to be sent.
4914 * byte2 - Integer containing second byte to be sent.
4915 * ncmds - Wait until ncmds or fewer cmds are left
4916 * in the cmd buffer before returning.
4918 *=======================================================================*/
4919 static void dgap_cmdb(struct channel_t *ch, uchar cmd, uchar byte1,
4920 uchar byte2, uint ncmds)
4922 char *vaddr = NULL;
4923 struct cm_t *cm_addr = NULL;
4924 uint count;
4925 uint n;
4926 u16 head;
4927 u16 tail;
4929 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
4930 return;
4933 * Check if board is still alive.
4935 if (ch->ch_bd->state == BOARD_FAILED)
4936 return;
4939 * Make sure the pointers are in range before
4940 * writing to the FEP memory.
4942 vaddr = ch->ch_bd->re_map_membase;
4944 if (!vaddr)
4945 return;
4947 cm_addr = (struct cm_t *) (vaddr + CMDBUF);
4948 head = readw(&(cm_addr->cm_head));
4951 * Forget it if pointers out of range.
4953 if (head >= (CMDMAX - CMDSTART) || (head & 03)) {
4954 ch->ch_bd->state = BOARD_FAILED;
4955 return;
4959 * Put the data in the circular command buffer.
4961 writeb(cmd, (char *) (vaddr + head + CMDSTART + 0));
4962 writeb((uchar) ch->ch_portnum, (char *) (vaddr + head + CMDSTART + 1));
4963 writeb(byte1, (char *) (vaddr + head + CMDSTART + 2));
4964 writeb(byte2, (char *) (vaddr + head + CMDSTART + 3));
4966 head = (head + 4) & (CMDMAX - CMDSTART - 4);
4968 writew(head, &(cm_addr->cm_head));
4971 * Wait if necessary before updating the head
4972 * pointer to limit the number of outstanding
4973 * commands to the FEP. If the time spent waiting
4974 * is outlandish, declare the FEP dead.
4976 for (count = dgap_count ;;) {
4978 head = readw(&(cm_addr->cm_head));
4979 tail = readw(&(cm_addr->cm_tail));
4981 n = (head - tail) & (CMDMAX - CMDSTART - 4);
4983 if (n <= ncmds * sizeof(struct cm_t))
4984 break;
4986 if (--count == 0) {
4987 ch->ch_bd->state = BOARD_FAILED;
4988 return;
4990 udelay(10);
4994 /*=======================================================================
4996 * dgap_cmdw - Sends a 1 word command to the FEP.
4998 * ch - Pointer to channel structure.
4999 * cmd - Command to be sent.
5000 * word - Integer containing word to be sent.
5001 * ncmds - Wait until ncmds or fewer cmds are left
5002 * in the cmd buffer before returning.
5004 *=======================================================================*/
5005 static void dgap_cmdw(struct channel_t *ch, uchar cmd, u16 word, uint ncmds)
5007 char *vaddr = NULL;
5008 struct cm_t *cm_addr = NULL;
5009 uint count;
5010 uint n;
5011 u16 head;
5012 u16 tail;
5014 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
5015 return;
5018 * Check if board is still alive.
5020 if (ch->ch_bd->state == BOARD_FAILED)
5021 return;
5024 * Make sure the pointers are in range before
5025 * writing to the FEP memory.
5027 vaddr = ch->ch_bd->re_map_membase;
5028 if (!vaddr)
5029 return;
5031 cm_addr = (struct cm_t *) (vaddr + CMDBUF);
5032 head = readw(&(cm_addr->cm_head));
5035 * Forget it if pointers out of range.
5037 if (head >= (CMDMAX - CMDSTART) || (head & 03)) {
5038 ch->ch_bd->state = BOARD_FAILED;
5039 return;
5043 * Put the data in the circular command buffer.
5045 writeb(cmd, (char *) (vaddr + head + CMDSTART + 0));
5046 writeb((uchar) ch->ch_portnum, (char *) (vaddr + head + CMDSTART + 1));
5047 writew((u16) word, (char *) (vaddr + head + CMDSTART + 2));
5049 head = (head + 4) & (CMDMAX - CMDSTART - 4);
5051 writew(head, &(cm_addr->cm_head));
5054 * Wait if necessary before updating the head
5055 * pointer to limit the number of outstanding
5056 * commands to the FEP. If the time spent waiting
5057 * is outlandish, declare the FEP dead.
5059 for (count = dgap_count ;;) {
5061 head = readw(&(cm_addr->cm_head));
5062 tail = readw(&(cm_addr->cm_tail));
5064 n = (head - tail) & (CMDMAX - CMDSTART - 4);
5066 if (n <= ncmds * sizeof(struct cm_t))
5067 break;
5069 if (--count == 0) {
5070 ch->ch_bd->state = BOARD_FAILED;
5071 return;
5073 udelay(10);
5077 /*=======================================================================
5079 * dgap_cmdw_ext - Sends a extended word command to the FEP.
5081 * ch - Pointer to channel structure.
5082 * cmd - Command to be sent.
5083 * word - Integer containing word to be sent.
5084 * ncmds - Wait until ncmds or fewer cmds are left
5085 * in the cmd buffer before returning.
5087 *=======================================================================*/
5088 static void dgap_cmdw_ext(struct channel_t *ch, u16 cmd, u16 word, uint ncmds)
5090 char *vaddr = NULL;
5091 struct cm_t *cm_addr = NULL;
5092 uint count;
5093 uint n;
5094 u16 head;
5095 u16 tail;
5097 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
5098 return;
5101 * Check if board is still alive.
5103 if (ch->ch_bd->state == BOARD_FAILED)
5104 return;
5107 * Make sure the pointers are in range before
5108 * writing to the FEP memory.
5110 vaddr = ch->ch_bd->re_map_membase;
5111 if (!vaddr)
5112 return;
5114 cm_addr = (struct cm_t *) (vaddr + CMDBUF);
5115 head = readw(&(cm_addr->cm_head));
5118 * Forget it if pointers out of range.
5120 if (head >= (CMDMAX - CMDSTART) || (head & 03)) {
5121 ch->ch_bd->state = BOARD_FAILED;
5122 return;
5126 * Put the data in the circular command buffer.
5129 /* Write an FF to tell the FEP that we want an extended command */
5130 writeb((uchar) 0xff, (char *) (vaddr + head + CMDSTART + 0));
5132 writeb((uchar) ch->ch_portnum, (uchar *) (vaddr + head + CMDSTART + 1));
5133 writew((u16) cmd, (char *) (vaddr + head + CMDSTART + 2));
5136 * If the second part of the command won't fit,
5137 * put it at the beginning of the circular buffer.
5139 if (((head + 4) >= ((CMDMAX - CMDSTART)) || (head & 03)))
5140 writew((u16) word, (char *) (vaddr + CMDSTART));
5141 else
5142 writew((u16) word, (char *) (vaddr + head + CMDSTART + 4));
5144 head = (head + 8) & (CMDMAX - CMDSTART - 4);
5146 writew(head, &(cm_addr->cm_head));
5149 * Wait if necessary before updating the head
5150 * pointer to limit the number of outstanding
5151 * commands to the FEP. If the time spent waiting
5152 * is outlandish, declare the FEP dead.
5154 for (count = dgap_count ;;) {
5156 head = readw(&(cm_addr->cm_head));
5157 tail = readw(&(cm_addr->cm_tail));
5159 n = (head - tail) & (CMDMAX - CMDSTART - 4);
5161 if (n <= ncmds * sizeof(struct cm_t))
5162 break;
5164 if (--count == 0) {
5165 ch->ch_bd->state = BOARD_FAILED;
5166 return;
5168 udelay(10);
5172 /*=======================================================================
5174 * dgap_wmove - Write data to FEP buffer.
5176 * ch - Pointer to channel structure.
5177 * buf - Poiter to characters to be moved.
5178 * cnt - Number of characters to move.
5180 *=======================================================================*/
5181 static void dgap_wmove(struct channel_t *ch, char *buf, uint cnt)
5183 int n;
5184 char *taddr;
5185 struct bs_t *bs;
5186 u16 head;
5188 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
5189 return;
5192 * Check parameters.
5194 bs = ch->ch_bs;
5195 head = readw(&(bs->tx_head));
5198 * If pointers are out of range, just return.
5200 if ((cnt > ch->ch_tsize) ||
5201 (unsigned)(head - ch->ch_tstart) >= ch->ch_tsize)
5202 return;
5205 * If the write wraps over the top of the circular buffer,
5206 * move the portion up to the wrap point, and reset the
5207 * pointers to the bottom.
5209 n = ch->ch_tstart + ch->ch_tsize - head;
5211 if (cnt >= n) {
5212 cnt -= n;
5213 taddr = ch->ch_taddr + head;
5214 memcpy_toio(taddr, buf, n);
5215 head = ch->ch_tstart;
5216 buf += n;
5220 * Move rest of data.
5222 taddr = ch->ch_taddr + head;
5223 n = cnt;
5224 memcpy_toio(taddr, buf, n);
5225 head += cnt;
5227 writew(head, &(bs->tx_head));
5231 * Retrives the current custom baud rate from FEP memory,
5232 * and returns it back to the user.
5233 * Returns 0 on error.
5235 static uint dgap_get_custom_baud(struct channel_t *ch)
5237 uchar *vaddr;
5238 ulong offset = 0;
5239 uint value = 0;
5241 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
5242 return 0;
5244 if (!ch->ch_bd || ch->ch_bd->magic != DGAP_BOARD_MAGIC)
5245 return 0;
5247 if (!(ch->ch_bd->bd_flags & BD_FEP5PLUS))
5248 return 0;
5250 vaddr = ch->ch_bd->re_map_membase;
5252 if (!vaddr)
5253 return 0;
5256 * Go get from fep mem, what the fep
5257 * believes the custom baud rate is.
5259 offset = ((((*(unsigned short *)(vaddr + ECS_SEG)) << 4) +
5260 (ch->ch_portnum * 0x28) + LINE_SPEED));
5262 value = readw(vaddr + offset);
5263 return value;
5267 * Calls the firmware to reset this channel.
5269 static void dgap_firmware_reset_port(struct channel_t *ch)
5271 dgap_cmdb(ch, CHRESET, 0, 0, 0);
5274 * Now that the channel is reset, we need to make sure
5275 * all the current settings get reapplied to the port
5276 * in the firmware.
5278 * So we will set the driver's cache of firmware
5279 * settings all to 0, and then call param.
5281 ch->ch_fepiflag = 0;
5282 ch->ch_fepcflag = 0;
5283 ch->ch_fepoflag = 0;
5284 ch->ch_fepstartc = 0;
5285 ch->ch_fepstopc = 0;
5286 ch->ch_fepastartc = 0;
5287 ch->ch_fepastopc = 0;
5288 ch->ch_mostat = 0;
5289 ch->ch_hflow = 0;
5292 /*=======================================================================
5294 * dgap_param - Set Digi parameters.
5296 * struct tty_struct * - TTY for port.
5298 *=======================================================================*/
5299 static int dgap_param(struct tty_struct *tty)
5301 struct ktermios *ts;
5302 struct board_t *bd;
5303 struct channel_t *ch;
5304 struct bs_t *bs;
5305 struct un_t *un;
5306 u16 head;
5307 u16 cflag;
5308 u16 iflag;
5309 uchar mval;
5310 uchar hflow;
5312 if (!tty || tty->magic != TTY_MAGIC)
5313 return -ENXIO;
5315 un = (struct un_t *) tty->driver_data;
5316 if (!un || un->magic != DGAP_UNIT_MAGIC)
5317 return -ENXIO;
5319 ch = un->un_ch;
5320 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
5321 return -ENXIO;
5323 bd = ch->ch_bd;
5324 if (!bd || bd->magic != DGAP_BOARD_MAGIC)
5325 return -ENXIO;
5327 bs = ch->ch_bs;
5328 if (!bs)
5329 return -ENXIO;
5331 ts = &tty->termios;
5334 * If baud rate is zero, flush queues, and set mval to drop DTR.
5336 if ((ch->ch_c_cflag & (CBAUD)) == 0) {
5338 /* flush rx */
5339 head = readw(&(ch->ch_bs->rx_head));
5340 writew(head, &(ch->ch_bs->rx_tail));
5342 /* flush tx */
5343 head = readw(&(ch->ch_bs->tx_head));
5344 writew(head, &(ch->ch_bs->tx_tail));
5346 ch->ch_flags |= (CH_BAUD0);
5348 /* Drop RTS and DTR */
5349 ch->ch_mval &= ~(D_RTS(ch)|D_DTR(ch));
5350 mval = D_DTR(ch) | D_RTS(ch);
5351 ch->ch_baud_info = 0;
5353 } else if (ch->ch_custom_speed && (bd->bd_flags & BD_FEP5PLUS)) {
5355 * Tell the fep to do the command
5358 dgap_cmdw_ext(ch, 0xff01, ch->ch_custom_speed, 0);
5361 * Now go get from fep mem, what the fep
5362 * believes the custom baud rate is.
5364 ch->ch_custom_speed = dgap_get_custom_baud(ch);
5365 ch->ch_baud_info = ch->ch_custom_speed;
5367 /* Handle transition from B0 */
5368 if (ch->ch_flags & CH_BAUD0) {
5369 ch->ch_flags &= ~(CH_BAUD0);
5370 ch->ch_mval |= (D_RTS(ch)|D_DTR(ch));
5372 mval = D_DTR(ch) | D_RTS(ch);
5374 } else {
5376 * Set baud rate, character size, and parity.
5380 int iindex = 0;
5381 int jindex = 0;
5382 int baud = 0;
5384 ulong bauds[4][16] = {
5385 { /* slowbaud */
5386 0, 50, 75, 110,
5387 134, 150, 200, 300,
5388 600, 1200, 1800, 2400,
5389 4800, 9600, 19200, 38400 },
5390 { /* slowbaud & CBAUDEX */
5391 0, 57600, 115200, 230400,
5392 460800, 150, 200, 921600,
5393 600, 1200, 1800, 2400,
5394 4800, 9600, 19200, 38400 },
5395 { /* fastbaud */
5396 0, 57600, 76800, 115200,
5397 14400, 57600, 230400, 76800,
5398 115200, 230400, 28800, 460800,
5399 921600, 9600, 19200, 38400 },
5400 { /* fastbaud & CBAUDEX */
5401 0, 57600, 115200, 230400,
5402 460800, 150, 200, 921600,
5403 600, 1200, 1800, 2400,
5404 4800, 9600, 19200, 38400 }
5408 * Only use the TXPrint baud rate if the
5409 * terminal unit is NOT open
5411 if (!(ch->ch_tun.un_flags & UN_ISOPEN) &&
5412 (un->un_type == DGAP_PRINT))
5413 baud = C_BAUD(ch->ch_pun.un_tty) & 0xff;
5414 else
5415 baud = C_BAUD(ch->ch_tun.un_tty) & 0xff;
5417 if (ch->ch_c_cflag & CBAUDEX)
5418 iindex = 1;
5420 if (ch->ch_digi.digi_flags & DIGI_FAST)
5421 iindex += 2;
5423 jindex = baud;
5425 if ((iindex >= 0) && (iindex < 4) &&
5426 (jindex >= 0) && (jindex < 16))
5427 baud = bauds[iindex][jindex];
5428 else
5429 baud = 0;
5431 if (baud == 0)
5432 baud = 9600;
5434 ch->ch_baud_info = baud;
5437 * CBAUD has bit position 0x1000 set these days to
5438 * indicate Linux baud rate remap.
5439 * We use a different bit assignment for high speed.
5440 * Clear this bit out while grabbing the parts of
5441 * "cflag" we want.
5443 cflag = ch->ch_c_cflag & ((CBAUD ^ CBAUDEX) | PARODD | PARENB |
5444 CSTOPB | CSIZE);
5447 * HUPCL bit is used by FEP to indicate fast baud
5448 * table is to be used.
5450 if ((ch->ch_digi.digi_flags & DIGI_FAST) ||
5451 (ch->ch_c_cflag & CBAUDEX))
5452 cflag |= HUPCL;
5454 if ((ch->ch_c_cflag & CBAUDEX) &&
5455 !(ch->ch_digi.digi_flags & DIGI_FAST)) {
5457 * The below code is trying to guarantee that only
5458 * baud rates 115200, 230400, 460800, 921600 are
5459 * remapped. We use exclusive or because the various
5460 * baud rates share common bit positions and therefore
5461 * can't be tested for easily.
5463 tcflag_t tcflag = (ch->ch_c_cflag & CBAUD) | CBAUDEX;
5464 int baudpart = 0;
5467 * Map high speed requests to index
5468 * into FEP's baud table
5470 switch (tcflag) {
5471 case B57600:
5472 baudpart = 1;
5473 break;
5474 #ifdef B76800
5475 case B76800:
5476 baudpart = 2;
5477 break;
5478 #endif
5479 case B115200:
5480 baudpart = 3;
5481 break;
5482 case B230400:
5483 baudpart = 9;
5484 break;
5485 case B460800:
5486 baudpart = 11;
5487 break;
5488 #ifdef B921600
5489 case B921600:
5490 baudpart = 12;
5491 break;
5492 #endif
5493 default:
5494 baudpart = 0;
5497 if (baudpart)
5498 cflag = (cflag & ~(CBAUD | CBAUDEX)) | baudpart;
5501 cflag &= 0xffff;
5503 if (cflag != ch->ch_fepcflag) {
5504 ch->ch_fepcflag = (u16) (cflag & 0xffff);
5507 * Okay to have channel and board
5508 * locks held calling this
5510 dgap_cmdw(ch, SCFLAG, (u16) cflag, 0);
5513 /* Handle transition from B0 */
5514 if (ch->ch_flags & CH_BAUD0) {
5515 ch->ch_flags &= ~(CH_BAUD0);
5516 ch->ch_mval |= (D_RTS(ch)|D_DTR(ch));
5518 mval = D_DTR(ch) | D_RTS(ch);
5522 * Get input flags.
5524 iflag = ch->ch_c_iflag & (IGNBRK | BRKINT | IGNPAR | PARMRK |
5525 INPCK | ISTRIP | IXON | IXANY | IXOFF);
5527 if ((ch->ch_startc == _POSIX_VDISABLE) ||
5528 (ch->ch_stopc == _POSIX_VDISABLE)) {
5529 iflag &= ~(IXON | IXOFF);
5530 ch->ch_c_iflag &= ~(IXON | IXOFF);
5534 * Only the IBM Xr card can switch between
5535 * 232 and 422 modes on the fly
5537 if (bd->device == PCI_DEV_XR_IBM_DID) {
5538 if (ch->ch_digi.digi_flags & DIGI_422)
5539 dgap_cmdb(ch, SCOMMODE, MODE_422, 0, 0);
5540 else
5541 dgap_cmdb(ch, SCOMMODE, MODE_232, 0, 0);
5544 if (ch->ch_digi.digi_flags & DIGI_ALTPIN)
5545 iflag |= IALTPIN;
5547 if (iflag != ch->ch_fepiflag) {
5548 ch->ch_fepiflag = iflag;
5550 /* Okay to have channel and board locks held calling this */
5551 dgap_cmdw(ch, SIFLAG, (u16) ch->ch_fepiflag, 0);
5555 * Select hardware handshaking.
5557 hflow = 0;
5559 if (ch->ch_c_cflag & CRTSCTS)
5560 hflow |= (D_RTS(ch) | D_CTS(ch));
5561 if (ch->ch_digi.digi_flags & RTSPACE)
5562 hflow |= D_RTS(ch);
5563 if (ch->ch_digi.digi_flags & DTRPACE)
5564 hflow |= D_DTR(ch);
5565 if (ch->ch_digi.digi_flags & CTSPACE)
5566 hflow |= D_CTS(ch);
5567 if (ch->ch_digi.digi_flags & DSRPACE)
5568 hflow |= D_DSR(ch);
5569 if (ch->ch_digi.digi_flags & DCDPACE)
5570 hflow |= D_CD(ch);
5572 if (hflow != ch->ch_hflow) {
5573 ch->ch_hflow = hflow;
5575 /* Okay to have channel and board locks held calling this */
5576 dgap_cmdb(ch, SHFLOW, (uchar) hflow, 0xff, 0);
5581 * Set RTS and/or DTR Toggle if needed,
5582 * but only if product is FEP5+ based.
5584 if (bd->bd_flags & BD_FEP5PLUS) {
5585 u16 hflow2 = 0;
5586 if (ch->ch_digi.digi_flags & DIGI_RTS_TOGGLE)
5587 hflow2 |= (D_RTS(ch));
5588 if (ch->ch_digi.digi_flags & DIGI_DTR_TOGGLE)
5589 hflow2 |= (D_DTR(ch));
5591 dgap_cmdw_ext(ch, 0xff03, hflow2, 0);
5595 * Set modem control lines.
5598 mval ^= ch->ch_mforce & (mval ^ ch->ch_mval);
5600 if (ch->ch_mostat ^ mval) {
5601 ch->ch_mostat = mval;
5603 /* Okay to have channel and board locks held calling this */
5604 dgap_cmdb(ch, SMODEM, (uchar) mval, D_RTS(ch)|D_DTR(ch), 0);
5608 * Read modem signals, and then call carrier function.
5610 ch->ch_mistat = readb(&(bs->m_stat));
5611 dgap_carrier(ch);
5614 * Set the start and stop characters.
5616 if (ch->ch_startc != ch->ch_fepstartc ||
5617 ch->ch_stopc != ch->ch_fepstopc) {
5618 ch->ch_fepstartc = ch->ch_startc;
5619 ch->ch_fepstopc = ch->ch_stopc;
5621 /* Okay to have channel and board locks held calling this */
5622 dgap_cmdb(ch, SFLOWC, ch->ch_fepstartc, ch->ch_fepstopc, 0);
5626 * Set the Auxiliary start and stop characters.
5628 if (ch->ch_astartc != ch->ch_fepastartc ||
5629 ch->ch_astopc != ch->ch_fepastopc) {
5630 ch->ch_fepastartc = ch->ch_astartc;
5631 ch->ch_fepastopc = ch->ch_astopc;
5633 /* Okay to have channel and board locks held calling this */
5634 dgap_cmdb(ch, SAFLOWC, ch->ch_fepastartc, ch->ch_fepastopc, 0);
5637 return 0;
5641 * dgap_parity_scan()
5643 * Convert the FEP5 way of reporting parity errors and breaks into
5644 * the Linux line discipline way.
5646 static void dgap_parity_scan(struct channel_t *ch, unsigned char *cbuf,
5647 unsigned char *fbuf, int *len)
5649 int l = *len;
5650 int count = 0;
5651 unsigned char *in, *cout, *fout;
5652 unsigned char c;
5654 in = cbuf;
5655 cout = cbuf;
5656 fout = fbuf;
5658 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
5659 return;
5661 while (l--) {
5662 c = *in++;
5663 switch (ch->pscan_state) {
5664 default:
5665 /* reset to sanity and fall through */
5666 ch->pscan_state = 0;
5668 case 0:
5669 /* No FF seen yet */
5670 if (c == (unsigned char) '\377')
5671 /* delete this character from stream */
5672 ch->pscan_state = 1;
5673 else {
5674 *cout++ = c;
5675 *fout++ = TTY_NORMAL;
5676 count += 1;
5678 break;
5680 case 1:
5681 /* first FF seen */
5682 if (c == (unsigned char) '\377') {
5683 /* doubled ff, transform to single ff */
5684 *cout++ = c;
5685 *fout++ = TTY_NORMAL;
5686 count += 1;
5687 ch->pscan_state = 0;
5688 } else {
5689 /* save value examination in next state */
5690 ch->pscan_savechar = c;
5691 ch->pscan_state = 2;
5693 break;
5695 case 2:
5696 /* third character of ff sequence */
5698 *cout++ = c;
5700 if (ch->pscan_savechar == 0x0) {
5702 if (c == 0x0) {
5703 ch->ch_err_break++;
5704 *fout++ = TTY_BREAK;
5705 } else {
5706 ch->ch_err_parity++;
5707 *fout++ = TTY_PARITY;
5711 count += 1;
5712 ch->pscan_state = 0;
5715 *len = count;
5718 /*=======================================================================
5720 * dgap_event - FEP to host event processing routine.
5722 * bd - Board of current event.
5724 *=======================================================================*/
5725 static int dgap_event(struct board_t *bd)
5727 struct channel_t *ch;
5728 ulong lock_flags;
5729 ulong lock_flags2;
5730 struct bs_t *bs;
5731 uchar *event;
5732 uchar *vaddr = NULL;
5733 struct ev_t *eaddr = NULL;
5734 uint head;
5735 uint tail;
5736 int port;
5737 int reason;
5738 int modem;
5739 int b1;
5741 if (!bd || bd->magic != DGAP_BOARD_MAGIC)
5742 return -ENXIO;
5744 DGAP_LOCK(bd->bd_lock, lock_flags);
5746 vaddr = bd->re_map_membase;
5748 if (!vaddr) {
5749 DGAP_UNLOCK(bd->bd_lock, lock_flags);
5750 return -ENXIO;
5753 eaddr = (struct ev_t *) (vaddr + EVBUF);
5755 /* Get our head and tail */
5756 head = readw(&(eaddr->ev_head));
5757 tail = readw(&(eaddr->ev_tail));
5760 * Forget it if pointers out of range.
5763 if (head >= EVMAX - EVSTART || tail >= EVMAX - EVSTART ||
5764 (head | tail) & 03) {
5765 /* Let go of board lock */
5766 DGAP_UNLOCK(bd->bd_lock, lock_flags);
5767 return -ENXIO;
5771 * Loop to process all the events in the buffer.
5773 while (tail != head) {
5776 * Get interrupt information.
5779 event = bd->re_map_membase + tail + EVSTART;
5781 port = event[0];
5782 reason = event[1];
5783 modem = event[2];
5784 b1 = event[3];
5787 * Make sure the interrupt is valid.
5789 if (port >= bd->nasync)
5790 goto next;
5792 if (!(reason & (IFMODEM | IFBREAK | IFTLW | IFTEM | IFDATA)))
5793 goto next;
5795 ch = bd->channels[port];
5797 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
5798 goto next;
5801 * If we have made it here, the event was valid.
5802 * Lock down the channel.
5804 DGAP_LOCK(ch->ch_lock, lock_flags2);
5806 bs = ch->ch_bs;
5808 if (!bs) {
5809 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
5810 goto next;
5814 * Process received data.
5816 if (reason & IFDATA) {
5819 * ALL LOCKS *MUST* BE DROPPED BEFORE CALLING INPUT!
5820 * input could send some data to ld, which in turn
5821 * could do a callback to one of our other functions.
5823 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
5824 DGAP_UNLOCK(bd->bd_lock, lock_flags);
5826 dgap_input(ch);
5828 DGAP_LOCK(bd->bd_lock, lock_flags);
5829 DGAP_LOCK(ch->ch_lock, lock_flags2);
5831 if (ch->ch_flags & CH_RACTIVE)
5832 ch->ch_flags |= CH_RENABLE;
5833 else
5834 writeb(1, &(bs->idata));
5836 if (ch->ch_flags & CH_RWAIT) {
5837 ch->ch_flags &= ~CH_RWAIT;
5839 wake_up_interruptible(&ch->ch_tun.un_flags_wait);
5844 * Process Modem change signals.
5846 if (reason & IFMODEM) {
5847 ch->ch_mistat = modem;
5848 dgap_carrier(ch);
5852 * Process break.
5854 if (reason & IFBREAK) {
5856 if (ch->ch_tun.un_tty) {
5857 /* A break has been indicated */
5858 ch->ch_err_break++;
5859 tty_buffer_request_room(ch->ch_tun.un_tty->port, 1);
5860 tty_insert_flip_char(ch->ch_tun.un_tty->port, 0, TTY_BREAK);
5861 tty_flip_buffer_push(ch->ch_tun.un_tty->port);
5866 * Process Transmit low.
5868 if (reason & IFTLW) {
5870 if (ch->ch_tun.un_flags & UN_LOW) {
5871 ch->ch_tun.un_flags &= ~UN_LOW;
5873 if (ch->ch_tun.un_flags & UN_ISOPEN) {
5874 if ((ch->ch_tun.un_tty->flags &
5875 (1 << TTY_DO_WRITE_WAKEUP)) &&
5876 ch->ch_tun.un_tty->ldisc->ops->write_wakeup) {
5877 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
5878 DGAP_UNLOCK(bd->bd_lock, lock_flags);
5879 (ch->ch_tun.un_tty->ldisc->ops->write_wakeup)(ch->ch_tun.un_tty);
5880 DGAP_LOCK(bd->bd_lock, lock_flags);
5881 DGAP_LOCK(ch->ch_lock, lock_flags2);
5883 wake_up_interruptible(&ch->ch_tun.un_tty->write_wait);
5884 wake_up_interruptible(&ch->ch_tun.un_flags_wait);
5888 if (ch->ch_pun.un_flags & UN_LOW) {
5889 ch->ch_pun.un_flags &= ~UN_LOW;
5890 if (ch->ch_pun.un_flags & UN_ISOPEN) {
5891 if ((ch->ch_pun.un_tty->flags &
5892 (1 << TTY_DO_WRITE_WAKEUP)) &&
5893 ch->ch_pun.un_tty->ldisc->ops->write_wakeup) {
5894 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
5895 DGAP_UNLOCK(bd->bd_lock, lock_flags);
5896 (ch->ch_pun.un_tty->ldisc->ops->write_wakeup)(ch->ch_pun.un_tty);
5897 DGAP_LOCK(bd->bd_lock, lock_flags);
5898 DGAP_LOCK(ch->ch_lock, lock_flags2);
5900 wake_up_interruptible(&ch->ch_pun.un_tty->write_wait);
5901 wake_up_interruptible(&ch->ch_pun.un_flags_wait);
5905 if (ch->ch_flags & CH_WLOW) {
5906 ch->ch_flags &= ~CH_WLOW;
5907 wake_up_interruptible(&ch->ch_flags_wait);
5912 * Process Transmit empty.
5914 if (reason & IFTEM) {
5915 if (ch->ch_tun.un_flags & UN_EMPTY) {
5916 ch->ch_tun.un_flags &= ~UN_EMPTY;
5917 if (ch->ch_tun.un_flags & UN_ISOPEN) {
5918 if ((ch->ch_tun.un_tty->flags &
5919 (1 << TTY_DO_WRITE_WAKEUP)) &&
5920 ch->ch_tun.un_tty->ldisc->ops->write_wakeup) {
5921 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
5922 DGAP_UNLOCK(bd->bd_lock, lock_flags);
5924 (ch->ch_tun.un_tty->ldisc->ops->write_wakeup)(ch->ch_tun.un_tty);
5925 DGAP_LOCK(bd->bd_lock, lock_flags);
5926 DGAP_LOCK(ch->ch_lock, lock_flags2);
5928 wake_up_interruptible(&ch->ch_tun.un_tty->write_wait);
5929 wake_up_interruptible(&ch->ch_tun.un_flags_wait);
5933 if (ch->ch_pun.un_flags & UN_EMPTY) {
5934 ch->ch_pun.un_flags &= ~UN_EMPTY;
5935 if (ch->ch_pun.un_flags & UN_ISOPEN) {
5936 if ((ch->ch_pun.un_tty->flags &
5937 (1 << TTY_DO_WRITE_WAKEUP)) &&
5938 ch->ch_pun.un_tty->ldisc->ops->write_wakeup) {
5939 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
5940 DGAP_UNLOCK(bd->bd_lock, lock_flags);
5941 (ch->ch_pun.un_tty->ldisc->ops->write_wakeup)(ch->ch_pun.un_tty);
5942 DGAP_LOCK(bd->bd_lock, lock_flags);
5943 DGAP_LOCK(ch->ch_lock, lock_flags2);
5945 wake_up_interruptible(&ch->ch_pun.un_tty->write_wait);
5946 wake_up_interruptible(&ch->ch_pun.un_flags_wait);
5951 if (ch->ch_flags & CH_WEMPTY) {
5952 ch->ch_flags &= ~CH_WEMPTY;
5953 wake_up_interruptible(&ch->ch_flags_wait);
5957 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
5959 next:
5960 tail = (tail + 4) & (EVMAX - EVSTART - 4);
5963 writew(tail, &(eaddr->ev_tail));
5964 DGAP_UNLOCK(bd->bd_lock, lock_flags);
5966 return 0;
5969 static ssize_t dgap_driver_version_show(struct device_driver *ddp, char *buf)
5971 return snprintf(buf, PAGE_SIZE, "%s\n", DG_PART);
5973 static DRIVER_ATTR(version, S_IRUSR, dgap_driver_version_show, NULL);
5976 static ssize_t dgap_driver_boards_show(struct device_driver *ddp, char *buf)
5978 return snprintf(buf, PAGE_SIZE, "%d\n", dgap_NumBoards);
5980 static DRIVER_ATTR(boards, S_IRUSR, dgap_driver_boards_show, NULL);
5983 static ssize_t dgap_driver_maxboards_show(struct device_driver *ddp, char *buf)
5985 return snprintf(buf, PAGE_SIZE, "%d\n", MAXBOARDS);
5987 static DRIVER_ATTR(maxboards, S_IRUSR, dgap_driver_maxboards_show, NULL);
5990 static ssize_t dgap_driver_pollcounter_show(struct device_driver *ddp, char *buf)
5992 return snprintf(buf, PAGE_SIZE, "%ld\n", dgap_poll_counter);
5994 static DRIVER_ATTR(pollcounter, S_IRUSR, dgap_driver_pollcounter_show, NULL);
5997 static ssize_t dgap_driver_state_show(struct device_driver *ddp, char *buf)
5999 return snprintf(buf, PAGE_SIZE, "%s\n", dgap_driver_state_text[dgap_driver_state]);
6001 static DRIVER_ATTR(state, S_IRUSR, dgap_driver_state_show, NULL);
6003 static ssize_t dgap_driver_pollrate_show(struct device_driver *ddp, char *buf)
6005 return snprintf(buf, PAGE_SIZE, "%dms\n", dgap_poll_tick);
6008 static ssize_t dgap_driver_pollrate_store(struct device_driver *ddp, const char *buf, size_t count)
6010 if (sscanf(buf, "%d\n", &dgap_poll_tick) != 1)
6011 return -EINVAL;
6012 return count;
6014 static DRIVER_ATTR(pollrate, (S_IRUSR | S_IWUSR), dgap_driver_pollrate_show, dgap_driver_pollrate_store);
6016 static int dgap_create_driver_sysfiles(struct pci_driver *dgap_driver)
6018 int rc = 0;
6019 struct device_driver *driverfs = &dgap_driver->driver;
6021 rc |= driver_create_file(driverfs, &driver_attr_version);
6022 rc |= driver_create_file(driverfs, &driver_attr_boards);
6023 rc |= driver_create_file(driverfs, &driver_attr_maxboards);
6024 rc |= driver_create_file(driverfs, &driver_attr_pollrate);
6025 rc |= driver_create_file(driverfs, &driver_attr_pollcounter);
6026 rc |= driver_create_file(driverfs, &driver_attr_state);
6028 return rc;
6031 static void dgap_remove_driver_sysfiles(struct pci_driver *dgap_driver)
6033 struct device_driver *driverfs = &dgap_driver->driver;
6034 driver_remove_file(driverfs, &driver_attr_version);
6035 driver_remove_file(driverfs, &driver_attr_boards);
6036 driver_remove_file(driverfs, &driver_attr_maxboards);
6037 driver_remove_file(driverfs, &driver_attr_pollrate);
6038 driver_remove_file(driverfs, &driver_attr_pollcounter);
6039 driver_remove_file(driverfs, &driver_attr_state);
6042 static struct board_t *dgap_verify_board(struct device *p)
6044 struct board_t *bd;
6046 if (!p)
6047 return NULL;
6049 bd = dev_get_drvdata(p);
6050 if (!bd || bd->magic != DGAP_BOARD_MAGIC || bd->state != BOARD_READY)
6051 return NULL;
6053 return bd;
6056 static ssize_t dgap_ports_state_show(struct device *p, struct device_attribute *attr, char *buf)
6058 struct board_t *bd;
6059 int count = 0;
6060 int i = 0;
6062 bd = dgap_verify_board(p);
6063 if (!bd)
6064 return 0;
6066 for (i = 0; i < bd->nasync; i++) {
6067 count += snprintf(buf + count, PAGE_SIZE - count,
6068 "%d %s\n", bd->channels[i]->ch_portnum,
6069 bd->channels[i]->ch_open_count ? "Open" : "Closed");
6071 return count;
6073 static DEVICE_ATTR(ports_state, S_IRUSR, dgap_ports_state_show, NULL);
6075 static ssize_t dgap_ports_baud_show(struct device *p, struct device_attribute *attr, char *buf)
6077 struct board_t *bd;
6078 int count = 0;
6079 int i = 0;
6081 bd = dgap_verify_board(p);
6082 if (!bd)
6083 return 0;
6085 for (i = 0; i < bd->nasync; i++) {
6086 count += snprintf(buf + count, PAGE_SIZE - count,
6087 "%d %d\n", bd->channels[i]->ch_portnum, bd->channels[i]->ch_baud_info);
6089 return count;
6091 static DEVICE_ATTR(ports_baud, S_IRUSR, dgap_ports_baud_show, NULL);
6093 static ssize_t dgap_ports_msignals_show(struct device *p, struct device_attribute *attr, char *buf)
6095 struct board_t *bd;
6096 int count = 0;
6097 int i = 0;
6099 bd = dgap_verify_board(p);
6100 if (!bd)
6101 return 0;
6103 for (i = 0; i < bd->nasync; i++) {
6104 if (bd->channels[i]->ch_open_count)
6105 count += snprintf(buf + count, PAGE_SIZE - count,
6106 "%d %s %s %s %s %s %s\n", bd->channels[i]->ch_portnum,
6107 (bd->channels[i]->ch_mostat & UART_MCR_RTS) ? "RTS" : "",
6108 (bd->channels[i]->ch_mistat & UART_MSR_CTS) ? "CTS" : "",
6109 (bd->channels[i]->ch_mostat & UART_MCR_DTR) ? "DTR" : "",
6110 (bd->channels[i]->ch_mistat & UART_MSR_DSR) ? "DSR" : "",
6111 (bd->channels[i]->ch_mistat & UART_MSR_DCD) ? "DCD" : "",
6112 (bd->channels[i]->ch_mistat & UART_MSR_RI) ? "RI" : "");
6113 else
6114 count += snprintf(buf + count, PAGE_SIZE - count,
6115 "%d\n", bd->channels[i]->ch_portnum);
6117 return count;
6119 static DEVICE_ATTR(ports_msignals, S_IRUSR, dgap_ports_msignals_show, NULL);
6121 static ssize_t dgap_ports_iflag_show(struct device *p, struct device_attribute *attr, char *buf)
6123 struct board_t *bd;
6124 int count = 0;
6125 int i = 0;
6127 bd = dgap_verify_board(p);
6128 if (!bd)
6129 return 0;
6131 for (i = 0; i < bd->nasync; i++)
6132 count += snprintf(buf + count, PAGE_SIZE - count, "%d %x\n",
6133 bd->channels[i]->ch_portnum, bd->channels[i]->ch_c_iflag);
6134 return count;
6136 static DEVICE_ATTR(ports_iflag, S_IRUSR, dgap_ports_iflag_show, NULL);
6138 static ssize_t dgap_ports_cflag_show(struct device *p, struct device_attribute *attr, char *buf)
6140 struct board_t *bd;
6141 int count = 0;
6142 int i = 0;
6144 bd = dgap_verify_board(p);
6145 if (!bd)
6146 return 0;
6148 for (i = 0; i < bd->nasync; i++)
6149 count += snprintf(buf + count, PAGE_SIZE - count, "%d %x\n",
6150 bd->channels[i]->ch_portnum, bd->channels[i]->ch_c_cflag);
6151 return count;
6153 static DEVICE_ATTR(ports_cflag, S_IRUSR, dgap_ports_cflag_show, NULL);
6155 static ssize_t dgap_ports_oflag_show(struct device *p, struct device_attribute *attr, char *buf)
6157 struct board_t *bd;
6158 int count = 0;
6159 int i = 0;
6161 bd = dgap_verify_board(p);
6162 if (!bd)
6163 return 0;
6165 for (i = 0; i < bd->nasync; i++)
6166 count += snprintf(buf + count, PAGE_SIZE - count, "%d %x\n",
6167 bd->channels[i]->ch_portnum, bd->channels[i]->ch_c_oflag);
6168 return count;
6170 static DEVICE_ATTR(ports_oflag, S_IRUSR, dgap_ports_oflag_show, NULL);
6172 static ssize_t dgap_ports_lflag_show(struct device *p, struct device_attribute *attr, char *buf)
6174 struct board_t *bd;
6175 int count = 0;
6176 int i = 0;
6178 bd = dgap_verify_board(p);
6179 if (!bd)
6180 return 0;
6182 for (i = 0; i < bd->nasync; i++)
6183 count += snprintf(buf + count, PAGE_SIZE - count, "%d %x\n",
6184 bd->channels[i]->ch_portnum, bd->channels[i]->ch_c_lflag);
6185 return count;
6187 static DEVICE_ATTR(ports_lflag, S_IRUSR, dgap_ports_lflag_show, NULL);
6189 static ssize_t dgap_ports_digi_flag_show(struct device *p, struct device_attribute *attr, char *buf)
6191 struct board_t *bd;
6192 int count = 0;
6193 int i = 0;
6195 bd = dgap_verify_board(p);
6196 if (!bd)
6197 return 0;
6199 for (i = 0; i < bd->nasync; i++)
6200 count += snprintf(buf + count, PAGE_SIZE - count, "%d %x\n",
6201 bd->channels[i]->ch_portnum, bd->channels[i]->ch_digi.digi_flags);
6202 return count;
6204 static DEVICE_ATTR(ports_digi_flag, S_IRUSR, dgap_ports_digi_flag_show, NULL);
6206 static ssize_t dgap_ports_rxcount_show(struct device *p, struct device_attribute *attr, char *buf)
6208 struct board_t *bd;
6209 int count = 0;
6210 int i = 0;
6212 bd = dgap_verify_board(p);
6213 if (!bd)
6214 return 0;
6216 for (i = 0; i < bd->nasync; i++)
6217 count += snprintf(buf + count, PAGE_SIZE - count, "%d %ld\n",
6218 bd->channels[i]->ch_portnum, bd->channels[i]->ch_rxcount);
6219 return count;
6221 static DEVICE_ATTR(ports_rxcount, S_IRUSR, dgap_ports_rxcount_show, NULL);
6223 static ssize_t dgap_ports_txcount_show(struct device *p, struct device_attribute *attr, char *buf)
6225 struct board_t *bd;
6226 int count = 0;
6227 int i = 0;
6229 bd = dgap_verify_board(p);
6230 if (!bd)
6231 return 0;
6233 for (i = 0; i < bd->nasync; i++)
6234 count += snprintf(buf + count, PAGE_SIZE - count, "%d %ld\n",
6235 bd->channels[i]->ch_portnum, bd->channels[i]->ch_txcount);
6236 return count;
6238 static DEVICE_ATTR(ports_txcount, S_IRUSR, dgap_ports_txcount_show, NULL);
6240 /* this function creates the sys files that will export each signal status
6241 * to sysfs each value will be put in a separate filename
6243 static void dgap_create_ports_sysfiles(struct board_t *bd)
6245 dev_set_drvdata(&bd->pdev->dev, bd);
6246 device_create_file(&(bd->pdev->dev), &dev_attr_ports_state);
6247 device_create_file(&(bd->pdev->dev), &dev_attr_ports_baud);
6248 device_create_file(&(bd->pdev->dev), &dev_attr_ports_msignals);
6249 device_create_file(&(bd->pdev->dev), &dev_attr_ports_iflag);
6250 device_create_file(&(bd->pdev->dev), &dev_attr_ports_cflag);
6251 device_create_file(&(bd->pdev->dev), &dev_attr_ports_oflag);
6252 device_create_file(&(bd->pdev->dev), &dev_attr_ports_lflag);
6253 device_create_file(&(bd->pdev->dev), &dev_attr_ports_digi_flag);
6254 device_create_file(&(bd->pdev->dev), &dev_attr_ports_rxcount);
6255 device_create_file(&(bd->pdev->dev), &dev_attr_ports_txcount);
6258 /* removes all the sys files created for that port */
6259 static void dgap_remove_ports_sysfiles(struct board_t *bd)
6261 device_remove_file(&(bd->pdev->dev), &dev_attr_ports_state);
6262 device_remove_file(&(bd->pdev->dev), &dev_attr_ports_baud);
6263 device_remove_file(&(bd->pdev->dev), &dev_attr_ports_msignals);
6264 device_remove_file(&(bd->pdev->dev), &dev_attr_ports_iflag);
6265 device_remove_file(&(bd->pdev->dev), &dev_attr_ports_cflag);
6266 device_remove_file(&(bd->pdev->dev), &dev_attr_ports_oflag);
6267 device_remove_file(&(bd->pdev->dev), &dev_attr_ports_lflag);
6268 device_remove_file(&(bd->pdev->dev), &dev_attr_ports_digi_flag);
6269 device_remove_file(&(bd->pdev->dev), &dev_attr_ports_rxcount);
6270 device_remove_file(&(bd->pdev->dev), &dev_attr_ports_txcount);
6273 static ssize_t dgap_tty_state_show(struct device *d, struct device_attribute *attr, char *buf)
6275 struct board_t *bd;
6276 struct channel_t *ch;
6277 struct un_t *un;
6279 if (!d)
6280 return 0;
6281 un = dev_get_drvdata(d);
6282 if (!un || un->magic != DGAP_UNIT_MAGIC)
6283 return 0;
6284 ch = un->un_ch;
6285 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
6286 return 0;
6287 bd = ch->ch_bd;
6288 if (!bd || bd->magic != DGAP_BOARD_MAGIC)
6289 return 0;
6290 if (bd->state != BOARD_READY)
6291 return 0;
6293 return snprintf(buf, PAGE_SIZE, "%s", un->un_open_count ? "Open" : "Closed");
6295 static DEVICE_ATTR(state, S_IRUSR, dgap_tty_state_show, NULL);
6297 static ssize_t dgap_tty_baud_show(struct device *d, struct device_attribute *attr, char *buf)
6299 struct board_t *bd;
6300 struct channel_t *ch;
6301 struct un_t *un;
6303 if (!d)
6304 return 0;
6305 un = dev_get_drvdata(d);
6306 if (!un || un->magic != DGAP_UNIT_MAGIC)
6307 return 0;
6308 ch = un->un_ch;
6309 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
6310 return 0;
6311 bd = ch->ch_bd;
6312 if (!bd || bd->magic != DGAP_BOARD_MAGIC)
6313 return 0;
6314 if (bd->state != BOARD_READY)
6315 return 0;
6317 return snprintf(buf, PAGE_SIZE, "%d\n", ch->ch_baud_info);
6319 static DEVICE_ATTR(baud, S_IRUSR, dgap_tty_baud_show, NULL);
6321 static ssize_t dgap_tty_msignals_show(struct device *d, struct device_attribute *attr, char *buf)
6323 struct board_t *bd;
6324 struct channel_t *ch;
6325 struct un_t *un;
6327 if (!d)
6328 return 0;
6329 un = dev_get_drvdata(d);
6330 if (!un || un->magic != DGAP_UNIT_MAGIC)
6331 return 0;
6332 ch = un->un_ch;
6333 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
6334 return 0;
6335 bd = ch->ch_bd;
6336 if (!bd || bd->magic != DGAP_BOARD_MAGIC)
6337 return 0;
6338 if (bd->state != BOARD_READY)
6339 return 0;
6341 if (ch->ch_open_count) {
6342 return snprintf(buf, PAGE_SIZE, "%s %s %s %s %s %s\n",
6343 (ch->ch_mostat & UART_MCR_RTS) ? "RTS" : "",
6344 (ch->ch_mistat & UART_MSR_CTS) ? "CTS" : "",
6345 (ch->ch_mostat & UART_MCR_DTR) ? "DTR" : "",
6346 (ch->ch_mistat & UART_MSR_DSR) ? "DSR" : "",
6347 (ch->ch_mistat & UART_MSR_DCD) ? "DCD" : "",
6348 (ch->ch_mistat & UART_MSR_RI) ? "RI" : "");
6350 return 0;
6352 static DEVICE_ATTR(msignals, S_IRUSR, dgap_tty_msignals_show, NULL);
6354 static ssize_t dgap_tty_iflag_show(struct device *d, struct device_attribute *attr, char *buf)
6356 struct board_t *bd;
6357 struct channel_t *ch;
6358 struct un_t *un;
6360 if (!d)
6361 return 0;
6362 un = dev_get_drvdata(d);
6363 if (!un || un->magic != DGAP_UNIT_MAGIC)
6364 return 0;
6365 ch = un->un_ch;
6366 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
6367 return 0;
6368 bd = ch->ch_bd;
6369 if (!bd || bd->magic != DGAP_BOARD_MAGIC)
6370 return 0;
6371 if (bd->state != BOARD_READY)
6372 return 0;
6374 return snprintf(buf, PAGE_SIZE, "%x\n", ch->ch_c_iflag);
6376 static DEVICE_ATTR(iflag, S_IRUSR, dgap_tty_iflag_show, NULL);
6378 static ssize_t dgap_tty_cflag_show(struct device *d, struct device_attribute *attr, char *buf)
6380 struct board_t *bd;
6381 struct channel_t *ch;
6382 struct un_t *un;
6384 if (!d)
6385 return 0;
6386 un = dev_get_drvdata(d);
6387 if (!un || un->magic != DGAP_UNIT_MAGIC)
6388 return 0;
6389 ch = un->un_ch;
6390 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
6391 return 0;
6392 bd = ch->ch_bd;
6393 if (!bd || bd->magic != DGAP_BOARD_MAGIC)
6394 return 0;
6395 if (bd->state != BOARD_READY)
6396 return 0;
6398 return snprintf(buf, PAGE_SIZE, "%x\n", ch->ch_c_cflag);
6400 static DEVICE_ATTR(cflag, S_IRUSR, dgap_tty_cflag_show, NULL);
6402 static ssize_t dgap_tty_oflag_show(struct device *d, struct device_attribute *attr, char *buf)
6404 struct board_t *bd;
6405 struct channel_t *ch;
6406 struct un_t *un;
6408 if (!d)
6409 return 0;
6410 un = dev_get_drvdata(d);
6411 if (!un || un->magic != DGAP_UNIT_MAGIC)
6412 return 0;
6413 ch = un->un_ch;
6414 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
6415 return 0;
6416 bd = ch->ch_bd;
6417 if (!bd || bd->magic != DGAP_BOARD_MAGIC)
6418 return 0;
6419 if (bd->state != BOARD_READY)
6420 return 0;
6422 return snprintf(buf, PAGE_SIZE, "%x\n", ch->ch_c_oflag);
6424 static DEVICE_ATTR(oflag, S_IRUSR, dgap_tty_oflag_show, NULL);
6426 static ssize_t dgap_tty_lflag_show(struct device *d, struct device_attribute *attr, char *buf)
6428 struct board_t *bd;
6429 struct channel_t *ch;
6430 struct un_t *un;
6432 if (!d)
6433 return 0;
6434 un = dev_get_drvdata(d);
6435 if (!un || un->magic != DGAP_UNIT_MAGIC)
6436 return 0;
6437 ch = un->un_ch;
6438 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
6439 return 0;
6440 bd = ch->ch_bd;
6441 if (!bd || bd->magic != DGAP_BOARD_MAGIC)
6442 return 0;
6443 if (bd->state != BOARD_READY)
6444 return 0;
6446 return snprintf(buf, PAGE_SIZE, "%x\n", ch->ch_c_lflag);
6448 static DEVICE_ATTR(lflag, S_IRUSR, dgap_tty_lflag_show, NULL);
6450 static ssize_t dgap_tty_digi_flag_show(struct device *d, struct device_attribute *attr, char *buf)
6452 struct board_t *bd;
6453 struct channel_t *ch;
6454 struct un_t *un;
6456 if (!d)
6457 return 0;
6458 un = dev_get_drvdata(d);
6459 if (!un || un->magic != DGAP_UNIT_MAGIC)
6460 return 0;
6461 ch = un->un_ch;
6462 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
6463 return 0;
6464 bd = ch->ch_bd;
6465 if (!bd || bd->magic != DGAP_BOARD_MAGIC)
6466 return 0;
6467 if (bd->state != BOARD_READY)
6468 return 0;
6470 return snprintf(buf, PAGE_SIZE, "%x\n", ch->ch_digi.digi_flags);
6472 static DEVICE_ATTR(digi_flag, S_IRUSR, dgap_tty_digi_flag_show, NULL);
6474 static ssize_t dgap_tty_rxcount_show(struct device *d, struct device_attribute *attr, char *buf)
6476 struct board_t *bd;
6477 struct channel_t *ch;
6478 struct un_t *un;
6480 if (!d)
6481 return 0;
6482 un = dev_get_drvdata(d);
6483 if (!un || un->magic != DGAP_UNIT_MAGIC)
6484 return 0;
6485 ch = un->un_ch;
6486 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
6487 return 0;
6488 bd = ch->ch_bd;
6489 if (!bd || bd->magic != DGAP_BOARD_MAGIC)
6490 return 0;
6491 if (bd->state != BOARD_READY)
6492 return 0;
6494 return snprintf(buf, PAGE_SIZE, "%ld\n", ch->ch_rxcount);
6496 static DEVICE_ATTR(rxcount, S_IRUSR, dgap_tty_rxcount_show, NULL);
6498 static ssize_t dgap_tty_txcount_show(struct device *d, struct device_attribute *attr, char *buf)
6500 struct board_t *bd;
6501 struct channel_t *ch;
6502 struct un_t *un;
6504 if (!d)
6505 return 0;
6506 un = dev_get_drvdata(d);
6507 if (!un || un->magic != DGAP_UNIT_MAGIC)
6508 return 0;
6509 ch = un->un_ch;
6510 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
6511 return 0;
6512 bd = ch->ch_bd;
6513 if (!bd || bd->magic != DGAP_BOARD_MAGIC)
6514 return 0;
6515 if (bd->state != BOARD_READY)
6516 return 0;
6518 return snprintf(buf, PAGE_SIZE, "%ld\n", ch->ch_txcount);
6520 static DEVICE_ATTR(txcount, S_IRUSR, dgap_tty_txcount_show, NULL);
6522 static ssize_t dgap_tty_name_show(struct device *d, struct device_attribute *attr, char *buf)
6524 struct board_t *bd;
6525 struct channel_t *ch;
6526 struct un_t *un;
6527 int cn;
6528 int bn;
6529 struct cnode *cptr = NULL;
6530 int found = FALSE;
6531 int ncount = 0;
6532 int starto = 0;
6533 int i = 0;
6535 if (!d)
6536 return 0;
6537 un = dev_get_drvdata(d);
6538 if (!un || un->magic != DGAP_UNIT_MAGIC)
6539 return 0;
6540 ch = un->un_ch;
6541 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
6542 return 0;
6543 bd = ch->ch_bd;
6544 if (!bd || bd->magic != DGAP_BOARD_MAGIC)
6545 return 0;
6546 if (bd->state != BOARD_READY)
6547 return 0;
6549 bn = bd->boardnum;
6550 cn = ch->ch_portnum;
6552 for (cptr = bd->bd_config; cptr; cptr = cptr->next) {
6554 if ((cptr->type == BNODE) &&
6555 ((cptr->u.board.type == APORT2_920P) || (cptr->u.board.type == APORT4_920P) ||
6556 (cptr->u.board.type == APORT8_920P) || (cptr->u.board.type == PAPORT4) ||
6557 (cptr->u.board.type == PAPORT8))) {
6559 found = TRUE;
6560 if (cptr->u.board.v_start)
6561 starto = cptr->u.board.start;
6562 else
6563 starto = 1;
6566 if (cptr->type == TNODE && found == TRUE) {
6567 char *ptr1;
6568 if (strstr(cptr->u.ttyname, "tty")) {
6569 ptr1 = cptr->u.ttyname;
6570 ptr1 += 3;
6571 } else
6572 ptr1 = cptr->u.ttyname;
6574 for (i = 0; i < dgap_config_get_number_of_ports(bd); i++) {
6575 if (cn == i)
6576 return snprintf(buf, PAGE_SIZE, "%s%s%02d\n",
6577 (un->un_type == DGAP_PRINT) ? "pr" : "tty",
6578 ptr1, i + starto);
6582 if (cptr->type == CNODE) {
6584 for (i = 0; i < cptr->u.conc.nport; i++) {
6585 if (cn == (i + ncount))
6587 return snprintf(buf, PAGE_SIZE, "%s%s%02d\n",
6588 (un->un_type == DGAP_PRINT) ? "pr" : "tty",
6589 cptr->u.conc.id,
6590 i + (cptr->u.conc.v_start ? cptr->u.conc.start : 1));
6593 ncount += cptr->u.conc.nport;
6596 if (cptr->type == MNODE) {
6598 for (i = 0; i < cptr->u.module.nport; i++) {
6599 if (cn == (i + ncount))
6600 return snprintf(buf, PAGE_SIZE, "%s%s%02d\n",
6601 (un->un_type == DGAP_PRINT) ? "pr" : "tty",
6602 cptr->u.module.id,
6603 i + (cptr->u.module.v_start ? cptr->u.module.start : 1));
6606 ncount += cptr->u.module.nport;
6611 return snprintf(buf, PAGE_SIZE, "%s_dgap_%d_%d\n",
6612 (un->un_type == DGAP_PRINT) ? "pr" : "tty", bn, cn);
6615 static DEVICE_ATTR(custom_name, S_IRUSR, dgap_tty_name_show, NULL);
6617 static struct attribute *dgap_sysfs_tty_entries[] = {
6618 &dev_attr_state.attr,
6619 &dev_attr_baud.attr,
6620 &dev_attr_msignals.attr,
6621 &dev_attr_iflag.attr,
6622 &dev_attr_cflag.attr,
6623 &dev_attr_oflag.attr,
6624 &dev_attr_lflag.attr,
6625 &dev_attr_digi_flag.attr,
6626 &dev_attr_rxcount.attr,
6627 &dev_attr_txcount.attr,
6628 &dev_attr_custom_name.attr,
6629 NULL
6632 static struct attribute_group dgap_tty_attribute_group = {
6633 .name = NULL,
6634 .attrs = dgap_sysfs_tty_entries,
6637 static void dgap_create_tty_sysfs(struct un_t *un, struct device *c)
6639 int ret;
6641 ret = sysfs_create_group(&c->kobj, &dgap_tty_attribute_group);
6642 if (ret)
6643 return;
6645 dev_set_drvdata(c, un);
6649 static void dgap_remove_tty_sysfs(struct device *c)
6651 sysfs_remove_group(&c->kobj, &dgap_tty_attribute_group);
6655 * Parse a configuration file read into memory as a string.
6657 static int dgap_parsefile(char **in, int Remove)
6659 struct cnode *p, *brd, *line, *conc;
6660 int rc;
6661 char *s = NULL, *s2 = NULL;
6662 int linecnt = 0;
6664 p = &dgap_head;
6665 brd = line = conc = NULL;
6667 /* perhaps we are adding to an existing list? */
6668 while (p->next != NULL)
6669 p = p->next;
6671 /* file must start with a BEGIN */
6672 while ((rc = dgap_gettok(in, p)) != BEGIN) {
6673 if (rc == 0) {
6674 dgap_err("unexpected EOF");
6675 return -1;
6679 for (; ;) {
6680 rc = dgap_gettok(in, p);
6681 if (rc == 0) {
6682 dgap_err("unexpected EOF");
6683 return -1;
6686 switch (rc) {
6687 case 0:
6688 dgap_err("unexpected end of file");
6689 return -1;
6691 case BEGIN: /* should only be 1 begin */
6692 dgap_err("unexpected config_begin\n");
6693 return -1;
6695 case END:
6696 return 0;
6698 case BOARD: /* board info */
6699 if (dgap_checknode(p))
6700 return -1;
6701 p->next = dgap_newnode(BNODE);
6702 if (!p->next) {
6703 dgap_err("out of memory");
6704 return -1;
6706 p = p->next;
6708 p->u.board.status = dgap_savestring("No");
6709 line = conc = NULL;
6710 brd = p;
6711 linecnt = -1;
6712 break;
6714 case APORT2_920P: /* AccelePort_4 */
6715 if (p->type != BNODE) {
6716 dgap_err("unexpected Digi_2r_920 string");
6717 return -1;
6719 p->u.board.type = APORT2_920P;
6720 p->u.board.v_type = 1;
6721 break;
6723 case APORT4_920P: /* AccelePort_4 */
6724 if (p->type != BNODE) {
6725 dgap_err("unexpected Digi_4r_920 string");
6726 return -1;
6728 p->u.board.type = APORT4_920P;
6729 p->u.board.v_type = 1;
6730 break;
6732 case APORT8_920P: /* AccelePort_8 */
6733 if (p->type != BNODE) {
6734 dgap_err("unexpected Digi_8r_920 string");
6735 return -1;
6737 p->u.board.type = APORT8_920P;
6738 p->u.board.v_type = 1;
6739 break;
6741 case PAPORT4: /* AccelePort_4 PCI */
6742 if (p->type != BNODE) {
6743 dgap_err("unexpected Digi_4r(PCI) string");
6744 return -1;
6746 p->u.board.type = PAPORT4;
6747 p->u.board.v_type = 1;
6748 break;
6750 case PAPORT8: /* AccelePort_8 PCI */
6751 if (p->type != BNODE) {
6752 dgap_err("unexpected Digi_8r string");
6753 return -1;
6755 p->u.board.type = PAPORT8;
6756 p->u.board.v_type = 1;
6757 break;
6759 case PCX: /* PCI C/X */
6760 if (p->type != BNODE) {
6761 dgap_err("unexpected Digi_C/X_(PCI) string");
6762 return -1;
6764 p->u.board.type = PCX;
6765 p->u.board.v_type = 1;
6766 p->u.board.conc1 = 0;
6767 p->u.board.conc2 = 0;
6768 p->u.board.module1 = 0;
6769 p->u.board.module2 = 0;
6770 break;
6772 case PEPC: /* PCI EPC/X */
6773 if (p->type != BNODE) {
6774 dgap_err("unexpected \"Digi_EPC/X_(PCI)\" string");
6775 return -1;
6777 p->u.board.type = PEPC;
6778 p->u.board.v_type = 1;
6779 p->u.board.conc1 = 0;
6780 p->u.board.conc2 = 0;
6781 p->u.board.module1 = 0;
6782 p->u.board.module2 = 0;
6783 break;
6785 case PPCM: /* PCI/Xem */
6786 if (p->type != BNODE) {
6787 dgap_err("unexpected PCI/Xem string");
6788 return -1;
6790 p->u.board.type = PPCM;
6791 p->u.board.v_type = 1;
6792 p->u.board.conc1 = 0;
6793 p->u.board.conc2 = 0;
6794 break;
6796 case IO: /* i/o port */
6797 if (p->type != BNODE) {
6798 dgap_err("IO port only vaild for boards");
6799 return -1;
6801 s = dgap_getword(in);
6802 if (s == NULL) {
6803 dgap_err("unexpected end of file");
6804 return -1;
6806 p->u.board.portstr = dgap_savestring(s);
6807 p->u.board.port = (short)simple_strtol(s, &s2, 0);
6808 if ((short)strlen(s) > (short)(s2 - s)) {
6809 dgap_err("bad number for IO port");
6810 return -1;
6812 p->u.board.v_port = 1;
6813 break;
6815 case MEM: /* memory address */
6816 if (p->type != BNODE) {
6817 dgap_err("memory address only vaild for boards");
6818 return -1;
6820 s = dgap_getword(in);
6821 if (s == NULL) {
6822 dgap_err("unexpected end of file");
6823 return -1;
6825 p->u.board.addrstr = dgap_savestring(s);
6826 p->u.board.addr = simple_strtoul(s, &s2, 0);
6827 if ((int)strlen(s) > (int)(s2 - s)) {
6828 dgap_err("bad number for memory address");
6829 return -1;
6831 p->u.board.v_addr = 1;
6832 break;
6834 case PCIINFO: /* pci information */
6835 if (p->type != BNODE) {
6836 dgap_err("memory address only vaild for boards");
6837 return -1;
6839 s = dgap_getword(in);
6840 if (s == NULL) {
6841 dgap_err("unexpected end of file");
6842 return -1;
6844 p->u.board.pcibusstr = dgap_savestring(s);
6845 p->u.board.pcibus = simple_strtoul(s, &s2, 0);
6846 if ((int)strlen(s) > (int)(s2 - s)) {
6847 dgap_err("bad number for pci bus");
6848 return -1;
6850 p->u.board.v_pcibus = 1;
6851 s = dgap_getword(in);
6852 if (s == NULL) {
6853 dgap_err("unexpected end of file");
6854 return -1;
6856 p->u.board.pcislotstr = dgap_savestring(s);
6857 p->u.board.pcislot = simple_strtoul(s, &s2, 0);
6858 if ((int)strlen(s) > (int)(s2 - s)) {
6859 dgap_err("bad number for pci slot");
6860 return -1;
6862 p->u.board.v_pcislot = 1;
6863 break;
6865 case METHOD:
6866 if (p->type != BNODE) {
6867 dgap_err("install method only vaild for boards");
6868 return -1;
6870 s = dgap_getword(in);
6871 if (s == NULL) {
6872 dgap_err("unexpected end of file");
6873 return -1;
6875 p->u.board.method = dgap_savestring(s);
6876 p->u.board.v_method = 1;
6877 break;
6879 case STATUS:
6880 if (p->type != BNODE) {
6881 dgap_err("config status only vaild for boards");
6882 return -1;
6884 s = dgap_getword(in);
6885 if (s == NULL) {
6886 dgap_err("unexpected end of file");
6887 return -1;
6889 p->u.board.status = dgap_savestring(s);
6890 break;
6892 case NPORTS: /* number of ports */
6893 if (p->type == BNODE) {
6894 s = dgap_getword(in);
6895 if (s == NULL) {
6896 dgap_err("unexpected end of file");
6897 return -1;
6899 p->u.board.nport = (char)simple_strtol(s, &s2, 0);
6900 if ((int)strlen(s) > (int)(s2 - s)) {
6901 dgap_err("bad number for number of ports");
6902 return -1;
6904 p->u.board.v_nport = 1;
6905 } else if (p->type == CNODE) {
6906 s = dgap_getword(in);
6907 if (s == NULL) {
6908 dgap_err("unexpected end of file");
6909 return -1;
6911 p->u.conc.nport = (char)simple_strtol(s, &s2, 0);
6912 if ((int)strlen(s) > (int)(s2 - s)) {
6913 dgap_err("bad number for number of ports");
6914 return -1;
6916 p->u.conc.v_nport = 1;
6917 } else if (p->type == MNODE) {
6918 s = dgap_getword(in);
6919 if (s == NULL) {
6920 dgap_err("unexpected end of file");
6921 return -1;
6923 p->u.module.nport = (char)simple_strtol(s, &s2, 0);
6924 if ((int)strlen(s) > (int)(s2 - s)) {
6925 dgap_err("bad number for number of ports");
6926 return -1;
6928 p->u.module.v_nport = 1;
6929 } else {
6930 dgap_err("nports only valid for concentrators or modules");
6931 return -1;
6933 break;
6935 case ID: /* letter ID used in tty name */
6936 s = dgap_getword(in);
6937 if (s == NULL) {
6938 dgap_err("unexpected end of file");
6939 return -1;
6942 p->u.board.status = dgap_savestring(s);
6944 if (p->type == CNODE) {
6945 p->u.conc.id = dgap_savestring(s);
6946 p->u.conc.v_id = 1;
6947 } else if (p->type == MNODE) {
6948 p->u.module.id = dgap_savestring(s);
6949 p->u.module.v_id = 1;
6950 } else {
6951 dgap_err("id only valid for concentrators or modules");
6952 return -1;
6954 break;
6956 case STARTO: /* start offset of ID */
6957 if (p->type == BNODE) {
6958 s = dgap_getword(in);
6959 if (s == NULL) {
6960 dgap_err("unexpected end of file");
6961 return -1;
6963 p->u.board.start = simple_strtol(s, &s2, 0);
6964 if ((int)strlen(s) > (int)(s2 - s)) {
6965 dgap_err("bad number for start of tty count");
6966 return -1;
6968 p->u.board.v_start = 1;
6969 } else if (p->type == CNODE) {
6970 s = dgap_getword(in);
6971 if (s == NULL) {
6972 dgap_err("unexpected end of file");
6973 return -1;
6975 p->u.conc.start = simple_strtol(s, &s2, 0);
6976 if ((int)strlen(s) > (int)(s2 - s)) {
6977 dgap_err("bad number for start of tty count");
6978 return -1;
6980 p->u.conc.v_start = 1;
6981 } else if (p->type == MNODE) {
6982 s = dgap_getword(in);
6983 if (s == NULL) {
6984 dgap_err("unexpected end of file");
6985 return -1;
6987 p->u.module.start = simple_strtol(s, &s2, 0);
6988 if ((int)strlen(s) > (int)(s2 - s)) {
6989 dgap_err("bad number for start of tty count");
6990 return -1;
6992 p->u.module.v_start = 1;
6993 } else {
6994 dgap_err("start only valid for concentrators or modules");
6995 return -1;
6997 break;
6999 case TTYN: /* tty name prefix */
7000 if (dgap_checknode(p))
7001 return -1;
7002 p->next = dgap_newnode(TNODE);
7003 if (!p->next) {
7004 dgap_err("out of memory");
7005 return -1;
7007 p = p->next;
7008 s = dgap_getword(in);
7009 if (!s) {
7010 dgap_err("unexpeced end of file");
7011 return -1;
7013 p->u.ttyname = dgap_savestring(s);
7014 if (!p->u.ttyname) {
7015 dgap_err("out of memory");
7016 return -1;
7018 break;
7020 case CU: /* cu name prefix */
7021 if (dgap_checknode(p))
7022 return -1;
7023 p->next = dgap_newnode(CUNODE);
7024 if (!p->next) {
7025 dgap_err("out of memory");
7026 return -1;
7028 p = p->next;
7029 s = dgap_getword(in);
7030 if (!s) {
7031 dgap_err("unexpeced end of file");
7032 return -1;
7034 p->u.cuname = dgap_savestring(s);
7035 if (!p->u.cuname) {
7036 dgap_err("out of memory");
7037 return -1;
7039 break;
7041 case LINE: /* line information */
7042 if (dgap_checknode(p))
7043 return -1;
7044 if (brd == NULL) {
7045 dgap_err("must specify board before line info");
7046 return -1;
7048 switch (brd->u.board.type) {
7049 case PPCM:
7050 dgap_err("line not vaild for PC/em");
7051 return -1;
7053 p->next = dgap_newnode(LNODE);
7054 if (!p->next) {
7055 dgap_err("out of memory");
7056 return -1;
7058 p = p->next;
7059 conc = NULL;
7060 line = p;
7061 linecnt++;
7062 break;
7064 case CONC: /* concentrator information */
7065 if (dgap_checknode(p))
7066 return -1;
7067 if (line == NULL) {
7068 dgap_err("must specify line info before concentrator");
7069 return -1;
7071 p->next = dgap_newnode(CNODE);
7072 if (!p->next) {
7073 dgap_err("out of memory");
7074 return -1;
7076 p = p->next;
7077 conc = p;
7078 if (linecnt)
7079 brd->u.board.conc2++;
7080 else
7081 brd->u.board.conc1++;
7083 break;
7085 case CX: /* c/x type concentrator */
7086 if (p->type != CNODE) {
7087 dgap_err("cx only valid for concentrators");
7088 return -1;
7090 p->u.conc.type = CX;
7091 p->u.conc.v_type = 1;
7092 break;
7094 case EPC: /* epc type concentrator */
7095 if (p->type != CNODE) {
7096 dgap_err("cx only valid for concentrators");
7097 return -1;
7099 p->u.conc.type = EPC;
7100 p->u.conc.v_type = 1;
7101 break;
7103 case MOD: /* EBI module */
7104 if (dgap_checknode(p))
7105 return -1;
7106 if (brd == NULL) {
7107 dgap_err("must specify board info before EBI modules");
7108 return -1;
7110 switch (brd->u.board.type) {
7111 case PPCM:
7112 linecnt = 0;
7113 break;
7114 default:
7115 if (conc == NULL) {
7116 dgap_err("must specify concentrator info before EBI module");
7117 return -1;
7120 p->next = dgap_newnode(MNODE);
7121 if (!p->next) {
7122 dgap_err("out of memory");
7123 return -1;
7125 p = p->next;
7126 if (linecnt)
7127 brd->u.board.module2++;
7128 else
7129 brd->u.board.module1++;
7131 break;
7133 case PORTS: /* ports type EBI module */
7134 if (p->type != MNODE) {
7135 dgap_err("ports only valid for EBI modules");
7136 return -1;
7138 p->u.module.type = PORTS;
7139 p->u.module.v_type = 1;
7140 break;
7142 case MODEM: /* ports type EBI module */
7143 if (p->type != MNODE) {
7144 dgap_err("modem only valid for modem modules");
7145 return -1;
7147 p->u.module.type = MODEM;
7148 p->u.module.v_type = 1;
7149 break;
7151 case CABLE:
7152 if (p->type == LNODE) {
7153 s = dgap_getword(in);
7154 if (!s) {
7155 dgap_err("unexpected end of file");
7156 return -1;
7158 p->u.line.cable = dgap_savestring(s);
7159 p->u.line.v_cable = 1;
7161 break;
7163 case SPEED: /* sync line speed indication */
7164 if (p->type == LNODE) {
7165 s = dgap_getword(in);
7166 if (s == NULL) {
7167 dgap_err("unexpected end of file");
7168 return -1;
7170 p->u.line.speed = (char)simple_strtol(s, &s2, 0);
7171 if ((short)strlen(s) > (short)(s2 - s)) {
7172 dgap_err("bad number for line speed");
7173 return -1;
7175 p->u.line.v_speed = 1;
7176 } else if (p->type == CNODE) {
7177 s = dgap_getword(in);
7178 if (s == NULL) {
7179 dgap_err("unexpected end of file");
7180 return -1;
7182 p->u.conc.speed = (char)simple_strtol(s, &s2, 0);
7183 if ((short)strlen(s) > (short)(s2 - s)) {
7184 dgap_err("bad number for line speed");
7185 return -1;
7187 p->u.conc.v_speed = 1;
7188 } else {
7189 dgap_err("speed valid only for lines or concentrators.");
7190 return -1;
7192 break;
7194 case CONNECT:
7195 if (p->type == CNODE) {
7196 s = dgap_getword(in);
7197 if (!s) {
7198 dgap_err("unexpected end of file");
7199 return -1;
7201 p->u.conc.connect = dgap_savestring(s);
7202 p->u.conc.v_connect = 1;
7204 break;
7205 case PRINT: /* transparent print name prefix */
7206 if (dgap_checknode(p))
7207 return -1;
7208 p->next = dgap_newnode(PNODE);
7209 if (!p->next) {
7210 dgap_err("out of memory");
7211 return -1;
7213 p = p->next;
7214 s = dgap_getword(in);
7215 if (!s) {
7216 dgap_err("unexpeced end of file");
7217 return -1;
7219 p->u.printname = dgap_savestring(s);
7220 if (!p->u.printname) {
7221 dgap_err("out of memory");
7222 return -1;
7224 break;
7226 case CMAJOR: /* major number */
7227 if (dgap_checknode(p))
7228 return -1;
7229 p->next = dgap_newnode(JNODE);
7230 if (!p->next) {
7231 dgap_err("out of memory");
7232 return -1;
7234 p = p->next;
7235 s = dgap_getword(in);
7236 if (s == NULL) {
7237 dgap_err("unexpected end of file");
7238 return -1;
7240 p->u.majornumber = simple_strtol(s, &s2, 0);
7241 if ((int)strlen(s) > (int)(s2 - s)) {
7242 dgap_err("bad number for major number");
7243 return -1;
7245 break;
7247 case ALTPIN: /* altpin setting */
7248 if (dgap_checknode(p))
7249 return -1;
7250 p->next = dgap_newnode(ANODE);
7251 if (!p->next) {
7252 dgap_err("out of memory");
7253 return -1;
7255 p = p->next;
7256 s = dgap_getword(in);
7257 if (s == NULL) {
7258 dgap_err("unexpected end of file");
7259 return -1;
7261 p->u.altpin = simple_strtol(s, &s2, 0);
7262 if ((int)strlen(s) > (int)(s2 - s)) {
7263 dgap_err("bad number for altpin");
7264 return -1;
7266 break;
7268 case USEINTR: /* enable interrupt setting */
7269 if (dgap_checknode(p))
7270 return -1;
7271 p->next = dgap_newnode(INTRNODE);
7272 if (!p->next) {
7273 dgap_err("out of memory");
7274 return -1;
7276 p = p->next;
7277 s = dgap_getword(in);
7278 if (s == NULL) {
7279 dgap_err("unexpected end of file");
7280 return -1;
7282 p->u.useintr = simple_strtol(s, &s2, 0);
7283 if ((int)strlen(s) > (int)(s2 - s)) {
7284 dgap_err("bad number for useintr");
7285 return -1;
7287 break;
7289 case TTSIZ: /* size of tty structure */
7290 if (dgap_checknode(p))
7291 return -1;
7292 p->next = dgap_newnode(TSNODE);
7293 if (!p->next) {
7294 dgap_err("out of memory");
7295 return -1;
7297 p = p->next;
7298 s = dgap_getword(in);
7299 if (s == NULL) {
7300 dgap_err("unexpected end of file");
7301 return -1;
7303 p->u.ttysize = simple_strtol(s, &s2, 0);
7304 if ((int)strlen(s) > (int)(s2 - s)) {
7305 dgap_err("bad number for ttysize");
7306 return -1;
7308 break;
7310 case CHSIZ: /* channel structure size */
7311 if (dgap_checknode(p))
7312 return -1;
7313 p->next = dgap_newnode(CSNODE);
7314 if (!p->next) {
7315 dgap_err("out of memory");
7316 return -1;
7318 p = p->next;
7319 s = dgap_getword(in);
7320 if (s == NULL) {
7321 dgap_err("unexpected end of file");
7322 return -1;
7324 p->u.chsize = simple_strtol(s, &s2, 0);
7325 if ((int)strlen(s) > (int)(s2 - s)) {
7326 dgap_err("bad number for chsize");
7327 return -1;
7329 break;
7331 case BSSIZ: /* board structure size */
7332 if (dgap_checknode(p))
7333 return -1;
7334 p->next = dgap_newnode(BSNODE);
7335 if (!p->next) {
7336 dgap_err("out of memory");
7337 return -1;
7339 p = p->next;
7340 s = dgap_getword(in);
7341 if (s == NULL) {
7342 dgap_err("unexpected end of file");
7343 return -1;
7345 p->u.bssize = simple_strtol(s, &s2, 0);
7346 if ((int)strlen(s) > (int)(s2 - s)) {
7347 dgap_err("bad number for bssize");
7348 return -1;
7350 break;
7352 case UNTSIZ: /* sched structure size */
7353 if (dgap_checknode(p))
7354 return -1;
7355 p->next = dgap_newnode(USNODE);
7356 if (!p->next) {
7357 dgap_err("out of memory");
7358 return -1;
7360 p = p->next;
7361 s = dgap_getword(in);
7362 if (s == NULL) {
7363 dgap_err("unexpected end of file");
7364 return -1;
7366 p->u.unsize = simple_strtol(s, &s2, 0);
7367 if ((int)strlen(s) > (int)(s2 - s)) {
7368 dgap_err("bad number for schedsize");
7369 return -1;
7371 break;
7373 case F2SIZ: /* f2200 structure size */
7374 if (dgap_checknode(p))
7375 return -1;
7376 p->next = dgap_newnode(FSNODE);
7377 if (!p->next) {
7378 dgap_err("out of memory");
7379 return -1;
7381 p = p->next;
7382 s = dgap_getword(in);
7383 if (s == NULL) {
7384 dgap_err("unexpected end of file");
7385 return -1;
7387 p->u.f2size = simple_strtol(s, &s2, 0);
7388 if ((int)strlen(s) > (int)(s2 - s)) {
7389 dgap_err("bad number for f2200size");
7390 return -1;
7392 break;
7394 case VPSIZ: /* vpix structure size */
7395 if (dgap_checknode(p))
7396 return -1;
7397 p->next = dgap_newnode(VSNODE);
7398 if (!p->next) {
7399 dgap_err("out of memory");
7400 return -1;
7402 p = p->next;
7403 s = dgap_getword(in);
7404 if (s == NULL) {
7405 dgap_err("unexpected end of file");
7406 return -1;
7408 p->u.vpixsize = simple_strtol(s, &s2, 0);
7409 if ((int)strlen(s) > (int)(s2 - s)) {
7410 dgap_err("bad number for vpixsize");
7411 return -1;
7413 break;
7419 * dgap_sindex: much like index(), but it looks for a match of any character in
7420 * the group, and returns that position. If the first character is a ^, then
7421 * this will match the first occurrence not in that group.
7423 static char *dgap_sindex(char *string, char *group)
7425 char *ptr;
7427 if (!string || !group)
7428 return (char *) NULL;
7430 if (*group == '^') {
7431 group++;
7432 for (; *string; string++) {
7433 for (ptr = group; *ptr; ptr++) {
7434 if (*ptr == *string)
7435 break;
7437 if (*ptr == '\0')
7438 return string;
7440 } else {
7441 for (; *string; string++) {
7442 for (ptr = group; *ptr; ptr++) {
7443 if (*ptr == *string)
7444 return string;
7449 return (char *) NULL;
7453 * Get a token from the input file; return 0 if end of file is reached
7455 static int dgap_gettok(char **in, struct cnode *p)
7457 char *w;
7458 struct toklist *t;
7460 if (strstr(dgap_cword, "boar")) {
7461 w = dgap_getword(in);
7462 snprintf(dgap_cword, MAXCWORD, "%s", w);
7463 for (t = dgap_tlist; t->token != 0; t++) {
7464 if (!strcmp(w, t->string))
7465 return t->token;
7467 dgap_err("board !!type not specified");
7468 return 1;
7469 } else {
7470 while ((w = dgap_getword(in))) {
7471 snprintf(dgap_cword, MAXCWORD, "%s", w);
7472 for (t = dgap_tlist; t->token != 0; t++) {
7473 if (!strcmp(w, t->string))
7474 return t->token;
7477 return 0;
7482 * get a word from the input stream, also keep track of current line number.
7483 * words are separated by whitespace.
7485 static char *dgap_getword(char **in)
7487 char *ret_ptr = *in;
7489 char *ptr = dgap_sindex(*in, " \t\n");
7491 /* If no word found, return null */
7492 if (!ptr)
7493 return NULL;
7495 /* Mark new location for our buffer */
7496 *ptr = '\0';
7497 *in = ptr + 1;
7499 /* Eat any extra spaces/tabs/newlines that might be present */
7500 while (*in && **in && ((**in == ' ') || (**in == '\t') || (**in == '\n'))) {
7501 **in = '\0';
7502 *in = *in + 1;
7505 return ret_ptr;
7509 * print an error message, giving the line number in the file where
7510 * the error occurred.
7512 static void dgap_err(char *s)
7514 pr_err("dgap: parse: %s\n", s);
7518 * allocate a new configuration node of type t
7520 static struct cnode *dgap_newnode(int t)
7522 struct cnode *n;
7524 n = kmalloc(sizeof(struct cnode), GFP_ATOMIC);
7525 if (n != NULL) {
7526 memset((char *)n, 0, sizeof(struct cnode));
7527 n->type = t;
7529 return n;
7533 * dgap_checknode: see if all the necessary info has been supplied for a node
7534 * before creating the next node.
7536 static int dgap_checknode(struct cnode *p)
7538 switch (p->type) {
7539 case BNODE:
7540 if (p->u.board.v_type == 0) {
7541 dgap_err("board type !not specified");
7542 return 1;
7545 return 0;
7547 case LNODE:
7548 if (p->u.line.v_speed == 0) {
7549 dgap_err("line speed not specified");
7550 return 1;
7552 return 0;
7554 case CNODE:
7555 if (p->u.conc.v_type == 0) {
7556 dgap_err("concentrator type not specified");
7557 return 1;
7559 if (p->u.conc.v_speed == 0) {
7560 dgap_err("concentrator line speed not specified");
7561 return 1;
7563 if (p->u.conc.v_nport == 0) {
7564 dgap_err("number of ports on concentrator not specified");
7565 return 1;
7567 if (p->u.conc.v_id == 0) {
7568 dgap_err("concentrator id letter not specified");
7569 return 1;
7571 return 0;
7573 case MNODE:
7574 if (p->u.module.v_type == 0) {
7575 dgap_err("EBI module type not specified");
7576 return 1;
7578 if (p->u.module.v_nport == 0) {
7579 dgap_err("number of ports on EBI module not specified");
7580 return 1;
7582 if (p->u.module.v_id == 0) {
7583 dgap_err("EBI module id letter not specified");
7584 return 1;
7586 return 0;
7588 return 0;
7592 * save a string somewhere
7594 static char *dgap_savestring(char *s)
7596 char *p;
7598 p = kmalloc(strlen(s) + 1, GFP_ATOMIC);
7599 if (p)
7600 strcpy(p, s);
7601 return p;
7605 * Given a board pointer, returns whether we should use interrupts or not.
7607 static uint dgap_config_get_useintr(struct board_t *bd)
7609 struct cnode *p = NULL;
7611 if (!bd)
7612 return 0;
7614 for (p = bd->bd_config; p; p = p->next) {
7615 switch (p->type) {
7616 case INTRNODE:
7618 * check for pcxr types.
7620 return p->u.useintr;
7621 default:
7622 break;
7626 /* If not found, then don't turn on interrupts. */
7627 return 0;
7631 * Given a board pointer, returns whether we turn on altpin or not.
7633 static uint dgap_config_get_altpin(struct board_t *bd)
7635 struct cnode *p = NULL;
7637 if (!bd)
7638 return 0;
7640 for (p = bd->bd_config; p; p = p->next) {
7641 switch (p->type) {
7642 case ANODE:
7644 * check for pcxr types.
7646 return p->u.altpin;
7647 default:
7648 break;
7652 /* If not found, then don't turn on interrupts. */
7653 return 0;
7657 * Given a specific type of board, if found, detached link and
7658 * returns the first occurrence in the list.
7660 static struct cnode *dgap_find_config(int type, int bus, int slot)
7662 struct cnode *p, *prev = NULL, *prev2 = NULL, *found = NULL;
7664 p = &dgap_head;
7666 while (p->next != NULL) {
7667 prev = p;
7668 p = p->next;
7670 if (p->type == BNODE) {
7672 if (p->u.board.type == type) {
7674 if (p->u.board.v_pcibus &&
7675 p->u.board.pcibus != bus)
7676 continue;
7677 if (p->u.board.v_pcislot &&
7678 p->u.board.pcislot != slot)
7679 continue;
7681 found = p;
7683 * Keep walking thru the list till we find the next board.
7685 while (p->next != NULL) {
7686 prev2 = p;
7687 p = p->next;
7688 if (p->type == BNODE) {
7691 * Mark the end of our 1 board chain of configs.
7693 prev2->next = NULL;
7696 * Link the "next" board to the previous board,
7697 * effectively "unlinking" our board from the main config.
7699 prev->next = p;
7701 return found;
7705 * It must be the last board in the list.
7707 prev->next = NULL;
7708 return found;
7712 return NULL;
7716 * Given a board pointer, walks the config link, counting up
7717 * all ports user specified should be on the board.
7718 * (This does NOT mean they are all actually present right now tho)
7720 static uint dgap_config_get_number_of_ports(struct board_t *bd)
7722 int count = 0;
7723 struct cnode *p = NULL;
7725 if (!bd)
7726 return 0;
7728 for (p = bd->bd_config; p; p = p->next) {
7730 switch (p->type) {
7731 case BNODE:
7733 * check for pcxr types.
7735 if (p->u.board.type > EPCFE)
7736 count += p->u.board.nport;
7737 break;
7738 case CNODE:
7739 count += p->u.conc.nport;
7740 break;
7741 case MNODE:
7742 count += p->u.module.nport;
7743 break;
7746 return count;
7749 static char *dgap_create_config_string(struct board_t *bd, char *string)
7751 char *ptr = string;
7752 struct cnode *p = NULL;
7753 struct cnode *q = NULL;
7754 int speed;
7756 if (!bd) {
7757 *ptr = 0xff;
7758 return string;
7761 for (p = bd->bd_config; p; p = p->next) {
7763 switch (p->type) {
7764 case LNODE:
7765 *ptr = '\0';
7766 ptr++;
7767 *ptr = p->u.line.speed;
7768 ptr++;
7769 break;
7770 case CNODE:
7772 * Because the EPC/con concentrators can have EM modules
7773 * hanging off of them, we have to walk ahead in the list
7774 * and keep adding the number of ports on each EM to the config.
7775 * UGH!
7777 speed = p->u.conc.speed;
7778 q = p->next;
7779 if ((q != NULL) && (q->type == MNODE)) {
7780 *ptr = (p->u.conc.nport + 0x80);
7781 ptr++;
7782 p = q;
7783 while ((q->next != NULL) && (q->next->type) == MNODE) {
7784 *ptr = (q->u.module.nport + 0x80);
7785 ptr++;
7786 p = q;
7787 q = q->next;
7789 *ptr = q->u.module.nport;
7790 ptr++;
7791 } else {
7792 *ptr = p->u.conc.nport;
7793 ptr++;
7796 *ptr = speed;
7797 ptr++;
7798 break;
7802 *ptr = 0xff;
7803 return string;