blob: bad355100825d569834065a8b6e41aa3d0a2a44b [file] [log] [blame]
Scott_Kilau@digi.comc84b8b52013-08-21 21:48:31 -04001/*
2 * Copyright 2003 Digi International (www.digi.com)
3 * Scott H Kilau <Scott_Kilau at digi dot com>
4 *
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.
Masanari Iida818cc6f2014-01-15 00:40:44 +09009 *
Scott_Kilau@digi.comc84b8b52013-08-21 21:48:31 -040010 * This program is distributed in the hope that it will be useful,
Masanari Iida818cc6f2014-01-15 00:40:44 +090011 * but WITHOUT ANY WARRANTY, EXPRESS OR IMPLIED; without even the
12 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
Scott_Kilau@digi.comc84b8b52013-08-21 21:48:31 -040013 * PURPOSE. See the GNU General Public License for more details.
Masanari Iida818cc6f2014-01-15 00:40:44 +090014 *
Scott_Kilau@digi.comc84b8b52013-08-21 21:48:31 -040015 */
16
Mark Hounschellb28ec882014-02-20 08:48:48 -050017/*
18 * In the original out of kernel Digi dgap driver, firmware
19 * loading was done via user land to driver handshaking.
20 *
21 * For cards that support a concentrator (port expander),
22 * I believe the concentrator its self told the card which
23 * concentrator is actually attached and then that info
24 * was used to tell user land which concentrator firmware
25 * image was to be downloaded. I think even the BIOS or
26 * FEP images required could change with the connection
27 * of a particular concentrator.
28 *
29 * Since I have no access to any of these cards or
30 * concentrators, I cannot put the correct concentrator
31 * firmware file names into the firmware_info structure
32 * as is now done for the BIOS and FEP images.
33 *
34 * I think, but am not certain, that the cards supporting
35 * concentrators will function without them. So support
36 * of these cards has been left in this driver.
37 *
38 * In order to fully support those cards, they would
39 * either have to be acquired for dissection or maybe
40 * Digi International could provide some assistance.
41 */
42#undef DIGI_CONCENTRATORS_SUPPORTED
Scott_Kilau@digi.comc84b8b52013-08-21 21:48:31 -040043
Daeseok Youn6c66843d2014-08-09 14:39:29 +090044#define pr_fmt(fmt) "dgap: " fmt
45
Scott_Kilau@digi.comc84b8b52013-08-21 21:48:31 -040046#include <linux/kernel.h>
Scott_Kilau@digi.comc84b8b52013-08-21 21:48:31 -040047#include <linux/module.h>
48#include <linux/pci.h>
49#include <linux/delay.h> /* For udelay */
Geert Uytterhoeven351699d2013-08-29 22:53:25 +020050#include <linux/slab.h>
Mark Hounschell3d9dc5d2014-02-28 12:42:12 -050051#include <linux/uaccess.h>
Scott_Kilau@digi.comc84b8b52013-08-21 21:48:31 -040052#include <linux/sched.h>
Scott_Kilau@digi.comc84b8b52013-08-21 21:48:31 -040053
Mark Hounschella6792a32014-02-19 13:11:59 -050054#include <linux/interrupt.h> /* For tasklet and interrupt structs/defines */
55#include <linux/ctype.h>
56#include <linux/tty.h>
57#include <linux/tty_flip.h>
58#include <linux/serial_reg.h>
Mark Hounschell3d9dc5d2014-02-28 12:42:12 -050059#include <linux/io.h> /* For read[bwl]/write[bwl] */
Mark Hounschella6792a32014-02-19 13:11:59 -050060
Mark Hounschell7568f7d2014-02-19 13:12:01 -050061#include <linux/string.h>
62#include <linux/device.h>
63#include <linux/kdev_t.h>
Mark Hounschellb28ec882014-02-20 08:48:48 -050064#include <linux/firmware.h>
Mark Hounschell7568f7d2014-02-19 13:12:01 -050065
Mark Hounschellffc1c1d2014-02-19 13:12:14 -050066#include "dgap.h"
Mark Hounschella6792a32014-02-19 13:11:59 -050067
Scott_Kilau@digi.comc84b8b52013-08-21 21:48:31 -040068/*
69 * File operations permitted on Control/Management major.
70 */
Mark Hounschellfea06832014-04-25 16:49:29 -040071static const struct file_operations dgap_board_fops = {
Mark Hounschell305ec872014-02-28 12:42:13 -050072 .owner = THIS_MODULE,
Scott_Kilau@digi.comc84b8b52013-08-21 21:48:31 -040073};
74
Mark Hounschellfea06832014-04-25 16:49:29 -040075static uint dgap_numboards;
76static struct board_t *dgap_board[MAXBOARDS];
Mark Hounschellb115b022014-02-28 12:42:15 -050077static ulong dgap_poll_counter;
Mark Hounschellb115b022014-02-28 12:42:15 -050078static int dgap_driver_state = DRIVER_INITIALIZED;
Mark Hounschellb115b022014-02-28 12:42:15 -050079static int dgap_poll_tick = 20; /* Poll interval - 20 ms */
Scott_Kilau@digi.comc84b8b52013-08-21 21:48:31 -040080
Mark Hounschellb115b022014-02-28 12:42:15 -050081static struct class *dgap_class;
Scott_Kilau@digi.comc84b8b52013-08-21 21:48:31 -040082
Mark Hounschellb053bb82014-02-19 13:12:00 -050083static uint dgap_count = 500;
Mark Hounschella6792a32014-02-19 13:11:59 -050084
Scott_Kilau@digi.comc84b8b52013-08-21 21:48:31 -040085/*
86 * Poller stuff
87 */
Mark Hounschell45c44dd2014-04-24 10:33:58 -040088static DEFINE_SPINLOCK(dgap_poll_lock); /* Poll scheduling lock */
Mark Hounschellb115b022014-02-28 12:42:15 -050089static ulong dgap_poll_time; /* Time of next poll */
90static uint dgap_poll_stop; /* Used to tell poller to stop */
Scott_Kilau@digi.comc84b8b52013-08-21 21:48:31 -040091static struct timer_list dgap_poll_timer;
92
Mark Hounschellb28ec882014-02-20 08:48:48 -050093/*
94 SUPPORTED PRODUCTS
95
96 Card Model Number of Ports Interface
97 ----------------------------------------------------------------
98 Acceleport Xem 4 - 64 (EIA232 & EIA422)
99 Acceleport Xr 4 & 8 (EIA232)
100 Acceleport Xr 920 4 & 8 (EIA232)
101 Acceleport C/X 8 - 128 (EIA232)
102 Acceleport EPC/X 8 - 224 (EIA232)
103 Acceleport Xr/422 4 & 8 (EIA422)
104 Acceleport 2r/920 2 (EIA232)
105 Acceleport 4r/920 4 (EIA232)
106 Acceleport 8r/920 8 (EIA232)
107
108 IBM 8-Port Asynchronous PCI Adapter (EIA232)
109 IBM 128-Port Asynchronous PCI Adapter (EIA232 & EIA422)
110*/
Scott_Kilau@digi.comc84b8b52013-08-21 21:48:31 -0400111
112static struct pci_device_id dgap_pci_tbl[] = {
Mark Hounschell2f60b332014-03-04 16:03:12 -0500113 { DIGI_VID, PCI_DEV_XEM_DID, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
114 { DIGI_VID, PCI_DEV_CX_DID, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 1 },
115 { DIGI_VID, PCI_DEV_CX_IBM_DID, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 2 },
116 { DIGI_VID, PCI_DEV_EPCJ_DID, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 3 },
117 { DIGI_VID, PCI_DEV_920_2_DID, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 4 },
118 { DIGI_VID, PCI_DEV_920_4_DID, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 5 },
119 { DIGI_VID, PCI_DEV_920_8_DID, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 6 },
120 { DIGI_VID, PCI_DEV_XR_DID, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 7 },
121 { DIGI_VID, PCI_DEV_XRJ_DID, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 8 },
122 { DIGI_VID, PCI_DEV_XR_422_DID, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 9 },
123 { DIGI_VID, PCI_DEV_XR_IBM_DID, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 10 },
124 { DIGI_VID, PCI_DEV_XR_SAIP_DID, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 11 },
125 { DIGI_VID, PCI_DEV_XR_BULL_DID, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 12 },
126 { DIGI_VID, PCI_DEV_920_8_HP_DID, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 13 },
127 { DIGI_VID, PCI_DEV_XEM_HP_DID, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 14 },
Scott_Kilau@digi.comc84b8b52013-08-21 21:48:31 -0400128 {0,} /* 0 terminated list. */
129};
130MODULE_DEVICE_TABLE(pci, dgap_pci_tbl);
131
Scott_Kilau@digi.comc84b8b52013-08-21 21:48:31 -0400132/*
133 * A generic list of Product names, PCI Vendor ID, and PCI Device ID.
134 */
135struct board_id {
136 uint config_type;
Mark Hounschell2023d182014-04-14 16:42:43 -0400137 u8 *name;
Scott_Kilau@digi.comc84b8b52013-08-21 21:48:31 -0400138 uint maxports;
139 uint dpatype;
140};
141
Mark Hounschellfea06832014-04-25 16:49:29 -0400142static struct board_id dgap_ids[] = {
Ioana Ciorneia9108b72015-10-21 23:17:56 +0300143 {PPCM, PCI_DEV_XEM_NAME, 64, (T_PCXM | T_PCLITE | T_PCIBUS)},
144 {PCX, PCI_DEV_CX_NAME, 128, (T_CX | T_PCIBUS) },
145 {PCX, PCI_DEV_CX_IBM_NAME, 128, (T_CX | T_PCIBUS) },
146 {PEPC, PCI_DEV_EPCJ_NAME, 224, (T_EPC | T_PCIBUS) },
147 {APORT2_920P, PCI_DEV_920_2_NAME, 2, (T_PCXR | T_PCLITE | T_PCIBUS)},
148 {APORT4_920P, PCI_DEV_920_4_NAME, 4, (T_PCXR | T_PCLITE | T_PCIBUS)},
149 {APORT8_920P, PCI_DEV_920_8_NAME, 8, (T_PCXR | T_PCLITE | T_PCIBUS)},
150 {PAPORT8, PCI_DEV_XR_NAME, 8, (T_PCXR | T_PCLITE | T_PCIBUS)},
151 {PAPORT8, PCI_DEV_XRJ_NAME, 8, (T_PCXR | T_PCLITE | T_PCIBUS)},
152 {PAPORT8, PCI_DEV_XR_422_NAME, 8, (T_PCXR | T_PCLITE | T_PCIBUS)},
153 {PAPORT8, PCI_DEV_XR_IBM_NAME, 8, (T_PCXR | T_PCLITE | T_PCIBUS)},
154 {PAPORT8, PCI_DEV_XR_SAIP_NAME, 8, (T_PCXR | T_PCLITE | T_PCIBUS)},
155 {PAPORT8, PCI_DEV_XR_BULL_NAME, 8, (T_PCXR | T_PCLITE | T_PCIBUS)},
156 {APORT8_920P, PCI_DEV_920_8_HP_NAME, 8, (T_PCXR | T_PCLITE | T_PCIBUS)},
157 {PPCM, PCI_DEV_XEM_HP_NAME, 64, (T_PCXM | T_PCLITE | T_PCIBUS)},
Scott_Kilau@digi.comc84b8b52013-08-21 21:48:31 -0400158 {0,} /* 0 terminated list. */
159};
160
Mark Hounschellb28ec882014-02-20 08:48:48 -0500161struct firmware_info {
Mark Hounschell174efc12014-05-23 12:54:04 -0400162 u8 *conf_name; /* dgap.conf */
Mark Hounschell2023d182014-04-14 16:42:43 -0400163 u8 *bios_name; /* BIOS filename */
164 u8 *fep_name; /* FEP filename */
165 u8 *con_name; /* Concentrator filename FIXME*/
Mark Hounschell174efc12014-05-23 12:54:04 -0400166 int num; /* sequence number */
Mark Hounschellb28ec882014-02-20 08:48:48 -0500167};
168
169/*
170 * Firmware - BIOS, FEP, and CONC filenames
171 */
172static struct firmware_info fw_info[] = {
Mark Hounschell8f5879c2014-04-24 10:41:44 -0400173 { "dgap/dgap.conf", "dgap/sxbios.bin", "dgap/sxfep.bin", NULL, 0 },
174 { "dgap/dgap.conf", "dgap/cxpbios.bin", "dgap/cxpfep.bin", NULL, 1 },
175 { "dgap/dgap.conf", "dgap/cxpbios.bin", "dgap/cxpfep.bin", NULL, 2 },
176 { "dgap/dgap.conf", "dgap/pcibios.bin", "dgap/pcifep.bin", NULL, 3 },
177 { "dgap/dgap.conf", "dgap/xrbios.bin", "dgap/xrfep.bin", NULL, 4 },
178 { "dgap/dgap.conf", "dgap/xrbios.bin", "dgap/xrfep.bin", NULL, 5 },
179 { "dgap/dgap.conf", "dgap/xrbios.bin", "dgap/xrfep.bin", NULL, 6 },
180 { "dgap/dgap.conf", "dgap/xrbios.bin", "dgap/xrfep.bin", NULL, 7 },
181 { "dgap/dgap.conf", "dgap/xrbios.bin", "dgap/xrfep.bin", NULL, 8 },
182 { "dgap/dgap.conf", "dgap/xrbios.bin", "dgap/xrfep.bin", NULL, 9 },
183 { "dgap/dgap.conf", "dgap/xrbios.bin", "dgap/xrfep.bin", NULL, 10 },
184 { "dgap/dgap.conf", "dgap/xrbios.bin", "dgap/xrfep.bin", NULL, 11 },
185 { "dgap/dgap.conf", "dgap/xrbios.bin", "dgap/xrfep.bin", NULL, 12 },
186 { "dgap/dgap.conf", "dgap/xrbios.bin", "dgap/xrfep.bin", NULL, 13 },
187 { "dgap/dgap.conf", "dgap/sxbios.bin", "dgap/sxfep.bin", NULL, 14 },
188 {NULL,}
Mark Hounschellb28ec882014-02-20 08:48:48 -0500189};
Scott_Kilau@digi.comc84b8b52013-08-21 21:48:31 -0400190
Mark Hounschella6792a32014-02-19 13:11:59 -0500191/*
192 * Default transparent print information.
193 */
194static struct digi_t dgap_digi_init = {
195 .digi_flags = DIGI_COOK, /* Flags */
196 .digi_maxcps = 100, /* Max CPS */
197 .digi_maxchar = 50, /* Max chars in print queue */
198 .digi_bufsize = 100, /* Printer buffer size */
199 .digi_onlen = 4, /* size of printer on string */
200 .digi_offlen = 4, /* size of printer off string */
201 .digi_onstr = "\033[5i", /* ANSI printer on string ] */
202 .digi_offstr = "\033[4i", /* ANSI printer off string ] */
203 .digi_term = "ansi" /* default terminal type */
204};
205
Mark Hounschella6792a32014-02-19 13:11:59 -0500206/*
207 * Define a local default termios struct. All ports will be created
208 * with this termios initially.
209 *
210 * This defines a raw port at 9600 baud, 8 data bits, no parity,
211 * 1 stop bit.
212 */
213
Mark Hounschellfea06832014-04-25 16:49:29 -0400214static struct ktermios dgap_default_termios = {
Mark Hounschella6792a32014-02-19 13:11:59 -0500215 .c_iflag = (DEFAULT_IFLAGS), /* iflags */
216 .c_oflag = (DEFAULT_OFLAGS), /* oflags */
217 .c_cflag = (DEFAULT_CFLAGS), /* cflags */
218 .c_lflag = (DEFAULT_LFLAGS), /* lflags */
219 .c_cc = INIT_C_CC,
Mark Hounschell7d6069d72014-02-28 12:42:10 -0500220 .c_line = 0,
Mark Hounschella6792a32014-02-19 13:11:59 -0500221};
222
Mark Hounschell69edaa22014-02-19 13:12:02 -0500223/*
224 * Our needed internal static variables from dgap_parse.c
225 */
226static struct cnode dgap_head;
227#define MAXCWORD 200
228static char dgap_cword[MAXCWORD];
229
230struct toklist {
Mark Hounschell174efc12014-05-23 12:54:04 -0400231 int token;
232 char *string;
Mark Hounschell69edaa22014-02-19 13:12:02 -0500233};
234
Daeseok Youn77a44922014-08-09 14:38:14 +0900235static struct toklist dgap_brdtype[] = {
Mark Hounschell2f60b332014-03-04 16:03:12 -0500236 { PCX, "Digi_AccelePort_C/X_PCI" },
237 { PEPC, "Digi_AccelePort_EPC/X_PCI" },
238 { PPCM, "Digi_AccelePort_Xem_PCI" },
239 { APORT2_920P, "Digi_AccelePort_2r_920_PCI" },
240 { APORT4_920P, "Digi_AccelePort_4r_920_PCI" },
241 { APORT8_920P, "Digi_AccelePort_8r_920_PCI" },
242 { PAPORT4, "Digi_AccelePort_4r_PCI(EIA-232/RS-422)" },
243 { PAPORT8, "Digi_AccelePort_8r_PCI(EIA-232/RS-422)" },
Daeseok Youn77a44922014-08-09 14:38:14 +0900244 { 0, NULL }
245};
246
247static struct toklist dgap_tlist[] = {
248 { BEGIN, "config_begin" },
249 { END, "config_end" },
250 { BOARD, "board" },
Mark Hounschell2f60b332014-03-04 16:03:12 -0500251 { PCIINFO, "pciinfo" },
252 { LINE, "line" },
253 { CONC, "conc" },
254 { CONC, "concentrator" },
255 { CX, "cx" },
256 { CX, "ccon" },
257 { EPC, "epccon" },
258 { EPC, "epc" },
259 { MOD, "module" },
260 { ID, "id" },
261 { STARTO, "start" },
262 { SPEED, "speed" },
263 { CABLE, "cable" },
264 { CONNECT, "connect" },
265 { METHOD, "method" },
266 { STATUS, "status" },
267 { CUSTOM, "Custom" },
268 { BASIC, "Basic" },
269 { MEM, "mem" },
270 { MEM, "memory" },
271 { PORTS, "ports" },
272 { MODEM, "modem" },
273 { NPORTS, "nports" },
274 { TTYN, "ttyname" },
275 { CU, "cuname" },
276 { PRINT, "prname" },
277 { CMAJOR, "major" },
278 { ALTPIN, "altpin" },
279 { USEINTR, "useintr" },
280 { TTSIZ, "ttysize" },
281 { CHSIZ, "chsize" },
282 { BSSIZ, "boardsize" },
283 { UNTSIZ, "schedsize" },
284 { F2SIZ, "f2200size" },
285 { VPSIZ, "vpixsize" },
286 { 0, NULL }
Mark Hounschell69edaa22014-02-19 13:12:02 -0500287};
Scott_Kilau@digi.comc84b8b52013-08-21 21:48:31 -0400288
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +0900289/*
290 * get a word from the input stream, also keep track of current line number.
291 * words are separated by whitespace.
292 */
293static char *dgap_getword(char **in)
294{
295 char *ret_ptr = *in;
Scott_Kilau@digi.comc84b8b52013-08-21 21:48:31 -0400296
Alexander Kuleshovb9f7f1d2015-09-12 00:22:15 +0600297 char *ptr = strpbrk(*in, " \t\n");
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +0900298
299 /* If no word found, return null */
300 if (!ptr)
301 return NULL;
302
303 /* Mark new location for our buffer */
304 *ptr = '\0';
305 *in = ptr + 1;
306
307 /* Eat any extra spaces/tabs/newlines that might be present */
308 while (*in && **in && ((**in == ' ') ||
309 (**in == '\t') ||
310 (**in == '\n'))) {
311 **in = '\0';
312 *in = *in + 1;
313 }
314
315 return ret_ptr;
316}
317
318
319/*
320 * Get a token from the input file; return 0 if end of file is reached
321 */
322static int dgap_gettok(char **in)
323{
324 char *w;
325 struct toklist *t;
326
327 if (strstr(dgap_cword, "board")) {
328 w = dgap_getword(in);
Sudip Mukherjee39be6b82015-09-10 17:24:58 +0530329 if (!w)
330 return 0;
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +0900331 snprintf(dgap_cword, MAXCWORD, "%s", w);
332 for (t = dgap_brdtype; t->token != 0; t++) {
333 if (!strcmp(w, t->string))
334 return t->token;
335 }
336 } else {
337 while ((w = dgap_getword(in))) {
338 snprintf(dgap_cword, MAXCWORD, "%s", w);
339 for (t = dgap_tlist; t->token != 0; t++) {
340 if (!strcmp(w, t->string))
341 return t->token;
342 }
343 }
344 }
Mark Hounschell002de0b2014-03-06 15:25:19 -0500345
346 return 0;
Scott_Kilau@digi.comc84b8b52013-08-21 21:48:31 -0400347}
348
Scott_Kilau@digi.comc84b8b52013-08-21 21:48:31 -0400349/*
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +0900350 * dgap_checknode: see if all the necessary info has been supplied for a node
351 * before creating the next node.
Scott_Kilau@digi.comc84b8b52013-08-21 21:48:31 -0400352 */
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +0900353static int dgap_checknode(struct cnode *p)
Scott_Kilau@digi.comc84b8b52013-08-21 21:48:31 -0400354{
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +0900355 switch (p->type) {
356 case LNODE:
357 if (p->u.line.v_speed == 0) {
358 pr_err("line speed not specified");
359 return 1;
360 }
361 return 0;
Scott_Kilau@digi.comc84b8b52013-08-21 21:48:31 -0400362
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +0900363 case CNODE:
364 if (p->u.conc.v_speed == 0) {
365 pr_err("concentrator line speed not specified");
366 return 1;
367 }
368 if (p->u.conc.v_nport == 0) {
369 pr_err("number of ports on concentrator not specified");
370 return 1;
371 }
372 if (p->u.conc.v_id == 0) {
373 pr_err("concentrator id letter not specified");
374 return 1;
375 }
376 return 0;
Scott_Kilau@digi.comc84b8b52013-08-21 21:48:31 -0400377
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +0900378 case MNODE:
379 if (p->u.module.v_nport == 0) {
380 pr_err("number of ports on EBI module not specified");
381 return 1;
382 }
383 if (p->u.module.v_id == 0) {
384 pr_err("EBI module id letter not specified");
385 return 1;
386 }
387 return 0;
Alexey Khoroshilov0669e5f2014-03-09 01:01:34 +0400388 }
Daeseok Youn3c3befe2014-06-13 18:22:40 +0900389 return 0;
Scott_Kilau@digi.comc84b8b52013-08-21 21:48:31 -0400390}
391
Scott_Kilau@digi.comc84b8b52013-08-21 21:48:31 -0400392/*
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +0900393 * Given a board pointer, returns whether we should use interrupts or not.
Scott_Kilau@digi.comc84b8b52013-08-21 21:48:31 -0400394 */
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +0900395static uint dgap_config_get_useintr(struct board_t *bd)
Scott_Kilau@digi.comc84b8b52013-08-21 21:48:31 -0400396{
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +0900397 struct cnode *p;
Scott_Kilau@digi.comc84b8b52013-08-21 21:48:31 -0400398
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +0900399 if (!bd)
400 return 0;
Scott_Kilau@digi.comc84b8b52013-08-21 21:48:31 -0400401
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +0900402 for (p = bd->bd_config; p; p = p->next) {
403 if (p->type == INTRNODE) {
404 /*
405 * check for pcxr types.
406 */
407 return p->u.useintr;
408 }
Scott_Kilau@digi.comc84b8b52013-08-21 21:48:31 -0400409 }
410
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +0900411 /* If not found, then don't turn on interrupts. */
412 return 0;
Scott_Kilau@digi.comc84b8b52013-08-21 21:48:31 -0400413}
414
Scott_Kilau@digi.comc84b8b52013-08-21 21:48:31 -0400415/*
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +0900416 * Given a board pointer, returns whether we turn on altpin or not.
Scott_Kilau@digi.comc84b8b52013-08-21 21:48:31 -0400417 */
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +0900418static uint dgap_config_get_altpin(struct board_t *bd)
Scott_Kilau@digi.comc84b8b52013-08-21 21:48:31 -0400419{
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +0900420 struct cnode *p;
Scott_Kilau@digi.comc84b8b52013-08-21 21:48:31 -0400421
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +0900422 if (!bd)
423 return 0;
Scott_Kilau@digi.comc84b8b52013-08-21 21:48:31 -0400424
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +0900425 for (p = bd->bd_config; p; p = p->next) {
426 if (p->type == ANODE) {
427 /*
428 * check for pcxr types.
429 */
430 return p->u.altpin;
431 }
432 }
Scott_Kilau@digi.comc84b8b52013-08-21 21:48:31 -0400433
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +0900434 /* If not found, then don't turn on interrupts. */
435 return 0;
Scott_Kilau@digi.comc84b8b52013-08-21 21:48:31 -0400436}
437
Scott_Kilau@digi.comc84b8b52013-08-21 21:48:31 -0400438/*
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +0900439 * Given a specific type of board, if found, detached link and
440 * returns the first occurrence in the list.
Scott_Kilau@digi.comc84b8b52013-08-21 21:48:31 -0400441 */
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +0900442static struct cnode *dgap_find_config(int type, int bus, int slot)
Scott_Kilau@digi.comc84b8b52013-08-21 21:48:31 -0400443{
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +0900444 struct cnode *p, *prev, *prev2, *found;
Scott_Kilau@digi.comc84b8b52013-08-21 21:48:31 -0400445
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +0900446 p = &dgap_head;
Mark Hounschell9b073ac2014-03-03 13:45:42 -0500447
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +0900448 while (p->next) {
449 prev = p;
450 p = p->next;
Scott_Kilau@digi.comc84b8b52013-08-21 21:48:31 -0400451
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +0900452 if (p->type != BNODE)
453 continue;
Scott_Kilau@digi.comc84b8b52013-08-21 21:48:31 -0400454
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +0900455 if (p->u.board.type != type)
456 continue;
Scott_Kilau@digi.comc84b8b52013-08-21 21:48:31 -0400457
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +0900458 if (p->u.board.v_pcibus &&
459 p->u.board.pcibus != bus)
460 continue;
Scott_Kilau@digi.comc84b8b52013-08-21 21:48:31 -0400461
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +0900462 if (p->u.board.v_pcislot &&
463 p->u.board.pcislot != slot)
464 continue;
Scott_Kilau@digi.comc84b8b52013-08-21 21:48:31 -0400465
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +0900466 found = p;
Scott_Kilau@digi.comc84b8b52013-08-21 21:48:31 -0400467 /*
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +0900468 * Keep walking thru the list till we
469 * find the next board.
Scott_Kilau@digi.comc84b8b52013-08-21 21:48:31 -0400470 */
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +0900471 while (p->next) {
472 prev2 = p;
473 p = p->next;
Scott_Kilau@digi.comc84b8b52013-08-21 21:48:31 -0400474
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +0900475 if (p->type != BNODE)
476 continue;
Scott_Kilau@digi.comc84b8b52013-08-21 21:48:31 -0400477
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +0900478 /*
479 * Mark the end of our 1 board
480 * chain of configs.
481 */
482 prev2->next = NULL;
Scott_Kilau@digi.comc84b8b52013-08-21 21:48:31 -0400483
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +0900484 /*
485 * Link the "next" board to the
486 * previous board, effectively
487 * "unlinking" our board from
488 * the main config.
489 */
490 prev->next = p;
Mark Hounschellc0c31b92014-03-12 12:50:56 -0400491
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +0900492 return found;
Mark Hounschellb28ec882014-02-20 08:48:48 -0500493 }
Mark Hounschell23aa2ad2014-04-23 10:33:45 -0400494 /*
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +0900495 * It must be the last board in the list.
Mark Hounschell23aa2ad2014-04-23 10:33:45 -0400496 */
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +0900497 prev->next = NULL;
498 return found;
Mark Hounschellb28ec882014-02-20 08:48:48 -0500499 }
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +0900500 return NULL;
501}
502
503/*
504 * Given a board pointer, walks the config link, counting up
505 * all ports user specified should be on the board.
506 * (This does NOT mean they are all actually present right now tho)
507 */
508static uint dgap_config_get_num_prts(struct board_t *bd)
509{
510 int count = 0;
511 struct cnode *p;
512
513 if (!bd)
514 return 0;
515
516 for (p = bd->bd_config; p; p = p->next) {
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +0900517 switch (p->type) {
518 case BNODE:
519 /*
520 * check for pcxr types.
521 */
522 if (p->u.board.type > EPCFE)
523 count += p->u.board.nport;
524 break;
525 case CNODE:
526 count += p->u.conc.nport;
527 break;
528 case MNODE:
529 count += p->u.module.nport;
530 break;
531 }
532 }
533 return count;
534}
535
536static char *dgap_create_config_string(struct board_t *bd, char *string)
537{
538 char *ptr = string;
539 struct cnode *p;
540 struct cnode *q;
541 int speed;
542
543 if (!bd) {
544 *ptr = 0xff;
545 return string;
546 }
547
548 for (p = bd->bd_config; p; p = p->next) {
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +0900549 switch (p->type) {
550 case LNODE:
551 *ptr = '\0';
552 ptr++;
553 *ptr = p->u.line.speed;
554 ptr++;
555 break;
556 case CNODE:
557 /*
558 * Because the EPC/con concentrators can have EM modules
559 * hanging off of them, we have to walk ahead in the
560 * list and keep adding the number of ports on each EM
561 * to the config. UGH!
562 */
563 speed = p->u.conc.speed;
564 q = p->next;
565 if (q && (q->type == MNODE)) {
566 *ptr = (p->u.conc.nport + 0x80);
567 ptr++;
568 p = q;
569 while (q->next && (q->next->type) == MNODE) {
570 *ptr = (q->u.module.nport + 0x80);
571 ptr++;
572 p = q;
573 q = q->next;
574 }
575 *ptr = q->u.module.nport;
576 ptr++;
577 } else {
578 *ptr = p->u.conc.nport;
579 ptr++;
580 }
581
582 *ptr = speed;
583 ptr++;
584 break;
585 }
586 }
587
588 *ptr = 0xff;
589 return string;
590}
591
592/*
593 * Parse a configuration file read into memory as a string.
594 */
595static int dgap_parsefile(char **in)
596{
597 struct cnode *p, *brd, *line, *conc;
598 int rc;
599 char *s;
600 int linecnt = 0;
601
602 p = &dgap_head;
603 brd = line = conc = NULL;
604
605 /* perhaps we are adding to an existing list? */
606 while (p->next)
607 p = p->next;
608
609 /* file must start with a BEGIN */
610 while ((rc = dgap_gettok(in)) != BEGIN) {
611 if (rc == 0) {
612 pr_err("unexpected EOF");
613 return -1;
614 }
615 }
616
617 for (; ;) {
618 int board_type = 0;
619 int conc_type = 0;
620 int module_type = 0;
621
622 rc = dgap_gettok(in);
623 if (rc == 0) {
624 pr_err("unexpected EOF");
625 return -1;
626 }
627
628 switch (rc) {
629 case BEGIN: /* should only be 1 begin */
630 pr_err("unexpected config_begin\n");
631 return -1;
632
633 case END:
634 return 0;
635
636 case BOARD: /* board info */
637 if (dgap_checknode(p))
638 return -1;
639
640 p->next = kzalloc(sizeof(struct cnode), GFP_KERNEL);
641 if (!p->next)
Javier Martinez Canillase98631d2015-09-22 02:39:36 +0200642 return -ENOMEM;
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +0900643
644 p = p->next;
645
646 p->type = BNODE;
647 p->u.board.status = kstrdup("No", GFP_KERNEL);
648 line = conc = NULL;
649 brd = p;
650 linecnt = -1;
651
652 board_type = dgap_gettok(in);
653 if (board_type == 0) {
654 pr_err("board !!type not specified");
655 return -1;
656 }
657
658 p->u.board.type = board_type;
659
660 break;
661
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +0900662 case MEM: /* memory address */
663 if (p->type != BNODE) {
664 pr_err("memory address only valid for boards");
665 return -1;
666 }
667 s = dgap_getword(in);
668 if (!s) {
669 pr_err("unexpected end of file");
670 return -1;
671 }
Ronit Halder5b624082015-09-30 11:39:45 +0530672 kfree(p->u.board.addrstr);
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +0900673 p->u.board.addrstr = kstrdup(s, GFP_KERNEL);
674 if (kstrtoul(s, 0, &p->u.board.addr)) {
675 pr_err("bad number for memory address");
676 return -1;
677 }
678 p->u.board.v_addr = 1;
679 break;
680
681 case PCIINFO: /* pci information */
682 if (p->type != BNODE) {
683 pr_err("memory address only valid for boards");
684 return -1;
685 }
686 s = dgap_getword(in);
687 if (!s) {
688 pr_err("unexpected end of file");
689 return -1;
690 }
Ronit Halder5b624082015-09-30 11:39:45 +0530691 kfree(p->u.board.pcibusstr);
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +0900692 p->u.board.pcibusstr = kstrdup(s, GFP_KERNEL);
693 if (kstrtoul(s, 0, &p->u.board.pcibus)) {
694 pr_err("bad number for pci bus");
695 return -1;
696 }
697 p->u.board.v_pcibus = 1;
698 s = dgap_getword(in);
699 if (!s) {
700 pr_err("unexpected end of file");
701 return -1;
702 }
Ronit Halder5b624082015-09-30 11:39:45 +0530703 kfree(p->u.board.pcislotstr);
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +0900704 p->u.board.pcislotstr = kstrdup(s, GFP_KERNEL);
705 if (kstrtoul(s, 0, &p->u.board.pcislot)) {
706 pr_err("bad number for pci slot");
707 return -1;
708 }
709 p->u.board.v_pcislot = 1;
710 break;
711
712 case METHOD:
713 if (p->type != BNODE) {
714 pr_err("install method only valid for boards");
715 return -1;
716 }
717 s = dgap_getword(in);
718 if (!s) {
719 pr_err("unexpected end of file");
720 return -1;
721 }
Ronit Halder5b624082015-09-30 11:39:45 +0530722 kfree(p->u.board.method);
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +0900723 p->u.board.method = kstrdup(s, GFP_KERNEL);
724 p->u.board.v_method = 1;
725 break;
726
727 case STATUS:
728 if (p->type != BNODE) {
729 pr_err("config status only valid for boards");
730 return -1;
731 }
732 s = dgap_getword(in);
733 if (!s) {
734 pr_err("unexpected end of file");
735 return -1;
736 }
Ronit Halder5b624082015-09-30 11:39:45 +0530737 kfree(p->u.board.status);
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +0900738 p->u.board.status = kstrdup(s, GFP_KERNEL);
739 break;
740
741 case NPORTS: /* number of ports */
742 if (p->type == BNODE) {
743 s = dgap_getword(in);
744 if (!s) {
745 pr_err("unexpected end of file");
746 return -1;
747 }
748 if (kstrtol(s, 0, &p->u.board.nport)) {
749 pr_err("bad number for number of ports");
750 return -1;
751 }
752 p->u.board.v_nport = 1;
753 } else if (p->type == CNODE) {
754 s = dgap_getword(in);
755 if (!s) {
756 pr_err("unexpected end of file");
757 return -1;
758 }
759 if (kstrtol(s, 0, &p->u.conc.nport)) {
760 pr_err("bad number for number of ports");
761 return -1;
762 }
763 p->u.conc.v_nport = 1;
764 } else if (p->type == MNODE) {
765 s = dgap_getword(in);
766 if (!s) {
767 pr_err("unexpected end of file");
768 return -1;
769 }
770 if (kstrtol(s, 0, &p->u.module.nport)) {
771 pr_err("bad number for number of ports");
772 return -1;
773 }
774 p->u.module.v_nport = 1;
775 } else {
776 pr_err("nports only valid for concentrators or modules");
777 return -1;
778 }
779 break;
780
781 case ID: /* letter ID used in tty name */
782 s = dgap_getword(in);
783 if (!s) {
784 pr_err("unexpected end of file");
785 return -1;
786 }
Ronit Halder5b624082015-09-30 11:39:45 +0530787 kfree(p->u.board.status);
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +0900788 p->u.board.status = kstrdup(s, GFP_KERNEL);
789
790 if (p->type == CNODE) {
Ronit Halder5b624082015-09-30 11:39:45 +0530791 kfree(p->u.conc.id);
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +0900792 p->u.conc.id = kstrdup(s, GFP_KERNEL);
793 p->u.conc.v_id = 1;
794 } else if (p->type == MNODE) {
Ronit Halder5b624082015-09-30 11:39:45 +0530795 kfree(p->u.module.id);
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +0900796 p->u.module.id = kstrdup(s, GFP_KERNEL);
797 p->u.module.v_id = 1;
798 } else {
799 pr_err("id only valid for concentrators or modules");
800 return -1;
801 }
802 break;
803
804 case STARTO: /* start offset of ID */
805 if (p->type == BNODE) {
806 s = dgap_getword(in);
807 if (!s) {
808 pr_err("unexpected end of file");
809 return -1;
810 }
811 if (kstrtol(s, 0, &p->u.board.start)) {
812 pr_err("bad number for start of tty count");
813 return -1;
814 }
815 p->u.board.v_start = 1;
816 } else if (p->type == CNODE) {
817 s = dgap_getword(in);
818 if (!s) {
819 pr_err("unexpected end of file");
820 return -1;
821 }
822 if (kstrtol(s, 0, &p->u.conc.start)) {
823 pr_err("bad number for start of tty count");
824 return -1;
825 }
826 p->u.conc.v_start = 1;
827 } else if (p->type == MNODE) {
828 s = dgap_getword(in);
829 if (!s) {
830 pr_err("unexpected end of file");
831 return -1;
832 }
833 if (kstrtol(s, 0, &p->u.module.start)) {
834 pr_err("bad number for start of tty count");
835 return -1;
836 }
837 p->u.module.v_start = 1;
838 } else {
839 pr_err("start only valid for concentrators or modules");
840 return -1;
841 }
842 break;
843
844 case TTYN: /* tty name prefix */
845 if (dgap_checknode(p))
846 return -1;
847
848 p->next = kzalloc(sizeof(struct cnode), GFP_KERNEL);
849 if (!p->next)
Javier Martinez Canillase98631d2015-09-22 02:39:36 +0200850 return -ENOMEM;
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +0900851
852 p = p->next;
853 p->type = TNODE;
854
855 s = dgap_getword(in);
856 if (!s) {
857 pr_err("unexpeced end of file");
858 return -1;
859 }
860 p->u.ttyname = kstrdup(s, GFP_KERNEL);
861 if (!p->u.ttyname)
862 return -1;
863
864 break;
865
866 case CU: /* cu name prefix */
867 if (dgap_checknode(p))
868 return -1;
869
870 p->next = kzalloc(sizeof(struct cnode), GFP_KERNEL);
871 if (!p->next)
Javier Martinez Canillase98631d2015-09-22 02:39:36 +0200872 return -ENOMEM;
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +0900873
874 p = p->next;
875 p->type = CUNODE;
876
877 s = dgap_getword(in);
878 if (!s) {
879 pr_err("unexpeced end of file");
880 return -1;
881 }
882 p->u.cuname = kstrdup(s, GFP_KERNEL);
883 if (!p->u.cuname)
884 return -1;
885
886 break;
887
888 case LINE: /* line information */
889 if (dgap_checknode(p))
890 return -1;
891 if (!brd) {
892 pr_err("must specify board before line info");
893 return -1;
894 }
895 switch (brd->u.board.type) {
896 case PPCM:
897 pr_err("line not valid for PC/em");
898 return -1;
899 }
900
901 p->next = kzalloc(sizeof(struct cnode), GFP_KERNEL);
902 if (!p->next)
Javier Martinez Canillase98631d2015-09-22 02:39:36 +0200903 return -ENOMEM;
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +0900904
905 p = p->next;
906 p->type = LNODE;
907 conc = NULL;
908 line = p;
909 linecnt++;
910 break;
911
912 case CONC: /* concentrator information */
913 if (dgap_checknode(p))
914 return -1;
915 if (!line) {
916 pr_err("must specify line info before concentrator");
917 return -1;
918 }
919
920 p->next = kzalloc(sizeof(struct cnode), GFP_KERNEL);
921 if (!p->next)
Javier Martinez Canillase98631d2015-09-22 02:39:36 +0200922 return -ENOMEM;
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +0900923
924 p = p->next;
925 p->type = CNODE;
926 conc = p;
927
928 if (linecnt)
929 brd->u.board.conc2++;
930 else
931 brd->u.board.conc1++;
932
933 conc_type = dgap_gettok(in);
Ioana Ciornei629c7342015-10-21 23:17:53 +0300934 if (conc_type == 0 ||
935 (conc_type != CX && conc_type != EPC)) {
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +0900936 pr_err("failed to set a type of concentratros");
937 return -1;
938 }
939
940 p->u.conc.type = conc_type;
941
942 break;
943
944 case MOD: /* EBI module */
945 if (dgap_checknode(p))
946 return -1;
947 if (!brd) {
948 pr_err("must specify board info before EBI modules");
949 return -1;
950 }
951 switch (brd->u.board.type) {
952 case PPCM:
953 linecnt = 0;
954 break;
955 default:
956 if (!conc) {
957 pr_err("must specify concentrator info before EBI module");
958 return -1;
959 }
960 }
961
962 p->next = kzalloc(sizeof(struct cnode), GFP_KERNEL);
963 if (!p->next)
Javier Martinez Canillase98631d2015-09-22 02:39:36 +0200964 return -ENOMEM;
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +0900965
966 p = p->next;
967 p->type = MNODE;
968
969 if (linecnt)
970 brd->u.board.module2++;
971 else
972 brd->u.board.module1++;
973
974 module_type = dgap_gettok(in);
Ioana Ciornei629c7342015-10-21 23:17:53 +0300975 if (module_type == 0 ||
976 (module_type != PORTS && module_type != MODEM)) {
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +0900977 pr_err("failed to set a type of module");
978 return -1;
979 }
980
981 p->u.module.type = module_type;
982
983 break;
984
985 case CABLE:
986 if (p->type == LNODE) {
987 s = dgap_getword(in);
988 if (!s) {
989 pr_err("unexpected end of file");
990 return -1;
991 }
Ronit Halder5b624082015-09-30 11:39:45 +0530992 kfree(p->u.line.cable);
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +0900993 p->u.line.cable = kstrdup(s, GFP_KERNEL);
994 p->u.line.v_cable = 1;
995 }
996 break;
997
998 case SPEED: /* sync line speed indication */
999 if (p->type == LNODE) {
1000 s = dgap_getword(in);
1001 if (!s) {
1002 pr_err("unexpected end of file");
1003 return -1;
1004 }
1005 if (kstrtol(s, 0, &p->u.line.speed)) {
1006 pr_err("bad number for line speed");
1007 return -1;
1008 }
1009 p->u.line.v_speed = 1;
1010 } else if (p->type == CNODE) {
1011 s = dgap_getword(in);
1012 if (!s) {
1013 pr_err("unexpected end of file");
1014 return -1;
1015 }
1016 if (kstrtol(s, 0, &p->u.conc.speed)) {
1017 pr_err("bad number for line speed");
1018 return -1;
1019 }
1020 p->u.conc.v_speed = 1;
1021 } else {
1022 pr_err("speed valid only for lines or concentrators.");
1023 return -1;
1024 }
1025 break;
1026
1027 case CONNECT:
1028 if (p->type == CNODE) {
1029 s = dgap_getword(in);
1030 if (!s) {
1031 pr_err("unexpected end of file");
1032 return -1;
1033 }
Ronit Halder5b624082015-09-30 11:39:45 +05301034 kfree(p->u.conc.connect);
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09001035 p->u.conc.connect = kstrdup(s, GFP_KERNEL);
1036 p->u.conc.v_connect = 1;
1037 }
1038 break;
1039 case PRINT: /* transparent print name prefix */
1040 if (dgap_checknode(p))
1041 return -1;
1042
1043 p->next = kzalloc(sizeof(struct cnode), GFP_KERNEL);
1044 if (!p->next)
Javier Martinez Canillase98631d2015-09-22 02:39:36 +02001045 return -ENOMEM;
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09001046
1047 p = p->next;
1048 p->type = PNODE;
1049
1050 s = dgap_getword(in);
1051 if (!s) {
1052 pr_err("unexpeced end of file");
1053 return -1;
1054 }
1055 p->u.printname = kstrdup(s, GFP_KERNEL);
1056 if (!p->u.printname)
1057 return -1;
1058
1059 break;
1060
1061 case CMAJOR: /* major number */
1062 if (dgap_checknode(p))
1063 return -1;
1064
1065 p->next = kzalloc(sizeof(struct cnode), GFP_KERNEL);
1066 if (!p->next)
Javier Martinez Canillase98631d2015-09-22 02:39:36 +02001067 return -ENOMEM;
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09001068
1069 p = p->next;
1070 p->type = JNODE;
1071
1072 s = dgap_getword(in);
1073 if (!s) {
1074 pr_err("unexpected end of file");
1075 return -1;
1076 }
1077 if (kstrtol(s, 0, &p->u.majornumber)) {
1078 pr_err("bad number for major number");
1079 return -1;
1080 }
1081 break;
1082
1083 case ALTPIN: /* altpin setting */
1084 if (dgap_checknode(p))
1085 return -1;
1086
1087 p->next = kzalloc(sizeof(struct cnode), GFP_KERNEL);
1088 if (!p->next)
Javier Martinez Canillase98631d2015-09-22 02:39:36 +02001089 return -ENOMEM;
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09001090
1091 p = p->next;
1092 p->type = ANODE;
1093
1094 s = dgap_getword(in);
1095 if (!s) {
1096 pr_err("unexpected end of file");
1097 return -1;
1098 }
1099 if (kstrtol(s, 0, &p->u.altpin)) {
1100 pr_err("bad number for altpin");
1101 return -1;
1102 }
1103 break;
1104
1105 case USEINTR: /* enable interrupt setting */
1106 if (dgap_checknode(p))
1107 return -1;
1108
1109 p->next = kzalloc(sizeof(struct cnode), GFP_KERNEL);
1110 if (!p->next)
Javier Martinez Canillase98631d2015-09-22 02:39:36 +02001111 return -ENOMEM;
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09001112
1113 p = p->next;
1114 p->type = INTRNODE;
1115 s = dgap_getword(in);
1116 if (!s) {
1117 pr_err("unexpected end of file");
1118 return -1;
1119 }
1120 if (kstrtol(s, 0, &p->u.useintr)) {
1121 pr_err("bad number for useintr");
1122 return -1;
1123 }
1124 break;
1125
1126 case TTSIZ: /* size of tty structure */
1127 if (dgap_checknode(p))
1128 return -1;
1129
1130 p->next = kzalloc(sizeof(struct cnode), GFP_KERNEL);
1131 if (!p->next)
Javier Martinez Canillase98631d2015-09-22 02:39:36 +02001132 return -ENOMEM;
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09001133
1134 p = p->next;
1135 p->type = TSNODE;
1136
1137 s = dgap_getword(in);
1138 if (!s) {
1139 pr_err("unexpected end of file");
1140 return -1;
1141 }
1142 if (kstrtol(s, 0, &p->u.ttysize)) {
1143 pr_err("bad number for ttysize");
1144 return -1;
1145 }
1146 break;
1147
1148 case CHSIZ: /* channel structure size */
1149 if (dgap_checknode(p))
1150 return -1;
1151
1152 p->next = kzalloc(sizeof(struct cnode), GFP_KERNEL);
1153 if (!p->next)
Javier Martinez Canillase98631d2015-09-22 02:39:36 +02001154 return -ENOMEM;
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09001155
1156 p = p->next;
1157 p->type = CSNODE;
1158
1159 s = dgap_getword(in);
1160 if (!s) {
1161 pr_err("unexpected end of file");
1162 return -1;
1163 }
1164 if (kstrtol(s, 0, &p->u.chsize)) {
1165 pr_err("bad number for chsize");
1166 return -1;
1167 }
1168 break;
1169
1170 case BSSIZ: /* board structure size */
1171 if (dgap_checknode(p))
1172 return -1;
1173
1174 p->next = kzalloc(sizeof(struct cnode), GFP_KERNEL);
1175 if (!p->next)
Javier Martinez Canillase98631d2015-09-22 02:39:36 +02001176 return -ENOMEM;
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09001177
1178 p = p->next;
1179 p->type = BSNODE;
1180
1181 s = dgap_getword(in);
1182 if (!s) {
1183 pr_err("unexpected end of file");
1184 return -1;
1185 }
1186 if (kstrtol(s, 0, &p->u.bssize)) {
1187 pr_err("bad number for bssize");
1188 return -1;
1189 }
1190 break;
1191
1192 case UNTSIZ: /* sched structure size */
1193 if (dgap_checknode(p))
1194 return -1;
1195
1196 p->next = kzalloc(sizeof(struct cnode), GFP_KERNEL);
1197 if (!p->next)
Javier Martinez Canillase98631d2015-09-22 02:39:36 +02001198 return -ENOMEM;
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09001199
1200 p = p->next;
1201 p->type = USNODE;
1202
1203 s = dgap_getword(in);
1204 if (!s) {
1205 pr_err("unexpected end of file");
1206 return -1;
1207 }
1208 if (kstrtol(s, 0, &p->u.unsize)) {
1209 pr_err("bad number for schedsize");
1210 return -1;
1211 }
1212 break;
1213
1214 case F2SIZ: /* f2200 structure size */
1215 if (dgap_checknode(p))
1216 return -1;
1217
1218 p->next = kzalloc(sizeof(struct cnode), GFP_KERNEL);
1219 if (!p->next)
Javier Martinez Canillase98631d2015-09-22 02:39:36 +02001220 return -ENOMEM;
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09001221
1222 p = p->next;
1223 p->type = FSNODE;
1224
1225 s = dgap_getword(in);
1226 if (!s) {
1227 pr_err("unexpected end of file");
1228 return -1;
1229 }
1230 if (kstrtol(s, 0, &p->u.f2size)) {
1231 pr_err("bad number for f2200size");
1232 return -1;
1233 }
1234 break;
1235
1236 case VPSIZ: /* vpix structure size */
1237 if (dgap_checknode(p))
1238 return -1;
1239
1240 p->next = kzalloc(sizeof(struct cnode), GFP_KERNEL);
1241 if (!p->next)
Javier Martinez Canillase98631d2015-09-22 02:39:36 +02001242 return -ENOMEM;
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09001243
1244 p = p->next;
1245 p->type = VSNODE;
1246
1247 s = dgap_getword(in);
1248 if (!s) {
1249 pr_err("unexpected end of file");
1250 return -1;
1251 }
1252 if (kstrtol(s, 0, &p->u.vpixsize)) {
1253 pr_err("bad number for vpixsize");
1254 return -1;
1255 }
1256 break;
1257 }
1258 }
1259}
1260
1261static void dgap_cleanup_nodes(void)
1262{
1263 struct cnode *p;
1264
1265 p = &dgap_head;
1266
1267 while (p) {
1268 struct cnode *tmp = p->next;
1269
1270 if (p->type == NULLNODE) {
1271 p = tmp;
1272 continue;
1273 }
1274
1275 switch (p->type) {
1276 case BNODE:
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09001277 kfree(p->u.board.addrstr);
1278 kfree(p->u.board.pcibusstr);
1279 kfree(p->u.board.pcislotstr);
1280 kfree(p->u.board.method);
1281 break;
1282 case CNODE:
1283 kfree(p->u.conc.id);
1284 kfree(p->u.conc.connect);
1285 break;
1286 case MNODE:
1287 kfree(p->u.module.id);
1288 break;
1289 case TNODE:
1290 kfree(p->u.ttyname);
1291 break;
1292 case CUNODE:
1293 kfree(p->u.cuname);
1294 break;
1295 case LNODE:
1296 kfree(p->u.line.cable);
1297 break;
1298 case PNODE:
1299 kfree(p->u.printname);
1300 break;
1301 }
1302
1303 kfree(p->u.board.status);
1304 kfree(p);
1305 p = tmp;
1306 }
1307}
1308
1309/*
1310 * Retrives the current custom baud rate from FEP memory,
1311 * and returns it back to the user.
1312 * Returns 0 on error.
1313 */
1314static uint dgap_get_custom_baud(struct channel_t *ch)
1315{
1316 u8 __iomem *vaddr;
1317 ulong offset;
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09001318
1319 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
1320 return 0;
1321
1322 if (!ch->ch_bd || ch->ch_bd->magic != DGAP_BOARD_MAGIC)
1323 return 0;
1324
1325 if (!(ch->ch_bd->bd_flags & BD_FEP5PLUS))
1326 return 0;
1327
1328 vaddr = ch->ch_bd->re_map_membase;
1329
1330 if (!vaddr)
1331 return 0;
Mark Hounschellb28ec882014-02-20 08:48:48 -05001332
Mark Hounschellb28ec882014-02-20 08:48:48 -05001333 /*
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09001334 * Go get from fep mem, what the fep
1335 * believes the custom baud rate is.
Mark Hounschellb28ec882014-02-20 08:48:48 -05001336 */
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09001337 offset = (ioread16(vaddr + ECS_SEG) << 4) + (ch->ch_portnum * 0x28)
1338 + LINE_SPEED;
Mark Hounschellb28ec882014-02-20 08:48:48 -05001339
Haneen Mohammed6019ee42015-03-08 00:02:34 +03001340 return readw(vaddr + offset);
Mark Hounschellb28ec882014-02-20 08:48:48 -05001341}
Scott_Kilau@digi.comc84b8b52013-08-21 21:48:31 -04001342
1343/*
1344 * Remap PCI memory.
1345 */
Daeseok Youn263bd1c2014-10-08 20:13:21 +09001346static int dgap_remap(struct board_t *brd)
Scott_Kilau@digi.comc84b8b52013-08-21 21:48:31 -04001347{
1348 if (!brd || brd->magic != DGAP_BOARD_MAGIC)
Mark Hounschell6d488a02014-05-28 16:18:03 -04001349 return -EIO;
Scott_Kilau@digi.comc84b8b52013-08-21 21:48:31 -04001350
Mark Hounschell31960c12014-02-28 12:42:08 -05001351 if (!request_mem_region(brd->membase, 0x200000, "dgap"))
Scott_Kilau@digi.comc84b8b52013-08-21 21:48:31 -04001352 return -ENOMEM;
Scott_Kilau@digi.comc84b8b52013-08-21 21:48:31 -04001353
Ioana Ciornei629c7342015-10-21 23:17:53 +03001354 if (!request_mem_region(brd->membase + PCI_IO_OFFSET, 0x200000, "dgap"))
Daeseok Younaa9895d2014-12-26 10:34:32 +09001355 goto err_req_mem;
Scott_Kilau@digi.comc84b8b52013-08-21 21:48:31 -04001356
1357 brd->re_map_membase = ioremap(brd->membase, 0x200000);
Daeseok Younaa9895d2014-12-26 10:34:32 +09001358 if (!brd->re_map_membase)
1359 goto err_remap_mem;
Scott_Kilau@digi.comc84b8b52013-08-21 21:48:31 -04001360
1361 brd->re_map_port = ioremap((brd->membase + PCI_IO_OFFSET), 0x200000);
Daeseok Younaa9895d2014-12-26 10:34:32 +09001362 if (!brd->re_map_port)
1363 goto err_remap_port;
Scott_Kilau@digi.comc84b8b52013-08-21 21:48:31 -04001364
Scott_Kilau@digi.comc84b8b52013-08-21 21:48:31 -04001365 return 0;
Daeseok Younaa9895d2014-12-26 10:34:32 +09001366
1367err_remap_port:
1368 iounmap(brd->re_map_membase);
1369err_remap_mem:
1370 release_mem_region(brd->membase + PCI_IO_OFFSET, 0x200000);
1371err_req_mem:
1372 release_mem_region(brd->membase, 0x200000);
1373
1374 return -ENOMEM;
Scott_Kilau@digi.comc84b8b52013-08-21 21:48:31 -04001375}
1376
Daeseok Youn263bd1c2014-10-08 20:13:21 +09001377static void dgap_unmap(struct board_t *brd)
Daeseok Youn91177d52014-06-13 18:23:15 +09001378{
Daeseok Youn9f2b7442014-10-08 20:12:48 +09001379 iounmap(brd->re_map_port);
1380 iounmap(brd->re_map_membase);
1381 release_mem_region(brd->membase + PCI_IO_OFFSET, 0x200000);
1382 release_mem_region(brd->membase, 0x200000);
Daeseok Youn91177d52014-06-13 18:23:15 +09001383}
Scott_Kilau@digi.comc84b8b52013-08-21 21:48:31 -04001384
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09001385/*
1386 * dgap_parity_scan()
1387 *
1388 * Convert the FEP5 way of reporting parity errors and breaks into
1389 * the Linux line discipline way.
1390 */
1391static void dgap_parity_scan(struct channel_t *ch, unsigned char *cbuf,
Ioana Ciornei629c7342015-10-21 23:17:53 +03001392 unsigned char *fbuf, int *len)
Scott_Kilau@digi.comc84b8b52013-08-21 21:48:31 -04001393{
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09001394 int l = *len;
1395 int count = 0;
1396 unsigned char *in, *cout, *fout;
1397 unsigned char c;
Scott_Kilau@digi.comc84b8b52013-08-21 21:48:31 -04001398
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09001399 in = cbuf;
1400 cout = cbuf;
1401 fout = fbuf;
Scott_Kilau@digi.comc84b8b52013-08-21 21:48:31 -04001402
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09001403 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
1404 return;
Scott_Kilau@digi.comc84b8b52013-08-21 21:48:31 -04001405
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09001406 while (l--) {
1407 c = *in++;
1408 switch (ch->pscan_state) {
1409 default:
1410 /* reset to sanity and fall through */
1411 ch->pscan_state = 0;
Scott_Kilau@digi.comc84b8b52013-08-21 21:48:31 -04001412
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09001413 case 0:
1414 /* No FF seen yet */
Ioana Ciornei18122552015-10-21 23:17:54 +03001415 if (c == (unsigned char)'\377')
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09001416 /* delete this character from stream */
1417 ch->pscan_state = 1;
1418 else {
1419 *cout++ = c;
1420 *fout++ = TTY_NORMAL;
1421 count += 1;
1422 }
1423 break;
Scott_Kilau@digi.comc84b8b52013-08-21 21:48:31 -04001424
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09001425 case 1:
1426 /* first FF seen */
Ioana Ciornei18122552015-10-21 23:17:54 +03001427 if (c == (unsigned char)'\377') {
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09001428 /* doubled ff, transform to single ff */
1429 *cout++ = c;
1430 *fout++ = TTY_NORMAL;
1431 count += 1;
1432 ch->pscan_state = 0;
1433 } else {
1434 /* save value examination in next state */
1435 ch->pscan_savechar = c;
1436 ch->pscan_state = 2;
1437 }
1438 break;
Scott_Kilau@digi.comc84b8b52013-08-21 21:48:31 -04001439
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09001440 case 2:
1441 /* third character of ff sequence */
Scott_Kilau@digi.comc84b8b52013-08-21 21:48:31 -04001442
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09001443 *cout++ = c;
1444
1445 if (ch->pscan_savechar == 0x0) {
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09001446 if (c == 0x0) {
1447 ch->ch_err_break++;
1448 *fout++ = TTY_BREAK;
1449 } else {
1450 ch->ch_err_parity++;
1451 *fout++ = TTY_PARITY;
1452 }
Scott_Kilau@digi.comc84b8b52013-08-21 21:48:31 -04001453 }
1454
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09001455 count += 1;
1456 ch->pscan_state = 0;
Scott_Kilau@digi.comc84b8b52013-08-21 21:48:31 -04001457 }
1458 }
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09001459 *len = count;
Mark Hounschella6792a32014-02-19 13:11:59 -05001460}
1461
Mark Hounschella6792a32014-02-19 13:11:59 -05001462/*=======================================================================
1463 *
1464 * dgap_input - Process received data.
1465 *
1466 * ch - Pointer to channel structure.
1467 *
1468 *=======================================================================*/
1469
Mark Hounschellfe0ef8e2014-02-19 13:12:13 -05001470static void dgap_input(struct channel_t *ch)
Mark Hounschella6792a32014-02-19 13:11:59 -05001471{
1472 struct board_t *bd;
Mark Hounschell405b26d2014-04-23 16:25:27 -04001473 struct bs_t __iomem *bs;
Mark Hounschella6792a32014-02-19 13:11:59 -05001474 struct tty_struct *tp;
1475 struct tty_ldisc *ld;
Mark Hounschell174efc12014-05-23 12:54:04 -04001476 uint rmask;
1477 uint head;
1478 uint tail;
1479 int data_len;
1480 ulong lock_flags;
1481 ulong lock_flags2;
Mark Hounschella6792a32014-02-19 13:11:59 -05001482 int flip_len;
Mark Hounschell7dfa3832014-05-23 10:14:02 -04001483 int len;
1484 int n;
Mark Hounschell2023d182014-04-14 16:42:43 -04001485 u8 *buf;
1486 u8 tmpchar;
Mark Hounschell7dfa3832014-05-23 10:14:02 -04001487 int s;
Mark Hounschella6792a32014-02-19 13:11:59 -05001488
1489 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
1490 return;
1491
1492 tp = ch->ch_tun.un_tty;
1493
1494 bs = ch->ch_bs;
Mark Hounschell305ec872014-02-28 12:42:13 -05001495 if (!bs)
Mark Hounschella6792a32014-02-19 13:11:59 -05001496 return;
Mark Hounschella6792a32014-02-19 13:11:59 -05001497
1498 bd = ch->ch_bd;
Mark Hounschellb115b022014-02-28 12:42:15 -05001499 if (!bd || bd->magic != DGAP_BOARD_MAGIC)
Mark Hounschella6792a32014-02-19 13:11:59 -05001500 return;
1501
Mark Hounschellc43846a2014-03-19 11:10:51 -04001502 spin_lock_irqsave(&bd->bd_lock, lock_flags);
1503 spin_lock_irqsave(&ch->ch_lock, lock_flags2);
Mark Hounschella6792a32014-02-19 13:11:59 -05001504
1505 /*
1506 * Figure the number of characters in the buffer.
1507 * Exit immediately if none.
1508 */
1509
1510 rmask = ch->ch_rsize - 1;
1511
Ioana Ciornei44209c92015-10-21 23:17:55 +03001512 head = readw(&bs->rx_head);
Mark Hounschella6792a32014-02-19 13:11:59 -05001513 head &= rmask;
Ioana Ciornei44209c92015-10-21 23:17:55 +03001514 tail = readw(&bs->rx_tail);
Mark Hounschella6792a32014-02-19 13:11:59 -05001515 tail &= rmask;
1516
1517 data_len = (head - tail) & rmask;
1518
1519 if (data_len == 0) {
Ioana Ciornei44209c92015-10-21 23:17:55 +03001520 writeb(1, &bs->idata);
Mark Hounschellc43846a2014-03-19 11:10:51 -04001521 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
1522 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
Mark Hounschella6792a32014-02-19 13:11:59 -05001523 return;
1524 }
1525
1526 /*
1527 * If the device is not open, or CREAD is off, flush
1528 * input data and return immediately.
1529 */
Mark Hounschell7d6069d72014-02-28 12:42:10 -05001530 if ((bd->state != BOARD_READY) || !tp ||
1531 (tp->magic != TTY_MAGIC) ||
1532 !(ch->ch_tun.un_flags & UN_ISOPEN) ||
1533 !(tp->termios.c_cflag & CREAD) ||
Mark Hounschella6792a32014-02-19 13:11:59 -05001534 (ch->ch_tun.un_flags & UN_CLOSING)) {
Ioana Ciornei44209c92015-10-21 23:17:55 +03001535 writew(head, &bs->rx_tail);
1536 writeb(1, &bs->idata);
Mark Hounschellc43846a2014-03-19 11:10:51 -04001537 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
1538 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
Mark Hounschella6792a32014-02-19 13:11:59 -05001539 return;
1540 }
1541
1542 /*
1543 * If we are throttled, simply don't read any data.
1544 */
1545 if (ch->ch_flags & CH_RXBLOCK) {
Ioana Ciornei44209c92015-10-21 23:17:55 +03001546 writeb(1, &bs->idata);
Mark Hounschellc43846a2014-03-19 11:10:51 -04001547 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
1548 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
Mark Hounschella6792a32014-02-19 13:11:59 -05001549 return;
1550 }
1551
1552 /*
1553 * Ignore oruns.
1554 */
Ioana Ciornei44209c92015-10-21 23:17:55 +03001555 tmpchar = readb(&bs->orun);
Mark Hounschella6792a32014-02-19 13:11:59 -05001556 if (tmpchar) {
1557 ch->ch_err_overrun++;
Ioana Ciornei44209c92015-10-21 23:17:55 +03001558 writeb(0, &bs->orun);
Mark Hounschella6792a32014-02-19 13:11:59 -05001559 }
1560
Mark Hounschella6792a32014-02-19 13:11:59 -05001561 /* Decide how much data we can send into the tty layer */
1562 flip_len = TTY_FLIPBUF_SIZE;
1563
1564 /* Chop down the length, if needed */
1565 len = min(data_len, flip_len);
1566 len = min(len, (N_TTY_BUF_SIZE - 1));
1567
1568 ld = tty_ldisc_ref(tp);
1569
1570#ifdef TTY_DONT_FLIP
1571 /*
1572 * If the DONT_FLIP flag is on, don't flush our buffer, and act
1573 * like the ld doesn't have any space to put the data right now.
1574 */
1575 if (test_bit(TTY_DONT_FLIP, &tp->flags))
1576 len = 0;
1577#endif
1578
1579 /*
1580 * If we were unable to get a reference to the ld,
1581 * don't flush our buffer, and act like the ld doesn't
1582 * have any space to put the data right now.
1583 */
1584 if (!ld) {
1585 len = 0;
1586 } else {
1587 /*
1588 * If ld doesn't have a pointer to a receive_buf function,
1589 * flush the data, then act like the ld doesn't have any
1590 * space to put the data right now.
1591 */
1592 if (!ld->ops->receive_buf) {
Ioana Ciornei44209c92015-10-21 23:17:55 +03001593 writew(head, &bs->rx_tail);
Mark Hounschella6792a32014-02-19 13:11:59 -05001594 len = 0;
1595 }
1596 }
1597
1598 if (len <= 0) {
Ioana Ciornei44209c92015-10-21 23:17:55 +03001599 writeb(1, &bs->idata);
Mark Hounschellc43846a2014-03-19 11:10:51 -04001600 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
1601 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
Mark Hounschella6792a32014-02-19 13:11:59 -05001602 if (ld)
1603 tty_ldisc_deref(ld);
1604 return;
1605 }
1606
1607 buf = ch->ch_bd->flipbuf;
1608 n = len;
1609
1610 /*
1611 * n now contains the most amount of data we can copy,
1612 * bounded either by our buffer size or the amount
1613 * of data the card actually has pending...
1614 */
1615 while (n) {
Mark Hounschella6792a32014-02-19 13:11:59 -05001616 s = ((head >= tail) ? head : ch->ch_rsize) - tail;
1617 s = min(s, n);
1618
1619 if (s <= 0)
1620 break;
1621
Mark Hounschell630b2ab2014-04-24 09:22:12 -04001622 memcpy_fromio(buf, ch->ch_raddr + tail, s);
Mark Hounschella6792a32014-02-19 13:11:59 -05001623
1624 tail += s;
1625 buf += s;
1626
1627 n -= s;
1628 /* Flip queue if needed */
1629 tail &= rmask;
1630 }
1631
Ioana Ciornei44209c92015-10-21 23:17:55 +03001632 writew(tail, &bs->rx_tail);
1633 writeb(1, &bs->idata);
Mark Hounschella6792a32014-02-19 13:11:59 -05001634 ch->ch_rxcount += len;
1635
1636 /*
1637 * If we are completely raw, we don't need to go through a lot
1638 * of the tty layers that exist.
1639 * In this case, we take the shortest and fastest route we
1640 * can to relay the data to the user.
1641 *
1642 * On the other hand, if we are not raw, we need to go through
1643 * the tty layer, which has its API more well defined.
1644 */
1645 if (I_PARMRK(tp) || I_BRKINT(tp) || I_INPCK(tp)) {
Mark Hounschell6045b6a2014-03-06 13:02:45 -05001646 dgap_parity_scan(ch, ch->ch_bd->flipbuf,
1647 ch->ch_bd->flipflagbuf, &len);
Mark Hounschella6792a32014-02-19 13:11:59 -05001648
1649 len = tty_buffer_request_room(tp->port, len);
1650 tty_insert_flip_string_flags(tp->port, ch->ch_bd->flipbuf,
Ioana Ciornei629c7342015-10-21 23:17:53 +03001651 ch->ch_bd->flipflagbuf, len);
Mark Hounschell305ec872014-02-28 12:42:13 -05001652 } else {
Mark Hounschella6792a32014-02-19 13:11:59 -05001653 len = tty_buffer_request_room(tp->port, len);
1654 tty_insert_flip_string(tp->port, ch->ch_bd->flipbuf, len);
1655 }
1656
Mark Hounschellc43846a2014-03-19 11:10:51 -04001657 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
1658 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
Mark Hounschella6792a32014-02-19 13:11:59 -05001659
1660 /* Tell the tty layer its okay to "eat" the data now */
1661 tty_flip_buffer_push(tp->port);
1662
1663 if (ld)
1664 tty_ldisc_deref(ld);
Mark Hounschella6792a32014-02-19 13:11:59 -05001665}
1666
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09001667static void dgap_write_wakeup(struct board_t *bd, struct channel_t *ch,
1668 struct un_t *un, u32 mask,
1669 unsigned long *irq_flags1,
1670 unsigned long *irq_flags2)
1671{
1672 if (!(un->un_flags & mask))
1673 return;
1674
1675 un->un_flags &= ~mask;
1676
1677 if (!(un->un_flags & UN_ISOPEN))
1678 return;
1679
1680 if ((un->un_tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
1681 un->un_tty->ldisc->ops->write_wakeup) {
1682 spin_unlock_irqrestore(&ch->ch_lock, *irq_flags2);
1683 spin_unlock_irqrestore(&bd->bd_lock, *irq_flags1);
1684
1685 (un->un_tty->ldisc->ops->write_wakeup)(un->un_tty);
1686
1687 spin_lock_irqsave(&bd->bd_lock, *irq_flags1);
1688 spin_lock_irqsave(&ch->ch_lock, *irq_flags2);
1689 }
1690 wake_up_interruptible(&un->un_tty->write_wait);
1691 wake_up_interruptible(&un->un_flags_wait);
1692}
1693
Mark Hounschella6792a32014-02-19 13:11:59 -05001694/************************************************************************
1695 * Determines when CARRIER changes state and takes appropriate
1696 * action.
1697 ************************************************************************/
Mark Hounschellfe0ef8e2014-02-19 13:12:13 -05001698static void dgap_carrier(struct channel_t *ch)
Mark Hounschella6792a32014-02-19 13:11:59 -05001699{
1700 struct board_t *bd;
1701
Mark Hounschell7d6069d72014-02-28 12:42:10 -05001702 int virt_carrier = 0;
1703 int phys_carrier = 0;
Mark Hounschella6792a32014-02-19 13:11:59 -05001704
Mark Hounschella6792a32014-02-19 13:11:59 -05001705 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
1706 return;
1707
1708 bd = ch->ch_bd;
1709
1710 if (!bd || bd->magic != DGAP_BOARD_MAGIC)
1711 return;
1712
1713 /* Make sure altpin is always set correctly */
1714 if (ch->ch_digi.digi_flags & DIGI_ALTPIN) {
1715 ch->ch_dsr = DM_CD;
1716 ch->ch_cd = DM_DSR;
Mark Hounschell305ec872014-02-28 12:42:13 -05001717 } else {
Mark Hounschella6792a32014-02-19 13:11:59 -05001718 ch->ch_dsr = DM_DSR;
1719 ch->ch_cd = DM_CD;
1720 }
1721
Mark Hounschell31960c12014-02-28 12:42:08 -05001722 if (ch->ch_mistat & D_CD(ch))
Mark Hounschella6792a32014-02-19 13:11:59 -05001723 phys_carrier = 1;
Mark Hounschella6792a32014-02-19 13:11:59 -05001724
Mark Hounschell305ec872014-02-28 12:42:13 -05001725 if (ch->ch_digi.digi_flags & DIGI_FORCEDCD)
Mark Hounschella6792a32014-02-19 13:11:59 -05001726 virt_carrier = 1;
Mark Hounschella6792a32014-02-19 13:11:59 -05001727
Mark Hounschell305ec872014-02-28 12:42:13 -05001728 if (ch->ch_c_cflag & CLOCAL)
Mark Hounschella6792a32014-02-19 13:11:59 -05001729 virt_carrier = 1;
Mark Hounschella6792a32014-02-19 13:11:59 -05001730
Mark Hounschella6792a32014-02-19 13:11:59 -05001731 /*
1732 * Test for a VIRTUAL carrier transition to HIGH.
1733 */
1734 if (((ch->ch_flags & CH_FCAR) == 0) && (virt_carrier == 1)) {
Mark Hounschella6792a32014-02-19 13:11:59 -05001735 /*
1736 * When carrier rises, wake any threads waiting
1737 * for carrier in the open routine.
1738 */
1739
Mark Hounschella6792a32014-02-19 13:11:59 -05001740 if (waitqueue_active(&(ch->ch_flags_wait)))
1741 wake_up_interruptible(&ch->ch_flags_wait);
1742 }
1743
1744 /*
1745 * Test for a PHYSICAL carrier transition to HIGH.
1746 */
1747 if (((ch->ch_flags & CH_CD) == 0) && (phys_carrier == 1)) {
Mark Hounschella6792a32014-02-19 13:11:59 -05001748 /*
1749 * When carrier rises, wake any threads waiting
1750 * for carrier in the open routine.
1751 */
1752
Mark Hounschella6792a32014-02-19 13:11:59 -05001753 if (waitqueue_active(&(ch->ch_flags_wait)))
1754 wake_up_interruptible(&ch->ch_flags_wait);
1755 }
1756
1757 /*
1758 * Test for a PHYSICAL transition to low, so long as we aren't
1759 * currently ignoring physical transitions (which is what "virtual
1760 * carrier" indicates).
1761 *
1762 * The transition of the virtual carrier to low really doesn't
1763 * matter... it really only means "ignore carrier state", not
1764 * "make pretend that carrier is there".
1765 */
Mark Hounschell305ec872014-02-28 12:42:13 -05001766 if ((virt_carrier == 0) &&
1767 ((ch->ch_flags & CH_CD) != 0) &&
1768 (phys_carrier == 0)) {
Mark Hounschella6792a32014-02-19 13:11:59 -05001769 /*
1770 * When carrier drops:
1771 *
1772 * Drop carrier on all open units.
1773 *
1774 * Flush queues, waking up any task waiting in the
1775 * line discipline.
1776 *
1777 * Send a hangup to the control terminal.
1778 *
1779 * Enable all select calls.
1780 */
1781 if (waitqueue_active(&(ch->ch_flags_wait)))
1782 wake_up_interruptible(&ch->ch_flags_wait);
1783
Mark Hounschell31960c12014-02-28 12:42:08 -05001784 if (ch->ch_tun.un_open_count > 0)
Mark Hounschella6792a32014-02-19 13:11:59 -05001785 tty_hangup(ch->ch_tun.un_tty);
Mark Hounschella6792a32014-02-19 13:11:59 -05001786
Mark Hounschell31960c12014-02-28 12:42:08 -05001787 if (ch->ch_pun.un_open_count > 0)
Mark Hounschella6792a32014-02-19 13:11:59 -05001788 tty_hangup(ch->ch_pun.un_tty);
Mark Hounschella6792a32014-02-19 13:11:59 -05001789 }
1790
1791 /*
1792 * Make sure that our cached values reflect the current reality.
1793 */
1794 if (virt_carrier == 1)
1795 ch->ch_flags |= CH_FCAR;
1796 else
1797 ch->ch_flags &= ~CH_FCAR;
1798
1799 if (phys_carrier == 1)
1800 ch->ch_flags |= CH_CD;
1801 else
1802 ch->ch_flags &= ~CH_CD;
1803}
1804
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09001805/*=======================================================================
Mark Hounschella6792a32014-02-19 13:11:59 -05001806 *
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09001807 * dgap_event - FEP to host event processing routine.
Mark Hounschella6792a32014-02-19 13:11:59 -05001808 *
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09001809 * bd - Board of current event.
Mark Hounschella6792a32014-02-19 13:11:59 -05001810 *
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09001811 *=======================================================================*/
1812static int dgap_event(struct board_t *bd)
Mark Hounschella6792a32014-02-19 13:11:59 -05001813{
Mark Hounschella6792a32014-02-19 13:11:59 -05001814 struct channel_t *ch;
Mark Hounschell174efc12014-05-23 12:54:04 -04001815 ulong lock_flags;
1816 ulong lock_flags2;
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09001817 struct bs_t __iomem *bs;
1818 u8 __iomem *event;
1819 u8 __iomem *vaddr;
1820 struct ev_t __iomem *eaddr;
1821 uint head;
1822 uint tail;
1823 int port;
1824 int reason;
1825 int modem;
Mark Hounschella6792a32014-02-19 13:11:59 -05001826
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09001827 if (!bd || bd->magic != DGAP_BOARD_MAGIC)
Mark Hounschell6d488a02014-05-28 16:18:03 -04001828 return -EIO;
Mark Hounschella6792a32014-02-19 13:11:59 -05001829
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09001830 spin_lock_irqsave(&bd->bd_lock, lock_flags);
1831
1832 vaddr = bd->re_map_membase;
1833
1834 if (!vaddr) {
1835 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
Mark Hounschell6d488a02014-05-28 16:18:03 -04001836 return -EIO;
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09001837 }
1838
Ioana Ciornei18122552015-10-21 23:17:54 +03001839 eaddr = (struct ev_t __iomem *)(vaddr + EVBUF);
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09001840
1841 /* Get our head and tail */
Ioana Ciornei44209c92015-10-21 23:17:55 +03001842 head = readw(&eaddr->ev_head);
1843 tail = readw(&eaddr->ev_tail);
Mark Hounschella6792a32014-02-19 13:11:59 -05001844
1845 /*
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09001846 * Forget it if pointers out of range.
Mark Hounschella6792a32014-02-19 13:11:59 -05001847 */
Mark Hounschella6792a32014-02-19 13:11:59 -05001848
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09001849 if (head >= EVMAX - EVSTART || tail >= EVMAX - EVSTART ||
1850 (head | tail) & 03) {
1851 /* Let go of board lock */
1852 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
Mark Hounschell6d488a02014-05-28 16:18:03 -04001853 return -EIO;
Mark Hounschell7d6069d72014-02-28 12:42:10 -05001854 }
Mark Hounschella6792a32014-02-19 13:11:59 -05001855
Mark Hounschella6792a32014-02-19 13:11:59 -05001856 /*
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09001857 * Loop to process all the events in the buffer.
Mark Hounschella6792a32014-02-19 13:11:59 -05001858 */
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09001859 while (tail != head) {
Mark Hounschella6792a32014-02-19 13:11:59 -05001860 /*
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09001861 * Get interrupt information.
Mark Hounschella6792a32014-02-19 13:11:59 -05001862 */
Mark Hounschella6792a32014-02-19 13:11:59 -05001863
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09001864 event = bd->re_map_membase + tail + EVSTART;
Mark Hounschella6792a32014-02-19 13:11:59 -05001865
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09001866 port = ioread8(event);
1867 reason = ioread8(event + 1);
1868 modem = ioread8(event + 2);
Sudip Mukherjee045ec412015-09-10 17:24:59 +05301869 ioread8(event + 3);
Mark Hounschella6792a32014-02-19 13:11:59 -05001870
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09001871 /*
1872 * Make sure the interrupt is valid.
1873 */
1874 if (port >= bd->nasync)
1875 goto next;
1876
1877 if (!(reason & (IFMODEM | IFBREAK | IFTLW | IFTEM | IFDATA)))
1878 goto next;
1879
1880 ch = bd->channels[port];
1881
1882 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
1883 goto next;
1884
1885 /*
1886 * If we have made it here, the event was valid.
1887 * Lock down the channel.
1888 */
1889 spin_lock_irqsave(&ch->ch_lock, lock_flags2);
1890
1891 bs = ch->ch_bs;
1892
1893 if (!bs) {
1894 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
1895 goto next;
1896 }
1897
1898 /*
1899 * Process received data.
1900 */
1901 if (reason & IFDATA) {
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09001902 /*
1903 * ALL LOCKS *MUST* BE DROPPED BEFORE CALLING INPUT!
1904 * input could send some data to ld, which in turn
1905 * could do a callback to one of our other functions.
1906 */
1907 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
1908 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
1909
1910 dgap_input(ch);
1911
1912 spin_lock_irqsave(&bd->bd_lock, lock_flags);
1913 spin_lock_irqsave(&ch->ch_lock, lock_flags2);
1914
1915 if (ch->ch_flags & CH_RACTIVE)
1916 ch->ch_flags |= CH_RENABLE;
1917 else
Ioana Ciornei44209c92015-10-21 23:17:55 +03001918 writeb(1, &bs->idata);
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09001919
1920 if (ch->ch_flags & CH_RWAIT) {
1921 ch->ch_flags &= ~CH_RWAIT;
1922
1923 wake_up_interruptible
1924 (&ch->ch_tun.un_flags_wait);
1925 }
1926 }
1927
1928 /*
1929 * Process Modem change signals.
1930 */
1931 if (reason & IFMODEM) {
1932 ch->ch_mistat = modem;
1933 dgap_carrier(ch);
1934 }
1935
1936 /*
1937 * Process break.
1938 */
1939 if (reason & IFBREAK) {
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09001940 if (ch->ch_tun.un_tty) {
1941 /* A break has been indicated */
1942 ch->ch_err_break++;
1943 tty_buffer_request_room
1944 (ch->ch_tun.un_tty->port, 1);
1945 tty_insert_flip_char(ch->ch_tun.un_tty->port,
1946 0, TTY_BREAK);
1947 tty_flip_buffer_push(ch->ch_tun.un_tty->port);
1948 }
1949 }
1950
1951 /*
1952 * Process Transmit low.
1953 */
1954 if (reason & IFTLW) {
1955 dgap_write_wakeup(bd, ch, &ch->ch_tun, UN_LOW,
1956 &lock_flags, &lock_flags2);
1957 dgap_write_wakeup(bd, ch, &ch->ch_pun, UN_LOW,
1958 &lock_flags, &lock_flags2);
1959 if (ch->ch_flags & CH_WLOW) {
1960 ch->ch_flags &= ~CH_WLOW;
1961 wake_up_interruptible(&ch->ch_flags_wait);
1962 }
1963 }
1964
1965 /*
1966 * Process Transmit empty.
1967 */
1968 if (reason & IFTEM) {
1969 dgap_write_wakeup(bd, ch, &ch->ch_tun, UN_EMPTY,
1970 &lock_flags, &lock_flags2);
1971 dgap_write_wakeup(bd, ch, &ch->ch_pun, UN_EMPTY,
1972 &lock_flags, &lock_flags2);
1973 if (ch->ch_flags & CH_WEMPTY) {
1974 ch->ch_flags &= ~CH_WEMPTY;
1975 wake_up_interruptible(&ch->ch_flags_wait);
1976 }
1977 }
1978
1979 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
1980
1981next:
1982 tail = (tail + 4) & (EVMAX - EVSTART - 4);
Mark Hounschella6792a32014-02-19 13:11:59 -05001983 }
1984
Ioana Ciornei44209c92015-10-21 23:17:55 +03001985 writew(tail, &eaddr->ev_tail);
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09001986 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
1987
1988 return 0;
1989}
1990
1991/*
1992 * Our board poller function.
1993 */
1994static void dgap_poll_tasklet(unsigned long data)
1995{
Ioana Ciornei18122552015-10-21 23:17:54 +03001996 struct board_t *bd = (struct board_t *)data;
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09001997 ulong lock_flags;
1998 char __iomem *vaddr;
1999 u16 head, tail;
2000
2001 if (!bd || (bd->magic != DGAP_BOARD_MAGIC))
2002 return;
2003
2004 if (bd->inhibit_poller)
2005 return;
2006
2007 spin_lock_irqsave(&bd->bd_lock, lock_flags);
2008
2009 vaddr = bd->re_map_membase;
2010
2011 /*
2012 * If board is ready, parse deeper to see if there is anything to do.
2013 */
2014 if (bd->state == BOARD_READY) {
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09002015 struct ev_t __iomem *eaddr;
2016
2017 if (!bd->re_map_membase) {
2018 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
2019 return;
2020 }
2021 if (!bd->re_map_port) {
2022 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
2023 return;
2024 }
2025
2026 if (!bd->nasync)
2027 goto out;
2028
Ioana Ciornei18122552015-10-21 23:17:54 +03002029 eaddr = (struct ev_t __iomem *)(vaddr + EVBUF);
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09002030
2031 /* Get our head and tail */
Ioana Ciornei44209c92015-10-21 23:17:55 +03002032 head = readw(&eaddr->ev_head);
2033 tail = readw(&eaddr->ev_tail);
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09002034
2035 /*
2036 * If there is an event pending. Go service it.
2037 */
2038 if (head != tail) {
2039 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
2040 dgap_event(bd);
2041 spin_lock_irqsave(&bd->bd_lock, lock_flags);
2042 }
2043
2044out:
2045 /*
2046 * If board is doing interrupts, ACK the interrupt.
2047 */
Shivani Bhardwaj258e7af2015-10-16 22:09:57 +05302048 if (bd->intr_running)
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09002049 readb(bd->re_map_port + 2);
2050
2051 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
2052 return;
2053 }
2054
2055 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
2056}
2057
2058/*
2059 * dgap_found_board()
2060 *
2061 * A board has been found, init it.
2062 */
2063static struct board_t *dgap_found_board(struct pci_dev *pdev, int id,
2064 int boardnum)
2065{
2066 struct board_t *brd;
2067 unsigned int pci_irq;
2068 int i;
2069 int ret;
2070
2071 /* get the board structure and prep it */
2072 brd = kzalloc(sizeof(struct board_t), GFP_KERNEL);
2073 if (!brd)
2074 return ERR_PTR(-ENOMEM);
2075
2076 /* store the info for the board we've found */
2077 brd->magic = DGAP_BOARD_MAGIC;
2078 brd->boardnum = boardnum;
2079 brd->vendor = dgap_pci_tbl[id].vendor;
2080 brd->device = dgap_pci_tbl[id].device;
2081 brd->pdev = pdev;
2082 brd->pci_bus = pdev->bus->number;
2083 brd->pci_slot = PCI_SLOT(pdev->devfn);
2084 brd->name = dgap_ids[id].name;
2085 brd->maxports = dgap_ids[id].maxports;
2086 brd->type = dgap_ids[id].config_type;
2087 brd->dpatype = dgap_ids[id].dpatype;
2088 brd->dpastatus = BD_NOFEP;
2089 init_waitqueue_head(&brd->state_wait);
2090
2091 spin_lock_init(&brd->bd_lock);
2092
2093 brd->inhibit_poller = FALSE;
2094 brd->wait_for_bios = 0;
2095 brd->wait_for_fep = 0;
2096
2097 for (i = 0; i < MAXPORTS; i++)
2098 brd->channels[i] = NULL;
2099
2100 /* store which card & revision we have */
2101 pci_read_config_word(pdev, PCI_SUBSYSTEM_VENDOR_ID, &brd->subvendor);
2102 pci_read_config_word(pdev, PCI_SUBSYSTEM_ID, &brd->subdevice);
2103 pci_read_config_byte(pdev, PCI_REVISION_ID, &brd->rev);
2104
2105 pci_irq = pdev->irq;
2106 brd->irq = pci_irq;
2107
2108 /* get the PCI Base Address Registers */
2109
2110 /* Xr Jupiter and EPC use BAR 2 */
2111 if (brd->device == PCI_DEV_XRJ_DID || brd->device == PCI_DEV_EPCJ_DID) {
2112 brd->membase = pci_resource_start(pdev, 2);
2113 brd->membase_end = pci_resource_end(pdev, 2);
2114 }
2115 /* Everyone else uses BAR 0 */
2116 else {
2117 brd->membase = pci_resource_start(pdev, 0);
2118 brd->membase_end = pci_resource_end(pdev, 0);
2119 }
2120
2121 if (!brd->membase) {
2122 ret = -ENODEV;
2123 goto free_brd;
2124 }
2125
2126 if (brd->membase & 1)
2127 brd->membase &= ~3;
2128 else
2129 brd->membase &= ~15;
2130
2131 /*
2132 * On the PCI boards, there is no IO space allocated
2133 * The I/O registers will be in the first 3 bytes of the
2134 * upper 2MB of the 4MB memory space. The board memory
2135 * will be mapped into the low 2MB of the 4MB memory space
2136 */
2137 brd->port = brd->membase + PCI_IO_OFFSET;
Fabio Estevam4c1d2dc2015-03-03 09:55:43 -03002138 brd->port_end = brd->port + PCI_IO_SIZE_DGAP;
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09002139
2140 /*
2141 * Special initialization for non-PLX boards
2142 */
2143 if (brd->device != PCI_DEV_XRJ_DID && brd->device != PCI_DEV_EPCJ_DID) {
2144 unsigned short cmd;
2145
2146 pci_write_config_byte(pdev, 0x40, 0);
2147 pci_write_config_byte(pdev, 0x46, 0);
2148
2149 /* Limit burst length to 2 doubleword transactions */
2150 pci_write_config_byte(pdev, 0x42, 1);
2151
2152 /*
2153 * Enable IO and mem if not already done.
2154 * This was needed for support on Itanium.
2155 */
2156 pci_read_config_word(pdev, PCI_COMMAND, &cmd);
2157 cmd |= (PCI_COMMAND_IO | PCI_COMMAND_MEMORY);
2158 pci_write_config_word(pdev, PCI_COMMAND, cmd);
2159 }
2160
2161 /* init our poll helper tasklet */
2162 tasklet_init(&brd->helper_tasklet, dgap_poll_tasklet,
Ioana Ciornei629c7342015-10-21 23:17:53 +03002163 (unsigned long)brd);
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09002164
2165 ret = dgap_remap(brd);
2166 if (ret)
2167 goto free_brd;
2168
2169 pr_info("dgap: board %d: %s (rev %d), irq %ld\n",
2170 boardnum, brd->name, brd->rev, brd->irq);
2171
2172 return brd;
2173
2174free_brd:
2175 kfree(brd);
2176
2177 return ERR_PTR(ret);
2178}
2179
2180/*
2181 * dgap_intr()
2182 *
2183 * Driver interrupt handler.
2184 */
2185static irqreturn_t dgap_intr(int irq, void *voidbrd)
2186{
2187 struct board_t *brd = voidbrd;
2188
2189 if (!brd)
2190 return IRQ_NONE;
2191
2192 /*
2193 * Check to make sure its for us.
2194 */
2195 if (brd->magic != DGAP_BOARD_MAGIC)
2196 return IRQ_NONE;
2197
2198 brd->intr_count++;
2199
2200 /*
2201 * Schedule tasklet to run at a better time.
2202 */
2203 tasklet_schedule(&brd->helper_tasklet);
2204 return IRQ_HANDLED;
2205}
2206
2207/*****************************************************************************
2208*
2209* Function:
2210*
2211* dgap_poll_handler
2212*
2213* Author:
2214*
2215* Scott H Kilau
2216*
2217* Parameters:
2218*
2219* dummy -- ignored
2220*
2221* Return Values:
2222*
2223* none
2224*
2225* Description:
2226*
2227* As each timer expires, it determines (a) whether the "transmit"
2228* waiter needs to be woken up, and (b) whether the poller needs to
2229* be rescheduled.
2230*
2231******************************************************************************/
2232
2233static void dgap_poll_handler(ulong dummy)
2234{
2235 unsigned int i;
2236 struct board_t *brd;
2237 unsigned long lock_flags;
2238 ulong new_time;
2239
2240 dgap_poll_counter++;
2241
2242 /*
2243 * Do not start the board state machine until
2244 * driver tells us its up and running, and has
2245 * everything it needs.
2246 */
2247 if (dgap_driver_state != DRIVER_READY)
2248 goto schedule_poller;
2249
2250 /*
2251 * If we have just 1 board, or the system is not SMP,
2252 * then use the typical old style poller.
2253 * Otherwise, use our new tasklet based poller, which should
2254 * speed things up for multiple boards.
2255 */
2256 if ((dgap_numboards == 1) || (num_online_cpus() <= 1)) {
2257 for (i = 0; i < dgap_numboards; i++) {
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09002258 brd = dgap_board[i];
2259
2260 if (brd->state == BOARD_FAILED)
2261 continue;
2262 if (!brd->intr_running)
2263 /* Call the real board poller directly */
Ioana Ciornei18122552015-10-21 23:17:54 +03002264 dgap_poll_tasklet((unsigned long)brd);
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09002265 }
2266 } else {
2267 /*
2268 * Go thru each board, kicking off a
2269 * tasklet for each if needed
2270 */
2271 for (i = 0; i < dgap_numboards; i++) {
2272 brd = dgap_board[i];
2273
2274 /*
2275 * Attempt to grab the board lock.
2276 *
2277 * If we can't get it, no big deal, the next poll
2278 * will get it. Basically, I just really don't want
2279 * to spin in here, because I want to kick off my
2280 * tasklets as fast as I can, and then get out the
2281 * poller.
2282 */
2283 if (!spin_trylock(&brd->bd_lock))
2284 continue;
2285
2286 /*
2287 * If board is in a failed state, don't bother
2288 * scheduling a tasklet
2289 */
2290 if (brd->state == BOARD_FAILED) {
2291 spin_unlock(&brd->bd_lock);
2292 continue;
2293 }
2294
2295 /* Schedule a poll helper task */
2296 if (!brd->intr_running)
2297 tasklet_schedule(&brd->helper_tasklet);
2298
2299 /*
2300 * Can't do DGAP_UNLOCK here, as we don't have
2301 * lock_flags because we did a trylock above.
2302 */
2303 spin_unlock(&brd->bd_lock);
2304 }
2305 }
2306
2307schedule_poller:
2308
2309 /*
2310 * Schedule ourself back at the nominal wakeup interval.
2311 */
2312 spin_lock_irqsave(&dgap_poll_lock, lock_flags);
2313 dgap_poll_time += dgap_jiffies_from_ms(dgap_poll_tick);
2314
2315 new_time = dgap_poll_time - jiffies;
2316
Ioana Ciornei18122552015-10-21 23:17:54 +03002317 if ((ulong)new_time >= 2 * dgap_poll_tick) {
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09002318 dgap_poll_time =
2319 jiffies + dgap_jiffies_from_ms(dgap_poll_tick);
2320 }
2321
2322 dgap_poll_timer.function = dgap_poll_handler;
2323 dgap_poll_timer.data = 0;
2324 dgap_poll_timer.expires = dgap_poll_time;
2325 spin_unlock_irqrestore(&dgap_poll_lock, lock_flags);
2326
2327 if (!dgap_poll_stop)
2328 add_timer(&dgap_poll_timer);
2329}
2330
2331/*=======================================================================
2332 *
2333 * dgap_cmdb - Sends a 2 byte command to the FEP.
2334 *
2335 * ch - Pointer to channel structure.
2336 * cmd - Command to be sent.
2337 * byte1 - Integer containing first byte to be sent.
2338 * byte2 - Integer containing second byte to be sent.
2339 * ncmds - Wait until ncmds or fewer cmds are left
2340 * in the cmd buffer before returning.
2341 *
2342 *=======================================================================*/
2343static void dgap_cmdb(struct channel_t *ch, u8 cmd, u8 byte1,
Ioana Ciornei629c7342015-10-21 23:17:53 +03002344 u8 byte2, uint ncmds)
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09002345{
2346 char __iomem *vaddr;
2347 struct __iomem cm_t *cm_addr;
2348 uint count;
2349 uint n;
2350 u16 head;
2351 u16 tail;
2352
2353 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
2354 return;
2355
2356 /*
2357 * Check if board is still alive.
2358 */
2359 if (ch->ch_bd->state == BOARD_FAILED)
2360 return;
2361
2362 /*
2363 * Make sure the pointers are in range before
2364 * writing to the FEP memory.
2365 */
2366 vaddr = ch->ch_bd->re_map_membase;
2367
2368 if (!vaddr)
2369 return;
2370
Ioana Ciornei18122552015-10-21 23:17:54 +03002371 cm_addr = (struct cm_t __iomem *)(vaddr + CMDBUF);
Ioana Ciornei44209c92015-10-21 23:17:55 +03002372 head = readw(&cm_addr->cm_head);
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09002373
2374 /*
2375 * Forget it if pointers out of range.
2376 */
2377 if (head >= (CMDMAX - CMDSTART) || (head & 03)) {
2378 ch->ch_bd->state = BOARD_FAILED;
2379 return;
2380 }
2381
2382 /*
2383 * Put the data in the circular command buffer.
2384 */
2385 writeb(cmd, (vaddr + head + CMDSTART + 0));
Ioana Ciornei18122552015-10-21 23:17:54 +03002386 writeb((u8)ch->ch_portnum, (vaddr + head + CMDSTART + 1));
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09002387 writeb(byte1, (vaddr + head + CMDSTART + 2));
2388 writeb(byte2, (vaddr + head + CMDSTART + 3));
2389
2390 head = (head + 4) & (CMDMAX - CMDSTART - 4);
2391
Ioana Ciornei44209c92015-10-21 23:17:55 +03002392 writew(head, &cm_addr->cm_head);
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09002393
2394 /*
2395 * Wait if necessary before updating the head
2396 * pointer to limit the number of outstanding
2397 * commands to the FEP. If the time spent waiting
2398 * is outlandish, declare the FEP dead.
2399 */
2400 for (count = dgap_count ;;) {
Ioana Ciornei44209c92015-10-21 23:17:55 +03002401 head = readw(&cm_addr->cm_head);
2402 tail = readw(&cm_addr->cm_tail);
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09002403
2404 n = (head - tail) & (CMDMAX - CMDSTART - 4);
2405
2406 if (n <= ncmds * sizeof(struct cm_t))
2407 break;
2408
2409 if (--count == 0) {
2410 ch->ch_bd->state = BOARD_FAILED;
2411 return;
2412 }
2413 udelay(10);
2414 }
2415}
2416
2417/*=======================================================================
2418 *
2419 * dgap_cmdw - Sends a 1 word command to the FEP.
2420 *
2421 * ch - Pointer to channel structure.
2422 * cmd - Command to be sent.
2423 * word - Integer containing word to be sent.
2424 * ncmds - Wait until ncmds or fewer cmds are left
2425 * in the cmd buffer before returning.
2426 *
2427 *=======================================================================*/
2428static void dgap_cmdw(struct channel_t *ch, u8 cmd, u16 word, uint ncmds)
2429{
2430 char __iomem *vaddr;
2431 struct __iomem cm_t *cm_addr;
2432 uint count;
2433 uint n;
2434 u16 head;
2435 u16 tail;
2436
2437 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
2438 return;
2439
2440 /*
2441 * Check if board is still alive.
2442 */
2443 if (ch->ch_bd->state == BOARD_FAILED)
2444 return;
2445
2446 /*
2447 * Make sure the pointers are in range before
2448 * writing to the FEP memory.
2449 */
2450 vaddr = ch->ch_bd->re_map_membase;
2451 if (!vaddr)
2452 return;
2453
Ioana Ciornei18122552015-10-21 23:17:54 +03002454 cm_addr = (struct cm_t __iomem *)(vaddr + CMDBUF);
Ioana Ciornei44209c92015-10-21 23:17:55 +03002455 head = readw(&cm_addr->cm_head);
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09002456
2457 /*
2458 * Forget it if pointers out of range.
2459 */
2460 if (head >= (CMDMAX - CMDSTART) || (head & 03)) {
2461 ch->ch_bd->state = BOARD_FAILED;
2462 return;
2463 }
2464
2465 /*
2466 * Put the data in the circular command buffer.
2467 */
2468 writeb(cmd, (vaddr + head + CMDSTART + 0));
Ioana Ciornei18122552015-10-21 23:17:54 +03002469 writeb((u8)ch->ch_portnum, (vaddr + head + CMDSTART + 1));
2470 writew((u16)word, (vaddr + head + CMDSTART + 2));
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09002471
2472 head = (head + 4) & (CMDMAX - CMDSTART - 4);
2473
Ioana Ciornei44209c92015-10-21 23:17:55 +03002474 writew(head, &cm_addr->cm_head);
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09002475
2476 /*
2477 * Wait if necessary before updating the head
2478 * pointer to limit the number of outstanding
2479 * commands to the FEP. If the time spent waiting
2480 * is outlandish, declare the FEP dead.
2481 */
2482 for (count = dgap_count ;;) {
Ioana Ciornei44209c92015-10-21 23:17:55 +03002483 head = readw(&cm_addr->cm_head);
2484 tail = readw(&cm_addr->cm_tail);
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09002485
2486 n = (head - tail) & (CMDMAX - CMDSTART - 4);
2487
2488 if (n <= ncmds * sizeof(struct cm_t))
2489 break;
2490
2491 if (--count == 0) {
2492 ch->ch_bd->state = BOARD_FAILED;
2493 return;
2494 }
2495 udelay(10);
2496 }
2497}
2498
2499/*=======================================================================
2500 *
2501 * dgap_cmdw_ext - Sends a extended word command to the FEP.
2502 *
2503 * ch - Pointer to channel structure.
2504 * cmd - Command to be sent.
2505 * word - Integer containing word to be sent.
2506 * ncmds - Wait until ncmds or fewer cmds are left
2507 * in the cmd buffer before returning.
2508 *
2509 *=======================================================================*/
2510static void dgap_cmdw_ext(struct channel_t *ch, u16 cmd, u16 word, uint ncmds)
2511{
2512 char __iomem *vaddr;
2513 struct __iomem cm_t *cm_addr;
2514 uint count;
2515 uint n;
2516 u16 head;
2517 u16 tail;
2518
2519 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
2520 return;
2521
2522 /*
2523 * Check if board is still alive.
2524 */
2525 if (ch->ch_bd->state == BOARD_FAILED)
2526 return;
2527
2528 /*
2529 * Make sure the pointers are in range before
2530 * writing to the FEP memory.
2531 */
2532 vaddr = ch->ch_bd->re_map_membase;
2533 if (!vaddr)
2534 return;
2535
Ioana Ciornei18122552015-10-21 23:17:54 +03002536 cm_addr = (struct cm_t __iomem *)(vaddr + CMDBUF);
Ioana Ciornei44209c92015-10-21 23:17:55 +03002537 head = readw(&cm_addr->cm_head);
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09002538
2539 /*
2540 * Forget it if pointers out of range.
2541 */
2542 if (head >= (CMDMAX - CMDSTART) || (head & 03)) {
2543 ch->ch_bd->state = BOARD_FAILED;
2544 return;
2545 }
2546
2547 /*
2548 * Put the data in the circular command buffer.
2549 */
2550
2551 /* Write an FF to tell the FEP that we want an extended command */
Ioana Ciornei18122552015-10-21 23:17:54 +03002552 writeb((u8)0xff, (vaddr + head + CMDSTART + 0));
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09002553
Ioana Ciornei18122552015-10-21 23:17:54 +03002554 writeb((u8)ch->ch_portnum, (vaddr + head + CMDSTART + 1));
2555 writew((u16)cmd, (vaddr + head + CMDSTART + 2));
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09002556
2557 /*
2558 * If the second part of the command won't fit,
2559 * put it at the beginning of the circular buffer.
2560 */
2561 if (((head + 4) >= ((CMDMAX - CMDSTART)) || (head & 03)))
Ioana Ciornei18122552015-10-21 23:17:54 +03002562 writew((u16)word, (vaddr + CMDSTART));
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09002563 else
Ioana Ciornei18122552015-10-21 23:17:54 +03002564 writew((u16)word, (vaddr + head + CMDSTART + 4));
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09002565
2566 head = (head + 8) & (CMDMAX - CMDSTART - 4);
2567
Ioana Ciornei44209c92015-10-21 23:17:55 +03002568 writew(head, &cm_addr->cm_head);
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09002569
2570 /*
2571 * Wait if necessary before updating the head
2572 * pointer to limit the number of outstanding
2573 * commands to the FEP. If the time spent waiting
2574 * is outlandish, declare the FEP dead.
2575 */
2576 for (count = dgap_count ;;) {
Ioana Ciornei44209c92015-10-21 23:17:55 +03002577 head = readw(&cm_addr->cm_head);
2578 tail = readw(&cm_addr->cm_tail);
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09002579
2580 n = (head - tail) & (CMDMAX - CMDSTART - 4);
2581
2582 if (n <= ncmds * sizeof(struct cm_t))
2583 break;
2584
2585 if (--count == 0) {
2586 ch->ch_bd->state = BOARD_FAILED;
2587 return;
2588 }
2589 udelay(10);
2590 }
2591}
2592
2593/*=======================================================================
2594 *
2595 * dgap_wmove - Write data to FEP buffer.
2596 *
2597 * ch - Pointer to channel structure.
Colin Cronin469caab2015-05-15 13:02:40 -07002598 * buf - Pointer to characters to be moved.
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09002599 * cnt - Number of characters to move.
2600 *
2601 *=======================================================================*/
2602static void dgap_wmove(struct channel_t *ch, char *buf, uint cnt)
2603{
2604 int n;
2605 char __iomem *taddr;
2606 struct bs_t __iomem *bs;
2607 u16 head;
2608
2609 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
2610 return;
2611
2612 /*
2613 * Check parameters.
2614 */
2615 bs = ch->ch_bs;
Ioana Ciornei44209c92015-10-21 23:17:55 +03002616 head = readw(&bs->tx_head);
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09002617
2618 /*
2619 * If pointers are out of range, just return.
2620 */
2621 if ((cnt > ch->ch_tsize) ||
2622 (unsigned)(head - ch->ch_tstart) >= ch->ch_tsize)
2623 return;
2624
2625 /*
2626 * If the write wraps over the top of the circular buffer,
2627 * move the portion up to the wrap point, and reset the
2628 * pointers to the bottom.
2629 */
2630 n = ch->ch_tstart + ch->ch_tsize - head;
2631
2632 if (cnt >= n) {
2633 cnt -= n;
2634 taddr = ch->ch_taddr + head;
2635 memcpy_toio(taddr, buf, n);
2636 head = ch->ch_tstart;
2637 buf += n;
2638 }
2639
2640 /*
2641 * Move rest of data.
2642 */
2643 taddr = ch->ch_taddr + head;
2644 n = cnt;
2645 memcpy_toio(taddr, buf, n);
2646 head += cnt;
2647
Ioana Ciornei44209c92015-10-21 23:17:55 +03002648 writew(head, &bs->tx_head);
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09002649}
2650
2651/*
2652 * Calls the firmware to reset this channel.
2653 */
2654static void dgap_firmware_reset_port(struct channel_t *ch)
2655{
2656 dgap_cmdb(ch, CHRESET, 0, 0, 0);
2657
2658 /*
2659 * Now that the channel is reset, we need to make sure
2660 * all the current settings get reapplied to the port
2661 * in the firmware.
2662 *
2663 * So we will set the driver's cache of firmware
2664 * settings all to 0, and then call param.
2665 */
2666 ch->ch_fepiflag = 0;
2667 ch->ch_fepcflag = 0;
2668 ch->ch_fepoflag = 0;
2669 ch->ch_fepstartc = 0;
2670 ch->ch_fepstopc = 0;
2671 ch->ch_fepastartc = 0;
2672 ch->ch_fepastopc = 0;
2673 ch->ch_mostat = 0;
2674 ch->ch_hflow = 0;
2675}
2676
2677/*=======================================================================
2678 *
2679 * dgap_param - Set Digi parameters.
2680 *
2681 * struct tty_struct * - TTY for port.
2682 *
2683 *=======================================================================*/
2684static int dgap_param(struct channel_t *ch, struct board_t *bd, u32 un_type)
2685{
2686 u16 head;
2687 u16 cflag;
2688 u16 iflag;
2689 u8 mval;
2690 u8 hflow;
2691
2692 /*
2693 * If baud rate is zero, flush queues, and set mval to drop DTR.
2694 */
2695 if ((ch->ch_c_cflag & (CBAUD)) == 0) {
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09002696 /* flush rx */
Ioana Ciornei44209c92015-10-21 23:17:55 +03002697 head = readw(&ch->ch_bs->rx_head);
2698 writew(head, &ch->ch_bs->rx_tail);
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09002699
2700 /* flush tx */
Ioana Ciornei44209c92015-10-21 23:17:55 +03002701 head = readw(&ch->ch_bs->tx_head);
2702 writew(head, &ch->ch_bs->tx_tail);
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09002703
2704 ch->ch_flags |= (CH_BAUD0);
2705
2706 /* Drop RTS and DTR */
Ioana Ciorneia9108b72015-10-21 23:17:56 +03002707 ch->ch_mval &= ~(D_RTS(ch) | D_DTR(ch));
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09002708 mval = D_DTR(ch) | D_RTS(ch);
2709 ch->ch_baud_info = 0;
2710
2711 } else if (ch->ch_custom_speed && (bd->bd_flags & BD_FEP5PLUS)) {
2712 /*
2713 * Tell the fep to do the command
2714 */
2715
2716 dgap_cmdw_ext(ch, 0xff01, ch->ch_custom_speed, 0);
2717
2718 /*
2719 * Now go get from fep mem, what the fep
2720 * believes the custom baud rate is.
2721 */
2722 ch->ch_custom_speed = dgap_get_custom_baud(ch);
2723 ch->ch_baud_info = ch->ch_custom_speed;
2724
2725 /* Handle transition from B0 */
2726 if (ch->ch_flags & CH_BAUD0) {
2727 ch->ch_flags &= ~(CH_BAUD0);
Ioana Ciorneia9108b72015-10-21 23:17:56 +03002728 ch->ch_mval |= (D_RTS(ch) | D_DTR(ch));
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09002729 }
2730 mval = D_DTR(ch) | D_RTS(ch);
2731
2732 } else {
2733 /*
2734 * Set baud rate, character size, and parity.
2735 */
2736
2737
2738 int iindex = 0;
2739 int jindex = 0;
2740 int baud = 0;
2741
2742 ulong bauds[4][16] = {
2743 { /* slowbaud */
2744 0, 50, 75, 110,
2745 134, 150, 200, 300,
2746 600, 1200, 1800, 2400,
2747 4800, 9600, 19200, 38400 },
2748 { /* slowbaud & CBAUDEX */
2749 0, 57600, 115200, 230400,
2750 460800, 150, 200, 921600,
2751 600, 1200, 1800, 2400,
2752 4800, 9600, 19200, 38400 },
2753 { /* fastbaud */
2754 0, 57600, 76800, 115200,
2755 14400, 57600, 230400, 76800,
2756 115200, 230400, 28800, 460800,
2757 921600, 9600, 19200, 38400 },
2758 { /* fastbaud & CBAUDEX */
2759 0, 57600, 115200, 230400,
2760 460800, 150, 200, 921600,
2761 600, 1200, 1800, 2400,
2762 4800, 9600, 19200, 38400 }
2763 };
2764
2765 /*
2766 * Only use the TXPrint baud rate if the
2767 * terminal unit is NOT open
2768 */
2769 if (!(ch->ch_tun.un_flags & UN_ISOPEN) &&
2770 un_type == DGAP_PRINT)
2771 baud = C_BAUD(ch->ch_pun.un_tty) & 0xff;
2772 else
2773 baud = C_BAUD(ch->ch_tun.un_tty) & 0xff;
2774
2775 if (ch->ch_c_cflag & CBAUDEX)
2776 iindex = 1;
2777
2778 if (ch->ch_digi.digi_flags & DIGI_FAST)
2779 iindex += 2;
2780
2781 jindex = baud;
2782
2783 if ((iindex >= 0) && (iindex < 4) &&
2784 (jindex >= 0) && (jindex < 16))
2785 baud = bauds[iindex][jindex];
2786 else
2787 baud = 0;
2788
2789 if (baud == 0)
2790 baud = 9600;
2791
2792 ch->ch_baud_info = baud;
2793
2794 /*
2795 * CBAUD has bit position 0x1000 set these days to
2796 * indicate Linux baud rate remap.
2797 * We use a different bit assignment for high speed.
2798 * Clear this bit out while grabbing the parts of
2799 * "cflag" we want.
2800 */
2801 cflag = ch->ch_c_cflag & ((CBAUD ^ CBAUDEX) | PARODD | PARENB |
2802 CSTOPB | CSIZE);
2803
2804 /*
2805 * HUPCL bit is used by FEP to indicate fast baud
2806 * table is to be used.
2807 */
2808 if ((ch->ch_digi.digi_flags & DIGI_FAST) ||
2809 (ch->ch_c_cflag & CBAUDEX))
2810 cflag |= HUPCL;
2811
2812 if ((ch->ch_c_cflag & CBAUDEX) &&
2813 !(ch->ch_digi.digi_flags & DIGI_FAST)) {
2814 /*
2815 * The below code is trying to guarantee that only
2816 * baud rates 115200, 230400, 460800, 921600 are
2817 * remapped. We use exclusive or because the various
2818 * baud rates share common bit positions and therefore
2819 * can't be tested for easily.
2820 */
2821 tcflag_t tcflag = (ch->ch_c_cflag & CBAUD) | CBAUDEX;
2822 int baudpart = 0;
2823
2824 /*
2825 * Map high speed requests to index
2826 * into FEP's baud table
2827 */
2828 switch (tcflag) {
2829 case B57600:
2830 baudpart = 1;
2831 break;
2832#ifdef B76800
2833 case B76800:
2834 baudpart = 2;
2835 break;
2836#endif
2837 case B115200:
2838 baudpart = 3;
2839 break;
2840 case B230400:
2841 baudpart = 9;
2842 break;
2843 case B460800:
2844 baudpart = 11;
2845 break;
2846#ifdef B921600
2847 case B921600:
2848 baudpart = 12;
2849 break;
2850#endif
2851 default:
2852 baudpart = 0;
2853 }
2854
2855 if (baudpart)
2856 cflag = (cflag & ~(CBAUD | CBAUDEX)) | baudpart;
2857 }
2858
2859 cflag &= 0xffff;
2860
2861 if (cflag != ch->ch_fepcflag) {
Ioana Ciornei18122552015-10-21 23:17:54 +03002862 ch->ch_fepcflag = (u16)(cflag & 0xffff);
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09002863
2864 /*
2865 * Okay to have channel and board
2866 * locks held calling this
2867 */
Ioana Ciornei18122552015-10-21 23:17:54 +03002868 dgap_cmdw(ch, SCFLAG, (u16)cflag, 0);
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09002869 }
2870
2871 /* Handle transition from B0 */
2872 if (ch->ch_flags & CH_BAUD0) {
2873 ch->ch_flags &= ~(CH_BAUD0);
Ioana Ciorneia9108b72015-10-21 23:17:56 +03002874 ch->ch_mval |= (D_RTS(ch) | D_DTR(ch));
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09002875 }
2876 mval = D_DTR(ch) | D_RTS(ch);
2877 }
2878
2879 /*
2880 * Get input flags.
2881 */
2882 iflag = ch->ch_c_iflag & (IGNBRK | BRKINT | IGNPAR | PARMRK |
2883 INPCK | ISTRIP | IXON | IXANY | IXOFF);
2884
2885 if ((ch->ch_startc == _POSIX_VDISABLE) ||
2886 (ch->ch_stopc == _POSIX_VDISABLE)) {
2887 iflag &= ~(IXON | IXOFF);
2888 ch->ch_c_iflag &= ~(IXON | IXOFF);
2889 }
2890
2891 /*
2892 * Only the IBM Xr card can switch between
2893 * 232 and 422 modes on the fly
2894 */
2895 if (bd->device == PCI_DEV_XR_IBM_DID) {
2896 if (ch->ch_digi.digi_flags & DIGI_422)
2897 dgap_cmdb(ch, SCOMMODE, MODE_422, 0, 0);
2898 else
2899 dgap_cmdb(ch, SCOMMODE, MODE_232, 0, 0);
2900 }
2901
2902 if (ch->ch_digi.digi_flags & DIGI_ALTPIN)
2903 iflag |= IALTPIN;
2904
2905 if (iflag != ch->ch_fepiflag) {
2906 ch->ch_fepiflag = iflag;
2907
2908 /* Okay to have channel and board locks held calling this */
Ioana Ciornei18122552015-10-21 23:17:54 +03002909 dgap_cmdw(ch, SIFLAG, (u16)ch->ch_fepiflag, 0);
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09002910 }
2911
2912 /*
2913 * Select hardware handshaking.
2914 */
2915 hflow = 0;
2916
2917 if (ch->ch_c_cflag & CRTSCTS)
2918 hflow |= (D_RTS(ch) | D_CTS(ch));
2919 if (ch->ch_digi.digi_flags & RTSPACE)
2920 hflow |= D_RTS(ch);
2921 if (ch->ch_digi.digi_flags & DTRPACE)
2922 hflow |= D_DTR(ch);
2923 if (ch->ch_digi.digi_flags & CTSPACE)
2924 hflow |= D_CTS(ch);
2925 if (ch->ch_digi.digi_flags & DSRPACE)
2926 hflow |= D_DSR(ch);
2927 if (ch->ch_digi.digi_flags & DCDPACE)
2928 hflow |= D_CD(ch);
2929
2930 if (hflow != ch->ch_hflow) {
2931 ch->ch_hflow = hflow;
2932
2933 /* Okay to have channel and board locks held calling this */
Ioana Ciornei18122552015-10-21 23:17:54 +03002934 dgap_cmdb(ch, SHFLOW, (u8)hflow, 0xff, 0);
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09002935 }
2936
2937 /*
2938 * Set RTS and/or DTR Toggle if needed,
2939 * but only if product is FEP5+ based.
2940 */
2941 if (bd->bd_flags & BD_FEP5PLUS) {
2942 u16 hflow2 = 0;
2943
2944 if (ch->ch_digi.digi_flags & DIGI_RTS_TOGGLE)
2945 hflow2 |= (D_RTS(ch));
2946 if (ch->ch_digi.digi_flags & DIGI_DTR_TOGGLE)
2947 hflow2 |= (D_DTR(ch));
2948
2949 dgap_cmdw_ext(ch, 0xff03, hflow2, 0);
2950 }
2951
2952 /*
2953 * Set modem control lines.
2954 */
2955
2956 mval ^= ch->ch_mforce & (mval ^ ch->ch_mval);
2957
2958 if (ch->ch_mostat ^ mval) {
2959 ch->ch_mostat = mval;
2960
2961 /* Okay to have channel and board locks held calling this */
Ioana Ciornei18122552015-10-21 23:17:54 +03002962 dgap_cmdb(ch, SMODEM, (u8)mval, D_RTS(ch) | D_DTR(ch), 0);
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09002963 }
2964
2965 /*
2966 * Read modem signals, and then call carrier function.
2967 */
Ioana Ciornei44209c92015-10-21 23:17:55 +03002968 ch->ch_mistat = readb(&ch->ch_bs->m_stat);
Mark Hounschella6792a32014-02-19 13:11:59 -05002969 dgap_carrier(ch);
Mark Hounschella6792a32014-02-19 13:11:59 -05002970
2971 /*
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09002972 * Set the start and stop characters.
Mark Hounschella6792a32014-02-19 13:11:59 -05002973 */
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09002974 if (ch->ch_startc != ch->ch_fepstartc ||
2975 ch->ch_stopc != ch->ch_fepstopc) {
2976 ch->ch_fepstartc = ch->ch_startc;
2977 ch->ch_fepstopc = ch->ch_stopc;
Mark Hounschella6792a32014-02-19 13:11:59 -05002978
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09002979 /* Okay to have channel and board locks held calling this */
2980 dgap_cmdb(ch, SFLOWC, ch->ch_fepstartc, ch->ch_fepstopc, 0);
2981 }
Mark Hounschella6792a32014-02-19 13:11:59 -05002982
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09002983 /*
2984 * Set the Auxiliary start and stop characters.
2985 */
2986 if (ch->ch_astartc != ch->ch_fepastartc ||
2987 ch->ch_astopc != ch->ch_fepastopc) {
2988 ch->ch_fepastartc = ch->ch_astartc;
2989 ch->ch_fepastopc = ch->ch_astopc;
Mark Hounschella6792a32014-02-19 13:11:59 -05002990
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09002991 /* Okay to have channel and board locks held calling this */
2992 dgap_cmdb(ch, SAFLOWC, ch->ch_fepastartc, ch->ch_fepastopc, 0);
2993 }
Mark Hounschella6792a32014-02-19 13:11:59 -05002994
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09002995 return 0;
Mark Hounschella6792a32014-02-19 13:11:59 -05002996}
2997
Mark Hounschella6792a32014-02-19 13:11:59 -05002998/*
2999 * dgap_block_til_ready()
3000 *
3001 * Wait for DCD, if needed.
3002 */
Mark Hounschell6045b6a2014-03-06 13:02:45 -05003003static int dgap_block_til_ready(struct tty_struct *tty, struct file *file,
3004 struct channel_t *ch)
Mark Hounschella6792a32014-02-19 13:11:59 -05003005{
3006 int retval = 0;
Mark Hounschell7dfa3832014-05-23 10:14:02 -04003007 struct un_t *un;
Mark Hounschell174efc12014-05-23 12:54:04 -04003008 ulong lock_flags;
3009 uint old_flags;
Mark Hounschell7dfa3832014-05-23 10:14:02 -04003010 int sleep_on_un_flags;
Mark Hounschella6792a32014-02-19 13:11:59 -05003011
Mark Hounschell305ec872014-02-28 12:42:13 -05003012 if (!tty || tty->magic != TTY_MAGIC || !file || !ch ||
Ioana Ciornei629c7342015-10-21 23:17:53 +03003013 ch->magic != DGAP_CHANNEL_MAGIC)
Mark Hounschell6d488a02014-05-28 16:18:03 -04003014 return -EIO;
Mark Hounschella6792a32014-02-19 13:11:59 -05003015
3016 un = tty->driver_data;
Mark Hounschell305ec872014-02-28 12:42:13 -05003017 if (!un || un->magic != DGAP_UNIT_MAGIC)
Mark Hounschell6d488a02014-05-28 16:18:03 -04003018 return -EIO;
Mark Hounschella6792a32014-02-19 13:11:59 -05003019
Mark Hounschellc43846a2014-03-19 11:10:51 -04003020 spin_lock_irqsave(&ch->ch_lock, lock_flags);
Mark Hounschella6792a32014-02-19 13:11:59 -05003021
3022 ch->ch_wopen++;
3023
3024 /* Loop forever */
3025 while (1) {
Mark Hounschella6792a32014-02-19 13:11:59 -05003026 sleep_on_un_flags = 0;
3027
3028 /*
Mark Hounschell6045b6a2014-03-06 13:02:45 -05003029 * If board has failed somehow during our sleep,
3030 * bail with error.
Mark Hounschella6792a32014-02-19 13:11:59 -05003031 */
3032 if (ch->ch_bd->state == BOARD_FAILED) {
Mark Hounschell6d488a02014-05-28 16:18:03 -04003033 retval = -EIO;
Mark Hounschella6792a32014-02-19 13:11:59 -05003034 break;
3035 }
3036
3037 /* If tty was hung up, break out of loop and set error. */
3038 if (tty_hung_up_p(file)) {
3039 retval = -EAGAIN;
3040 break;
3041 }
3042
3043 /*
3044 * If either unit is in the middle of the fragile part of close,
3045 * we just cannot touch the channel safely.
3046 * Go back to sleep, knowing that when the channel can be
3047 * touched safely, the close routine will signal the
3048 * ch_wait_flags to wake us back up.
3049 */
Mark Hounschell6045b6a2014-03-06 13:02:45 -05003050 if (!((ch->ch_tun.un_flags | ch->ch_pun.un_flags) &
3051 UN_CLOSING)) {
Mark Hounschella6792a32014-02-19 13:11:59 -05003052 /*
3053 * Our conditions to leave cleanly and happily:
3054 * 1) NONBLOCKING on the tty is set.
3055 * 2) CLOCAL is set.
3056 * 3) DCD (fake or real) is active.
3057 */
3058
Mark Hounschell305ec872014-02-28 12:42:13 -05003059 if (file->f_flags & O_NONBLOCK)
Mark Hounschella6792a32014-02-19 13:11:59 -05003060 break;
Mark Hounschella6792a32014-02-19 13:11:59 -05003061
Mark Hounschell305ec872014-02-28 12:42:13 -05003062 if (tty->flags & (1 << TTY_IO_ERROR))
Mark Hounschella6792a32014-02-19 13:11:59 -05003063 break;
Mark Hounschella6792a32014-02-19 13:11:59 -05003064
Mark Hounschell31960c12014-02-28 12:42:08 -05003065 if (ch->ch_flags & CH_CD)
Mark Hounschella6792a32014-02-19 13:11:59 -05003066 break;
Mark Hounschella6792a32014-02-19 13:11:59 -05003067
Mark Hounschell31960c12014-02-28 12:42:08 -05003068 if (ch->ch_flags & CH_FCAR)
Mark Hounschella6792a32014-02-19 13:11:59 -05003069 break;
Mark Hounschell305ec872014-02-28 12:42:13 -05003070 } else {
Mark Hounschella6792a32014-02-19 13:11:59 -05003071 sleep_on_un_flags = 1;
3072 }
3073
3074 /*
3075 * If there is a signal pending, the user probably
3076 * interrupted (ctrl-c) us.
3077 * Leave loop with error set.
3078 */
3079 if (signal_pending(current)) {
Mark Hounschella6792a32014-02-19 13:11:59 -05003080 retval = -ERESTARTSYS;
3081 break;
3082 }
3083
Mark Hounschella6792a32014-02-19 13:11:59 -05003084 /*
3085 * Store the flags before we let go of channel lock
3086 */
3087 if (sleep_on_un_flags)
3088 old_flags = ch->ch_tun.un_flags | ch->ch_pun.un_flags;
3089 else
3090 old_flags = ch->ch_flags;
3091
3092 /*
3093 * Let go of channel lock before calling schedule.
3094 * Our poller will get any FEP events and wake us up when DCD
3095 * eventually goes active.
3096 */
3097
Mark Hounschellc43846a2014-03-19 11:10:51 -04003098 spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
Mark Hounschella6792a32014-02-19 13:11:59 -05003099
Mark Hounschella6792a32014-02-19 13:11:59 -05003100 /*
Mark Hounschell6045b6a2014-03-06 13:02:45 -05003101 * Wait for something in the flags to change
3102 * from the current value.
Mark Hounschella6792a32014-02-19 13:11:59 -05003103 */
3104 if (sleep_on_un_flags) {
3105 retval = wait_event_interruptible(un->un_flags_wait,
Mark Hounschell6045b6a2014-03-06 13:02:45 -05003106 (old_flags != (ch->ch_tun.un_flags |
3107 ch->ch_pun.un_flags)));
Mark Hounschell305ec872014-02-28 12:42:13 -05003108 } else {
Mark Hounschella6792a32014-02-19 13:11:59 -05003109 retval = wait_event_interruptible(ch->ch_flags_wait,
3110 (old_flags != ch->ch_flags));
3111 }
3112
Mark Hounschella6792a32014-02-19 13:11:59 -05003113 /*
3114 * We got woken up for some reason.
3115 * Before looping around, grab our channel lock.
3116 */
Mark Hounschellc43846a2014-03-19 11:10:51 -04003117 spin_lock_irqsave(&ch->ch_lock, lock_flags);
Mark Hounschella6792a32014-02-19 13:11:59 -05003118 }
3119
3120 ch->ch_wopen--;
3121
Mark Hounschellc43846a2014-03-19 11:10:51 -04003122 spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
Mark Hounschella6792a32014-02-19 13:11:59 -05003123
Daeseok Youndfa30ac2014-07-15 18:47:11 +09003124 return retval;
Mark Hounschella6792a32014-02-19 13:11:59 -05003125}
3126
Mark Hounschella6792a32014-02-19 13:11:59 -05003127/*
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09003128 * dgap_tty_flush_buffer()
3129 *
3130 * Flush Tx buffer (make in == out)
3131 */
3132static void dgap_tty_flush_buffer(struct tty_struct *tty)
3133{
3134 struct board_t *bd;
3135 struct channel_t *ch;
3136 struct un_t *un;
3137 ulong lock_flags;
3138 ulong lock_flags2;
3139 u16 head;
3140
3141 if (!tty || tty->magic != TTY_MAGIC)
3142 return;
3143
3144 un = tty->driver_data;
3145 if (!un || un->magic != DGAP_UNIT_MAGIC)
3146 return;
3147
3148 ch = un->un_ch;
3149 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
3150 return;
3151
3152 bd = ch->ch_bd;
3153 if (!bd || bd->magic != DGAP_BOARD_MAGIC)
3154 return;
3155
3156 spin_lock_irqsave(&bd->bd_lock, lock_flags);
3157 spin_lock_irqsave(&ch->ch_lock, lock_flags2);
3158
3159 ch->ch_flags &= ~CH_STOP;
Ioana Ciornei44209c92015-10-21 23:17:55 +03003160 head = readw(&ch->ch_bs->tx_head);
Ioana Ciornei18122552015-10-21 23:17:54 +03003161 dgap_cmdw(ch, FLUSHTX, (u16)head, 0);
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09003162 dgap_cmdw(ch, RESUMETX, 0, 0);
Ioana Ciorneia9108b72015-10-21 23:17:56 +03003163 if (ch->ch_tun.un_flags & (UN_LOW | UN_EMPTY)) {
3164 ch->ch_tun.un_flags &= ~(UN_LOW | UN_EMPTY);
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09003165 wake_up_interruptible(&ch->ch_tun.un_flags_wait);
3166 }
Ioana Ciorneia9108b72015-10-21 23:17:56 +03003167 if (ch->ch_pun.un_flags & (UN_LOW | UN_EMPTY)) {
3168 ch->ch_pun.un_flags &= ~(UN_LOW | UN_EMPTY);
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09003169 wake_up_interruptible(&ch->ch_pun.un_flags_wait);
3170 }
3171
3172 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
3173 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
3174 if (waitqueue_active(&tty->write_wait))
3175 wake_up_interruptible(&tty->write_wait);
3176 tty_wakeup(tty);
3177}
3178
3179/*
Mark Hounschella6792a32014-02-19 13:11:59 -05003180 * dgap_tty_hangup()
3181 *
3182 * Hangup the port. Like a close, but don't wait for output to drain.
3183 */
3184static void dgap_tty_hangup(struct tty_struct *tty)
3185{
Mark Hounschell174efc12014-05-23 12:54:04 -04003186 struct board_t *bd;
Mark Hounschella6792a32014-02-19 13:11:59 -05003187 struct channel_t *ch;
Mark Hounschell174efc12014-05-23 12:54:04 -04003188 struct un_t *un;
Mark Hounschella6792a32014-02-19 13:11:59 -05003189
3190 if (!tty || tty->magic != TTY_MAGIC)
3191 return;
3192
3193 un = tty->driver_data;
3194 if (!un || un->magic != DGAP_UNIT_MAGIC)
3195 return;
3196
3197 ch = un->un_ch;
3198 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
3199 return;
3200
3201 bd = ch->ch_bd;
3202 if (!bd || bd->magic != DGAP_BOARD_MAGIC)
3203 return;
3204
Mark Hounschella6792a32014-02-19 13:11:59 -05003205 /* flush the transmit queues */
3206 dgap_tty_flush_buffer(tty);
Mark Hounschella6792a32014-02-19 13:11:59 -05003207}
3208
Mark Hounschella6792a32014-02-19 13:11:59 -05003209/*
Mark Hounschella6792a32014-02-19 13:11:59 -05003210 * dgap_tty_chars_in_buffer()
3211 *
3212 * Return number of characters that have not been transmitted yet.
3213 *
3214 * This routine is used by the line discipline to determine if there
3215 * is data waiting to be transmitted/drained/flushed or not.
3216 */
3217static int dgap_tty_chars_in_buffer(struct tty_struct *tty)
3218{
Mark Hounschell7dfa3832014-05-23 10:14:02 -04003219 struct board_t *bd;
3220 struct channel_t *ch;
3221 struct un_t *un;
Mark Hounschell405b26d2014-04-23 16:25:27 -04003222 struct bs_t __iomem *bs;
Mark Hounschell2023d182014-04-14 16:42:43 -04003223 u8 tbusy;
Mark Hounschell7dfa3832014-05-23 10:14:02 -04003224 uint chars;
Mark Hounschella6792a32014-02-19 13:11:59 -05003225 u16 thead, ttail, tmask, chead, ctail;
Mark Hounschell174efc12014-05-23 12:54:04 -04003226 ulong lock_flags = 0;
3227 ulong lock_flags2 = 0;
Mark Hounschella6792a32014-02-19 13:11:59 -05003228
Mark Hounschell9a133a92014-05-28 16:17:45 -04003229 if (!tty)
Mark Hounschellcf42c342014-02-28 12:42:09 -05003230 return 0;
Mark Hounschella6792a32014-02-19 13:11:59 -05003231
3232 un = tty->driver_data;
3233 if (!un || un->magic != DGAP_UNIT_MAGIC)
Mark Hounschellcf42c342014-02-28 12:42:09 -05003234 return 0;
Mark Hounschella6792a32014-02-19 13:11:59 -05003235
3236 ch = un->un_ch;
3237 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
Mark Hounschellcf42c342014-02-28 12:42:09 -05003238 return 0;
Mark Hounschella6792a32014-02-19 13:11:59 -05003239
3240 bd = ch->ch_bd;
3241 if (!bd || bd->magic != DGAP_BOARD_MAGIC)
Mark Hounschellcf42c342014-02-28 12:42:09 -05003242 return 0;
Mark Hounschella6792a32014-02-19 13:11:59 -05003243
Mark Hounschell7d6069d72014-02-28 12:42:10 -05003244 bs = ch->ch_bs;
Mark Hounschella6792a32014-02-19 13:11:59 -05003245 if (!bs)
Mark Hounschellcf42c342014-02-28 12:42:09 -05003246 return 0;
Mark Hounschella6792a32014-02-19 13:11:59 -05003247
Mark Hounschellc43846a2014-03-19 11:10:51 -04003248 spin_lock_irqsave(&bd->bd_lock, lock_flags);
3249 spin_lock_irqsave(&ch->ch_lock, lock_flags2);
Mark Hounschella6792a32014-02-19 13:11:59 -05003250
3251 tmask = (ch->ch_tsize - 1);
3252
3253 /* Get Transmit queue pointers */
Ioana Ciornei44209c92015-10-21 23:17:55 +03003254 thead = readw(&bs->tx_head) & tmask;
3255 ttail = readw(&bs->tx_tail) & tmask;
Mark Hounschella6792a32014-02-19 13:11:59 -05003256
3257 /* Get tbusy flag */
Ioana Ciornei44209c92015-10-21 23:17:55 +03003258 tbusy = readb(&bs->tbusy);
Mark Hounschella6792a32014-02-19 13:11:59 -05003259
3260 /* Get Command queue pointers */
Ioana Ciornei44209c92015-10-21 23:17:55 +03003261 chead = readw(&ch->ch_cm->cm_head);
3262 ctail = readw(&ch->ch_cm->cm_tail);
Mark Hounschella6792a32014-02-19 13:11:59 -05003263
Mark Hounschellc43846a2014-03-19 11:10:51 -04003264 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
3265 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
Mark Hounschella6792a32014-02-19 13:11:59 -05003266
3267 /*
3268 * The only way we know for sure if there is no pending
3269 * data left to be transferred, is if:
3270 * 1) Transmit head and tail are equal (empty).
3271 * 2) Command queue head and tail are equal (empty).
3272 * 3) The "TBUSY" flag is 0. (Transmitter not busy).
Mark Hounschell7d6069d72014-02-28 12:42:10 -05003273 */
Mark Hounschella6792a32014-02-19 13:11:59 -05003274
3275 if ((ttail == thead) && (tbusy == 0) && (chead == ctail)) {
3276 chars = 0;
Mark Hounschell305ec872014-02-28 12:42:13 -05003277 } else {
Mark Hounschella6792a32014-02-19 13:11:59 -05003278 if (thead >= ttail)
3279 chars = thead - ttail;
3280 else
3281 chars = thead - ttail + ch->ch_tsize;
3282 /*
3283 * Fudge factor here.
3284 * If chars is zero, we know that the command queue had
3285 * something in it or tbusy was set. Because we cannot
3286 * be sure if there is still some data to be transmitted,
3287 * lets lie, and tell ld we have 1 byte left.
3288 */
3289 if (chars == 0) {
3290 /*
3291 * If TBUSY is still set, and our tx buffers are empty,
3292 * force the firmware to send me another wakeup after
3293 * TBUSY has been cleared.
3294 */
3295 if (tbusy != 0) {
Mark Hounschellc43846a2014-03-19 11:10:51 -04003296 spin_lock_irqsave(&ch->ch_lock, lock_flags);
Mark Hounschella6792a32014-02-19 13:11:59 -05003297 un->un_flags |= UN_EMPTY;
Ioana Ciornei44209c92015-10-21 23:17:55 +03003298 writeb(1, &bs->iempty);
Mark Hounschellc43846a2014-03-19 11:10:51 -04003299 spin_unlock_irqrestore(&ch->ch_lock,
3300 lock_flags);
Mark Hounschella6792a32014-02-19 13:11:59 -05003301 }
3302 chars = 1;
3303 }
3304 }
3305
Mark Hounschellcf42c342014-02-28 12:42:09 -05003306 return chars;
Mark Hounschella6792a32014-02-19 13:11:59 -05003307}
3308
Mark Hounschella6792a32014-02-19 13:11:59 -05003309static int dgap_wait_for_drain(struct tty_struct *tty)
3310{
3311 struct channel_t *ch;
3312 struct un_t *un;
Mark Hounschell405b26d2014-04-23 16:25:27 -04003313 struct bs_t __iomem *bs;
Mark Hounschell7dfa3832014-05-23 10:14:02 -04003314 int ret = 0;
Mark Hounschella6792a32014-02-19 13:11:59 -05003315 uint count = 1;
Mark Hounschell174efc12014-05-23 12:54:04 -04003316 ulong lock_flags = 0;
Mark Hounschella6792a32014-02-19 13:11:59 -05003317
3318 if (!tty || tty->magic != TTY_MAGIC)
Mark Hounschell7dfa3832014-05-23 10:14:02 -04003319 return -EIO;
Mark Hounschella6792a32014-02-19 13:11:59 -05003320
3321 un = tty->driver_data;
3322 if (!un || un->magic != DGAP_UNIT_MAGIC)
Mark Hounschell7dfa3832014-05-23 10:14:02 -04003323 return -EIO;
Mark Hounschella6792a32014-02-19 13:11:59 -05003324
3325 ch = un->un_ch;
3326 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
Mark Hounschell7dfa3832014-05-23 10:14:02 -04003327 return -EIO;
Mark Hounschella6792a32014-02-19 13:11:59 -05003328
Mark Hounschell7d6069d72014-02-28 12:42:10 -05003329 bs = ch->ch_bs;
Mark Hounschella6792a32014-02-19 13:11:59 -05003330 if (!bs)
Mark Hounschell7dfa3832014-05-23 10:14:02 -04003331 return -EIO;
Mark Hounschella6792a32014-02-19 13:11:59 -05003332
Mark Hounschella6792a32014-02-19 13:11:59 -05003333 /* Loop until data is drained */
3334 while (count != 0) {
Mark Hounschella6792a32014-02-19 13:11:59 -05003335 count = dgap_tty_chars_in_buffer(tty);
3336
3337 if (count == 0)
3338 break;
3339
3340 /* Set flag waiting for drain */
Mark Hounschellc43846a2014-03-19 11:10:51 -04003341 spin_lock_irqsave(&ch->ch_lock, lock_flags);
Mark Hounschella6792a32014-02-19 13:11:59 -05003342 un->un_flags |= UN_EMPTY;
Ioana Ciornei44209c92015-10-21 23:17:55 +03003343 writeb(1, &bs->iempty);
Mark Hounschellc43846a2014-03-19 11:10:51 -04003344 spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
Mark Hounschella6792a32014-02-19 13:11:59 -05003345
3346 /* Go to sleep till we get woken up */
Mark Hounschell6045b6a2014-03-06 13:02:45 -05003347 ret = wait_event_interruptible(un->un_flags_wait,
3348 ((un->un_flags & UN_EMPTY) == 0));
Mark Hounschella6792a32014-02-19 13:11:59 -05003349 /* If ret is non-zero, user ctrl-c'ed us */
Mark Hounschell305ec872014-02-28 12:42:13 -05003350 if (ret)
Mark Hounschella6792a32014-02-19 13:11:59 -05003351 break;
Mark Hounschella6792a32014-02-19 13:11:59 -05003352 }
3353
Mark Hounschellc43846a2014-03-19 11:10:51 -04003354 spin_lock_irqsave(&ch->ch_lock, lock_flags);
Mark Hounschella6792a32014-02-19 13:11:59 -05003355 un->un_flags &= ~(UN_EMPTY);
Mark Hounschellc43846a2014-03-19 11:10:51 -04003356 spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
Mark Hounschella6792a32014-02-19 13:11:59 -05003357
Mark Hounschellcf42c342014-02-28 12:42:09 -05003358 return ret;
Mark Hounschella6792a32014-02-19 13:11:59 -05003359}
3360
Mark Hounschella6792a32014-02-19 13:11:59 -05003361/*
3362 * dgap_maxcps_room
3363 *
3364 * Reduces bytes_available to the max number of characters
3365 * that can be sent currently given the maxcps value, and
3366 * returns the new bytes_available. This only affects printer
3367 * output.
3368 */
Daeseok Younbdf4d4f2014-07-10 12:26:04 +09003369static int dgap_maxcps_room(struct channel_t *ch, struct un_t *un,
3370 int bytes_available)
Mark Hounschella6792a32014-02-19 13:11:59 -05003371{
Mark Hounschella6792a32014-02-19 13:11:59 -05003372 /*
3373 * If its not the Transparent print device, return
3374 * the full data amount.
3375 */
3376 if (un->un_type != DGAP_PRINT)
Mark Hounschellcf42c342014-02-28 12:42:09 -05003377 return bytes_available;
Mark Hounschella6792a32014-02-19 13:11:59 -05003378
Mark Hounschellb115b022014-02-28 12:42:15 -05003379 if (ch->ch_digi.digi_maxcps > 0 && ch->ch_digi.digi_bufsize > 0) {
Mark Hounschella6792a32014-02-19 13:11:59 -05003380 int cps_limit = 0;
3381 unsigned long current_time = jiffies;
3382 unsigned long buffer_time = current_time +
Mark Hounschell6045b6a2014-03-06 13:02:45 -05003383 (HZ * ch->ch_digi.digi_bufsize) /
3384 ch->ch_digi.digi_maxcps;
Mark Hounschella6792a32014-02-19 13:11:59 -05003385
3386 if (ch->ch_cpstime < current_time) {
3387 /* buffer is empty */
Mark Hounschell8a2c9c42014-03-05 15:54:50 -05003388 ch->ch_cpstime = current_time; /* reset ch_cpstime */
Mark Hounschella6792a32014-02-19 13:11:59 -05003389 cps_limit = ch->ch_digi.digi_bufsize;
Mark Hounschell305ec872014-02-28 12:42:13 -05003390 } else if (ch->ch_cpstime < buffer_time) {
Mark Hounschella6792a32014-02-19 13:11:59 -05003391 /* still room in the buffer */
Mark Hounschell6045b6a2014-03-06 13:02:45 -05003392 cps_limit = ((buffer_time - ch->ch_cpstime) *
3393 ch->ch_digi.digi_maxcps) / HZ;
Mark Hounschell305ec872014-02-28 12:42:13 -05003394 } else {
Mark Hounschella6792a32014-02-19 13:11:59 -05003395 /* no room in the buffer */
3396 cps_limit = 0;
3397 }
3398
3399 bytes_available = min(cps_limit, bytes_available);
3400 }
3401
Mark Hounschellcf42c342014-02-28 12:42:09 -05003402 return bytes_available;
Mark Hounschella6792a32014-02-19 13:11:59 -05003403}
3404
Mark Hounschella6792a32014-02-19 13:11:59 -05003405static inline void dgap_set_firmware_event(struct un_t *un, unsigned int event)
3406{
Mark Hounschell7dfa3832014-05-23 10:14:02 -04003407 struct channel_t *ch;
3408 struct bs_t __iomem *bs;
Mark Hounschella6792a32014-02-19 13:11:59 -05003409
3410 if (!un || un->magic != DGAP_UNIT_MAGIC)
3411 return;
3412 ch = un->un_ch;
3413 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
3414 return;
Mark Hounschell7d6069d72014-02-28 12:42:10 -05003415 bs = ch->ch_bs;
Mark Hounschella6792a32014-02-19 13:11:59 -05003416 if (!bs)
3417 return;
3418
3419 if ((event & UN_LOW) != 0) {
3420 if ((un->un_flags & UN_LOW) == 0) {
3421 un->un_flags |= UN_LOW;
Ioana Ciornei44209c92015-10-21 23:17:55 +03003422 writeb(1, &bs->ilow);
Mark Hounschella6792a32014-02-19 13:11:59 -05003423 }
3424 }
3425 if ((event & UN_LOW) != 0) {
3426 if ((un->un_flags & UN_EMPTY) == 0) {
3427 un->un_flags |= UN_EMPTY;
Ioana Ciornei44209c92015-10-21 23:17:55 +03003428 writeb(1, &bs->iempty);
Mark Hounschella6792a32014-02-19 13:11:59 -05003429 }
3430 }
3431}
3432
Mark Hounschella6792a32014-02-19 13:11:59 -05003433/*
3434 * dgap_tty_write_room()
3435 *
3436 * Return space available in Tx buffer
3437 */
3438static int dgap_tty_write_room(struct tty_struct *tty)
3439{
Mark Hounschell7dfa3832014-05-23 10:14:02 -04003440 struct channel_t *ch;
3441 struct un_t *un;
Mark Hounschell405b26d2014-04-23 16:25:27 -04003442 struct bs_t __iomem *bs;
Mark Hounschella6792a32014-02-19 13:11:59 -05003443 u16 head, tail, tmask;
Mark Hounschell7dfa3832014-05-23 10:14:02 -04003444 int ret;
Mark Hounschell174efc12014-05-23 12:54:04 -04003445 ulong lock_flags = 0;
Mark Hounschella6792a32014-02-19 13:11:59 -05003446
Mark Hounschell61260222014-03-06 13:03:33 -05003447 if (!tty)
Mark Hounschellcf42c342014-02-28 12:42:09 -05003448 return 0;
Mark Hounschella6792a32014-02-19 13:11:59 -05003449
3450 un = tty->driver_data;
3451 if (!un || un->magic != DGAP_UNIT_MAGIC)
Mark Hounschellcf42c342014-02-28 12:42:09 -05003452 return 0;
Mark Hounschella6792a32014-02-19 13:11:59 -05003453
3454 ch = un->un_ch;
3455 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
Mark Hounschellcf42c342014-02-28 12:42:09 -05003456 return 0;
Mark Hounschella6792a32014-02-19 13:11:59 -05003457
Mark Hounschell7d6069d72014-02-28 12:42:10 -05003458 bs = ch->ch_bs;
Mark Hounschella6792a32014-02-19 13:11:59 -05003459 if (!bs)
Mark Hounschellcf42c342014-02-28 12:42:09 -05003460 return 0;
Mark Hounschella6792a32014-02-19 13:11:59 -05003461
Mark Hounschellc43846a2014-03-19 11:10:51 -04003462 spin_lock_irqsave(&ch->ch_lock, lock_flags);
Mark Hounschella6792a32014-02-19 13:11:59 -05003463
3464 tmask = ch->ch_tsize - 1;
Ioana Ciornei44209c92015-10-21 23:17:55 +03003465 head = readw(&bs->tx_head) & tmask;
3466 tail = readw(&bs->tx_tail) & tmask;
Mark Hounschella6792a32014-02-19 13:11:59 -05003467
Mark Hounschell7d6069d72014-02-28 12:42:10 -05003468 ret = tail - head - 1;
3469 if (ret < 0)
3470 ret += ch->ch_tsize;
Mark Hounschella6792a32014-02-19 13:11:59 -05003471
3472 /* Limit printer to maxcps */
Daeseok Younbdf4d4f2014-07-10 12:26:04 +09003473 ret = dgap_maxcps_room(ch, un, ret);
Mark Hounschella6792a32014-02-19 13:11:59 -05003474
3475 /*
3476 * If we are printer device, leave space for
3477 * possibly both the on and off strings.
3478 */
3479 if (un->un_type == DGAP_PRINT) {
3480 if (!(ch->ch_flags & CH_PRON))
3481 ret -= ch->ch_digi.digi_onlen;
3482 ret -= ch->ch_digi.digi_offlen;
Mark Hounschell305ec872014-02-28 12:42:13 -05003483 } else {
Mark Hounschella6792a32014-02-19 13:11:59 -05003484 if (ch->ch_flags & CH_PRON)
3485 ret -= ch->ch_digi.digi_offlen;
3486 }
3487
3488 if (ret < 0)
3489 ret = 0;
3490
3491 /*
3492 * Schedule FEP to wake us up if needed.
3493 *
3494 * TODO: This might be overkill...
3495 * Do we really need to schedule callbacks from the FEP
3496 * in every case? Can we get smarter based on ret?
3497 */
3498 dgap_set_firmware_event(un, UN_LOW | UN_EMPTY);
Mark Hounschellc43846a2014-03-19 11:10:51 -04003499 spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
Mark Hounschella6792a32014-02-19 13:11:59 -05003500
Mark Hounschellcf42c342014-02-28 12:42:09 -05003501 return ret;
Mark Hounschella6792a32014-02-19 13:11:59 -05003502}
3503
Mark Hounschella6792a32014-02-19 13:11:59 -05003504/*
Mark Hounschella6792a32014-02-19 13:11:59 -05003505 * dgap_tty_write()
3506 *
3507 * Take data from the user or kernel and send it out to the FEP.
3508 * In here exists all the Transparent Print magic as well.
3509 */
Mark Hounschell8a2c9c42014-03-05 15:54:50 -05003510static int dgap_tty_write(struct tty_struct *tty, const unsigned char *buf,
Ioana Ciornei629c7342015-10-21 23:17:53 +03003511 int count)
Mark Hounschella6792a32014-02-19 13:11:59 -05003512{
Mark Hounschell7dfa3832014-05-23 10:14:02 -04003513 struct channel_t *ch;
3514 struct un_t *un;
Mark Hounschell405b26d2014-04-23 16:25:27 -04003515 struct bs_t __iomem *bs;
Mark Hounschell630b2ab2014-04-24 09:22:12 -04003516 char __iomem *vaddr;
Mark Hounschella6792a32014-02-19 13:11:59 -05003517 u16 head, tail, tmask, remain;
Mark Hounschell7dfa3832014-05-23 10:14:02 -04003518 int bufcount, n;
Mark Hounschella6792a32014-02-19 13:11:59 -05003519 ulong lock_flags;
Mark Hounschella6792a32014-02-19 13:11:59 -05003520
Mark Hounschell61260222014-03-06 13:03:33 -05003521 if (!tty)
Mark Hounschellcf42c342014-02-28 12:42:09 -05003522 return 0;
Mark Hounschella6792a32014-02-19 13:11:59 -05003523
3524 un = tty->driver_data;
3525 if (!un || un->magic != DGAP_UNIT_MAGIC)
Mark Hounschellcf42c342014-02-28 12:42:09 -05003526 return 0;
Mark Hounschella6792a32014-02-19 13:11:59 -05003527
3528 ch = un->un_ch;
3529 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
Mark Hounschellcf42c342014-02-28 12:42:09 -05003530 return 0;
Mark Hounschella6792a32014-02-19 13:11:59 -05003531
Mark Hounschell7d6069d72014-02-28 12:42:10 -05003532 bs = ch->ch_bs;
Mark Hounschella6792a32014-02-19 13:11:59 -05003533 if (!bs)
Mark Hounschellcf42c342014-02-28 12:42:09 -05003534 return 0;
Mark Hounschella6792a32014-02-19 13:11:59 -05003535
3536 if (!count)
Mark Hounschellcf42c342014-02-28 12:42:09 -05003537 return 0;
Mark Hounschella6792a32014-02-19 13:11:59 -05003538
Mark Hounschellc43846a2014-03-19 11:10:51 -04003539 spin_lock_irqsave(&ch->ch_lock, lock_flags);
Mark Hounschella6792a32014-02-19 13:11:59 -05003540
3541 /* Get our space available for the channel from the board */
3542 tmask = ch->ch_tsize - 1;
3543 head = readw(&(bs->tx_head)) & tmask;
3544 tail = readw(&(bs->tx_tail)) & tmask;
3545
Mark Hounschelld1c3c6f2014-02-28 15:48:44 -05003546 bufcount = tail - head - 1;
3547 if (bufcount < 0)
Mark Hounschella6792a32014-02-19 13:11:59 -05003548 bufcount += ch->ch_tsize;
3549
Mark Hounschella6792a32014-02-19 13:11:59 -05003550 /*
3551 * Limit printer output to maxcps overall, with bursts allowed
3552 * up to bufsize characters.
3553 */
Daeseok Younbdf4d4f2014-07-10 12:26:04 +09003554 bufcount = dgap_maxcps_room(ch, un, bufcount);
Mark Hounschella6792a32014-02-19 13:11:59 -05003555
3556 /*
3557 * Take minimum of what the user wants to send, and the
3558 * space available in the FEP buffer.
3559 */
3560 count = min(count, bufcount);
3561
3562 /*
3563 * Bail if no space left.
3564 */
3565 if (count <= 0) {
3566 dgap_set_firmware_event(un, UN_LOW | UN_EMPTY);
Mark Hounschellc43846a2014-03-19 11:10:51 -04003567 spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
Mark Hounschellcf42c342014-02-28 12:42:09 -05003568 return 0;
Mark Hounschella6792a32014-02-19 13:11:59 -05003569 }
3570
3571 /*
3572 * Output the printer ON string, if we are in terminal mode, but
3573 * need to be in printer mode.
3574 */
3575 if ((un->un_type == DGAP_PRINT) && !(ch->ch_flags & CH_PRON)) {
3576 dgap_wmove(ch, ch->ch_digi.digi_onstr,
Ioana Ciornei629c7342015-10-21 23:17:53 +03003577 (int)ch->ch_digi.digi_onlen);
Ioana Ciornei44209c92015-10-21 23:17:55 +03003578 head = readw(&bs->tx_head) & tmask;
Mark Hounschella6792a32014-02-19 13:11:59 -05003579 ch->ch_flags |= CH_PRON;
3580 }
3581
3582 /*
3583 * On the other hand, output the printer OFF string, if we are
3584 * currently in printer mode, but need to output to the terminal.
3585 */
3586 if ((un->un_type != DGAP_PRINT) && (ch->ch_flags & CH_PRON)) {
3587 dgap_wmove(ch, ch->ch_digi.digi_offstr,
Ioana Ciornei629c7342015-10-21 23:17:53 +03003588 (int)ch->ch_digi.digi_offlen);
Ioana Ciornei44209c92015-10-21 23:17:55 +03003589 head = readw(&bs->tx_head) & tmask;
Mark Hounschella6792a32014-02-19 13:11:59 -05003590 ch->ch_flags &= ~CH_PRON;
3591 }
3592
Mark Hounschella6792a32014-02-19 13:11:59 -05003593 n = count;
3594
3595 /*
3596 * If the write wraps over the top of the circular buffer,
3597 * move the portion up to the wrap point, and reset the
3598 * pointers to the bottom.
3599 */
3600 remain = ch->ch_tstart + ch->ch_tsize - head;
3601
3602 if (n >= remain) {
3603 n -= remain;
3604 vaddr = ch->ch_taddr + head;
3605
Ioana Ciornei18122552015-10-21 23:17:54 +03003606 memcpy_toio(vaddr, (u8 *)buf, remain);
Mark Hounschella6792a32014-02-19 13:11:59 -05003607
3608 head = ch->ch_tstart;
3609 buf += remain;
3610 }
3611
3612 if (n > 0) {
Mark Hounschella6792a32014-02-19 13:11:59 -05003613 /*
3614 * Move rest of data.
3615 */
3616 vaddr = ch->ch_taddr + head;
3617 remain = n;
3618
Ioana Ciornei18122552015-10-21 23:17:54 +03003619 memcpy_toio(vaddr, (u8 *)buf, remain);
Mark Hounschella6792a32014-02-19 13:11:59 -05003620 head += remain;
Mark Hounschella6792a32014-02-19 13:11:59 -05003621 }
3622
3623 if (count) {
3624 ch->ch_txcount += count;
3625 head &= tmask;
Ioana Ciornei44209c92015-10-21 23:17:55 +03003626 writew(head, &bs->tx_head);
Mark Hounschella6792a32014-02-19 13:11:59 -05003627 }
3628
Mark Hounschella6792a32014-02-19 13:11:59 -05003629 dgap_set_firmware_event(un, UN_LOW | UN_EMPTY);
3630
3631 /*
3632 * If this is the print device, and the
3633 * printer is still on, we need to turn it
3634 * off before going idle. If the buffer is
3635 * non-empty, wait until it goes empty.
3636 * Otherwise turn it off right now.
3637 */
3638 if ((un->un_type == DGAP_PRINT) && (ch->ch_flags & CH_PRON)) {
Ioana Ciornei44209c92015-10-21 23:17:55 +03003639 tail = readw(&bs->tx_tail) & tmask;
Mark Hounschella6792a32014-02-19 13:11:59 -05003640
3641 if (tail != head) {
3642 un->un_flags |= UN_EMPTY;
Ioana Ciornei44209c92015-10-21 23:17:55 +03003643 writeb(1, &bs->iempty);
Mark Hounschell305ec872014-02-28 12:42:13 -05003644 } else {
Mark Hounschella6792a32014-02-19 13:11:59 -05003645 dgap_wmove(ch, ch->ch_digi.digi_offstr,
Ioana Ciornei629c7342015-10-21 23:17:53 +03003646 (int)ch->ch_digi.digi_offlen);
Ioana Ciornei44209c92015-10-21 23:17:55 +03003647 head = readw(&bs->tx_head) & tmask;
Mark Hounschella6792a32014-02-19 13:11:59 -05003648 ch->ch_flags &= ~CH_PRON;
3649 }
3650 }
3651
3652 /* Update printer buffer empty time. */
3653 if ((un->un_type == DGAP_PRINT) && (ch->ch_digi.digi_maxcps > 0)
3654 && (ch->ch_digi.digi_bufsize > 0)) {
Mark Hounschell7d6069d72014-02-28 12:42:10 -05003655 ch->ch_cpstime += (HZ * count) / ch->ch_digi.digi_maxcps;
Mark Hounschella6792a32014-02-19 13:11:59 -05003656 }
3657
Mark Hounschellc43846a2014-03-19 11:10:51 -04003658 spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
Mark Hounschella6792a32014-02-19 13:11:59 -05003659
Mark Hounschellcf42c342014-02-28 12:42:09 -05003660 return count;
Mark Hounschella6792a32014-02-19 13:11:59 -05003661}
3662
Mark Hounschella6792a32014-02-19 13:11:59 -05003663/*
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09003664 * dgap_tty_put_char()
3665 *
3666 * Put a character into ch->ch_buf
3667 *
3668 * - used by the line discipline for OPOST processing
3669 */
3670static int dgap_tty_put_char(struct tty_struct *tty, unsigned char c)
3671{
3672 /*
3673 * Simply call tty_write.
3674 */
3675 dgap_tty_write(tty, &c, 1);
3676 return 1;
3677}
3678
3679/*
Mark Hounschella6792a32014-02-19 13:11:59 -05003680 * Return modem signals to ld.
3681 */
3682static int dgap_tty_tiocmget(struct tty_struct *tty)
3683{
3684 struct channel_t *ch;
3685 struct un_t *un;
Mark Hounschell7dfa3832014-05-23 10:14:02 -04003686 int result;
3687 u8 mstat;
Mark Hounschella6792a32014-02-19 13:11:59 -05003688 ulong lock_flags;
3689
3690 if (!tty || tty->magic != TTY_MAGIC)
Mark Hounschell7dfa3832014-05-23 10:14:02 -04003691 return -EIO;
Mark Hounschella6792a32014-02-19 13:11:59 -05003692
3693 un = tty->driver_data;
3694 if (!un || un->magic != DGAP_UNIT_MAGIC)
Mark Hounschell7dfa3832014-05-23 10:14:02 -04003695 return -EIO;
Mark Hounschella6792a32014-02-19 13:11:59 -05003696
3697 ch = un->un_ch;
3698 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
Mark Hounschell7dfa3832014-05-23 10:14:02 -04003699 return -EIO;
Mark Hounschella6792a32014-02-19 13:11:59 -05003700
Mark Hounschellc43846a2014-03-19 11:10:51 -04003701 spin_lock_irqsave(&ch->ch_lock, lock_flags);
Mark Hounschella6792a32014-02-19 13:11:59 -05003702
Ioana Ciornei44209c92015-10-21 23:17:55 +03003703 mstat = readb(&ch->ch_bs->m_stat);
Mark Hounschell7d6069d72014-02-28 12:42:10 -05003704 /* Append any outbound signals that might be pending... */
3705 mstat |= ch->ch_mostat;
Mark Hounschella6792a32014-02-19 13:11:59 -05003706
Mark Hounschellc43846a2014-03-19 11:10:51 -04003707 spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
Mark Hounschella6792a32014-02-19 13:11:59 -05003708
3709 result = 0;
3710
3711 if (mstat & D_DTR(ch))
3712 result |= TIOCM_DTR;
3713 if (mstat & D_RTS(ch))
3714 result |= TIOCM_RTS;
3715 if (mstat & D_CTS(ch))
3716 result |= TIOCM_CTS;
3717 if (mstat & D_DSR(ch))
3718 result |= TIOCM_DSR;
3719 if (mstat & D_RI(ch))
3720 result |= TIOCM_RI;
3721 if (mstat & D_CD(ch))
3722 result |= TIOCM_CD;
3723
Mark Hounschella6792a32014-02-19 13:11:59 -05003724 return result;
3725}
3726
Mark Hounschella6792a32014-02-19 13:11:59 -05003727/*
3728 * dgap_tty_tiocmset()
3729 *
3730 * Set modem signals, called by ld.
3731 */
Mark Hounschella6792a32014-02-19 13:11:59 -05003732static int dgap_tty_tiocmset(struct tty_struct *tty,
Ioana Ciornei629c7342015-10-21 23:17:53 +03003733 unsigned int set, unsigned int clear)
Mark Hounschella6792a32014-02-19 13:11:59 -05003734{
3735 struct board_t *bd;
3736 struct channel_t *ch;
3737 struct un_t *un;
Mark Hounschella6792a32014-02-19 13:11:59 -05003738 ulong lock_flags;
3739 ulong lock_flags2;
3740
3741 if (!tty || tty->magic != TTY_MAGIC)
Mark Hounschell7dfa3832014-05-23 10:14:02 -04003742 return -EIO;
Mark Hounschella6792a32014-02-19 13:11:59 -05003743
3744 un = tty->driver_data;
3745 if (!un || un->magic != DGAP_UNIT_MAGIC)
Mark Hounschell7dfa3832014-05-23 10:14:02 -04003746 return -EIO;
Mark Hounschella6792a32014-02-19 13:11:59 -05003747
3748 ch = un->un_ch;
3749 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
Mark Hounschell7dfa3832014-05-23 10:14:02 -04003750 return -EIO;
Mark Hounschella6792a32014-02-19 13:11:59 -05003751
3752 bd = ch->ch_bd;
3753 if (!bd || bd->magic != DGAP_BOARD_MAGIC)
Mark Hounschell7dfa3832014-05-23 10:14:02 -04003754 return -EIO;
Mark Hounschella6792a32014-02-19 13:11:59 -05003755
Mark Hounschellc43846a2014-03-19 11:10:51 -04003756 spin_lock_irqsave(&bd->bd_lock, lock_flags);
3757 spin_lock_irqsave(&ch->ch_lock, lock_flags2);
Mark Hounschella6792a32014-02-19 13:11:59 -05003758
3759 if (set & TIOCM_RTS) {
3760 ch->ch_mforce |= D_RTS(ch);
3761 ch->ch_mval |= D_RTS(ch);
Mark Hounschell7d6069d72014-02-28 12:42:10 -05003762 }
Mark Hounschella6792a32014-02-19 13:11:59 -05003763
3764 if (set & TIOCM_DTR) {
3765 ch->ch_mforce |= D_DTR(ch);
3766 ch->ch_mval |= D_DTR(ch);
Mark Hounschell7d6069d72014-02-28 12:42:10 -05003767 }
Mark Hounschella6792a32014-02-19 13:11:59 -05003768
3769 if (clear & TIOCM_RTS) {
3770 ch->ch_mforce |= D_RTS(ch);
3771 ch->ch_mval &= ~(D_RTS(ch));
Mark Hounschell7d6069d72014-02-28 12:42:10 -05003772 }
Mark Hounschella6792a32014-02-19 13:11:59 -05003773
3774 if (clear & TIOCM_DTR) {
3775 ch->ch_mforce |= D_DTR(ch);
3776 ch->ch_mval &= ~(D_DTR(ch));
Mark Hounschell7d6069d72014-02-28 12:42:10 -05003777 }
Mark Hounschella6792a32014-02-19 13:11:59 -05003778
Daeseok Younab6cdcb2014-07-11 19:22:39 +09003779 dgap_param(ch, bd, un->un_type);
Mark Hounschella6792a32014-02-19 13:11:59 -05003780
Mark Hounschellc43846a2014-03-19 11:10:51 -04003781 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
3782 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
Mark Hounschella6792a32014-02-19 13:11:59 -05003783
Mark Hounschellcf42c342014-02-28 12:42:09 -05003784 return 0;
Mark Hounschella6792a32014-02-19 13:11:59 -05003785}
3786
Mark Hounschella6792a32014-02-19 13:11:59 -05003787/*
3788 * dgap_tty_send_break()
3789 *
3790 * Send a Break, called by ld.
3791 */
3792static int dgap_tty_send_break(struct tty_struct *tty, int msec)
3793{
3794 struct board_t *bd;
3795 struct channel_t *ch;
3796 struct un_t *un;
Mark Hounschella6792a32014-02-19 13:11:59 -05003797 ulong lock_flags;
3798 ulong lock_flags2;
3799
3800 if (!tty || tty->magic != TTY_MAGIC)
Mark Hounschell7dfa3832014-05-23 10:14:02 -04003801 return -EIO;
Mark Hounschella6792a32014-02-19 13:11:59 -05003802
3803 un = tty->driver_data;
3804 if (!un || un->magic != DGAP_UNIT_MAGIC)
Mark Hounschell7dfa3832014-05-23 10:14:02 -04003805 return -EIO;
Mark Hounschella6792a32014-02-19 13:11:59 -05003806
3807 ch = un->un_ch;
3808 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
Mark Hounschell7dfa3832014-05-23 10:14:02 -04003809 return -EIO;
Mark Hounschella6792a32014-02-19 13:11:59 -05003810
3811 bd = ch->ch_bd;
3812 if (!bd || bd->magic != DGAP_BOARD_MAGIC)
Mark Hounschell7dfa3832014-05-23 10:14:02 -04003813 return -EIO;
Mark Hounschella6792a32014-02-19 13:11:59 -05003814
3815 switch (msec) {
3816 case -1:
3817 msec = 0xFFFF;
3818 break;
3819 case 0:
3820 msec = 1;
3821 break;
3822 default:
3823 msec /= 10;
3824 break;
3825 }
3826
Mark Hounschellc43846a2014-03-19 11:10:51 -04003827 spin_lock_irqsave(&bd->bd_lock, lock_flags);
3828 spin_lock_irqsave(&ch->ch_lock, lock_flags2);
Mark Hounschella6792a32014-02-19 13:11:59 -05003829#if 0
3830 dgap_cmdw(ch, SBREAK, (u16) SBREAK_TIME, 0);
3831#endif
Ioana Ciornei18122552015-10-21 23:17:54 +03003832 dgap_cmdw(ch, SBREAK, (u16)msec, 0);
Mark Hounschella6792a32014-02-19 13:11:59 -05003833
Mark Hounschellc43846a2014-03-19 11:10:51 -04003834 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
3835 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
Mark Hounschella6792a32014-02-19 13:11:59 -05003836
Mark Hounschellcf42c342014-02-28 12:42:09 -05003837 return 0;
Mark Hounschella6792a32014-02-19 13:11:59 -05003838}
3839
Mark Hounschella6792a32014-02-19 13:11:59 -05003840/*
3841 * dgap_tty_wait_until_sent()
3842 *
3843 * wait until data has been transmitted, called by ld.
3844 */
3845static void dgap_tty_wait_until_sent(struct tty_struct *tty, int timeout)
3846{
Mark Hounschell31960c12014-02-28 12:42:08 -05003847 dgap_wait_for_drain(tty);
Mark Hounschella6792a32014-02-19 13:11:59 -05003848}
3849
Mark Hounschella6792a32014-02-19 13:11:59 -05003850/*
3851 * dgap_send_xchar()
3852 *
3853 * send a high priority character, called by ld.
3854 */
3855static void dgap_tty_send_xchar(struct tty_struct *tty, char c)
3856{
3857 struct board_t *bd;
3858 struct channel_t *ch;
3859 struct un_t *un;
3860 ulong lock_flags;
3861 ulong lock_flags2;
3862
3863 if (!tty || tty->magic != TTY_MAGIC)
3864 return;
3865
3866 un = tty->driver_data;
3867 if (!un || un->magic != DGAP_UNIT_MAGIC)
3868 return;
3869
3870 ch = un->un_ch;
3871 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
3872 return;
3873
3874 bd = ch->ch_bd;
3875 if (!bd || bd->magic != DGAP_BOARD_MAGIC)
3876 return;
3877
Mark Hounschellc43846a2014-03-19 11:10:51 -04003878 spin_lock_irqsave(&bd->bd_lock, lock_flags);
3879 spin_lock_irqsave(&ch->ch_lock, lock_flags2);
Mark Hounschella6792a32014-02-19 13:11:59 -05003880
3881 /*
3882 * This is technically what we should do.
3883 * However, the NIST tests specifically want
3884 * to see each XON or XOFF character that it
3885 * sends, so lets just send each character
3886 * by hand...
3887 */
3888#if 0
Mark Hounschell305ec872014-02-28 12:42:13 -05003889 if (c == STOP_CHAR(tty))
Mark Hounschella6792a32014-02-19 13:11:59 -05003890 dgap_cmdw(ch, RPAUSE, 0, 0);
Mark Hounschell305ec872014-02-28 12:42:13 -05003891 else if (c == START_CHAR(tty))
Mark Hounschella6792a32014-02-19 13:11:59 -05003892 dgap_cmdw(ch, RRESUME, 0, 0);
Mark Hounschell305ec872014-02-28 12:42:13 -05003893 else
Mark Hounschella6792a32014-02-19 13:11:59 -05003894 dgap_wmove(ch, &c, 1);
Mark Hounschella6792a32014-02-19 13:11:59 -05003895#else
3896 dgap_wmove(ch, &c, 1);
3897#endif
3898
Mark Hounschellc43846a2014-03-19 11:10:51 -04003899 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
3900 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
Mark Hounschella6792a32014-02-19 13:11:59 -05003901}
3902
Mark Hounschella6792a32014-02-19 13:11:59 -05003903/*
3904 * Return modem signals to ld.
3905 */
3906static int dgap_get_modem_info(struct channel_t *ch, unsigned int __user *value)
3907{
Mark Hounschell7dfa3832014-05-23 10:14:02 -04003908 int result;
3909 u8 mstat;
Mark Hounschella6792a32014-02-19 13:11:59 -05003910 ulong lock_flags;
Mark Hounschella6792a32014-02-19 13:11:59 -05003911
Mark Hounschellc43846a2014-03-19 11:10:51 -04003912 spin_lock_irqsave(&ch->ch_lock, lock_flags);
Mark Hounschella6792a32014-02-19 13:11:59 -05003913
Ioana Ciornei44209c92015-10-21 23:17:55 +03003914 mstat = readb(&ch->ch_bs->m_stat);
Mark Hounschella6792a32014-02-19 13:11:59 -05003915 /* Append any outbound signals that might be pending... */
3916 mstat |= ch->ch_mostat;
3917
Mark Hounschellc43846a2014-03-19 11:10:51 -04003918 spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
Mark Hounschella6792a32014-02-19 13:11:59 -05003919
3920 result = 0;
3921
3922 if (mstat & D_DTR(ch))
3923 result |= TIOCM_DTR;
3924 if (mstat & D_RTS(ch))
3925 result |= TIOCM_RTS;
3926 if (mstat & D_CTS(ch))
3927 result |= TIOCM_CTS;
3928 if (mstat & D_DSR(ch))
3929 result |= TIOCM_DSR;
3930 if (mstat & D_RI(ch))
3931 result |= TIOCM_RI;
3932 if (mstat & D_CD(ch))
3933 result |= TIOCM_CD;
3934
Aya Mahfouze20af8a2015-02-27 15:11:02 +02003935 return put_user(result, value);
Mark Hounschella6792a32014-02-19 13:11:59 -05003936}
3937
Mark Hounschella6792a32014-02-19 13:11:59 -05003938/*
3939 * dgap_set_modem_info()
3940 *
3941 * Set modem signals, called by ld.
3942 */
Piotr Witoslawski70c0ed92014-09-04 08:18:53 +02003943static int dgap_set_modem_info(struct channel_t *ch, struct board_t *bd,
3944 struct un_t *un, unsigned int command,
3945 unsigned int __user *value)
Mark Hounschella6792a32014-02-19 13:11:59 -05003946{
Mark Hounschell7dfa3832014-05-23 10:14:02 -04003947 int ret;
3948 unsigned int arg;
Mark Hounschella6792a32014-02-19 13:11:59 -05003949 ulong lock_flags;
3950 ulong lock_flags2;
3951
Mark Hounschella6792a32014-02-19 13:11:59 -05003952 ret = get_user(arg, value);
Mark Hounschell31960c12014-02-28 12:42:08 -05003953 if (ret)
Mark Hounschellcf42c342014-02-28 12:42:09 -05003954 return ret;
Mark Hounschella6792a32014-02-19 13:11:59 -05003955
3956 switch (command) {
3957 case TIOCMBIS:
3958 if (arg & TIOCM_RTS) {
3959 ch->ch_mforce |= D_RTS(ch);
3960 ch->ch_mval |= D_RTS(ch);
Mark Hounschell7d6069d72014-02-28 12:42:10 -05003961 }
Mark Hounschella6792a32014-02-19 13:11:59 -05003962
3963 if (arg & TIOCM_DTR) {
3964 ch->ch_mforce |= D_DTR(ch);
3965 ch->ch_mval |= D_DTR(ch);
Mark Hounschell7d6069d72014-02-28 12:42:10 -05003966 }
Mark Hounschella6792a32014-02-19 13:11:59 -05003967
3968 break;
3969
3970 case TIOCMBIC:
3971 if (arg & TIOCM_RTS) {
3972 ch->ch_mforce |= D_RTS(ch);
3973 ch->ch_mval &= ~(D_RTS(ch));
Mark Hounschell7d6069d72014-02-28 12:42:10 -05003974 }
Mark Hounschella6792a32014-02-19 13:11:59 -05003975
3976 if (arg & TIOCM_DTR) {
3977 ch->ch_mforce |= D_DTR(ch);
3978 ch->ch_mval &= ~(D_DTR(ch));
Mark Hounschell7d6069d72014-02-28 12:42:10 -05003979 }
Mark Hounschella6792a32014-02-19 13:11:59 -05003980
3981 break;
3982
Mark Hounschell7d6069d72014-02-28 12:42:10 -05003983 case TIOCMSET:
Ioana Ciorneia9108b72015-10-21 23:17:56 +03003984 ch->ch_mforce = D_DTR(ch) | D_RTS(ch);
Mark Hounschella6792a32014-02-19 13:11:59 -05003985
Mark Hounschell305ec872014-02-28 12:42:13 -05003986 if (arg & TIOCM_RTS)
Mark Hounschella6792a32014-02-19 13:11:59 -05003987 ch->ch_mval |= D_RTS(ch);
Mark Hounschell305ec872014-02-28 12:42:13 -05003988 else
Mark Hounschella6792a32014-02-19 13:11:59 -05003989 ch->ch_mval &= ~(D_RTS(ch));
Mark Hounschella6792a32014-02-19 13:11:59 -05003990
Mark Hounschell305ec872014-02-28 12:42:13 -05003991 if (arg & TIOCM_DTR)
Mark Hounschella6792a32014-02-19 13:11:59 -05003992 ch->ch_mval |= (D_DTR(ch));
Mark Hounschell305ec872014-02-28 12:42:13 -05003993 else
Mark Hounschella6792a32014-02-19 13:11:59 -05003994 ch->ch_mval &= ~(D_DTR(ch));
Mark Hounschella6792a32014-02-19 13:11:59 -05003995
3996 break;
3997
3998 default:
Mark Hounschellcf42c342014-02-28 12:42:09 -05003999 return -EINVAL;
Mark Hounschella6792a32014-02-19 13:11:59 -05004000 }
4001
Mark Hounschellc43846a2014-03-19 11:10:51 -04004002 spin_lock_irqsave(&bd->bd_lock, lock_flags);
4003 spin_lock_irqsave(&ch->ch_lock, lock_flags2);
Mark Hounschella6792a32014-02-19 13:11:59 -05004004
Daeseok Younab6cdcb2014-07-11 19:22:39 +09004005 dgap_param(ch, bd, un->un_type);
Mark Hounschella6792a32014-02-19 13:11:59 -05004006
Mark Hounschellc43846a2014-03-19 11:10:51 -04004007 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
4008 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
Mark Hounschella6792a32014-02-19 13:11:59 -05004009
Mark Hounschellcf42c342014-02-28 12:42:09 -05004010 return 0;
Mark Hounschella6792a32014-02-19 13:11:59 -05004011}
4012
Mark Hounschella6792a32014-02-19 13:11:59 -05004013/*
4014 * dgap_tty_digigeta()
4015 *
4016 * Ioctl to get the information for ditty.
4017 *
4018 *
4019 *
4020 */
Piotr Witoslawski70c0ed92014-09-04 08:18:53 +02004021static int dgap_tty_digigeta(struct channel_t *ch,
4022 struct digi_t __user *retinfo)
Mark Hounschella6792a32014-02-19 13:11:59 -05004023{
Mark Hounschella6792a32014-02-19 13:11:59 -05004024 struct digi_t tmp;
4025 ulong lock_flags;
4026
4027 if (!retinfo)
Mark Hounschellcf42c342014-02-28 12:42:09 -05004028 return -EFAULT;
Mark Hounschella6792a32014-02-19 13:11:59 -05004029
Mark Hounschella6792a32014-02-19 13:11:59 -05004030 memset(&tmp, 0, sizeof(tmp));
4031
Mark Hounschellc43846a2014-03-19 11:10:51 -04004032 spin_lock_irqsave(&ch->ch_lock, lock_flags);
Mark Hounschella6792a32014-02-19 13:11:59 -05004033 memcpy(&tmp, &ch->ch_digi, sizeof(tmp));
Mark Hounschellc43846a2014-03-19 11:10:51 -04004034 spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
Mark Hounschella6792a32014-02-19 13:11:59 -05004035
4036 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
Mark Hounschellcf42c342014-02-28 12:42:09 -05004037 return -EFAULT;
Mark Hounschella6792a32014-02-19 13:11:59 -05004038
Mark Hounschellcf42c342014-02-28 12:42:09 -05004039 return 0;
Mark Hounschella6792a32014-02-19 13:11:59 -05004040}
4041
Mark Hounschella6792a32014-02-19 13:11:59 -05004042/*
4043 * dgap_tty_digiseta()
4044 *
4045 * Ioctl to set the information for ditty.
4046 *
4047 *
4048 *
4049 */
Daeseok Younffc11c12014-07-11 19:22:58 +09004050static int dgap_tty_digiseta(struct channel_t *ch, struct board_t *bd,
4051 struct un_t *un, struct digi_t __user *new_info)
Mark Hounschella6792a32014-02-19 13:11:59 -05004052{
Mark Hounschella6792a32014-02-19 13:11:59 -05004053 struct digi_t new_digi;
Mark Hounschell174efc12014-05-23 12:54:04 -04004054 ulong lock_flags = 0;
Mark Hounschella6792a32014-02-19 13:11:59 -05004055 unsigned long lock_flags2;
4056
Mark Hounschell31960c12014-02-28 12:42:08 -05004057 if (copy_from_user(&new_digi, new_info, sizeof(struct digi_t)))
4058 return -EFAULT;
Mark Hounschella6792a32014-02-19 13:11:59 -05004059
Mark Hounschellc43846a2014-03-19 11:10:51 -04004060 spin_lock_irqsave(&bd->bd_lock, lock_flags);
4061 spin_lock_irqsave(&ch->ch_lock, lock_flags2);
Mark Hounschella6792a32014-02-19 13:11:59 -05004062
4063 memcpy(&ch->ch_digi, &new_digi, sizeof(struct digi_t));
4064
4065 if (ch->ch_digi.digi_maxcps < 1)
4066 ch->ch_digi.digi_maxcps = 1;
4067
4068 if (ch->ch_digi.digi_maxcps > 10000)
4069 ch->ch_digi.digi_maxcps = 10000;
4070
4071 if (ch->ch_digi.digi_bufsize < 10)
4072 ch->ch_digi.digi_bufsize = 10;
4073
4074 if (ch->ch_digi.digi_maxchar < 1)
4075 ch->ch_digi.digi_maxchar = 1;
4076
4077 if (ch->ch_digi.digi_maxchar > ch->ch_digi.digi_bufsize)
4078 ch->ch_digi.digi_maxchar = ch->ch_digi.digi_bufsize;
4079
4080 if (ch->ch_digi.digi_onlen > DIGI_PLEN)
4081 ch->ch_digi.digi_onlen = DIGI_PLEN;
4082
4083 if (ch->ch_digi.digi_offlen > DIGI_PLEN)
4084 ch->ch_digi.digi_offlen = DIGI_PLEN;
4085
Daeseok Younab6cdcb2014-07-11 19:22:39 +09004086 dgap_param(ch, bd, un->un_type);
Mark Hounschella6792a32014-02-19 13:11:59 -05004087
Mark Hounschellc43846a2014-03-19 11:10:51 -04004088 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
4089 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
Mark Hounschella6792a32014-02-19 13:11:59 -05004090
Mark Hounschellcf42c342014-02-28 12:42:09 -05004091 return 0;
Mark Hounschella6792a32014-02-19 13:11:59 -05004092}
4093
Mark Hounschella6792a32014-02-19 13:11:59 -05004094/*
4095 * dgap_tty_digigetedelay()
4096 *
4097 * Ioctl to get the current edelay setting.
4098 *
4099 *
4100 *
4101 */
4102static int dgap_tty_digigetedelay(struct tty_struct *tty, int __user *retinfo)
4103{
4104 struct channel_t *ch;
4105 struct un_t *un;
4106 int tmp;
4107 ulong lock_flags;
4108
4109 if (!retinfo)
Mark Hounschellcf42c342014-02-28 12:42:09 -05004110 return -EFAULT;
Mark Hounschella6792a32014-02-19 13:11:59 -05004111
4112 if (!tty || tty->magic != TTY_MAGIC)
Mark Hounschellcf42c342014-02-28 12:42:09 -05004113 return -EFAULT;
Mark Hounschella6792a32014-02-19 13:11:59 -05004114
4115 un = tty->driver_data;
4116 if (!un || un->magic != DGAP_UNIT_MAGIC)
Mark Hounschellcf42c342014-02-28 12:42:09 -05004117 return -EFAULT;
Mark Hounschella6792a32014-02-19 13:11:59 -05004118
4119 ch = un->un_ch;
4120 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
Mark Hounschellcf42c342014-02-28 12:42:09 -05004121 return -EFAULT;
Mark Hounschella6792a32014-02-19 13:11:59 -05004122
4123 memset(&tmp, 0, sizeof(tmp));
4124
Mark Hounschellc43846a2014-03-19 11:10:51 -04004125 spin_lock_irqsave(&ch->ch_lock, lock_flags);
Ioana Ciornei44209c92015-10-21 23:17:55 +03004126 tmp = readw(&ch->ch_bs->edelay);
Mark Hounschellc43846a2014-03-19 11:10:51 -04004127 spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
Mark Hounschella6792a32014-02-19 13:11:59 -05004128
4129 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
Mark Hounschellcf42c342014-02-28 12:42:09 -05004130 return -EFAULT;
Mark Hounschella6792a32014-02-19 13:11:59 -05004131
Mark Hounschellcf42c342014-02-28 12:42:09 -05004132 return 0;
Mark Hounschella6792a32014-02-19 13:11:59 -05004133}
4134
Mark Hounschella6792a32014-02-19 13:11:59 -05004135/*
4136 * dgap_tty_digisetedelay()
4137 *
4138 * Ioctl to set the EDELAY setting
4139 *
4140 */
Daeseok Younccbe7e52014-07-11 19:23:54 +09004141static int dgap_tty_digisetedelay(struct channel_t *ch, struct board_t *bd,
4142 struct un_t *un, int __user *new_info)
Mark Hounschella6792a32014-02-19 13:11:59 -05004143{
Mark Hounschella6792a32014-02-19 13:11:59 -05004144 int new_digi;
4145 ulong lock_flags;
4146 ulong lock_flags2;
4147
Mark Hounschell31960c12014-02-28 12:42:08 -05004148 if (copy_from_user(&new_digi, new_info, sizeof(int)))
4149 return -EFAULT;
Mark Hounschella6792a32014-02-19 13:11:59 -05004150
Mark Hounschellc43846a2014-03-19 11:10:51 -04004151 spin_lock_irqsave(&bd->bd_lock, lock_flags);
4152 spin_lock_irqsave(&ch->ch_lock, lock_flags2);
Mark Hounschella6792a32014-02-19 13:11:59 -05004153
Ioana Ciornei18122552015-10-21 23:17:54 +03004154 writew((u16)new_digi, &ch->ch_bs->edelay);
Mark Hounschella6792a32014-02-19 13:11:59 -05004155
Daeseok Younab6cdcb2014-07-11 19:22:39 +09004156 dgap_param(ch, bd, un->un_type);
Mark Hounschella6792a32014-02-19 13:11:59 -05004157
Mark Hounschellc43846a2014-03-19 11:10:51 -04004158 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
4159 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
Mark Hounschella6792a32014-02-19 13:11:59 -05004160
Mark Hounschellcf42c342014-02-28 12:42:09 -05004161 return 0;
Mark Hounschella6792a32014-02-19 13:11:59 -05004162}
4163
Mark Hounschella6792a32014-02-19 13:11:59 -05004164/*
4165 * dgap_tty_digigetcustombaud()
4166 *
4167 * Ioctl to get the current custom baud rate setting.
4168 */
Daeseok Youn501bcd42014-07-11 19:24:14 +09004169static int dgap_tty_digigetcustombaud(struct channel_t *ch, struct un_t *un,
4170 int __user *retinfo)
Mark Hounschella6792a32014-02-19 13:11:59 -05004171{
Mark Hounschella6792a32014-02-19 13:11:59 -05004172 int tmp;
4173 ulong lock_flags;
4174
4175 if (!retinfo)
Mark Hounschellcf42c342014-02-28 12:42:09 -05004176 return -EFAULT;
Mark Hounschella6792a32014-02-19 13:11:59 -05004177
Mark Hounschella6792a32014-02-19 13:11:59 -05004178 memset(&tmp, 0, sizeof(tmp));
4179
Mark Hounschellc43846a2014-03-19 11:10:51 -04004180 spin_lock_irqsave(&ch->ch_lock, lock_flags);
Mark Hounschella6792a32014-02-19 13:11:59 -05004181 tmp = dgap_get_custom_baud(ch);
Mark Hounschellc43846a2014-03-19 11:10:51 -04004182 spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
Mark Hounschella6792a32014-02-19 13:11:59 -05004183
Mark Hounschella6792a32014-02-19 13:11:59 -05004184 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
Mark Hounschellcf42c342014-02-28 12:42:09 -05004185 return -EFAULT;
Mark Hounschella6792a32014-02-19 13:11:59 -05004186
Mark Hounschellcf42c342014-02-28 12:42:09 -05004187 return 0;
Mark Hounschella6792a32014-02-19 13:11:59 -05004188}
4189
Mark Hounschella6792a32014-02-19 13:11:59 -05004190/*
4191 * dgap_tty_digisetcustombaud()
4192 *
4193 * Ioctl to set the custom baud rate setting
4194 */
Daeseok Youndb6fc2d2014-07-11 19:24:34 +09004195static int dgap_tty_digisetcustombaud(struct channel_t *ch, struct board_t *bd,
4196 struct un_t *un, int __user *new_info)
Mark Hounschella6792a32014-02-19 13:11:59 -05004197{
Mark Hounschella6792a32014-02-19 13:11:59 -05004198 uint new_rate;
4199 ulong lock_flags;
4200 ulong lock_flags2;
4201
Mark Hounschell31960c12014-02-28 12:42:08 -05004202 if (copy_from_user(&new_rate, new_info, sizeof(unsigned int)))
Mark Hounschellcf42c342014-02-28 12:42:09 -05004203 return -EFAULT;
Mark Hounschella6792a32014-02-19 13:11:59 -05004204
4205 if (bd->bd_flags & BD_FEP5PLUS) {
Mark Hounschellc43846a2014-03-19 11:10:51 -04004206 spin_lock_irqsave(&bd->bd_lock, lock_flags);
4207 spin_lock_irqsave(&ch->ch_lock, lock_flags2);
Mark Hounschella6792a32014-02-19 13:11:59 -05004208
4209 ch->ch_custom_speed = new_rate;
4210
Daeseok Younab6cdcb2014-07-11 19:22:39 +09004211 dgap_param(ch, bd, un->un_type);
Mark Hounschella6792a32014-02-19 13:11:59 -05004212
Mark Hounschellc43846a2014-03-19 11:10:51 -04004213 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
4214 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
Mark Hounschella6792a32014-02-19 13:11:59 -05004215 }
4216
Mark Hounschellcf42c342014-02-28 12:42:09 -05004217 return 0;
Mark Hounschella6792a32014-02-19 13:11:59 -05004218}
4219
Mark Hounschella6792a32014-02-19 13:11:59 -05004220/*
4221 * dgap_set_termios()
4222 */
Mark Hounschell8a2c9c42014-03-05 15:54:50 -05004223static void dgap_tty_set_termios(struct tty_struct *tty,
Ioana Ciornei629c7342015-10-21 23:17:53 +03004224 struct ktermios *old_termios)
Mark Hounschella6792a32014-02-19 13:11:59 -05004225{
4226 struct board_t *bd;
4227 struct channel_t *ch;
4228 struct un_t *un;
4229 unsigned long lock_flags;
4230 unsigned long lock_flags2;
4231
4232 if (!tty || tty->magic != TTY_MAGIC)
4233 return;
4234
4235 un = tty->driver_data;
4236 if (!un || un->magic != DGAP_UNIT_MAGIC)
4237 return;
4238
4239 ch = un->un_ch;
4240 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
4241 return;
4242
4243 bd = ch->ch_bd;
4244 if (!bd || bd->magic != DGAP_BOARD_MAGIC)
4245 return;
4246
Mark Hounschellc43846a2014-03-19 11:10:51 -04004247 spin_lock_irqsave(&bd->bd_lock, lock_flags);
4248 spin_lock_irqsave(&ch->ch_lock, lock_flags2);
Mark Hounschella6792a32014-02-19 13:11:59 -05004249
4250 ch->ch_c_cflag = tty->termios.c_cflag;
4251 ch->ch_c_iflag = tty->termios.c_iflag;
4252 ch->ch_c_oflag = tty->termios.c_oflag;
4253 ch->ch_c_lflag = tty->termios.c_lflag;
4254 ch->ch_startc = tty->termios.c_cc[VSTART];
4255 ch->ch_stopc = tty->termios.c_cc[VSTOP];
4256
4257 dgap_carrier(ch);
Daeseok Younab6cdcb2014-07-11 19:22:39 +09004258 dgap_param(ch, bd, un->un_type);
Mark Hounschella6792a32014-02-19 13:11:59 -05004259
Mark Hounschellc43846a2014-03-19 11:10:51 -04004260 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
4261 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
Mark Hounschella6792a32014-02-19 13:11:59 -05004262}
4263
Mark Hounschella6792a32014-02-19 13:11:59 -05004264static void dgap_tty_throttle(struct tty_struct *tty)
4265{
4266 struct board_t *bd;
4267 struct channel_t *ch;
4268 struct un_t *un;
Mark Hounschell174efc12014-05-23 12:54:04 -04004269 ulong lock_flags;
4270 ulong lock_flags2;
Mark Hounschella6792a32014-02-19 13:11:59 -05004271
4272 if (!tty || tty->magic != TTY_MAGIC)
4273 return;
4274
4275 un = tty->driver_data;
4276 if (!un || un->magic != DGAP_UNIT_MAGIC)
4277 return;
4278
Mark Hounschell7d6069d72014-02-28 12:42:10 -05004279 ch = un->un_ch;
4280 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
Mark Hounschellcf42c342014-02-28 12:42:09 -05004281 return;
Mark Hounschella6792a32014-02-19 13:11:59 -05004282
4283 bd = ch->ch_bd;
4284 if (!bd || bd->magic != DGAP_BOARD_MAGIC)
4285 return;
4286
Mark Hounschellc43846a2014-03-19 11:10:51 -04004287 spin_lock_irqsave(&bd->bd_lock, lock_flags);
4288 spin_lock_irqsave(&ch->ch_lock, lock_flags2);
Mark Hounschella6792a32014-02-19 13:11:59 -05004289
4290 ch->ch_flags |= (CH_RXBLOCK);
4291#if 1
4292 dgap_cmdw(ch, RPAUSE, 0, 0);
4293#endif
4294
Mark Hounschellc43846a2014-03-19 11:10:51 -04004295 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
4296 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
Mark Hounschella6792a32014-02-19 13:11:59 -05004297}
4298
Mark Hounschella6792a32014-02-19 13:11:59 -05004299static void dgap_tty_unthrottle(struct tty_struct *tty)
4300{
4301 struct board_t *bd;
4302 struct channel_t *ch;
4303 struct un_t *un;
Mark Hounschell174efc12014-05-23 12:54:04 -04004304 ulong lock_flags;
4305 ulong lock_flags2;
Mark Hounschella6792a32014-02-19 13:11:59 -05004306
4307 if (!tty || tty->magic != TTY_MAGIC)
4308 return;
4309
4310 un = tty->driver_data;
4311 if (!un || un->magic != DGAP_UNIT_MAGIC)
4312 return;
4313
Mark Hounschell7d6069d72014-02-28 12:42:10 -05004314 ch = un->un_ch;
4315 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
Mark Hounschellcf42c342014-02-28 12:42:09 -05004316 return;
Mark Hounschella6792a32014-02-19 13:11:59 -05004317
4318 bd = ch->ch_bd;
4319 if (!bd || bd->magic != DGAP_BOARD_MAGIC)
4320 return;
4321
Mark Hounschellc43846a2014-03-19 11:10:51 -04004322 spin_lock_irqsave(&bd->bd_lock, lock_flags);
4323 spin_lock_irqsave(&ch->ch_lock, lock_flags2);
Mark Hounschella6792a32014-02-19 13:11:59 -05004324
4325 ch->ch_flags &= ~(CH_RXBLOCK);
4326
4327#if 1
4328 dgap_cmdw(ch, RRESUME, 0, 0);
4329#endif
4330
Mark Hounschellc43846a2014-03-19 11:10:51 -04004331 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
4332 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
Mark Hounschella6792a32014-02-19 13:11:59 -05004333}
4334
Daeseok Youn48d43be2014-11-06 19:27:59 +09004335static struct board_t *find_board_by_major(unsigned int major)
4336{
4337 unsigned int i;
4338
4339 for (i = 0; i < MAXBOARDS; i++) {
4340 struct board_t *brd = dgap_board[i];
Anjana Sasindrand268b502014-11-21 00:27:50 +05304341
Daeseok Youn48d43be2014-11-06 19:27:59 +09004342 if (!brd)
4343 return NULL;
4344 if (major == brd->serial_driver->major ||
4345 major == brd->print_driver->major)
4346 return brd;
4347 }
4348
4349 return NULL;
4350}
4351
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09004352/************************************************************************
4353 *
4354 * TTY Entry points and helper functions
4355 *
4356 ************************************************************************/
4357
4358/*
4359 * dgap_tty_open()
4360 *
4361 */
4362static int dgap_tty_open(struct tty_struct *tty, struct file *file)
4363{
4364 struct board_t *brd;
4365 struct channel_t *ch;
4366 struct un_t *un;
4367 struct bs_t __iomem *bs;
4368 uint major;
4369 uint minor;
4370 int rc;
4371 ulong lock_flags;
4372 ulong lock_flags2;
4373 u16 head;
4374
4375 major = MAJOR(tty_devnum(tty));
4376 minor = MINOR(tty_devnum(tty));
4377
Daeseok Youn48d43be2014-11-06 19:27:59 +09004378 brd = find_board_by_major(major);
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09004379 if (!brd)
4380 return -EIO;
4381
4382 /*
4383 * If board is not yet up to a state of READY, go to
4384 * sleep waiting for it to happen or they cancel the open.
4385 */
4386 rc = wait_event_interruptible(brd->state_wait,
Ioana Ciornei629c7342015-10-21 23:17:53 +03004387 (brd->state & BOARD_READY));
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09004388
4389 if (rc)
4390 return rc;
4391
4392 spin_lock_irqsave(&brd->bd_lock, lock_flags);
4393
4394 /* The wait above should guarantee this cannot happen */
4395 if (brd->state != BOARD_READY) {
4396 spin_unlock_irqrestore(&brd->bd_lock, lock_flags);
4397 return -EIO;
4398 }
4399
4400 /* If opened device is greater than our number of ports, bail. */
4401 if (MINOR(tty_devnum(tty)) > brd->nasync) {
4402 spin_unlock_irqrestore(&brd->bd_lock, lock_flags);
4403 return -EIO;
4404 }
4405
4406 ch = brd->channels[minor];
4407 if (!ch) {
4408 spin_unlock_irqrestore(&brd->bd_lock, lock_flags);
4409 return -EIO;
4410 }
4411
4412 /* Grab channel lock */
4413 spin_lock_irqsave(&ch->ch_lock, lock_flags2);
4414
4415 /* Figure out our type */
Daeseok Youn4c5dbca2014-11-06 19:27:33 +09004416 if (major == brd->serial_driver->major) {
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09004417 un = &brd->channels[minor]->ch_tun;
4418 un->un_type = DGAP_SERIAL;
Daeseok Youn4c5dbca2014-11-06 19:27:33 +09004419 } else if (major == brd->print_driver->major) {
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09004420 un = &brd->channels[minor]->ch_pun;
4421 un->un_type = DGAP_PRINT;
4422 } else {
4423 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
4424 spin_unlock_irqrestore(&brd->bd_lock, lock_flags);
4425 return -EIO;
4426 }
4427
4428 /* Store our unit into driver_data, so we always have it available. */
4429 tty->driver_data = un;
4430
4431 /*
4432 * Error if channel info pointer is NULL.
4433 */
4434 bs = ch->ch_bs;
4435 if (!bs) {
4436 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
4437 spin_unlock_irqrestore(&brd->bd_lock, lock_flags);
4438 return -EIO;
4439 }
4440
4441 /*
4442 * Initialize tty's
4443 */
4444 if (!(un->un_flags & UN_ISOPEN)) {
4445 /* Store important variables. */
4446 un->un_tty = tty;
4447
4448 /* Maybe do something here to the TTY struct as well? */
4449 }
4450
4451 /*
4452 * Initialize if neither terminal or printer is open.
4453 */
4454 if (!((ch->ch_tun.un_flags | ch->ch_pun.un_flags) & UN_ISOPEN)) {
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09004455 ch->ch_mforce = 0;
4456 ch->ch_mval = 0;
4457
4458 /*
4459 * Flush input queue.
4460 */
Ioana Ciornei44209c92015-10-21 23:17:55 +03004461 head = readw(&bs->rx_head);
4462 writew(head, &bs->rx_tail);
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09004463
4464 ch->ch_flags = 0;
4465 ch->pscan_state = 0;
4466 ch->pscan_savechar = 0;
4467
4468 ch->ch_c_cflag = tty->termios.c_cflag;
4469 ch->ch_c_iflag = tty->termios.c_iflag;
4470 ch->ch_c_oflag = tty->termios.c_oflag;
4471 ch->ch_c_lflag = tty->termios.c_lflag;
4472 ch->ch_startc = tty->termios.c_cc[VSTART];
4473 ch->ch_stopc = tty->termios.c_cc[VSTOP];
4474
4475 /* TODO: flush our TTY struct here? */
4476 }
4477
4478 dgap_carrier(ch);
4479 /*
4480 * Run param in case we changed anything
4481 */
4482 dgap_param(ch, brd, un->un_type);
4483
4484 /*
4485 * follow protocol for opening port
4486 */
4487
4488 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
4489 spin_unlock_irqrestore(&brd->bd_lock, lock_flags);
4490
4491 rc = dgap_block_til_ready(tty, file, ch);
4492
4493 if (!un->un_tty)
4494 return -ENODEV;
4495
4496 /* No going back now, increment our unit and channel counters */
4497 spin_lock_irqsave(&ch->ch_lock, lock_flags);
4498 ch->ch_open_count++;
4499 un->un_open_count++;
4500 un->un_flags |= (UN_ISOPEN);
4501 spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
4502
4503 return rc;
4504}
4505
4506/*
4507 * dgap_tty_close()
4508 *
4509 */
4510static void dgap_tty_close(struct tty_struct *tty, struct file *file)
4511{
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09004512 struct board_t *bd;
4513 struct channel_t *ch;
4514 struct un_t *un;
4515 ulong lock_flags;
4516
4517 if (!tty || tty->magic != TTY_MAGIC)
4518 return;
4519
4520 un = tty->driver_data;
4521 if (!un || un->magic != DGAP_UNIT_MAGIC)
4522 return;
4523
4524 ch = un->un_ch;
4525 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
4526 return;
4527
4528 bd = ch->ch_bd;
4529 if (!bd || bd->magic != DGAP_BOARD_MAGIC)
4530 return;
4531
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09004532 spin_lock_irqsave(&ch->ch_lock, lock_flags);
4533
4534 /*
4535 * Determine if this is the last close or not - and if we agree about
4536 * which type of close it is with the Line Discipline
4537 */
4538 if ((tty->count == 1) && (un->un_open_count != 1)) {
4539 /*
4540 * Uh, oh. tty->count is 1, which means that the tty
4541 * structure will be freed. un_open_count should always
4542 * be one in these conditions. If it's greater than
4543 * one, we've got real problems, since it means the
4544 * serial port won't be shutdown.
4545 */
4546 un->un_open_count = 1;
4547 }
4548
4549 if (--un->un_open_count < 0)
4550 un->un_open_count = 0;
4551
4552 ch->ch_open_count--;
4553
4554 if (ch->ch_open_count && un->un_open_count) {
4555 spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
4556 return;
4557 }
4558
4559 /* OK, its the last close on the unit */
4560
4561 un->un_flags |= UN_CLOSING;
4562
4563 tty->closing = 1;
4564
4565 /*
4566 * Only officially close channel if count is 0 and
4567 * DIGI_PRINTER bit is not set.
4568 */
4569 if ((ch->ch_open_count == 0) &&
4570 !(ch->ch_digi.digi_flags & DIGI_PRINTER)) {
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09004571 ch->ch_flags &= ~(CH_RXBLOCK);
4572
4573 spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
4574
4575 /* wait for output to drain */
4576 /* This will also return if we take an interrupt */
4577
4578 dgap_wait_for_drain(tty);
4579
4580 dgap_tty_flush_buffer(tty);
4581 tty_ldisc_flush(tty);
4582
4583 spin_lock_irqsave(&ch->ch_lock, lock_flags);
4584
4585 tty->closing = 0;
4586
4587 /*
4588 * If we have HUPCL set, lower DTR and RTS
4589 */
4590 if (ch->ch_c_cflag & HUPCL) {
Ioana Ciorneia9108b72015-10-21 23:17:56 +03004591 ch->ch_mostat &= ~(D_RTS(ch) | D_DTR(ch));
4592 dgap_cmdb(ch, SMODEM, 0, D_DTR(ch) | D_RTS(ch), 0);
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09004593
4594 /*
4595 * Go to sleep to ensure RTS/DTR
4596 * have been dropped for modems to see it.
4597 */
4598 spin_unlock_irqrestore(&ch->ch_lock,
Ioana Ciornei629c7342015-10-21 23:17:53 +03004599 lock_flags);
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09004600
4601 /* .25 second delay for dropping RTS/DTR */
4602 schedule_timeout_interruptible(msecs_to_jiffies(250));
4603
4604 spin_lock_irqsave(&ch->ch_lock, lock_flags);
4605 }
4606
4607 ch->pscan_state = 0;
4608 ch->pscan_savechar = 0;
4609 ch->ch_baud_info = 0;
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09004610 }
4611
4612 /*
4613 * turn off print device when closing print device.
4614 */
4615 if ((un->un_type == DGAP_PRINT) && (ch->ch_flags & CH_PRON)) {
4616 dgap_wmove(ch, ch->ch_digi.digi_offstr,
Ioana Ciornei629c7342015-10-21 23:17:53 +03004617 (int)ch->ch_digi.digi_offlen);
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09004618 ch->ch_flags &= ~CH_PRON;
4619 }
4620
4621 un->un_tty = NULL;
4622 un->un_flags &= ~(UN_ISOPEN | UN_CLOSING);
4623 tty->driver_data = NULL;
4624
4625 wake_up_interruptible(&ch->ch_flags_wait);
4626 wake_up_interruptible(&un->un_flags_wait);
4627
4628 spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
4629}
4630
Mark Hounschella6792a32014-02-19 13:11:59 -05004631static void dgap_tty_start(struct tty_struct *tty)
4632{
4633 struct board_t *bd;
4634 struct channel_t *ch;
4635 struct un_t *un;
Mark Hounschell174efc12014-05-23 12:54:04 -04004636 ulong lock_flags;
4637 ulong lock_flags2;
Mark Hounschella6792a32014-02-19 13:11:59 -05004638
4639 if (!tty || tty->magic != TTY_MAGIC)
4640 return;
4641
4642 un = tty->driver_data;
4643 if (!un || un->magic != DGAP_UNIT_MAGIC)
4644 return;
4645
Mark Hounschell7d6069d72014-02-28 12:42:10 -05004646 ch = un->un_ch;
4647 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
Mark Hounschellcf42c342014-02-28 12:42:09 -05004648 return;
Mark Hounschella6792a32014-02-19 13:11:59 -05004649
4650 bd = ch->ch_bd;
4651 if (!bd || bd->magic != DGAP_BOARD_MAGIC)
4652 return;
4653
Mark Hounschellc43846a2014-03-19 11:10:51 -04004654 spin_lock_irqsave(&bd->bd_lock, lock_flags);
4655 spin_lock_irqsave(&ch->ch_lock, lock_flags2);
Mark Hounschella6792a32014-02-19 13:11:59 -05004656
4657 dgap_cmdw(ch, RESUMETX, 0, 0);
4658
Mark Hounschellc43846a2014-03-19 11:10:51 -04004659 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
4660 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
Mark Hounschella6792a32014-02-19 13:11:59 -05004661}
4662
Mark Hounschella6792a32014-02-19 13:11:59 -05004663static void dgap_tty_stop(struct tty_struct *tty)
4664{
4665 struct board_t *bd;
4666 struct channel_t *ch;
4667 struct un_t *un;
Mark Hounschell174efc12014-05-23 12:54:04 -04004668 ulong lock_flags;
4669 ulong lock_flags2;
Mark Hounschella6792a32014-02-19 13:11:59 -05004670
4671 if (!tty || tty->magic != TTY_MAGIC)
4672 return;
4673
4674 un = tty->driver_data;
4675 if (!un || un->magic != DGAP_UNIT_MAGIC)
4676 return;
4677
Mark Hounschell7d6069d72014-02-28 12:42:10 -05004678 ch = un->un_ch;
4679 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
Mark Hounschellcf42c342014-02-28 12:42:09 -05004680 return;
Mark Hounschella6792a32014-02-19 13:11:59 -05004681
4682 bd = ch->ch_bd;
4683 if (!bd || bd->magic != DGAP_BOARD_MAGIC)
4684 return;
4685
Mark Hounschellc43846a2014-03-19 11:10:51 -04004686 spin_lock_irqsave(&bd->bd_lock, lock_flags);
4687 spin_lock_irqsave(&ch->ch_lock, lock_flags2);
Mark Hounschella6792a32014-02-19 13:11:59 -05004688
4689 dgap_cmdw(ch, PAUSETX, 0, 0);
4690
Mark Hounschellc43846a2014-03-19 11:10:51 -04004691 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
4692 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
Mark Hounschella6792a32014-02-19 13:11:59 -05004693}
4694
Mark Hounschella6792a32014-02-19 13:11:59 -05004695/*
4696 * dgap_tty_flush_chars()
4697 *
4698 * Flush the cook buffer
4699 *
4700 * Note to self, and any other poor souls who venture here:
4701 *
4702 * flush in this case DOES NOT mean dispose of the data.
4703 * instead, it means "stop buffering and send it if you
4704 * haven't already." Just guess how I figured that out... SRW 2-Jun-98
4705 *
4706 * It is also always called in interrupt context - JAR 8-Sept-99
4707 */
4708static void dgap_tty_flush_chars(struct tty_struct *tty)
4709{
4710 struct board_t *bd;
4711 struct channel_t *ch;
4712 struct un_t *un;
Mark Hounschell174efc12014-05-23 12:54:04 -04004713 ulong lock_flags;
4714 ulong lock_flags2;
Mark Hounschella6792a32014-02-19 13:11:59 -05004715
4716 if (!tty || tty->magic != TTY_MAGIC)
4717 return;
4718
4719 un = tty->driver_data;
4720 if (!un || un->magic != DGAP_UNIT_MAGIC)
4721 return;
4722
Mark Hounschell7d6069d72014-02-28 12:42:10 -05004723 ch = un->un_ch;
4724 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
Mark Hounschellcf42c342014-02-28 12:42:09 -05004725 return;
Mark Hounschella6792a32014-02-19 13:11:59 -05004726
4727 bd = ch->ch_bd;
4728 if (!bd || bd->magic != DGAP_BOARD_MAGIC)
4729 return;
4730
Mark Hounschellc43846a2014-03-19 11:10:51 -04004731 spin_lock_irqsave(&bd->bd_lock, lock_flags);
4732 spin_lock_irqsave(&ch->ch_lock, lock_flags2);
Mark Hounschella6792a32014-02-19 13:11:59 -05004733
4734 /* TODO: Do something here */
4735
Mark Hounschellc43846a2014-03-19 11:10:51 -04004736 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
4737 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
Mark Hounschella6792a32014-02-19 13:11:59 -05004738}
4739
Mark Hounschella6792a32014-02-19 13:11:59 -05004740/*****************************************************************************
4741 *
4742 * The IOCTL function and all of its helpers
4743 *
4744 *****************************************************************************/
4745
4746/*
4747 * dgap_tty_ioctl()
4748 *
4749 * The usual assortment of ioctl's
4750 */
4751static int dgap_tty_ioctl(struct tty_struct *tty, unsigned int cmd,
Ioana Ciornei629c7342015-10-21 23:17:53 +03004752 unsigned long arg)
Mark Hounschella6792a32014-02-19 13:11:59 -05004753{
4754 struct board_t *bd;
4755 struct channel_t *ch;
4756 struct un_t *un;
4757 int rc;
Mark Hounschell174efc12014-05-23 12:54:04 -04004758 u16 head;
4759 ulong lock_flags = 0;
4760 ulong lock_flags2 = 0;
Ioana Ciornei18122552015-10-21 23:17:54 +03004761 void __user *uarg = (void __user *)arg;
Mark Hounschella6792a32014-02-19 13:11:59 -05004762
4763 if (!tty || tty->magic != TTY_MAGIC)
Mark Hounschellcf42c342014-02-28 12:42:09 -05004764 return -ENODEV;
Mark Hounschella6792a32014-02-19 13:11:59 -05004765
4766 un = tty->driver_data;
4767 if (!un || un->magic != DGAP_UNIT_MAGIC)
Mark Hounschellcf42c342014-02-28 12:42:09 -05004768 return -ENODEV;
Mark Hounschella6792a32014-02-19 13:11:59 -05004769
4770 ch = un->un_ch;
4771 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
Mark Hounschellcf42c342014-02-28 12:42:09 -05004772 return -ENODEV;
Mark Hounschella6792a32014-02-19 13:11:59 -05004773
4774 bd = ch->ch_bd;
4775 if (!bd || bd->magic != DGAP_BOARD_MAGIC)
Mark Hounschellcf42c342014-02-28 12:42:09 -05004776 return -ENODEV;
Mark Hounschella6792a32014-02-19 13:11:59 -05004777
Mark Hounschellc43846a2014-03-19 11:10:51 -04004778 spin_lock_irqsave(&bd->bd_lock, lock_flags);
4779 spin_lock_irqsave(&ch->ch_lock, lock_flags2);
Mark Hounschella6792a32014-02-19 13:11:59 -05004780
4781 if (un->un_open_count <= 0) {
Mark Hounschellc43846a2014-03-19 11:10:51 -04004782 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
4783 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
Mark Hounschellcf42c342014-02-28 12:42:09 -05004784 return -EIO;
Mark Hounschella6792a32014-02-19 13:11:59 -05004785 }
4786
4787 switch (cmd) {
Mark Hounschella6792a32014-02-19 13:11:59 -05004788 /* Here are all the standard ioctl's that we MUST implement */
Mark Hounschella6792a32014-02-19 13:11:59 -05004789 case TCSBRK:
4790 /*
4791 * TCSBRK is SVID version: non-zero arg --> no break
4792 * this behaviour is exploited by tcdrain().
4793 *
4794 * According to POSIX.1 spec (7.2.2.1.2) breaks should be
4795 * between 0.25 and 0.5 seconds so we'll ask for something
4796 * in the middle: 0.375 seconds.
4797 */
4798 rc = tty_check_change(tty);
Mark Hounschellc43846a2014-03-19 11:10:51 -04004799 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
4800 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
Mark Hounschell305ec872014-02-28 12:42:13 -05004801 if (rc)
Mark Hounschellcf42c342014-02-28 12:42:09 -05004802 return rc;
Mark Hounschella6792a32014-02-19 13:11:59 -05004803
4804 rc = dgap_wait_for_drain(tty);
4805
Mark Hounschell31960c12014-02-28 12:42:08 -05004806 if (rc)
Mark Hounschellcf42c342014-02-28 12:42:09 -05004807 return -EINTR;
Mark Hounschella6792a32014-02-19 13:11:59 -05004808
Mark Hounschellc43846a2014-03-19 11:10:51 -04004809 spin_lock_irqsave(&bd->bd_lock, lock_flags);
4810 spin_lock_irqsave(&ch->ch_lock, lock_flags2);
Mark Hounschella6792a32014-02-19 13:11:59 -05004811
Mark Hounschell305ec872014-02-28 12:42:13 -05004812 if (((cmd == TCSBRK) && (!arg)) || (cmd == TCSBRKP))
Ioana Ciornei18122552015-10-21 23:17:54 +03004813 dgap_cmdw(ch, SBREAK, (u16)SBREAK_TIME, 0);
Mark Hounschella6792a32014-02-19 13:11:59 -05004814
Mark Hounschellc43846a2014-03-19 11:10:51 -04004815 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
4816 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
Mark Hounschella6792a32014-02-19 13:11:59 -05004817
Mark Hounschellcf42c342014-02-28 12:42:09 -05004818 return 0;
Mark Hounschella6792a32014-02-19 13:11:59 -05004819
Mark Hounschella6792a32014-02-19 13:11:59 -05004820 case TCSBRKP:
Mark Hounschell7d6069d72014-02-28 12:42:10 -05004821 /* support for POSIX tcsendbreak()
Mark Hounschella6792a32014-02-19 13:11:59 -05004822
4823 * According to POSIX.1 spec (7.2.2.1.2) breaks should be
4824 * between 0.25 and 0.5 seconds so we'll ask for something
4825 * in the middle: 0.375 seconds.
4826 */
4827 rc = tty_check_change(tty);
Mark Hounschellc43846a2014-03-19 11:10:51 -04004828 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
4829 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
Mark Hounschell305ec872014-02-28 12:42:13 -05004830 if (rc)
Mark Hounschellcf42c342014-02-28 12:42:09 -05004831 return rc;
Mark Hounschella6792a32014-02-19 13:11:59 -05004832
4833 rc = dgap_wait_for_drain(tty);
Mark Hounschell31960c12014-02-28 12:42:08 -05004834 if (rc)
Mark Hounschellcf42c342014-02-28 12:42:09 -05004835 return -EINTR;
Mark Hounschella6792a32014-02-19 13:11:59 -05004836
Mark Hounschellc43846a2014-03-19 11:10:51 -04004837 spin_lock_irqsave(&bd->bd_lock, lock_flags);
4838 spin_lock_irqsave(&ch->ch_lock, lock_flags2);
Mark Hounschella6792a32014-02-19 13:11:59 -05004839
Ioana Ciornei18122552015-10-21 23:17:54 +03004840 dgap_cmdw(ch, SBREAK, (u16)SBREAK_TIME, 0);
Mark Hounschella6792a32014-02-19 13:11:59 -05004841
Mark Hounschellc43846a2014-03-19 11:10:51 -04004842 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
4843 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
Mark Hounschella6792a32014-02-19 13:11:59 -05004844
Mark Hounschellcf42c342014-02-28 12:42:09 -05004845 return 0;
Mark Hounschella6792a32014-02-19 13:11:59 -05004846
Mark Hounschell7d6069d72014-02-28 12:42:10 -05004847 case TIOCSBRK:
Mark Hounschella6792a32014-02-19 13:11:59 -05004848 /*
4849 * FEP5 doesn't support turning on a break unconditionally.
4850 * The FEP5 device will stop sending a break automatically
4851 * after the specified time value that was sent when turning on
4852 * the break.
4853 */
4854 rc = tty_check_change(tty);
Mark Hounschellc43846a2014-03-19 11:10:51 -04004855 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
4856 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
Mark Hounschell305ec872014-02-28 12:42:13 -05004857 if (rc)
Mark Hounschellcf42c342014-02-28 12:42:09 -05004858 return rc;
Mark Hounschella6792a32014-02-19 13:11:59 -05004859
4860 rc = dgap_wait_for_drain(tty);
Mark Hounschell31960c12014-02-28 12:42:08 -05004861 if (rc)
Mark Hounschellcf42c342014-02-28 12:42:09 -05004862 return -EINTR;
Mark Hounschella6792a32014-02-19 13:11:59 -05004863
Mark Hounschellc43846a2014-03-19 11:10:51 -04004864 spin_lock_irqsave(&bd->bd_lock, lock_flags);
4865 spin_lock_irqsave(&ch->ch_lock, lock_flags2);
Mark Hounschella6792a32014-02-19 13:11:59 -05004866
Ioana Ciornei18122552015-10-21 23:17:54 +03004867 dgap_cmdw(ch, SBREAK, (u16)SBREAK_TIME, 0);
Mark Hounschella6792a32014-02-19 13:11:59 -05004868
Mark Hounschellc43846a2014-03-19 11:10:51 -04004869 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
4870 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
Mark Hounschella6792a32014-02-19 13:11:59 -05004871
Mark Hounschella6792a32014-02-19 13:11:59 -05004872 return 0;
4873
Mark Hounschell7d6069d72014-02-28 12:42:10 -05004874 case TIOCCBRK:
Mark Hounschella6792a32014-02-19 13:11:59 -05004875 /*
4876 * FEP5 doesn't support turning off a break unconditionally.
4877 * The FEP5 device will stop sending a break automatically
4878 * after the specified time value that was sent when turning on
4879 * the break.
4880 */
Mark Hounschellc43846a2014-03-19 11:10:51 -04004881 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
4882 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
Mark Hounschella6792a32014-02-19 13:11:59 -05004883 return 0;
4884
4885 case TIOCGSOFTCAR:
4886
Mark Hounschellc43846a2014-03-19 11:10:51 -04004887 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
4888 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
Mark Hounschella6792a32014-02-19 13:11:59 -05004889
Shraddha Barke6ba4df22015-07-27 23:30:52 +05304890 return put_user(C_CLOCAL(tty) ? 1 : 0,
Ioana Ciornei18122552015-10-21 23:17:54 +03004891 (unsigned long __user *)arg);
Mark Hounschella6792a32014-02-19 13:11:59 -05004892
4893 case TIOCSSOFTCAR:
Mark Hounschellc43846a2014-03-19 11:10:51 -04004894 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
4895 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
Mark Hounschella6792a32014-02-19 13:11:59 -05004896
Ioana Ciornei18122552015-10-21 23:17:54 +03004897 rc = get_user(arg, (unsigned long __user *)arg);
Mark Hounschella6792a32014-02-19 13:11:59 -05004898 if (rc)
Mark Hounschellcf42c342014-02-28 12:42:09 -05004899 return rc;
Mark Hounschella6792a32014-02-19 13:11:59 -05004900
Mark Hounschellc43846a2014-03-19 11:10:51 -04004901 spin_lock_irqsave(&bd->bd_lock, lock_flags);
4902 spin_lock_irqsave(&ch->ch_lock, lock_flags2);
Mark Hounschell8a2c9c42014-03-05 15:54:50 -05004903 tty->termios.c_cflag = ((tty->termios.c_cflag & ~CLOCAL) |
4904 (arg ? CLOCAL : 0));
Daeseok Younab6cdcb2014-07-11 19:22:39 +09004905 dgap_param(ch, bd, un->un_type);
Mark Hounschellc43846a2014-03-19 11:10:51 -04004906 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
4907 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
Mark Hounschella6792a32014-02-19 13:11:59 -05004908
Mark Hounschellcf42c342014-02-28 12:42:09 -05004909 return 0;
Mark Hounschella6792a32014-02-19 13:11:59 -05004910
4911 case TIOCMGET:
Mark Hounschellc43846a2014-03-19 11:10:51 -04004912 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
4913 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
Mark Hounschellcf42c342014-02-28 12:42:09 -05004914 return dgap_get_modem_info(ch, uarg);
Mark Hounschella6792a32014-02-19 13:11:59 -05004915
4916 case TIOCMBIS:
4917 case TIOCMBIC:
4918 case TIOCMSET:
Mark Hounschellc43846a2014-03-19 11:10:51 -04004919 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
4920 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
Daeseok Youn4285c972014-07-11 19:23:34 +09004921 return dgap_set_modem_info(ch, bd, un, cmd, uarg);
Mark Hounschella6792a32014-02-19 13:11:59 -05004922
4923 /*
4924 * Here are any additional ioctl's that we want to implement
4925 */
4926
4927 case TCFLSH:
4928 /*
4929 * The linux tty driver doesn't have a flush
4930 * input routine for the driver, assuming all backed
4931 * up data is in the line disc. buffers. However,
4932 * we all know that's not the case. Here, we
4933 * act on the ioctl, but then lie and say we didn't
4934 * so the line discipline will process the flush
4935 * also.
4936 */
4937 rc = tty_check_change(tty);
4938 if (rc) {
Mark Hounschellc43846a2014-03-19 11:10:51 -04004939 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
4940 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
Mark Hounschellcf42c342014-02-28 12:42:09 -05004941 return rc;
Mark Hounschella6792a32014-02-19 13:11:59 -05004942 }
4943
4944 if ((arg == TCIFLUSH) || (arg == TCIOFLUSH)) {
4945 if (!(un->un_type == DGAP_PRINT)) {
Ioana Ciornei44209c92015-10-21 23:17:55 +03004946 head = readw(&ch->ch_bs->rx_head);
4947 writew(head, &ch->ch_bs->rx_tail);
4948 writeb(0, &ch->ch_bs->orun);
Mark Hounschella6792a32014-02-19 13:11:59 -05004949 }
4950 }
4951
Mark Hounschell70d97a62014-03-10 14:39:41 -04004952 if ((arg != TCOFLUSH) && (arg != TCIOFLUSH)) {
4953 /* pretend we didn't recognize this IOCTL */
Mark Hounschellc43846a2014-03-19 11:10:51 -04004954 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
4955 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
Mark Hounschell70d97a62014-03-10 14:39:41 -04004956
4957 return -ENOIOCTLCMD;
Mark Hounschella6792a32014-02-19 13:11:59 -05004958 }
4959
Mark Hounschell70d97a62014-03-10 14:39:41 -04004960 ch->ch_flags &= ~CH_STOP;
Ioana Ciornei44209c92015-10-21 23:17:55 +03004961 head = readw(&ch->ch_bs->tx_head);
Ioana Ciornei18122552015-10-21 23:17:54 +03004962 dgap_cmdw(ch, FLUSHTX, (u16)head, 0);
Mark Hounschell70d97a62014-03-10 14:39:41 -04004963 dgap_cmdw(ch, RESUMETX, 0, 0);
Ioana Ciorneia9108b72015-10-21 23:17:56 +03004964 if (ch->ch_tun.un_flags & (UN_LOW | UN_EMPTY)) {
4965 ch->ch_tun.un_flags &= ~(UN_LOW | UN_EMPTY);
Mark Hounschell70d97a62014-03-10 14:39:41 -04004966 wake_up_interruptible(&ch->ch_tun.un_flags_wait);
4967 }
Ioana Ciorneia9108b72015-10-21 23:17:56 +03004968 if (ch->ch_pun.un_flags & (UN_LOW | UN_EMPTY)) {
4969 ch->ch_pun.un_flags &= ~(UN_LOW | UN_EMPTY);
Mark Hounschell70d97a62014-03-10 14:39:41 -04004970 wake_up_interruptible(&ch->ch_pun.un_flags_wait);
4971 }
4972 if (waitqueue_active(&tty->write_wait))
4973 wake_up_interruptible(&tty->write_wait);
4974
4975 /* Can't hold any locks when calling tty_wakeup! */
Mark Hounschellc43846a2014-03-19 11:10:51 -04004976 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
4977 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
Mark Hounschell70d97a62014-03-10 14:39:41 -04004978 tty_wakeup(tty);
Mark Hounschella6792a32014-02-19 13:11:59 -05004979
Mark Hounschell70d97a62014-03-10 14:39:41 -04004980 /* pretend we didn't recognize this IOCTL */
Mark Hounschellcf42c342014-02-28 12:42:09 -05004981 return -ENOIOCTLCMD;
Mark Hounschella6792a32014-02-19 13:11:59 -05004982
4983 case TCSETSF:
4984 case TCSETSW:
4985 /*
4986 * The linux tty driver doesn't have a flush
4987 * input routine for the driver, assuming all backed
4988 * up data is in the line disc. buffers. However,
4989 * we all know that's not the case. Here, we
4990 * act on the ioctl, but then lie and say we didn't
4991 * so the line discipline will process the flush
4992 * also.
4993 */
4994 if (cmd == TCSETSF) {
4995 /* flush rx */
4996 ch->ch_flags &= ~CH_STOP;
Ioana Ciornei44209c92015-10-21 23:17:55 +03004997 head = readw(&ch->ch_bs->rx_head);
4998 writew(head, &ch->ch_bs->rx_tail);
Mark Hounschella6792a32014-02-19 13:11:59 -05004999 }
5000
5001 /* now wait for all the output to drain */
Mark Hounschellc43846a2014-03-19 11:10:51 -04005002 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
5003 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
Mark Hounschella6792a32014-02-19 13:11:59 -05005004 rc = dgap_wait_for_drain(tty);
Mark Hounschell31960c12014-02-28 12:42:08 -05005005 if (rc)
Mark Hounschellcf42c342014-02-28 12:42:09 -05005006 return -EINTR;
Mark Hounschella6792a32014-02-19 13:11:59 -05005007
5008 /* pretend we didn't recognize this */
Mark Hounschellcf42c342014-02-28 12:42:09 -05005009 return -ENOIOCTLCMD;
Mark Hounschella6792a32014-02-19 13:11:59 -05005010
5011 case TCSETAW:
5012
Mark Hounschellc43846a2014-03-19 11:10:51 -04005013 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
5014 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
Mark Hounschella6792a32014-02-19 13:11:59 -05005015 rc = dgap_wait_for_drain(tty);
Mark Hounschell31960c12014-02-28 12:42:08 -05005016 if (rc)
Mark Hounschellcf42c342014-02-28 12:42:09 -05005017 return -EINTR;
Mark Hounschella6792a32014-02-19 13:11:59 -05005018
5019 /* pretend we didn't recognize this */
Mark Hounschellcf42c342014-02-28 12:42:09 -05005020 return -ENOIOCTLCMD;
Mark Hounschella6792a32014-02-19 13:11:59 -05005021
5022 case TCXONC:
5023 /*
5024 * The Linux Line Discipline (LD) would do this for us if we
5025 * let it, but we have the special firmware options to do this
5026 * the "right way" regardless of hardware or software flow
5027 * control so we'll do it outselves instead of letting the LD
5028 * do it.
5029 */
5030 rc = tty_check_change(tty);
5031 if (rc) {
Mark Hounschellc43846a2014-03-19 11:10:51 -04005032 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
5033 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
Mark Hounschellcf42c342014-02-28 12:42:09 -05005034 return rc;
Mark Hounschella6792a32014-02-19 13:11:59 -05005035 }
5036
Mark Hounschella6792a32014-02-19 13:11:59 -05005037 switch (arg) {
Mark Hounschella6792a32014-02-19 13:11:59 -05005038 case TCOON:
Mark Hounschellc43846a2014-03-19 11:10:51 -04005039 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
5040 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
Mark Hounschella6792a32014-02-19 13:11:59 -05005041 dgap_tty_start(tty);
Mark Hounschellcf42c342014-02-28 12:42:09 -05005042 return 0;
Mark Hounschella6792a32014-02-19 13:11:59 -05005043 case TCOOFF:
Mark Hounschellc43846a2014-03-19 11:10:51 -04005044 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
5045 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
Mark Hounschella6792a32014-02-19 13:11:59 -05005046 dgap_tty_stop(tty);
Mark Hounschellcf42c342014-02-28 12:42:09 -05005047 return 0;
Mark Hounschella6792a32014-02-19 13:11:59 -05005048 case TCION:
Mark Hounschellc43846a2014-03-19 11:10:51 -04005049 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
5050 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
Mark Hounschella6792a32014-02-19 13:11:59 -05005051 /* Make the ld do it */
Mark Hounschellcf42c342014-02-28 12:42:09 -05005052 return -ENOIOCTLCMD;
Mark Hounschella6792a32014-02-19 13:11:59 -05005053 case TCIOFF:
Mark Hounschellc43846a2014-03-19 11:10:51 -04005054 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
5055 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
Mark Hounschella6792a32014-02-19 13:11:59 -05005056 /* Make the ld do it */
Mark Hounschellcf42c342014-02-28 12:42:09 -05005057 return -ENOIOCTLCMD;
Mark Hounschella6792a32014-02-19 13:11:59 -05005058 default:
Mark Hounschellc43846a2014-03-19 11:10:51 -04005059 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
5060 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
Mark Hounschellcf42c342014-02-28 12:42:09 -05005061 return -EINVAL;
Mark Hounschella6792a32014-02-19 13:11:59 -05005062 }
5063
5064 case DIGI_GETA:
5065 /* get information for ditty */
Mark Hounschellc43846a2014-03-19 11:10:51 -04005066 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
5067 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
Daeseok Youn4285c972014-07-11 19:23:34 +09005068 return dgap_tty_digigeta(ch, uarg);
Mark Hounschella6792a32014-02-19 13:11:59 -05005069
5070 case DIGI_SETAW:
5071 case DIGI_SETAF:
5072
5073 /* set information for ditty */
5074 if (cmd == (DIGI_SETAW)) {
Mark Hounschellc43846a2014-03-19 11:10:51 -04005075 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
5076 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
Mark Hounschella6792a32014-02-19 13:11:59 -05005077 rc = dgap_wait_for_drain(tty);
Mark Hounschell31960c12014-02-28 12:42:08 -05005078 if (rc)
Mark Hounschellcf42c342014-02-28 12:42:09 -05005079 return -EINTR;
Mark Hounschellc43846a2014-03-19 11:10:51 -04005080 spin_lock_irqsave(&bd->bd_lock, lock_flags);
5081 spin_lock_irqsave(&ch->ch_lock, lock_flags2);
Mark Hounschell305ec872014-02-28 12:42:13 -05005082 } else
Mark Hounschella6792a32014-02-19 13:11:59 -05005083 tty_ldisc_flush(tty);
Mark Hounschella6792a32014-02-19 13:11:59 -05005084 /* fall thru */
5085
5086 case DIGI_SETA:
Mark Hounschellc43846a2014-03-19 11:10:51 -04005087 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
5088 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
Daeseok Younffc11c12014-07-11 19:22:58 +09005089 return dgap_tty_digiseta(ch, bd, un, uarg);
Mark Hounschella6792a32014-02-19 13:11:59 -05005090
5091 case DIGI_GEDELAY:
Mark Hounschellc43846a2014-03-19 11:10:51 -04005092 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
5093 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
Mark Hounschellcf42c342014-02-28 12:42:09 -05005094 return dgap_tty_digigetedelay(tty, uarg);
Mark Hounschella6792a32014-02-19 13:11:59 -05005095
5096 case DIGI_SEDELAY:
Mark Hounschellc43846a2014-03-19 11:10:51 -04005097 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
5098 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
Daeseok Younccbe7e52014-07-11 19:23:54 +09005099 return dgap_tty_digisetedelay(ch, bd, un, uarg);
Mark Hounschella6792a32014-02-19 13:11:59 -05005100
5101 case DIGI_GETCUSTOMBAUD:
Mark Hounschellc43846a2014-03-19 11:10:51 -04005102 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
5103 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
Daeseok Youn501bcd42014-07-11 19:24:14 +09005104 return dgap_tty_digigetcustombaud(ch, un, uarg);
Mark Hounschella6792a32014-02-19 13:11:59 -05005105
5106 case DIGI_SETCUSTOMBAUD:
Mark Hounschellc43846a2014-03-19 11:10:51 -04005107 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
5108 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
Daeseok Youndb6fc2d2014-07-11 19:24:34 +09005109 return dgap_tty_digisetcustombaud(ch, bd, un, uarg);
Mark Hounschella6792a32014-02-19 13:11:59 -05005110
5111 case DIGI_RESET_PORT:
5112 dgap_firmware_reset_port(ch);
Daeseok Younab6cdcb2014-07-11 19:22:39 +09005113 dgap_param(ch, bd, un->un_type);
Mark Hounschellc43846a2014-03-19 11:10:51 -04005114 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
5115 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
Mark Hounschella6792a32014-02-19 13:11:59 -05005116 return 0;
5117
5118 default:
Mark Hounschellc43846a2014-03-19 11:10:51 -04005119 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
5120 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
Mark Hounschella6792a32014-02-19 13:11:59 -05005121
Mark Hounschellcf42c342014-02-28 12:42:09 -05005122 return -ENOIOCTLCMD;
Mark Hounschella6792a32014-02-19 13:11:59 -05005123 }
5124}
Mark Hounschellb28ec882014-02-20 08:48:48 -05005125
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09005126static const struct tty_operations dgap_tty_ops = {
5127 .open = dgap_tty_open,
5128 .close = dgap_tty_close,
5129 .write = dgap_tty_write,
5130 .write_room = dgap_tty_write_room,
5131 .flush_buffer = dgap_tty_flush_buffer,
5132 .chars_in_buffer = dgap_tty_chars_in_buffer,
5133 .flush_chars = dgap_tty_flush_chars,
5134 .ioctl = dgap_tty_ioctl,
5135 .set_termios = dgap_tty_set_termios,
5136 .stop = dgap_tty_stop,
5137 .start = dgap_tty_start,
5138 .throttle = dgap_tty_throttle,
5139 .unthrottle = dgap_tty_unthrottle,
5140 .hangup = dgap_tty_hangup,
5141 .put_char = dgap_tty_put_char,
5142 .tiocmget = dgap_tty_tiocmget,
5143 .tiocmset = dgap_tty_tiocmset,
5144 .break_ctl = dgap_tty_send_break,
5145 .wait_until_sent = dgap_tty_wait_until_sent,
5146 .send_xchar = dgap_tty_send_xchar
5147};
5148
5149/************************************************************************
5150 *
5151 * TTY Initialization/Cleanup Functions
5152 *
5153 ************************************************************************/
5154
5155/*
5156 * dgap_tty_register()
5157 *
5158 * Init the tty subsystem for this board.
5159 */
5160static int dgap_tty_register(struct board_t *brd)
5161{
5162 int rc;
5163
Daeseok Youndc3cfcd2014-11-06 19:26:47 +09005164 brd->serial_driver = tty_alloc_driver(MAXPORTS,
5165 TTY_DRIVER_REAL_RAW |
5166 TTY_DRIVER_DYNAMIC_DEV |
5167 TTY_DRIVER_HARDWARE_BREAK);
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09005168 if (IS_ERR(brd->serial_driver))
5169 return PTR_ERR(brd->serial_driver);
5170
5171 snprintf(brd->serial_name, MAXTTYNAMELEN, "tty_dgap_%d_",
5172 brd->boardnum);
5173 brd->serial_driver->name = brd->serial_name;
5174 brd->serial_driver->name_base = 0;
5175 brd->serial_driver->major = 0;
5176 brd->serial_driver->minor_start = 0;
5177 brd->serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
5178 brd->serial_driver->subtype = SERIAL_TYPE_NORMAL;
5179 brd->serial_driver->init_termios = dgap_default_termios;
5180 brd->serial_driver->driver_name = DRVSTR;
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09005181
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09005182 /*
5183 * Entry points for driver. Called by the kernel from
5184 * tty_io.c and n_tty.c.
5185 */
5186 tty_set_operations(brd->serial_driver, &dgap_tty_ops);
5187
5188 /*
5189 * If we're doing transparent print, we have to do all of the above
5190 * again, separately so we don't get the LD confused about what major
5191 * we are when we get into the dgap_tty_open() routine.
5192 */
Daeseok Youndc3cfcd2014-11-06 19:26:47 +09005193 brd->print_driver = tty_alloc_driver(MAXPORTS,
5194 TTY_DRIVER_REAL_RAW |
5195 TTY_DRIVER_DYNAMIC_DEV |
5196 TTY_DRIVER_HARDWARE_BREAK);
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09005197 if (IS_ERR(brd->print_driver)) {
5198 rc = PTR_ERR(brd->print_driver);
5199 goto free_serial_drv;
5200 }
5201
5202 snprintf(brd->print_name, MAXTTYNAMELEN, "pr_dgap_%d_",
5203 brd->boardnum);
5204 brd->print_driver->name = brd->print_name;
5205 brd->print_driver->name_base = 0;
5206 brd->print_driver->major = 0;
5207 brd->print_driver->minor_start = 0;
5208 brd->print_driver->type = TTY_DRIVER_TYPE_SERIAL;
5209 brd->print_driver->subtype = SERIAL_TYPE_NORMAL;
5210 brd->print_driver->init_termios = dgap_default_termios;
5211 brd->print_driver->driver_name = DRVSTR;
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09005212
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09005213 /*
5214 * Entry points for driver. Called by the kernel from
5215 * tty_io.c and n_tty.c.
5216 */
5217 tty_set_operations(brd->print_driver, &dgap_tty_ops);
5218
5219 /* Register tty devices */
5220 rc = tty_register_driver(brd->serial_driver);
5221 if (rc < 0)
5222 goto free_print_drv;
5223
5224 /* Register Transparent Print devices */
5225 rc = tty_register_driver(brd->print_driver);
5226 if (rc < 0)
5227 goto unregister_serial_drv;
5228
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09005229 return 0;
5230
5231unregister_serial_drv:
5232 tty_unregister_driver(brd->serial_driver);
5233free_print_drv:
5234 put_tty_driver(brd->print_driver);
5235free_serial_drv:
5236 put_tty_driver(brd->serial_driver);
5237
5238 return rc;
5239}
5240
5241static void dgap_tty_unregister(struct board_t *brd)
5242{
5243 tty_unregister_driver(brd->print_driver);
5244 tty_unregister_driver(brd->serial_driver);
5245 put_tty_driver(brd->print_driver);
5246 put_tty_driver(brd->serial_driver);
5247}
5248
Daeseok Youn3f7fa942014-06-13 18:22:05 +09005249static int dgap_alloc_flipbuf(struct board_t *brd)
Mark Hounschellb053bb82014-02-19 13:12:00 -05005250{
Mark Hounschellb28ec882014-02-20 08:48:48 -05005251 /*
Mark Hounschellb28ec882014-02-20 08:48:48 -05005252 * allocate flip buffer for board.
Mark Hounschellb053bb82014-02-19 13:12:00 -05005253 */
Daeseok Youn8148f372014-06-03 17:01:31 +09005254 brd->flipbuf = kmalloc(MYFLIPLEN, GFP_KERNEL);
5255 if (!brd->flipbuf)
Mark Hounschellb28ec882014-02-20 08:48:48 -05005256 return -ENOMEM;
Mark Hounschellb053bb82014-02-19 13:12:00 -05005257
Daeseok Youn8148f372014-06-03 17:01:31 +09005258 brd->flipflagbuf = kmalloc(MYFLIPLEN, GFP_KERNEL);
5259 if (!brd->flipflagbuf) {
5260 kfree(brd->flipbuf);
Mark Hounschellb28ec882014-02-20 08:48:48 -05005261 return -ENOMEM;
Mark Hounschellb053bb82014-02-19 13:12:00 -05005262 }
5263
Mark Hounschellb28ec882014-02-20 08:48:48 -05005264 return 0;
Mark Hounschellb053bb82014-02-19 13:12:00 -05005265}
5266
Daeseok Youn91177d52014-06-13 18:23:15 +09005267static void dgap_free_flipbuf(struct board_t *brd)
5268{
5269 kfree(brd->flipbuf);
5270 kfree(brd->flipflagbuf);
5271}
5272
Mark Hounschell3eb14152014-03-04 09:33:46 -05005273static struct board_t *dgap_verify_board(struct device *p)
5274{
5275 struct board_t *bd;
5276
5277 if (!p)
5278 return NULL;
5279
5280 bd = dev_get_drvdata(p);
5281 if (!bd || bd->magic != DGAP_BOARD_MAGIC || bd->state != BOARD_READY)
5282 return NULL;
5283
5284 return bd;
5285}
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005286
Mark Hounschell84e88282014-03-10 15:46:54 -04005287static ssize_t dgap_ports_state_show(struct device *p,
5288 struct device_attribute *attr,
5289 char *buf)
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005290{
5291 struct board_t *bd;
5292 int count = 0;
Tapasweni Pathakac2f46c2014-10-26 20:38:32 +05305293 unsigned int i;
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005294
Mark Hounschell3eb14152014-03-04 09:33:46 -05005295 bd = dgap_verify_board(p);
5296 if (!bd)
5297 return 0;
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005298
5299 for (i = 0; i < bd->nasync; i++) {
5300 count += snprintf(buf + count, PAGE_SIZE - count,
5301 "%d %s\n", bd->channels[i]->ch_portnum,
5302 bd->channels[i]->ch_open_count ? "Open" : "Closed");
5303 }
5304 return count;
5305}
5306static DEVICE_ATTR(ports_state, S_IRUSR, dgap_ports_state_show, NULL);
5307
Mark Hounschell84e88282014-03-10 15:46:54 -04005308static ssize_t dgap_ports_baud_show(struct device *p,
5309 struct device_attribute *attr,
5310 char *buf)
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005311{
5312 struct board_t *bd;
5313 int count = 0;
Tapasweni Pathakac2f46c2014-10-26 20:38:32 +05305314 unsigned int i;
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005315
Mark Hounschell3eb14152014-03-04 09:33:46 -05005316 bd = dgap_verify_board(p);
5317 if (!bd)
5318 return 0;
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005319
5320 for (i = 0; i < bd->nasync; i++) {
Mark Hounschell84e88282014-03-10 15:46:54 -04005321 count += snprintf(buf + count, PAGE_SIZE - count, "%d %d\n",
5322 bd->channels[i]->ch_portnum,
5323 bd->channels[i]->ch_baud_info);
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005324 }
5325 return count;
5326}
5327static DEVICE_ATTR(ports_baud, S_IRUSR, dgap_ports_baud_show, NULL);
5328
Mark Hounschell84e88282014-03-10 15:46:54 -04005329static ssize_t dgap_ports_msignals_show(struct device *p,
5330 struct device_attribute *attr,
5331 char *buf)
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005332{
5333 struct board_t *bd;
5334 int count = 0;
Tapasweni Pathakac2f46c2014-10-26 20:38:32 +05305335 unsigned int i;
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005336
Mark Hounschell3eb14152014-03-04 09:33:46 -05005337 bd = dgap_verify_board(p);
5338 if (!bd)
5339 return 0;
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005340
5341 for (i = 0; i < bd->nasync; i++) {
Mark Hounschell305ec872014-02-28 12:42:13 -05005342 if (bd->channels[i]->ch_open_count)
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005343 count += snprintf(buf + count, PAGE_SIZE - count,
Mark Hounschell84e88282014-03-10 15:46:54 -04005344 "%d %s %s %s %s %s %s\n",
5345 bd->channels[i]->ch_portnum,
5346 (bd->channels[i]->ch_mostat &
5347 UART_MCR_RTS) ? "RTS" : "",
5348 (bd->channels[i]->ch_mistat &
5349 UART_MSR_CTS) ? "CTS" : "",
5350 (bd->channels[i]->ch_mostat &
5351 UART_MCR_DTR) ? "DTR" : "",
5352 (bd->channels[i]->ch_mistat &
5353 UART_MSR_DSR) ? "DSR" : "",
5354 (bd->channels[i]->ch_mistat &
5355 UART_MSR_DCD) ? "DCD" : "",
5356 (bd->channels[i]->ch_mistat &
5357 UART_MSR_RI) ? "RI" : "");
Mark Hounschell305ec872014-02-28 12:42:13 -05005358 else
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005359 count += snprintf(buf + count, PAGE_SIZE - count,
5360 "%d\n", bd->channels[i]->ch_portnum);
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005361 }
5362 return count;
5363}
5364static DEVICE_ATTR(ports_msignals, S_IRUSR, dgap_ports_msignals_show, NULL);
5365
Mark Hounschell84e88282014-03-10 15:46:54 -04005366static ssize_t dgap_ports_iflag_show(struct device *p,
5367 struct device_attribute *attr,
5368 char *buf)
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005369{
5370 struct board_t *bd;
5371 int count = 0;
Tapasweni Pathakac2f46c2014-10-26 20:38:32 +05305372 unsigned int i;
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005373
Mark Hounschell3eb14152014-03-04 09:33:46 -05005374 bd = dgap_verify_board(p);
5375 if (!bd)
5376 return 0;
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005377
Mark Hounschell305ec872014-02-28 12:42:13 -05005378 for (i = 0; i < bd->nasync; i++)
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005379 count += snprintf(buf + count, PAGE_SIZE - count, "%d %x\n",
Mark Hounschell84e88282014-03-10 15:46:54 -04005380 bd->channels[i]->ch_portnum,
5381 bd->channels[i]->ch_c_iflag);
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005382 return count;
5383}
5384static DEVICE_ATTR(ports_iflag, S_IRUSR, dgap_ports_iflag_show, NULL);
5385
Mark Hounschell84e88282014-03-10 15:46:54 -04005386static ssize_t dgap_ports_cflag_show(struct device *p,
5387 struct device_attribute *attr,
5388 char *buf)
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005389{
5390 struct board_t *bd;
5391 int count = 0;
Tapasweni Pathakac2f46c2014-10-26 20:38:32 +05305392 unsigned int i;
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005393
Mark Hounschell3eb14152014-03-04 09:33:46 -05005394 bd = dgap_verify_board(p);
5395 if (!bd)
5396 return 0;
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005397
Mark Hounschell305ec872014-02-28 12:42:13 -05005398 for (i = 0; i < bd->nasync; i++)
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005399 count += snprintf(buf + count, PAGE_SIZE - count, "%d %x\n",
Mark Hounschell84e88282014-03-10 15:46:54 -04005400 bd->channels[i]->ch_portnum,
5401 bd->channels[i]->ch_c_cflag);
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005402 return count;
5403}
5404static DEVICE_ATTR(ports_cflag, S_IRUSR, dgap_ports_cflag_show, NULL);
5405
Mark Hounschell84e88282014-03-10 15:46:54 -04005406static ssize_t dgap_ports_oflag_show(struct device *p,
5407 struct device_attribute *attr,
5408 char *buf)
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005409{
5410 struct board_t *bd;
5411 int count = 0;
Tapasweni Pathakac2f46c2014-10-26 20:38:32 +05305412 unsigned int i;
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005413
Mark Hounschell3eb14152014-03-04 09:33:46 -05005414 bd = dgap_verify_board(p);
5415 if (!bd)
5416 return 0;
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005417
Mark Hounschell305ec872014-02-28 12:42:13 -05005418 for (i = 0; i < bd->nasync; i++)
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005419 count += snprintf(buf + count, PAGE_SIZE - count, "%d %x\n",
Mark Hounschell84e88282014-03-10 15:46:54 -04005420 bd->channels[i]->ch_portnum,
5421 bd->channels[i]->ch_c_oflag);
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005422 return count;
5423}
5424static DEVICE_ATTR(ports_oflag, S_IRUSR, dgap_ports_oflag_show, NULL);
5425
Mark Hounschell84e88282014-03-10 15:46:54 -04005426static ssize_t dgap_ports_lflag_show(struct device *p,
5427 struct device_attribute *attr,
5428 char *buf)
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005429{
5430 struct board_t *bd;
5431 int count = 0;
Tapasweni Pathakac2f46c2014-10-26 20:38:32 +05305432 unsigned int i;
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005433
Mark Hounschell3eb14152014-03-04 09:33:46 -05005434 bd = dgap_verify_board(p);
5435 if (!bd)
5436 return 0;
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005437
Mark Hounschell305ec872014-02-28 12:42:13 -05005438 for (i = 0; i < bd->nasync; i++)
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005439 count += snprintf(buf + count, PAGE_SIZE - count, "%d %x\n",
Mark Hounschell84e88282014-03-10 15:46:54 -04005440 bd->channels[i]->ch_portnum,
5441 bd->channels[i]->ch_c_lflag);
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005442 return count;
5443}
5444static DEVICE_ATTR(ports_lflag, S_IRUSR, dgap_ports_lflag_show, NULL);
5445
Mark Hounschell84e88282014-03-10 15:46:54 -04005446static ssize_t dgap_ports_digi_flag_show(struct device *p,
5447 struct device_attribute *attr,
5448 char *buf)
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005449{
5450 struct board_t *bd;
5451 int count = 0;
Tapasweni Pathakac2f46c2014-10-26 20:38:32 +05305452 unsigned int i;
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005453
Mark Hounschell3eb14152014-03-04 09:33:46 -05005454 bd = dgap_verify_board(p);
5455 if (!bd)
5456 return 0;
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005457
Mark Hounschell305ec872014-02-28 12:42:13 -05005458 for (i = 0; i < bd->nasync; i++)
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005459 count += snprintf(buf + count, PAGE_SIZE - count, "%d %x\n",
Mark Hounschell84e88282014-03-10 15:46:54 -04005460 bd->channels[i]->ch_portnum,
5461 bd->channels[i]->ch_digi.digi_flags);
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005462 return count;
5463}
5464static DEVICE_ATTR(ports_digi_flag, S_IRUSR, dgap_ports_digi_flag_show, NULL);
5465
Mark Hounschell84e88282014-03-10 15:46:54 -04005466static ssize_t dgap_ports_rxcount_show(struct device *p,
5467 struct device_attribute *attr,
5468 char *buf)
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005469{
5470 struct board_t *bd;
5471 int count = 0;
Tapasweni Pathakac2f46c2014-10-26 20:38:32 +05305472 unsigned int i;
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005473
Mark Hounschell3eb14152014-03-04 09:33:46 -05005474 bd = dgap_verify_board(p);
5475 if (!bd)
5476 return 0;
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005477
Mark Hounschell305ec872014-02-28 12:42:13 -05005478 for (i = 0; i < bd->nasync; i++)
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005479 count += snprintf(buf + count, PAGE_SIZE - count, "%d %ld\n",
Mark Hounschell84e88282014-03-10 15:46:54 -04005480 bd->channels[i]->ch_portnum,
5481 bd->channels[i]->ch_rxcount);
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005482 return count;
5483}
5484static DEVICE_ATTR(ports_rxcount, S_IRUSR, dgap_ports_rxcount_show, NULL);
5485
Mark Hounschell84e88282014-03-10 15:46:54 -04005486static ssize_t dgap_ports_txcount_show(struct device *p,
5487 struct device_attribute *attr,
5488 char *buf)
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005489{
5490 struct board_t *bd;
5491 int count = 0;
Tapasweni Pathakac2f46c2014-10-26 20:38:32 +05305492 unsigned int i;
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005493
Mark Hounschell3eb14152014-03-04 09:33:46 -05005494 bd = dgap_verify_board(p);
5495 if (!bd)
5496 return 0;
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005497
Mark Hounschell305ec872014-02-28 12:42:13 -05005498 for (i = 0; i < bd->nasync; i++)
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005499 count += snprintf(buf + count, PAGE_SIZE - count, "%d %ld\n",
Mark Hounschell84e88282014-03-10 15:46:54 -04005500 bd->channels[i]->ch_portnum,
5501 bd->channels[i]->ch_txcount);
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005502 return count;
5503}
5504static DEVICE_ATTR(ports_txcount, S_IRUSR, dgap_ports_txcount_show, NULL);
5505
Mark Hounschell84e88282014-03-10 15:46:54 -04005506static ssize_t dgap_tty_state_show(struct device *d,
5507 struct device_attribute *attr,
5508 char *buf)
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005509{
5510 struct board_t *bd;
5511 struct channel_t *ch;
5512 struct un_t *un;
5513
5514 if (!d)
Mark Hounschellcf42c342014-02-28 12:42:09 -05005515 return 0;
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005516 un = dev_get_drvdata(d);
5517 if (!un || un->magic != DGAP_UNIT_MAGIC)
Mark Hounschellcf42c342014-02-28 12:42:09 -05005518 return 0;
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005519 ch = un->un_ch;
5520 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
Mark Hounschellcf42c342014-02-28 12:42:09 -05005521 return 0;
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005522 bd = ch->ch_bd;
5523 if (!bd || bd->magic != DGAP_BOARD_MAGIC)
Mark Hounschellcf42c342014-02-28 12:42:09 -05005524 return 0;
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005525 if (bd->state != BOARD_READY)
Mark Hounschellcf42c342014-02-28 12:42:09 -05005526 return 0;
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005527
Mark Hounschell84e88282014-03-10 15:46:54 -04005528 return snprintf(buf, PAGE_SIZE, "%s", un->un_open_count ?
5529 "Open" : "Closed");
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005530}
5531static DEVICE_ATTR(state, S_IRUSR, dgap_tty_state_show, NULL);
5532
Mark Hounschell84e88282014-03-10 15:46:54 -04005533static ssize_t dgap_tty_baud_show(struct device *d,
5534 struct device_attribute *attr,
5535 char *buf)
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005536{
5537 struct board_t *bd;
5538 struct channel_t *ch;
5539 struct un_t *un;
5540
5541 if (!d)
Mark Hounschellcf42c342014-02-28 12:42:09 -05005542 return 0;
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005543 un = dev_get_drvdata(d);
5544 if (!un || un->magic != DGAP_UNIT_MAGIC)
Mark Hounschellcf42c342014-02-28 12:42:09 -05005545 return 0;
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005546 ch = un->un_ch;
5547 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
Mark Hounschellcf42c342014-02-28 12:42:09 -05005548 return 0;
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005549 bd = ch->ch_bd;
5550 if (!bd || bd->magic != DGAP_BOARD_MAGIC)
Mark Hounschellcf42c342014-02-28 12:42:09 -05005551 return 0;
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005552 if (bd->state != BOARD_READY)
Mark Hounschellcf42c342014-02-28 12:42:09 -05005553 return 0;
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005554
5555 return snprintf(buf, PAGE_SIZE, "%d\n", ch->ch_baud_info);
5556}
5557static DEVICE_ATTR(baud, S_IRUSR, dgap_tty_baud_show, NULL);
5558
Mark Hounschell84e88282014-03-10 15:46:54 -04005559static ssize_t dgap_tty_msignals_show(struct device *d,
5560 struct device_attribute *attr,
5561 char *buf)
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005562{
5563 struct board_t *bd;
5564 struct channel_t *ch;
5565 struct un_t *un;
5566
5567 if (!d)
Mark Hounschellcf42c342014-02-28 12:42:09 -05005568 return 0;
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005569 un = dev_get_drvdata(d);
5570 if (!un || un->magic != DGAP_UNIT_MAGIC)
Mark Hounschellcf42c342014-02-28 12:42:09 -05005571 return 0;
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005572 ch = un->un_ch;
5573 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
Mark Hounschellcf42c342014-02-28 12:42:09 -05005574 return 0;
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005575 bd = ch->ch_bd;
5576 if (!bd || bd->magic != DGAP_BOARD_MAGIC)
Mark Hounschellcf42c342014-02-28 12:42:09 -05005577 return 0;
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005578 if (bd->state != BOARD_READY)
Mark Hounschellcf42c342014-02-28 12:42:09 -05005579 return 0;
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005580
5581 if (ch->ch_open_count) {
5582 return snprintf(buf, PAGE_SIZE, "%s %s %s %s %s %s\n",
5583 (ch->ch_mostat & UART_MCR_RTS) ? "RTS" : "",
5584 (ch->ch_mistat & UART_MSR_CTS) ? "CTS" : "",
5585 (ch->ch_mostat & UART_MCR_DTR) ? "DTR" : "",
5586 (ch->ch_mistat & UART_MSR_DSR) ? "DSR" : "",
5587 (ch->ch_mistat & UART_MSR_DCD) ? "DCD" : "",
5588 (ch->ch_mistat & UART_MSR_RI) ? "RI" : "");
5589 }
5590 return 0;
5591}
5592static DEVICE_ATTR(msignals, S_IRUSR, dgap_tty_msignals_show, NULL);
5593
Mark Hounschell84e88282014-03-10 15:46:54 -04005594static ssize_t dgap_tty_iflag_show(struct device *d,
5595 struct device_attribute *attr,
5596 char *buf)
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005597{
5598 struct board_t *bd;
5599 struct channel_t *ch;
5600 struct un_t *un;
5601
5602 if (!d)
Mark Hounschellcf42c342014-02-28 12:42:09 -05005603 return 0;
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005604 un = dev_get_drvdata(d);
5605 if (!un || un->magic != DGAP_UNIT_MAGIC)
Mark Hounschellcf42c342014-02-28 12:42:09 -05005606 return 0;
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005607 ch = un->un_ch;
5608 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
Mark Hounschellcf42c342014-02-28 12:42:09 -05005609 return 0;
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005610 bd = ch->ch_bd;
5611 if (!bd || bd->magic != DGAP_BOARD_MAGIC)
Mark Hounschellcf42c342014-02-28 12:42:09 -05005612 return 0;
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005613 if (bd->state != BOARD_READY)
Mark Hounschellcf42c342014-02-28 12:42:09 -05005614 return 0;
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005615
5616 return snprintf(buf, PAGE_SIZE, "%x\n", ch->ch_c_iflag);
5617}
5618static DEVICE_ATTR(iflag, S_IRUSR, dgap_tty_iflag_show, NULL);
5619
Mark Hounschell84e88282014-03-10 15:46:54 -04005620static ssize_t dgap_tty_cflag_show(struct device *d,
5621 struct device_attribute *attr,
5622 char *buf)
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005623{
5624 struct board_t *bd;
5625 struct channel_t *ch;
5626 struct un_t *un;
5627
5628 if (!d)
Mark Hounschellcf42c342014-02-28 12:42:09 -05005629 return 0;
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005630 un = dev_get_drvdata(d);
5631 if (!un || un->magic != DGAP_UNIT_MAGIC)
Mark Hounschellcf42c342014-02-28 12:42:09 -05005632 return 0;
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005633 ch = un->un_ch;
5634 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
Mark Hounschellcf42c342014-02-28 12:42:09 -05005635 return 0;
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005636 bd = ch->ch_bd;
5637 if (!bd || bd->magic != DGAP_BOARD_MAGIC)
Mark Hounschellcf42c342014-02-28 12:42:09 -05005638 return 0;
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005639 if (bd->state != BOARD_READY)
Mark Hounschellcf42c342014-02-28 12:42:09 -05005640 return 0;
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005641
5642 return snprintf(buf, PAGE_SIZE, "%x\n", ch->ch_c_cflag);
5643}
5644static DEVICE_ATTR(cflag, S_IRUSR, dgap_tty_cflag_show, NULL);
5645
Mark Hounschell84e88282014-03-10 15:46:54 -04005646static ssize_t dgap_tty_oflag_show(struct device *d,
5647 struct device_attribute *attr,
5648 char *buf)
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005649{
5650 struct board_t *bd;
5651 struct channel_t *ch;
5652 struct un_t *un;
5653
5654 if (!d)
Mark Hounschellcf42c342014-02-28 12:42:09 -05005655 return 0;
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005656 un = dev_get_drvdata(d);
5657 if (!un || un->magic != DGAP_UNIT_MAGIC)
Mark Hounschellcf42c342014-02-28 12:42:09 -05005658 return 0;
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005659 ch = un->un_ch;
5660 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
Mark Hounschellcf42c342014-02-28 12:42:09 -05005661 return 0;
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005662 bd = ch->ch_bd;
5663 if (!bd || bd->magic != DGAP_BOARD_MAGIC)
Mark Hounschellcf42c342014-02-28 12:42:09 -05005664 return 0;
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005665 if (bd->state != BOARD_READY)
Mark Hounschellcf42c342014-02-28 12:42:09 -05005666 return 0;
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005667
5668 return snprintf(buf, PAGE_SIZE, "%x\n", ch->ch_c_oflag);
5669}
5670static DEVICE_ATTR(oflag, S_IRUSR, dgap_tty_oflag_show, NULL);
5671
Mark Hounschell84e88282014-03-10 15:46:54 -04005672static ssize_t dgap_tty_lflag_show(struct device *d,
5673 struct device_attribute *attr,
5674 char *buf)
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005675{
5676 struct board_t *bd;
5677 struct channel_t *ch;
5678 struct un_t *un;
5679
5680 if (!d)
Mark Hounschellcf42c342014-02-28 12:42:09 -05005681 return 0;
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005682 un = dev_get_drvdata(d);
5683 if (!un || un->magic != DGAP_UNIT_MAGIC)
Mark Hounschellcf42c342014-02-28 12:42:09 -05005684 return 0;
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005685 ch = un->un_ch;
5686 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
Mark Hounschellcf42c342014-02-28 12:42:09 -05005687 return 0;
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005688 bd = ch->ch_bd;
5689 if (!bd || bd->magic != DGAP_BOARD_MAGIC)
Mark Hounschellcf42c342014-02-28 12:42:09 -05005690 return 0;
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005691 if (bd->state != BOARD_READY)
Mark Hounschellcf42c342014-02-28 12:42:09 -05005692 return 0;
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005693
5694 return snprintf(buf, PAGE_SIZE, "%x\n", ch->ch_c_lflag);
5695}
5696static DEVICE_ATTR(lflag, S_IRUSR, dgap_tty_lflag_show, NULL);
5697
Mark Hounschell84e88282014-03-10 15:46:54 -04005698static ssize_t dgap_tty_digi_flag_show(struct device *d,
5699 struct device_attribute *attr,
5700 char *buf)
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005701{
5702 struct board_t *bd;
5703 struct channel_t *ch;
5704 struct un_t *un;
5705
5706 if (!d)
Mark Hounschellcf42c342014-02-28 12:42:09 -05005707 return 0;
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005708 un = dev_get_drvdata(d);
5709 if (!un || un->magic != DGAP_UNIT_MAGIC)
Mark Hounschellcf42c342014-02-28 12:42:09 -05005710 return 0;
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005711 ch = un->un_ch;
5712 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
Mark Hounschellcf42c342014-02-28 12:42:09 -05005713 return 0;
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005714 bd = ch->ch_bd;
5715 if (!bd || bd->magic != DGAP_BOARD_MAGIC)
Mark Hounschellcf42c342014-02-28 12:42:09 -05005716 return 0;
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005717 if (bd->state != BOARD_READY)
Mark Hounschellcf42c342014-02-28 12:42:09 -05005718 return 0;
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005719
5720 return snprintf(buf, PAGE_SIZE, "%x\n", ch->ch_digi.digi_flags);
5721}
5722static DEVICE_ATTR(digi_flag, S_IRUSR, dgap_tty_digi_flag_show, NULL);
5723
Mark Hounschell84e88282014-03-10 15:46:54 -04005724static ssize_t dgap_tty_rxcount_show(struct device *d,
5725 struct device_attribute *attr,
5726 char *buf)
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005727{
5728 struct board_t *bd;
5729 struct channel_t *ch;
5730 struct un_t *un;
5731
5732 if (!d)
Mark Hounschellcf42c342014-02-28 12:42:09 -05005733 return 0;
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005734 un = dev_get_drvdata(d);
5735 if (!un || un->magic != DGAP_UNIT_MAGIC)
Mark Hounschellcf42c342014-02-28 12:42:09 -05005736 return 0;
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005737 ch = un->un_ch;
5738 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
Mark Hounschellcf42c342014-02-28 12:42:09 -05005739 return 0;
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005740 bd = ch->ch_bd;
5741 if (!bd || bd->magic != DGAP_BOARD_MAGIC)
Mark Hounschellcf42c342014-02-28 12:42:09 -05005742 return 0;
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005743 if (bd->state != BOARD_READY)
Mark Hounschellcf42c342014-02-28 12:42:09 -05005744 return 0;
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005745
5746 return snprintf(buf, PAGE_SIZE, "%ld\n", ch->ch_rxcount);
5747}
5748static DEVICE_ATTR(rxcount, S_IRUSR, dgap_tty_rxcount_show, NULL);
5749
Mark Hounschell84e88282014-03-10 15:46:54 -04005750static ssize_t dgap_tty_txcount_show(struct device *d,
5751 struct device_attribute *attr,
5752 char *buf)
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005753{
5754 struct board_t *bd;
5755 struct channel_t *ch;
5756 struct un_t *un;
5757
5758 if (!d)
Mark Hounschellcf42c342014-02-28 12:42:09 -05005759 return 0;
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005760 un = dev_get_drvdata(d);
5761 if (!un || un->magic != DGAP_UNIT_MAGIC)
Mark Hounschellcf42c342014-02-28 12:42:09 -05005762 return 0;
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005763 ch = un->un_ch;
5764 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
Mark Hounschellcf42c342014-02-28 12:42:09 -05005765 return 0;
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005766 bd = ch->ch_bd;
5767 if (!bd || bd->magic != DGAP_BOARD_MAGIC)
Mark Hounschellcf42c342014-02-28 12:42:09 -05005768 return 0;
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005769 if (bd->state != BOARD_READY)
Mark Hounschellcf42c342014-02-28 12:42:09 -05005770 return 0;
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005771
5772 return snprintf(buf, PAGE_SIZE, "%ld\n", ch->ch_txcount);
5773}
5774static DEVICE_ATTR(txcount, S_IRUSR, dgap_tty_txcount_show, NULL);
5775
Mark Hounschell84e88282014-03-10 15:46:54 -04005776static ssize_t dgap_tty_name_show(struct device *d,
5777 struct device_attribute *attr,
5778 char *buf)
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005779{
5780 struct board_t *bd;
5781 struct channel_t *ch;
5782 struct un_t *un;
Mark Hounschell174efc12014-05-23 12:54:04 -04005783 int cn;
5784 int bn;
Mark Hounschell7dfa3832014-05-23 10:14:02 -04005785 struct cnode *cptr;
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005786 int found = FALSE;
5787 int ncount = 0;
5788 int starto = 0;
Mark Hounschell7dfa3832014-05-23 10:14:02 -04005789 int i;
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005790
5791 if (!d)
Mark Hounschellcf42c342014-02-28 12:42:09 -05005792 return 0;
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005793 un = dev_get_drvdata(d);
5794 if (!un || un->magic != DGAP_UNIT_MAGIC)
Mark Hounschellcf42c342014-02-28 12:42:09 -05005795 return 0;
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005796 ch = un->un_ch;
5797 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
Mark Hounschellcf42c342014-02-28 12:42:09 -05005798 return 0;
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005799 bd = ch->ch_bd;
5800 if (!bd || bd->magic != DGAP_BOARD_MAGIC)
Mark Hounschellcf42c342014-02-28 12:42:09 -05005801 return 0;
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005802 if (bd->state != BOARD_READY)
Mark Hounschellcf42c342014-02-28 12:42:09 -05005803 return 0;
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005804
Mark Hounschell7d6069d72014-02-28 12:42:10 -05005805 bn = bd->boardnum;
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005806 cn = ch->ch_portnum;
5807
5808 for (cptr = bd->bd_config; cptr; cptr = cptr->next) {
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005809 if ((cptr->type == BNODE) &&
Mark Hounschell84e88282014-03-10 15:46:54 -04005810 ((cptr->u.board.type == APORT2_920P) ||
5811 (cptr->u.board.type == APORT4_920P) ||
5812 (cptr->u.board.type == APORT8_920P) ||
5813 (cptr->u.board.type == PAPORT4) ||
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005814 (cptr->u.board.type == PAPORT8))) {
Mark Hounschell84e88282014-03-10 15:46:54 -04005815 found = TRUE;
5816 if (cptr->u.board.v_start)
5817 starto = cptr->u.board.start;
5818 else
5819 starto = 1;
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005820 }
5821
5822 if (cptr->type == TNODE && found == TRUE) {
5823 char *ptr1;
Daeseok Youn96045db2014-07-02 16:07:16 +09005824
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005825 if (strstr(cptr->u.ttyname, "tty")) {
5826 ptr1 = cptr->u.ttyname;
5827 ptr1 += 3;
Mark Hounschell305ec872014-02-28 12:42:13 -05005828 } else
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005829 ptr1 = cptr->u.ttyname;
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005830
Mark Hounschellbbfbe832014-03-19 11:10:52 -04005831 for (i = 0; i < dgap_config_get_num_prts(bd); i++) {
5832 if (cn != i)
5833 continue;
5834
5835 return snprintf(buf, PAGE_SIZE, "%s%s%02d\n",
5836 (un->un_type == DGAP_PRINT) ?
5837 "pr" : "tty",
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005838 ptr1, i + starto);
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005839 }
5840 }
5841
5842 if (cptr->type == CNODE) {
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005843 for (i = 0; i < cptr->u.conc.nport; i++) {
Mark Hounschellbbfbe832014-03-19 11:10:52 -04005844 if (cn != (i + ncount))
5845 continue;
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005846
Mark Hounschell67d5dc82014-03-19 15:46:57 -04005847 return snprintf(buf, PAGE_SIZE, "%s%s%02ld\n",
Mark Hounschellbbfbe832014-03-19 11:10:52 -04005848 (un->un_type == DGAP_PRINT) ?
5849 "pr" : "tty",
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005850 cptr->u.conc.id,
Mark Hounschellbbfbe832014-03-19 11:10:52 -04005851 i + (cptr->u.conc.v_start ?
5852 cptr->u.conc.start : 1));
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005853 }
5854
5855 ncount += cptr->u.conc.nport;
5856 }
5857
5858 if (cptr->type == MNODE) {
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005859 for (i = 0; i < cptr->u.module.nport; i++) {
Mark Hounschellbbfbe832014-03-19 11:10:52 -04005860 if (cn != (i + ncount))
5861 continue;
5862
Mark Hounschell67d5dc82014-03-19 15:46:57 -04005863 return snprintf(buf, PAGE_SIZE, "%s%s%02ld\n",
Mark Hounschellbbfbe832014-03-19 11:10:52 -04005864 (un->un_type == DGAP_PRINT) ?
5865 "pr" : "tty",
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005866 cptr->u.module.id,
Mark Hounschellbbfbe832014-03-19 11:10:52 -04005867 i + (cptr->u.module.v_start ?
5868 cptr->u.module.start : 1));
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005869 }
5870
5871 ncount += cptr->u.module.nport;
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005872 }
5873 }
5874
5875 return snprintf(buf, PAGE_SIZE, "%s_dgap_%d_%d\n",
5876 (un->un_type == DGAP_PRINT) ? "pr" : "tty", bn, cn);
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005877}
5878static DEVICE_ATTR(custom_name, S_IRUSR, dgap_tty_name_show, NULL);
5879
Mark Hounschell7568f7d2014-02-19 13:12:01 -05005880static struct attribute *dgap_sysfs_tty_entries[] = {
5881 &dev_attr_state.attr,
5882 &dev_attr_baud.attr,
5883 &dev_attr_msignals.attr,
5884 &dev_attr_iflag.attr,
5885 &dev_attr_cflag.attr,
5886 &dev_attr_oflag.attr,
5887 &dev_attr_lflag.attr,
5888 &dev_attr_digi_flag.attr,
5889 &dev_attr_rxcount.attr,
5890 &dev_attr_txcount.attr,
5891 &dev_attr_custom_name.attr,
5892 NULL
5893};
5894
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09005895
5896/* this function creates the sys files that will export each signal status
5897 * to sysfs each value will be put in a separate filename
5898 */
5899static void dgap_create_ports_sysfiles(struct board_t *bd)
5900{
5901 dev_set_drvdata(&bd->pdev->dev, bd);
Ioana Ciornei44209c92015-10-21 23:17:55 +03005902 device_create_file(&bd->pdev->dev, &dev_attr_ports_state);
5903 device_create_file(&bd->pdev->dev, &dev_attr_ports_baud);
5904 device_create_file(&bd->pdev->dev, &dev_attr_ports_msignals);
5905 device_create_file(&bd->pdev->dev, &dev_attr_ports_iflag);
5906 device_create_file(&bd->pdev->dev, &dev_attr_ports_cflag);
5907 device_create_file(&bd->pdev->dev, &dev_attr_ports_oflag);
5908 device_create_file(&bd->pdev->dev, &dev_attr_ports_lflag);
5909 device_create_file(&bd->pdev->dev, &dev_attr_ports_digi_flag);
5910 device_create_file(&bd->pdev->dev, &dev_attr_ports_rxcount);
5911 device_create_file(&bd->pdev->dev, &dev_attr_ports_txcount);
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09005912}
5913
5914/* removes all the sys files created for that port */
5915static void dgap_remove_ports_sysfiles(struct board_t *bd)
5916{
Ioana Ciornei44209c92015-10-21 23:17:55 +03005917 device_remove_file(&bd->pdev->dev, &dev_attr_ports_state);
5918 device_remove_file(&bd->pdev->dev, &dev_attr_ports_baud);
5919 device_remove_file(&bd->pdev->dev, &dev_attr_ports_msignals);
5920 device_remove_file(&bd->pdev->dev, &dev_attr_ports_iflag);
5921 device_remove_file(&bd->pdev->dev, &dev_attr_ports_cflag);
5922 device_remove_file(&bd->pdev->dev, &dev_attr_ports_oflag);
5923 device_remove_file(&bd->pdev->dev, &dev_attr_ports_lflag);
5924 device_remove_file(&bd->pdev->dev, &dev_attr_ports_digi_flag);
5925 device_remove_file(&bd->pdev->dev, &dev_attr_ports_rxcount);
5926 device_remove_file(&bd->pdev->dev, &dev_attr_ports_txcount);
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09005927}
5928
5929/*
5930 * Copies the BIOS code from the user to the board,
5931 * and starts the BIOS running.
5932 */
5933static void dgap_do_bios_load(struct board_t *brd, const u8 *ubios, int len)
5934{
5935 u8 __iomem *addr;
5936 uint offset;
5937 unsigned int i;
5938
5939 if (!brd || (brd->magic != DGAP_BOARD_MAGIC) || !brd->re_map_membase)
5940 return;
5941
5942 addr = brd->re_map_membase;
5943
5944 /*
5945 * clear POST area
5946 */
5947 for (i = 0; i < 16; i++)
5948 writeb(0, addr + POSTAREA + i);
5949
5950 /*
5951 * Download bios
5952 */
5953 offset = 0x1000;
5954 memcpy_toio(addr + offset, ubios, len);
5955
5956 writel(0x0bf00401, addr);
5957 writel(0, (addr + 4));
5958
5959 /* Clear the reset, and change states. */
5960 writeb(FEPCLR, brd->re_map_port);
5961}
5962
5963/*
5964 * Checks to see if the BIOS completed running on the card.
5965 */
5966static int dgap_test_bios(struct board_t *brd)
5967{
5968 u8 __iomem *addr;
5969 u16 word;
5970 u16 err1;
5971 u16 err2;
5972
5973 if (!brd || (brd->magic != DGAP_BOARD_MAGIC) || !brd->re_map_membase)
5974 return -EINVAL;
5975
5976 addr = brd->re_map_membase;
5977 word = readw(addr + POSTAREA);
5978
5979 /*
5980 * It can take 5-6 seconds for a board to
5981 * pass the bios self test and post results.
5982 * Give it 10 seconds.
5983 */
5984 brd->wait_for_bios = 0;
5985 while (brd->wait_for_bios < 1000) {
5986 /* Check to see if BIOS thinks board is good. (GD). */
Ioana Ciornei18122552015-10-21 23:17:54 +03005987 if (word == *(u16 *)"GD")
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09005988 return 0;
5989 msleep_interruptible(10);
5990 brd->wait_for_bios++;
5991 word = readw(addr + POSTAREA);
5992 }
5993
5994 /* Gave up on board after too long of time taken */
5995 err1 = readw(addr + SEQUENCE);
5996 err2 = readw(addr + ERROR);
5997 dev_warn(&brd->pdev->dev, "%s failed diagnostics. Error #(%x,%x).\n",
Ioana Ciornei629c7342015-10-21 23:17:53 +03005998 brd->name, err1, err2);
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09005999 brd->state = BOARD_FAILED;
6000 brd->dpastatus = BD_NOBIOS;
6001
6002 return -EIO;
6003}
6004
6005/*
6006 * Copies the FEP code from the user to the board,
6007 * and starts the FEP running.
6008 */
6009static void dgap_do_fep_load(struct board_t *brd, const u8 *ufep, int len)
6010{
6011 u8 __iomem *addr;
6012 uint offset;
6013
6014 if (!brd || (brd->magic != DGAP_BOARD_MAGIC) || !brd->re_map_membase)
6015 return;
6016
6017 addr = brd->re_map_membase;
6018
6019 /*
6020 * Download FEP
6021 */
6022 offset = 0x1000;
6023 memcpy_toio(addr + offset, ufep, len);
6024
6025 /*
6026 * If board is a concentrator product, we need to give
6027 * it its config string describing how the concentrators look.
6028 */
6029 if ((brd->type == PCX) || (brd->type == PEPC)) {
6030 u8 string[100];
6031 u8 __iomem *config;
6032 u8 *xconfig;
6033 unsigned int i = 0;
6034
6035 xconfig = dgap_create_config_string(brd, string);
6036
6037 /* Write string to board memory */
6038 config = addr + CONFIG;
6039 for (; i < CONFIGSIZE; i++, config++, xconfig++) {
6040 writeb(*xconfig, config);
6041 if ((*xconfig & 0xff) == 0xff)
6042 break;
6043 }
6044 }
6045
6046 writel(0xbfc01004, (addr + 0xc34));
6047 writel(0x3, (addr + 0xc30));
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09006048}
6049
6050/*
6051 * Waits for the FEP to report thats its ready for us to use.
6052 */
6053static int dgap_test_fep(struct board_t *brd)
6054{
6055 u8 __iomem *addr;
6056 u16 word;
6057 u16 err1;
6058 u16 err2;
6059
6060 if (!brd || (brd->magic != DGAP_BOARD_MAGIC) || !brd->re_map_membase)
6061 return -EINVAL;
6062
6063 addr = brd->re_map_membase;
6064 word = readw(addr + FEPSTAT);
6065
6066 /*
6067 * It can take 2-3 seconds for the FEP to
6068 * be up and running. Give it 5 secs.
6069 */
6070 brd->wait_for_fep = 0;
6071 while (brd->wait_for_fep < 500) {
6072 /* Check to see if FEP is up and running now. */
Ioana Ciornei18122552015-10-21 23:17:54 +03006073 if (word == *(u16 *)"OS") {
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09006074 /*
6075 * Check to see if the board can support FEP5+ commands.
6076 */
6077 word = readw(addr + FEP5_PLUS);
Ioana Ciornei18122552015-10-21 23:17:54 +03006078 if (word == *(u16 *)"5A")
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09006079 brd->bd_flags |= BD_FEP5PLUS;
6080
6081 return 0;
6082 }
6083 msleep_interruptible(10);
6084 brd->wait_for_fep++;
6085 word = readw(addr + FEPSTAT);
6086 }
6087
6088 /* Gave up on board after too long of time taken */
6089 err1 = readw(addr + SEQUENCE);
6090 err2 = readw(addr + ERROR);
6091 dev_warn(&brd->pdev->dev,
6092 "FEPOS for %s not functioning. Error #(%x,%x).\n",
6093 brd->name, err1, err2);
6094 brd->state = BOARD_FAILED;
6095 brd->dpastatus = BD_NOFEP;
6096
6097 return -EIO;
6098}
6099
6100/*
6101 * Physically forces the FEP5 card to reset itself.
6102 */
6103static void dgap_do_reset_board(struct board_t *brd)
6104{
6105 u8 check;
6106 u32 check1;
6107 u32 check2;
6108 unsigned int i;
6109
6110 if (!brd || (brd->magic != DGAP_BOARD_MAGIC) ||
6111 !brd->re_map_membase || !brd->re_map_port)
6112 return;
6113
6114 /* FEPRST does not vary among supported boards */
6115 writeb(FEPRST, brd->re_map_port);
6116
6117 for (i = 0; i <= 1000; i++) {
6118 check = readb(brd->re_map_port) & 0xe;
6119 if (check == FEPRST)
6120 break;
6121 udelay(10);
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09006122 }
6123 if (i > 1000) {
6124 dev_warn(&brd->pdev->dev,
6125 "dgap: Board not resetting... Failing board.\n");
6126 brd->state = BOARD_FAILED;
6127 brd->dpastatus = BD_NOFEP;
6128 return;
6129 }
6130
6131 /*
6132 * Make sure there really is memory out there.
6133 */
6134 writel(0xa55a3cc3, (brd->re_map_membase + LOWMEM));
6135 writel(0x5aa5c33c, (brd->re_map_membase + HIGHMEM));
6136 check1 = readl(brd->re_map_membase + LOWMEM);
6137 check2 = readl(brd->re_map_membase + HIGHMEM);
6138
6139 if ((check1 != 0xa55a3cc3) || (check2 != 0x5aa5c33c)) {
6140 dev_warn(&brd->pdev->dev,
6141 "No memory at %p for board.\n",
6142 brd->re_map_membase);
6143 brd->state = BOARD_FAILED;
6144 brd->dpastatus = BD_NOFEP;
6145 return;
6146 }
6147}
6148
6149#ifdef DIGI_CONCENTRATORS_SUPPORTED
6150/*
6151 * Sends a concentrator image into the FEP5 board.
6152 */
6153static void dgap_do_conc_load(struct board_t *brd, u8 *uaddr, int len)
6154{
6155 char __iomem *vaddr;
6156 u16 offset;
6157 struct downld_t *to_dp;
6158
6159 if (!brd || (brd->magic != DGAP_BOARD_MAGIC) || !brd->re_map_membase)
6160 return;
6161
6162 vaddr = brd->re_map_membase;
6163
Ioana Ciornei18122552015-10-21 23:17:54 +03006164 offset = readw((u16 *)(vaddr + DOWNREQ));
6165 to_dp = (struct downld_t *)(vaddr + (int)offset);
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09006166 memcpy_toio(to_dp, uaddr, len);
6167
6168 /* Tell card we have data for it */
6169 writew(0, vaddr + (DOWNREQ));
6170
6171 brd->conc_dl_status = NO_PENDING_CONCENTRATOR_REQUESTS;
6172}
6173#endif
6174
6175#define EXPANSION_ROM_SIZE (64 * 1024)
6176#define FEP5_ROM_MAGIC (0xFEFFFFFF)
6177
6178static void dgap_get_vpd(struct board_t *brd)
6179{
6180 u32 magic;
6181 u32 base_offset;
6182 u16 rom_offset;
6183 u16 vpd_offset;
6184 u16 image_length;
6185 u16 i;
6186 u8 byte1;
6187 u8 byte2;
6188
6189 /*
6190 * Poke the magic number at the PCI Rom Address location.
6191 * If VPD is supported, the value read from that address
6192 * will be non-zero.
6193 */
6194 magic = FEP5_ROM_MAGIC;
6195 pci_write_config_dword(brd->pdev, PCI_ROM_ADDRESS, magic);
6196 pci_read_config_dword(brd->pdev, PCI_ROM_ADDRESS, &magic);
6197
6198 /* VPD not supported, bail */
6199 if (!magic)
6200 return;
6201
6202 /*
6203 * To get to the OTPROM memory, we have to send the boards base
6204 * address or'ed with 1 into the PCI Rom Address location.
6205 */
6206 magic = brd->membase | 0x01;
6207 pci_write_config_dword(brd->pdev, PCI_ROM_ADDRESS, magic);
6208 pci_read_config_dword(brd->pdev, PCI_ROM_ADDRESS, &magic);
6209
6210 byte1 = readb(brd->re_map_membase);
6211 byte2 = readb(brd->re_map_membase + 1);
6212
6213 /*
6214 * If the board correctly swapped to the OTPROM memory,
6215 * the first 2 bytes (header) should be 0x55, 0xAA
6216 */
6217 if (byte1 == 0x55 && byte2 == 0xAA) {
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09006218 base_offset = 0;
6219
6220 /*
6221 * We have to run through all the OTPROM memory looking
6222 * for the VPD offset.
6223 */
6224 while (base_offset <= EXPANSION_ROM_SIZE) {
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09006225 /*
6226 * Lots of magic numbers here.
6227 *
6228 * The VPD offset is located inside the ROM Data
6229 * Structure.
6230 *
6231 * We also have to remember the length of each
6232 * ROM Data Structure, so we can "hop" to the next
6233 * entry if the VPD isn't in the current
6234 * ROM Data Structure.
6235 */
6236 rom_offset = readw(brd->re_map_membase +
6237 base_offset + 0x18);
6238 image_length = readw(brd->re_map_membase +
6239 rom_offset + 0x10) * 512;
6240 vpd_offset = readw(brd->re_map_membase +
6241 rom_offset + 0x08);
6242
6243 /* Found the VPD entry */
6244 if (vpd_offset)
6245 break;
6246
6247 /* We didn't find a VPD entry, go to next ROM entry. */
6248 base_offset += image_length;
6249
6250 byte1 = readb(brd->re_map_membase + base_offset);
6251 byte2 = readb(brd->re_map_membase + base_offset + 1);
6252
6253 /*
6254 * If the new ROM offset doesn't have 0x55, 0xAA
6255 * as its header, we have run out of ROM.
6256 */
6257 if (byte1 != 0x55 || byte2 != 0xAA)
6258 break;
6259 }
6260
6261 /*
6262 * If we have a VPD offset, then mark the board
6263 * as having a valid VPD, and copy VPDSIZE (512) bytes of
6264 * that VPD to the buffer we have in our board structure.
6265 */
6266 if (vpd_offset) {
6267 brd->bd_flags |= BD_HAS_VPD;
6268 for (i = 0; i < VPDSIZE; i++) {
6269 brd->vpd[i] = readb(brd->re_map_membase +
6270 vpd_offset + i);
6271 }
6272 }
6273 }
6274
6275 /*
6276 * We MUST poke the magic number at the PCI Rom Address location again.
6277 * This makes the card report the regular board memory back to us,
6278 * rather than the OTPROM memory.
6279 */
6280 magic = FEP5_ROM_MAGIC;
6281 pci_write_config_dword(brd->pdev, PCI_ROM_ADDRESS, magic);
6282}
6283
6284
6285static ssize_t dgap_driver_version_show(struct device_driver *ddp, char *buf)
6286{
6287 return snprintf(buf, PAGE_SIZE, "%s\n", DG_PART);
6288}
6289static DRIVER_ATTR(version, S_IRUSR, dgap_driver_version_show, NULL);
6290
6291
6292static ssize_t dgap_driver_boards_show(struct device_driver *ddp, char *buf)
6293{
6294 return snprintf(buf, PAGE_SIZE, "%d\n", dgap_numboards);
6295}
6296static DRIVER_ATTR(boards, S_IRUSR, dgap_driver_boards_show, NULL);
6297
6298
6299static ssize_t dgap_driver_maxboards_show(struct device_driver *ddp, char *buf)
6300{
6301 return snprintf(buf, PAGE_SIZE, "%d\n", MAXBOARDS);
6302}
6303static DRIVER_ATTR(maxboards, S_IRUSR, dgap_driver_maxboards_show, NULL);
6304
6305
6306static ssize_t dgap_driver_pollcounter_show(struct device_driver *ddp,
6307 char *buf)
6308{
6309 return snprintf(buf, PAGE_SIZE, "%ld\n", dgap_poll_counter);
6310}
6311static DRIVER_ATTR(pollcounter, S_IRUSR, dgap_driver_pollcounter_show, NULL);
6312
6313static ssize_t dgap_driver_pollrate_show(struct device_driver *ddp, char *buf)
6314{
6315 return snprintf(buf, PAGE_SIZE, "%dms\n", dgap_poll_tick);
6316}
6317
6318static ssize_t dgap_driver_pollrate_store(struct device_driver *ddp,
6319 const char *buf, size_t count)
6320{
6321 if (sscanf(buf, "%d\n", &dgap_poll_tick) != 1)
6322 return -EINVAL;
6323 return count;
6324}
6325static DRIVER_ATTR(pollrate, (S_IRUSR | S_IWUSR), dgap_driver_pollrate_show,
6326 dgap_driver_pollrate_store);
6327
6328
6329static int dgap_create_driver_sysfiles(struct pci_driver *dgap_driver)
6330{
6331 int rc = 0;
6332 struct device_driver *driverfs = &dgap_driver->driver;
6333
6334 rc |= driver_create_file(driverfs, &driver_attr_version);
6335 rc |= driver_create_file(driverfs, &driver_attr_boards);
6336 rc |= driver_create_file(driverfs, &driver_attr_maxboards);
6337 rc |= driver_create_file(driverfs, &driver_attr_pollrate);
6338 rc |= driver_create_file(driverfs, &driver_attr_pollcounter);
6339
6340 return rc;
6341}
6342
6343static void dgap_remove_driver_sysfiles(struct pci_driver *dgap_driver)
6344{
6345 struct device_driver *driverfs = &dgap_driver->driver;
6346
6347 driver_remove_file(driverfs, &driver_attr_version);
6348 driver_remove_file(driverfs, &driver_attr_boards);
6349 driver_remove_file(driverfs, &driver_attr_maxboards);
6350 driver_remove_file(driverfs, &driver_attr_pollrate);
6351 driver_remove_file(driverfs, &driver_attr_pollcounter);
6352}
6353
Mark Hounschell7568f7d2014-02-19 13:12:01 -05006354static struct attribute_group dgap_tty_attribute_group = {
6355 .name = NULL,
6356 .attrs = dgap_sysfs_tty_entries,
6357};
6358
Mark Hounschellfe0ef8e2014-02-19 13:12:13 -05006359static void dgap_create_tty_sysfs(struct un_t *un, struct device *c)
Mark Hounschell7568f7d2014-02-19 13:12:01 -05006360{
6361 int ret;
6362
6363 ret = sysfs_create_group(&c->kobj, &dgap_tty_attribute_group);
Mark Hounschell54794d12014-03-04 16:03:07 -05006364 if (ret)
Mark Hounschell7568f7d2014-02-19 13:12:01 -05006365 return;
Mark Hounschell7568f7d2014-02-19 13:12:01 -05006366
6367 dev_set_drvdata(c, un);
Mark Hounschell7568f7d2014-02-19 13:12:01 -05006368}
6369
Mark Hounschellfe0ef8e2014-02-19 13:12:13 -05006370static void dgap_remove_tty_sysfs(struct device *c)
Mark Hounschell7568f7d2014-02-19 13:12:01 -05006371{
6372 sysfs_remove_group(&c->kobj, &dgap_tty_attribute_group);
6373}
Mark Hounschell69edaa22014-02-19 13:12:02 -05006374
6375/*
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09006376 * Create pr and tty device entries
Mark Hounschell69edaa22014-02-19 13:12:02 -05006377 */
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09006378static int dgap_tty_register_ports(struct board_t *brd)
Mark Hounschell69edaa22014-02-19 13:12:02 -05006379{
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09006380 struct channel_t *ch;
6381 int i;
6382 int ret;
6383
6384 brd->serial_ports = kcalloc(brd->nasync, sizeof(*brd->serial_ports),
6385 GFP_KERNEL);
6386 if (!brd->serial_ports)
6387 return -ENOMEM;
6388
6389 brd->printer_ports = kcalloc(brd->nasync, sizeof(*brd->printer_ports),
6390 GFP_KERNEL);
6391 if (!brd->printer_ports) {
6392 ret = -ENOMEM;
6393 goto free_serial_ports;
6394 }
6395
6396 for (i = 0; i < brd->nasync; i++) {
6397 tty_port_init(&brd->serial_ports[i]);
6398 tty_port_init(&brd->printer_ports[i]);
6399 }
6400
6401 ch = brd->channels[0];
6402 for (i = 0; i < brd->nasync; i++, ch = brd->channels[i]) {
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09006403 struct device *classp;
6404
6405 classp = tty_port_register_device(&brd->serial_ports[i],
6406 brd->serial_driver,
6407 i, NULL);
6408
6409 if (IS_ERR(classp)) {
6410 ret = PTR_ERR(classp);
6411 goto unregister_ttys;
6412 }
6413
6414 dgap_create_tty_sysfs(&ch->ch_tun, classp);
6415 ch->ch_tun.un_sysfs = classp;
6416
6417 classp = tty_port_register_device(&brd->printer_ports[i],
6418 brd->print_driver,
6419 i, NULL);
6420
6421 if (IS_ERR(classp)) {
6422 ret = PTR_ERR(classp);
6423 goto unregister_ttys;
6424 }
6425
6426 dgap_create_tty_sysfs(&ch->ch_pun, classp);
6427 ch->ch_pun.un_sysfs = classp;
6428 }
6429 dgap_create_ports_sysfiles(brd);
6430
6431 return 0;
6432
6433unregister_ttys:
6434 while (i >= 0) {
6435 ch = brd->channels[i];
6436 if (ch->ch_tun.un_sysfs) {
6437 dgap_remove_tty_sysfs(ch->ch_tun.un_sysfs);
6438 tty_unregister_device(brd->serial_driver, i);
6439 }
6440
6441 if (ch->ch_pun.un_sysfs) {
6442 dgap_remove_tty_sysfs(ch->ch_pun.un_sysfs);
6443 tty_unregister_device(brd->print_driver, i);
6444 }
6445 i--;
6446 }
6447
6448 for (i = 0; i < brd->nasync; i++) {
6449 tty_port_destroy(&brd->serial_ports[i]);
6450 tty_port_destroy(&brd->printer_ports[i]);
6451 }
6452
6453 kfree(brd->printer_ports);
6454 brd->printer_ports = NULL;
6455
6456free_serial_ports:
6457 kfree(brd->serial_ports);
6458 brd->serial_ports = NULL;
6459
6460 return ret;
6461}
6462
6463/*
6464 * dgap_cleanup_tty()
6465 *
6466 * Uninitialize the TTY portion of this driver. Free all memory and
6467 * resources.
6468 */
6469static void dgap_cleanup_tty(struct board_t *brd)
6470{
6471 struct device *dev;
6472 unsigned int i;
6473
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09006474 for (i = 0; i < brd->nasync; i++) {
6475 tty_port_destroy(&brd->serial_ports[i]);
6476 dev = brd->channels[i]->ch_tun.un_sysfs;
6477 dgap_remove_tty_sysfs(dev);
6478 tty_unregister_device(brd->serial_driver, i);
6479 }
6480 tty_unregister_driver(brd->serial_driver);
6481 put_tty_driver(brd->serial_driver);
6482 kfree(brd->serial_ports);
6483
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09006484 for (i = 0; i < brd->nasync; i++) {
6485 tty_port_destroy(&brd->printer_ports[i]);
6486 dev = brd->channels[i]->ch_pun.un_sysfs;
6487 dgap_remove_tty_sysfs(dev);
6488 tty_unregister_device(brd->print_driver, i);
6489 }
6490 tty_unregister_driver(brd->print_driver);
6491 put_tty_driver(brd->print_driver);
6492 kfree(brd->printer_ports);
6493}
6494
6495static int dgap_request_irq(struct board_t *brd)
6496{
Mark Hounschell174efc12014-05-23 12:54:04 -04006497 int rc;
Mark Hounschell69edaa22014-02-19 13:12:02 -05006498
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09006499 if (!brd || brd->magic != DGAP_BOARD_MAGIC)
6500 return -ENODEV;
Mark Hounschell69edaa22014-02-19 13:12:02 -05006501
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09006502 /*
6503 * Set up our interrupt handler if we are set to do interrupts.
6504 */
6505 if (dgap_config_get_useintr(brd) && brd->irq) {
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09006506 rc = request_irq(brd->irq, dgap_intr, IRQF_SHARED, "DGAP", brd);
Mark Hounschell69edaa22014-02-19 13:12:02 -05006507
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09006508 if (!rc)
6509 brd->intr_used = 1;
Mark Hounschell69edaa22014-02-19 13:12:02 -05006510 }
Mark Hounschellcf42c342014-02-28 12:42:09 -05006511 return 0;
Mark Hounschell69edaa22014-02-19 13:12:02 -05006512}
6513
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09006514static void dgap_free_irq(struct board_t *brd)
Mark Hounschell69edaa22014-02-19 13:12:02 -05006515{
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09006516 if (brd->intr_used && brd->irq)
6517 free_irq(brd->irq, brd);
Mark Hounschell69edaa22014-02-19 13:12:02 -05006518}
6519
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09006520static int dgap_firmware_load(struct pci_dev *pdev, int card_type,
6521 struct board_t *brd)
Mark Hounschell69edaa22014-02-19 13:12:02 -05006522{
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09006523 const struct firmware *fw;
6524 char *tmp_ptr;
6525 int ret;
6526 char *dgap_config_buf;
Mark Hounschell69edaa22014-02-19 13:12:02 -05006527
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09006528 dgap_get_vpd(brd);
6529 dgap_do_reset_board(brd);
Mark Hounschell69edaa22014-02-19 13:12:02 -05006530
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09006531 if (fw_info[card_type].conf_name) {
6532 ret = request_firmware(&fw, fw_info[card_type].conf_name,
Ioana Ciornei629c7342015-10-21 23:17:53 +03006533 &pdev->dev);
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09006534 if (ret) {
6535 dev_err(&pdev->dev, "config file %s not found\n",
6536 fw_info[card_type].conf_name);
6537 return ret;
Mark Hounschell69edaa22014-02-19 13:12:02 -05006538 }
Mark Hounschell69edaa22014-02-19 13:12:02 -05006539
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09006540 dgap_config_buf = kzalloc(fw->size + 1, GFP_KERNEL);
6541 if (!dgap_config_buf) {
6542 release_firmware(fw);
6543 return -ENOMEM;
6544 }
Mark Hounschell69edaa22014-02-19 13:12:02 -05006545
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09006546 memcpy(dgap_config_buf, fw->data, fw->size);
6547 release_firmware(fw);
Mark Hounschell69edaa22014-02-19 13:12:02 -05006548
Mark Hounschell0be048c2014-05-28 16:17:55 -04006549 /*
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09006550 * preserve dgap_config_buf
6551 * as dgap_parsefile would
6552 * otherwise alter it.
Mark Hounschell0be048c2014-05-28 16:17:55 -04006553 */
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09006554 tmp_ptr = dgap_config_buf;
Mark Hounschell69edaa22014-02-19 13:12:02 -05006555
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09006556 if (dgap_parsefile(&tmp_ptr) != 0) {
6557 kfree(dgap_config_buf);
6558 return -EINVAL;
Mark Hounschell69edaa22014-02-19 13:12:02 -05006559 }
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09006560 kfree(dgap_config_buf);
Mark Hounschell69edaa22014-02-19 13:12:02 -05006561 }
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09006562
6563 /*
6564 * Match this board to a config the user created for us.
6565 */
6566 brd->bd_config =
6567 dgap_find_config(brd->type, brd->pci_bus, brd->pci_slot);
6568
6569 /*
6570 * Because the 4 port Xr products share the same PCI ID
6571 * as the 8 port Xr products, if we receive a NULL config
6572 * back, and this is a PAPORT8 board, retry with a
6573 * PAPORT4 attempt as well.
6574 */
6575 if (brd->type == PAPORT8 && !brd->bd_config)
6576 brd->bd_config =
6577 dgap_find_config(PAPORT4, brd->pci_bus, brd->pci_slot);
6578
6579 if (!brd->bd_config) {
6580 dev_err(&pdev->dev, "No valid configuration found\n");
6581 return -EINVAL;
6582 }
6583
6584 if (fw_info[card_type].bios_name) {
6585 ret = request_firmware(&fw, fw_info[card_type].bios_name,
Ioana Ciornei629c7342015-10-21 23:17:53 +03006586 &pdev->dev);
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09006587 if (ret) {
6588 dev_err(&pdev->dev, "bios file %s not found\n",
6589 fw_info[card_type].bios_name);
6590 return ret;
6591 }
6592 dgap_do_bios_load(brd, fw->data, fw->size);
6593 release_firmware(fw);
6594
6595 /* Wait for BIOS to test board... */
6596 ret = dgap_test_bios(brd);
6597 if (ret)
6598 return ret;
6599 }
6600
6601 if (fw_info[card_type].fep_name) {
6602 ret = request_firmware(&fw, fw_info[card_type].fep_name,
Ioana Ciornei629c7342015-10-21 23:17:53 +03006603 &pdev->dev);
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09006604 if (ret) {
6605 dev_err(&pdev->dev, "dgap: fep file %s not found\n",
6606 fw_info[card_type].fep_name);
6607 return ret;
6608 }
6609 dgap_do_fep_load(brd, fw->data, fw->size);
6610 release_firmware(fw);
6611
6612 /* Wait for FEP to load on board... */
6613 ret = dgap_test_fep(brd);
6614 if (ret)
6615 return ret;
6616 }
6617
6618#ifdef DIGI_CONCENTRATORS_SUPPORTED
6619 /*
6620 * If this is a CX or EPCX, we need to see if the firmware
6621 * is requesting a concentrator image from us.
6622 */
6623 if ((bd->type == PCX) || (bd->type == PEPC)) {
Ioana Ciornei18122552015-10-21 23:17:54 +03006624 chk_addr = (u16 *)(vaddr + DOWNREQ);
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09006625 /* Nonzero if FEP is requesting concentrator image. */
6626 check = readw(chk_addr);
6627 vaddr = brd->re_map_membase;
6628 }
6629
6630 if (fw_info[card_type].con_name && check && vaddr) {
6631 ret = request_firmware(&fw, fw_info[card_type].con_name,
Ioana Ciornei629c7342015-10-21 23:17:53 +03006632 &pdev->dev);
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09006633 if (ret) {
6634 dev_err(&pdev->dev, "conc file %s not found\n",
6635 fw_info[card_type].con_name);
6636 return ret;
6637 }
6638 /* Put concentrator firmware loading code here */
Ioana Ciornei18122552015-10-21 23:17:54 +03006639 offset = readw((u16 *)(vaddr + DOWNREQ));
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09006640 memcpy_toio(offset, fw->data, fw->size);
6641
6642 dgap_do_conc_load(brd, (char *)fw->data, fw->size)
6643 release_firmware(fw);
6644 }
6645#endif
6646
6647 return 0;
Mark Hounschell69edaa22014-02-19 13:12:02 -05006648}
6649
6650/*
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09006651 * dgap_tty_init()
6652 *
6653 * Init the tty subsystem. Called once per board after board has been
6654 * downloaded and init'ed.
Mark Hounschell69edaa22014-02-19 13:12:02 -05006655 */
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09006656static int dgap_tty_init(struct board_t *brd)
Mark Hounschell69edaa22014-02-19 13:12:02 -05006657{
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09006658 int i;
6659 int tlw;
6660 uint true_count;
6661 u8 __iomem *vaddr;
6662 u8 modem;
6663 struct channel_t *ch;
6664 struct bs_t __iomem *bs;
6665 struct cm_t __iomem *cm;
6666 int ret;
Mark Hounschell69edaa22014-02-19 13:12:02 -05006667
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09006668 /*
6669 * Initialize board structure elements.
6670 */
Mark Hounschell69edaa22014-02-19 13:12:02 -05006671
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09006672 vaddr = brd->re_map_membase;
6673 true_count = readw((vaddr + NCHAN));
Mark Hounschell69edaa22014-02-19 13:12:02 -05006674
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09006675 brd->nasync = dgap_config_get_num_prts(brd);
6676
6677 if (!brd->nasync)
6678 brd->nasync = brd->maxports;
6679
6680 if (brd->nasync > brd->maxports)
6681 brd->nasync = brd->maxports;
6682
6683 if (true_count != brd->nasync) {
6684 dev_warn(&brd->pdev->dev,
6685 "%s configured for %d ports, has %d ports.\n",
6686 brd->name, brd->nasync, true_count);
6687
6688 if ((brd->type == PPCM) &&
6689 (true_count == 64 || true_count == 0)) {
6690 dev_warn(&brd->pdev->dev,
6691 "Please make SURE the EBI cable running from the card\n");
6692 dev_warn(&brd->pdev->dev,
6693 "to each EM module is plugged into EBI IN!\n");
Mark Hounschell69edaa22014-02-19 13:12:02 -05006694 }
Mark Hounschell69edaa22014-02-19 13:12:02 -05006695
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09006696 brd->nasync = true_count;
Mark Hounschell69edaa22014-02-19 13:12:02 -05006697
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09006698 /* If no ports, don't bother going any further */
6699 if (!brd->nasync) {
6700 brd->state = BOARD_FAILED;
6701 brd->dpastatus = BD_NOFEP;
6702 return -EIO;
Mark Hounschell69edaa22014-02-19 13:12:02 -05006703 }
6704 }
6705
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09006706 /*
6707 * Allocate channel memory that might not have been allocated
6708 * when the driver was first loaded.
6709 */
6710 for (i = 0; i < brd->nasync; i++) {
6711 brd->channels[i] =
6712 kzalloc(sizeof(struct channel_t), GFP_KERNEL);
6713 if (!brd->channels[i]) {
6714 ret = -ENOMEM;
6715 goto free_chan;
6716 }
6717 }
6718
6719 ch = brd->channels[0];
6720 vaddr = brd->re_map_membase;
6721
Ioana Ciornei18122552015-10-21 23:17:54 +03006722 bs = (struct bs_t __iomem *)((ulong)vaddr + CHANBUF);
6723 cm = (struct cm_t __iomem *)((ulong)vaddr + CMDBUF);
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09006724
6725 brd->bd_bs = bs;
6726
6727 /* Set up channel variables */
6728 for (i = 0; i < brd->nasync; i++, ch = brd->channels[i], bs++) {
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09006729 spin_lock_init(&ch->ch_lock);
6730
6731 /* Store all our magic numbers */
6732 ch->magic = DGAP_CHANNEL_MAGIC;
6733 ch->ch_tun.magic = DGAP_UNIT_MAGIC;
6734 ch->ch_tun.un_type = DGAP_SERIAL;
6735 ch->ch_tun.un_ch = ch;
6736 ch->ch_tun.un_dev = i;
6737
6738 ch->ch_pun.magic = DGAP_UNIT_MAGIC;
6739 ch->ch_pun.un_type = DGAP_PRINT;
6740 ch->ch_pun.un_ch = ch;
6741 ch->ch_pun.un_dev = i;
6742
6743 ch->ch_vaddr = vaddr;
6744 ch->ch_bs = bs;
6745 ch->ch_cm = cm;
6746 ch->ch_bd = brd;
6747 ch->ch_portnum = i;
6748 ch->ch_digi = dgap_digi_init;
6749
6750 /*
6751 * Set up digi dsr and dcd bits based on altpin flag.
6752 */
6753 if (dgap_config_get_altpin(brd)) {
6754 ch->ch_dsr = DM_CD;
6755 ch->ch_cd = DM_DSR;
6756 ch->ch_digi.digi_flags |= DIGI_ALTPIN;
6757 } else {
6758 ch->ch_cd = DM_CD;
6759 ch->ch_dsr = DM_DSR;
6760 }
6761
Ioana Ciornei44209c92015-10-21 23:17:55 +03006762 ch->ch_taddr = vaddr + (ioread16(&ch->ch_bs->tx_seg) << 4);
6763 ch->ch_raddr = vaddr + (ioread16(&ch->ch_bs->rx_seg) << 4);
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09006764 ch->ch_tx_win = 0;
6765 ch->ch_rx_win = 0;
Ioana Ciornei44209c92015-10-21 23:17:55 +03006766 ch->ch_tsize = readw(&ch->ch_bs->tx_max) + 1;
6767 ch->ch_rsize = readw(&ch->ch_bs->rx_max) + 1;
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09006768 ch->ch_tstart = 0;
6769 ch->ch_rstart = 0;
6770
6771 /*
6772 * Set queue water marks, interrupt mask,
6773 * and general tty parameters.
6774 */
6775 tlw = ch->ch_tsize >= 2000 ? ((ch->ch_tsize * 5) / 8) :
6776 ch->ch_tsize / 2;
6777 ch->ch_tlw = tlw;
6778
6779 dgap_cmdw(ch, STLOW, tlw, 0);
6780
6781 dgap_cmdw(ch, SRLOW, ch->ch_rsize / 2, 0);
6782
6783 dgap_cmdw(ch, SRHIGH, 7 * ch->ch_rsize / 8, 0);
6784
Ioana Ciornei44209c92015-10-21 23:17:55 +03006785 ch->ch_mistat = readb(&ch->ch_bs->m_stat);
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09006786
6787 init_waitqueue_head(&ch->ch_flags_wait);
6788 init_waitqueue_head(&ch->ch_tun.un_flags_wait);
6789 init_waitqueue_head(&ch->ch_pun.un_flags_wait);
6790
6791 /* Turn on all modem interrupts for now */
6792 modem = (DM_CD | DM_DSR | DM_CTS | DM_RI);
Ioana Ciornei44209c92015-10-21 23:17:55 +03006793 writeb(modem, &ch->ch_bs->m_int);
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09006794
6795 /*
6796 * Set edelay to 0 if interrupts are turned on,
6797 * otherwise set edelay to the usual 100.
6798 */
6799 if (brd->intr_used)
Ioana Ciornei44209c92015-10-21 23:17:55 +03006800 writew(0, &ch->ch_bs->edelay);
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09006801 else
Ioana Ciornei44209c92015-10-21 23:17:55 +03006802 writew(100, &ch->ch_bs->edelay);
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09006803
Ioana Ciornei44209c92015-10-21 23:17:55 +03006804 writeb(1, &ch->ch_bs->idata);
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09006805 }
6806
6807 return 0;
6808
6809free_chan:
6810 while (--i >= 0) {
6811 kfree(brd->channels[i]);
6812 brd->channels[i] = NULL;
6813 }
6814 return ret;
Mark Hounschell69edaa22014-02-19 13:12:02 -05006815}
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09006816
6817/*
6818 * dgap_tty_free()
6819 *
6820 * Free the channles which are allocated in dgap_tty_init().
6821 */
6822static void dgap_tty_free(struct board_t *brd)
6823{
6824 int i;
6825
6826 for (i = 0; i < brd->nasync; i++)
6827 kfree(brd->channels[i]);
6828}
6829
6830static int dgap_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
6831{
6832 int rc;
6833 struct board_t *brd;
6834
6835 if (dgap_numboards >= MAXBOARDS)
6836 return -EPERM;
6837
6838 rc = pci_enable_device(pdev);
6839 if (rc)
6840 return -EIO;
6841
6842 brd = dgap_found_board(pdev, ent->driver_data, dgap_numboards);
6843 if (IS_ERR(brd))
6844 return PTR_ERR(brd);
6845
6846 rc = dgap_firmware_load(pdev, ent->driver_data, brd);
6847 if (rc)
6848 goto cleanup_brd;
6849
6850 rc = dgap_alloc_flipbuf(brd);
6851 if (rc)
6852 goto cleanup_brd;
6853
6854 rc = dgap_tty_register(brd);
6855 if (rc)
6856 goto free_flipbuf;
6857
6858 rc = dgap_request_irq(brd);
6859 if (rc)
6860 goto unregister_tty;
6861
6862 /*
6863 * Do tty device initialization.
6864 */
6865 rc = dgap_tty_init(brd);
6866 if (rc < 0)
6867 goto free_irq;
6868
6869 rc = dgap_tty_register_ports(brd);
6870 if (rc)
6871 goto tty_free;
6872
6873 brd->state = BOARD_READY;
6874 brd->dpastatus = BD_RUNNING;
6875
6876 dgap_board[dgap_numboards++] = brd;
6877
6878 return 0;
6879
6880tty_free:
6881 dgap_tty_free(brd);
6882free_irq:
6883 dgap_free_irq(brd);
6884unregister_tty:
6885 dgap_tty_unregister(brd);
6886free_flipbuf:
6887 dgap_free_flipbuf(brd);
6888cleanup_brd:
6889 dgap_cleanup_nodes();
6890 dgap_unmap(brd);
6891 kfree(brd);
6892
6893 return rc;
6894}
6895
Sudip Mukherjeed1c9f3e2015-05-07 16:42:20 +05306896/*
6897 * dgap_cleanup_board()
6898 *
6899 * Free all the memory associated with a board
6900 */
6901static void dgap_cleanup_board(struct board_t *brd)
6902{
6903 unsigned int i;
6904
6905 if (!brd || brd->magic != DGAP_BOARD_MAGIC)
6906 return;
6907
6908 dgap_free_irq(brd);
6909
6910 tasklet_kill(&brd->helper_tasklet);
6911
6912 dgap_unmap(brd);
6913
6914 /* Free all allocated channels structs */
6915 for (i = 0; i < MAXPORTS ; i++)
6916 kfree(brd->channels[i]);
6917
6918 kfree(brd->flipbuf);
6919 kfree(brd->flipflagbuf);
6920
6921 dgap_board[brd->boardnum] = NULL;
6922
6923 kfree(brd);
6924}
6925
Sudip Mukherjee174b83c2015-07-16 18:28:19 +05306926static void dgap_stop(bool removesys, struct pci_driver *drv)
Sudip Mukherjeeeda03952015-07-16 18:28:18 +05306927{
6928 unsigned long lock_flags;
6929
6930 spin_lock_irqsave(&dgap_poll_lock, lock_flags);
6931 dgap_poll_stop = 1;
6932 spin_unlock_irqrestore(&dgap_poll_lock, lock_flags);
6933
6934 del_timer_sync(&dgap_poll_timer);
Sudip Mukherjee174b83c2015-07-16 18:28:19 +05306935 if (removesys)
6936 dgap_remove_driver_sysfiles(drv);
Sudip Mukherjeeeda03952015-07-16 18:28:18 +05306937
6938 device_destroy(dgap_class, MKDEV(DIGI_DGAP_MAJOR, 0));
6939 class_destroy(dgap_class);
6940 unregister_chrdev(DIGI_DGAP_MAJOR, "dgap");
6941}
6942
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09006943static void dgap_remove_one(struct pci_dev *dev)
6944{
Sudip Mukherjee1397e2f2015-05-07 16:42:21 +05306945 unsigned int i;
Sudip Mukherjee1397e2f2015-05-07 16:42:21 +05306946 struct pci_driver *drv = to_pci_driver(dev->dev.driver);
6947
Sudip Mukherjeeb8d1f262015-07-16 18:28:20 +05306948 dgap_stop(true, drv);
Sudip Mukherjee1397e2f2015-05-07 16:42:21 +05306949 for (i = 0; i < dgap_numboards; ++i) {
6950 dgap_remove_ports_sysfiles(dgap_board[i]);
6951 dgap_cleanup_tty(dgap_board[i]);
6952 dgap_cleanup_board(dgap_board[i]);
6953 }
6954
6955 dgap_cleanup_nodes();
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09006956}
6957
6958static struct pci_driver dgap_driver = {
6959 .name = "dgap",
6960 .probe = dgap_init_one,
6961 .id_table = dgap_pci_tbl,
6962 .remove = dgap_remove_one,
6963};
6964
6965/*
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09006966 * Start of driver.
6967 */
6968static int dgap_start(void)
6969{
6970 int rc;
6971 unsigned long flags;
6972 struct device *device;
6973
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09006974 dgap_numboards = 0;
6975
6976 pr_info("For the tools package please visit http://www.digi.com\n");
6977
6978 /*
6979 * Register our base character device into the kernel.
6980 */
6981
6982 /*
6983 * Register management/dpa devices
6984 */
6985 rc = register_chrdev(DIGI_DGAP_MAJOR, "dgap", &dgap_board_fops);
6986 if (rc < 0)
6987 return rc;
6988
6989 dgap_class = class_create(THIS_MODULE, "dgap_mgmt");
6990 if (IS_ERR(dgap_class)) {
6991 rc = PTR_ERR(dgap_class);
6992 goto failed_class;
6993 }
6994
6995 device = device_create(dgap_class, NULL,
Ioana Ciornei629c7342015-10-21 23:17:53 +03006996 MKDEV(DIGI_DGAP_MAJOR, 0),
6997 NULL, "dgap_mgmt");
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09006998 if (IS_ERR(device)) {
6999 rc = PTR_ERR(device);
7000 goto failed_device;
7001 }
7002
7003 /* Start the poller */
7004 spin_lock_irqsave(&dgap_poll_lock, flags);
Somya Anand2049f1e2015-03-11 17:02:14 +05307005 setup_timer(&dgap_poll_timer, dgap_poll_handler, 0);
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09007006 dgap_poll_timer.data = 0;
7007 dgap_poll_time = jiffies + dgap_jiffies_from_ms(dgap_poll_tick);
7008 dgap_poll_timer.expires = dgap_poll_time;
7009 spin_unlock_irqrestore(&dgap_poll_lock, flags);
7010
7011 add_timer(&dgap_poll_timer);
7012
7013 return rc;
7014
7015failed_device:
7016 class_destroy(dgap_class);
7017failed_class:
7018 unregister_chrdev(DIGI_DGAP_MAJOR, "dgap");
7019 return rc;
7020}
7021
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09007022/************************************************************************
7023 *
7024 * Driver load/unload functions
7025 *
7026 ************************************************************************/
7027
7028/*
7029 * init_module()
7030 *
7031 * Module load. This is where it all starts.
7032 */
7033static int dgap_init_module(void)
7034{
7035 int rc;
7036
7037 pr_info("%s, Digi International Part Number %s\n", DG_NAME, DG_PART);
7038
7039 rc = dgap_start();
7040 if (rc)
7041 return rc;
7042
7043 rc = pci_register_driver(&dgap_driver);
Sudip Mukherjeeac4e5042015-07-16 18:28:17 +05307044 if (rc) {
Sudip Mukherjee174b83c2015-07-16 18:28:19 +05307045 dgap_stop(false, NULL);
Sudip Mukherjeeac4e5042015-07-16 18:28:17 +05307046 return rc;
7047 }
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09007048
7049 rc = dgap_create_driver_sysfiles(&dgap_driver);
7050 if (rc)
7051 goto err_unregister;
7052
7053 dgap_driver_state = DRIVER_READY;
7054
7055 return 0;
7056
7057err_unregister:
7058 pci_unregister_driver(&dgap_driver);
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09007059 return rc;
7060}
7061
7062/*
7063 * dgap_cleanup_module()
7064 *
7065 * Module unload. This is where it all ends.
7066 */
7067static void dgap_cleanup_module(void)
7068{
Daeseok Youn0b2cf5c2014-10-31 10:20:37 +09007069 if (dgap_numboards)
7070 pci_unregister_driver(&dgap_driver);
7071}
7072
7073module_init(dgap_init_module);
7074module_exit(dgap_cleanup_module);
7075
7076MODULE_LICENSE("GPL");
7077MODULE_AUTHOR("Digi International, http://www.digi.com");
7078MODULE_DESCRIPTION("Driver for the Digi International EPCA PCI based product line");
7079MODULE_SUPPORTED_DEVICE("dgap");