blob: 456e4ede049f2a0dad2bd0452199461272f1c353 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 Copyright (C) 1996 Digi International.
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07003
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 For technical support please email digiLinux@dgii.com or
5 call Digi tech support at (612) 912-3456
6
Alan Coxf2cf8e22005-09-06 15:16:44 -07007 ** This driver is no longer supported by Digi **
8
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07009 Much of this design and code came from epca.c which was
10 copyright (C) 1994, 1995 Troy De Jongh, and subsquently
11 modified by David Nugent, Christoph Lameter, Mike McLagan.
Linus Torvalds1da177e2005-04-16 15:20:36 -070012
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -070013 This program is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 2 of the License, or
16 (at your option) any later version.
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -070018 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -070023 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26*/
Linus Torvalds1da177e2005-04-16 15:20:36 -070027/* See README.epca for change history --DAT*/
28
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/module.h>
30#include <linux/kernel.h>
31#include <linux/types.h>
32#include <linux/init.h>
33#include <linux/serial.h>
34#include <linux/delay.h>
35#include <linux/ctype.h>
36#include <linux/tty.h>
37#include <linux/tty_flip.h>
38#include <linux/slab.h>
39#include <linux/ioport.h>
40#include <linux/interrupt.h>
Alan Cox191260a2008-04-30 00:54:16 -070041#include <linux/uaccess.h>
42#include <linux/io.h>
Alan Coxf2cf8e22005-09-06 15:16:44 -070043#include <linux/spinlock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <linux/pci.h>
45#include "digiPCI.h"
Alan Coxf2cf8e22005-09-06 15:16:44 -070046
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48#include "digi1.h"
49#include "digiFep1.h"
50#include "epca.h"
51#include "epcaconfig.h"
52
Alan Coxf2cf8e22005-09-06 15:16:44 -070053#define VERSION "1.3.0.1-LK2.6"
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55/* This major needs to be submitted to Linux to join the majors list */
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -070056#define DIGIINFOMAJOR 35 /* For Digi specific ioctl */
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
58
59#define MAXCARDS 7
60#define epcaassert(x, msg) if (!(x)) epca_error(__LINE__, msg)
61
62#define PFX "epca: "
63
Linus Torvalds1da177e2005-04-16 15:20:36 -070064static int nbdevs, num_cards, liloconfig;
65static int digi_poller_inhibited = 1 ;
66
67static int setup_error_code;
68static int invalid_lilo_config;
69
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -070070/*
71 * The ISA boards do window flipping into the same spaces so its only sane with
72 * a single lock. It's still pretty efficient.
73 */
Ingo Molnar34af9462006-06-27 02:53:55 -070074static DEFINE_SPINLOCK(epca_lock);
Alan Coxf2cf8e22005-09-06 15:16:44 -070075
Alan Cox191260a2008-04-30 00:54:16 -070076/* MAXBOARDS is typically 12, but ISA and EISA cards are restricted
77 to 7 below. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070078static struct board_info boards[MAXBOARDS];
79
Linus Torvalds1da177e2005-04-16 15:20:36 -070080static struct tty_driver *pc_driver;
81static struct tty_driver *pc_info;
82
83/* ------------------ Begin Digi specific structures -------------------- */
84
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -070085/*
86 * digi_channels represents an array of structures that keep track of each
87 * channel of the Digi product. Information such as transmit and receive
88 * pointers, termio data, and signal definitions (DTR, CTS, etc ...) are stored
89 * here. This structure is NOT used to overlay the cards physical channel
90 * structure.
91 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070092static struct channel digi_channels[MAX_ALLOC];
93
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -070094/*
95 * card_ptr is an array used to hold the address of the first channel structure
96 * of each card. This array will hold the addresses of various channels located
97 * in digi_channels.
98 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070099static struct channel *card_ptr[MAXCARDS];
100
101static struct timer_list epca_timer;
102
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -0700103/*
104 * Begin generic memory functions. These functions will be alias (point at)
105 * more specific functions dependent on the board being configured.
106 */
Alan Coxf2cf8e22005-09-06 15:16:44 -0700107static void memwinon(struct board_info *b, unsigned int win);
108static void memwinoff(struct board_info *b, unsigned int win);
109static void globalwinon(struct channel *ch);
110static void rxwinon(struct channel *ch);
111static void txwinon(struct channel *ch);
112static void memoff(struct channel *ch);
113static void assertgwinon(struct channel *ch);
114static void assertmemoff(struct channel *ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
116/* ---- Begin more 'specific' memory functions for cx_like products --- */
117
Alan Coxf2cf8e22005-09-06 15:16:44 -0700118static void pcxem_memwinon(struct board_info *b, unsigned int win);
119static void pcxem_memwinoff(struct board_info *b, unsigned int win);
120static void pcxem_globalwinon(struct channel *ch);
121static void pcxem_rxwinon(struct channel *ch);
122static void pcxem_txwinon(struct channel *ch);
123static void pcxem_memoff(struct channel *ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
125/* ------ Begin more 'specific' memory functions for the pcxe ------- */
126
Alan Coxf2cf8e22005-09-06 15:16:44 -0700127static void pcxe_memwinon(struct board_info *b, unsigned int win);
128static void pcxe_memwinoff(struct board_info *b, unsigned int win);
129static void pcxe_globalwinon(struct channel *ch);
130static void pcxe_rxwinon(struct channel *ch);
131static void pcxe_txwinon(struct channel *ch);
132static void pcxe_memoff(struct channel *ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
134/* ---- Begin more 'specific' memory functions for the pc64xe and pcxi ---- */
135/* Note : pc64xe and pcxi share the same windowing routines */
136
Alan Coxf2cf8e22005-09-06 15:16:44 -0700137static void pcxi_memwinon(struct board_info *b, unsigned int win);
138static void pcxi_memwinoff(struct board_info *b, unsigned int win);
139static void pcxi_globalwinon(struct channel *ch);
140static void pcxi_rxwinon(struct channel *ch);
141static void pcxi_txwinon(struct channel *ch);
142static void pcxi_memoff(struct channel *ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
144/* - Begin 'specific' do nothing memory functions needed for some cards - */
145
Alan Coxf2cf8e22005-09-06 15:16:44 -0700146static void dummy_memwinon(struct board_info *b, unsigned int win);
147static void dummy_memwinoff(struct board_info *b, unsigned int win);
148static void dummy_globalwinon(struct channel *ch);
149static void dummy_rxwinon(struct channel *ch);
150static void dummy_txwinon(struct channel *ch);
151static void dummy_memoff(struct channel *ch);
152static void dummy_assertgwinon(struct channel *ch);
153static void dummy_assertmemoff(struct channel *ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Alan Coxf2cf8e22005-09-06 15:16:44 -0700155static struct channel *verifyChannel(struct tty_struct *);
156static void pc_sched_event(struct channel *, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157static void epca_error(int, char *);
158static void pc_close(struct tty_struct *, struct file *);
159static void shutdown(struct channel *);
160static void pc_hangup(struct tty_struct *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161static int pc_write_room(struct tty_struct *);
162static int pc_chars_in_buffer(struct tty_struct *);
163static void pc_flush_buffer(struct tty_struct *);
164static void pc_flush_chars(struct tty_struct *);
165static int block_til_ready(struct tty_struct *, struct file *,
Alan Cox191260a2008-04-30 00:54:16 -0700166 struct channel *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167static int pc_open(struct tty_struct *, struct file *);
168static void post_fep_init(unsigned int crd);
169static void epcapoll(unsigned long);
170static void doevent(int);
171static void fepcmd(struct channel *, int, int, int, int, int);
172static unsigned termios2digi_h(struct channel *ch, unsigned);
173static unsigned termios2digi_i(struct channel *ch, unsigned);
174static unsigned termios2digi_c(struct channel *ch, unsigned);
175static void epcaparam(struct tty_struct *, struct channel *);
176static void receive_data(struct channel *);
177static int pc_ioctl(struct tty_struct *, struct file *,
Alan Cox191260a2008-04-30 00:54:16 -0700178 unsigned int, unsigned long);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179static int info_ioctl(struct tty_struct *, struct file *,
Alan Cox191260a2008-04-30 00:54:16 -0700180 unsigned int, unsigned long);
Alan Cox606d0992006-12-08 02:38:45 -0800181static void pc_set_termios(struct tty_struct *, struct ktermios *);
David Howellsc4028952006-11-22 14:57:56 +0000182static void do_softint(struct work_struct *work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183static void pc_stop(struct tty_struct *);
184static void pc_start(struct tty_struct *);
Alan Cox191260a2008-04-30 00:54:16 -0700185static void pc_throttle(struct tty_struct *tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186static void pc_unthrottle(struct tty_struct *tty);
Alan Coxdcbf1282008-07-22 11:18:12 +0100187static int pc_send_break(struct tty_struct *tty, int msec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188static void setup_empty_event(struct tty_struct *tty, struct channel *ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190static int pc_write(struct tty_struct *, const unsigned char *, int);
Alan Coxf2cf8e22005-09-06 15:16:44 -0700191static int pc_init(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192static int init_PCI(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -0700194/*
195 * Table of functions for each board to handle memory. Mantaining parallelism
196 * is a *very* good idea here. The idea is for the runtime code to blindly call
197 * these functions, not knowing/caring about the underlying hardware. This
198 * stuff should contain no conditionals; if more functionality is needed a
199 * different entry should be established. These calls are the interface calls
200 * and are the only functions that should be accessed. Anyone caught making
201 * direct calls deserves what they get.
202 */
Alan Coxf2cf8e22005-09-06 15:16:44 -0700203static void memwinon(struct board_info *b, unsigned int win)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -0700205 b->memwinon(b, win);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206}
207
Alan Coxf2cf8e22005-09-06 15:16:44 -0700208static void memwinoff(struct board_info *b, unsigned int win)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209{
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -0700210 b->memwinoff(b, win);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211}
212
Alan Coxf2cf8e22005-09-06 15:16:44 -0700213static void globalwinon(struct channel *ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214{
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -0700215 ch->board->globalwinon(ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216}
217
Alan Coxf2cf8e22005-09-06 15:16:44 -0700218static void rxwinon(struct channel *ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219{
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -0700220 ch->board->rxwinon(ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221}
222
Alan Coxf2cf8e22005-09-06 15:16:44 -0700223static void txwinon(struct channel *ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224{
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -0700225 ch->board->txwinon(ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226}
227
Alan Coxf2cf8e22005-09-06 15:16:44 -0700228static void memoff(struct channel *ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229{
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -0700230 ch->board->memoff(ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231}
Alan Coxf2cf8e22005-09-06 15:16:44 -0700232static void assertgwinon(struct channel *ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233{
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -0700234 ch->board->assertgwinon(ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235}
236
Alan Coxf2cf8e22005-09-06 15:16:44 -0700237static void assertmemoff(struct channel *ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238{
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -0700239 ch->board->assertmemoff(ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240}
241
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -0700242/* PCXEM windowing is the same as that used in the PCXR and CX series cards. */
Alan Coxf2cf8e22005-09-06 15:16:44 -0700243static void pcxem_memwinon(struct board_info *b, unsigned int win)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244{
Alan Cox191260a2008-04-30 00:54:16 -0700245 outb_p(FEPWIN | win, b->port + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246}
247
Alan Coxf2cf8e22005-09-06 15:16:44 -0700248static void pcxem_memwinoff(struct board_info *b, unsigned int win)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249{
Alan Coxf2cf8e22005-09-06 15:16:44 -0700250 outb_p(0, b->port + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251}
252
Alan Coxf2cf8e22005-09-06 15:16:44 -0700253static void pcxem_globalwinon(struct channel *ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254{
Alan Cox191260a2008-04-30 00:54:16 -0700255 outb_p(FEPWIN, (int)ch->board->port + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256}
257
Alan Coxf2cf8e22005-09-06 15:16:44 -0700258static void pcxem_rxwinon(struct channel *ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259{
260 outb_p(ch->rxwin, (int)ch->board->port + 1);
261}
262
Alan Coxf2cf8e22005-09-06 15:16:44 -0700263static void pcxem_txwinon(struct channel *ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264{
265 outb_p(ch->txwin, (int)ch->board->port + 1);
266}
267
Alan Coxf2cf8e22005-09-06 15:16:44 -0700268static void pcxem_memoff(struct channel *ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269{
270 outb_p(0, (int)ch->board->port + 1);
271}
272
273/* ----------------- Begin pcxe memory window stuff ------------------ */
Alan Coxf2cf8e22005-09-06 15:16:44 -0700274static void pcxe_memwinon(struct board_info *b, unsigned int win)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275{
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -0700276 outb_p(FEPWIN | win, b->port + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277}
278
Alan Coxf2cf8e22005-09-06 15:16:44 -0700279static void pcxe_memwinoff(struct board_info *b, unsigned int win)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280{
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -0700281 outb_p(inb(b->port) & ~FEPMEM, b->port + 1);
Alan Coxf2cf8e22005-09-06 15:16:44 -0700282 outb_p(0, b->port + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283}
284
Alan Coxf2cf8e22005-09-06 15:16:44 -0700285static void pcxe_globalwinon(struct channel *ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286{
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -0700287 outb_p(FEPWIN, (int)ch->board->port + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288}
289
Alan Coxf2cf8e22005-09-06 15:16:44 -0700290static void pcxe_rxwinon(struct channel *ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291{
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -0700292 outb_p(ch->rxwin, (int)ch->board->port + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293}
294
Alan Coxf2cf8e22005-09-06 15:16:44 -0700295static void pcxe_txwinon(struct channel *ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296{
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -0700297 outb_p(ch->txwin, (int)ch->board->port + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298}
299
Alan Coxf2cf8e22005-09-06 15:16:44 -0700300static void pcxe_memoff(struct channel *ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301{
302 outb_p(0, (int)ch->board->port);
303 outb_p(0, (int)ch->board->port + 1);
304}
305
306/* ------------- Begin pc64xe and pcxi memory window stuff -------------- */
Alan Coxf2cf8e22005-09-06 15:16:44 -0700307static void pcxi_memwinon(struct board_info *b, unsigned int win)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308{
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -0700309 outb_p(inb(b->port) | FEPMEM, b->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310}
311
Alan Coxf2cf8e22005-09-06 15:16:44 -0700312static void pcxi_memwinoff(struct board_info *b, unsigned int win)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313{
Alan Coxf2cf8e22005-09-06 15:16:44 -0700314 outb_p(inb(b->port) & ~FEPMEM, b->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315}
316
Alan Coxf2cf8e22005-09-06 15:16:44 -0700317static void pcxi_globalwinon(struct channel *ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318{
Alan Coxf2cf8e22005-09-06 15:16:44 -0700319 outb_p(FEPMEM, ch->board->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320}
321
Alan Coxf2cf8e22005-09-06 15:16:44 -0700322static void pcxi_rxwinon(struct channel *ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323{
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -0700324 outb_p(FEPMEM, ch->board->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325}
326
Alan Coxf2cf8e22005-09-06 15:16:44 -0700327static void pcxi_txwinon(struct channel *ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328{
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -0700329 outb_p(FEPMEM, ch->board->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330}
331
Alan Coxf2cf8e22005-09-06 15:16:44 -0700332static void pcxi_memoff(struct channel *ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333{
Alan Coxf2cf8e22005-09-06 15:16:44 -0700334 outb_p(0, ch->board->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335}
336
Alan Coxf2cf8e22005-09-06 15:16:44 -0700337static void pcxi_assertgwinon(struct channel *ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338{
Alan Coxf2cf8e22005-09-06 15:16:44 -0700339 epcaassert(inb(ch->board->port) & FEPMEM, "Global memory off");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340}
341
Alan Coxf2cf8e22005-09-06 15:16:44 -0700342static void pcxi_assertmemoff(struct channel *ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343{
Alan Coxf2cf8e22005-09-06 15:16:44 -0700344 epcaassert(!(inb(ch->board->port) & FEPMEM), "Memory on");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345}
346
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -0700347/*
348 * Not all of the cards need specific memory windowing routines. Some cards
349 * (Such as PCI) needs no windowing routines at all. We provide these do
350 * nothing routines so that the same code base can be used. The driver will
351 * ALWAYS call a windowing routine if it thinks it needs to; regardless of the
352 * card. However, dependent on the card the routine may or may not do anything.
353 */
Alan Coxf2cf8e22005-09-06 15:16:44 -0700354static void dummy_memwinon(struct board_info *b, unsigned int win)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355{
356}
357
Alan Coxf2cf8e22005-09-06 15:16:44 -0700358static void dummy_memwinoff(struct board_info *b, unsigned int win)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359{
360}
361
Alan Coxf2cf8e22005-09-06 15:16:44 -0700362static void dummy_globalwinon(struct channel *ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363{
364}
365
Alan Coxf2cf8e22005-09-06 15:16:44 -0700366static void dummy_rxwinon(struct channel *ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367{
368}
369
Alan Coxf2cf8e22005-09-06 15:16:44 -0700370static void dummy_txwinon(struct channel *ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371{
372}
373
Alan Coxf2cf8e22005-09-06 15:16:44 -0700374static void dummy_memoff(struct channel *ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375{
376}
377
Alan Coxf2cf8e22005-09-06 15:16:44 -0700378static void dummy_assertgwinon(struct channel *ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379{
380}
381
Alan Coxf2cf8e22005-09-06 15:16:44 -0700382static void dummy_assertmemoff(struct channel *ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383{
384}
385
Alan Coxf2cf8e22005-09-06 15:16:44 -0700386static struct channel *verifyChannel(struct tty_struct *tty)
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -0700387{
388 /*
389 * This routine basically provides a sanity check. It insures that the
390 * channel returned is within the proper range of addresses as well as
391 * properly initialized. If some bogus info gets passed in
392 * through tty->driver_data this should catch it.
393 */
Alan Coxf2cf8e22005-09-06 15:16:44 -0700394 if (tty) {
395 struct channel *ch = (struct channel *)tty->driver_data;
Alan Cox191260a2008-04-30 00:54:16 -0700396 if (ch >= &digi_channels[0] && ch < &digi_channels[nbdevs]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 if (ch->magic == EPCA_MAGIC)
398 return ch;
399 }
Alan Coxf2cf8e22005-09-06 15:16:44 -0700400 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 return NULL;
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -0700402}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
Alan Coxf2cf8e22005-09-06 15:16:44 -0700404static void pc_sched_event(struct channel *ch, int event)
405{
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -0700406 /*
407 * We call this to schedule interrupt processing on some event. The
408 * kernel sees our request and calls the related routine in OUR driver.
409 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 ch->event |= 1 << event;
411 schedule_work(&ch->tqueue);
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -0700412}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
414static void epca_error(int line, char *msg)
Alan Coxf2cf8e22005-09-06 15:16:44 -0700415{
Alan Cox191260a2008-04-30 00:54:16 -0700416 printk(KERN_ERR "epca_error (Digi): line = %d %s\n", line, msg);
Alan Coxf2cf8e22005-09-06 15:16:44 -0700417}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -0700419static void pc_close(struct tty_struct *tty, struct file *filp)
Alan Coxf2cf8e22005-09-06 15:16:44 -0700420{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 struct channel *ch;
422 unsigned long flags;
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -0700423 /*
424 * verifyChannel returns the channel from the tty struct if it is
425 * valid. This serves as a sanity check.
426 */
Alan Cox191260a2008-04-30 00:54:16 -0700427 ch = verifyChannel(tty);
428 if (ch != NULL) {
Alan Coxf2cf8e22005-09-06 15:16:44 -0700429 spin_lock_irqsave(&epca_lock, flags);
430 if (tty_hung_up_p(filp)) {
431 spin_unlock_irqrestore(&epca_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 return;
433 }
Alan Cox52d41732008-07-16 21:55:02 +0100434 if (ch->port.count-- > 1) {
Alan Coxf2cf8e22005-09-06 15:16:44 -0700435 /* Begin channel is open more than once */
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -0700436 /*
437 * Return without doing anything. Someone might still
438 * be using the channel.
439 */
Alan Coxf2cf8e22005-09-06 15:16:44 -0700440 spin_unlock_irqrestore(&epca_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 return;
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -0700442 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 /* Port open only once go ahead with shutdown & reset */
Alan Cox52d41732008-07-16 21:55:02 +0100444 BUG_ON(ch->port.count < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -0700446 /*
447 * Let the rest of the driver know the channel is being closed.
448 * This becomes important if an open is attempted before close
449 * is finished.
450 */
Alan Cox52d41732008-07-16 21:55:02 +0100451 ch->port.flags |= ASYNC_CLOSING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 tty->closing = 1;
453
Alan Coxf2cf8e22005-09-06 15:16:44 -0700454 spin_unlock_irqrestore(&epca_lock, flags);
455
Alan Cox52d41732008-07-16 21:55:02 +0100456 if (ch->port.flags & ASYNC_INITIALIZED) {
Alan Cox191260a2008-04-30 00:54:16 -0700457 /* Setup an event to indicate when the
458 transmit buffer empties */
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -0700459 setup_empty_event(tty, ch);
Alan Cox191260a2008-04-30 00:54:16 -0700460 /* 30 seconds timeout */
461 tty_wait_until_sent(tty, 3000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 }
Alan Cox978e5952008-04-30 00:53:59 -0700463 pc_flush_buffer(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
465 tty_ldisc_flush(tty);
466 shutdown(ch);
Alan Coxf2cf8e22005-09-06 15:16:44 -0700467
468 spin_lock_irqsave(&epca_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 tty->closing = 0;
470 ch->event = 0;
Alan Cox52d41732008-07-16 21:55:02 +0100471 ch->port.tty = NULL;
Alan Coxf2cf8e22005-09-06 15:16:44 -0700472 spin_unlock_irqrestore(&epca_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
Alan Cox52d41732008-07-16 21:55:02 +0100474 if (ch->port.blocked_open) {
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -0700475 if (ch->close_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 msleep_interruptible(jiffies_to_msecs(ch->close_delay));
Alan Cox52d41732008-07-16 21:55:02 +0100477 wake_up_interruptible(&ch->port.open_wait);
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -0700478 }
Alan Cox52d41732008-07-16 21:55:02 +0100479 ch->port.flags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_INITIALIZED |
Alan Cox191260a2008-04-30 00:54:16 -0700480 ASYNC_CLOSING);
Alan Cox52d41732008-07-16 21:55:02 +0100481 wake_up_interruptible(&ch->port.close_wait);
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -0700482 }
483}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
485static void shutdown(struct channel *ch)
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -0700486{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 unsigned long flags;
488 struct tty_struct *tty;
Al Virobc9a5152005-09-15 22:53:28 +0100489 struct board_chan __iomem *bc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
Alan Cox52d41732008-07-16 21:55:02 +0100491 if (!(ch->port.flags & ASYNC_INITIALIZED))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 return;
493
Alan Coxf2cf8e22005-09-06 15:16:44 -0700494 spin_lock_irqsave(&epca_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
Alan Coxf2cf8e22005-09-06 15:16:44 -0700496 globalwinon(ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 bc = ch->brdchan;
498
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -0700499 /*
500 * In order for an event to be generated on the receipt of data the
501 * idata flag must be set. Since we are shutting down, this is not
502 * necessary clear this flag.
503 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 if (bc)
Alan Coxf2cf8e22005-09-06 15:16:44 -0700505 writeb(0, &bc->idata);
Alan Cox52d41732008-07-16 21:55:02 +0100506 tty = ch->port.tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -0700508 /* If we're a modem control device and HUPCL is on, drop RTS & DTR. */
Alan Coxf2cf8e22005-09-06 15:16:44 -0700509 if (tty->termios->c_cflag & HUPCL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 ch->omodem &= ~(ch->m_rts | ch->m_dtr);
511 fepcmd(ch, SETMODEM, 0, ch->m_dtr | ch->m_rts, 10, 1);
512 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 memoff(ch);
514
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -0700515 /*
516 * The channel has officialy been closed. The next time it is opened it
517 * will have to reinitialized. Set a flag to indicate this.
518 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 /* Prevent future Digi programmed interrupts from coming active */
Alan Cox52d41732008-07-16 21:55:02 +0100520 ch->port.flags &= ~ASYNC_INITIALIZED;
Alan Coxf2cf8e22005-09-06 15:16:44 -0700521 spin_unlock_irqrestore(&epca_lock, flags);
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -0700522}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
524static void pc_hangup(struct tty_struct *tty)
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -0700525{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 struct channel *ch;
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -0700527 /*
528 * verifyChannel returns the channel from the tty struct if it is
529 * valid. This serves as a sanity check.
530 */
Alan Cox191260a2008-04-30 00:54:16 -0700531 ch = verifyChannel(tty);
532 if (ch != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 unsigned long flags;
534
Alan Cox978e5952008-04-30 00:53:59 -0700535 pc_flush_buffer(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 tty_ldisc_flush(tty);
537 shutdown(ch);
538
Alan Coxf2cf8e22005-09-06 15:16:44 -0700539 spin_lock_irqsave(&epca_lock, flags);
Alan Cox52d41732008-07-16 21:55:02 +0100540 ch->port.tty = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 ch->event = 0;
Alan Cox52d41732008-07-16 21:55:02 +0100542 ch->port.count = 0;
543 ch->port.flags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_INITIALIZED);
Alan Coxf2cf8e22005-09-06 15:16:44 -0700544 spin_unlock_irqrestore(&epca_lock, flags);
Alan Cox52d41732008-07-16 21:55:02 +0100545 wake_up_interruptible(&ch->port.open_wait);
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -0700546 }
547}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -0700549static int pc_write(struct tty_struct *tty,
Alan Cox191260a2008-04-30 00:54:16 -0700550 const unsigned char *buf, int bytesAvailable)
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -0700551{
Alan Coxf2cf8e22005-09-06 15:16:44 -0700552 unsigned int head, tail;
553 int dataLen;
554 int size;
555 int amountCopied;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 struct channel *ch;
557 unsigned long flags;
558 int remain;
Al Virobc9a5152005-09-15 22:53:28 +0100559 struct board_chan __iomem *bc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -0700561 /*
562 * pc_write is primarily called directly by the kernel routine
563 * tty_write (Though it can also be called by put_char) found in
564 * tty_io.c. pc_write is passed a line discipline buffer where the data
565 * to be written out is stored. The line discipline implementation
566 * itself is done at the kernel level and is not brought into the
567 * driver.
568 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -0700570 /*
571 * verifyChannel returns the channel from the tty struct if it is
572 * valid. This serves as a sanity check.
573 */
Alan Cox191260a2008-04-30 00:54:16 -0700574 ch = verifyChannel(tty);
575 if (ch == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 return 0;
577
578 /* Make a pointer to the channel data structure found on the board. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 bc = ch->brdchan;
580 size = ch->txbufsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 amountCopied = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
Alan Coxf2cf8e22005-09-06 15:16:44 -0700583 spin_lock_irqsave(&epca_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 globalwinon(ch);
585
Alan Coxf2cf8e22005-09-06 15:16:44 -0700586 head = readw(&bc->tin) & (size - 1);
587 tail = readw(&bc->tout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
Alan Coxf2cf8e22005-09-06 15:16:44 -0700589 if (tail != readw(&bc->tout))
590 tail = readw(&bc->tout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 tail &= (size - 1);
592
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -0700593 if (head >= tail) {
594 /* head has not wrapped */
595 /*
596 * remain (much like dataLen above) represents the total amount
597 * of space available on the card for data. Here dataLen
598 * represents the space existing between the head pointer and
599 * the end of buffer. This is important because a memcpy cannot
600 * be told to automatically wrap around when it hits the buffer
601 * end.
602 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 dataLen = size - head;
604 remain = size - (head - tail) - 1;
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -0700605 } else {
606 /* head has wrapped around */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 remain = tail - head - 1;
608 dataLen = remain;
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -0700609 }
610 /*
611 * Check the space on the card. If we have more data than space; reduce
612 * the amount of data to fit the space.
613 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 bytesAvailable = min(remain, bytesAvailable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 txwinon(ch);
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -0700616 while (bytesAvailable > 0) {
617 /* there is data to copy onto card */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -0700619 /*
620 * If head is not wrapped, the below will make sure the first
621 * data copy fills to the end of card buffer.
622 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 dataLen = min(bytesAvailable, dataLen);
Al Virobc9a5152005-09-15 22:53:28 +0100624 memcpy_toio(ch->txptr + head, buf, dataLen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 buf += dataLen;
626 head += dataLen;
627 amountCopied += dataLen;
628 bytesAvailable -= dataLen;
629
Alan Coxf2cf8e22005-09-06 15:16:44 -0700630 if (head >= size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 head = 0;
632 dataLen = tail;
633 }
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -0700634 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 ch->statusflags |= TXBUSY;
636 globalwinon(ch);
Alan Coxf2cf8e22005-09-06 15:16:44 -0700637 writew(head, &bc->tin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
Alan Coxf2cf8e22005-09-06 15:16:44 -0700639 if ((ch->statusflags & LOWWAIT) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 ch->statusflags |= LOWWAIT;
Alan Coxf2cf8e22005-09-06 15:16:44 -0700641 writeb(1, &bc->ilow);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 }
643 memoff(ch);
Alan Coxf2cf8e22005-09-06 15:16:44 -0700644 spin_unlock_irqrestore(&epca_lock, flags);
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -0700645 return amountCopied;
646}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648static int pc_write_room(struct tty_struct *tty)
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -0700649{
Alan Cox191260a2008-04-30 00:54:16 -0700650 int remain = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 struct channel *ch;
652 unsigned long flags;
653 unsigned int head, tail;
Al Virobc9a5152005-09-15 22:53:28 +0100654 struct board_chan __iomem *bc;
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -0700655 /*
656 * verifyChannel returns the channel from the tty struct if it is
657 * valid. This serves as a sanity check.
658 */
Alan Cox191260a2008-04-30 00:54:16 -0700659 ch = verifyChannel(tty);
660 if (ch != NULL) {
Alan Coxf2cf8e22005-09-06 15:16:44 -0700661 spin_lock_irqsave(&epca_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 globalwinon(ch);
663
664 bc = ch->brdchan;
Alan Coxf2cf8e22005-09-06 15:16:44 -0700665 head = readw(&bc->tin) & (ch->txbufsize - 1);
666 tail = readw(&bc->tout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
Alan Coxf2cf8e22005-09-06 15:16:44 -0700668 if (tail != readw(&bc->tout))
669 tail = readw(&bc->tout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 /* Wrap tail if necessary */
671 tail &= (ch->txbufsize - 1);
Alan Cox191260a2008-04-30 00:54:16 -0700672 remain = tail - head - 1;
673 if (remain < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 remain += ch->txbufsize;
675
Alan Coxf2cf8e22005-09-06 15:16:44 -0700676 if (remain && (ch->statusflags & LOWWAIT) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 ch->statusflags |= LOWWAIT;
Alan Coxf2cf8e22005-09-06 15:16:44 -0700678 writeb(1, &bc->ilow);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 }
680 memoff(ch);
Alan Coxf2cf8e22005-09-06 15:16:44 -0700681 spin_unlock_irqrestore(&epca_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 /* Return how much room is left on card */
684 return remain;
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -0700685}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
687static int pc_chars_in_buffer(struct tty_struct *tty)
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -0700688{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 int chars;
690 unsigned int ctail, head, tail;
691 int remain;
692 unsigned long flags;
693 struct channel *ch;
Al Virobc9a5152005-09-15 22:53:28 +0100694 struct board_chan __iomem *bc;
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -0700695 /*
696 * verifyChannel returns the channel from the tty struct if it is
697 * valid. This serves as a sanity check.
698 */
Alan Cox191260a2008-04-30 00:54:16 -0700699 ch = verifyChannel(tty);
700 if (ch == NULL)
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -0700701 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
Alan Coxf2cf8e22005-09-06 15:16:44 -0700703 spin_lock_irqsave(&epca_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 globalwinon(ch);
705
706 bc = ch->brdchan;
Alan Coxf2cf8e22005-09-06 15:16:44 -0700707 tail = readw(&bc->tout);
708 head = readw(&bc->tin);
709 ctail = readw(&ch->mailbox->cout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
Alan Cox191260a2008-04-30 00:54:16 -0700711 if (tail == head && readw(&ch->mailbox->cin) == ctail &&
712 readb(&bc->tbusy) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 chars = 0;
Alan Coxf2cf8e22005-09-06 15:16:44 -0700714 else { /* Begin if some space on the card has been used */
715 head = readw(&bc->tin) & (ch->txbufsize - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 tail &= (ch->txbufsize - 1);
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -0700717 /*
718 * The logic here is basically opposite of the above
719 * pc_write_room here we are finding the amount of bytes in the
720 * buffer filled. Not the amount of bytes empty.
721 */
Alan Cox191260a2008-04-30 00:54:16 -0700722 remain = tail - head - 1;
723 if (remain < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 remain += ch->txbufsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 chars = (int)(ch->txbufsize - remain);
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -0700726 /*
727 * Make it possible to wakeup anything waiting for output in
728 * tty_ioctl.c, etc.
729 *
730 * If not already set. Setup an event to indicate when the
731 * transmit buffer empties.
732 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 if (!(ch->statusflags & EMPTYWAIT))
Alan Cox191260a2008-04-30 00:54:16 -0700734 setup_empty_event(tty, ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 } /* End if some space on the card has been used */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 memoff(ch);
Alan Coxf2cf8e22005-09-06 15:16:44 -0700737 spin_unlock_irqrestore(&epca_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 /* Return number of characters residing on card. */
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -0700739 return chars;
740}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
742static void pc_flush_buffer(struct tty_struct *tty)
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -0700743{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 unsigned int tail;
745 unsigned long flags;
746 struct channel *ch;
Al Virobc9a5152005-09-15 22:53:28 +0100747 struct board_chan __iomem *bc;
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -0700748 /*
749 * verifyChannel returns the channel from the tty struct if it is
750 * valid. This serves as a sanity check.
751 */
Alan Cox191260a2008-04-30 00:54:16 -0700752 ch = verifyChannel(tty);
753 if (ch == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 return;
755
Alan Coxf2cf8e22005-09-06 15:16:44 -0700756 spin_lock_irqsave(&epca_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 globalwinon(ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 bc = ch->brdchan;
Alan Coxf2cf8e22005-09-06 15:16:44 -0700759 tail = readw(&bc->tout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 /* Have FEP move tout pointer; effectively flushing transmit buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 fepcmd(ch, STOUT, (unsigned) tail, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 memoff(ch);
Alan Coxf2cf8e22005-09-06 15:16:44 -0700763 spin_unlock_irqrestore(&epca_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 tty_wakeup(tty);
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -0700765}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
767static void pc_flush_chars(struct tty_struct *tty)
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -0700768{
769 struct channel *ch;
770 /*
771 * verifyChannel returns the channel from the tty struct if it is
772 * valid. This serves as a sanity check.
773 */
Alan Cox191260a2008-04-30 00:54:16 -0700774 ch = verifyChannel(tty);
775 if (ch != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 unsigned long flags;
Alan Coxf2cf8e22005-09-06 15:16:44 -0700777 spin_lock_irqsave(&epca_lock, flags);
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -0700778 /*
779 * If not already set and the transmitter is busy setup an
780 * event to indicate when the transmit empties.
781 */
Alan Cox191260a2008-04-30 00:54:16 -0700782 if ((ch->statusflags & TXBUSY) &&
783 !(ch->statusflags & EMPTYWAIT))
784 setup_empty_event(tty, ch);
Alan Coxf2cf8e22005-09-06 15:16:44 -0700785 spin_unlock_irqrestore(&epca_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 }
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -0700787}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -0700789static int block_til_ready(struct tty_struct *tty,
Alan Cox191260a2008-04-30 00:54:16 -0700790 struct file *filp, struct channel *ch)
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -0700791{
Alan Cox191260a2008-04-30 00:54:16 -0700792 DECLARE_WAITQUEUE(wait, current);
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -0700793 int retval, do_clocal = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 unsigned long flags;
795
Alan Coxf2cf8e22005-09-06 15:16:44 -0700796 if (tty_hung_up_p(filp)) {
Alan Cox52d41732008-07-16 21:55:02 +0100797 if (ch->port.flags & ASYNC_HUP_NOTIFY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 retval = -EAGAIN;
799 else
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -0700800 retval = -ERESTARTSYS;
801 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 }
803
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -0700804 /*
805 * If the device is in the middle of being closed, then block until
806 * it's done, and then try again.
807 */
Alan Cox52d41732008-07-16 21:55:02 +0100808 if (ch->port.flags & ASYNC_CLOSING) {
809 interruptible_sleep_on(&ch->port.close_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
Alan Cox52d41732008-07-16 21:55:02 +0100811 if (ch->port.flags & ASYNC_HUP_NOTIFY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 return -EAGAIN;
813 else
814 return -ERESTARTSYS;
815 }
816
Alan Coxf2cf8e22005-09-06 15:16:44 -0700817 if (filp->f_flags & O_NONBLOCK) {
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -0700818 /*
819 * If non-blocking mode is set, then make the check up front
820 * and then exit.
821 */
Alan Cox52d41732008-07-16 21:55:02 +0100822 ch->port.flags |= ASYNC_NORMAL_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 return 0;
824 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 if (tty->termios->c_cflag & CLOCAL)
826 do_clocal = 1;
Alan Coxf2cf8e22005-09-06 15:16:44 -0700827 /* Block waiting for the carrier detect and the line to become free */
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -0700828
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 retval = 0;
Alan Cox52d41732008-07-16 21:55:02 +0100830 add_wait_queue(&ch->port.open_wait, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
Alan Coxf2cf8e22005-09-06 15:16:44 -0700832 spin_lock_irqsave(&epca_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 /* We dec count so that pc_close will know when to free things */
834 if (!tty_hung_up_p(filp))
Alan Cox52d41732008-07-16 21:55:02 +0100835 ch->port.count--;
836 ch->port.blocked_open++;
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -0700837 while (1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 set_current_state(TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 if (tty_hung_up_p(filp) ||
Alan Cox52d41732008-07-16 21:55:02 +0100840 !(ch->port.flags & ASYNC_INITIALIZED)) {
841 if (ch->port.flags & ASYNC_HUP_NOTIFY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 retval = -EAGAIN;
843 else
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -0700844 retval = -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 break;
846 }
Alan Cox52d41732008-07-16 21:55:02 +0100847 if (!(ch->port.flags & ASYNC_CLOSING) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 (do_clocal || (ch->imodem & ch->dcd)))
849 break;
Alan Coxf2cf8e22005-09-06 15:16:44 -0700850 if (signal_pending(current)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 retval = -ERESTARTSYS;
852 break;
853 }
Alan Coxf2cf8e22005-09-06 15:16:44 -0700854 spin_unlock_irqrestore(&epca_lock, flags);
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -0700855 /*
856 * Allow someone else to be scheduled. We will occasionally go
857 * through this loop until one of the above conditions change.
858 * The below schedule call will allow other processes to enter
859 * and prevent this loop from hogging the cpu.
860 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 schedule();
Alan Coxf2cf8e22005-09-06 15:16:44 -0700862 spin_lock_irqsave(&epca_lock, flags);
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -0700863 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
Milind Arun Choudharycc0a8fb2007-05-08 00:30:52 -0700865 __set_current_state(TASK_RUNNING);
Alan Cox52d41732008-07-16 21:55:02 +0100866 remove_wait_queue(&ch->port.open_wait, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 if (!tty_hung_up_p(filp))
Alan Cox52d41732008-07-16 21:55:02 +0100868 ch->port.count++;
869 ch->port.blocked_open--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
Alan Coxf2cf8e22005-09-06 15:16:44 -0700871 spin_unlock_irqrestore(&epca_lock, flags);
872
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 if (retval)
874 return retval;
875
Alan Cox52d41732008-07-16 21:55:02 +0100876 ch->port.flags |= ASYNC_NORMAL_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 return 0;
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -0700878}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
Alan Cox191260a2008-04-30 00:54:16 -0700880static int pc_open(struct tty_struct *tty, struct file *filp)
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -0700881{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 struct channel *ch;
883 unsigned long flags;
884 int line, retval, boardnum;
Al Virobc9a5152005-09-15 22:53:28 +0100885 struct board_chan __iomem *bc;
Alan Coxf2cf8e22005-09-06 15:16:44 -0700886 unsigned int head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
888 line = tty->index;
Alan Coxf2cf8e22005-09-06 15:16:44 -0700889 if (line < 0 || line >= nbdevs)
890 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
892 ch = &digi_channels[line];
893 boardnum = ch->boardnum;
894
895 /* Check status of board configured in system. */
896
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -0700897 /*
898 * I check to see if the epca_setup routine detected an user error. It
899 * might be better to put this in pc_init, but for the moment it goes
900 * here.
901 */
Alan Coxf2cf8e22005-09-06 15:16:44 -0700902 if (invalid_lilo_config) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 if (setup_error_code & INVALID_BOARD_TYPE)
Alan Coxf2cf8e22005-09-06 15:16:44 -0700904 printk(KERN_ERR "epca: pc_open: Invalid board type specified in kernel options.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 if (setup_error_code & INVALID_NUM_PORTS)
Alan Coxf2cf8e22005-09-06 15:16:44 -0700906 printk(KERN_ERR "epca: pc_open: Invalid number of ports specified in kernel options.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 if (setup_error_code & INVALID_MEM_BASE)
Alan Coxf2cf8e22005-09-06 15:16:44 -0700908 printk(KERN_ERR "epca: pc_open: Invalid board memory address specified in kernel options.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 if (setup_error_code & INVALID_PORT_BASE)
Alan Coxf2cf8e22005-09-06 15:16:44 -0700910 printk(KERN_ERR "epca; pc_open: Invalid board port address specified in kernel options.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 if (setup_error_code & INVALID_BOARD_STATUS)
Alan Coxf2cf8e22005-09-06 15:16:44 -0700912 printk(KERN_ERR "epca: pc_open: Invalid board status specified in kernel options.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 if (setup_error_code & INVALID_ALTPIN)
Alan Coxf2cf8e22005-09-06 15:16:44 -0700914 printk(KERN_ERR "epca: pc_open: Invalid board altpin specified in kernel options;\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 tty->driver_data = NULL; /* Mark this device as 'down' */
Alan Coxf2cf8e22005-09-06 15:16:44 -0700916 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 }
Alan Coxf2cf8e22005-09-06 15:16:44 -0700918 if (boardnum >= num_cards || boards[boardnum].status == DISABLED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 tty->driver_data = NULL; /* Mark this device as 'down' */
920 return(-ENODEV);
921 }
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -0700922
Harvey Harrison11fb09b2008-04-30 00:53:52 -0700923 bc = ch->brdchan;
924 if (bc == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 tty->driver_data = NULL;
Alan Coxf2cf8e22005-09-06 15:16:44 -0700926 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 }
928
Alan Coxf2cf8e22005-09-06 15:16:44 -0700929 spin_lock_irqsave(&epca_lock, flags);
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -0700930 /*
931 * Every time a channel is opened, increment a counter. This is
932 * necessary because we do not wish to flush and shutdown the channel
933 * until the last app holding the channel open, closes it.
934 */
Alan Cox52d41732008-07-16 21:55:02 +0100935 ch->port.count++;
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -0700936 /*
937 * Set a kernel structures pointer to our local channel structure. This
938 * way we can get to it when passed only a tty struct.
939 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 tty->driver_data = ch;
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -0700941 /*
942 * If this is the first time the channel has been opened, initialize
943 * the tty->termios struct otherwise let pc_close handle it.
944 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 globalwinon(ch);
946 ch->statusflags = 0;
947
948 /* Save boards current modem status */
Al Virobc9a5152005-09-15 22:53:28 +0100949 ch->imodem = readb(&bc->mstat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -0700951 /*
952 * Set receive head and tail ptrs to each other. This indicates no data
953 * available to read.
954 */
Alan Coxf2cf8e22005-09-06 15:16:44 -0700955 head = readw(&bc->rin);
956 writew(head, &bc->rout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
958 /* Set the channels associated tty structure */
Alan Cox52d41732008-07-16 21:55:02 +0100959 ch->port.tty = tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -0700961 /*
962 * The below routine generally sets up parity, baud, flow control
963 * issues, etc.... It effect both control flags and input flags.
964 */
Alan Cox191260a2008-04-30 00:54:16 -0700965 epcaparam(tty, ch);
Alan Cox52d41732008-07-16 21:55:02 +0100966 ch->port.flags |= ASYNC_INITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 memoff(ch);
Alan Coxf2cf8e22005-09-06 15:16:44 -0700968 spin_unlock_irqrestore(&epca_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969
970 retval = block_til_ready(tty, filp, ch);
971 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 return retval;
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -0700973 /*
974 * Set this again in case a hangup set it to zero while this open() was
975 * waiting for the line...
976 */
Alan Coxf2cf8e22005-09-06 15:16:44 -0700977 spin_lock_irqsave(&epca_lock, flags);
Alan Cox52d41732008-07-16 21:55:02 +0100978 ch->port.tty = tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 globalwinon(ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 /* Enable Digi Data events */
Alan Coxf2cf8e22005-09-06 15:16:44 -0700981 writeb(1, &bc->idata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 memoff(ch);
Alan Coxf2cf8e22005-09-06 15:16:44 -0700983 spin_unlock_irqrestore(&epca_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985}
986
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -0700987static int __init epca_module_init(void)
988{
989 return pc_init();
990}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991module_init(epca_module_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993static struct pci_driver epca_driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
995static void __exit epca_module_exit(void)
996{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 int count, crd;
998 struct board_info *bd;
999 struct channel *ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
1001 del_timer_sync(&epca_timer);
1002
Alan Cox191260a2008-04-30 00:54:16 -07001003 if (tty_unregister_driver(pc_driver) ||
1004 tty_unregister_driver(pc_info)) {
Alan Coxf2cf8e22005-09-06 15:16:44 -07001005 printk(KERN_WARNING "epca: cleanup_module failed to un-register tty driver\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 return;
1007 }
1008 put_tty_driver(pc_driver);
1009 put_tty_driver(pc_info);
1010
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07001011 for (crd = 0; crd < num_cards; crd++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 bd = &boards[crd];
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07001013 if (!bd) { /* sanity check */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 printk(KERN_ERR "<Error> - Digi : cleanup_module failed\n");
1015 return;
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07001016 }
Alan Coxf2cf8e22005-09-06 15:16:44 -07001017 ch = card_ptr[crd];
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07001018 for (count = 0; count < bd->numports; count++, ch++) {
Alan Cox52d41732008-07-16 21:55:02 +01001019 if (ch && ch->port.tty)
1020 tty_hangup(ch->port.tty);
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07001021 }
1022 }
1023 pci_unregister_driver(&epca_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024}
1025module_exit(epca_module_exit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026
Jeff Dikeb68e31d2006-10-02 02:17:18 -07001027static const struct tty_operations pc_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 .open = pc_open,
1029 .close = pc_close,
1030 .write = pc_write,
1031 .write_room = pc_write_room,
1032 .flush_buffer = pc_flush_buffer,
1033 .chars_in_buffer = pc_chars_in_buffer,
1034 .flush_chars = pc_flush_chars,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 .ioctl = pc_ioctl,
1036 .set_termios = pc_set_termios,
1037 .stop = pc_stop,
1038 .start = pc_start,
1039 .throttle = pc_throttle,
1040 .unthrottle = pc_unthrottle,
1041 .hangup = pc_hangup,
Alan Coxdcbf1282008-07-22 11:18:12 +01001042 .break_ctl = pc_send_break
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043};
1044
Alan Cox191260a2008-04-30 00:54:16 -07001045static int info_open(struct tty_struct *tty, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046{
1047 return 0;
1048}
1049
1050static struct tty_operations info_ops = {
1051 .open = info_open,
1052 .ioctl = info_ioctl,
1053};
1054
Alan Coxf2cf8e22005-09-06 15:16:44 -07001055static int __init pc_init(void)
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07001056{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 int crd;
1058 struct board_info *bd;
1059 unsigned char board_id = 0;
Akinobu Mitadabad052006-10-17 00:10:28 -07001060 int err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 int pci_boards_found, pci_count;
1063
1064 pci_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065
1066 pc_driver = alloc_tty_driver(MAX_ALLOC);
1067 if (!pc_driver)
Akinobu Mitadabad052006-10-17 00:10:28 -07001068 goto out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069
1070 pc_info = alloc_tty_driver(MAX_ALLOC);
Akinobu Mitadabad052006-10-17 00:10:28 -07001071 if (!pc_info)
1072 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07001074 /*
1075 * If epca_setup has not been ran by LILO set num_cards to defaults;
1076 * copy board structure defined by digiConfig into drivers board
1077 * structure. Note : If LILO has ran epca_setup then epca_setup will
1078 * handle defining num_cards as well as copying the data into the board
1079 * structure.
1080 */
1081 if (!liloconfig) {
1082 /* driver has been configured via. epcaconfig */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 nbdevs = NBDEVS;
1084 num_cards = NUMCARDS;
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07001085 memcpy(&boards, &static_boards,
1086 sizeof(struct board_info) * NUMCARDS);
1087 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07001089 /*
1090 * Note : If lilo was used to configure the driver and the ignore
1091 * epcaconfig option was choosen (digiepca=2) then nbdevs and num_cards
1092 * will equal 0 at this point. This is okay; PCI cards will still be
1093 * picked up if detected.
1094 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07001096 /*
1097 * Set up interrupt, we will worry about memory allocation in
1098 * post_fep_init.
1099 */
Alan Cox191260a2008-04-30 00:54:16 -07001100 printk(KERN_INFO "DIGI epca driver version %s loaded.\n", VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07001102 /*
1103 * NOTE : This code assumes that the number of ports found in the
1104 * boards array is correct. This could be wrong if the card in question
1105 * is PCI (And therefore has no ports entry in the boards structure.)
1106 * The rest of the information will be valid for PCI because the
1107 * beginning of pc_init scans for PCI and determines i/o and base
1108 * memory addresses. I am not sure if it is possible to read the number
1109 * of ports supported by the card prior to it being booted (Since that
1110 * is the state it is in when pc_init is run). Because it is not
1111 * possible to query the number of supported ports until after the card
1112 * has booted; we are required to calculate the card_ptrs as the card
1113 * is initialized (Inside post_fep_init). The negative thing about this
1114 * approach is that digiDload's call to GET_INFO will have a bad port
1115 * value. (Since this is called prior to post_fep_init.)
1116 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 pci_boards_found = 0;
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07001118 if (num_cards < MAXBOARDS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 pci_boards_found += init_PCI();
1120 num_cards += pci_boards_found;
1121
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 pc_driver->owner = THIS_MODULE;
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07001123 pc_driver->name = "ttyD";
1124 pc_driver->major = DIGI_MAJOR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 pc_driver->minor_start = 0;
1126 pc_driver->type = TTY_DRIVER_TYPE_SERIAL;
1127 pc_driver->subtype = SERIAL_TYPE_NORMAL;
1128 pc_driver->init_termios = tty_std_termios;
1129 pc_driver->init_termios.c_iflag = 0;
1130 pc_driver->init_termios.c_oflag = 0;
1131 pc_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
1132 pc_driver->init_termios.c_lflag = 0;
Alan Cox606d0992006-12-08 02:38:45 -08001133 pc_driver->init_termios.c_ispeed = 9600;
1134 pc_driver->init_termios.c_ospeed = 9600;
Alan Coxdcbf1282008-07-22 11:18:12 +01001135 pc_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_HARDWARE_BREAK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 tty_set_operations(pc_driver, &pc_ops);
1137
1138 pc_info->owner = THIS_MODULE;
1139 pc_info->name = "digi_ctl";
1140 pc_info->major = DIGIINFOMAJOR;
1141 pc_info->minor_start = 0;
1142 pc_info->type = TTY_DRIVER_TYPE_SERIAL;
1143 pc_info->subtype = SERIAL_TYPE_INFO;
1144 pc_info->init_termios = tty_std_termios;
1145 pc_info->init_termios.c_iflag = 0;
1146 pc_info->init_termios.c_oflag = 0;
1147 pc_info->init_termios.c_lflag = 0;
1148 pc_info->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL;
Alan Cox606d0992006-12-08 02:38:45 -08001149 pc_info->init_termios.c_ispeed = 9600;
1150 pc_info->init_termios.c_ospeed = 9600;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 pc_info->flags = TTY_DRIVER_REAL_RAW;
1152 tty_set_operations(pc_info, &info_ops);
1153
1154
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07001155 for (crd = 0; crd < num_cards; crd++) {
1156 /*
1157 * This is where the appropriate memory handlers for the
1158 * hardware is set. Everything at runtime blindly jumps through
1159 * these vectors.
1160 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
1162 /* defined in epcaconfig.h */
1163 bd = &boards[crd];
1164
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07001165 switch (bd->type) {
1166 case PCXEM:
1167 case EISAXEM:
1168 bd->memwinon = pcxem_memwinon;
1169 bd->memwinoff = pcxem_memwinoff;
1170 bd->globalwinon = pcxem_globalwinon;
1171 bd->txwinon = pcxem_txwinon;
1172 bd->rxwinon = pcxem_rxwinon;
1173 bd->memoff = pcxem_memoff;
1174 bd->assertgwinon = dummy_assertgwinon;
1175 bd->assertmemoff = dummy_assertmemoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 break;
1177
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07001178 case PCIXEM:
1179 case PCIXRJ:
1180 case PCIXR:
1181 bd->memwinon = dummy_memwinon;
1182 bd->memwinoff = dummy_memwinoff;
1183 bd->globalwinon = dummy_globalwinon;
1184 bd->txwinon = dummy_txwinon;
1185 bd->rxwinon = dummy_rxwinon;
1186 bd->memoff = dummy_memoff;
1187 bd->assertgwinon = dummy_assertgwinon;
1188 bd->assertmemoff = dummy_assertmemoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 break;
1190
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07001191 case PCXE:
1192 case PCXEVE:
1193 bd->memwinon = pcxe_memwinon;
1194 bd->memwinoff = pcxe_memwinoff;
1195 bd->globalwinon = pcxe_globalwinon;
1196 bd->txwinon = pcxe_txwinon;
1197 bd->rxwinon = pcxe_rxwinon;
1198 bd->memoff = pcxe_memoff;
1199 bd->assertgwinon = dummy_assertgwinon;
1200 bd->assertmemoff = dummy_assertmemoff;
1201 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07001203 case PCXI:
1204 case PC64XE:
1205 bd->memwinon = pcxi_memwinon;
1206 bd->memwinoff = pcxi_memwinoff;
1207 bd->globalwinon = pcxi_globalwinon;
1208 bd->txwinon = pcxi_txwinon;
1209 bd->rxwinon = pcxi_rxwinon;
1210 bd->memoff = pcxi_memoff;
1211 bd->assertgwinon = pcxi_assertgwinon;
1212 bd->assertmemoff = pcxi_assertmemoff;
1213 break;
1214
1215 default:
1216 break;
1217 }
1218
1219 /*
1220 * Some cards need a memory segment to be defined for use in
1221 * transmit and receive windowing operations. These boards are
1222 * listed in the below switch. In the case of the XI the amount
1223 * of memory on the board is variable so the memory_seg is also
1224 * variable. This code determines what they segment should be.
1225 */
1226 switch (bd->type) {
1227 case PCXE:
1228 case PCXEVE:
1229 case PC64XE:
1230 bd->memory_seg = 0xf000;
1231 break;
1232
1233 case PCXI:
1234 board_id = inb((int)bd->port);
1235 if ((board_id & 0x1) == 0x1) {
1236 /* it's an XI card */
1237 /* Is it a 64K board */
1238 if ((board_id & 0x30) == 0)
1239 bd->memory_seg = 0xf000;
1240
1241 /* Is it a 128K board */
1242 if ((board_id & 0x30) == 0x10)
1243 bd->memory_seg = 0xe000;
1244
1245 /* Is is a 256K board */
1246 if ((board_id & 0x30) == 0x20)
1247 bd->memory_seg = 0xc000;
1248
1249 /* Is it a 512K board */
1250 if ((board_id & 0x30) == 0x30)
1251 bd->memory_seg = 0x8000;
1252 } else
Alan Cox191260a2008-04-30 00:54:16 -07001253 printk(KERN_ERR "epca: Board at 0x%x doesn't appear to be an XI\n", (int)bd->port);
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07001254 break;
1255 }
1256 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
Akinobu Mitadabad052006-10-17 00:10:28 -07001258 err = tty_register_driver(pc_driver);
1259 if (err) {
1260 printk(KERN_ERR "Couldn't register Digi PC/ driver");
1261 goto out3;
1262 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
Akinobu Mitadabad052006-10-17 00:10:28 -07001264 err = tty_register_driver(pc_info);
1265 if (err) {
1266 printk(KERN_ERR "Couldn't register Digi PC/ info ");
1267 goto out4;
1268 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07001270 /* Start up the poller to check for events on all enabled boards */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 init_timer(&epca_timer);
1272 epca_timer.function = epcapoll;
1273 mod_timer(&epca_timer, jiffies + HZ/25);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 return 0;
1275
Akinobu Mitadabad052006-10-17 00:10:28 -07001276out4:
1277 tty_unregister_driver(pc_driver);
1278out3:
1279 put_tty_driver(pc_info);
1280out2:
1281 put_tty_driver(pc_driver);
1282out1:
1283 return err;
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07001284}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285
1286static void post_fep_init(unsigned int crd)
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07001287{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 int i;
Al Virobc9a5152005-09-15 22:53:28 +01001289 void __iomem *memaddr;
1290 struct global_data __iomem *gd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 struct board_info *bd;
Al Virobc9a5152005-09-15 22:53:28 +01001292 struct board_chan __iomem *bc;
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07001293 struct channel *ch;
1294 int shrinkmem = 0, lowwater;
1295
1296 /*
1297 * This call is made by the user via. the ioctl call DIGI_INIT. It is
1298 * responsible for setting up all the card specific stuff.
1299 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 bd = &boards[crd];
1301
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07001302 /*
1303 * If this is a PCI board, get the port info. Remember PCI cards do not
1304 * have entries into the epcaconfig.h file, so we can't get the number
1305 * of ports from it. Unfortunetly, this means that anyone doing a
1306 * DIGI_GETINFO before the board has booted will get an invalid number
1307 * of ports returned (It should return 0). Calls to DIGI_GETINFO after
1308 * DIGI_INIT has been called will return the proper values.
1309 */
Alan Coxf2cf8e22005-09-06 15:16:44 -07001310 if (bd->type >= PCIXEM) { /* Begin get PCI number of ports */
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07001311 /*
1312 * Below we use XEMPORTS as a memory offset regardless of which
1313 * PCI card it is. This is because all of the supported PCI
1314 * cards have the same memory offset for the channel data. This
1315 * will have to be changed if we ever develop a PCI/XE card.
1316 * NOTE : The FEP manual states that the port offset is 0xC22
1317 * as opposed to 0xC02. This is only true for PC/XE, and PC/XI
1318 * cards; not for the XEM, or CX series. On the PCI cards the
1319 * number of ports is determined by reading a ID PROM located
1320 * in the box attached to the card. The card can then determine
1321 * the index the id to determine the number of ports available.
1322 * (FYI - The id should be located at 0x1ac (And may use up to
1323 * 4 bytes if the box in question is a XEM or CX)).
1324 */
Alan Coxf2cf8e22005-09-06 15:16:44 -07001325 /* PCI cards are already remapped at this point ISA are not */
1326 bd->numports = readw(bd->re_map_membase + XEMPORTS);
Alan Cox191260a2008-04-30 00:54:16 -07001327 epcaassert(bd->numports <= 64, "PCI returned a invalid number of ports");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 nbdevs += (bd->numports);
Alan Coxf2cf8e22005-09-06 15:16:44 -07001329 } else {
1330 /* Fix up the mappings for ISA/EISA etc */
1331 /* FIXME: 64K - can we be smarter ? */
Alan Cox191260a2008-04-30 00:54:16 -07001332 bd->re_map_membase = ioremap_nocache(bd->membase, 0x10000);
Alan Coxf2cf8e22005-09-06 15:16:44 -07001333 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334
1335 if (crd != 0)
1336 card_ptr[crd] = card_ptr[crd-1] + boards[crd-1].numports;
1337 else
1338 card_ptr[crd] = &digi_channels[crd]; /* <- For card 0 only */
1339
1340 ch = card_ptr[crd];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 epcaassert(ch <= &digi_channels[nbdevs - 1], "ch out of range");
1342
Alan Coxf2cf8e22005-09-06 15:16:44 -07001343 memaddr = bd->re_map_membase;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07001345 /*
1346 * The below assignment will set bc to point at the BEGINING of the
1347 * cards channel structures. For 1 card there will be between 8 and 64
1348 * of these structures.
1349 */
Al Virobc9a5152005-09-15 22:53:28 +01001350 bc = memaddr + CHANSTRUCT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07001352 /*
1353 * The below assignment will set gd to point at the BEGINING of global
1354 * memory address 0xc00. The first data in that global memory actually
1355 * starts at address 0xc1a. The command in pointer begins at 0xd10.
1356 */
Al Virobc9a5152005-09-15 22:53:28 +01001357 gd = memaddr + GLOBAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07001359 /*
1360 * XEPORTS (address 0xc22) points at the number of channels the card
1361 * supports. (For 64XE, XI, XEM, and XR use 0xc02)
1362 */
Alan Cox191260a2008-04-30 00:54:16 -07001363 if ((bd->type == PCXEVE || bd->type == PCXE) &&
1364 (readw(memaddr + XEPORTS) < 3))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 shrinkmem = 1;
1366 if (bd->type < PCIXEM)
1367 if (!request_region((int)bd->port, 4, board_desc[bd->type]))
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07001368 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 memwinon(bd, 0);
1370
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07001371 /*
1372 * Remember ch is the main drivers channels structure, while bc is the
1373 * cards channel structure.
1374 */
1375 for (i = 0; i < bd->numports; i++, ch++, bc++) {
Alan Coxf2cf8e22005-09-06 15:16:44 -07001376 unsigned long flags;
Al Virobc9a5152005-09-15 22:53:28 +01001377 u16 tseg, rseg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07001379 ch->brdchan = bc;
1380 ch->mailbox = gd;
David Howellsc4028952006-11-22 14:57:56 +00001381 INIT_WORK(&ch->tqueue, do_softint);
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07001382 ch->board = &boards[crd];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383
Alan Coxf2cf8e22005-09-06 15:16:44 -07001384 spin_lock_irqsave(&epca_lock, flags);
1385 switch (bd->type) {
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07001386 /*
1387 * Since some of the boards use different bitmaps for
1388 * their control signals we cannot hard code these
1389 * values and retain portability. We virtualize this
1390 * data here.
1391 */
1392 case EISAXEM:
1393 case PCXEM:
1394 case PCIXEM:
1395 case PCIXRJ:
1396 case PCIXR:
1397 ch->m_rts = 0x02;
1398 ch->m_dcd = 0x80;
1399 ch->m_dsr = 0x20;
1400 ch->m_cts = 0x10;
1401 ch->m_ri = 0x40;
1402 ch->m_dtr = 0x01;
1403 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07001405 case PCXE:
1406 case PCXEVE:
1407 case PCXI:
1408 case PC64XE:
1409 ch->m_rts = 0x02;
1410 ch->m_dcd = 0x08;
1411 ch->m_dsr = 0x10;
1412 ch->m_cts = 0x20;
1413 ch->m_ri = 0x40;
1414 ch->m_dtr = 0x80;
1415 break;
1416 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417
Alan Coxf2cf8e22005-09-06 15:16:44 -07001418 if (boards[crd].altpin) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 ch->dsr = ch->m_dcd;
1420 ch->dcd = ch->m_dsr;
1421 ch->digiext.digi_flags |= DIGI_ALTPIN;
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07001422 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 ch->dcd = ch->m_dcd;
1424 ch->dsr = ch->m_dsr;
1425 }
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07001426
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 ch->boardnum = crd;
1428 ch->channelnum = i;
1429 ch->magic = EPCA_MAGIC;
Alan Cox52d41732008-07-16 21:55:02 +01001430 ch->port.tty = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431
Alan Coxf2cf8e22005-09-06 15:16:44 -07001432 if (shrinkmem) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 fepcmd(ch, SETBUFFER, 32, 0, 0, 0);
1434 shrinkmem = 0;
1435 }
1436
Al Virobc9a5152005-09-15 22:53:28 +01001437 tseg = readw(&bc->tseg);
1438 rseg = readw(&bc->rseg);
1439
Alan Coxf2cf8e22005-09-06 15:16:44 -07001440 switch (bd->type) {
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07001441 case PCIXEM:
1442 case PCIXRJ:
1443 case PCIXR:
1444 /* Cover all the 2MEG cards */
1445 ch->txptr = memaddr + ((tseg << 4) & 0x1fffff);
1446 ch->rxptr = memaddr + ((rseg << 4) & 0x1fffff);
1447 ch->txwin = FEPWIN | (tseg >> 11);
1448 ch->rxwin = FEPWIN | (rseg >> 11);
1449 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07001451 case PCXEM:
1452 case EISAXEM:
1453 /* Cover all the 32K windowed cards */
1454 /* Mask equal to window size - 1 */
1455 ch->txptr = memaddr + ((tseg << 4) & 0x7fff);
1456 ch->rxptr = memaddr + ((rseg << 4) & 0x7fff);
1457 ch->txwin = FEPWIN | (tseg >> 11);
1458 ch->rxwin = FEPWIN | (rseg >> 11);
1459 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07001461 case PCXEVE:
1462 case PCXE:
Alan Cox191260a2008-04-30 00:54:16 -07001463 ch->txptr = memaddr + (((tseg - bd->memory_seg) << 4)
1464 & 0x1fff);
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07001465 ch->txwin = FEPWIN | ((tseg - bd->memory_seg) >> 9);
Alan Cox191260a2008-04-30 00:54:16 -07001466 ch->rxptr = memaddr + (((rseg - bd->memory_seg) << 4)
1467 & 0x1fff);
1468 ch->rxwin = FEPWIN | ((rseg - bd->memory_seg) >> 9);
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07001469 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07001471 case PCXI:
1472 case PC64XE:
1473 ch->txptr = memaddr + ((tseg - bd->memory_seg) << 4);
1474 ch->rxptr = memaddr + ((rseg - bd->memory_seg) << 4);
1475 ch->txwin = ch->rxwin = 0;
1476 break;
1477 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478
1479 ch->txbufhead = 0;
Al Virobc9a5152005-09-15 22:53:28 +01001480 ch->txbufsize = readw(&bc->tmax) + 1;
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07001481
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 ch->rxbufhead = 0;
Al Virobc9a5152005-09-15 22:53:28 +01001483 ch->rxbufsize = readw(&bc->rmax) + 1;
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07001484
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 lowwater = ch->txbufsize >= 2000 ? 1024 : (ch->txbufsize / 2);
1486
1487 /* Set transmitter low water mark */
1488 fepcmd(ch, STXLWATER, lowwater, 0, 10, 0);
1489
1490 /* Set receiver low water mark */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 fepcmd(ch, SRXLWATER, (ch->rxbufsize / 4), 0, 10, 0);
1492
1493 /* Set receiver high water mark */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 fepcmd(ch, SRXHWATER, (3 * ch->rxbufsize / 4), 0, 10, 0);
1495
Alan Coxf2cf8e22005-09-06 15:16:44 -07001496 writew(100, &bc->edelay);
1497 writeb(1, &bc->idata);
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07001498
Alan Coxf2cf8e22005-09-06 15:16:44 -07001499 ch->startc = readb(&bc->startc);
1500 ch->stopc = readb(&bc->stopc);
1501 ch->startca = readb(&bc->startca);
1502 ch->stopca = readb(&bc->stopca);
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07001503
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 ch->fepcflag = 0;
1505 ch->fepiflag = 0;
1506 ch->fepoflag = 0;
1507 ch->fepstartc = 0;
1508 ch->fepstopc = 0;
1509 ch->fepstartca = 0;
1510 ch->fepstopca = 0;
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07001511
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 ch->close_delay = 50;
Alan Cox52d41732008-07-16 21:55:02 +01001513 ch->port.count = 0;
1514 ch->port.blocked_open = 0;
1515 init_waitqueue_head(&ch->port.open_wait);
1516 init_waitqueue_head(&ch->port.close_wait);
Alan Coxf2cf8e22005-09-06 15:16:44 -07001517
1518 spin_unlock_irqrestore(&epca_lock, flags);
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07001519 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07001521 printk(KERN_INFO
Alan Cox191260a2008-04-30 00:54:16 -07001522 "Digi PC/Xx Driver V%s: %s I/O = 0x%lx Mem = 0x%lx Ports = %d\n",
1523 VERSION, board_desc[bd->type], (long)bd->port,
1524 (long)bd->membase, bd->numports);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 memwinoff(bd, 0);
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07001526}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527
1528static void epcapoll(unsigned long ignored)
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07001529{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 unsigned long flags;
1531 int crd;
Alan Cox191260a2008-04-30 00:54:16 -07001532 unsigned int head, tail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 struct channel *ch;
1534 struct board_info *bd;
1535
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07001536 /*
1537 * This routine is called upon every timer interrupt. Even though the
1538 * Digi series cards are capable of generating interrupts this method
1539 * of non-looping polling is more efficient. This routine checks for
1540 * card generated events (Such as receive data, are transmit buffer
1541 * empty) and acts on those events.
1542 */
1543 for (crd = 0; crd < num_cards; crd++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 bd = &boards[crd];
1545 ch = card_ptr[crd];
1546
1547 if ((bd->status == DISABLED) || digi_poller_inhibited)
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07001548 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07001550 /*
1551 * assertmemoff is not needed here; indeed it is an empty
1552 * subroutine. It is being kept because future boards may need
1553 * this as well as some legacy boards.
1554 */
Alan Coxf2cf8e22005-09-06 15:16:44 -07001555 spin_lock_irqsave(&epca_lock, flags);
1556
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 assertmemoff(ch);
1558
1559 globalwinon(ch);
1560
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07001561 /*
1562 * In this case head and tail actually refer to the event queue
1563 * not the transmit or receive queue.
1564 */
Alan Coxf2cf8e22005-09-06 15:16:44 -07001565 head = readw(&ch->mailbox->ein);
1566 tail = readw(&ch->mailbox->eout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07001568 /* If head isn't equal to tail we have an event */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 if (head != tail)
1570 doevent(crd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 memoff(ch);
1572
Alan Coxf2cf8e22005-09-06 15:16:44 -07001573 spin_unlock_irqrestore(&epca_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 } /* End for each card */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 mod_timer(&epca_timer, jiffies + (HZ / 25));
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07001576}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577
1578static void doevent(int crd)
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07001579{
Al Virobc9a5152005-09-15 22:53:28 +01001580 void __iomem *eventbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 struct channel *ch, *chan0;
1582 static struct tty_struct *tty;
Alan Coxf2cf8e22005-09-06 15:16:44 -07001583 struct board_info *bd;
Al Virobc9a5152005-09-15 22:53:28 +01001584 struct board_chan __iomem *bc;
Alan Coxf2cf8e22005-09-06 15:16:44 -07001585 unsigned int tail, head;
1586 int event, channel;
1587 int mstat, lstat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07001589 /*
1590 * This subroutine is called by epcapoll when an event is detected
1591 * in the event queue. This routine responds to those events.
1592 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 bd = &boards[crd];
1594
1595 chan0 = card_ptr[crd];
1596 epcaassert(chan0 <= &digi_channels[nbdevs - 1], "ch out of range");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 assertgwinon(chan0);
Alan Cox191260a2008-04-30 00:54:16 -07001598 while ((tail = readw(&chan0->mailbox->eout)) !=
1599 (head = readw(&chan0->mailbox->ein))) {
1600 /* Begin while something in event queue */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 assertgwinon(chan0);
Alan Coxf2cf8e22005-09-06 15:16:44 -07001602 eventbuf = bd->re_map_membase + tail + ISTART;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 /* Get the channel the event occurred on */
Alan Coxf2cf8e22005-09-06 15:16:44 -07001604 channel = readb(eventbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 /* Get the actual event code that occurred */
Alan Coxf2cf8e22005-09-06 15:16:44 -07001606 event = readb(eventbuf + 1);
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07001607 /*
1608 * The two assignments below get the current modem status
1609 * (mstat) and the previous modem status (lstat). These are
1610 * useful becuase an event could signal a change in modem
1611 * signals itself.
1612 */
Alan Coxf2cf8e22005-09-06 15:16:44 -07001613 mstat = readb(eventbuf + 2);
1614 lstat = readb(eventbuf + 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615
1616 ch = chan0 + channel;
Alan Coxf2cf8e22005-09-06 15:16:44 -07001617 if ((unsigned)channel >= bd->numports || !ch) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 if (channel >= bd->numports)
1619 ch = chan0;
1620 bc = ch->brdchan;
1621 goto next;
1622 }
1623
Alan Cox191260a2008-04-30 00:54:16 -07001624 bc = ch->brdchan;
1625 if (bc == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 goto next;
1627
Alan Coxf2cf8e22005-09-06 15:16:44 -07001628 if (event & DATA_IND) { /* Begin DATA_IND */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 receive_data(ch);
1630 assertgwinon(ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 } /* End DATA_IND */
1632 /* else *//* Fix for DCD transition missed bug */
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07001633 if (event & MODEMCHG_IND) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 /* A modem signal change has been indicated */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 ch->imodem = mstat;
Alan Cox52d41732008-07-16 21:55:02 +01001636 if (ch->port.flags & ASYNC_CHECK_CD) {
Alan Cox191260a2008-04-30 00:54:16 -07001637 /* We are now receiving dcd */
1638 if (mstat & ch->dcd)
Alan Cox52d41732008-07-16 21:55:02 +01001639 wake_up_interruptible(&ch->port.open_wait);
Alan Cox191260a2008-04-30 00:54:16 -07001640 else /* No dcd; hangup */
1641 pc_sched_event(ch, EPCA_EVENT_HANGUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 }
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07001643 }
Alan Cox52d41732008-07-16 21:55:02 +01001644 tty = ch->port.tty;
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07001645 if (tty) {
1646 if (event & BREAK_IND) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 /* A break has been indicated */
Alan Cox33f0f882006-01-09 20:54:13 -08001648 tty_insert_flip_char(tty, 0, TTY_BREAK);
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07001649 tty_schedule_flip(tty);
1650 } else if (event & LOWTX_IND) {
1651 if (ch->statusflags & LOWWAIT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 ch->statusflags &= ~LOWWAIT;
1653 tty_wakeup(tty);
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07001654 }
1655 } else if (event & EMPTYTX_IND) {
Alan Cox191260a2008-04-30 00:54:16 -07001656 /* This event is generated by
1657 setup_empty_event */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 ch->statusflags &= ~TXBUSY;
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07001659 if (ch->statusflags & EMPTYWAIT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 ch->statusflags &= ~EMPTYWAIT;
1661 tty_wakeup(tty);
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07001662 }
1663 }
1664 }
Alan Cox191260a2008-04-30 00:54:16 -07001665next:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 globalwinon(ch);
Alan Coxf2cf8e22005-09-06 15:16:44 -07001667 BUG_ON(!bc);
1668 writew(1, &bc->idata);
1669 writew((tail + 4) & (IMAX - ISTART - 4), &chan0->mailbox->eout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 globalwinon(chan0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 } /* End while something in event queue */
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07001672}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673
1674static void fepcmd(struct channel *ch, int cmd, int word_or_byte,
Alan Cox191260a2008-04-30 00:54:16 -07001675 int byte2, int ncmds, int bytecmd)
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07001676{
Al Virobc9a5152005-09-15 22:53:28 +01001677 unchar __iomem *memaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 unsigned int head, cmdTail, cmdStart, cmdMax;
1679 long count;
1680 int n;
1681
1682 /* This is the routine in which commands may be passed to the card. */
1683
1684 if (ch->board->status == DISABLED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 assertgwinon(ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 /* Remember head (As well as max) is just an offset not a base addr */
Alan Coxf2cf8e22005-09-06 15:16:44 -07001688 head = readw(&ch->mailbox->cin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 /* cmdStart is a base address */
Alan Coxf2cf8e22005-09-06 15:16:44 -07001690 cmdStart = readw(&ch->mailbox->cstart);
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07001691 /*
1692 * We do the addition below because we do not want a max pointer
1693 * relative to cmdStart. We want a max pointer that points at the
1694 * physical end of the command queue.
1695 */
Alan Coxf2cf8e22005-09-06 15:16:44 -07001696 cmdMax = (cmdStart + 4 + readw(&ch->mailbox->cmax));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 memaddr = ch->board->re_map_membase;
1698
Alan Coxf2cf8e22005-09-06 15:16:44 -07001699 if (head >= (cmdMax - cmdStart) || (head & 03)) {
Alan Cox191260a2008-04-30 00:54:16 -07001700 printk(KERN_ERR "line %d: Out of range, cmd = %x, head = %x\n",
1701 __LINE__, cmd, head);
1702 printk(KERN_ERR "line %d: Out of range, cmdMax = %x, cmdStart = %x\n",
1703 __LINE__, cmdMax, cmdStart);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 return;
1705 }
Alan Coxf2cf8e22005-09-06 15:16:44 -07001706 if (bytecmd) {
1707 writeb(cmd, memaddr + head + cmdStart + 0);
1708 writeb(ch->channelnum, memaddr + head + cmdStart + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 /* Below word_or_byte is bits to set */
Alan Coxf2cf8e22005-09-06 15:16:44 -07001710 writeb(word_or_byte, memaddr + head + cmdStart + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 /* Below byte2 is bits to reset */
Alan Coxf2cf8e22005-09-06 15:16:44 -07001712 writeb(byte2, memaddr + head + cmdStart + 3);
1713 } else {
1714 writeb(cmd, memaddr + head + cmdStart + 0);
1715 writeb(ch->channelnum, memaddr + head + cmdStart + 1);
1716 writeb(word_or_byte, memaddr + head + cmdStart + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 head = (head + 4) & (cmdMax - cmdStart - 4);
Alan Coxf2cf8e22005-09-06 15:16:44 -07001719 writew(head, &ch->mailbox->cin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 count = FEPTIMEOUT;
1721
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07001722 for (;;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 count--;
Alan Coxf2cf8e22005-09-06 15:16:44 -07001724 if (count == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 printk(KERN_ERR "<Error> - Fep not responding in fepcmd()\n");
1726 return;
1727 }
Alan Coxf2cf8e22005-09-06 15:16:44 -07001728 head = readw(&ch->mailbox->cin);
1729 cmdTail = readw(&ch->mailbox->cout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 n = (head - cmdTail) & (cmdMax - cmdStart - 4);
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07001731 /*
1732 * Basically this will break when the FEP acknowledges the
1733 * command by incrementing cmdTail (Making it equal to head).
1734 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 if (n <= ncmds * (sizeof(short) * 4))
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07001736 break;
1737 }
1738}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07001740/*
1741 * Digi products use fields in their channels structures that are very similar
1742 * to the c_cflag and c_iflag fields typically found in UNIX termios
1743 * structures. The below three routines allow mappings between these hardware
1744 * "flags" and their respective Linux flags.
1745 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746static unsigned termios2digi_h(struct channel *ch, unsigned cflag)
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07001747{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 unsigned res = 0;
1749
Alan Coxf2cf8e22005-09-06 15:16:44 -07001750 if (cflag & CRTSCTS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 ch->digiext.digi_flags |= (RTSPACE | CTSPACE);
1752 res |= ((ch->m_cts) | (ch->m_rts));
1753 }
1754
1755 if (ch->digiext.digi_flags & RTSPACE)
1756 res |= ch->m_rts;
1757
1758 if (ch->digiext.digi_flags & DTRPACE)
1759 res |= ch->m_dtr;
1760
1761 if (ch->digiext.digi_flags & CTSPACE)
1762 res |= ch->m_cts;
1763
1764 if (ch->digiext.digi_flags & DSRPACE)
1765 res |= ch->dsr;
1766
1767 if (ch->digiext.digi_flags & DCDPACE)
1768 res |= ch->dcd;
1769
1770 if (res & (ch->m_rts))
1771 ch->digiext.digi_flags |= RTSPACE;
1772
1773 if (res & (ch->m_cts))
1774 ch->digiext.digi_flags |= CTSPACE;
1775
1776 return res;
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07001777}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779static unsigned termios2digi_i(struct channel *ch, unsigned iflag)
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07001780{
1781 unsigned res = iflag & (IGNBRK | BRKINT | IGNPAR | PARMRK |
Alan Cox191260a2008-04-30 00:54:16 -07001782 INPCK | ISTRIP | IXON | IXANY | IXOFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 if (ch->digiext.digi_flags & DIGI_AIXON)
1784 res |= IAIXON;
1785 return res;
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07001786}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787
1788static unsigned termios2digi_c(struct channel *ch, unsigned cflag)
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07001789{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 unsigned res = 0;
Alan Coxf2cf8e22005-09-06 15:16:44 -07001791 if (cflag & CBAUDEX) {
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07001792 ch->digiext.digi_flags |= DIGI_FAST;
1793 /*
1794 * HUPCL bit is used by FEP to indicate fast baud table is to
1795 * be used.
1796 */
1797 res |= FEP_HUPCL;
1798 } else
1799 ch->digiext.digi_flags &= ~DIGI_FAST;
1800 /*
1801 * CBAUD has bit position 0x1000 set these days to indicate Linux
1802 * baud rate remap. Digi hardware can't handle the bit assignment.
1803 * (We use a different bit assignment for high speed.). Clear this
1804 * bit out.
1805 */
1806 res |= cflag & ((CBAUD ^ CBAUDEX) | PARODD | PARENB | CSTOPB | CSIZE);
1807 /*
1808 * This gets a little confusing. The Digi cards have their own
Joe Perches8dfba4d2008-02-03 17:11:42 +02001809 * representation of c_cflags controlling baud rate. For the most part
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07001810 * this is identical to the Linux implementation. However; Digi
1811 * supports one rate (76800) that Linux doesn't. This means that the
1812 * c_cflag entry that would normally mean 76800 for Digi actually means
1813 * 115200 under Linux. Without the below mapping, a stty 115200 would
1814 * only drive the board at 76800. Since the rate 230400 is also found
1815 * after 76800, the same problem afflicts us when we choose a rate of
1816 * 230400. Without the below modificiation stty 230400 would actually
1817 * give us 115200.
1818 *
1819 * There are two additional differences. The Linux value for CLOCAL
1820 * (0x800; 0004000) has no meaning to the Digi hardware. Also in later
1821 * releases of Linux; the CBAUD define has CBAUDEX (0x1000; 0010000)
1822 * ored into it (CBAUD = 0x100f as opposed to 0xf). CBAUDEX should be
1823 * checked for a screened out prior to termios2digi_c returning. Since
1824 * CLOCAL isn't used by the board this can be ignored as long as the
1825 * returned value is used only by Digi hardware.
1826 */
1827 if (cflag & CBAUDEX) {
1828 /*
1829 * The below code is trying to guarantee that only baud rates
1830 * 115200 and 230400 are remapped. We use exclusive or because
1831 * the various baud rates share common bit positions and
1832 * therefore can't be tested for easily.
1833 */
1834 if ((!((cflag & 0x7) ^ (B115200 & ~CBAUDEX))) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 (!((cflag & 0x7) ^ (B230400 & ~CBAUDEX))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 res += 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 return res;
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07001839}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840
Alan Coxf2cf8e22005-09-06 15:16:44 -07001841/* Caller must hold the locks */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842static void epcaparam(struct tty_struct *tty, struct channel *ch)
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07001843{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 unsigned int cmdHead;
Alan Cox606d0992006-12-08 02:38:45 -08001845 struct ktermios *ts;
Al Virobc9a5152005-09-15 22:53:28 +01001846 struct board_chan __iomem *bc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 unsigned mval, hflow, cflag, iflag;
1848
1849 bc = ch->brdchan;
Harvey Harrison11fb09b2008-04-30 00:53:52 -07001850 epcaassert(bc != NULL, "bc out of range");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851
1852 assertgwinon(ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 ts = tty->termios;
Alan Coxf2cf8e22005-09-06 15:16:44 -07001854 if ((ts->c_cflag & CBAUD) == 0) { /* Begin CBAUD detected */
1855 cmdHead = readw(&bc->rin);
Al Virobc9a5152005-09-15 22:53:28 +01001856 writew(cmdHead, &bc->rout);
Alan Coxf2cf8e22005-09-06 15:16:44 -07001857 cmdHead = readw(&bc->tin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 /* Changing baud in mid-stream transmission can be wonderful */
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07001859 /*
1860 * Flush current transmit buffer by setting cmdTail pointer
1861 * (tout) to cmdHead pointer (tin). Hopefully the transmit
1862 * buffer is empty.
1863 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 fepcmd(ch, STOUT, (unsigned) cmdHead, 0, 0, 0);
1865 mval = 0;
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07001866 } else { /* Begin CBAUD not detected */
1867 /*
1868 * c_cflags have changed but that change had nothing to do with
1869 * BAUD. Propagate the change to the card.
1870 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 cflag = termios2digi_c(ch, ts->c_cflag);
Alan Coxf2cf8e22005-09-06 15:16:44 -07001872 if (cflag != ch->fepcflag) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 ch->fepcflag = cflag;
1874 /* Set baud rate, char size, stop bits, parity */
1875 fepcmd(ch, SETCTRLFLAGS, (unsigned) cflag, 0, 0, 0);
1876 }
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07001877 /*
1878 * If the user has not forced CLOCAL and if the device is not a
1879 * CALLOUT device (Which is always CLOCAL) we set flags such
1880 * that the driver will wait on carrier detect.
1881 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 if (ts->c_cflag & CLOCAL)
Alan Cox52d41732008-07-16 21:55:02 +01001883 ch->port.flags &= ~ASYNC_CHECK_CD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 else
Alan Cox52d41732008-07-16 21:55:02 +01001885 ch->port.flags |= ASYNC_CHECK_CD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 mval = ch->m_dtr | ch->m_rts;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 } /* End CBAUD not detected */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 iflag = termios2digi_i(ch, ts->c_iflag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 /* Check input mode flags */
Alan Coxf2cf8e22005-09-06 15:16:44 -07001890 if (iflag != ch->fepiflag) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 ch->fepiflag = iflag;
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07001892 /*
1893 * Command sets channels iflag structure on the board. Such
1894 * things as input soft flow control, handling of parity
1895 * errors, and break handling are all set here.
Alan Cox191260a2008-04-30 00:54:16 -07001896 *
1897 * break handling, parity handling, input stripping,
1898 * flow control chars
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07001899 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 fepcmd(ch, SETIFLAGS, (unsigned int) ch->fepiflag, 0, 0, 0);
1901 }
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07001902 /*
1903 * Set the board mint value for this channel. This will cause hardware
1904 * events to be generated each time the DCD signal (Described in mint)
1905 * changes.
1906 */
Alan Coxf2cf8e22005-09-06 15:16:44 -07001907 writeb(ch->dcd, &bc->mint);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 if ((ts->c_cflag & CLOCAL) || (ch->digiext.digi_flags & DIGI_FORCEDCD))
1909 if (ch->digiext.digi_flags & DIGI_FORCEDCD)
Alan Coxf2cf8e22005-09-06 15:16:44 -07001910 writeb(0, &bc->mint);
1911 ch->imodem = readb(&bc->mstat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 hflow = termios2digi_h(ch, ts->c_cflag);
Alan Coxf2cf8e22005-09-06 15:16:44 -07001913 if (hflow != ch->hflow) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 ch->hflow = hflow;
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07001915 /*
1916 * Hard flow control has been selected but the board is not
1917 * using it. Activate hard flow control now.
1918 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 fepcmd(ch, SETHFLOW, hflow, 0xff, 0, 1);
1920 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 mval ^= ch->modemfake & (mval ^ ch->modem);
1922
Alan Coxf2cf8e22005-09-06 15:16:44 -07001923 if (ch->omodem ^ mval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 ch->omodem = mval;
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07001925 /*
1926 * The below command sets the DTR and RTS mstat structure. If
1927 * hard flow control is NOT active these changes will drive the
1928 * output of the actual DTR and RTS lines. If hard flow control
1929 * is active, the changes will be saved in the mstat structure
1930 * and only asserted when hard flow control is turned off.
1931 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932
1933 /* First reset DTR & RTS; then set them */
1934 fepcmd(ch, SETMODEM, 0, ((ch->m_dtr)|(ch->m_rts)), 0, 1);
1935 fepcmd(ch, SETMODEM, mval, 0, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 }
Alan Coxf2cf8e22005-09-06 15:16:44 -07001937 if (ch->startc != ch->fepstartc || ch->stopc != ch->fepstopc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 ch->fepstartc = ch->startc;
1939 ch->fepstopc = ch->stopc;
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07001940 /*
1941 * The XON / XOFF characters have changed; propagate these
1942 * changes to the card.
1943 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 fepcmd(ch, SONOFFC, ch->fepstartc, ch->fepstopc, 0, 1);
1945 }
Alan Coxf2cf8e22005-09-06 15:16:44 -07001946 if (ch->startca != ch->fepstartca || ch->stopca != ch->fepstopca) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 ch->fepstartca = ch->startca;
1948 ch->fepstopca = ch->stopca;
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07001949 /*
1950 * Similar to the above, this time the auxilarly XON / XOFF
1951 * characters have changed; propagate these changes to the card.
1952 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 fepcmd(ch, SAUXONOFFC, ch->fepstartca, ch->fepstopca, 0, 1);
1954 }
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07001955}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956
Alan Coxf2cf8e22005-09-06 15:16:44 -07001957/* Caller holds lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958static void receive_data(struct channel *ch)
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07001959{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 unchar *rptr;
Alan Cox606d0992006-12-08 02:38:45 -08001961 struct ktermios *ts = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 struct tty_struct *tty;
Al Virobc9a5152005-09-15 22:53:28 +01001963 struct board_chan __iomem *bc;
Alan Coxf2cf8e22005-09-06 15:16:44 -07001964 int dataToRead, wrapgap, bytesAvailable;
1965 unsigned int tail, head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 unsigned int wrapmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07001968 /*
1969 * This routine is called by doint when a receive data event has taken
1970 * place.
1971 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 globalwinon(ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 if (ch->statusflags & RXSTOPPED)
1974 return;
Alan Cox52d41732008-07-16 21:55:02 +01001975 tty = ch->port.tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 if (tty)
1977 ts = tty->termios;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 bc = ch->brdchan;
Alan Coxf2cf8e22005-09-06 15:16:44 -07001979 BUG_ON(!bc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 wrapmask = ch->rxbufsize - 1;
1981
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07001982 /*
1983 * Get the head and tail pointers to the receiver queue. Wrap the head
1984 * pointer if it has reached the end of the buffer.
1985 */
Alan Coxf2cf8e22005-09-06 15:16:44 -07001986 head = readw(&bc->rin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 head &= wrapmask;
Alan Coxf2cf8e22005-09-06 15:16:44 -07001988 tail = readw(&bc->rout) & wrapmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989
1990 bytesAvailable = (head - tail) & wrapmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 if (bytesAvailable == 0)
1992 return;
1993
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07001994 /* If CREAD bit is off or device not open, set TX tail to head */
Alan Cox191260a2008-04-30 00:54:16 -07001995 if (!tty || !ts || !(ts->c_cflag & CREAD)) {
Al Virobc9a5152005-09-15 22:53:28 +01001996 writew(head, &bc->rout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 return;
1998 }
1999
Alan Cox33f0f882006-01-09 20:54:13 -08002000 if (tty_buffer_request_room(tty, bytesAvailable + 1) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 return;
2002
Alan Coxf2cf8e22005-09-06 15:16:44 -07002003 if (readb(&bc->orun)) {
2004 writeb(0, &bc->orun);
Alan Cox191260a2008-04-30 00:54:16 -07002005 printk(KERN_WARNING "epca; overrun! DigiBoard device %s\n",
2006 tty->name);
Alan Cox33f0f882006-01-09 20:54:13 -08002007 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 rxwinon(ch);
Alan Cox191260a2008-04-30 00:54:16 -07002010 while (bytesAvailable > 0) {
2011 /* Begin while there is data on the card */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 wrapgap = (head >= tail) ? head - tail : ch->rxbufsize - tail;
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07002013 /*
2014 * Even if head has wrapped around only report the amount of
2015 * data to be equal to the size - tail. Remember memcpy can't
2016 * automaticly wrap around the receive buffer.
2017 */
Alan Cox191260a2008-04-30 00:54:16 -07002018 dataToRead = (wrapgap < bytesAvailable) ? wrapgap
2019 : bytesAvailable;
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07002020 /* Make sure we don't overflow the buffer */
Alan Cox33f0f882006-01-09 20:54:13 -08002021 dataToRead = tty_prepare_flip_string(tty, &rptr, dataToRead);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 if (dataToRead == 0)
2023 break;
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07002024 /*
2025 * Move data read from our card into the line disciplines
2026 * buffer for translation if necessary.
2027 */
Alan Coxf2cf8e22005-09-06 15:16:44 -07002028 memcpy_fromio(rptr, ch->rxptr + tail, dataToRead);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 tail = (tail + dataToRead) & wrapmask;
2030 bytesAvailable -= dataToRead;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 } /* End while there is data on the card */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 globalwinon(ch);
Alan Coxf2cf8e22005-09-06 15:16:44 -07002033 writew(tail, &bc->rout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 /* Must be called with global data */
Alan Cox52d41732008-07-16 21:55:02 +01002035 tty_schedule_flip(ch->port.tty);
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07002036}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07002038static int info_ioctl(struct tty_struct *tty, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 unsigned int cmd, unsigned long arg)
2040{
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07002041 switch (cmd) {
2042 case DIGI_GETINFO:
2043 {
2044 struct digi_info di;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 int brd;
2046
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07002047 if (get_user(brd, (unsigned int __user *)arg))
Alan Coxf2cf8e22005-09-06 15:16:44 -07002048 return -EFAULT;
2049 if (brd < 0 || brd >= num_cards || num_cards == 0)
2050 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051
2052 memset(&di, 0, sizeof(di));
2053
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07002054 di.board = brd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 di.status = boards[brd].status;
2056 di.type = boards[brd].type ;
2057 di.numports = boards[brd].numports ;
Alan Coxf2cf8e22005-09-06 15:16:44 -07002058 /* Legacy fixups - just move along nothing to see */
2059 di.port = (unsigned char *)boards[brd].port ;
2060 di.membase = (unsigned char *)boards[brd].membase ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07002062 if (copy_to_user((void __user *)arg, &di, sizeof(di)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 return -EFAULT;
2064 break;
2065
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07002066 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07002068 case DIGI_POLLER:
2069 {
2070 int brd = arg & 0xff000000 >> 16;
2071 unsigned char state = arg & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072
Alan Coxf2cf8e22005-09-06 15:16:44 -07002073 if (brd < 0 || brd >= num_cards) {
2074 printk(KERN_ERR "epca: DIGI POLLER : brd not valid!\n");
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07002075 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 }
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07002077 digi_poller_inhibited = state;
2078 break;
2079 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07002081 case DIGI_INIT:
2082 {
2083 /*
2084 * This call is made by the apps to complete the
Joe Perches8dfba4d2008-02-03 17:11:42 +02002085 * initialization of the board(s). This routine is
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07002086 * responsible for setting the card to its initial
2087 * state and setting the drivers control fields to the
2088 * sutianle settings for the card in question.
2089 */
2090 int crd;
2091 for (crd = 0; crd < num_cards; crd++)
2092 post_fep_init(crd);
2093 break;
2094 }
2095 default:
2096 return -ENOTTY;
2097 }
2098 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100
2101static int pc_tiocmget(struct tty_struct *tty, struct file *file)
2102{
2103 struct channel *ch = (struct channel *) tty->driver_data;
Al Virobc9a5152005-09-15 22:53:28 +01002104 struct board_chan __iomem *bc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 unsigned int mstat, mflag = 0;
2106 unsigned long flags;
2107
2108 if (ch)
2109 bc = ch->brdchan;
2110 else
Alan Coxf2cf8e22005-09-06 15:16:44 -07002111 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112
Alan Coxf2cf8e22005-09-06 15:16:44 -07002113 spin_lock_irqsave(&epca_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 globalwinon(ch);
Alan Coxf2cf8e22005-09-06 15:16:44 -07002115 mstat = readb(&bc->mstat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 memoff(ch);
Alan Coxf2cf8e22005-09-06 15:16:44 -07002117 spin_unlock_irqrestore(&epca_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118
2119 if (mstat & ch->m_dtr)
2120 mflag |= TIOCM_DTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 if (mstat & ch->m_rts)
2122 mflag |= TIOCM_RTS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 if (mstat & ch->m_cts)
2124 mflag |= TIOCM_CTS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 if (mstat & ch->dsr)
2126 mflag |= TIOCM_DSR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 if (mstat & ch->m_ri)
2128 mflag |= TIOCM_RI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 if (mstat & ch->dcd)
2130 mflag |= TIOCM_CD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 return mflag;
2132}
2133
2134static int pc_tiocmset(struct tty_struct *tty, struct file *file,
2135 unsigned int set, unsigned int clear)
2136{
2137 struct channel *ch = (struct channel *) tty->driver_data;
2138 unsigned long flags;
2139
Alan Coxf2cf8e22005-09-06 15:16:44 -07002140 if (!ch)
2141 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142
Alan Coxf2cf8e22005-09-06 15:16:44 -07002143 spin_lock_irqsave(&epca_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 /*
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07002145 * I think this modemfake stuff is broken. It doesn't correctly reflect
2146 * the behaviour desired by the TIOCM* ioctls. Therefore this is
2147 * probably broken.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 */
2149 if (set & TIOCM_RTS) {
2150 ch->modemfake |= ch->m_rts;
2151 ch->modem |= ch->m_rts;
2152 }
2153 if (set & TIOCM_DTR) {
2154 ch->modemfake |= ch->m_dtr;
2155 ch->modem |= ch->m_dtr;
2156 }
2157 if (clear & TIOCM_RTS) {
2158 ch->modemfake |= ch->m_rts;
2159 ch->modem &= ~ch->m_rts;
2160 }
2161 if (clear & TIOCM_DTR) {
2162 ch->modemfake |= ch->m_dtr;
2163 ch->modem &= ~ch->m_dtr;
2164 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 globalwinon(ch);
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07002166 /*
2167 * The below routine generally sets up parity, baud, flow control
2168 * issues, etc.... It effect both control flags and input flags.
2169 */
Alan Cox191260a2008-04-30 00:54:16 -07002170 epcaparam(tty, ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 memoff(ch);
Alan Coxf2cf8e22005-09-06 15:16:44 -07002172 spin_unlock_irqrestore(&epca_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 return 0;
2174}
2175
Alan Cox191260a2008-04-30 00:54:16 -07002176static int pc_ioctl(struct tty_struct *tty, struct file *file,
2177 unsigned int cmd, unsigned long arg)
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07002178{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 digiflow_t dflow;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 unsigned long flags;
2181 unsigned int mflag, mstat;
2182 unsigned char startc, stopc;
Al Virobc9a5152005-09-15 22:53:28 +01002183 struct board_chan __iomem *bc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 struct channel *ch = (struct channel *) tty->driver_data;
2185 void __user *argp = (void __user *)arg;
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07002186
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 if (ch)
2188 bc = ch->brdchan;
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07002189 else
Alan Coxf2cf8e22005-09-06 15:16:44 -07002190 return -EINVAL;
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07002191 switch (cmd) {
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07002192 case TIOCMODG:
2193 mflag = pc_tiocmget(tty, file);
2194 if (put_user(mflag, (unsigned long __user *)argp))
2195 return -EFAULT;
2196 break;
2197 case TIOCMODS:
2198 if (get_user(mstat, (unsigned __user *)argp))
2199 return -EFAULT;
2200 return pc_tiocmset(tty, file, mstat, ~mstat);
2201 case TIOCSDTR:
2202 spin_lock_irqsave(&epca_lock, flags);
2203 ch->omodem |= ch->m_dtr;
2204 globalwinon(ch);
2205 fepcmd(ch, SETMODEM, ch->m_dtr, 0, 10, 1);
2206 memoff(ch);
2207 spin_unlock_irqrestore(&epca_lock, flags);
2208 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07002210 case TIOCCDTR:
2211 spin_lock_irqsave(&epca_lock, flags);
2212 ch->omodem &= ~ch->m_dtr;
2213 globalwinon(ch);
2214 fepcmd(ch, SETMODEM, 0, ch->m_dtr, 10, 1);
2215 memoff(ch);
2216 spin_unlock_irqrestore(&epca_lock, flags);
2217 break;
2218 case DIGI_GETA:
2219 if (copy_to_user(argp, &ch->digiext, sizeof(digi_t)))
2220 return -EFAULT;
2221 break;
2222 case DIGI_SETAW:
2223 case DIGI_SETAF:
Alan Cox37925e02008-04-30 00:53:17 -07002224 lock_kernel();
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07002225 if (cmd == DIGI_SETAW) {
Alan Cox191260a2008-04-30 00:54:16 -07002226 /* Setup an event to indicate when the transmit
2227 buffer empties */
Alan Coxf2cf8e22005-09-06 15:16:44 -07002228 spin_lock_irqsave(&epca_lock, flags);
Alan Cox191260a2008-04-30 00:54:16 -07002229 setup_empty_event(tty, ch);
Alan Coxf2cf8e22005-09-06 15:16:44 -07002230 spin_unlock_irqrestore(&epca_lock, flags);
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07002231 tty_wait_until_sent(tty, 0);
2232 } else {
2233 /* ldisc lock already held in ioctl */
Alan Coxa352def2008-07-16 21:53:12 +01002234 if (tty->ldisc.ops->flush_buffer)
2235 tty->ldisc.ops->flush_buffer(tty);
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07002236 }
Alan Cox37925e02008-04-30 00:53:17 -07002237 unlock_kernel();
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07002238 /* Fall Thru */
2239 case DIGI_SETA:
2240 if (copy_from_user(&ch->digiext, argp, sizeof(digi_t)))
2241 return -EFAULT;
2242
2243 if (ch->digiext.digi_flags & DIGI_ALTPIN) {
2244 ch->dcd = ch->m_dsr;
2245 ch->dsr = ch->m_dcd;
2246 } else {
2247 ch->dcd = ch->m_dcd;
2248 ch->dsr = ch->m_dsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 }
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07002250
2251 spin_lock_irqsave(&epca_lock, flags);
2252 globalwinon(ch);
2253
2254 /*
2255 * The below routine generally sets up parity, baud, flow
2256 * control issues, etc.... It effect both control flags and
2257 * input flags.
2258 */
Alan Cox191260a2008-04-30 00:54:16 -07002259 epcaparam(tty, ch);
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07002260 memoff(ch);
2261 spin_unlock_irqrestore(&epca_lock, flags);
2262 break;
2263
2264 case DIGI_GETFLOW:
2265 case DIGI_GETAFLOW:
2266 spin_lock_irqsave(&epca_lock, flags);
2267 globalwinon(ch);
2268 if (cmd == DIGI_GETFLOW) {
2269 dflow.startc = readb(&bc->startc);
2270 dflow.stopc = readb(&bc->stopc);
2271 } else {
2272 dflow.startc = readb(&bc->startca);
2273 dflow.stopc = readb(&bc->stopca);
2274 }
2275 memoff(ch);
2276 spin_unlock_irqrestore(&epca_lock, flags);
2277
2278 if (copy_to_user(argp, &dflow, sizeof(dflow)))
2279 return -EFAULT;
2280 break;
2281
2282 case DIGI_SETAFLOW:
2283 case DIGI_SETFLOW:
2284 if (cmd == DIGI_SETFLOW) {
2285 startc = ch->startc;
2286 stopc = ch->stopc;
2287 } else {
2288 startc = ch->startca;
2289 stopc = ch->stopca;
2290 }
2291
2292 if (copy_from_user(&dflow, argp, sizeof(dflow)))
2293 return -EFAULT;
2294
Alan Cox191260a2008-04-30 00:54:16 -07002295 if (dflow.startc != startc || dflow.stopc != stopc) {
2296 /* Begin if setflow toggled */
Alan Coxf2cf8e22005-09-06 15:16:44 -07002297 spin_lock_irqsave(&epca_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 globalwinon(ch);
2299
Alan Coxf2cf8e22005-09-06 15:16:44 -07002300 if (cmd == DIGI_SETFLOW) {
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07002301 ch->fepstartc = ch->startc = dflow.startc;
2302 ch->fepstopc = ch->stopc = dflow.stopc;
Alan Cox191260a2008-04-30 00:54:16 -07002303 fepcmd(ch, SONOFFC, ch->fepstartc,
2304 ch->fepstopc, 0, 1);
Alan Coxf2cf8e22005-09-06 15:16:44 -07002305 } else {
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07002306 ch->fepstartca = ch->startca = dflow.startc;
2307 ch->fepstopca = ch->stopca = dflow.stopc;
Alan Cox191260a2008-04-30 00:54:16 -07002308 fepcmd(ch, SAUXONOFFC, ch->fepstartca,
2309 ch->fepstopca, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 }
2311
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07002312 if (ch->statusflags & TXSTOPPED)
2313 pc_start(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07002315 memoff(ch);
2316 spin_unlock_irqrestore(&epca_lock, flags);
2317 } /* End if setflow toggled */
2318 break;
2319 default:
2320 return -ENOIOCTLCMD;
2321 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 return 0;
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07002323}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324
Alan Cox606d0992006-12-08 02:38:45 -08002325static void pc_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07002326{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 struct channel *ch;
2328 unsigned long flags;
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07002329 /*
2330 * verifyChannel returns the channel from the tty struct if it is
2331 * valid. This serves as a sanity check.
2332 */
Alan Cox191260a2008-04-30 00:54:16 -07002333 ch = verifyChannel(tty);
2334
2335 if (ch != NULL) { /* Begin if channel valid */
Alan Coxf2cf8e22005-09-06 15:16:44 -07002336 spin_lock_irqsave(&epca_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 globalwinon(ch);
2338 epcaparam(tty, ch);
2339 memoff(ch);
Alan Coxf2cf8e22005-09-06 15:16:44 -07002340 spin_unlock_irqrestore(&epca_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341
2342 if ((old_termios->c_cflag & CRTSCTS) &&
2343 ((tty->termios->c_cflag & CRTSCTS) == 0))
2344 tty->hw_stopped = 0;
2345
2346 if (!(old_termios->c_cflag & CLOCAL) &&
2347 (tty->termios->c_cflag & CLOCAL))
Alan Cox52d41732008-07-16 21:55:02 +01002348 wake_up_interruptible(&ch->port.open_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 } /* End if channel valid */
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07002351}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352
David Howellsc4028952006-11-22 14:57:56 +00002353static void do_softint(struct work_struct *work)
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07002354{
David Howellsc4028952006-11-22 14:57:56 +00002355 struct channel *ch = container_of(work, struct channel, tqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 /* Called in response to a modem change event */
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07002357 if (ch && ch->magic == EPCA_MAGIC) {
Alan Cox52d41732008-07-16 21:55:02 +01002358 struct tty_struct *tty = ch->port.tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359
Alan Coxf2cf8e22005-09-06 15:16:44 -07002360 if (tty && tty->driver_data) {
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07002361 if (test_and_clear_bit(EPCA_EVENT_HANGUP, &ch->event)) {
Alan Cox191260a2008-04-30 00:54:16 -07002362 tty_hangup(tty);
Alan Cox52d41732008-07-16 21:55:02 +01002363 wake_up_interruptible(&ch->port.open_wait);
2364 ch->port.flags &= ~ASYNC_NORMAL_ACTIVE;
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07002365 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 }
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07002367 }
2368}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07002370/*
2371 * pc_stop and pc_start provide software flow control to the routine and the
2372 * pc_ioctl routine.
2373 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374static void pc_stop(struct tty_struct *tty)
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07002375{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 struct channel *ch;
2377 unsigned long flags;
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07002378 /*
2379 * verifyChannel returns the channel from the tty struct if it is
2380 * valid. This serves as a sanity check.
2381 */
Alan Cox191260a2008-04-30 00:54:16 -07002382 ch = verifyChannel(tty);
2383 if (ch != NULL) {
Alan Coxf2cf8e22005-09-06 15:16:44 -07002384 spin_lock_irqsave(&epca_lock, flags);
Alan Cox191260a2008-04-30 00:54:16 -07002385 if ((ch->statusflags & TXSTOPPED) == 0) {
2386 /* Begin if transmit stop requested */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 globalwinon(ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 /* STOP transmitting now !! */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 fepcmd(ch, PAUSETX, 0, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 ch->statusflags |= TXSTOPPED;
2391 memoff(ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 } /* End if transmit stop requested */
Alan Coxf2cf8e22005-09-06 15:16:44 -07002393 spin_unlock_irqrestore(&epca_lock, flags);
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07002394 }
2395}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396
2397static void pc_start(struct tty_struct *tty)
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07002398{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 struct channel *ch;
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07002400 /*
2401 * verifyChannel returns the channel from the tty struct if it is
2402 * valid. This serves as a sanity check.
2403 */
Alan Cox191260a2008-04-30 00:54:16 -07002404 ch = verifyChannel(tty);
2405 if (ch != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 unsigned long flags;
Alan Coxf2cf8e22005-09-06 15:16:44 -07002407 spin_lock_irqsave(&epca_lock, flags);
Alan Cox191260a2008-04-30 00:54:16 -07002408 /* Just in case output was resumed because of a change
2409 in Digi-flow */
2410 if (ch->statusflags & TXSTOPPED) {
2411 /* Begin transmit resume requested */
Al Virobc9a5152005-09-15 22:53:28 +01002412 struct board_chan __iomem *bc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 globalwinon(ch);
2414 bc = ch->brdchan;
2415 if (ch->statusflags & LOWWAIT)
Alan Coxf2cf8e22005-09-06 15:16:44 -07002416 writeb(1, &bc->ilow);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 /* Okay, you can start transmitting again... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 fepcmd(ch, RESUMETX, 0, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 ch->statusflags &= ~TXSTOPPED;
2420 memoff(ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 } /* End transmit resume requested */
Alan Coxf2cf8e22005-09-06 15:16:44 -07002422 spin_unlock_irqrestore(&epca_lock, flags);
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07002423 }
2424}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07002426/*
2427 * The below routines pc_throttle and pc_unthrottle are used to slow (And
2428 * resume) the receipt of data into the kernels receive buffers. The exact
2429 * occurrence of this depends on the size of the kernels receive buffer and
2430 * what the 'watermarks' are set to for that buffer. See the n_ttys.c file for
2431 * more details.
2432 */
2433static void pc_throttle(struct tty_struct *tty)
2434{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 struct channel *ch;
2436 unsigned long flags;
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07002437 /*
2438 * verifyChannel returns the channel from the tty struct if it is
2439 * valid. This serves as a sanity check.
2440 */
Alan Cox191260a2008-04-30 00:54:16 -07002441 ch = verifyChannel(tty);
2442 if (ch != NULL) {
Alan Coxf2cf8e22005-09-06 15:16:44 -07002443 spin_lock_irqsave(&epca_lock, flags);
2444 if ((ch->statusflags & RXSTOPPED) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 globalwinon(ch);
2446 fepcmd(ch, PAUSERX, 0, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 ch->statusflags |= RXSTOPPED;
2448 memoff(ch);
2449 }
Alan Coxf2cf8e22005-09-06 15:16:44 -07002450 spin_unlock_irqrestore(&epca_lock, flags);
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07002451 }
2452}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453
2454static void pc_unthrottle(struct tty_struct *tty)
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07002455{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 struct channel *ch;
2457 unsigned long flags;
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07002458 /*
2459 * verifyChannel returns the channel from the tty struct if it is
2460 * valid. This serves as a sanity check.
2461 */
Alan Cox191260a2008-04-30 00:54:16 -07002462 ch = verifyChannel(tty);
2463 if (ch != NULL) {
2464 /* Just in case output was resumed because of a change
2465 in Digi-flow */
Alan Coxf2cf8e22005-09-06 15:16:44 -07002466 spin_lock_irqsave(&epca_lock, flags);
2467 if (ch->statusflags & RXSTOPPED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 globalwinon(ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 fepcmd(ch, RESUMERX, 0, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 ch->statusflags &= ~RXSTOPPED;
2471 memoff(ch);
2472 }
Alan Coxf2cf8e22005-09-06 15:16:44 -07002473 spin_unlock_irqrestore(&epca_lock, flags);
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07002474 }
2475}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476
Alan Coxdcbf1282008-07-22 11:18:12 +01002477static int pc_send_break(struct tty_struct *tty, int msec)
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07002478{
Alan Coxdcbf1282008-07-22 11:18:12 +01002479 struct channel *ch = (struct channel *) tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 unsigned long flags;
2481
Alan Coxdcbf1282008-07-22 11:18:12 +01002482 if (msec == -1)
2483 return -EOPNOTSUPP;
2484
Alan Coxf2cf8e22005-09-06 15:16:44 -07002485 spin_lock_irqsave(&epca_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 globalwinon(ch);
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07002487 /*
2488 * Maybe I should send an infinite break here, schedule() for msec
2489 * amount of time, and then stop the break. This way, the user can't
2490 * screw up the FEP by causing digi_send_break() to be called (i.e. via
2491 * an ioctl()) more than once in msec amount of time.
2492 * Try this for now...
2493 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 fepcmd(ch, SENDBREAK, msec, 0, 10, 0);
2495 memoff(ch);
Alan Coxf2cf8e22005-09-06 15:16:44 -07002496 spin_unlock_irqrestore(&epca_lock, flags);
Alan Coxdcbf1282008-07-22 11:18:12 +01002497 return 0;
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07002498}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499
Alan Coxf2cf8e22005-09-06 15:16:44 -07002500/* Caller MUST hold the lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501static void setup_empty_event(struct tty_struct *tty, struct channel *ch)
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07002502{
Al Virobc9a5152005-09-15 22:53:28 +01002503 struct board_chan __iomem *bc = ch->brdchan;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 globalwinon(ch);
2506 ch->statusflags |= EMPTYWAIT;
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07002507 /*
2508 * When set the iempty flag request a event to be generated when the
2509 * transmit buffer is empty (If there is no BREAK in progress).
2510 */
Alan Coxf2cf8e22005-09-06 15:16:44 -07002511 writeb(1, &bc->iempty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 memoff(ch);
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07002513}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514
David Howells88e88242008-07-22 11:20:45 +01002515#ifndef MODULE
2516static void __init epca_setup(char *str, int *ints)
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07002517{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 struct board_info board;
2519 int index, loop, last;
2520 char *temp, *t2;
2521 unsigned len;
2522
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07002523 /*
2524 * If this routine looks a little strange it is because it is only
2525 * called if a LILO append command is given to boot the kernel with
2526 * parameters. In this way, we can provide the user a method of
2527 * changing his board configuration without rebuilding the kernel.
2528 */
2529 if (!liloconfig)
2530 liloconfig = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531
2532 memset(&board, 0, sizeof(board));
2533
2534 /* Assume the data is int first, later we can change it */
2535 /* I think that array position 0 of ints holds the number of args */
2536 for (last = 0, index = 1; index <= ints[0]; index++)
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07002537 switch (index) { /* Begin parse switch */
2538 case 1:
2539 board.status = ints[index];
2540 /*
2541 * We check for 2 (As opposed to 1; because 2 is a flag
2542 * instructing the driver to ignore epcaconfig.) For
2543 * this reason we check for 2.
2544 */
Alan Cox191260a2008-04-30 00:54:16 -07002545 if (board.status == 2) {
2546 /* Begin ignore epcaconfig as well as lilo cmd line */
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07002547 nbdevs = 0;
2548 num_cards = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 return;
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07002550 } /* End ignore epcaconfig as well as lilo cmd line */
2551
2552 if (board.status > 2) {
Alan Cox191260a2008-04-30 00:54:16 -07002553 printk(KERN_ERR "epca_setup: Invalid board status 0x%x\n",
2554 board.status);
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07002555 invalid_lilo_config = 1;
2556 setup_error_code |= INVALID_BOARD_STATUS;
2557 return;
2558 }
2559 last = index;
2560 break;
2561 case 2:
2562 board.type = ints[index];
2563 if (board.type >= PCIXEM) {
2564 printk(KERN_ERR "epca_setup: Invalid board type 0x%x\n", board.type);
2565 invalid_lilo_config = 1;
2566 setup_error_code |= INVALID_BOARD_TYPE;
2567 return;
2568 }
2569 last = index;
2570 break;
2571 case 3:
2572 board.altpin = ints[index];
2573 if (board.altpin > 1) {
2574 printk(KERN_ERR "epca_setup: Invalid board altpin 0x%x\n", board.altpin);
2575 invalid_lilo_config = 1;
2576 setup_error_code |= INVALID_ALTPIN;
2577 return;
2578 }
2579 last = index;
2580 break;
2581
2582 case 4:
2583 board.numports = ints[index];
2584 if (board.numports < 2 || board.numports > 256) {
2585 printk(KERN_ERR "epca_setup: Invalid board numports 0x%x\n", board.numports);
2586 invalid_lilo_config = 1;
2587 setup_error_code |= INVALID_NUM_PORTS;
2588 return;
2589 }
2590 nbdevs += board.numports;
2591 last = index;
2592 break;
2593
2594 case 5:
2595 board.port = ints[index];
2596 if (ints[index] <= 0) {
2597 printk(KERN_ERR "epca_setup: Invalid io port 0x%x\n", (unsigned int)board.port);
2598 invalid_lilo_config = 1;
2599 setup_error_code |= INVALID_PORT_BASE;
2600 return;
2601 }
2602 last = index;
2603 break;
2604
2605 case 6:
2606 board.membase = ints[index];
2607 if (ints[index] <= 0) {
Alan Cox191260a2008-04-30 00:54:16 -07002608 printk(KERN_ERR "epca_setup: Invalid memory base 0x%x\n",
2609 (unsigned int)board.membase);
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07002610 invalid_lilo_config = 1;
2611 setup_error_code |= INVALID_MEM_BASE;
2612 return;
2613 }
2614 last = index;
2615 break;
2616
2617 default:
2618 printk(KERN_ERR "<Error> - epca_setup: Too many integer parms\n");
2619 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620
2621 } /* End parse switch */
2622
Alan Coxf2cf8e22005-09-06 15:16:44 -07002623 while (str && *str) { /* Begin while there is a string arg */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 /* find the next comma or terminator */
2625 temp = str;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 /* While string is not null, and a comma hasn't been found */
2627 while (*temp && (*temp != ','))
2628 temp++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 if (!*temp)
2630 temp = NULL;
2631 else
2632 *temp++ = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 /* Set index to the number of args + 1 */
2634 index = last + 1;
2635
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07002636 switch (index) {
2637 case 1:
2638 len = strlen(str);
2639 if (strncmp("Disable", str, len) == 0)
2640 board.status = 0;
2641 else if (strncmp("Enable", str, len) == 0)
2642 board.status = 1;
2643 else {
2644 printk(KERN_ERR "epca_setup: Invalid status %s\n", str);
2645 invalid_lilo_config = 1;
2646 setup_error_code |= INVALID_BOARD_STATUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 return;
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07002648 }
2649 last = index;
2650 break;
2651
2652 case 2:
2653 for (loop = 0; loop < EPCA_NUM_TYPES; loop++)
2654 if (strcmp(board_desc[loop], str) == 0)
2655 break;
2656 /*
2657 * If the index incremented above refers to a
2658 * legitamate board type set it here.
2659 */
2660 if (index < EPCA_NUM_TYPES)
2661 board.type = loop;
2662 else {
2663 printk(KERN_ERR "epca_setup: Invalid board type: %s\n", str);
2664 invalid_lilo_config = 1;
2665 setup_error_code |= INVALID_BOARD_TYPE;
2666 return;
2667 }
2668 last = index;
2669 break;
2670
2671 case 3:
2672 len = strlen(str);
2673 if (strncmp("Disable", str, len) == 0)
2674 board.altpin = 0;
2675 else if (strncmp("Enable", str, len) == 0)
2676 board.altpin = 1;
2677 else {
2678 printk(KERN_ERR "epca_setup: Invalid altpin %s\n", str);
2679 invalid_lilo_config = 1;
2680 setup_error_code |= INVALID_ALTPIN;
2681 return;
2682 }
2683 last = index;
2684 break;
2685
2686 case 4:
2687 t2 = str;
2688 while (isdigit(*t2))
2689 t2++;
2690
2691 if (*t2) {
2692 printk(KERN_ERR "epca_setup: Invalid port count %s\n", str);
2693 invalid_lilo_config = 1;
2694 setup_error_code |= INVALID_NUM_PORTS;
2695 return;
2696 }
2697
2698 /*
2699 * There is not a man page for simple_strtoul but the
2700 * code can be found in vsprintf.c. The first argument
2701 * is the string to translate (To an unsigned long
2702 * obviously), the second argument can be the address
2703 * of any character variable or a NULL. If a variable
2704 * is given, the end pointer of the string will be
2705 * stored in that variable; if a NULL is given the end
2706 * pointer will not be returned. The last argument is
2707 * the base to use. If a 0 is indicated, the routine
2708 * will attempt to determine the proper base by looking
2709 * at the values prefix (A '0' for octal, a 'x' for
2710 * hex, etc ... If a value is given it will use that
2711 * value as the base.
2712 */
2713 board.numports = simple_strtoul(str, NULL, 0);
2714 nbdevs += board.numports;
2715 last = index;
2716 break;
2717
2718 case 5:
2719 t2 = str;
2720 while (isxdigit(*t2))
2721 t2++;
2722
2723 if (*t2) {
2724 printk(KERN_ERR "epca_setup: Invalid i/o address %s\n", str);
2725 invalid_lilo_config = 1;
2726 setup_error_code |= INVALID_PORT_BASE;
2727 return;
2728 }
2729
2730 board.port = simple_strtoul(str, NULL, 16);
2731 last = index;
2732 break;
2733
2734 case 6:
2735 t2 = str;
2736 while (isxdigit(*t2))
2737 t2++;
2738
2739 if (*t2) {
Alan Cox191260a2008-04-30 00:54:16 -07002740 printk(KERN_ERR "epca_setup: Invalid memory base %s\n", str);
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07002741 invalid_lilo_config = 1;
2742 setup_error_code |= INVALID_MEM_BASE;
2743 return;
2744 }
2745 board.membase = simple_strtoul(str, NULL, 16);
2746 last = index;
2747 break;
2748 default:
2749 printk(KERN_ERR "epca: Too many string parms\n");
2750 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751 }
2752 str = temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753 } /* End while there is a string arg */
2754
Alan Coxf2cf8e22005-09-06 15:16:44 -07002755 if (last < 6) {
2756 printk(KERN_ERR "epca: Insufficient parms specified\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757 return;
2758 }
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07002759
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 /* I should REALLY validate the stuff here */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761 /* Copies our local copy of board into boards */
Alan Cox191260a2008-04-30 00:54:16 -07002762 memcpy((void *)&boards[num_cards], (void *)&board, sizeof(board));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763 /* Does this get called once per lilo arg are what ? */
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07002764 printk(KERN_INFO "PC/Xx: Added board %i, %s %i ports at 0x%4.4X base 0x%6.6X\n",
2765 num_cards, board_desc[board.type],
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 board.numports, (int)board.port, (unsigned int) board.membase);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767 num_cards++;
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07002768}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769
David Howells88e88242008-07-22 11:20:45 +01002770static int __init epca_real_setup(char *str)
2771{
2772 int ints[11];
2773
2774 epca_setup(get_options(str, 11, ints), ints);
2775 return 1;
2776}
2777
2778__setup("digiepca", epca_real_setup);
2779#endif
2780
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781enum epic_board_types {
2782 brd_xr = 0,
2783 brd_xem,
2784 brd_cx,
2785 brd_xrj,
2786};
2787
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788/* indexed directly by epic_board_types enum */
2789static struct {
2790 unsigned char board_type;
2791 unsigned bar_idx; /* PCI base address region */
2792} epca_info_tbl[] = {
2793 { PCIXR, 0, },
2794 { PCIXEM, 0, },
2795 { PCICX, 0, },
2796 { PCIXRJ, 2, },
2797};
2798
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07002799static int __devinit epca_init_one(struct pci_dev *pdev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 const struct pci_device_id *ent)
2801{
2802 static int board_num = -1;
2803 int board_idx, info_idx = ent->driver_data;
2804 unsigned long addr;
2805
2806 if (pci_enable_device(pdev))
2807 return -EIO;
2808
2809 board_num++;
2810 board_idx = board_num + num_cards;
2811 if (board_idx >= MAXBOARDS)
2812 goto err_out;
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07002813
Alan Cox191260a2008-04-30 00:54:16 -07002814 addr = pci_resource_start(pdev, epca_info_tbl[info_idx].bar_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 if (!addr) {
Alan Cox191260a2008-04-30 00:54:16 -07002816 printk(KERN_ERR PFX "PCI region #%d not available (size 0)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817 epca_info_tbl[info_idx].bar_idx);
2818 goto err_out;
2819 }
2820
2821 boards[board_idx].status = ENABLED;
2822 boards[board_idx].type = epca_info_tbl[info_idx].board_type;
2823 boards[board_idx].numports = 0x0;
Alan Coxf2cf8e22005-09-06 15:16:44 -07002824 boards[board_idx].port = addr + PCI_IO_OFFSET;
2825 boards[board_idx].membase = addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826
Alan Cox191260a2008-04-30 00:54:16 -07002827 if (!request_mem_region(addr + PCI_IO_OFFSET, 0x200000, "epca")) {
2828 printk(KERN_ERR PFX "resource 0x%x @ 0x%lx unavailable\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 0x200000, addr + PCI_IO_OFFSET);
2830 goto err_out;
2831 }
2832
Alan Cox191260a2008-04-30 00:54:16 -07002833 boards[board_idx].re_map_port = ioremap_nocache(addr + PCI_IO_OFFSET,
2834 0x200000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 if (!boards[board_idx].re_map_port) {
Alan Cox191260a2008-04-30 00:54:16 -07002836 printk(KERN_ERR PFX "cannot map 0x%x @ 0x%lx\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 0x200000, addr + PCI_IO_OFFSET);
2838 goto err_out_free_pciio;
2839 }
2840
Alan Cox191260a2008-04-30 00:54:16 -07002841 if (!request_mem_region(addr, 0x200000, "epca")) {
2842 printk(KERN_ERR PFX "resource 0x%x @ 0x%lx unavailable\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 0x200000, addr);
2844 goto err_out_free_iounmap;
2845 }
2846
Alan Cox191260a2008-04-30 00:54:16 -07002847 boards[board_idx].re_map_membase = ioremap_nocache(addr, 0x200000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 if (!boards[board_idx].re_map_membase) {
Alan Cox191260a2008-04-30 00:54:16 -07002849 printk(KERN_ERR PFX "cannot map 0x%x @ 0x%lx\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850 0x200000, addr + PCI_IO_OFFSET);
2851 goto err_out_free_memregion;
2852 }
2853
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07002854 /*
2855 * I don't know what the below does, but the hardware guys say its
2856 * required on everything except PLX (In this case XRJ).
2857 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858 if (info_idx != brd_xrj) {
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07002859 pci_write_config_byte(pdev, 0x40, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 pci_write_config_byte(pdev, 0x46, 0);
2861 }
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07002862
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 return 0;
2864
2865err_out_free_memregion:
Alan Cox191260a2008-04-30 00:54:16 -07002866 release_mem_region(addr, 0x200000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867err_out_free_iounmap:
Alan Cox191260a2008-04-30 00:54:16 -07002868 iounmap(boards[board_idx].re_map_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869err_out_free_pciio:
Alan Cox191260a2008-04-30 00:54:16 -07002870 release_mem_region(addr + PCI_IO_OFFSET, 0x200000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871err_out:
2872 return -ENODEV;
2873}
2874
2875
2876static struct pci_device_id epca_pci_tbl[] = {
2877 { PCI_VENDOR_DIGI, PCI_DEVICE_XR, PCI_ANY_ID, PCI_ANY_ID, 0, 0, brd_xr },
2878 { PCI_VENDOR_DIGI, PCI_DEVICE_XEM, PCI_ANY_ID, PCI_ANY_ID, 0, 0, brd_xem },
2879 { PCI_VENDOR_DIGI, PCI_DEVICE_CX, PCI_ANY_ID, PCI_ANY_ID, 0, 0, brd_cx },
2880 { PCI_VENDOR_DIGI, PCI_DEVICE_XRJ, PCI_ANY_ID, PCI_ANY_ID, 0, 0, brd_xrj },
2881 { 0, }
2882};
2883
2884MODULE_DEVICE_TABLE(pci, epca_pci_tbl);
2885
Harvey Harrison11fb09b2008-04-30 00:53:52 -07002886static int __init init_PCI(void)
Alexey Dobriyanae0b78d2007-10-16 23:26:37 -07002887{
Alan Cox191260a2008-04-30 00:54:16 -07002888 memset(&epca_driver, 0, sizeof(epca_driver));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 epca_driver.name = "epca";
2890 epca_driver.id_table = epca_pci_tbl;
2891 epca_driver.probe = epca_init_one;
2892
2893 return pci_register_driver(&epca_driver);
Alan Coxf2cf8e22005-09-06 15:16:44 -07002894}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895
2896MODULE_LICENSE("GPL");